Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 4412cad

Browse files
fix: error during assertion when parsing GraphQL errors
Also added one more test scenario.
1 parent fdb32f4 commit 4412cad

File tree

7 files changed

+103
-1
lines changed

7 files changed

+103
-1
lines changed

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestError.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.Optional;
16+
17+
import static java.util.Objects.nonNull;
1518

1619
/**
1720
* An implementation of the {@link GraphQLError} interface for testing purposes.
@@ -22,9 +25,27 @@
2225
@AllArgsConstructor
2326
public class GraphQLTestError implements GraphQLError {
2427
private String message;
28+
@JsonTypeInfo(defaultImpl = JacksonFriendlySourceLocation.class, use = JsonTypeInfo.Id.CLASS)
2529
private List<SourceLocation> locations;
2630
@JsonTypeInfo(defaultImpl = ErrorType.class, use = JsonTypeInfo.Id.CLASS)
2731
private ErrorClassification errorType;
2832
private List<Object> path;
2933
private Map<String, Object> extensions;
34+
35+
@Override
36+
public String toString() {
37+
final StringBuilder sb = new StringBuilder();
38+
sb.append(Optional.of(message).orElse("Error without error message"));
39+
if (nonNull(locations) && !locations.isEmpty()) {
40+
sb.append(" at line ");
41+
locations.forEach(
42+
location -> sb
43+
.append(location.getLine())
44+
.append(", column ")
45+
.append(location.getColumn()).append(" in ")
46+
.append(Optional.ofNullable(location.getSourceName()).orElse("unnamed/unspecified source."))
47+
);
48+
}
49+
return sb.toString();
50+
}
3051
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import graphql.language.SourceLocation;
6+
import org.springframework.lang.Nullable;
7+
8+
/**
9+
* Allow deserialization of {@link graphql.language.SourceLocation}.
10+
*/
11+
public class JacksonFriendlySourceLocation extends SourceLocation {
12+
13+
@JsonCreator
14+
public JacksonFriendlySourceLocation(
15+
final @JsonProperty("line") int line,
16+
final @JsonProperty("column") int column,
17+
final @Nullable @JsonProperty("sourceName") String sourceName
18+
) {
19+
super(line, column, sourceName);
20+
}
21+
}

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/assertions/GraphQLErrorListAssertion.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public GraphQLErrorListAssertion(final GraphQLResponse graphQLResponse) {
3333
public GraphQLResponseAssertion hasNoErrors() {
3434
final List<? extends GraphQLError> graphQLErrors = getGraphQLErrors(graphQLResponse);
3535
if (nonNull(graphQLErrors) && !graphQLErrors.isEmpty()) {
36-
final String combinedMessage = graphQLErrors.stream().map(GraphQLError::getMessage)
36+
final String combinedMessage = graphQLErrors.stream()
37+
.map(GraphQLError::toString)
3738
.collect(Collectors.joining(System.lineSeparator()));
3839
fail(String.format("Expected no GraphQL errors, but got %s: %s", graphQLErrors.size(), combinedMessage));
3940
}

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLTestTemplateIntegrationTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.fasterxml.jackson.databind.node.ObjectNode;
55
import com.graphql.spring.boot.test.beans.FooBar;
6+
import graphql.GraphQLError;
67
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.DisplayName;
89
import org.junit.jupiter.api.Test;
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.boot.test.context.SpringBootTest;
1112
import org.springframework.boot.test.web.client.TestRestTemplate;
1213
import org.springframework.core.io.ResourceLoader;
14+
import org.springframework.http.HttpHeaders;
1315

16+
import java.awt.*;
1417
import java.io.IOException;
1518
import java.util.Collections;
1619
import java.util.List;
20+
import java.util.UUID;
1721

1822
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
1923
public class GraphQLTestTemplateIntegrationTest {
@@ -22,9 +26,13 @@ public class GraphQLTestTemplateIntegrationTest {
2226
private static final String SIMPLE_TEST_QUERY_WITH_FRAGMENTS = "simple-test-query-with-fragments.graphql";
2327
private static final String TEST_FRAGMENT_FILE = "foo-bar-fragment.graphql";
2428
private static final String QUERY_WITH_VARIABLES = "query-with-variables.graphql";
29+
private static final String COMPLEX_TEST_QUERY = "complex-query.graphql";
2530
private static final String MULTIPLE_QUERIES = "multiple-queries.graphql";
2631
private static final String INPUT_STRING_VALUE = "input-value";
2732
private static final String INPUT_STRING_NAME = "input";
33+
private static final String INPUT_HEADER_NAME = "headerName";
34+
private static final String TEST_HEADER_NAME = "x-test";
35+
private static final String TEST_HEADER_VALUE = String.valueOf(UUID.randomUUID());
2836

2937
@Autowired
3038
private ResourceLoader resourceLoader;
@@ -115,4 +123,33 @@ void testPerformWithOperationName() throws IOException {
115123
.assertThatNoErrorsArePresent()
116124
.assertThatField("$.data.otherQuery").asString().isEqualTo("TEST");
117125
}
126+
127+
@Test
128+
@DisplayName("Test perform with GraphQL errors.")
129+
void testPerformWithGraphQLError() throws IOException {
130+
graphQLTestTemplate.postForResource(SIMPLE_TEST_QUERY, Collections.singletonList(TEST_FRAGMENT_FILE))
131+
.assertThatDataField().isNotPresentOrNull()
132+
.and().assertThatNumberOfErrors().isOne()
133+
.and().assertThatListOfErrors().extracting(GraphQLError::getMessage)
134+
.allMatch(message -> message.contains("UnusedFragment"));
135+
}
136+
137+
@Test
138+
@DisplayName("Test perform with all possible inputs.")
139+
void testPerformWithAllInputs() throws IOException {
140+
// GIVEN
141+
final ObjectNode variables = objectMapper.createObjectNode();
142+
variables.put(INPUT_STRING_NAME, INPUT_STRING_VALUE);
143+
variables.put(INPUT_HEADER_NAME, TEST_HEADER_NAME);
144+
final HttpHeaders httpHeaders = new HttpHeaders();
145+
httpHeaders.add(TEST_HEADER_NAME, TEST_HEADER_VALUE);
146+
// WHEN - THEN
147+
graphQLTestTemplate
148+
.withHeaders(httpHeaders)
149+
.perform(COMPLEX_TEST_QUERY, "complexQuery", variables, Collections.singletonList(TEST_FRAGMENT_FILE))
150+
.assertThatNoErrorsArePresent()
151+
.assertThatField("$.data.queryWithHeader").asString().isEqualTo(TEST_HEADER_VALUE)
152+
.and().assertThatField("$.data.queryWithVariables").asString().isEqualTo(INPUT_STRING_VALUE)
153+
.and().assertThatField("$.data.fooBar").as(FooBar.class).isEqualTo(new FooBar("FOO", "BAR"));
154+
}
118155
}

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/beans/DummyQuery.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.graphql.spring.boot.test.beans;
22

3+
import graphql.kickstart.servlet.context.GraphQLServletContext;
34
import graphql.kickstart.tools.GraphQLQueryResolver;
5+
import graphql.schema.DataFetchingEnvironment;
46
import org.springframework.stereotype.Service;
57

68
import java.util.Optional;
@@ -26,4 +28,12 @@ public FooBar fooBar(String foo, String bar) {
2628
public String queryWithVariables(final String input) {
2729
return input;
2830
}
31+
32+
public String queryWithHeader(
33+
final String headerName,
34+
final DataFetchingEnvironment dataFetchingEnvironment
35+
) {
36+
return ((GraphQLServletContext) dataFetchingEnvironment.getContext()).getHttpServletRequest()
37+
.getHeader(headerName);
38+
}
2939
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
query simpleUnusedQuery {
2+
dummy
3+
}
4+
5+
query complexQuery($input: String!, $headerName: String!) {
6+
queryWithHeader(headerName: $headerName)
7+
queryWithVariables(input: $input)
8+
fooBar {
9+
...FooBarFragment
10+
}
11+
}

graphql-spring-boot-test/src/test/resources/test-schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ type Query {
1616
otherQuery: String!
1717
fooBar(foo: String, bar: String): FooBar!
1818
queryWithVariables(input: String!): String!
19+
queryWithHeader(headerName: String!): String
1920
}

0 commit comments

Comments
 (0)