Skip to content

Commit 218b16e

Browse files
committed
JSON config in ExecutionGraphQlServiceTester builder
See gh-345
1 parent 85dc2e5 commit 218b16e

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultExecutionGraphQlServiceTesterBuilder.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
package org.springframework.graphql.test.tester;
1818

1919

20+
import java.util.Collections;
2021
import java.util.function.Consumer;
2122

23+
import org.springframework.core.codec.Decoder;
24+
import org.springframework.core.codec.Encoder;
2225
import org.springframework.graphql.ExecutionGraphQlService;
26+
import org.springframework.lang.Nullable;
2327
import org.springframework.util.Assert;
2428

2529

@@ -36,6 +40,12 @@ final class DefaultExecutionGraphQlServiceTesterBuilder
3640

3741
private final ExecutionGraphQlService service;
3842

43+
@Nullable
44+
private Encoder<?> encoder;
45+
46+
@Nullable
47+
private Decoder<?> decoder;
48+
3949

4050
DefaultExecutionGraphQlServiceTesterBuilder(ExecutionGraphQlService service) {
4151
Assert.notNull(service, "GraphQlService is required");
@@ -46,14 +56,36 @@ final class DefaultExecutionGraphQlServiceTesterBuilder
4656
this.service = transport.getGraphQlService();
4757
}
4858

59+
@Override
60+
public DefaultExecutionGraphQlServiceTesterBuilder encoder(Encoder<?> encoder) {
61+
this.encoder = encoder;
62+
return this;
63+
}
64+
65+
@Override
66+
public DefaultExecutionGraphQlServiceTesterBuilder decoder(Decoder<?> decoder) {
67+
this.decoder = decoder;
68+
return this;
69+
}
4970

5071
@Override
5172
public ExecutionGraphQlServiceTester build() {
73+
registerJsonPathMappingProvider();
5274
GraphQlServiceGraphQlTransport transport = new GraphQlServiceGraphQlTransport(this.service);
5375
GraphQlTester tester = super.buildGraphQlTester(transport);
5476
return new DefaultExecutionGraphQlServiceTester(tester, transport, getBuilderInitializer());
5577
}
5678

79+
private void registerJsonPathMappingProvider() {
80+
if (this.encoder != null && this.decoder != null) {
81+
configureJsonPathConfig(config -> {
82+
EncoderDecoderMappingProvider provider = new EncoderDecoderMappingProvider(
83+
Collections.singletonList(this.encoder), Collections.singletonList(this.decoder));
84+
return config.mappingProvider(provider);
85+
});
86+
}
87+
}
88+
5789

5890
/**
5991
* Default {@link ExecutionGraphQlServiceTester} implementation.

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/ExecutionGraphQlServiceTester.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.graphql.test.tester;
1818

19+
import org.springframework.core.codec.Decoder;
20+
import org.springframework.core.codec.Encoder;
1921
import org.springframework.graphql.ExecutionGraphQlService;
2022

2123
/**
@@ -52,6 +54,18 @@ static ExecutionGraphQlServiceTester.Builder<?> builder(ExecutionGraphQlService
5254
*/
5355
interface Builder<B extends Builder<B>> extends GraphQlTester.Builder<B> {
5456

57+
/**
58+
* Configure the JSON encoder to use for mapping response data to
59+
* higher level objects.
60+
*/
61+
B encoder(Encoder<?> encoder);
62+
63+
/**
64+
* Configure the JSON decoder to use for mapping response data to
65+
* higher level objects.
66+
*/
67+
B decoder(Decoder<?> decoder);
68+
5569
/**
5670
* Build a {@link ExecutionGraphQlServiceTester} instance.
5771
*/

spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterBuilderTests.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,23 @@
1616

1717
package org.springframework.graphql.test.tester;
1818

19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
import graphql.ExecutionResultImpl;
1923
import graphql.GraphqlErrorBuilder;
2024
import org.junit.jupiter.api.Test;
2125
import reactor.core.publisher.Mono;
2226

27+
import org.springframework.core.ResolvableType;
28+
import org.springframework.core.codec.DecodingException;
29+
import org.springframework.core.io.buffer.DataBuffer;
2330
import org.springframework.graphql.ExecutionGraphQlService;
2431
import org.springframework.graphql.support.DocumentSource;
32+
import org.springframework.http.codec.json.Jackson2JsonDecoder;
33+
import org.springframework.http.codec.json.Jackson2JsonEncoder;
34+
import org.springframework.lang.Nullable;
35+
import org.springframework.util.MimeType;
2536

2637
import static org.assertj.core.api.Assertions.assertThat;
2738

@@ -65,6 +76,34 @@ void mutateDocumentSource() {
6576
assertThat(getActualRequestDocument()).isEqualTo(DOCUMENT);
6677
}
6778

79+
@Test
80+
void codecConfigurerRegistersJsonPathMappingProvider() {
81+
82+
TestJackson2JsonDecoder testDecoder = new TestJackson2JsonDecoder();
83+
84+
ExecutionGraphQlServiceTester.Builder<?> builder = graphQlTesterBuilder()
85+
.encoder(new Jackson2JsonEncoder())
86+
.decoder(testDecoder);
87+
88+
String document = "{me {name}}";
89+
MovieCharacter character = MovieCharacter.create("Luke Skywalker");
90+
getGraphQlService().setResponse(document,
91+
ExecutionResultImpl.newExecutionResult()
92+
.data(Collections.singletonMap("me", character))
93+
.build());
94+
95+
GraphQlTester client = builder.build();
96+
GraphQlTester.Response response = client.document(document).execute();
97+
98+
testDecoder.resetLastValue();
99+
assertThat(testDecoder.getLastValue()).isNull();
100+
101+
assertThat(response).isNotNull();
102+
response.path("me").entity(MovieCharacter.class).isEqualTo(character);
103+
response.path("me").matchesJson("{name:\"Luke Skywalker\"}");
104+
assertThat(testDecoder.getLastValue()).isEqualTo(character);
105+
}
106+
68107
@Test
69108
void errorsFilteredGlobally() {
70109

@@ -86,4 +125,29 @@ void errorsFilteredGlobally() {
86125
assertThat(getActualRequestDocument()).contains(document);
87126
}
88127

128+
129+
private static class TestJackson2JsonDecoder extends Jackson2JsonDecoder {
130+
131+
@Nullable
132+
private Object lastValue;
133+
134+
@Nullable
135+
Object getLastValue() {
136+
return this.lastValue;
137+
}
138+
139+
@Override
140+
public Object decode(DataBuffer dataBuffer, ResolvableType targetType,
141+
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) throws DecodingException {
142+
143+
this.lastValue = super.decode(dataBuffer, targetType, mimeType, hints);
144+
return this.lastValue;
145+
}
146+
147+
void resetLastValue() {
148+
this.lastValue = null;
149+
}
150+
151+
}
152+
89153
}

spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTestSupport.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public class GraphQlTesterTestSupport {
2828

2929
private final MockExecutionGraphQlService graphQlService = new MockExecutionGraphQlService();
3030

31-
private final GraphQlTester.Builder<?> graphQlTesterBuilder = ExecutionGraphQlServiceTester.builder(this.graphQlService);
31+
private final ExecutionGraphQlServiceTester.Builder<?> graphQlTesterBuilder =
32+
ExecutionGraphQlServiceTester.builder(this.graphQlService);
3233

3334
private final GraphQlTester graphQlTester = this.graphQlTesterBuilder.build();
3435

@@ -45,7 +46,7 @@ protected GraphQlTester graphQlTester() {
4546
return this.graphQlTester;
4647
}
4748

48-
public GraphQlTester.Builder<?> graphQlTesterBuilder() {
49+
public ExecutionGraphQlServiceTester.Builder<?> graphQlTesterBuilder() {
4950
return this.graphQlTesterBuilder;
5051
}
5152

0 commit comments

Comments
 (0)