Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.

Commit df3ba09

Browse files
committed
webmvc testing
1 parent ad77a01 commit df3ba09

File tree

9 files changed

+36
-24
lines changed

9 files changed

+36
-24
lines changed

graphql-java-spring-boot-starter-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLEndpointConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package graphql.spring.web.servlet;
22

3-
import graphql.spring.web.servlet.controller.GraphQLController;
3+
import graphql.spring.web.servlet.components.GraphQLController;
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
66
import org.springframework.context.ApplicationContext;

graphql-java-spring-webmvc/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ dependencies {
1111
testCompile group: 'junit', name: 'junit', version: '4.12'
1212
testCompile "org.springframework:spring-test:$springVersion"
1313
testCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
14+
testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0'
1415
testCompile "org.mockito:mockito-core:2.+"
1516
}

graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/ExecutionResultHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
import graphql.ExecutionResult;
44
import graphql.PublicSpi;
5-
import org.springframework.http.server.ServerHttpResponse;
65

76
import java.util.concurrent.CompletableFuture;
87

98
@PublicSpi
109
public interface ExecutionResultHandler {
1110

12-
Object handleExecutionResult(CompletableFuture<ExecutionResult> executionResultCF, ServerHttpResponse serverHttpResponse);
11+
Object handleExecutionResult(CompletableFuture<ExecutionResult> executionResultCF);
1312
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package graphql.spring.web.servlet;
1+
package graphql.spring.web.servlet.components;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import graphql.ExecutionResult;
5+
import graphql.spring.web.servlet.ExecutionResultHandler;
56
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.http.server.ServerHttpResponse;
77
import org.springframework.stereotype.Component;
88

99
import java.util.concurrent.CompletableFuture;
@@ -15,11 +15,11 @@ public class DefaultExecutionResultHandler implements ExecutionResultHandler {
1515
ObjectMapper objectMapper;
1616

1717
@Override
18-
public Object handleExecutionResult(CompletableFuture<ExecutionResult> executionResultMono, ServerHttpResponse serverHttpResponse) {
19-
return executionResultMono.thenApply(executionResult -> handleImpl(executionResult, serverHttpResponse));
18+
public Object handleExecutionResult(CompletableFuture<ExecutionResult> executionResultMono) {
19+
return executionResultMono.thenApply(executionResult -> handleImpl(executionResult));
2020
}
2121

22-
private Object handleImpl(ExecutionResult executionResult, ServerHttpResponse serverHttpResponse) {
22+
private Object handleImpl(ExecutionResult executionResult) {
2323
return executionResult.toSpecification();
2424
}
2525
}

graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/DefaultGraphQLInvocation.java renamed to graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/DefaultGraphQLInvocation.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package graphql.spring.web.servlet;
1+
package graphql.spring.web.servlet.components;
22

33
import graphql.ExecutionInput;
44
import graphql.ExecutionResult;
55
import graphql.GraphQL;
66
import graphql.Internal;
7+
import graphql.spring.web.servlet.GraphQLInvocation;
8+
import graphql.spring.web.servlet.GraphQLInvocationData;
79
import org.springframework.beans.factory.annotation.Autowired;
810
import org.springframework.stereotype.Component;
911
import org.springframework.web.context.request.WebRequest;
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package graphql.spring.web.servlet.controller;
1+
package graphql.spring.web.servlet.components;
22

33

44
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -9,7 +9,6 @@
99
import graphql.spring.web.servlet.GraphQLInvocationData;
1010
import org.springframework.beans.factory.annotation.Autowired;
1111
import org.springframework.http.MediaType;
12-
import org.springframework.http.server.ServerHttpResponse;
1312
import org.springframework.web.bind.annotation.RequestBody;
1413
import org.springframework.web.bind.annotation.RequestMapping;
1514
import org.springframework.web.bind.annotation.RequestMethod;
@@ -37,29 +36,27 @@ public class GraphQLController {
3736
@RequestMapping(value = "${graphql.url:graphql}",
3837
method = RequestMethod.POST,
3938
consumes = MediaType.APPLICATION_JSON_VALUE,
40-
produces = MediaType.APPLICATION_JSON_VALUE)
39+
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
4140
public Object graphqlPOST(@RequestBody GraphQLRequestBody body,
42-
WebRequest webRequest,
43-
ServerHttpResponse serverHttpResponse) {
41+
WebRequest webRequest) {
4442
String query = body.getQuery();
4543
if (query == null) {
4644
query = "";
4745
}
4846
CompletableFuture<ExecutionResult> executionResult = graphQLInvocation.invoke(new GraphQLInvocationData(query, body.getOperationName(), body.getVariables()), webRequest);
49-
return executionResultHandler.handleExecutionResult(executionResult, serverHttpResponse);
47+
return executionResultHandler.handleExecutionResult(executionResult);
5048
}
5149

5250
@RequestMapping(value = "${graphql.url:graphql}",
5351
method = RequestMethod.GET,
54-
produces = MediaType.APPLICATION_JSON_VALUE)
52+
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
5553
public Object graphqlGET(
5654
@RequestParam("query") String query,
5755
@RequestParam(value = "operationName", required = false) String operationName,
5856
@RequestParam(value = "variables", required = false) String variablesJson,
59-
WebRequest webRequest,
60-
ServerHttpResponse serverHttpResponse) {
57+
WebRequest webRequest) {
6158
CompletableFuture<ExecutionResult> executionResult = graphQLInvocation.invoke(new GraphQLInvocationData(query, operationName, convertVariablesJson(variablesJson)), webRequest);
62-
return executionResultHandler.handleExecutionResult(executionResult, serverHttpResponse);
59+
return executionResultHandler.handleExecutionResult(executionResult);
6360
}
6461

6562
private Map<String, Object> convertVariablesJson(String jsonMap) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package graphql.spring.web.servlet.controller;
1+
package graphql.spring.web.servlet.components;
22

33
import graphql.Internal;
44

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package graphql.spring.web.servlet.controller;
1+
package graphql.spring.web.servlet.components;
22

33
import org.junit.Before;
44
import org.junit.Test;
55
import org.junit.runner.RunWith;
66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.MediaType;
78
import org.springframework.test.context.ContextConfiguration;
89
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
910
import org.springframework.test.context.web.WebAppConfiguration;
@@ -12,8 +13,13 @@
1213
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1314
import org.springframework.web.context.WebApplicationContext;
1415

16+
import static org.hamcrest.CoreMatchers.is;
17+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
1518
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1619
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
20+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
21+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
1723
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1824

1925
@RunWith(SpringJUnit4ClassRunner.class)
@@ -31,15 +37,22 @@ public void setup() throws Exception {
3137
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
3238
}
3339

34-
3540
@Test
36-
public void testVariableJson() throws Exception {
41+
public void testGetRequest() throws Exception {
3742
String query = "{foo}";
3843
String variablesJson = "{\"key\":\"value\"}";
44+
3945
MvcResult mvcResult = this.mockMvc.perform(get("/graphql")
4046
.param("query", query)
4147
.param("variables", variablesJson))
48+
.andExpect(status().isOk())
49+
.andExpect(request().asyncStarted())
50+
.andReturn();
51+
52+
this.mockMvc.perform(asyncDispatch(mvcResult))
4253
.andDo(print()).andExpect(status().isOk())
54+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
55+
.andExpect(jsonPath("data", is("foo")))
4356
.andReturn();
4457

4558
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package graphql.spring.web.servlet.controller;
1+
package graphql.spring.web.servlet.components;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import graphql.ExecutionInput;

0 commit comments

Comments
 (0)