|
2 | 2 |
|
3 | 3 |
|
4 | 4 | import static org.gitlab4j.api.JsonUtils.compareJson; |
| 5 | +import static org.gitlab4j.api.JsonUtils.readTreeFromResource; |
5 | 6 | import static org.gitlab4j.api.JsonUtils.unmarshalResource; |
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | +import static org.junit.Assert.assertNotNull; |
6 | 9 | import static org.junit.Assert.assertTrue; |
| 10 | +import static org.mockito.BDDMockito.given; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | + |
| 13 | +import java.util.logging.Level; |
| 14 | + |
| 15 | +import javax.servlet.ServletInputStream; |
| 16 | +import javax.servlet.http.HttpServletRequest; |
7 | 17 |
|
8 | 18 | import org.gitlab4j.api.systemhooks.MergeRequestSystemHookEvent; |
9 | 19 | import org.gitlab4j.api.systemhooks.ProjectSystemHookEvent; |
10 | 20 | import org.gitlab4j.api.systemhooks.PushSystemHookEvent; |
11 | 21 | import org.gitlab4j.api.systemhooks.SystemHookEvent; |
| 22 | +import org.gitlab4j.api.systemhooks.SystemHookListener; |
| 23 | +import org.gitlab4j.api.systemhooks.SystemHookManager; |
12 | 24 | import org.gitlab4j.api.systemhooks.TeamMemberSystemHookEvent; |
13 | 25 | import org.gitlab4j.api.utils.JacksonJson; |
14 | 26 | import org.gitlab4j.api.webhook.BuildEvent; |
|
20 | 32 | import org.gitlab4j.api.webhook.PushEvent; |
21 | 33 | import org.gitlab4j.api.webhook.TagPushEvent; |
22 | 34 | import org.gitlab4j.api.webhook.WikiPageEvent; |
| 35 | +import org.junit.AfterClass; |
23 | 36 | import org.junit.BeforeClass; |
24 | 37 | import org.junit.Test; |
25 | 38 |
|
| 39 | +import com.fasterxml.jackson.databind.JsonNode; |
26 | 40 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 41 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
27 | 42 |
|
28 | 43 | public class TestGitLabApiEvents { |
29 | 44 |
|
30 | 45 | private static JacksonJson jacksonJson; |
| 46 | + private static Level savedLevel; |
31 | 47 |
|
32 | 48 | public TestGitLabApiEvents() { |
33 | 49 | super(); |
34 | 50 | } |
35 | 51 |
|
36 | 52 | @BeforeClass |
37 | | - public static void setup() { |
| 53 | + public static void setup() throws Exception { |
38 | 54 | jacksonJson = new JacksonJson(); |
39 | 55 | jacksonJson.getObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); |
| 56 | + savedLevel = GitLabApi.getLogger().getLevel(); |
| 57 | + } |
| 58 | + |
| 59 | + @AfterClass |
| 60 | + public static void teardown() { |
| 61 | + GitLabApi.getLogger().setLevel(savedLevel); |
40 | 62 | } |
41 | 63 |
|
42 | 64 | @Test |
@@ -243,4 +265,37 @@ public void testPolymorphicSystemHookEvent() throws Exception { |
243 | 265 | event = unmarshalResource(SystemHookEvent.class, "merge-request-system-hook-event.json"); |
244 | 266 | assertTrue(compareJson(event, "merge-request-system-hook-event.json")); |
245 | 267 | } |
| 268 | + |
| 269 | + @Test |
| 270 | + public void testSystemHookManagerHandleEvent() throws Exception { |
| 271 | + |
| 272 | + // Turn off logging. This is a hack as if we don't turn it off the logging tests ran later will fail |
| 273 | + GitLabApi.getLogger().setLevel(Level.OFF);; |
| 274 | + |
| 275 | + // Arrange |
| 276 | + HttpServletRequest request = mock(HttpServletRequest.class); |
| 277 | + given(request.getHeader("X-Gitlab-Event")).willReturn(SystemHookManager.SYSTEM_HOOK_EVENT); |
| 278 | + |
| 279 | + JsonNode tree = readTreeFromResource("merge-request-system-hook-event.json"); |
| 280 | + ((ObjectNode)tree).remove("event_name"); |
| 281 | + String json = jacksonJson.getObjectMapper().writeValueAsString(tree); |
| 282 | + ServletInputStream servletInputStream = new MockServletInputStream(json); |
| 283 | + given(request.getInputStream()).willReturn(servletInputStream); |
| 284 | + |
| 285 | + SystemHookManager systemHookMgr = new SystemHookManager(); |
| 286 | + final SystemHookEvent receivedEvents[] = new SystemHookEvent[1]; |
| 287 | + systemHookMgr.addListener(new SystemHookListener() { |
| 288 | + public void onMergeRequestEvent(MergeRequestSystemHookEvent event) { |
| 289 | + receivedEvents[0] = event; |
| 290 | + } |
| 291 | + }); |
| 292 | + |
| 293 | + // Act |
| 294 | + systemHookMgr.handleEvent(request); |
| 295 | + |
| 296 | + // Assert |
| 297 | + assertNotNull(receivedEvents[0]); |
| 298 | + assertEquals(MergeRequestSystemHookEvent.class, receivedEvents[0].getClass()); |
| 299 | + assertTrue(compareJson(receivedEvents[0], "merge-request-system-hook-event.json")); |
| 300 | + } |
246 | 301 | } |
0 commit comments