|
1 | 1 | import com.fasterxml.jackson.core.JsonProcessingException; |
2 | 2 | import com.fasterxml.jackson.databind.JsonNode; |
3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; |
4 | | -import com.mashape.unirest.http.HttpResponse; |
5 | 4 | import com.mashape.unirest.http.Unirest; |
6 | 5 | import com.mashape.unirest.http.exceptions.UnirestException; |
7 | 6 | import io.github.cdimascio.japierrors.ApiError; |
8 | 7 | import io.github.cdimascio.japierrors.ApiErrorCreator; |
9 | 8 | import io.github.cdimascio.japierrors.basic.ApiErrorBasic; |
10 | | -import io.github.cdimascio.japierrors.wcp.ApiErrorWcp; |
11 | 9 | import org.junit.jupiter.api.BeforeAll; |
12 | 10 | import org.junit.jupiter.api.Test; |
13 | 11 |
|
@@ -38,25 +36,45 @@ public static void beforeAll() { |
38 | 36 | // } |
39 | 37 |
|
40 | 38 | @Test |
41 | | - public void unirestAutoConvert() { |
| 39 | + public void serializeToJson() { |
42 | 40 | 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) { |
46 | 50 | e.printStackTrace(); |
47 | 51 | fail("unexpected exception"); |
48 | 52 | } |
49 | 53 | } |
50 | 54 |
|
51 | 55 | @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() { |
53 | 72 | 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(); |
59 | 76 |
|
| 77 | + assertEquals(404, error.getCode()); |
60 | 78 | } catch (UnirestException e) { |
61 | 79 | e.printStackTrace(); |
62 | 80 | fail("unexpected exception"); |
|
0 commit comments