Skip to content

Commit 3ca215c

Browse files
authored
Added ActionResolverTest.java and related .json files (jenkinsci#125)
1 parent 1f6eca6 commit 3ca215c

File tree

5 files changed

+572
-0
lines changed

5 files changed

+572
-0
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
package com.gitee.jenkins.webhook;
2+
3+
import static org.hamcrest.CoreMatchers.instanceOf;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
import static org.mockito.Mockito.when;
6+
7+
import com.gitee.jenkins.webhook.ActionResolver.NoopAction;
8+
import com.gitee.jenkins.webhook.build.PullRequestBuildAction;
9+
import com.gitee.jenkins.webhook.build.NoteBuildAction;
10+
import com.gitee.jenkins.webhook.build.PushBuildAction;
11+
import com.gitee.jenkins.webhook.status.BranchBuildPageRedirectAction;
12+
import com.gitee.jenkins.webhook.status.BranchStatusPngAction;
13+
import com.gitee.jenkins.webhook.status.CommitBuildPageRedirectAction;
14+
import com.gitee.jenkins.webhook.status.CommitStatusPngAction;
15+
import com.gitee.jenkins.webhook.status.StatusJsonAction;
16+
import jakarta.servlet.ReadListener;
17+
import jakarta.servlet.ServletInputStream;
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
import org.jvnet.hudson.test.JenkinsRule;
24+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
25+
import org.kohsuke.stapler.StaplerRequest2;
26+
import org.mockito.Mock;
27+
import org.mockito.junit.jupiter.MockitoExtension;
28+
import org.mockito.junit.jupiter.MockitoSettings;
29+
import org.mockito.quality.Strictness;
30+
31+
/**
32+
* @author Robin Müller
33+
*/
34+
@WithJenkins
35+
@ExtendWith(MockitoExtension.class)
36+
@MockitoSettings(strictness = Strictness.LENIENT)
37+
class ActionResolverTest {
38+
39+
private static JenkinsRule jenkins;
40+
41+
@Mock
42+
private StaplerRequest2 request;
43+
44+
@BeforeAll
45+
static void setUp(JenkinsRule rule) {
46+
jenkins = rule;
47+
}
48+
49+
@Test
50+
void getBranchBuildPageRedirect() throws Exception {
51+
String projectName = "getBranchBuildPageRedirect";
52+
jenkins.createFreeStyleProject(projectName);
53+
when(request.getRestOfPath()).thenReturn("");
54+
when(request.hasParameter("ref")).thenReturn(true);
55+
when(request.getMethod()).thenReturn("GET");
56+
57+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
58+
59+
assertThat(resolvedAction, instanceOf(BranchBuildPageRedirectAction.class));
60+
}
61+
62+
@Test
63+
void getCommitStatus() throws Exception {
64+
String projectName = "getCommitStatus";
65+
jenkins.createFreeStyleProject(projectName);
66+
when(request.getRestOfPath()).thenReturn("builds/1234abcd/status.json");
67+
when(request.getMethod()).thenReturn("GET");
68+
69+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
70+
71+
assertThat(resolvedAction, instanceOf(StatusJsonAction.class));
72+
}
73+
74+
@Test
75+
void getCommitBuildPageRedirect_builds() throws Exception {
76+
String projectName = "getCommitBuildPageRedirect_builds";
77+
jenkins.createFreeStyleProject(projectName);
78+
when(request.getRestOfPath()).thenReturn("builds/1234abcd");
79+
when(request.getMethod()).thenReturn("GET");
80+
81+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
82+
83+
assertThat(resolvedAction, instanceOf(CommitBuildPageRedirectAction.class));
84+
}
85+
86+
@Test
87+
void getCommitBuildPageRedirect_commits() throws Exception {
88+
String projectName = "getCommitBuildPageRedirect_commits";
89+
jenkins.createFreeStyleProject(projectName);
90+
when(request.getRestOfPath()).thenReturn("commits/7890efab");
91+
when(request.getMethod()).thenReturn("GET");
92+
93+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
94+
95+
assertThat(resolvedAction, instanceOf(CommitBuildPageRedirectAction.class));
96+
}
97+
98+
@Test
99+
void getBranchStatusPng() throws Exception {
100+
String projectName = "getBranchStatusPng";
101+
jenkins.createFreeStyleProject(projectName);
102+
when(request.getRestOfPath()).thenReturn("builds/status.png");
103+
when(request.hasParameter("ref")).thenReturn(true);
104+
when(request.getMethod()).thenReturn("GET");
105+
106+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
107+
108+
assertThat(resolvedAction, instanceOf(BranchStatusPngAction.class));
109+
}
110+
111+
@Test
112+
void getCommitStatusPng() throws Exception {
113+
String projectName = "getCommitStatusPng";
114+
jenkins.createFreeStyleProject(projectName);
115+
when(request.getRestOfPath()).thenReturn("builds/status.png");
116+
when(request.hasParameter("ref")).thenReturn(false);
117+
when(request.getMethod()).thenReturn("GET");
118+
119+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
120+
121+
assertThat(resolvedAction, instanceOf(CommitStatusPngAction.class));
122+
}
123+
124+
@Test
125+
void postMergeRequest() throws Exception {
126+
String projectName = "postMergeRequest";
127+
jenkins.createFreeStyleProject(projectName);
128+
when(request.getRestOfPath()).thenReturn("");
129+
when(request.getMethod()).thenReturn("POST");
130+
when(request.getHeader("X-Gitee-Event")).thenReturn("Merge Request Hook");
131+
when(request.getInputStream())
132+
.thenReturn(new ResourceServletInputStream("ActionResolverTest_pullRequest.json"));
133+
134+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
135+
136+
assertThat(resolvedAction, instanceOf(PullRequestBuildAction.class));
137+
}
138+
139+
@Test
140+
void postNote() throws Exception {
141+
String projectName = "postNote";
142+
jenkins.createFreeStyleProject(projectName);
143+
when(request.getRestOfPath()).thenReturn("");
144+
when(request.getMethod()).thenReturn("POST");
145+
when(request.getHeader("X-Gitee-Event")).thenReturn("Note Hook");
146+
when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postNote.json"));
147+
148+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
149+
150+
assertThat(resolvedAction, instanceOf(NoteBuildAction.class));
151+
}
152+
153+
@Test
154+
void postPush() throws Exception {
155+
String projectName = "postPush";
156+
jenkins.createFreeStyleProject(projectName);
157+
when(request.getRestOfPath()).thenReturn("");
158+
when(request.getMethod()).thenReturn("POST");
159+
when(request.getHeader("X-Gitee-Event")).thenReturn("Push Hook");
160+
when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json"));
161+
162+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
163+
164+
assertThat(resolvedAction, instanceOf(PushBuildAction.class));
165+
}
166+
167+
@Test
168+
void postPushTag() throws Exception {
169+
String projectName = "postPushTag";
170+
jenkins.createFreeStyleProject(projectName);
171+
when(request.getRestOfPath()).thenReturn("");
172+
when(request.getMethod()).thenReturn("POST");
173+
when(request.getHeader("X-Gitee-Event")).thenReturn("Tag Push Hook");
174+
when(request.getInputStream())
175+
.thenReturn(new ResourceServletInputStream("ActionResolverTest_postPushTag.json"));
176+
177+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
178+
179+
assertThat(resolvedAction, instanceOf(PushBuildAction.class));
180+
}
181+
182+
@Test
183+
void postPushMissingEventHeader() throws Exception {
184+
String projectName = "postPushMissingEventHeader";
185+
jenkins.createFreeStyleProject(projectName);
186+
when(request.getRestOfPath()).thenReturn("");
187+
when(request.getMethod()).thenReturn("POST");
188+
when(request.getHeader("X-Gitee-Event")).thenReturn(null);
189+
when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json"));
190+
191+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
192+
193+
assertThat(resolvedAction, instanceOf(NoopAction.class));
194+
}
195+
196+
@Test
197+
void postPushUnsupportedEventHeader() throws Exception {
198+
String projectName = "postPushUnsupportedEventHeader";
199+
jenkins.createFreeStyleProject(projectName);
200+
when(request.getRestOfPath()).thenReturn("");
201+
when(request.getMethod()).thenReturn("POST");
202+
when(request.getHeader("X-Gitee-Event")).thenReturn("__Not Supported Header__");
203+
when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json"));
204+
205+
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
206+
207+
assertThat(resolvedAction, instanceOf(NoopAction.class));
208+
}
209+
210+
private static class ResourceServletInputStream extends ServletInputStream {
211+
212+
private final InputStream input;
213+
214+
private ResourceServletInputStream(String classResourceName) {
215+
this.input = getClass().getResourceAsStream(classResourceName);
216+
}
217+
218+
@Override
219+
public int read() throws IOException {
220+
return input.read();
221+
}
222+
223+
@Override
224+
public boolean isReady() {
225+
return true;
226+
}
227+
228+
@Override
229+
public boolean isFinished() {
230+
return true;
231+
}
232+
233+
@Override
234+
public void setReadListener(ReadListener var1) {}
235+
}
236+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"object_kind": "note",
3+
"user": {
4+
"name": "Administrator",
5+
"username": "root",
6+
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
7+
},
8+
"project_id": 5,
9+
"project":{
10+
"name":"Gitee Test",
11+
"description":"Aut reprehenderit ut est.",
12+
"web_url":"http://example.com/gitee/gitee-test",
13+
"avatar_url":null,
14+
"git_ssh_url":"[email protected]:gitee/gitee-test.git",
15+
"git_http_url":"http://example.com/gitee/gitee-test.git",
16+
"namespace":"Gitee Org",
17+
"visibility_level":10,
18+
"path_with_namespace":"gitee/gitee-test",
19+
"default_branch":"master",
20+
"homepage":"http://example.com/gitee/gitee-test",
21+
"url":"http://example.com/gitee/gitee-test.git",
22+
"ssh_url":"[email protected]:gitee/gitee-test.git",
23+
"http_url":"http://example.com/gitee/gitee-test.git"
24+
},
25+
"repository":{
26+
"name": "Gitee Test",
27+
"url": "http://localhost/gitee/gitee-test.git",
28+
"description": "Aut reprehenderit ut est.",
29+
"homepage": "http://example.com/gitee/gitee-test"
30+
},
31+
"object_attributes": {
32+
"id": 1244,
33+
"note": "This MR needs work.",
34+
"noteable_type": "MergeRequest",
35+
"author_id": 1,
36+
"created_at": "2015-05-17 18:21:36 UTC",
37+
"updated_at": "2015-05-17 18:21:36 UTC",
38+
"project_id": 5,
39+
"attachment": null,
40+
"line_code": null,
41+
"commit_id": "",
42+
"noteable_id": 7,
43+
"system": false,
44+
"st_diff": null,
45+
"url": "http://example.com/gitee/gitee-test/merge_requests/1#note_1244"
46+
},
47+
"merge_request": {
48+
"id": 7,
49+
"target_branch": "markdown",
50+
"source_branch": "master",
51+
"source_project_id": 5,
52+
"author_id": 8,
53+
"assignee_id": 28,
54+
"title": "Tempora et eos debitis quae laborum et.",
55+
"created_at": "2015-03-01 20:12:53 UTC",
56+
"updated_at": "2015-03-21 18:27:27 UTC",
57+
"milestone_id": 11,
58+
"state": "opened",
59+
"merge_status": "cannot_be_merged",
60+
"target_project_id": 5,
61+
"iid": 1,
62+
"description": "Et voluptas corrupti assumenda temporibus. Architecto cum animi eveniet amet asperiores. Vitae numquam voluptate est natus sit et ad id.",
63+
"position": 0,
64+
"locked_at": null,
65+
"source":{
66+
"name":"Gitee Test",
67+
"description":"Aut reprehenderit ut est.",
68+
"web_url":"http://example.com/gitee/gitee-test",
69+
"avatar_url":null,
70+
"git_ssh_url":"[email protected]:gitee/gitee-test.git",
71+
"git_http_url":"http://example.com/gitee/gitee-test.git",
72+
"namespace":"Gitee Org",
73+
"visibility_level":10,
74+
"path_with_namespace":"gitee/gitee-test",
75+
"default_branch":"master",
76+
"homepage":"http://example.com/gitee/gitee-test",
77+
"url":"http://example.com/gitee/gitee-test.git",
78+
"ssh_url":"[email protected]:gitee/gitee-test.git",
79+
"http_url":"http://example.com/gitee/gitee-test.git"
80+
},
81+
"target": {
82+
"name":"Gitee Test",
83+
"description":"Aut reprehenderit ut est.",
84+
"web_url":"http://example.com/gitee/gitee-test",
85+
"avatar_url":null,
86+
"git_ssh_url":"[email protected]:gitee/gitee-test.git",
87+
"git_http_url":"http://example.com/gitee/gitee-test.git",
88+
"namespace":"Gitee Org",
89+
"visibility_level":10,
90+
"path_with_namespace":"gitee/gitee-test",
91+
"default_branch":"master",
92+
"homepage":"http://example.com/gitee/gitee-test",
93+
"url":"http://example.com/gitee/gitee-test.git",
94+
"ssh_url":"[email protected]:gitee/gitee-test.git",
95+
"http_url":"http://example.com/gitee/gitee-test.git"
96+
},
97+
"last_commit": {
98+
"id": "562e173be03b8ff2efb05345d12df18815438a4b",
99+
"message": "Merge branch 'another-branch' into 'master'\n\nCheck in this test\n",
100+
"timestamp": "2015-04-08T21: 00:25-07:00",
101+
"url": "http://example.com/gitee/gitee-test/commit/562e173be03b8ff2efb05345d12df18815438a4b",
102+
"author": {
103+
"name": "John Smith",
104+
"email": "[email protected]"
105+
}
106+
},
107+
"work_in_progress": false,
108+
"assignee": {
109+
"name": "User1",
110+
"username": "user1",
111+
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)