|
| 1 | +package graphql.kickstart.spring.webclient.boot; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | +import static org.mockito.ArgumentMatchers.eq; |
| 8 | +import static org.mockito.ArgumentMatchers.isA; |
| 9 | +import static org.mockito.Mockito.spy; |
| 10 | +import static org.mockito.Mockito.times; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.databind.JsonNode; |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | +import java.util.List; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +class GraphQLResponseTest { |
| 19 | + |
| 20 | + private final ObjectMapper mockedMapper = spy(new ObjectMapper()); |
| 21 | + |
| 22 | + @Test |
| 23 | + void readRawResponse_throwsJsonProcessingException_isConvertedToCustomException() { |
| 24 | + GraphQLClientException e = assertThrows(GraphQLClientException.class, |
| 25 | + () -> constructResponse("no valid json")); |
| 26 | + assertEquals("Cannot read response 'no valid json'", e.getMessage()); |
| 27 | + } |
| 28 | + |
| 29 | + private GraphQLResponse constructResponse(String json) { |
| 30 | + return new GraphQLResponse(json, mockedMapper); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void getFieldName_dataIsNull_returnsNull() { |
| 35 | + GraphQLResponse response = constructResponse("{ \"data\": null }"); |
| 36 | + assertNull(response.get("test", String.class)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void getFieldName_notDataFieldExists_returnsNull() { |
| 41 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": null } }"); |
| 42 | + assertNull(response.get("test", String.class)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void getFieldName_dataFieldIsNull_returnsNull() { |
| 47 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": null } }"); |
| 48 | + assertNull(response.get("field", String.class)); |
| 49 | + verify(mockedMapper, times(0)).convertValue(null, String.class); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void getFieldName_dataFieldExists_returnsValue() { |
| 54 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": \"value\" } }"); |
| 55 | + String value = response.get("field", String.class); |
| 56 | + assertEquals("value", value); |
| 57 | + verify(mockedMapper, times(1)).convertValue(isA(JsonNode.class), eq(String.class)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void getFirstObject_dataIsNull_returnsNull() { |
| 62 | + GraphQLResponse response = constructResponse("{ \"data\": null }"); |
| 63 | + assertNull(response.getFirstObject(String.class)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void getFirstObject_notDataFieldExists_returnsNull() { |
| 68 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": null } }"); |
| 69 | + assertNull(response.getFirstObject(String.class)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void getList_dataIsNull_returnsEmptyList() { |
| 74 | + GraphQLResponse response = constructResponse("{ \"data\": null }"); |
| 75 | + assertNull(response.getData()); |
| 76 | + assertTrue(response.getList("test", String.class).isEmpty()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void getList_notDataFieldExists_returnsEmptyList() { |
| 81 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": null } }"); |
| 82 | + assertTrue(response.getList("test", String.class).isEmpty()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void getList_dataFieldExists_returnsList() { |
| 87 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": [\"value\"] } }"); |
| 88 | + List<String> values = response.getList("field", String.class); |
| 89 | + assertEquals(1, values.size()); |
| 90 | + assertEquals("value", values.get(0)); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments