Skip to content

Commit 04a7698

Browse files
committed
Reorganized and added new system hook event tests (#332).
1 parent 26362d9 commit 04a7698

File tree

3 files changed

+134
-132
lines changed

3 files changed

+134
-132
lines changed

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@
8282
import org.gitlab4j.api.models.Variable;
8383
import org.gitlab4j.api.services.JiraService;
8484
import org.gitlab4j.api.services.SlackService;
85-
import org.gitlab4j.api.systemhooks.ProjectSystemHookEvent;
86-
import org.gitlab4j.api.systemhooks.PushSystemHookEvent;
87-
import org.gitlab4j.api.systemhooks.SystemHookEvent;
88-
import org.gitlab4j.api.systemhooks.TeamMemberSystemHookEvent;
8985
import org.junit.Test;
9086

9187
public class TestGitLabApiBeans {
@@ -442,56 +438,6 @@ public void testOauthToken() throws Exception {
442438
assertTrue(compareJson(token, "oauth-token.json"));
443439
}
444440

445-
@Test
446-
public void testProjectSystemHookEvent() throws Exception {
447-
ProjectSystemHookEvent event = unmarshalResource(ProjectSystemHookEvent.class,
448-
"project-system-hook-event.json");
449-
assertTrue(compareJson(event, "project-system-hook-event.json"));
450-
}
451-
452-
@Test
453-
public void testTeamMemberSystemHookEvent() throws Exception {
454-
TeamMemberSystemHookEvent event = unmarshalResource(TeamMemberSystemHookEvent.class,
455-
"team-member-system-hook-event.json");
456-
assertTrue(compareJson(event, "team-member-system-hook-event.json"));
457-
}
458-
459-
@Test
460-
public void testPushSystemHookEvent() throws Exception {
461-
PushSystemHookEvent event = unmarshalResource(PushSystemHookEvent.class, "push-system-hook-event.json");
462-
assertTrue(compareJson(event, "push-system-hook-event.json"));
463-
}
464-
465-
@Test
466-
public void testUserSystemHookEvent() throws Exception {
467-
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "user-system-hook-event.json");
468-
assertTrue(compareJson(event, "user-system-hook-event.json"));
469-
}
470-
471-
@Test
472-
public void testGroupSystemHookEvent() throws Exception {
473-
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "group-system-hook-event.json");
474-
assertTrue(compareJson(event, "group-system-hook-event.json"));
475-
}
476-
477-
@Test
478-
public void testGroupMemberSystemHookEvent() throws Exception {
479-
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "group-member-system-hook-event.json");
480-
assertTrue(compareJson(event, "group-member-system-hook-event.json"));
481-
}
482-
483-
@Test
484-
public void testTagPushSystemHookEvent() throws Exception {
485-
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "tag-push-system-hook-event.json");
486-
assertTrue(compareJson(event, "tag-push-system-hook-event.json"));
487-
}
488-
489-
@Test
490-
public void testRepositorySystemHookEvent() throws Exception {
491-
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "repository-system-hook-event.json");
492-
assertTrue(compareJson(event, "repository-system-hook-event.json"));
493-
}
494-
495441
@Test
496442
public void testLabels() throws Exception {
497443
List<Label> labels = unmarshalResourceList(Label.class, "labels.json");
Lines changed: 133 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package org.gitlab4j.api;
22

3-
import static org.junit.Assert.assertTrue;
43

5-
import java.io.IOException;
6-
import java.io.InputStreamReader;
4+
import static org.gitlab4j.api.JsonUtils.compareJson;
5+
import static org.gitlab4j.api.JsonUtils.unmarshalResource;
6+
import static org.junit.Assert.assertTrue;
77

88
import org.gitlab4j.api.systemhooks.MergeRequestSystemHookEvent;
9+
import org.gitlab4j.api.systemhooks.ProjectSystemHookEvent;
10+
import org.gitlab4j.api.systemhooks.PushSystemHookEvent;
11+
import org.gitlab4j.api.systemhooks.SystemHookEvent;
12+
import org.gitlab4j.api.systemhooks.TeamMemberSystemHookEvent;
913
import org.gitlab4j.api.utils.JacksonJson;
1014
import org.gitlab4j.api.webhook.BuildEvent;
1115
import org.gitlab4j.api.webhook.Event;
@@ -19,11 +23,6 @@
1923
import org.junit.BeforeClass;
2024
import org.junit.Test;
2125

22-
import com.fasterxml.jackson.core.JsonParseException;
23-
import com.fasterxml.jackson.core.JsonProcessingException;
24-
import com.fasterxml.jackson.databind.JsonMappingException;
25-
import com.fasterxml.jackson.databind.JsonNode;
26-
import com.fasterxml.jackson.databind.ObjectMapper;
2726
import com.fasterxml.jackson.databind.SerializationFeature;
2827

2928
public class TestGitLabApiEvents {
@@ -43,149 +42,205 @@ public static void setup() {
4342
@Test
4443
public void testIssueEvent() throws Exception {
4544

46-
Event issueEvent = makeFakeApiCall(IssueEvent.class, "issue-event");
47-
assertTrue(compareJson(issueEvent, "issue-event"));
45+
Event issueEvent = unmarshalResource(IssueEvent.class, "issue-event.json");
46+
assertTrue(compareJson(issueEvent, "issue-event.json"));
4847
}
4948

5049
@Test
5150
public void testMergeRequestEvent() throws Exception {
5251

53-
Event mergeRequestEvent = makeFakeApiCall(MergeRequestEvent.class, "merge-request-event");
54-
assertTrue(compareJson(mergeRequestEvent, "merge-request-event"));
55-
}
56-
57-
@Test
58-
public void testMergeRequestSystemHookEvent() throws Exception {
59-
60-
MergeRequestSystemHookEvent mergeRequestEvent = makeFakeApiCall(MergeRequestSystemHookEvent.class, "merge-request-system-hook-event");
61-
assertTrue(compareJson(mergeRequestEvent, "merge-request-system-hook-event"));
52+
Event mergeRequestEvent = unmarshalResource(MergeRequestEvent.class, "merge-request-event.json");
53+
assertTrue(compareJson(mergeRequestEvent, "merge-request-event.json"));
6254
}
6355

6456
@Test
6557
public void testPipelineEvent() throws Exception {
6658

67-
Event event = makeFakeApiCall(PipelineEvent.class, "pipeline-event");
68-
assertTrue(compareJson(event, "pipeline-event"));
59+
Event event = unmarshalResource(PipelineEvent.class, "pipeline-event.json");
60+
assertTrue(compareJson(event, "pipeline-event.json"));
6961
}
7062

7163
@Test
7264
public void testPushEvent() throws Exception {
7365

74-
Event pushEvent = makeFakeApiCall(PushEvent.class, "push-event");
75-
assertTrue(compareJson(pushEvent, "push-event"));
66+
Event pushEvent = unmarshalResource(PushEvent.class, "push-event.json");
67+
assertTrue(compareJson(pushEvent, "push-event.json"));
7668
}
7769

7870
@Test
7971
public void testTagPushEvent() throws Exception {
8072

81-
Event pushEvent = makeFakeApiCall(TagPushEvent.class, "tag-push-event");
82-
assertTrue(compareJson(pushEvent, "tag-push-event"));
73+
Event pushEvent = unmarshalResource(TagPushEvent.class, "tag-push-event.json");
74+
assertTrue(compareJson(pushEvent, "tag-push-event.json"));
8375
}
8476

8577
@Test
8678
public void testNoteCommitEvent() throws Exception {
8779

88-
Event noteEvent = makeFakeApiCall(NoteEvent.class, "note-commit-event");
89-
assertTrue(compareJson(noteEvent, "note-commit-event"));
80+
Event noteEvent = unmarshalResource(NoteEvent.class, "note-commit-event.json");
81+
assertTrue(compareJson(noteEvent, "note-commit-event.json"));
9082
}
9183

9284
@Test
9385
public void testNoteMergeRequestEvent() throws Exception {
9486

95-
Event noteEvent = makeFakeApiCall(NoteEvent.class, "note-merge-request-event");
96-
assertTrue(compareJson(noteEvent, "note-merge-request-event"));
87+
Event noteEvent = unmarshalResource(NoteEvent.class, "note-merge-request-event.json");
88+
assertTrue(compareJson(noteEvent, "note-merge-request-event.json"));
9789
}
9890

9991
@Test
10092
public void testNoteIssueEvent() throws Exception {
10193

102-
Event noteEvent = makeFakeApiCall(NoteEvent.class, "note-issue-event");
103-
assertTrue(compareJson(noteEvent, "note-issue-event"));
94+
Event noteEvent = unmarshalResource(NoteEvent.class, "note-issue-event.json");
95+
assertTrue(compareJson(noteEvent, "note-issue-event.json"));
10496
}
10597

10698
@Test
10799
public void testNoteSnippetEvent() throws Exception {
108100

109-
Event noteEvent = makeFakeApiCall(NoteEvent.class, "note-snippet-event");
110-
assertTrue(compareJson(noteEvent, "note-snippet-event"));
101+
Event noteEvent = unmarshalResource(NoteEvent.class, "note-snippet-event.json");
102+
assertTrue(compareJson(noteEvent, "note-snippet-event.json"));
111103
}
112104

113105
@Test
114106
public void testBuildEvent() throws Exception {
115107

116-
Event event = makeFakeApiCall(BuildEvent.class, "build-event");
117-
assertTrue(compareJson(event, "build-event"));
108+
Event event = unmarshalResource(BuildEvent.class, "build-event.json");
109+
assertTrue(compareJson(event, "build-event.json"));
118110
}
119111

120112
@Test
121113
public void testWikiPageEvent() throws Exception {
122114

123-
Event event = makeFakeApiCall(WikiPageEvent.class, "wiki-page-event");
124-
assertTrue(compareJson(event, "wiki-page-event"));
115+
Event event = unmarshalResource(WikiPageEvent.class, "wiki-page-event.json");
116+
assertTrue(compareJson(event, "wiki-page-event.json"));
125117
}
126118

127119
@Test
128120
public void testPolymorphicEvent() throws Exception {
129121

130-
Event event = makeFakeApiCall(Event.class, "build-event");
131-
assertTrue(compareJson(event, "build-event"));
122+
Event event = unmarshalResource(Event.class, "build-event.json");
123+
assertTrue(compareJson(event, "build-event.json"));
124+
125+
event = unmarshalResource(Event.class, "issue-event.json");
126+
assertTrue(compareJson(event, "issue-event.json"));
127+
128+
event = unmarshalResource(Event.class, "merge-request-event.json");
129+
assertTrue(compareJson(event, "merge-request-event.json"));
130+
131+
event = unmarshalResource(Event.class, "note-commit-event.json");
132+
assertTrue(compareJson(event, "note-commit-event.json"));
132133

133-
event = makeFakeApiCall(Event.class, "issue-event");
134-
assertTrue(compareJson(event, "issue-event"));
134+
event = unmarshalResource(Event.class, "note-issue-event.json");
135+
assertTrue(compareJson(event, "note-issue-event.json"));
135136

136-
event = makeFakeApiCall(Event.class, "merge-request-event");
137-
assertTrue(compareJson(event, "merge-request-event"));
137+
event = unmarshalResource(Event.class, "note-merge-request-event.json");
138+
assertTrue(compareJson(event, "note-merge-request-event.json"));
138139

139-
event = makeFakeApiCall(Event.class, "note-commit-event");
140-
assertTrue(compareJson(event, "note-commit-event"));
140+
event = unmarshalResource(Event.class, "note-snippet-event.json");
141+
assertTrue(compareJson(event, "note-snippet-event.json"));
141142

142-
event = makeFakeApiCall(Event.class, "note-issue-event");
143-
assertTrue(compareJson(event, "note-issue-event"));
143+
event = unmarshalResource(Event.class, "pipeline-event.json");
144+
assertTrue(compareJson(event, "pipeline-event.json"));
144145

145-
event = makeFakeApiCall(Event.class, "note-merge-request-event");
146-
assertTrue(compareJson(event, "note-merge-request-event"));
146+
event = unmarshalResource(Event.class, "push-event.json");
147+
assertTrue(compareJson(event, "push-event.json"));
147148

148-
event = makeFakeApiCall(Event.class, "note-snippet-event");
149-
assertTrue(compareJson(event, "note-snippet-event"));
149+
event = unmarshalResource(Event.class, "tag-push-event.json");
150+
assertTrue(compareJson(event, "tag-push-event.json"));
150151

151-
event = makeFakeApiCall(Event.class, "pipeline-event");
152-
assertTrue(compareJson(event, "pipeline-event"));
152+
event = unmarshalResource(Event.class, "wiki-page-event.json");
153+
assertTrue(compareJson(event, "wiki-page-event.json"));
154+
}
155+
156+
157+
@Test
158+
public void testProjectSystemHookEvent() throws Exception {
159+
ProjectSystemHookEvent event = unmarshalResource(ProjectSystemHookEvent.class,
160+
"project-system-hook-event.json");
161+
assertTrue(compareJson(event, "project-system-hook-event.json"));
162+
}
153163

154-
event = makeFakeApiCall(Event.class, "push-event");
155-
assertTrue(compareJson(event, "push-event"));
164+
@Test
165+
public void testTeamMemberSystemHookEvent() throws Exception {
166+
TeamMemberSystemHookEvent event = unmarshalResource(TeamMemberSystemHookEvent.class,
167+
"team-member-system-hook-event.json");
168+
assertTrue(compareJson(event, "team-member-system-hook-event.json"));
169+
}
170+
171+
@Test
172+
public void testPushSystemHookEvent() throws Exception {
173+
PushSystemHookEvent event = unmarshalResource(PushSystemHookEvent.class, "push-system-hook-event.json");
174+
assertTrue(compareJson(event, "push-system-hook-event.json"));
175+
}
156176

157-
event = makeFakeApiCall(Event.class, "tag-push-event");
158-
assertTrue(compareJson(event, "tag-push-event"));
177+
@Test
178+
public void testUserSystemHookEvent() throws Exception {
179+
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "user-system-hook-event.json");
180+
assertTrue(compareJson(event, "user-system-hook-event.json"));
181+
}
159182

160-
event = makeFakeApiCall(Event.class, "wiki-page-event");
161-
assertTrue(compareJson(event, "wiki-page-event"));
183+
@Test
184+
public void testGroupSystemHookEvent() throws Exception {
185+
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "group-system-hook-event.json");
186+
assertTrue(compareJson(event, "group-system-hook-event.json"));
162187
}
163188

164-
private <T> T makeFakeApiCall(Class<T> returnType, String file) throws JsonParseException, JsonMappingException, IOException {
189+
@Test
190+
public void testGroupMemberSystemHookEvent() throws Exception {
191+
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "group-member-system-hook-event.json");
192+
assertTrue(compareJson(event, "group-member-system-hook-event.json"));
193+
}
165194

166-
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream(file + ".json"));
167-
ObjectMapper objectMapper = jacksonJson.getContext(returnType);
168-
return (objectMapper.readValue(reader, returnType));
195+
@Test
196+
public void testTagPushSystemHookEvent() throws Exception {
197+
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "tag-push-system-hook-event.json");
198+
assertTrue(compareJson(event, "tag-push-system-hook-event.json"));
169199
}
170200

171-
private <T> boolean compareJson(T apiObject, String file) throws IOException {
201+
@Test
202+
public void testRepositorySystemHookEvent() throws Exception {
203+
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "repository-system-hook-event.json");
204+
assertTrue(compareJson(event, "repository-system-hook-event.json"));
205+
}
172206

173-
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream(file + ".json"));
174-
String objectJson = jacksonJson.marshal(apiObject);
175-
JsonNode tree1 = jacksonJson.getObjectMapper().readTree(objectJson.getBytes());
176-
JsonNode tree2 = jacksonJson.getObjectMapper().readTree(reader);
207+
@Test
208+
public void testMergeRequestSystemHookEvent() throws Exception {
209+
MergeRequestSystemHookEvent mergeRequestEvent = unmarshalResource(MergeRequestSystemHookEvent.class, "merge-request-system-hook-event.json");
210+
assertTrue(compareJson(mergeRequestEvent, "merge-request-system-hook-event.json"));
177211

178-
boolean sameJson = tree1.equals(tree2);
179-
if (!sameJson) {
180-
System.out.println("JSON did not match:");
181-
sortedDump(tree1);
182-
sortedDump(tree2);
183-
}
184-
return (sameJson);
212+
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "merge-request-system-hook-event.json");
213+
assertTrue(compareJson(event, "merge-request-system-hook-event.json"));
185214
}
186215

187-
private void sortedDump(final JsonNode node) throws JsonProcessingException {
188-
final Object obj = jacksonJson.getObjectMapper().treeToValue(node, Object.class);
189-
System.out.println(jacksonJson.getObjectMapper().writeValueAsString(obj));
216+
@Test
217+
public void testPolymorphicSystemHookEvent() throws Exception {
218+
219+
SystemHookEvent event = unmarshalResource(SystemHookEvent.class, "project-system-hook-event.json");
220+
assertTrue(compareJson(event, "project-system-hook-event.json"));
221+
222+
event = unmarshalResource(SystemHookEvent.class, "team-member-system-hook-event.json");
223+
assertTrue(compareJson(event, "team-member-system-hook-event.json"));
224+
225+
event = unmarshalResource(PushSystemHookEvent.class, "push-system-hook-event.json");
226+
assertTrue(compareJson(event, "push-system-hook-event.json"));
227+
228+
event = unmarshalResource(SystemHookEvent.class, "user-system-hook-event.json");
229+
assertTrue(compareJson(event, "user-system-hook-event.json"));
230+
231+
event = unmarshalResource(SystemHookEvent.class, "group-system-hook-event.json");
232+
assertTrue(compareJson(event, "group-system-hook-event.json"));
233+
234+
event = unmarshalResource(SystemHookEvent.class, "group-member-system-hook-event.json");
235+
assertTrue(compareJson(event, "group-member-system-hook-event.json"));
236+
237+
event = unmarshalResource(SystemHookEvent.class, "tag-push-system-hook-event.json");
238+
assertTrue(compareJson(event, "tag-push-system-hook-event.json"));
239+
240+
event = unmarshalResource(SystemHookEvent.class, "repository-system-hook-event.json");
241+
assertTrue(compareJson(event, "repository-system-hook-event.json"));
242+
243+
event = unmarshalResource(SystemHookEvent.class, "merge-request-system-hook-event.json");
244+
assertTrue(compareJson(event, "merge-request-system-hook-event.json"));
190245
}
191246
}

src/test/resources/org/gitlab4j/api/merge-request-system-hook-event.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"object_kind": "merge_request",
3+
"event_name": "merge_request",
34
"user": {
45
"name": "Administrator",
56
"username": "root",

0 commit comments

Comments
 (0)