Commit cbd070d5 authored by Greg Messner's avatar Greg Messner
Browse files

Added testing of SystemHookManager.handleEvent() (#332).

parent afdd3ef3
...@@ -25,7 +25,11 @@ public class JsonUtils { ...@@ -25,7 +25,11 @@ public class JsonUtils {
jacksonJson.getObjectMapper().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); jacksonJson.getObjectMapper().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
} }
static JsonNode readTreeFromResource(String filename) throws JsonParseException, JsonMappingException, IOException {
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
return (jacksonJson.readTree(reader));
}
static <T> T unmarshalResource(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException { static <T> T unmarshalResource(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename)); InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
return (jacksonJson.unmarshal(returnType, reader)); return (jacksonJson.unmarshal(returnType, reader));
...@@ -45,6 +49,10 @@ public class JsonUtils { ...@@ -45,6 +49,10 @@ public class JsonUtils {
return (jacksonJson.unmarshal(returnType, reader)); return (jacksonJson.unmarshal(returnType, reader));
} }
static <T> T unmarshal(Class<T> returnType, JsonNode tree) throws JsonParseException, JsonMappingException, IOException {
return (jacksonJson.unmarshal(returnType, tree));
}
static <T> T unmarshal(Class<T> returnType, String json) throws JsonParseException, JsonMappingException, IOException { static <T> T unmarshal(Class<T> returnType, String json) throws JsonParseException, JsonMappingException, IOException {
return (jacksonJson.unmarshal(returnType, json)); return (jacksonJson.unmarshal(returnType, json));
} }
......
package org.gitlab4j.api;
import java.io.IOException;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
public class MockServletInputStream extends ServletInputStream {
private byte[] inputBytes;
private int lastIndexRetrieved = -1;
private ReadListener readListener = null;
public MockServletInputStream(String data) {
inputBytes = data.getBytes();
}
@Override
public boolean isFinished() {
return (lastIndexRetrieved == inputBytes.length-1);
}
@Override
public boolean isReady() {
// This implementation will never block
// We also never need to call the readListener from this method, as this method will never return false
return isFinished();
}
@Override
public void setReadListener(ReadListener readListener) {
this.readListener = readListener;
if (!isFinished()) {
try {
readListener.onDataAvailable();
} catch (IOException e) {
readListener.onError(e);
}
} else {
try {
readListener.onAllDataRead();
} catch (IOException e) {
readListener.onError(e);
}
}
}
@Override
public int read() throws IOException {
int i;
if (!isFinished()) {
i = inputBytes[lastIndexRetrieved+1];
lastIndexRetrieved++;
if (isFinished() && (readListener != null)) {
try {
readListener.onAllDataRead();
} catch (IOException ex) {
readListener.onError(ex);
throw ex;
}
}
return i;
} else {
return -1;
}
}
}
\ No newline at end of file
...@@ -2,13 +2,25 @@ package org.gitlab4j.api; ...@@ -2,13 +2,25 @@ package org.gitlab4j.api;
import static org.gitlab4j.api.JsonUtils.compareJson; import static org.gitlab4j.api.JsonUtils.compareJson;
import static org.gitlab4j.api.JsonUtils.readTreeFromResource;
import static org.gitlab4j.api.JsonUtils.unmarshalResource; import static org.gitlab4j.api.JsonUtils.unmarshalResource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import java.util.logging.Level;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import org.gitlab4j.api.systemhooks.MergeRequestSystemHookEvent; import org.gitlab4j.api.systemhooks.MergeRequestSystemHookEvent;
import org.gitlab4j.api.systemhooks.ProjectSystemHookEvent; import org.gitlab4j.api.systemhooks.ProjectSystemHookEvent;
import org.gitlab4j.api.systemhooks.PushSystemHookEvent; import org.gitlab4j.api.systemhooks.PushSystemHookEvent;
import org.gitlab4j.api.systemhooks.SystemHookEvent; import org.gitlab4j.api.systemhooks.SystemHookEvent;
import org.gitlab4j.api.systemhooks.SystemHookListener;
import org.gitlab4j.api.systemhooks.SystemHookManager;
import org.gitlab4j.api.systemhooks.TeamMemberSystemHookEvent; import org.gitlab4j.api.systemhooks.TeamMemberSystemHookEvent;
import org.gitlab4j.api.utils.JacksonJson; import org.gitlab4j.api.utils.JacksonJson;
import org.gitlab4j.api.webhook.BuildEvent; import org.gitlab4j.api.webhook.BuildEvent;
...@@ -20,23 +32,33 @@ import org.gitlab4j.api.webhook.PipelineEvent; ...@@ -20,23 +32,33 @@ import org.gitlab4j.api.webhook.PipelineEvent;
import org.gitlab4j.api.webhook.PushEvent; import org.gitlab4j.api.webhook.PushEvent;
import org.gitlab4j.api.webhook.TagPushEvent; import org.gitlab4j.api.webhook.TagPushEvent;
import org.gitlab4j.api.webhook.WikiPageEvent; import org.gitlab4j.api.webhook.WikiPageEvent;
import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class TestGitLabApiEvents { public class TestGitLabApiEvents {
private static JacksonJson jacksonJson; private static JacksonJson jacksonJson;
private static Level savedLevel;
public TestGitLabApiEvents() { public TestGitLabApiEvents() {
super(); super();
} }
@BeforeClass @BeforeClass
public static void setup() { public static void setup() throws Exception {
jacksonJson = new JacksonJson(); jacksonJson = new JacksonJson();
jacksonJson.getObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); jacksonJson.getObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
savedLevel = GitLabApi.getLogger().getLevel();
}
@AfterClass
public static void teardown() {
GitLabApi.getLogger().setLevel(savedLevel);
} }
@Test @Test
...@@ -243,4 +265,37 @@ public class TestGitLabApiEvents { ...@@ -243,4 +265,37 @@ public class TestGitLabApiEvents {
event = unmarshalResource(SystemHookEvent.class, "merge-request-system-hook-event.json"); event = unmarshalResource(SystemHookEvent.class, "merge-request-system-hook-event.json");
assertTrue(compareJson(event, "merge-request-system-hook-event.json")); assertTrue(compareJson(event, "merge-request-system-hook-event.json"));
} }
@Test
public void testSystemHookManagerHandleEvent() throws Exception {
// Turn off logging. This is a hack as if we don't turn it off the logging tests ran later will fail
GitLabApi.getLogger().setLevel(Level.OFF);;
// Arrange
HttpServletRequest request = mock(HttpServletRequest.class);
given(request.getHeader("X-Gitlab-Event")).willReturn(SystemHookManager.SYSTEM_HOOK_EVENT);
JsonNode tree = readTreeFromResource("merge-request-system-hook-event.json");
((ObjectNode)tree).remove("event_name");
String json = jacksonJson.getObjectMapper().writeValueAsString(tree);
ServletInputStream servletInputStream = new MockServletInputStream(json);
given(request.getInputStream()).willReturn(servletInputStream);
SystemHookManager systemHookMgr = new SystemHookManager();
final SystemHookEvent receivedEvents[] = new SystemHookEvent[1];
systemHookMgr.addListener(new SystemHookListener() {
public void onMergeRequestEvent(MergeRequestSystemHookEvent event) {
receivedEvents[0] = event;
}
});
// Act
systemHookMgr.handleEvent(request);
// Assert
assertNotNull(receivedEvents[0]);
assertEquals(MergeRequestSystemHookEvent.class, receivedEvents[0].getClass());
assertTrue(compareJson(receivedEvents[0], "merge-request-system-hook-event.json"));
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment