|
| 1 | +package com.slmdev.jsonapi.simple.response; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JavaType; |
| 4 | +import org.hamcrest.Matchers; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 12 | +import static org.hamcrest.Matchers.is; |
| 13 | +import static org.hamcrest.Matchers.nullValue; |
| 14 | + |
| 15 | +public class ResponseDeserializationTest extends BaseTest { |
| 16 | + @Test |
| 17 | + public void shouldDeserializeResponseWithDataAs1ObjectWithoutErrorsAndUriSpecified() throws Exception { |
| 18 | + final TestDto testDto = buildTestDto1(); |
| 19 | + final Response<Data<TestDto>> response = Response.<Data<TestDto>, TestDto>builder() |
| 20 | + .data(testDto) |
| 21 | + .build(); |
| 22 | + |
| 23 | + final String json = objectMapper.writeValueAsString(response); |
| 24 | + final JavaType dataType = objectMapper.getTypeFactory().constructParametricType(Data.class, TestDto.class); |
| 25 | + final Response<Data<TestDto>> deserealizedResponse = objectMapper.readValue(json, objectMapper.getTypeFactory().constructParametricType(Response.class, dataType)); |
| 26 | + |
| 27 | + assertThat(deserealizedResponse.getData().getAttributes().getId(), is(TEST_DTO_1_ID)); |
| 28 | + assertThat(deserealizedResponse.getData().getAttributes().getName(), is(TEST_DTO_1_NAME)); |
| 29 | + assertThat(deserealizedResponse.getData().getAttributes().getCreateDate(), is(TEST_DTO_1_DATE_CREATE)); |
| 30 | + assertThat(deserealizedResponse.getData().getId(), is(TEST_DTO_1_ID.toString())); |
| 31 | + assertThat(deserealizedResponse.getData().getType(), Matchers.is(TestDto.API_TYPE)); |
| 32 | + assertThat(deserealizedResponse.getData().getLinks().getSelf(), is(buildSelfLink(testDto))); |
| 33 | + assertThat(deserealizedResponse.getData().getLinks().getRelated(), nullValue()); |
| 34 | + |
| 35 | + assertThat(deserealizedResponse.getErrors(), nullValue()); |
| 36 | + |
| 37 | + assertThat(deserealizedResponse.getMeta().getApi().getVersion(), is("1")); |
| 38 | + assertThat(deserealizedResponse.getMeta().getPage().getMaxSize(), is(25)); |
| 39 | + assertThat(deserealizedResponse.getMeta().getPage().getTotal(), is(1L)); |
| 40 | + |
| 41 | + assertThatAttributesIdFieldIsPresentInObject(deserealizedResponse); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void shouldDeserializeResponseWithDataListWithoutErrorsAndUriSpecified() throws Exception { |
| 46 | + final TestDto testDto1 = buildTestDto1(); |
| 47 | + final TestDto testDto2 = buildTestDto2(); |
| 48 | + final Response<List<Data<TestDto>>> response = Response.<List<Data<TestDto>>, TestDto>builder() |
| 49 | + .data( |
| 50 | + Arrays.asList(testDto1, testDto2) |
| 51 | + ).build(); |
| 52 | + |
| 53 | + final String json = objectMapper.writeValueAsString(response); |
| 54 | + final JavaType dataType = objectMapper.getTypeFactory().constructParametricType(Data.class, TestDto.class); |
| 55 | + final JavaType listType = objectMapper.getTypeFactory().constructParametricType(List.class, dataType); |
| 56 | + final Response<List<Data<TestDto>>> deserealizedResponse = objectMapper.readValue(json, objectMapper.getTypeFactory().constructParametricType(Response.class, listType)); |
| 57 | + |
| 58 | + assertThat(deserealizedResponse.getData().get(0).getAttributes().getId(), is(TEST_DTO_1_ID)); |
| 59 | + assertThat(deserealizedResponse.getData().get(0).getAttributes().getName(), is(TEST_DTO_1_NAME)); |
| 60 | + assertThat(deserealizedResponse.getData().get(0).getAttributes().getCreateDate(), is(TEST_DTO_1_DATE_CREATE)); |
| 61 | + assertThat(deserealizedResponse.getData().get(0).getId(), is(TEST_DTO_1_ID.toString())); |
| 62 | + assertThat(deserealizedResponse.getData().get(0).getType(), Matchers.is(TestDto.API_TYPE)); |
| 63 | + assertThat(deserealizedResponse.getData().get(0).getLinks().getSelf(), is(buildSelfLink(testDto1))); |
| 64 | + assertThat(deserealizedResponse.getData().get(0).getLinks().getRelated(), nullValue()); |
| 65 | + |
| 66 | + assertThat(deserealizedResponse.getData().get(1).getAttributes().getId(), is(TEST_DTO_2_ID)); |
| 67 | + assertThat(deserealizedResponse.getData().get(1).getAttributes().getName(), is(TEST_DTO_2_NAME)); |
| 68 | + assertThat(deserealizedResponse.getData().get(1).getAttributes().getCreateDate(), is(TEST_DTO_2_DATE_CREATE)); |
| 69 | + assertThat(deserealizedResponse.getData().get(1).getId(), is(TEST_DTO_2_ID.toString())); |
| 70 | + assertThat(deserealizedResponse.getData().get(1).getType(), Matchers.is(TestDto.API_TYPE)); |
| 71 | + assertThat(deserealizedResponse.getData().get(1).getLinks().getSelf(), is(buildSelfLink(testDto2))); |
| 72 | + assertThat(deserealizedResponse.getData().get(1).getLinks().getRelated(), nullValue()); |
| 73 | + |
| 74 | + assertThat(deserealizedResponse.getErrors(), nullValue()); |
| 75 | + |
| 76 | + assertThat(deserealizedResponse.getMeta().getApi().getVersion(), is("1")); |
| 77 | + assertThat(deserealizedResponse.getMeta().getPage().getMaxSize(), is(25)); |
| 78 | + assertThat(deserealizedResponse.getMeta().getPage().getTotal(), is(2L)); |
| 79 | + |
| 80 | + assertThatAttributesIdFieldIsPresentInCollection(deserealizedResponse); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void shouldDeserializeResponseWithErrorWithData() throws Exception { |
| 85 | + final Response<Data<TestDto>> response = Response.<Data<TestDto>, TestDto>builder() |
| 86 | + .data(buildTestDto1()) |
| 87 | + .error(HttpStatus.BAD_REQUEST, ERROR_DESCRIPTION) |
| 88 | + .build(); |
| 89 | + |
| 90 | + final String json = objectMapper.writeValueAsString(response); |
| 91 | + final JavaType dataType = objectMapper.getTypeFactory().constructParametricType(Data.class, TestDto.class); |
| 92 | + final Response<Data<TestDto>> deserealizedResponse = objectMapper.readValue(json, objectMapper.getTypeFactory().constructParametricType(Response.class, dataType)); |
| 93 | + |
| 94 | + assertErrorResponse(deserealizedResponse); |
| 95 | + } |
| 96 | +} |
0 commit comments