Skip to content

Commit fe1168f

Browse files
authored
Added PushBuildActionTest.java (jenkinsci#106)
1 parent 7ef6f61 commit fe1168f

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.gitee.jenkins.webhook.build;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
5+
import static org.hamcrest.Matchers.notNullValue;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
import static org.mockito.ArgumentMatchers.any;
8+
import static org.mockito.ArgumentMatchers.isA;
9+
import static org.mockito.Mockito.isA;
10+
import static org.mockito.Mockito.mock;
11+
import static org.mockito.Mockito.never;
12+
import static org.mockito.Mockito.verify;
13+
import static org.mockito.Mockito.when;
14+
15+
import com.gitee.jenkins.trigger.GiteePushTrigger;
16+
import com.gitee.jenkins.gitee.hook.model.PushHook;
17+
import hudson.model.FreeStyleProject;
18+
import hudson.security.ACL;
19+
import java.nio.charset.StandardCharsets;
20+
import java.util.Collections;
21+
import jenkins.plugins.git.GitSCMSource;
22+
import jenkins.plugins.git.traits.IgnoreOnPushNotificationTrait;
23+
import jenkins.scm.api.SCMSourceOwner;
24+
import org.apache.commons.io.IOUtils;
25+
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.Disabled;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.jvnet.hudson.test.JenkinsRule;
30+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
31+
import org.kohsuke.stapler.HttpResponses;
32+
import org.kohsuke.stapler.StaplerResponse2;
33+
import org.mockito.ArgumentCaptor;
34+
import org.mockito.Mock;
35+
import org.mockito.junit.jupiter.MockitoExtension;
36+
import org.mockito.junit.jupiter.MockitoSettings;
37+
import org.mockito.quality.Strictness;
38+
39+
/**
40+
* @author Robin Müller
41+
*/
42+
@WithJenkins
43+
@ExtendWith(MockitoExtension.class)
44+
@MockitoSettings(strictness = Strictness.LENIENT)
45+
class PushBuildActionTest {
46+
47+
private static JenkinsRule jenkins;
48+
49+
@Mock
50+
private StaplerResponse2 response;
51+
52+
@Mock
53+
private GiteePushTrigger trigger;
54+
55+
@BeforeAll
56+
static void setUp(JenkinsRule rule) {
57+
jenkins = rule;
58+
}
59+
60+
@Test
61+
void skip_missingRepositoryUrl() throws Exception {
62+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
63+
testProject.addTrigger(trigger);
64+
65+
new PushBuildAction(testProject, getJson("PushEvent_missingRepositoryUrl.json"), null).execute(response);
66+
67+
verify(trigger, never()).onPost(any(PushHook.class));
68+
}
69+
70+
@Test
71+
void build() {
72+
assertThrows(HttpResponses.HttpResponseException.class, () -> {
73+
try {
74+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
75+
testProject.addTrigger(trigger);
76+
77+
// exception.expect(HttpResponses.HttpResponseException.class);
78+
new PushBuildAction(testProject, getJson("PushEvent.json"), null).execute(response);
79+
} finally {
80+
ArgumentCaptor<PushHook> pushHookArgumentCaptor = ArgumentCaptor.forClass(PushHook.class);
81+
verify(trigger).onPost(pushHookArgumentCaptor.capture());
82+
assertThat(pushHookArgumentCaptor.getValue().getProject(), is(notNullValue()));
83+
assertThat(pushHookArgumentCaptor.getValue().getProject().getWebUrl(), is(notNullValue()));
84+
}
85+
});
86+
}
87+
88+
@Test
89+
void invalidToken() {
90+
assertThrows(HttpResponses.HttpResponseException.class, () -> {
91+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
92+
when(trigger.getSecretToken()).thenReturn("secret");
93+
testProject.addTrigger(trigger);
94+
new PushBuildAction(testProject, getJson("PushEvent.json"), "wrong-secret").execute(response);
95+
96+
verify(trigger, never()).onPost(any(PushHook.class));
97+
});
98+
}
99+
100+
private String getJson(String name) throws Exception {
101+
return IOUtils.toString(getClass().getResourceAsStream(name), StandardCharsets.UTF_8);
102+
}
103+
104+
@Test
105+
void scmSourceOnUpdateExecuted() {
106+
GitSCMSource source = new GitSCMSource("http://test");
107+
SCMSourceOwner item = mock(SCMSourceOwner.class);
108+
ACL acl = mock(ACL.class);
109+
when(item.getSCMSources()).thenReturn(Collections.singletonList(source));
110+
when(item.getACL()).thenReturn(acl);
111+
assertThrows(
112+
HttpResponses.HttpResponseException.class,
113+
() -> new PushBuildAction(item, getJson("PushEvent.json"), null).execute(response));
114+
// item.onSCMSourceUpdated(source);
115+
verify(item).onSCMSourceUpdated(isA(GitSCMSource.class));
116+
}
117+
118+
119+
// Test is currently unsupported. A check is missing in the SCMSourceOwnerNotifier.
120+
@Test
121+
@Disabled
122+
void scmSourceOnUpdateNotExecuted() {
123+
GitSCMSource source = new GitSCMSource("http://test");
124+
source.getTraits().add(new IgnoreOnPushNotificationTrait());
125+
SCMSourceOwner item = mock(SCMSourceOwner.class);
126+
when(item.getSCMSources()).thenReturn(Collections.singletonList(source));
127+
assertThrows(
128+
HttpResponses.HttpResponseException.class,
129+
() -> new PushBuildAction(item, getJson("PushEvent.json"), null).execute(response));
130+
verify(item, never()).onSCMSourceUpdated(isA(GitSCMSource.class));
131+
}
132+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"object_kind": "push",
3+
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
4+
"after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
5+
"ref": "refs/heads/master",
6+
"user_id": 4,
7+
"user_name": "John Smith",
8+
"user_username": "jsmith",
9+
"user_email": "[email protected]",
10+
"user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
11+
"project_id": 15,
12+
"project": {
13+
"id": 2,
14+
"name": "Diaspora",
15+
"description": "",
16+
"web_url": "http://example.com/mike/diaspora",
17+
"avatar_url": null,
18+
"ssh_url": "[email protected]:mike/diaspora.git",
19+
"git_http_url": "http://example.com/mike/diaspora.git",
20+
"namespace": "Mike",
21+
"path_with_namespace": "mike/diaspora",
22+
"default_branch": "master",
23+
"homepage": "http://example.com/mike/diaspora",
24+
"url": "[email protected]:mike/diasporadiaspora.git"
25+
},
26+
"repository": {
27+
"name": "Diaspora",
28+
"url": "[email protected]:mike/diasporadiaspora.git",
29+
"description": "",
30+
"homepage": "http://example.com/mike/diaspora",
31+
"git_http_url": "http://example.com/mike/diaspora.git",
32+
"git_ssh_url": "[email protected]:mike/diaspora.git",
33+
"visibility_level": 0
34+
},
35+
"commits": [
36+
{
37+
"id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
38+
"message": "Update Catalan translation to e38cb41.",
39+
"timestamp": "2011-12-12T14:27:31+02:00",
40+
"url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
41+
"author": {
42+
"name": "Jordi Mallach",
43+
"email": "[email protected]"
44+
},
45+
"added": [
46+
"CHANGELOG"
47+
],
48+
"modified": [
49+
"app/controller/application.rb"
50+
],
51+
"removed": []
52+
},
53+
{
54+
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
55+
"message": "fixed readme",
56+
"timestamp": "2012-01-03T23:36:29+02:00",
57+
"url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
58+
"author": {
59+
"name": "Gitee dev user",
60+
"email": "Giteedev@dv6700.(none)"
61+
},
62+
"added": [
63+
"CHANGELOG"
64+
],
65+
"modified": [
66+
"app/controller/application.rb"
67+
],
68+
"removed": []
69+
}
70+
],
71+
"total_commits_count": 4
72+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"object_kind": "push",
3+
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
4+
"after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
5+
"ref": "refs/heads/master",
6+
"user_id": 4,
7+
"user_name": "John Smith",
8+
"user_username": "jsmith",
9+
"user_email": "[email protected]",
10+
"user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
11+
"project_id": 15,
12+
"project": {
13+
"id": 2,
14+
"name": "Diaspora",
15+
"description": "",
16+
"web_url": "http://example.com/mike/diaspora",
17+
"avatar_url": null,
18+
"ssh_url": "[email protected]:mike/diaspora.git",
19+
"git_http_url": "http://example.com/mike/diaspora.git",
20+
"namespace": "Mike",
21+
"path_with_namespace": "mike/diaspora",
22+
"default_branch": "master",
23+
"homepage": "http://example.com/mike/diaspora",
24+
"url": "[email protected]:mike/diasporadiaspora.git"
25+
},
26+
"repository": {
27+
"name": "Diaspora",
28+
"description": "",
29+
"homepage": "http://example.com/mike/diaspora",
30+
"git_http_url": "http://example.com/mike/diaspora.git",
31+
"git_ssh_url": "[email protected]:mike/diaspora.git",
32+
"visibility_level": 0
33+
},
34+
"commits": [
35+
{
36+
"id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
37+
"message": "Update Catalan translation to e38cb41.",
38+
"timestamp": "2011-12-12T14:27:31+02:00",
39+
"url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
40+
"author": {
41+
"name": "Jordi Mallach",
42+
"email": "[email protected]"
43+
},
44+
"added": [
45+
"CHANGELOG"
46+
],
47+
"modified": [
48+
"app/controller/application.rb"
49+
],
50+
"removed": []
51+
},
52+
{
53+
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
54+
"message": "fixed readme",
55+
"timestamp": "2012-01-03T23:36:29+02:00",
56+
"url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
57+
"author": {
58+
"name": "Gitee dev user",
59+
"email": "Giteedev@dv6700.(none)"
60+
},
61+
"added": [
62+
"CHANGELOG"
63+
],
64+
"modified": [
65+
"app/controller/application.rb"
66+
],
67+
"removed": []
68+
}
69+
],
70+
"total_commits_count": 4
71+
}

0 commit comments

Comments
 (0)