11package graphql .spring .web .reactive .components ;
22
3+ import graphql .ExecutionInput ;
4+ import graphql .ExecutionResultImpl ;
5+ import graphql .GraphQL ;
36import org .junit .Before ;
47import org .junit .Test ;
58import org .junit .runner .RunWith ;
9+ import org .mockito .ArgumentCaptor ;
10+ import org .mockito .Mockito ;
611import org .springframework .beans .factory .annotation .Autowired ;
712import org .springframework .context .ApplicationContext ;
813import org .springframework .http .MediaType ;
1520import java .net .URLEncoder ;
1621import java .util .LinkedHashMap ;
1722import java .util .Map ;
23+ import java .util .concurrent .CompletableFuture ;
1824
1925import static org .hamcrest .CoreMatchers .is ;
26+ import static org .junit .Assert .assertThat ;
2027
2128@ RunWith (SpringRunner .class )
2229@ ContextConfiguration (classes = {TestAppConfig .class })
@@ -26,6 +33,9 @@ public class GraphQLControllerTest {
2633 @ Autowired
2734 private ApplicationContext applicationContext ;
2835
36+ @ Autowired
37+ GraphQL graphql ;
38+
2939 private WebTestClient client ;
3040
3141 @ Before
@@ -37,37 +47,68 @@ public void setup() {
3747 @ Test
3848 public void testPostRequest () throws Exception {
3949 Map <String , Object > request = new LinkedHashMap <>();
50+ Map <String , Object > variables = new LinkedHashMap <>();
51+ variables .put ("variable" , "variableValue" );
52+ String query = "query myQuery {foo}" ;
53+ request .put ("query" , query );
54+ request .put ("variables" , variables );
55+ String operationName = "myQuery" ;
56+ request .put ("operationName" , operationName );
57+
58+ ExecutionResultImpl executionResult = ExecutionResultImpl .newExecutionResult ()
59+ .data ("bar" )
60+ .build ();
61+ CompletableFuture cf = CompletableFuture .completedFuture (executionResult );
62+ ArgumentCaptor <ExecutionInput > captor = ArgumentCaptor .forClass (ExecutionInput .class );
63+ Mockito .when (graphql .executeAsync (captor .capture ())).thenReturn (cf );
64+
4065 client .post ().uri ("/graphql" )
4166 .body (Mono .just (request ), Map .class )
4267 .accept (MediaType .APPLICATION_JSON_UTF8 )
4368 .exchange ()
4469 .expectStatus ().isOk ()
4570 .expectBody ()
46- .jsonPath ("data" , is ("foo" ));
47- // String query = "{foo}";
48- // String variablesJson = "{\"key\":\"value\"}";
49- // MvcResult mvcResult = this.mockMvc.perform(get("/graphql")
50- // .param("query", query)
51- // .param("variables", variablesJson))
52- // .andDo(print()).andExpect(status().isOk())
53- // .andReturn();
71+ .jsonPath ("data" , is ("bar" ));
5472
73+ assertThat (captor .getAllValues ().size (), is (1 ));
74+
75+ assertThat (captor .getValue ().getQuery (), is (query ));
76+ assertThat (captor .getValue ().getVariables (), is (variables ));
77+ assertThat (captor .getValue ().getOperationName (), is (operationName ));
5578 }
5679
5780 @ Test
58- public void testGetRequestWithVariables () throws Exception {
59- String variablesJson = "{\" key \" :\" value \" }" ;
81+ public void testGetRequest () throws Exception {
82+ String variablesJson = "{\" variable \" :\" variableValue \" }" ;
6083 String variablesValue = URLEncoder .encode (variablesJson , "UTF-8" );
61- String queryString = URLEncoder .encode ("{foo}" , "UTF-8" );
84+ String query = "query myQuery {foo}" ;
85+ String queryString = URLEncoder .encode (query , "UTF-8" );
86+ String operationName = "myQuery" ;
87+
88+ ExecutionResultImpl executionResult = ExecutionResultImpl .newExecutionResult ()
89+ .data ("bar" )
90+ .build ();
91+ CompletableFuture cf = CompletableFuture .completedFuture (executionResult );
92+ ArgumentCaptor <ExecutionInput > captor = ArgumentCaptor .forClass (ExecutionInput .class );
93+ Mockito .when (graphql .executeAsync (captor .capture ())).thenReturn (cf );
94+
6295 client .get ().uri (uriBuilder -> uriBuilder .path ("/graphql" )
6396 .queryParam ("variables" , variablesValue )
6497 .queryParam ("query" , queryString )
98+ .queryParam ("operationName" , operationName )
6599 .build (variablesJson , queryString ))
66100 .accept (MediaType .APPLICATION_JSON_UTF8 )
67101 .exchange ()
68102 .expectStatus ().isOk ()
69103 .expectBody ()
70104 .jsonPath ("data" , is ("foo" ));
71105
106+ assertThat (captor .getAllValues ().size (), is (1 ));
107+
108+ Map <String , Object > variables = new LinkedHashMap <>();
109+ variables .put ("variable" , "variableValue" );
110+ assertThat (captor .getValue ().getQuery (), is (query ));
111+ assertThat (captor .getValue ().getVariables (), is (variables ));
112+ assertThat (captor .getValue ().getOperationName (), is (operationName ));
72113 }
73114}
0 commit comments