Skip to content

Commit d542f33

Browse files
committed
Mods to support the FakeResponse class for mocking tests.
1 parent c7d0570 commit d542f33

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.BufferedReader;
44
import java.io.IOException;
55
import java.io.InputStreamReader;
6+
import java.io.Reader;
67
import java.util.List;
78
import java.util.Map;
89

@@ -23,26 +24,56 @@ public class JsonUtils {
2324
jacksonJson.getObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
2425
jacksonJson.getObjectMapper().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
2526
}
27+
2628

29+
static <T> T unmarshalResource(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
30+
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
31+
return (jacksonJson.unmarshal(returnType, reader));
32+
}
2733

28-
static <T> T unmarshal(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
29-
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream(filename));
34+
static <T> List<T> unmarshalResourceList(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
35+
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
36+
return (JsonUtils.unmarshalList(returnType, reader));
37+
}
38+
39+
static <T> Map<String, T> unmarshalResourceMap(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
40+
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
41+
return (jacksonJson.unmarshalMap(returnType, reader));
42+
}
43+
44+
static <T> T unmarshal(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
3045
return (jacksonJson.unmarshal(returnType, reader));
3146
}
3247

33-
static <T> List<T> unmarshalList(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
34-
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream(filename));
48+
static <T> T unmarshal(Class<T> returnType, String json) throws JsonParseException, JsonMappingException, IOException {
49+
return (jacksonJson.unmarshal(returnType, json));
50+
}
51+
52+
static <T> List<T> unmarshalList(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
3553
return (jacksonJson.unmarshalList(returnType, reader));
3654
}
3755

38-
static <T> Map<String, T> unmarshalMap(Class<T> returnType, String filename) throws JsonParseException, JsonMappingException, IOException {
39-
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream(filename));
56+
static <T> List<T> unmarshalList(Class<T> returnType, String json) throws JsonParseException, JsonMappingException, IOException {
57+
return (jacksonJson.unmarshalList(returnType, json));
58+
}
59+
60+
static <T> Map<String, T> unmarshalMap(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
4061
return (jacksonJson.unmarshalMap(returnType, reader));
4162
}
4263

64+
static <T> Map<String, T> unmarshalMap(Class<T> returnType, String json) throws JsonParseException, JsonMappingException, IOException {
65+
return (jacksonJson.unmarshalMap(returnType, json));
66+
}
67+
68+
69+
4370
static <T> boolean compareJson(T apiObject, String filename) throws IOException {
71+
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
72+
return (compareJson(apiObject, reader));
73+
}
74+
75+
static <T> boolean compareJson(T apiObject, InputStreamReader reader) throws IOException {
4476

45-
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream(filename));
4677
String objectJson = jacksonJson.marshal(apiObject);
4778
JsonNode tree1 = jacksonJson.getObjectMapper().readTree(objectJson.getBytes());
4879
JsonNode tree2 = jacksonJson.getObjectMapper().readTree(reader);
@@ -57,10 +88,11 @@ static <T> boolean compareJson(T apiObject, String filename) throws IOException
5788
return (sameJson);
5889
}
5990

91+
6092
static void sortedDump(final JsonNode node) throws JsonProcessingException {
6193
final Object obj = jacksonJson.getObjectMapper().treeToValue(node, Object.class);
62-
System.out.println(jacksonJson.getObjectMapper().writeValueAsString(obj));
63-
System.out.flush();
94+
System.err.println(jacksonJson.getObjectMapper().writeValueAsString(obj));
95+
System.err.flush();
6496
}
6597

6698
static String readResource(String filename) throws IOException {

0 commit comments

Comments
 (0)