1
1
package graphql .spring .web .servlet .components ;
2
2
3
+ import graphql .ExecutionInput ;
4
+ import graphql .ExecutionResultImpl ;
5
+ import graphql .GraphQL ;
3
6
import org .junit .Before ;
4
7
import org .junit .Test ;
5
8
import org .junit .runner .RunWith ;
9
+ import org .mockito .ArgumentCaptor ;
10
+ import org .mockito .Mockito ;
6
11
import org .springframework .beans .factory .annotation .Autowired ;
7
12
import org .springframework .http .MediaType ;
8
13
import org .springframework .test .context .ContextConfiguration ;
13
18
import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
14
19
import org .springframework .web .context .WebApplicationContext ;
15
20
21
+ import java .util .LinkedHashMap ;
22
+ import java .util .Map ;
23
+ import java .util .concurrent .CompletableFuture ;
24
+
16
25
import static org .hamcrest .CoreMatchers .is ;
26
+ import static org .junit .Assert .assertThat ;
17
27
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .asyncDispatch ;
18
28
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
19
29
import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
@@ -32,29 +42,50 @@ public class GraphQLControllerTest {
32
42
@ Autowired
33
43
private WebApplicationContext wac ;
34
44
45
+ @ Autowired
46
+ GraphQL graphql ;
47
+
35
48
@ Before
36
49
public void setup () throws Exception {
37
50
this .mockMvc = MockMvcBuilders .webAppContextSetup (this .wac ).build ();
38
51
}
39
52
40
53
@ Test
41
54
public void testGetRequest () throws Exception {
42
- String query = "{foo}" ;
43
- String variablesJson = "{\" key\" :\" value\" }" ;
55
+ String variablesJson = "{\" variable\" :\" variableValue\" }" ;
56
+ String query = "query myQuery {foo}" ;
57
+ String operationName = "myQuery" ;
58
+
59
+ ExecutionResultImpl executionResult = ExecutionResultImpl .newExecutionResult ()
60
+ .data ("bar" )
61
+ .build ();
62
+ CompletableFuture cf = CompletableFuture .completedFuture (executionResult );
63
+ ArgumentCaptor <ExecutionInput > captor = ArgumentCaptor .forClass (ExecutionInput .class );
64
+ Mockito .when (graphql .executeAsync (captor .capture ())).thenReturn (cf );
65
+
44
66
45
67
MvcResult mvcResult = this .mockMvc .perform (get ("/graphql" )
46
68
.param ("query" , query )
47
- .param ("variables" , variablesJson ))
69
+ .param ("variables" , variablesJson )
70
+ .param ("operationName" , operationName ))
48
71
.andExpect (status ().isOk ())
49
72
.andExpect (request ().asyncStarted ())
50
73
.andReturn ();
51
74
52
75
this .mockMvc .perform (asyncDispatch (mvcResult ))
53
76
.andDo (print ()).andExpect (status ().isOk ())
54
77
.andExpect (content ().contentType (MediaType .APPLICATION_JSON_UTF8 ))
55
- .andExpect (jsonPath ("data" , is ("foo " )))
78
+ .andExpect (jsonPath ("data" , is ("bar " )))
56
79
.andReturn ();
57
80
81
+ assertThat (captor .getAllValues ().size (), is (1 ));
82
+
83
+ Map <String , Object > variables = new LinkedHashMap <>();
84
+ variables .put ("variable" , "variableValue" );
85
+ assertThat (captor .getValue ().getQuery (), is (query ));
86
+ assertThat (captor .getValue ().getVariables (), is (variables ));
87
+ assertThat (captor .getValue ().getOperationName (), is (operationName ));
88
+
58
89
}
59
90
60
91
}
0 commit comments