6
6
import hudson .model .Item ;
7
7
import hudson .plugins .git .GitSCM ;
8
8
import org .jenkinsci .plugins .github .extension .GHSubscriberEvent ;
9
+ import org .jenkinsci .plugins .github .GitHubPlugin ;
10
+ import org .jenkinsci .plugins .github .config .GitHubServerConfig ;
9
11
import org .jenkinsci .plugins .github .extension .GHEventsSubscriber ;
10
12
import org .jenkinsci .plugins .github .webhook .WebhookManager ;
11
13
import org .jenkinsci .plugins .github .webhook .WebhookManagerTest ;
12
14
import org .jenkinsci .plugins .github .webhook .subscriber .PingGHEventSubscriber ;
13
15
import org .junit .Before ;
14
16
import org .junit .Rule ;
15
17
import org .junit .Test ;
18
+ import org .junit .runner .RunWith ;
16
19
import org .jvnet .hudson .test .Issue ;
17
20
import org .jvnet .hudson .test .JenkinsRule ;
18
21
import org .jvnet .hudson .test .recipes .LocalData ;
19
22
import 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 ;
20
27
21
28
import javax .inject .Inject ;
22
29
import java .io .IOException ;
30
+ import java .util .Arrays ;
23
31
import java .util .Collections ;
24
32
25
33
import static com .cloudbees .jenkins .GitHubRepositoryName .create ;
32
40
import static org .hamcrest .Matchers .notNullValue ;
33
41
import static org .hamcrest .Matchers .nullValue ;
34
42
import static org .junit .Assert .assertThat ;
43
+ import static org .mockito .Mockito .when ;
35
44
36
45
/**
37
46
* @author lanwen (Merkushev Kirill)
38
47
*/
39
48
@ Issue ("JENKINS-24690" )
49
+ @ RunWith (MockitoJUnitRunner .class )
40
50
public class GitHubHookRegisterProblemMonitorTest {
41
51
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 );
43
54
44
55
private static final GitHubRepositoryName REPO_FROM_PING_PAYLOAD = create ("https://github.com/lanwen/test" );
45
56
@@ -55,9 +66,26 @@ public class GitHubHookRegisterProblemMonitorTest {
55
66
@ Rule
56
67
public JenkinsRule jRule = new JenkinsRule ();
57
68
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
+
58
81
@ Before
59
82
public void setUp () throws Exception {
60
83
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 );
61
89
}
62
90
63
91
@ Test
@@ -149,20 +177,44 @@ public void shouldReportAboutHookProblemOnRegister() throws IOException {
149
177
job .addTrigger (new GitHubPushTrigger ());
150
178
job .setScm (REPO_GIT_SCM );
151
179
180
+ when (github .getRepository ("user/repo" ))
181
+ .thenThrow (new RuntimeException ("shouldReportAboutHookProblemOnRegister" ));
152
182
WebhookManager .forHookUrl (WebhookManagerTest .HOOK_ENDPOINT )
153
183
.registerFor ((Item ) job ).run ();
154
184
155
185
assertThat ("should reg problem" , monitor .isProblemWith (REPO ), is (true ));
156
186
}
157
187
158
188
@ 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" ));
160
204
WebhookManager .forHookUrl (WebhookManagerTest .HOOK_ENDPOINT )
161
205
.unregisterFor (REPO , Collections .<GitHubRepositoryName >emptyList ());
162
206
163
207
assertThat ("should reg problem" , monitor .isProblemWith (REPO ), is (true ));
164
208
}
165
209
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
+
166
218
@ Test
167
219
public void shouldResolveOnPingHook () {
168
220
monitor .registerProblem (REPO_FROM_PING_PAYLOAD , new IOException ());
0 commit comments