Skip to content

Commit 1580b7f

Browse files
author
Carmine DiMascio
committed
add new tests
1 parent 506486a commit 1580b7f

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<main.class>io.github.cdimascio.japierrors.ApiErrors</main.class>
5555
<junit.version>4.12</junit.version>
5656
<jackson.version>2.9.7</jackson.version>
57+
<unirest.version>1.4.9</unirest.version>
5758
<junit.jupiter.version>5.3.1</junit.jupiter.version>
5859
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5960

@@ -88,6 +89,13 @@
8889
<scope>test</scope>
8990
</dependency>
9091

92+
<dependency>
93+
<groupId>com.mashape.unirest</groupId>
94+
<artifactId>unirest-java</artifactId>
95+
<version>${unirest.version}</version>
96+
<scope>test</scope>
97+
</dependency>
98+
9199
<dependency>
92100
<groupId>org.junit.jupiter</groupId>
93101
<artifactId>junit-jupiter-api</artifactId>

src/test/java/UsageSpec.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import com.fasterxml.jackson.core.JsonProcessingException;
2+
import com.fasterxml.jackson.databind.JsonNode;
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.mashape.unirest.http.HttpResponse;
5+
import com.mashape.unirest.http.Unirest;
6+
import com.mashape.unirest.http.exceptions.UnirestException;
7+
import io.github.cdimascio.japierrors.ApiError;
8+
import io.github.cdimascio.japierrors.ApiErrorCreator;
9+
import io.github.cdimascio.japierrors.basic.ApiErrorBasic;
10+
import io.github.cdimascio.japierrors.wcp.ApiErrorWcp;
11+
import org.junit.jupiter.api.BeforeAll;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.io.IOException;
15+
16+
import static org.junit.jupiter.api.Assertions.*;
17+
18+
public class UsageSpec {
19+
private ObjectMapper m = new ObjectMapper();
20+
21+
@BeforeAll
22+
public static void beforeAll() {
23+
configureUnirest();
24+
ApiError.creator(ApiErrorCreator.BASIC);
25+
}
26+
27+
// @Test
28+
// public void autoConvert() {
29+
// ApiErrorBasic notFound = ApiError.unauthorized();
30+
// assertEquals(401, notFound.getCode());
31+
// }
32+
//
33+
// @Test
34+
// public void throwsClassCastIfWrongType() {
35+
// assertThrows(ClassCastException.class, () -> {
36+
// ApiErrorWcp notFound = ApiError.notFound();
37+
// });
38+
// }
39+
40+
@Test
41+
public void unirestAutoConvert() {
42+
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) {
46+
e.printStackTrace();
47+
fail("unexpected exception");
48+
}
49+
}
50+
51+
@Test
52+
public void unirestJson() {
53+
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"));
59+
60+
} catch (UnirestException e) {
61+
e.printStackTrace();
62+
fail("unexpected exception");
63+
}
64+
}
65+
66+
private static void configureUnirest() {
67+
ObjectMapper mapper = new ObjectMapper();
68+
Unirest.setObjectMapper(new com.mashape.unirest.http.ObjectMapper() {
69+
@Override
70+
public <T> T readValue(String value, Class<T> valueType) {
71+
try {
72+
return mapper.readValue(value, valueType);
73+
} catch (IOException e) {
74+
throw new RuntimeException(e);
75+
}
76+
}
77+
78+
@Override
79+
public String writeValue(Object value) {
80+
try {
81+
return mapper.writeValueAsString(value);
82+
} catch (JsonProcessingException e) {
83+
throw new RuntimeException(e);
84+
}
85+
}
86+
});
87+
}
88+
}
89+

0 commit comments

Comments
 (0)