66import hudson .model .Item ;
77import hudson .plugins .git .GitSCM ;
88import org .jenkinsci .plugins .github .extension .GHSubscriberEvent ;
9+ import org .jenkinsci .plugins .github .GitHubPlugin ;
10+ import org .jenkinsci .plugins .github .config .GitHubServerConfig ;
911import org .jenkinsci .plugins .github .extension .GHEventsSubscriber ;
1012import org .jenkinsci .plugins .github .webhook .WebhookManager ;
1113import org .jenkinsci .plugins .github .webhook .WebhookManagerTest ;
1214import org .jenkinsci .plugins .github .webhook .subscriber .PingGHEventSubscriber ;
1315import org .junit .Before ;
1416import org .junit .Rule ;
1517import org .junit .Test ;
18+ import org .junit .runner .RunWith ;
1619import org .jvnet .hudson .test .Issue ;
1720import org .jvnet .hudson .test .JenkinsRule ;
1821import org .jvnet .hudson .test .recipes .LocalData ;
1922import org .kohsuke .github .GHEvent ;
23+ import org .kohsuke .github .GHRepository ;
24+ import org .kohsuke .github .GitHub ;
25+ import org .mockito .Mock ;
26+ import org .mockito .runners .MockitoJUnitRunner ;
2027
2128import javax .inject .Inject ;
2229import java .io .IOException ;
30+ import java .util .Arrays ;
2331import java .util .Collections ;
2432
2533import static com .cloudbees .jenkins .GitHubRepositoryName .create ;
3240import static org .hamcrest .Matchers .notNullValue ;
3341import static org .hamcrest .Matchers .nullValue ;
3442import static org .junit .Assert .assertThat ;
43+ import static org .mockito .Mockito .when ;
3544
3645/**
3746 * @author lanwen (Merkushev Kirill)
3847 */
3948@ Issue ("JENKINS-24690" )
49+ @ RunWith (MockitoJUnitRunner .class )
4050public class GitHubHookRegisterProblemMonitorTest {
4151 private static final GitHubRepositoryName REPO = new GitHubRepositoryName ("host" , "user" , "repo" );
42- private static final GitSCM REPO_GIT_SCM = new GitSCM ("git://host/user/repo.git" );
52+ private static final String REPO_GIT_URI = "host/user/repo.git" ;
53+ private static final GitSCM REPO_GIT_SCM = new GitSCM ("git://" +REPO_GIT_URI );
4354
4455 private static final GitHubRepositoryName REPO_FROM_PING_PAYLOAD = create ("https://github.com/lanwen/test" );
4556
@@ -55,9 +66,26 @@ public class GitHubHookRegisterProblemMonitorTest {
5566 @ Rule
5667 public JenkinsRule jRule = new JenkinsRule ();
5768
69+ @ Mock
70+ private GitHub github ;
71+ @ Mock
72+ private GHRepository ghRepository ;
73+
74+ class GitHubServerConfigForTest extends GitHubServerConfig {
75+ public GitHubServerConfigForTest (String credentialsId ) {
76+ super (credentialsId );
77+ this .setCachedClient (github );
78+ }
79+ }
80+
5881 @ Before
5982 public void setUp () throws Exception {
6083 jRule .getInstance ().getInjector ().injectMembers (this );
84+ GitHubServerConfig config = new GitHubServerConfigForTest ("" );
85+ config .setApiUrl ("http://" + REPO_GIT_URI );
86+ GitHubPlugin .configuration ().setConfigs (Arrays .asList (config ));
87+ when (github .getRepository ("user/repo" )).thenReturn (ghRepository );
88+ when (ghRepository .hasAdminAccess ()).thenReturn (true );
6189 }
6290
6391 @ Test
@@ -149,20 +177,44 @@ public void shouldReportAboutHookProblemOnRegister() throws IOException {
149177 job .addTrigger (new GitHubPushTrigger ());
150178 job .setScm (REPO_GIT_SCM );
151179
180+ when (github .getRepository ("user/repo" ))
181+ .thenThrow (new RuntimeException ("shouldReportAboutHookProblemOnRegister" ));
152182 WebhookManager .forHookUrl (WebhookManagerTest .HOOK_ENDPOINT )
153183 .registerFor ((Item ) job ).run ();
154184
155185 assertThat ("should reg problem" , monitor .isProblemWith (REPO ), is (true ));
156186 }
157187
158188 @ Test
159- public void shouldReportAboutHookProblemOnUnregister () {
189+ public void shouldNotReportAboutHookProblemOnRegister () throws IOException {
190+ FreeStyleProject job = jRule .createFreeStyleProject ();
191+ job .addTrigger (new GitHubPushTrigger ());
192+ job .setScm (REPO_GIT_SCM );
193+
194+ WebhookManager .forHookUrl (WebhookManagerTest .HOOK_ENDPOINT )
195+ .registerFor ((Item ) job ).run ();
196+
197+ assertThat ("should reg problem" , monitor .isProblemWith (REPO ), is (false ));
198+ }
199+
200+ @ Test
201+ public void shouldReportAboutHookProblemOnUnregister () throws IOException {
202+ when (github .getRepository ("user/repo" ))
203+ .thenThrow (new RuntimeException ("shouldReportAboutHookProblemOnUnregister" ));
160204 WebhookManager .forHookUrl (WebhookManagerTest .HOOK_ENDPOINT )
161205 .unregisterFor (REPO , Collections .<GitHubRepositoryName >emptyList ());
162206
163207 assertThat ("should reg problem" , monitor .isProblemWith (REPO ), is (true ));
164208 }
165209
210+ @ Test
211+ public void shouldNotReportAboutHookAuthProblemOnUnregister () {
212+ WebhookManager .forHookUrl (WebhookManagerTest .HOOK_ENDPOINT )
213+ .unregisterFor (REPO , Collections .<GitHubRepositoryName >emptyList ());
214+
215+ assertThat ("should not reg problem" , monitor .isProblemWith (REPO ), is (false ));
216+ }
217+
166218 @ Test
167219 public void shouldResolveOnPingHook () {
168220 monitor .registerProblem (REPO_FROM_PING_PAYLOAD , new IOException ());
0 commit comments