Skip to content

Commit 93b57e9

Browse files
authored
Added BuildStatusActionTest.java and StatusJsonActionTest.java (#118)
1 parent 4e6e513 commit 93b57e9

File tree

2 files changed

+298
-0
lines changed

2 files changed

+298
-0
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
package com.gitee.jenkins.webhook.status;
2+
3+
import static org.mockito.Mockito.when;
4+
5+
import hudson.Launcher;
6+
import hudson.model.AbstractBuild;
7+
import hudson.model.BuildListener;
8+
import hudson.model.FreeStyleBuild;
9+
import hudson.model.FreeStyleProject;
10+
import hudson.model.Result;
11+
import hudson.plugins.git.GitSCM;
12+
import hudson.util.OneShotEvent;
13+
import jakarta.servlet.ServletException;
14+
import jakarta.servlet.ServletOutputStream;
15+
import java.io.ByteArrayOutputStream;
16+
import java.io.File;
17+
import java.io.IOException;
18+
import java.io.PrintWriter;
19+
import org.eclipse.jgit.api.Git;
20+
import org.eclipse.jgit.revwalk.RevCommit;
21+
import org.junit.jupiter.api.BeforeAll;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.junit.jupiter.api.io.TempDir;
26+
import org.jvnet.hudson.test.JenkinsRule;
27+
import org.jvnet.hudson.test.TestBuilder;
28+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
29+
import org.kohsuke.stapler.StaplerResponse2;
30+
import org.mockito.Mock;
31+
import org.mockito.junit.jupiter.MockitoExtension;
32+
import org.mockito.junit.jupiter.MockitoSettings;
33+
import org.mockito.quality.Strictness;
34+
35+
/**
36+
* @author Robin Müller
37+
*/
38+
@WithJenkins
39+
@ExtendWith(MockitoExtension.class)
40+
@MockitoSettings(strictness = Strictness.LENIENT)
41+
abstract class BuildStatusActionTest {
42+
43+
private static JenkinsRule jenkins;
44+
45+
@TempDir
46+
private File tmp;
47+
48+
protected String commitSha1;
49+
protected String branch = "master";
50+
51+
@Mock
52+
private StaplerResponse2 response;
53+
54+
private String gitRepoUrl;
55+
56+
@BeforeAll
57+
static void setUp(JenkinsRule rule) {
58+
jenkins = rule;
59+
}
60+
61+
@BeforeEach
62+
void setUp() throws Exception {
63+
Git.init().setDirectory(tmp).call();
64+
File.createTempFile("test", null, tmp);
65+
Git git = Git.open(tmp);
66+
git.add().addFilepattern("test");
67+
RevCommit commit = git.commit().setSign(false).setMessage("test").call();
68+
commitSha1 = commit.getId().getName();
69+
gitRepoUrl = tmp.toURI().toString();
70+
}
71+
72+
@Test
73+
void successfulBuild() throws Exception {
74+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
75+
testProject.setScm(new GitSCM(gitRepoUrl));
76+
FreeStyleBuild build = testProject.scheduleBuild2(0).get();
77+
78+
ByteArrayOutputStream out = new ByteArrayOutputStream();
79+
mockResponse(out);
80+
getBuildStatusAction(testProject).execute(response);
81+
82+
assertSuccessfulBuild(build, out, response);
83+
}
84+
85+
@Test
86+
void failedBuild() throws Exception {
87+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
88+
testProject.setScm(new GitSCM(gitRepoUrl));
89+
testProject.getBuildersList().add(new TestBuilder() {
90+
@Override
91+
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
92+
build.setResult(Result.FAILURE);
93+
return true;
94+
}
95+
});
96+
FreeStyleBuild build = testProject.scheduleBuild2(0).get();
97+
98+
ByteArrayOutputStream out = new ByteArrayOutputStream();
99+
mockResponse(out);
100+
getBuildStatusAction(testProject).execute(response);
101+
102+
assertFailedBuild(build, out, response);
103+
}
104+
105+
@Test
106+
void runningBuild() throws Exception {
107+
final OneShotEvent buildStarted = new OneShotEvent();
108+
final OneShotEvent keepRunning = new OneShotEvent();
109+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
110+
testProject.setScm(new GitSCM(gitRepoUrl));
111+
testProject.getBuildersList().add(new TestBuilder() {
112+
@Override
113+
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
114+
throws InterruptedException {
115+
buildStarted.signal();
116+
keepRunning.block();
117+
return true;
118+
}
119+
});
120+
FreeStyleBuild build = testProject.scheduleBuild2(0).waitForStart();
121+
buildStarted.block();
122+
123+
ByteArrayOutputStream out = new ByteArrayOutputStream();
124+
mockResponse(out);
125+
getBuildStatusAction(testProject).execute(response);
126+
keepRunning.signal();
127+
128+
assertRunningBuild(build, out, response);
129+
}
130+
131+
@Test
132+
void canceledBuild() throws Exception {
133+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
134+
testProject.setScm(new GitSCM(gitRepoUrl));
135+
testProject.getBuildersList().add(new TestBuilder() {
136+
@Override
137+
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
138+
throws IOException {
139+
try {
140+
build.doStop();
141+
} catch (ServletException e) {
142+
throw new IOException(e);
143+
}
144+
return true;
145+
}
146+
});
147+
FreeStyleBuild build = testProject.scheduleBuild2(0).get();
148+
149+
ByteArrayOutputStream out = new ByteArrayOutputStream();
150+
mockResponse(out);
151+
getBuildStatusAction(testProject).execute(response);
152+
153+
assertCanceledBuild(build, out, response);
154+
}
155+
156+
@Test
157+
void unstableBuild() throws Exception {
158+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
159+
testProject.setScm(new GitSCM(gitRepoUrl));
160+
testProject.getBuildersList().add(new TestBuilder() {
161+
@Override
162+
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
163+
build.setResult(Result.UNSTABLE);
164+
return true;
165+
}
166+
});
167+
FreeStyleBuild build = testProject.scheduleBuild2(0).get();
168+
169+
ByteArrayOutputStream out = new ByteArrayOutputStream();
170+
mockResponse(out);
171+
getBuildStatusAction(testProject).execute(response);
172+
173+
assertUnstableBuild(build, out, response);
174+
}
175+
176+
@Test
177+
void notFoundBuild() throws Exception {
178+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
179+
testProject.setScm(new GitSCM(gitRepoUrl));
180+
181+
ByteArrayOutputStream out = new ByteArrayOutputStream();
182+
mockResponse(out);
183+
getBuildStatusAction(testProject).execute(response);
184+
185+
assertNotFoundBuild(out, response);
186+
}
187+
188+
protected abstract BuildStatusAction getBuildStatusAction(FreeStyleProject project);
189+
190+
protected abstract void assertSuccessfulBuild(
191+
FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) throws Exception;
192+
193+
protected abstract void assertFailedBuild(
194+
FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) throws Exception;
195+
196+
protected abstract void assertRunningBuild(
197+
FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) throws Exception;
198+
199+
protected abstract void assertCanceledBuild(
200+
FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) throws Exception;
201+
202+
protected abstract void assertUnstableBuild(
203+
FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) throws Exception;
204+
205+
protected abstract void assertNotFoundBuild(ByteArrayOutputStream out, StaplerResponse2 response) throws Exception;
206+
207+
private void mockResponse(final ByteArrayOutputStream out) throws Exception {
208+
ServletOutputStream servletOutputStream = new ServletOutputStream() {
209+
@Override
210+
public void write(int b) {
211+
out.write(b);
212+
}
213+
214+
@Override
215+
public void setWriteListener(jakarta.servlet.WriteListener writeListener) {}
216+
217+
@Override
218+
public boolean isReady() {
219+
return true;
220+
}
221+
};
222+
when(response.getOutputStream()).thenReturn(servletOutputStream);
223+
when(response.getWriter()).thenReturn(new PrintWriter(servletOutputStream));
224+
}
225+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.gitee.jenkins.webhook.status;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import hudson.model.FreeStyleBuild;
7+
import hudson.model.FreeStyleProject;
8+
import java.io.ByteArrayOutputStream;
9+
import net.sf.json.JSONObject;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
12+
import org.kohsuke.stapler.StaplerResponse2;
13+
import org.mockito.junit.jupiter.MockitoExtension;
14+
15+
/**
16+
* @author Robin Müller
17+
*/
18+
@WithJenkins
19+
@ExtendWith(MockitoExtension.class)
20+
class StatusJsonActionTest extends BuildStatusActionTest {
21+
22+
@Override
23+
protected BuildStatusAction getBuildStatusAction(FreeStyleProject project) {
24+
return new StatusJsonAction(project, commitSha1);
25+
}
26+
27+
@Override
28+
protected void assertSuccessfulBuild(FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) {
29+
JSONObject object = JSONObject.fromObject(out.toString());
30+
assertThat(object.getString("sha"), is(commitSha1));
31+
assertThat(object.getInt("id"), is(build.getNumber()));
32+
assertThat(object.getString("status"), is("success"));
33+
}
34+
35+
@Override
36+
protected void assertFailedBuild(FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) {
37+
JSONObject object = JSONObject.fromObject(out.toString());
38+
assertThat(object.getString("sha"), is(commitSha1));
39+
assertThat(object.getInt("id"), is(build.getNumber()));
40+
assertThat(object.getString("status"), is("failed"));
41+
}
42+
43+
@Override
44+
protected void assertRunningBuild(FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) {
45+
JSONObject object = JSONObject.fromObject(out.toString());
46+
assertThat(object.getString("sha"), is(commitSha1));
47+
assertThat(object.getInt("id"), is(build.getNumber()));
48+
assertThat(object.getString("status"), is("running"));
49+
}
50+
51+
@Override
52+
protected void assertCanceledBuild(FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) {
53+
JSONObject object = JSONObject.fromObject(out.toString());
54+
assertThat(object.getString("sha"), is(commitSha1));
55+
assertThat(object.getInt("id"), is(build.getNumber()));
56+
assertThat(object.getString("status"), is("canceled"));
57+
}
58+
59+
@Override
60+
protected void assertUnstableBuild(FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) {
61+
JSONObject object = JSONObject.fromObject(out.toString());
62+
assertThat(object.getString("sha"), is(commitSha1));
63+
assertThat(object.getInt("id"), is(build.getNumber()));
64+
assertThat(object.getString("status"), is("failed"));
65+
}
66+
67+
@Override
68+
protected void assertNotFoundBuild(ByteArrayOutputStream out, StaplerResponse2 response) {
69+
JSONObject object = JSONObject.fromObject(out.toString());
70+
assertThat(object.getString("sha"), is(commitSha1));
71+
assertThat(object.getString("status"), is("not_found"));
72+
}
73+
}

0 commit comments

Comments
 (0)