1
1
/*
2
- * Copyright 2021-2022 the original author or authors.
2
+ * Copyright 2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
21
21
import java .time .Duration ;
22
22
import java .util .Collections ;
23
- import java .util .Locale ;
24
23
import java .util .Map ;
25
24
import java .util .Objects ;
26
25
import java .util .UUID ;
33
32
import org .springframework .core .io .ClassPathResource ;
34
33
import org .springframework .graphql .GraphQlService ;
35
34
import org .springframework .graphql .RequestInput ;
36
- import org .springframework .graphql .RequestOutput ;
37
35
import org .springframework .graphql .data .method .annotation .Argument ;
38
36
import org .springframework .graphql .data .method .annotation .MutationMapping ;
39
37
import org .springframework .graphql .data .method .annotation .QueryMapping ;
56
54
import org .springframework .stereotype .Repository ;
57
55
import org .springframework .test .annotation .DirtiesContext ;
58
56
import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
59
- import org .springframework .util .AlternativeJdkIdGenerator ;
60
- import org .springframework .util .IdGenerator ;
61
57
58
+ import graphql .ExecutionResult ;
59
+ import graphql .ExecutionResultImpl ;
62
60
import graphql .execution .reactive .SubscriptionPublisher ;
63
61
import reactor .core .publisher .Flux ;
64
62
import reactor .core .publisher .Mono ;
67
65
/**
68
66
*
69
67
* @author Daniel Frey
70
- * @since 6.0
68
+ *
71
69
*/
72
- @ SpringJUnitConfig
73
- @ DirtiesContext ( classMode = DirtiesContext . ClassMode . AFTER_EACH_TEST_METHOD )
70
+ @ SpringJUnitConfig ( GraphQlMessageHandlerTests . TestConfig . class )
71
+ @ DirtiesContext
74
72
public class GraphQlMessageHandlerTests {
75
73
76
74
@ Autowired
@@ -82,42 +80,31 @@ public class GraphQlMessageHandlerTests {
82
80
@ Autowired
83
81
private PollableChannel errorChannel ;
84
82
85
- @ Autowired
86
- private GraphQlMessageHandler graphQlMessageHandler ;
87
-
88
83
@ Autowired
89
84
private UpdateRepository updateRepository ;
90
85
91
- @ Autowired
92
- private IdGenerator idGenerator ;
93
-
94
-
95
86
@ Test
96
87
@ SuppressWarnings ("unchecked" )
97
- void testHandleMessageForQueryWithRequestInputProvided () {
98
-
99
- Locale locale = Locale .getDefault ();
100
- String executionId = this .idGenerator .generateId ().toString ();
101
- this .graphQlMessageHandler .setLocale (locale );
102
- this .graphQlMessageHandler .setExecutionId (executionId );
88
+ void testHandleMessageForQuery () {
103
89
104
90
StepVerifier verifier = StepVerifier .create (
105
91
Flux .from (this .resultChannel )
106
92
.map (Message ::getPayload )
107
- .cast (RequestOutput .class )
93
+ .cast (ExecutionResult .class )
108
94
)
109
95
.consumeNextWith (result -> {
110
- assertThat (result ).isInstanceOf (RequestOutput .class );
96
+ assertThat (result ).isInstanceOf (ExecutionResultImpl .class );
111
97
Map <String , Object > data = result .getData ();
112
98
Map <String , Object > testQuery = (Map <String , Object >) data .get ("testQuery" );
113
99
assertThat (testQuery .get ("id" )).isEqualTo ("test-data" );
114
- })
100
+ }
101
+ )
115
102
.thenCancel ()
116
103
.verifyLater ();
117
104
118
105
this .inputChannel .send (
119
106
MessageBuilder
120
- .withPayload (new RequestInput ("{ testQuery { id } }" , null , Collections .emptyMap (), locale , executionId ))
107
+ .withPayload (new RequestInput ("{ testQuery { id } }" , null , Collections .emptyMap ()))
121
108
.build ()
122
109
);
123
110
@@ -126,44 +113,18 @@ void testHandleMessageForQueryWithRequestInputProvided() {
126
113
127
114
@ Test
128
115
@ SuppressWarnings ("unchecked" )
129
- void testHandleMessageForQueryWithQueryProvided () {
130
-
131
- String executionId = this .idGenerator .generateId ().toString ();
132
- this .graphQlMessageHandler .setExecutionId (executionId );
133
-
134
- String fakeQuery = "{ testQuery { id } }" ;
135
- this .graphQlMessageHandler .setQuery (fakeQuery );
136
-
137
- StepVerifier .create (
138
- Mono .from ((Mono <RequestOutput >) this .graphQlMessageHandler .handleRequestMessage (MessageBuilder .withPayload (fakeQuery ).build ()))
139
- )
140
- .consumeNextWith (result -> {
141
- assertThat (result ).isInstanceOf (RequestOutput .class );
142
- Map <String , Object > data = result .getData ();
143
- Map <String , Object > testQuery = (Map <String , Object >) data .get ("testQuery" );
144
- assertThat (testQuery .get ("id" )).isEqualTo ("test-data" );
145
- })
146
- .expectComplete ()
147
- .verify ();
148
- }
149
-
150
- @ Test
151
- @ SuppressWarnings ("unchecked" )
152
- void testHandleMessageForMutationWithRequestInputProvided () {
153
-
154
- String executionId = this .idGenerator .generateId ().toString ();
155
- this .graphQlMessageHandler .setExecutionId (executionId );
116
+ void testHandleMessageForMutation () {
156
117
157
118
String fakeId = UUID .randomUUID ().toString ();
158
119
Update expected = new Update (fakeId );
159
120
160
121
StepVerifier verifier = StepVerifier .create (
161
122
Flux .from (this .resultChannel )
162
123
.map (Message ::getPayload )
163
- .cast (RequestOutput .class )
124
+ .cast (ExecutionResult .class )
164
125
)
165
126
.consumeNextWith (result -> {
166
- assertThat (result ).isInstanceOf (RequestOutput .class );
127
+ assertThat (result ).isInstanceOf (ExecutionResultImpl .class );
167
128
Map <String , Object > data = result .getData ();
168
129
Map <String , Object > update = (Map <String , Object >) data .get ("update" );
169
130
assertThat (update .get ("id" )).isEqualTo (fakeId );
@@ -176,7 +137,7 @@ void testHandleMessageForMutationWithRequestInputProvided() {
176
137
177
138
this .inputChannel .send (
178
139
MessageBuilder
179
- .withPayload (new RequestInput ("mutation { update(id: \" " + fakeId + "\" ) { id } }" , null , Collections .emptyMap (), null , executionId ))
140
+ .withPayload (new RequestInput ("mutation { update(id: \" " + fakeId + "\" ) { id } }" , null , Collections .emptyMap ()))
180
141
.build ()
181
142
);
182
143
@@ -191,22 +152,19 @@ void testHandleMessageForMutationWithRequestInputProvided() {
191
152
192
153
@ Test
193
154
@ SuppressWarnings ("unchecked" )
194
- void testHandleMessageForSubscriptionWithRequestInputProvided () {
195
-
196
- String executionId = this .idGenerator .generateId ().toString ();
197
- this .graphQlMessageHandler .setExecutionId (executionId );
155
+ void testHandleMessageForSubscription () {
198
156
199
157
StepVerifier verifier = StepVerifier .create (
200
158
Flux .from (this .resultChannel )
201
159
.map (Message ::getPayload )
202
- .cast (RequestOutput .class )
203
- .mapNotNull ( RequestOutput ::getData )
160
+ .cast (ExecutionResult .class )
161
+ .map ( ExecutionResult ::getData )
204
162
.cast (SubscriptionPublisher .class )
205
163
.map (Flux ::from )
206
164
.flatMap (data -> data )
207
165
)
208
- .consumeNextWith (requestOutput -> {
209
- Map <String , Object > results = (Map <String , Object >) requestOutput .getData ();
166
+ .consumeNextWith (executionResult -> {
167
+ Map <String , Object > results = (Map <String , Object >) executionResult .getData ();
210
168
assertThat (results ).containsKey ("results" );
211
169
212
170
Map <String , Object > queryResult = (Map <String , Object >) results .get ("results" );
@@ -221,7 +179,7 @@ void testHandleMessageForSubscriptionWithRequestInputProvided() {
221
179
222
180
this .inputChannel .send (
223
181
MessageBuilder
224
- .withPayload (new RequestInput ("subscription { results { id } }" , null , Collections .emptyMap (), null , executionId ))
182
+ .withPayload (new RequestInput ("subscription { results { id } }" , null , Collections .emptyMap ()))
225
183
.build ()
226
184
);
227
185
@@ -233,7 +191,7 @@ void testHandleMessageForQueryWithInvalidPayload() {
233
191
234
192
this .inputChannel .send (
235
193
MessageBuilder
236
- .withPayload (new Object () )
194
+ .withPayload ("{ testQuery { id } }" )
237
195
.build ()
238
196
);
239
197
@@ -244,16 +202,18 @@ void testHandleMessageForQueryWithInvalidPayload() {
244
202
.isInstanceOf (MessageHandlingException .class )
245
203
.satisfies ((ex ) -> assertThat ((Exception ) ex )
246
204
.hasMessageContaining (
247
- "'queryExpression' must not be null " ));
205
+ "Message payload needs to be 'org.springframework.graphql.RequestInput' " ));
248
206
249
207
}
250
208
251
209
@ Test
252
210
void testHandleMessageForMutationWithInvalidPayload () {
253
211
212
+ String fakeId = UUID .randomUUID ().toString ();
213
+
254
214
this .inputChannel .send (
255
215
MessageBuilder
256
- .withPayload (new Object () )
216
+ .withPayload ("mutation { update(id: \" " + fakeId + " \" ) { id } }" )
257
217
.build ()
258
218
);
259
219
@@ -264,7 +224,7 @@ void testHandleMessageForMutationWithInvalidPayload() {
264
224
.isInstanceOf (MessageHandlingException .class )
265
225
.satisfies ((ex ) -> assertThat ((Exception ) ex )
266
226
.hasMessageContaining (
267
- "'queryExpression' must not be null " ));
227
+ "Message payload needs to be 'org.springframework.graphql.RequestInput' " ));
268
228
269
229
}
270
230
@@ -273,7 +233,7 @@ void testHandleMessageForSubscriptionWithInvalidPayload() {
273
233
274
234
this .inputChannel .send (
275
235
MessageBuilder
276
- .withPayload (new Object () )
236
+ .withPayload ("subscription { results { id } }" )
277
237
.build ()
278
238
);
279
239
@@ -284,7 +244,7 @@ void testHandleMessageForSubscriptionWithInvalidPayload() {
284
244
.isInstanceOf (MessageHandlingException .class )
285
245
.satisfies ((ex ) -> assertThat ((Exception ) ex )
286
246
.hasMessageContaining (
287
- "'queryExpression' must not be null " ));
247
+ "Message payload needs to be 'org.springframework.graphql.RequestInput' " ));
288
248
289
249
}
290
250
@@ -302,11 +262,6 @@ public Mono<QueryResult> testQuery() {
302
262
return Mono .just (new QueryResult ("test-data" ));
303
263
}
304
264
305
- @ QueryMapping
306
- public Mono <QueryResult > testQueryById (@ Argument String id ) {
307
- return Mono .just (new QueryResult ("test-data" ));
308
- }
309
-
310
265
@ MutationMapping
311
266
public Mono <Update > update (@ Argument String id ) {
312
267
return this .updateRepository .save (new Update (id ));
@@ -403,12 +358,6 @@ AnnotatedControllerConfigurer annotatedDataFetcherConfigurer() {
403
358
return new AnnotatedControllerConfigurer ();
404
359
}
405
360
406
- @ Bean
407
- IdGenerator idGenerator () {
408
-
409
- return new AlternativeJdkIdGenerator ();
410
- }
411
-
412
361
}
413
362
414
363
static class QueryResult {
0 commit comments