|
| 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