Skip to content

Commit

Permalink
use mocks instead of putting null check in execution code
Browse files Browse the repository at this point in the history
  • Loading branch information
gbhat618 committed Feb 4, 2025
1 parent 3d9c96b commit 0a3820c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/cloudbees/jenkins/GitHubWebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public List<Item> reRegisterAllHooks() {
public void doIndex(@NonNull @GHEventHeader GHEvent event, @NonNull @GHEventPayload String payload) {
var currentRequest = Stapler.getCurrentRequest2();
// during unit tests currentRequest is null
String eventGuid = currentRequest != null ? currentRequest.getHeader(X_GITHUB_DELIVERY) : null;
String eventGuid = currentRequest.getHeader(X_GITHUB_DELIVERY);
GHSubscriberEvent subscriberEvent =
new GHSubscriberEvent(eventGuid, SCMEvent.originOf(currentRequest), event, payload);
from(GHEventsSubscriber.all())
Expand Down
40 changes: 29 additions & 11 deletions src/test/java/com/cloudbees/jenkins/GitHubWebHookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.inject.Inject;

import hudson.model.Item;
import hudson.model.Job;

import org.jenkinsci.plugins.github.extension.GHEventsSubscriber;
import org.junit.Before;
Expand All @@ -12,6 +11,11 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.github.GHEvent;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest2;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.util.Set;

Expand Down Expand Up @@ -41,31 +45,45 @@ public class GitHubWebHookTest {
@Inject
private ThrowablePullRequestSubscriber throwablePullRequestSubscriber;

@Mock
private StaplerRequest2 req2;

@Before
public void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
jenkins.getInstance().getInjector().injectMembers(this);
}

@Test
public void shouldCallExtensionInterestedInIssues() throws Exception {
new GitHubWebHook().doIndex(GHEvent.ISSUES, PAYLOAD);
assertThat("should get interested event", subscriber.lastEvent(), equalTo(GHEvent.ISSUES));
try(var mockedStapler = Mockito.mockStatic(Stapler.class)) {
mockedStapler.when(Stapler::getCurrentRequest2).thenReturn(req2);

new GitHubWebHook().doIndex(GHEvent.ISSUES, PAYLOAD);
assertThat("should get interested event", subscriber.lastEvent(), equalTo(GHEvent.ISSUES));
}
}

@Test
public void shouldNotCallAnyExtensionsWithPublicEventIfNotRegistered() throws Exception {
new GitHubWebHook().doIndex(GHEvent.PUBLIC, PAYLOAD);
assertThat("should not get not interested event", subscriber.lastEvent(), nullValue());
try(var mockedStapler = Mockito.mockStatic(Stapler.class)) {
mockedStapler.when(Stapler::getCurrentRequest2).thenReturn(req2);

new GitHubWebHook().doIndex(GHEvent.PUBLIC, PAYLOAD);
assertThat("should not get not interested event", subscriber.lastEvent(), nullValue());
}
}

@Test
public void shouldCatchThrowableOnFailedSubscriber() throws Exception {
new GitHubWebHook().doIndex(GHEvent.PULL_REQUEST, PAYLOAD);
assertThat("each extension should get event",
asList(
pullRequestSubscriber.lastEvent(),
throwablePullRequestSubscriber.lastEvent()
), everyItem(equalTo(GHEvent.PULL_REQUEST)));
try(var mockedStapler = Mockito.mockStatic(Stapler.class)) {
mockedStapler.when(Stapler::getCurrentRequest2).thenReturn(req2);

new GitHubWebHook().doIndex(GHEvent.PULL_REQUEST, PAYLOAD);
assertThat("each extension should get event",
asList(pullRequestSubscriber.lastEvent(), throwablePullRequestSubscriber.lastEvent()),
everyItem(equalTo(GHEvent.PULL_REQUEST)));
}
}

@TestExtension
Expand Down

0 comments on commit 0a3820c

Please sign in to comment.