-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathGogsWebHookTest.java
More file actions
290 lines (224 loc) · 10.4 KB
/
GogsWebHookTest.java
File metadata and controls
290 lines (224 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package org.jenkinsci.plugins.gogs;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.kohsuke.stapler.RequestImpl;
import org.kohsuke.stapler.ResponseImpl;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GogsWebHookTest {
final Logger log = LoggerFactory.getLogger(GogsWebHookTest.class);
@Rule
public TestRule watcher = new TestWatcher() {
protected void starting(Description description) {
GogsWebHookTest.this.log.info("\t ************** Start Test ({}) *******************", description
.getMethodName());
}
};
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void callDoIndexWithNullReqMessageMustThrowException() throws IOException {
GogsWebHook gogsWebHook = new GogsWebHook();
StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
try {
gogsWebHook.internalDoIndex(new GogsResults(),null, staplerResponse);
} catch (NullPointerException e) {
String expectedErrMsg = "Null request submitted to doIndex method";
assertEquals("Not the expected error message.", expectedErrMsg, e.getMessage());
log.info("call failed as expected.");
return;
}
fail("The call should have failed.");
}
@Test
public void callDoIndexWithNullResponseMessageMustThrowException() throws IOException {
GogsWebHook gogsWebHook = new GogsWebHook();
StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
try {
gogsWebHook.internalDoIndex(new GogsResults(), staplerRequest, null);
} catch (NullPointerException e) {
String expectedErrMsg = "Null reply submitted to doIndex method";
assertEquals("Not the expected error message.", expectedErrMsg, e.getMessage());
log.info("call failed as expected.");
return;
}
fail("The call should have failed.");
}
@Test
public void whenEmptyHeaderTypeMustReturnError() throws Exception {
//Prepare the SUT
File uniqueFile = Files.createTempFile(new File("target").toPath(), "webHookTest_", ".txt").toFile();
StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
//perform the test
performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
//validate that everything was done as planed
verify(staplerResponse).setStatus(403);
String expectedOutput = "Only push event can be accepted.";
isExpectedOutput(uniqueFile, expectedOutput);
log.info("Test succeeded.");
}
@Test
public void whenWrongHeaderTypeMustReturnError() throws Exception {
//Prepare the SUT
File uniqueFile = Files.createTempFile(new File("target").toPath(), "webHookTest_", ".txt").toFile();
StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("junk");
//perform the testÎ
performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
//validate that everything was done as planed
verify(staplerResponse).setStatus(403);
String expectedOutput = "Only push event can be accepted.";
isExpectedOutput(uniqueFile, expectedOutput);
log.info("Test succeeded.");
}
@Test
public void whenQueryStringIsNullMustThrowException() throws Exception {
//Prepare the SUT
StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
when(staplerRequest.getQueryString()).thenReturn(null);
GogsWebHook gogsWebHook = new GogsWebHook();
try {
gogsWebHook.internalDoIndex(new GogsResults(), staplerRequest, staplerResponse);
} catch (NullPointerException e) {
String expectedErrMsg = "The queryString in the request is null";
assertEquals("Not the expected error message.", expectedErrMsg, e.getMessage());
log.info("call failed as expected.");
return;
}
fail("The call should have failed.");
}
@Test
public void whenNoJobInQueryStringMustReturnError() throws Exception {
//Prepare the SUT
File uniqueFile = Files.createTempFile(new File("target").toPath(), "webHookTest_", ".txt").toFile();
StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
when(staplerRequest.getQueryString()).thenReturn("foo=bar&blaah=blaah");
//perform the testÎ
performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
//validate that everything was done as planed
verify(staplerResponse).setStatus(404);
String expectedOutput = "Parameter 'job' is missing.";
isExpectedOutput(uniqueFile, expectedOutput);
log.info("Test succeeded.");
}
@Test
public void whenEmptyJobInQueryStringMustReturnError() throws Exception {
//Prepare the SUT
File uniqueFile = Files.createTempFile(new File("target").toPath(), "webHookTest_", ".txt").toFile();
StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
when(staplerRequest.getQueryString()).thenReturn("job&foo=bar");
//perform the testÎ
performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
//validate that everything was done as planed
verify(staplerResponse).setStatus(404);
String expectedOutput = "No value assigned to parameter 'job'";
isExpectedOutput(uniqueFile, expectedOutput);
log.info("Test succeeded.");
}
@Test
public void whenEmptyJob2InQueryStringMustReturnError() throws Exception {
//Prepare the SUT
File uniqueFile = Files.createTempFile(new File("target").toPath(), "webHookTest_", ".txt").toFile();
StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
when(staplerRequest.getQueryString()).thenReturn("job=&foo=bar");
//perform the testÎ
performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
//validate that everything was done as planed
verify(staplerResponse).setStatus(404);
String expectedOutput = "No value assigned to parameter 'job'";
isExpectedOutput(uniqueFile, expectedOutput);
log.info("Test succeeded.");
}
@Test
public void whenUriDoesNotContainUrlNameMustReturnError() throws Exception {
//Prepare the SUT
File uniqueFile = Files.createTempFile(new File("target").toPath(), "webHookTest_", ".txt").toFile();
StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
when(staplerRequest.getQueryString()).thenReturn("job=myJob");
MockServletInputStream inputStream = new MockServletInputStream("body");
when(staplerRequest.getInputStream()).thenReturn(inputStream);
when(staplerRequest.getRequestURI()).thenReturn("/badUri/aaa");
//perform the testÎ
performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
//validate that everything was done as planed
verify(staplerResponse).setStatus(404);
String expectedOutput = "No payload or URI contains invalid entries.";
isExpectedOutput(uniqueFile, expectedOutput);
log.info("Test succeeded.");
}
//
// Helper methods
//
private void performDoIndexTest(StaplerRequest staplerRequest, StaplerResponse staplerResponse, File file) throws IOException {
PrintWriter printWriter = new PrintWriter(file.getAbsoluteFile(), "UTF-8");
when(staplerResponse.getWriter()).thenReturn(printWriter);
GogsWebHook gogsWebHook = new GogsWebHook();
//Call the method under test
gogsWebHook.doIndex(staplerRequest, staplerResponse);
//Save the Jason log file so we can check it
printWriter.flush();
printWriter.close();
}
private void isExpectedOutput(File uniqueFile, String expectedOutput) throws IOException {
String output = FileUtils.readFileToString(uniqueFile, "utf-8");
uniqueFile.delete();
String completeExpectedOutput = "{\"result\":\"ERROR\",\"message\":\"" + expectedOutput + "\"}";
assertEquals("Not the expected output file content", completeExpectedOutput, output);
}
class MockServletInputStream extends ServletInputStream {
InputStream inputStream;
MockServletInputStream(String string) {
this.inputStream = IOUtils.toInputStream(string);
}
@Override
public int read() throws IOException {
return inputStream.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
}
}