Skip to content

Commit

Permalink
mock get propagation Url
Browse files Browse the repository at this point in the history
  • Loading branch information
julioc-p committed Aug 29, 2024
1 parent 196ce7a commit 4aa86d0
Showing 1 changed file with 78 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,16 @@

package io.jenkins.plugins.bitbucketpushandpullrequest.processor;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import io.jenkins.plugins.bitbucketpushandpullrequest.BitBucketPPRJobProbe;
import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRAction;
import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig;
import io.jenkins.plugins.bitbucketpushandpullrequest.exception.BitBucketPPRPayloadPropertyNotFoundException;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRHookEvent;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerPayload;
import io.jenkins.plugins.bitbucketpushandpullrequest.observer.BitBucketPPRObservable;
import io.jenkins.plugins.bitbucketpushandpullrequest.observer.BitBucketPPRObserverFactory;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
Expand All @@ -49,101 +40,109 @@
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;


@RunWith(MockitoJUnitRunner.class)
public class BitBucketPPRPullRequestServerPayloadProcessorTest {

@Captor
private ArgumentCaptor<BitBucketPPRHookEvent> eventCaptor;
@Captor
private ArgumentCaptor<BitBucketPPRHookEvent> eventCaptor;

@Captor
private ArgumentCaptor<BitBucketPPRAction> actionCaptor;
@Captor
private ArgumentCaptor<BitBucketPPRAction> actionCaptor;

@Captor
private ArgumentCaptor<BitBucketPPRObservable> observableCaptor;
@Captor
private ArgumentCaptor<BitBucketPPRObservable> observableCaptor;

BitBucketPPRPullRequestServerPayloadProcessor pullRequestPayloadProcessor;
BitBucketPPRPullRequestServerPayloadProcessor pullRequestPayloadProcessor;


@Test
public void testProcessPayload() throws Exception {
JsonReader reader = null;
@Test
public void testProcessPayload() throws Exception {
JsonReader reader = null;

try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("./server/pr_opened.json");
assert is != null;
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
reader = new JsonReader(isr);
} catch (Exception e) {
e.printStackTrace();
}
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("./server/pr_opened.json");
assert is != null;
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
reader = new JsonReader(isr);
} catch (Exception e) {
e.printStackTrace();
}

try (MockedStatic<BitBucketPPRPluginConfig> config = Mockito.mockStatic(
BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);
try (MockedStatic<BitBucketPPRPluginConfig> config = Mockito.mockStatic(
BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);
when(c.getPropagationUrl()).thenReturn("");

BitBucketPPRJobProbe probe = mock(BitBucketPPRJobProbe.class);
BitBucketPPRJobProbe probe = mock(BitBucketPPRJobProbe.class);

Gson gson = new Gson();
assert reader != null;
BitBucketPPRPayload payload = gson.fromJson(reader, BitBucketPPRServerPayload.class);
Gson gson = new Gson();
assert reader != null;
BitBucketPPRPayload payload = gson.fromJson(reader, BitBucketPPRServerPayload.class);

BitBucketPPRHookEvent bitbucketEvent = new BitBucketPPRHookEvent("pr:opened");
BitBucketPPRHookEvent bitbucketEvent = new BitBucketPPRHookEvent("pr:opened");

pullRequestPayloadProcessor =
new BitBucketPPRPullRequestServerPayloadProcessor(probe, bitbucketEvent);
pullRequestPayloadProcessor =
new BitBucketPPRPullRequestServerPayloadProcessor(probe, bitbucketEvent);

BitBucketPPRObservable observable =
BitBucketPPRObserverFactory.createObservable(bitbucketEvent);
pullRequestPayloadProcessor.processPayload(payload, observable);
BitBucketPPRObservable observable =
BitBucketPPRObserverFactory.createObservable(bitbucketEvent);
pullRequestPayloadProcessor.processPayload(payload, observable);

verify(probe).triggerMatchingJobs(eventCaptor.capture(), actionCaptor.capture(),
observableCaptor.capture());
verify(probe).triggerMatchingJobs(eventCaptor.capture(), actionCaptor.capture(),
observableCaptor.capture());

assertEquals(bitbucketEvent, eventCaptor.getValue());
assertEquals(payload, actionCaptor.getValue().getPayload());
assertEquals(observable, observableCaptor.getValue());
}
}

@Test
public void testProcessPayloadException() throws Exception {
JsonReader reader = null;

try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("./server/pr_opened_no_clone_property.json");
assert is != null;
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
reader = new JsonReader(isr);
} catch (Exception e) {
e.printStackTrace();
assertEquals(bitbucketEvent, eventCaptor.getValue());
assertEquals(payload, actionCaptor.getValue().getPayload());
assertEquals(observable, observableCaptor.getValue());
}
}

try (MockedStatic<BitBucketPPRPluginConfig> config = Mockito.mockStatic(
BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);
@Test
public void testProcessPayloadException() throws Exception {
JsonReader reader = null;

try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("./server/pr_opened_no_clone_property.json");
assert is != null;
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
reader = new JsonReader(isr);
} catch (Exception e) {
e.printStackTrace();
}

try (MockedStatic<BitBucketPPRPluginConfig> config = Mockito.mockStatic(
BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);

BitBucketPPRJobProbe jobProbe = mock(BitBucketPPRJobProbe.class);
BitBucketPPRJobProbe jobProbe = mock(BitBucketPPRJobProbe.class);

Gson gson = new Gson();
assert reader != null;
BitBucketPPRPayload payload = gson.fromJson(reader, BitBucketPPRServerPayload.class);
Gson gson = new Gson();
assert reader != null;
BitBucketPPRPayload payload = gson.fromJson(reader, BitBucketPPRServerPayload.class);

BitBucketPPRHookEvent bitbucketEvent = new BitBucketPPRHookEvent("pr:opened");
BitBucketPPRHookEvent bitbucketEvent = new BitBucketPPRHookEvent("pr:opened");

pullRequestPayloadProcessor =
new BitBucketPPRPullRequestServerPayloadProcessor(jobProbe, bitbucketEvent);
pullRequestPayloadProcessor =
new BitBucketPPRPullRequestServerPayloadProcessor(jobProbe, bitbucketEvent);

BitBucketPPRObservable observable =
BitBucketPPRObserverFactory.createObservable(bitbucketEvent);
BitBucketPPRObservable observable =
BitBucketPPRObserverFactory.createObservable(bitbucketEvent);

Assertions.assertThrows(Exception.class, () -> {
pullRequestPayloadProcessor.processPayload(payload, observable);
});
Assertions.assertThrows(Exception.class, () -> {
pullRequestPayloadProcessor.processPayload(payload, observable);
});
}
}
}
}

0 comments on commit 4aa86d0

Please sign in to comment.