Add MockExecutionGraphQlService in testFixtures

This commit is contained in:
rstoyanchev
2022-03-30 09:02:00 +01:00
parent 8bd39ba0f0
commit 9289f0a1b5
15 changed files with 288 additions and 315 deletions

View File

@@ -41,20 +41,20 @@ public class GraphQlTesterBuilderTests extends GraphQlTesterTestSupport {
DocumentSource documentSource = name -> name.equals("name") ?
Mono.just(DOCUMENT) : Mono.error(new IllegalArgumentException());
setMockResponse("{}");
getGraphQlService().setDataAsJson(DOCUMENT, "{}");
// Original
GraphQlTester.Builder<?> builder = graphQlTesterBuilder().documentSource(documentSource);
GraphQlTester tester = builder.build();
tester.documentName("name").execute();
assertThat(request().getDocument()).isEqualTo(DOCUMENT);
assertThat(getActualRequestDocument()).isEqualTo(DOCUMENT);
// Mutate
tester = tester.mutate().build();
tester.documentName("name").execute();
assertThat(request().getDocument()).isEqualTo(DOCUMENT);
assertThat(getActualRequestDocument()).isEqualTo(DOCUMENT);
}
@Test
@@ -62,7 +62,8 @@ public class GraphQlTesterBuilderTests extends GraphQlTesterTestSupport {
String document = "{me {name, friends}}";
setMockResponse(
getGraphQlService().setErrors(
document,
GraphqlErrorBuilder.newError().message("some error").build(),
GraphqlErrorBuilder.newError().message("some other error").build());
@@ -74,7 +75,7 @@ public class GraphQlTesterBuilderTests extends GraphQlTesterTestSupport {
.errors().verify()
.path("me").pathDoesNotExist();
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
}

View File

@@ -16,25 +16,8 @@
package org.springframework.graphql.test.tester;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Consumer;
import org.springframework.graphql.execution.MockExecutionGraphQlService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.GraphQLError;
import org.mockito.ArgumentCaptor;
import reactor.core.publisher.Mono;
import org.springframework.graphql.ExecutionGraphQlRequest;
import org.springframework.graphql.ExecutionGraphQlService;
import org.springframework.graphql.support.DefaultExecutionGraphQlResponse;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Base class for {@link GraphQlTester} tests.
@@ -43,18 +26,21 @@ import static org.mockito.Mockito.mock;
*/
public class GraphQlTesterTestSupport {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private final ArgumentCaptor<ExecutionGraphQlRequest> requestCaptor = ArgumentCaptor.forClass(ExecutionGraphQlRequest.class);
private final ExecutionGraphQlService graphQlService = mock(ExecutionGraphQlService.class);
private final MockExecutionGraphQlService graphQlService = new MockExecutionGraphQlService();
private final GraphQlTester.Builder<?> graphQlTesterBuilder = ExecutionGraphQlServiceTester.builder(this.graphQlService);
private final GraphQlTester graphQlTester = this.graphQlTesterBuilder.build();
public MockExecutionGraphQlService getGraphQlService() {
return this.graphQlService;
}
protected String getActualRequestDocument() {
return this.graphQlService.getGraphQlRequest().getDocument();
}
protected GraphQlTester graphQlTester() {
return this.graphQlTester;
}
@@ -63,37 +49,4 @@ public class GraphQlTesterTestSupport {
return this.graphQlTesterBuilder;
}
protected ExecutionGraphQlRequest request() {
return this.requestCaptor.getValue();
}
protected void setMockResponse(String data) {
setMockResponse(builder -> serialize(data, builder));
}
protected void setMockResponse(GraphQLError... errors) {
setMockResponse(builder -> builder.errors(Arrays.asList(errors)));
}
private void setMockResponse(Consumer<ExecutionResultImpl.Builder> consumer) {
ExecutionResultImpl.Builder builder = new ExecutionResultImpl.Builder();
consumer.accept(builder);
ExecutionInput executionInput = ExecutionInput.newExecutionInput("{}").build();
ExecutionResult result = builder.build();
given(this.graphQlService.execute(this.requestCaptor.capture()))
.willReturn(Mono.just(new DefaultExecutionGraphQlResponse(executionInput, result)));
}
private void serialize(String data, ExecutionResultImpl.Builder builder) {
try {
builder.data(OBJECT_MAPPER.readValue(data, Map.class));
}
catch (JsonProcessingException ex) {
throw new IllegalStateException(ex);
}
}
}

View File

@@ -43,7 +43,7 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
void hasValue() {
String document = "{me {name, friends}}";
setMockResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
getGraphQlService().setDataAsJson(document, "{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
GraphQlTester.Response response = graphQlTester().document(document).execute();
response.path("me.name").hasValue();
@@ -52,14 +52,14 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
assertThatThrownBy(() -> response.path("hero").hasValue())
.hasMessageContaining("No value at JSON path \"$['data']['hero']");
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
void valueIsNull() {
String document = "{me {name, friends}}";
setMockResponse("{\"me\": {\"name\":null, \"friends\":null}}");
getGraphQlService().setDataAsJson(document, "{\"me\": {\"name\":null, \"friends\":null}}");
GraphQlTester.Response response = graphQlTester().document(document).execute();
@@ -69,14 +69,14 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
assertThatThrownBy(() -> response.path("me").valueIsNull())
.hasMessageContaining("Expected null value at JSON path");
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
void valueIsEmptyList() {
String document = "{me {name, friends}}";
setMockResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
getGraphQlService().setDataAsJson(document, "{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
GraphQlTester.Response response = graphQlTester().document(document).execute();
response.path("me.friends").hasValue().entityList(MovieCharacter.class).hasSize(0);
@@ -85,7 +85,7 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
@Test
void pathDoesNotExist() {
String document = "{me {name, friends}}";
setMockResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
getGraphQlService().setDataAsJson(document, "{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
GraphQlTester.Response response = graphQlTester().document(document).execute();
@@ -99,7 +99,7 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
void matchesJson() {
String document = "{me {name}}";
setMockResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
getGraphQlService().setDataAsJson(document, "{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
GraphQlTester.Response response = graphQlTester().document(document).execute();
@@ -111,14 +111,14 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
.as("Extended fields should fail in strict mode")
.hasMessageContaining("Unexpected: name");
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
void entity() {
String document = "{me {name}}";
setMockResponse("{\"me\": {\"name\":\"Luke Skywalker\"}}");
getGraphQlService().setDataAsJson(document, "{\"me\": {\"name\":\"Luke Skywalker\"}}");
GraphQlTester.Response response = graphQlTester().document(document).execute();
@@ -141,14 +141,15 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
.entity(new ParameterizedTypeReference<Map<String, MovieCharacter>>() {})
.isEqualTo(Collections.singletonMap("me", luke));
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
void entityList() {
String document = "{me {name, friends}}";
setMockResponse("{" +
getGraphQlService().setDataAsJson(document,
"{" +
" \"me\":{" +
" \"name\":\"Luke Skywalker\","
+ " \"friends\":[{\"name\":\"Han Solo\"}, {\"name\":\"Leia Organa\"}]" +
@@ -183,7 +184,7 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
.entityList(new ParameterizedTypeReference<MovieCharacter>() {})
.containsExactly(han, leia);
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
@@ -195,7 +196,7 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
+ " }" +
"}";
setMockResponse("{\"hero\": {\"name\":\"R2-D2\"}}");
getGraphQlService().setDataAsJson(document, "{\"hero\": {\"name\":\"R2-D2\"}}");
GraphQlTester.Response response = graphQlTester().document(document)
.operationName("HeroNameAndFriends")
@@ -206,7 +207,7 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
response.path("hero").entity(MovieCharacter.class).isEqualTo(MovieCharacter.create("R2-D2"));
ExecutionGraphQlRequest request = request();
ExecutionGraphQlRequest request = getGraphQlService().getGraphQlRequest();
assertThat(request.getDocument()).contains(document);
assertThat(request.getOperationName()).isEqualTo("HeroNameAndFriends");
assertThat(request.getVariables()).hasSize(3);
@@ -219,42 +220,43 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
void errorsEmptyOnExecuteAndVerify() {
String document = "{me {name, friends}}";
setMockResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
getGraphQlService().setDataAsJson(document, "{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
graphQlTester().document(document).executeAndVerify();
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
void errorsCheckedOnExecuteAndVerify() {
String document = "{me {name, friends}}";
setMockResponse(GraphqlErrorBuilder.newError().message("Invalid query").build());
getGraphQlService().setError(document, builder -> builder.message("Invalid query"));
assertThatThrownBy(() -> graphQlTester().document(document).executeAndVerify())
.hasMessageContaining("Response has 1 unexpected error(s)");
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
void errorsCheckedOnTraverse() {
String document = "{me {name, friends}}";
setMockResponse(GraphqlErrorBuilder.newError().message("Invalid query").build());
getGraphQlService().setError(document, builder -> builder.message("Invalid query"));
assertThatThrownBy(() -> graphQlTester().document(document).execute().path("me"))
.hasMessageContaining("Response has 1 unexpected error(s)");
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
void errorsPartiallyFiltered() {
String document = "{me {name, friends}}";
setMockResponse(
getGraphQlService().setErrors(
document,
GraphqlErrorBuilder.newError().message("some error").build(),
GraphqlErrorBuilder.newError().message("some other error").build());
@@ -266,14 +268,15 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
.verify())
.hasMessageContaining("Response has 1 unexpected error(s) of 2 total.");
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
void errorsFiltered() {
String document = "{me {name, friends}}";
setMockResponse(
getGraphQlService().setErrors(
document,
GraphqlErrorBuilder.newError().message("some error").build(),
GraphqlErrorBuilder.newError().message("some other error").build());
@@ -285,14 +288,15 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
.path("me")
.pathDoesNotExist();
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
void errorsExpected() {
String document = "{me {name, friends}}";
setMockResponse(
getGraphQlService().setErrors(
document,
GraphqlErrorBuilder.newError().message("some error").build(),
GraphqlErrorBuilder.newError().message("some other error").build());
@@ -303,14 +307,15 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
.verify()
.path("me").pathDoesNotExist();
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
@Test
void errorsExpectedButNotFound() {
String document = "{me {name, friends}}";
setMockResponse(
getGraphQlService().setErrors(
document,
GraphqlErrorBuilder.newError().message("some error").build(),
GraphqlErrorBuilder.newError().message("some other error").build());
@@ -325,10 +330,8 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
void errorsConsumed() {
String document = "{me {name, friends}}";
setMockResponse(GraphqlErrorBuilder.newError()
.message("Invalid query")
.location(new SourceLocation(1, 2))
.build());
getGraphQlService().setError(document, builder ->
builder.message("Invalid query").location(new SourceLocation(1, 2)).build());
graphQlTester().document(document)
.execute()
@@ -342,7 +345,7 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
})
.path("me").pathDoesNotExist();
assertThat(request().getDocument()).contains(document);
assertThat(getActualRequestDocument()).contains(document);
}
}

View File

@@ -17,11 +17,8 @@
package org.springframework.graphql.test.tester;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import io.rsocket.Closeable;
import io.rsocket.SocketAcceptor;
@@ -36,11 +33,9 @@ import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.DecodingException;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.graphql.ExecutionGraphQlResponse;
import org.springframework.graphql.ExecutionGraphQlService;
import org.springframework.graphql.GraphQlRequest;
import org.springframework.graphql.execution.MockExecutionGraphQlService;
import org.springframework.graphql.server.GraphQlRSocketHandler;
import org.springframework.graphql.support.DefaultExecutionGraphQlResponse;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.lang.Nullable;
@@ -48,7 +43,6 @@ import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.rsocket.RSocketStrategies;
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import static org.assertj.core.api.Assertions.assertThat;
@@ -106,7 +100,7 @@ public class RSocketGraphQlTesterBuilderTests {
String document = "{me {name}}";
MovieCharacter character = MovieCharacter.create("Luke Skywalker");
this.builderSetup.setMockResponse(document,
this.builderSetup.getGraphQlService().setResponse(document,
ExecutionResultImpl.newExecutionResult()
.data(Collections.singletonMap("me", character))
.build());
@@ -126,34 +120,19 @@ public class RSocketGraphQlTesterBuilderTests {
private static class BuilderSetup {
private GraphQlRequest graphQlRequest;
private final Map<String, ExecutionGraphQlResponse> responses = new HashMap<>();
private final MockExecutionGraphQlService graphQlService = new MockExecutionGraphQlService();
@Nullable
private Closeable server;
public BuilderSetup() {
ExecutionGraphQlResponse defaultResponse = new DefaultExecutionGraphQlResponse(
ExecutionInput.newExecutionInput().query(DOCUMENT).build(),
ExecutionResultImpl.newExecutionResult().build());
this.responses.put(DOCUMENT, defaultResponse);
this.graphQlService.setDefaultDataAsJson("{}");
}
public RSocketGraphQlTester.Builder<?> initBuilder() {
ExecutionGraphQlService graphQlService = request -> {
this.graphQlRequest = request;
String document = request.getDocument();
ExecutionGraphQlResponse response = this.responses.get(document);
Assert.notNull(response, "Unexpected request: " + document);
return Mono.just(response);
};
GraphQlRSocketController controller = new GraphQlRSocketController(
new GraphQlRSocketHandler(graphQlService, Collections.emptyList(), new Jackson2JsonEncoder()));
new GraphQlRSocketHandler(this.graphQlService, Collections.emptyList(), new Jackson2JsonEncoder()));
this.server = RSocketServer.create()
.acceptor(createSocketAcceptor(controller))
@@ -178,14 +157,12 @@ public class RSocketGraphQlTesterBuilderTests {
return handler.responder();
}
@SuppressWarnings("unused")
public void setMockResponse(String document, ExecutionResult result) {
ExecutionInput executionInput = ExecutionInput.newExecutionInput().query(document).build();
this.responses.put(document, new DefaultExecutionGraphQlResponse(executionInput, result));
public MockExecutionGraphQlService getGraphQlService() {
return this.graphQlService;
}
public GraphQlRequest getGraphQlRequest() {
return this.graphQlRequest;
return this.graphQlService.getGraphQlRequest();
}
public void shutDown() {

View File

@@ -19,11 +19,9 @@ package org.springframework.graphql.test.tester;
import java.net.URI;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import org.junit.jupiter.api.Test;
@@ -34,21 +32,19 @@ import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.DecodingException;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.graphql.ExecutionGraphQlResponse;
import org.springframework.graphql.support.DefaultExecutionGraphQlResponse;
import org.springframework.graphql.support.DocumentSource;
import org.springframework.graphql.server.WebGraphQlRequest;
import org.springframework.graphql.server.TestWebSocketClient;
import org.springframework.graphql.server.TestWebSocketConnection;
import org.springframework.graphql.client.TestWebSocketClient;
import org.springframework.graphql.client.TestWebSocketConnection;
import org.springframework.graphql.execution.MockExecutionGraphQlService;
import org.springframework.graphql.server.WebGraphQlHandler;
import org.springframework.graphql.server.WebGraphQlInterceptor;
import org.springframework.graphql.server.WebGraphQlRequest;
import org.springframework.graphql.server.webflux.GraphQlHttpHandler;
import org.springframework.graphql.server.webflux.GraphQlWebSocketHandler;
import org.springframework.graphql.support.DocumentSource;
import org.springframework.http.codec.ClientCodecConfigurer;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.lang.Nullable;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
@@ -211,15 +207,10 @@ public class WebGraphQlTesterBuilderTests {
private WebGraphQlRequest request;
private final Map<String, ExecutionGraphQlResponse> responses = new HashMap<>();
private final MockExecutionGraphQlService graphQlService = new MockExecutionGraphQlService();
public WebBuilderSetup() {
ExecutionGraphQlResponse defaultResponse = new DefaultExecutionGraphQlResponse(
ExecutionInput.newExecutionInput().query(DOCUMENT).build(),
ExecutionResultImpl.newExecutionResult().build());
this.responses.put(DOCUMENT, defaultResponse);
this.graphQlService.setDefaultDataAsJson("{}");
}
@Override
@@ -228,12 +219,7 @@ public class WebGraphQlTesterBuilderTests {
}
protected WebGraphQlHandler webGraphQlHandler() {
return WebGraphQlHandler.builder(request -> {
String document = request.getDocument();
ExecutionGraphQlResponse response = this.responses.get(document);
Assert.notNull(response, "Unexpected request: " + document);
return Mono.just(response);
})
return WebGraphQlHandler.builder(this.graphQlService)
.interceptor((input, chain) -> {
this.request = input;
return chain.next(request);
@@ -243,8 +229,7 @@ public class WebGraphQlTesterBuilderTests {
@Override
public void setMockResponse(String document, ExecutionResult result) {
ExecutionInput executionInput = ExecutionInput.newExecutionInput().query(document).build();
this.responses.put(document, new DefaultExecutionGraphQlResponse(executionInput, result));
this.graphQlService.setResponse(document, result);
}
@Override