Skip to content

Commit 53fb1d4

Browse files
committed
Update and expand GraphQlTester tests
See gh-317
1 parent 29b1aa2 commit 53fb1d4

File tree

9 files changed

+811
-428
lines changed

9 files changed

+811
-428
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.test.tester;
18+
19+
import graphql.GraphqlErrorBuilder;
20+
import org.junit.jupiter.api.Test;
21+
import reactor.core.publisher.Mono;
22+
23+
import org.springframework.graphql.GraphQlService;
24+
import org.springframework.graphql.support.DocumentSource;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link GraphQlTester} builder with a mock {@link GraphQlService}.
30+
*
31+
* @author Rossen Stoyanchev
32+
*/
33+
public class GraphQlTesterBuilderTests extends GraphQlTesterTestSupport {
34+
35+
private static final String DOCUMENT = "{ Query }";
36+
37+
38+
@Test
39+
void mutateDocumentSource() {
40+
41+
DocumentSource documentSource = name -> name.equals("name") ?
42+
Mono.just(DOCUMENT) : Mono.error(new IllegalArgumentException());
43+
44+
setMockResponse("{}");
45+
46+
// Original
47+
GraphQlTester.Builder<?> builder = graphQlTesterBuilder().documentSource(documentSource);
48+
GraphQlTester tester = builder.build();
49+
tester.documentName("name").execute();
50+
51+
assertThat(requestInput().getDocument()).isEqualTo(DOCUMENT);
52+
53+
// Mutate
54+
tester = tester.mutate().build();
55+
tester.documentName("name").execute();
56+
57+
assertThat(requestInput().getDocument()).isEqualTo(DOCUMENT);
58+
}
59+
60+
@Test
61+
void errorsFilteredGlobally() {
62+
63+
String document = "{me {name, friends}}";
64+
65+
setMockResponse(
66+
GraphqlErrorBuilder.newError().message("some error").build(),
67+
GraphqlErrorBuilder.newError().message("some other error").build());
68+
69+
graphQlTesterBuilder()
70+
.errorFilter((error) -> error.getMessage().startsWith("some "))
71+
.build()
72+
.document(document)
73+
.execute()
74+
.errors().verify()
75+
.path("me").pathDoesNotExist();
76+
77+
assertThat(requestInput().getDocument()).contains(document);
78+
}
79+
80+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.test.tester;
18+
19+
import java.util.Arrays;
20+
import java.util.Map;
21+
import java.util.function.Consumer;
22+
23+
import com.fasterxml.jackson.core.JsonProcessingException;
24+
import com.fasterxml.jackson.databind.ObjectMapper;
25+
import graphql.ExecutionInput;
26+
import graphql.ExecutionResult;
27+
import graphql.ExecutionResultImpl;
28+
import graphql.GraphQLError;
29+
import org.mockito.ArgumentCaptor;
30+
import reactor.core.publisher.Mono;
31+
32+
import org.springframework.graphql.GraphQlService;
33+
import org.springframework.graphql.RequestInput;
34+
import org.springframework.graphql.RequestOutput;
35+
36+
import static org.mockito.BDDMockito.given;
37+
import static org.mockito.Mockito.mock;
38+
39+
/**
40+
* Base class for {@link GraphQlTester} tests.
41+
*
42+
* @author Rossen Stoyanchev
43+
*/
44+
public class GraphQlTesterTestSupport {
45+
46+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
47+
48+
49+
private final ArgumentCaptor<RequestInput> inputCaptor = ArgumentCaptor.forClass(RequestInput.class);
50+
51+
private final GraphQlService graphQlService = mock(GraphQlService.class);
52+
53+
private final GraphQlTester.Builder<?> graphQlTesterBuilder = GraphQlServiceTester.builder(this.graphQlService);
54+
55+
private final GraphQlTester graphQlTester = this.graphQlTesterBuilder.build();
56+
57+
58+
protected GraphQlTester graphQlTester() {
59+
return this.graphQlTester;
60+
}
61+
62+
public GraphQlTester.Builder<?> graphQlTesterBuilder() {
63+
return this.graphQlTesterBuilder;
64+
}
65+
66+
protected RequestInput requestInput() {
67+
return this.inputCaptor.getValue();
68+
}
69+
70+
71+
protected void setMockResponse(String data) {
72+
setMockResponse(builder -> serialize(data, builder));
73+
}
74+
75+
protected void setMockResponse(GraphQLError... errors) {
76+
setMockResponse(builder -> builder.errors(Arrays.asList(errors)));
77+
}
78+
79+
private void setMockResponse(Consumer<ExecutionResultImpl.Builder> consumer) {
80+
81+
ExecutionResultImpl.Builder builder = new ExecutionResultImpl.Builder();
82+
consumer.accept(builder);
83+
ExecutionInput executionInput = ExecutionInput.newExecutionInput("{}").build();
84+
ExecutionResult result = builder.build();
85+
86+
given(this.graphQlService.execute(this.inputCaptor.capture()))
87+
.willReturn(Mono.just(new RequestOutput(executionInput, result)));
88+
}
89+
90+
private void serialize(String data, ExecutionResultImpl.Builder builder) {
91+
try {
92+
builder.data(OBJECT_MAPPER.readValue(data, Map.class));
93+
}
94+
catch (JsonProcessingException ex) {
95+
throw new IllegalStateException(ex);
96+
}
97+
}
98+
99+
}

0 commit comments

Comments
 (0)