Skip to content

Commit 9de57d1

Browse files
author
Carmine DiMascio
committed
add tests
1 parent 1580b7f commit 9de57d1

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

src/test/java/UsageSpec.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import com.fasterxml.jackson.core.JsonProcessingException;
22
import com.fasterxml.jackson.databind.JsonNode;
33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import com.mashape.unirest.http.HttpResponse;
54
import com.mashape.unirest.http.Unirest;
65
import com.mashape.unirest.http.exceptions.UnirestException;
76
import io.github.cdimascio.japierrors.ApiError;
87
import io.github.cdimascio.japierrors.ApiErrorCreator;
98
import io.github.cdimascio.japierrors.basic.ApiErrorBasic;
10-
import io.github.cdimascio.japierrors.wcp.ApiErrorWcp;
119
import org.junit.jupiter.api.BeforeAll;
1210
import org.junit.jupiter.api.Test;
1311

@@ -38,25 +36,45 @@ public static void beforeAll() {
3836
// }
3937

4038
@Test
41-
public void unirestAutoConvert() {
39+
public void serializeToJson() {
4240
try {
43-
ApiErrorBasic error = Unirest.get("http://mockbin.org/status/404/not_found").asObject(ApiErrorBasic.class).getBody();
44-
assertEquals(404, error.getCode());
45-
} catch (UnirestException e) {
41+
ObjectMapper mapper = new ObjectMapper();
42+
String s = mapper.writeValueAsString(ApiError.badRequest());
43+
System.out.println(s);
44+
JsonNode json = mapper.readTree(s);
45+
assertNull(json.get("cause"));
46+
assertNull(json.get("message"));
47+
assertEquals(400, json.get("code").asInt());
48+
assertEquals("bad request", json.get("error").asText());
49+
} catch (IOException e) {
4650
e.printStackTrace();
4751
fail("unexpected exception");
4852
}
4953
}
5054

5155
@Test
52-
public void unirestJson() {
56+
public void deserializeToApiError() {
57+
58+
ObjectMapper mapper = new ObjectMapper();
59+
JsonNode json = mapper.createObjectNode()
60+
.put("code", 400)
61+
.put("error", "bad request")
62+
.put("extra", "junk");
63+
64+
ApiErrorBasic error = mapper.convertValue(json, ApiErrorBasic.class);
65+
66+
assertEquals(400, error.getCode());
67+
assertEquals("bad request", error.getError());
68+
}
69+
70+
@Test
71+
public void httpErrorToApiError() {
5372
try {
54-
HttpResponse<JsonNode> res = Unirest.get("http://mockbin.org/status/404/not_found").asObject(JsonNode.class);
55-
assertEquals(404, res.getStatus());
56-
JsonNode body = res.getBody();
57-
System.out.println(body);
58-
assertNull(body.get("cause"));
73+
ApiErrorBasic error = Unirest
74+
.get("http://mockbin.org/status/404/not_found")
75+
.asObject(ApiErrorBasic.class).getBody();
5976

77+
assertEquals(404, error.getCode());
6078
} catch (UnirestException e) {
6179
e.printStackTrace();
6280
fail("unexpected exception");

0 commit comments

Comments
 (0)