11package graphql .spring .web .servlet .components ;
22
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
4+ import com .fasterxml .jackson .databind .ObjectMapper ;
35import graphql .ExecutionInput ;
46import graphql .ExecutionResultImpl ;
57import graphql .GraphQL ;
2628import static org .junit .Assert .assertThat ;
2729import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .asyncDispatch ;
2830import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
31+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
2932import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
3033import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
3134import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
@@ -45,11 +48,62 @@ public class GraphQLControllerTest {
4548 @ Autowired
4649 GraphQL graphql ;
4750
51+ @ Autowired
52+ ObjectMapper objectMapper ;
53+
4854 @ Before
4955 public void setup () throws Exception {
5056 this .mockMvc = MockMvcBuilders .webAppContextSetup (this .wac ).build ();
5157 }
5258
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+
53107 @ Test
54108 public void testGetRequest () throws Exception {
55109 String variablesJson = "{\" variable\" :\" variableValue\" }" ;
0 commit comments