68
68
* @since 6.0
69
69
*/
70
70
@ SpringJUnitConfig
71
- @ DirtiesContext
71
+ @ DirtiesContext ( classMode = DirtiesContext . ClassMode . AFTER_EACH_TEST_METHOD )
72
72
public class GraphQlMessageHandlerTests {
73
73
74
74
@ Autowired
@@ -80,6 +80,9 @@ public class GraphQlMessageHandlerTests {
80
80
@ Autowired
81
81
private PollableChannel errorChannel ;
82
82
83
+ @ Autowired
84
+ private GraphQlMessageHandler graphQlMessageHandler ;
85
+
83
86
@ Autowired
84
87
private UpdateRepository updateRepository ;
85
88
@@ -97,8 +100,7 @@ void testHandleMessageForQuery() {
97
100
Map <String , Object > data = result .getData ();
98
101
Map <String , Object > testQuery = (Map <String , Object >) data .get ("testQuery" );
99
102
assertThat (testQuery .get ("id" )).isEqualTo ("test-data" );
100
- }
101
- )
103
+ })
102
104
.thenCancel ()
103
105
.verifyLater ();
104
106
@@ -111,6 +113,69 @@ void testHandleMessageForQuery() {
111
113
verifier .verify (Duration .ofSeconds (10 ));
112
114
}
113
115
116
+ @ Test
117
+ @ SuppressWarnings ("unchecked" )
118
+ void testHandleMessageForQueryWithQueryProvided () {
119
+
120
+ String fakeQuery = "{ testQuery { id } }" ;
121
+ this .graphQlMessageHandler .setQuery (fakeQuery );
122
+
123
+ StepVerifier .create (
124
+ Mono .from ((Mono <ExecutionResult >)this .graphQlMessageHandler .handleRequestMessage (MessageBuilder .withPayload (fakeQuery ).build ()))
125
+ )
126
+ .consumeNextWith (result -> {
127
+ assertThat (result ).isInstanceOf (ExecutionResultImpl .class );
128
+ Map <String , Object > data = result .getData ();
129
+ Map <String , Object > testQuery = (Map <String , Object >) data .get ("testQuery" );
130
+ assertThat (testQuery .get ("id" )).isEqualTo ("test-data" );
131
+ })
132
+ .expectComplete ()
133
+ .verify ();
134
+ }
135
+
136
+ @ Test
137
+ @ SuppressWarnings ("unchecked" )
138
+ void testHandleMessageForQueryWithQueryAndOperationProvided () {
139
+
140
+ String fakeQuery = "query FriendlyName { testQuery { id } }" ;
141
+ this .graphQlMessageHandler .setQuery (fakeQuery );
142
+ this .graphQlMessageHandler .setOperationName ("FriendlyName" );
143
+
144
+ StepVerifier .create (
145
+ Mono .from ((Mono <ExecutionResult >)this .graphQlMessageHandler .handleRequestMessage (MessageBuilder .withPayload (fakeQuery ).build ()))
146
+ )
147
+ .consumeNextWith (result -> {
148
+ assertThat (result ).isInstanceOf (ExecutionResultImpl .class );
149
+ Map <String , Object > data = result .getData ();
150
+ Map <String , Object > testQuery = (Map <String , Object >) data .get ("testQuery" );
151
+ assertThat (testQuery .get ("id" )).isEqualTo ("test-data" );
152
+ })
153
+ .expectComplete ()
154
+ .verify ();
155
+ }
156
+
157
+ @ Test
158
+ @ SuppressWarnings ("unchecked" )
159
+ void testHandleMessageForQueryWithQueryAndOperationNameAndVariablesProvided () {
160
+
161
+ String fakeQuery = "query FriendlyName($id: String) { testQueryById(id: $id) { id } }" ;
162
+ this .graphQlMessageHandler .setQuery (fakeQuery );
163
+ this .graphQlMessageHandler .setOperationName ("FriendlyName" );
164
+ this .graphQlMessageHandler .setVariables (Map .of ("$id" , "test-data" ));
165
+
166
+ StepVerifier .create (
167
+ Mono .from ((Mono <ExecutionResult >)this .graphQlMessageHandler .handleRequestMessage (MessageBuilder .withPayload (fakeQuery ).build ()))
168
+ )
169
+ .consumeNextWith (result -> {
170
+ assertThat (result ).isInstanceOf (ExecutionResultImpl .class );
171
+ Map <String , Object > data = result .getData ();
172
+ Map <String , Object > testQuery = (Map <String , Object >) data .get ("testQueryById" );
173
+ assertThat (testQuery .get ("id" )).isEqualTo ("test-data" );
174
+ })
175
+ .expectComplete ()
176
+ .verify ();
177
+ }
178
+
114
179
@ Test
115
180
@ SuppressWarnings ("unchecked" )
116
181
void testHandleMessageForMutation () {
@@ -191,7 +256,7 @@ void testHandleMessageForQueryWithInvalidPayload() {
191
256
192
257
this .inputChannel .send (
193
258
MessageBuilder
194
- .withPayload ("{ testQuery { id } }" )
259
+ .withPayload (new Object () )
195
260
.build ()
196
261
);
197
262
@@ -202,7 +267,7 @@ void testHandleMessageForQueryWithInvalidPayload() {
202
267
.isInstanceOf (MessageHandlingException .class )
203
268
.satisfies ((ex ) -> assertThat ((Exception ) ex )
204
269
.hasMessageContaining (
205
- "Message payload needs to be 'org.springframework.graphql.RequestInput'" ));
270
+ "Message payload does not meet criteria to construct a 'org.springframework.graphql.RequestInput'" ));
206
271
207
272
}
208
273
@@ -213,7 +278,7 @@ void testHandleMessageForMutationWithInvalidPayload() {
213
278
214
279
this .inputChannel .send (
215
280
MessageBuilder
216
- .withPayload ("mutation { update(id: \" " + fakeId + " \" ) { id } }" )
281
+ .withPayload (new Object () )
217
282
.build ()
218
283
);
219
284
@@ -224,7 +289,7 @@ void testHandleMessageForMutationWithInvalidPayload() {
224
289
.isInstanceOf (MessageHandlingException .class )
225
290
.satisfies ((ex ) -> assertThat ((Exception ) ex )
226
291
.hasMessageContaining (
227
- "Message payload needs to be 'org.springframework.graphql.RequestInput'" ));
292
+ "Message payload does not meet criteria to construct a 'org.springframework.graphql.RequestInput'" ));
228
293
229
294
}
230
295
@@ -233,7 +298,7 @@ void testHandleMessageForSubscriptionWithInvalidPayload() {
233
298
234
299
this .inputChannel .send (
235
300
MessageBuilder
236
- .withPayload ("subscription { results { id } }" )
301
+ .withPayload (new Object () )
237
302
.build ()
238
303
);
239
304
@@ -244,10 +309,14 @@ void testHandleMessageForSubscriptionWithInvalidPayload() {
244
309
.isInstanceOf (MessageHandlingException .class )
245
310
.satisfies ((ex ) -> assertThat ((Exception ) ex )
246
311
.hasMessageContaining (
247
- "Message payload needs to be 'org.springframework.graphql.RequestInput'" ));
312
+ "Message payload does not meet criteria to construct a 'org.springframework.graphql.RequestInput'" ));
248
313
249
314
}
250
315
316
+ private static <T > T waitFor (Mono <T > mono ) {
317
+ return mono .block (Duration .ofSeconds (10 ));
318
+ }
319
+
251
320
@ Controller
252
321
static class GraphQlController {
253
322
@@ -262,6 +331,11 @@ public Mono<QueryResult> testQuery() {
262
331
return Mono .just (new QueryResult ("test-data" ));
263
332
}
264
333
334
+ @ QueryMapping
335
+ public Mono <QueryResult > testQueryById (@ Argument String id ) {
336
+ return Mono .just (new QueryResult ("test-data" ));
337
+ }
338
+
265
339
@ MutationMapping
266
340
public Mono <Update > update (@ Argument String id ) {
267
341
return this .updateRepository .save (new Update (id ));
0 commit comments