1
1
package graphql .spring .web .servlet .components ;
2
2
3
+ import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import com .fasterxml .jackson .databind .ObjectMapper ;
3
5
import graphql .ExecutionInput ;
4
6
import graphql .ExecutionResultImpl ;
5
7
import graphql .GraphQL ;
26
28
import static org .junit .Assert .assertThat ;
27
29
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .asyncDispatch ;
28
30
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
31
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
29
32
import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
30
33
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
31
34
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
@@ -45,11 +48,62 @@ public class GraphQLControllerTest {
45
48
@ Autowired
46
49
GraphQL graphql ;
47
50
51
+ @ Autowired
52
+ ObjectMapper objectMapper ;
53
+
48
54
@ Before
49
55
public void setup () throws Exception {
50
56
this .mockMvc = MockMvcBuilders .webAppContextSetup (this .wac ).build ();
51
57
}
52
58
59
+ private String toJson (Map <String , Object > input ) {
60
+ try {
61
+ return objectMapper .writeValueAsString (input );
62
+ } catch (JsonProcessingException e ) {
63
+ throw new RuntimeException (e );
64
+ }
65
+ }
66
+
67
+ @ Test
68
+ public void testPostRequest () throws Exception {
69
+ Map <String , Object > request = new LinkedHashMap <>();
70
+ Map <String , Object > variables = new LinkedHashMap <>();
71
+ variables .put ("variable" , "variableValue" );
72
+ String query = "query myQuery {foo}" ;
73
+ request .put ("query" , query );
74
+ request .put ("variables" , variables );
75
+ String operationName = "myQuery" ;
76
+ request .put ("operationName" , operationName );
77
+
78
+ ExecutionResultImpl executionResult = ExecutionResultImpl .newExecutionResult ()
79
+ .data ("bar" )
80
+ .build ();
81
+ CompletableFuture cf = CompletableFuture .completedFuture (executionResult );
82
+ ArgumentCaptor <ExecutionInput > captor = ArgumentCaptor .forClass (ExecutionInput .class );
83
+ Mockito .when (graphql .executeAsync (captor .capture ())).thenReturn (cf );
84
+
85
+
86
+ MvcResult mvcResult = this .mockMvc .perform (post ("/graphql" )
87
+ .content (toJson (request ))
88
+ .contentType (MediaType .APPLICATION_JSON_UTF8 ))
89
+ .andExpect (status ().isOk ())
90
+ .andExpect (request ().asyncStarted ())
91
+ .andReturn ();
92
+
93
+ this .mockMvc .perform (asyncDispatch (mvcResult ))
94
+ .andDo (print ()).andExpect (status ().isOk ())
95
+ .andExpect (content ().contentType (MediaType .APPLICATION_JSON_UTF8 ))
96
+ .andExpect (jsonPath ("data" , is ("bar" )))
97
+ .andReturn ();
98
+
99
+ assertThat (captor .getAllValues ().size (), is (1 ));
100
+
101
+ assertThat (captor .getValue ().getQuery (), is (query ));
102
+ assertThat (captor .getValue ().getVariables (), is (variables ));
103
+ assertThat (captor .getValue ().getOperationName (), is (operationName ));
104
+
105
+ }
106
+
53
107
@ Test
54
108
public void testGetRequest () throws Exception {
55
109
String variablesJson = "{\" variable\" :\" variableValue\" }" ;
0 commit comments