|
1 | 1 | package graphql.kickstart.spring.webclient.boot;
|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 4 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertNull;
|
5 | 6 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
6 | 7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
12 | 13 |
|
13 | 14 | import com.fasterxml.jackson.databind.JsonNode;
|
14 | 15 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 16 | +import com.jayway.jsonpath.PathNotFoundException; |
| 17 | + |
15 | 18 | import java.util.List;
|
| 19 | +import java.util.Map; |
| 20 | + |
16 | 21 | import org.junit.jupiter.api.Test;
|
17 | 22 |
|
18 | 23 | class GraphQLResponseTest {
|
@@ -95,4 +100,83 @@ void getList_dataFieldExists_returnsList() {
|
95 | 100 | assertEquals("value", values.get(0));
|
96 | 101 | }
|
97 | 102 |
|
| 103 | + @Test |
| 104 | + void getAt_dataFieldExists_returnsValue() { |
| 105 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { \"innerField\": \"value\" } } }"); |
| 106 | + String value = response.getAt("field.innerField", String.class); |
| 107 | + assertEquals("value", value); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void getAt_noDataFieldExists_throwsException() { |
| 112 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { } } }"); |
| 113 | + GraphQLResponseReadException ex = assertThrows(GraphQLResponseReadException.class, () -> response.getAt("field.innerField", String.class)); |
| 114 | + assertInstanceOf(PathNotFoundException.class, ex.getCause()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void getAt_dataIsNull_returnsNull() { |
| 119 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { \"innerField\": null } } }"); |
| 120 | + assertNull(response.getAt("field.innerField", String.class)); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void getListAt_dataFieldExists_returnsList() { |
| 125 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { \"innerField\": [\"value\"] } } }"); |
| 126 | + List<String> values = response.getListAt("field.innerField", String.class); |
| 127 | + assertEquals(1, values.size()); |
| 128 | + assertEquals("value", values.get(0)); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void getListAt_dataIsNull_returnsNull() { |
| 133 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { \"innerField\": null } } }"); |
| 134 | + assertNull(response.getListAt("field.innerField", String.class)); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void getListAt_noDataFieldExists_throwsException() { |
| 139 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { } } }"); |
| 140 | + GraphQLResponseReadException ex = assertThrows(GraphQLResponseReadException.class, () -> response.getListAt("field.innerField", String.class)); |
| 141 | + assertInstanceOf(PathNotFoundException.class, ex.getCause()); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void getAtAutoCast_dataFieldExists_returnsMap() { |
| 146 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { \"innerField\": { \"blah\": \"value\" } } } }"); |
| 147 | + Map<String, String> value = response.getAt("field.innerField"); |
| 148 | + assertEquals(1, value.size()); |
| 149 | + assertEquals("value", value.get("blah")); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void getAtAutoCast_dataFieldExists_returnsString() { |
| 154 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { \"innerField\": \"value\" } } }"); |
| 155 | + String value = response.getAt("field.innerField"); |
| 156 | + assertEquals("value", value); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void getAtAutoCast_dataFieldExists_returnsInt() { |
| 161 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { \"innerField\": 42 } } }"); |
| 162 | + Integer value = response.getAt("field.innerField"); |
| 163 | + assertEquals(42, value); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + void getAtAutoCast_dataFieldExists_returnsDouble() { |
| 168 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { \"innerField\": 42.5 } } }"); |
| 169 | + Double value = response.getAt("field.innerField"); |
| 170 | + assertEquals(42.5, value); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void getAtAutoCast_dataFieldExists_returnsList() { |
| 175 | + GraphQLResponse response = constructResponse("{ \"data\": { \"field\": { \"innerField\": [ \"value\", 42 ] } } }"); |
| 176 | + List<Object> values = response.getAt("field.innerField"); |
| 177 | + assertEquals(2, values.size()); |
| 178 | + assertEquals("value", values.get(0)); |
| 179 | + assertEquals(42, values.get(1)); |
| 180 | + } |
| 181 | + |
98 | 182 | }
|
0 commit comments