|
22 | 22 | import java.util.List;
|
23 | 23 | import java.util.Map;
|
24 | 24 |
|
| 25 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
25 | 27 | import graphql.GraphQLError;
|
26 | 28 | import graphql.GraphqlErrorBuilder;
|
27 | 29 | import graphql.execution.ResultPath;
|
28 | 30 | import graphql.language.SourceLocation;
|
29 | 31 | import org.jspecify.annotations.Nullable;
|
30 | 32 | import org.junit.jupiter.api.Test;
|
31 |
| -import org.testcontainers.shaded.com.fasterxml.jackson.databind.DeserializationFeature; |
32 |
| -import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; |
33 | 33 |
|
| 34 | +import org.springframework.core.codec.DecodingException; |
34 | 35 | import org.springframework.graphql.ResponseError;
|
35 | 36 | import org.springframework.http.codec.json.JacksonJsonDecoder;
|
36 | 37 | import org.springframework.http.codec.json.JacksonJsonEncoder;
|
37 | 38 |
|
38 |
| - |
39 | 39 | import static org.assertj.core.api.Assertions.assertThat;
|
40 | 40 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 41 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
41 | 42 |
|
42 | 43 |
|
43 | 44 | /**
|
44 | 45 | * Unit tests for {@link DefaultClientGraphQlResponse}.
|
45 | 46 | * @author Rossen Stoyanchev
|
46 | 47 | */
|
47 |
| -class DefaultGraphQlClientResponseTests { |
| 48 | +class DefaultClientGraphQlResponseTests { |
48 | 49 |
|
49 | 50 | private static final ObjectMapper mapper = new ObjectMapper();
|
50 | 51 |
|
@@ -120,6 +121,37 @@ private void testFieldValueInvalidPath(String path, String json) {
|
120 | 121 | .withMessageStartingWith("Invalid path");
|
121 | 122 | }
|
122 | 123 |
|
| 124 | + |
| 125 | + @Test |
| 126 | + void fieldToEntity() throws Exception { |
| 127 | + ClientResponseField bookById = getFieldOnDataResponse("bookById", """ |
| 128 | + { |
| 129 | + "bookById": { |
| 130 | + "id": "42", |
| 131 | + "name": "Spring for GraphQL" |
| 132 | + } |
| 133 | + } |
| 134 | + """); |
| 135 | + Book book = bookById.toEntity(Book.class); |
| 136 | + assertThat(book).isEqualTo(new Book(42L, "Spring for GraphQL")); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void fieldToEntityWhenNullThrowsFieldAccessException() throws Exception { |
| 141 | + ClientGraphQlResponse response = createResponse("{ \"bookById\": null }", createError("/bookById", "fail-book")); |
| 142 | + assertThatThrownBy(() -> response.field("bookById").toEntity(Book.class)) |
| 143 | + .isInstanceOf(FieldAccessException.class).hasMessageContaining("Invalid field 'bookById'"); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void fieldToEntityWhenInvalidThrowsGraphQlClientException() throws Exception { |
| 148 | + ClientGraphQlResponse response = createResponse("{ \"bookById\": \"invalid\" }"); |
| 149 | + assertThatThrownBy(() -> response.field("bookById").toEntity(Book.class)) |
| 150 | + .isInstanceOf(GraphQlClientException.class) |
| 151 | + .hasMessageContaining("Cannot read field 'bookById'") |
| 152 | + .hasCauseInstanceOf(DecodingException.class); |
| 153 | + } |
| 154 | + |
123 | 155 | @Test
|
124 | 156 | void fieldErrors() {
|
125 | 157 |
|
@@ -181,11 +213,21 @@ private ClientResponseField getFieldOnErrorResponse(String path, GraphQLError...
|
181 | 213 | return response.field(path);
|
182 | 214 | }
|
183 | 215 |
|
| 216 | + private ClientGraphQlResponse createResponse(String dataJson, GraphQLError... errors) throws Exception { |
| 217 | + Map<?, ?> dataMap = mapper.readValue(dataJson, Map.class); |
| 218 | + List<?> errorList = Arrays.stream(errors).map(GraphQLError::toSpecification).toList(); |
| 219 | + return createResponse(Map.of("data", dataMap, "errors", errorList)); |
| 220 | + } |
| 221 | + |
184 | 222 | private ClientGraphQlResponse createResponse(Map<String, Object> responseMap) {
|
185 | 223 | return new DefaultClientGraphQlResponse(
|
186 | 224 | new DefaultClientGraphQlRequest("{test}", null, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()),
|
187 | 225 | new ResponseMapGraphQlResponse(responseMap),
|
188 | 226 | new JacksonJsonEncoder(), new JacksonJsonDecoder());
|
189 | 227 | }
|
190 | 228 |
|
| 229 | + record Book(Long id, String name) { |
| 230 | + |
| 231 | + } |
| 232 | + |
191 | 233 | }
|
0 commit comments