1
1
/*
2
- * Copyright 2021 the original author or authors.
2
+ * Copyright 2021-2022 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 ;
23
24
import java .util .Map ;
24
25
import java .util .Objects ;
25
26
import java .util .UUID ;
32
33
import org .springframework .core .io .ClassPathResource ;
33
34
import org .springframework .graphql .GraphQlService ;
34
35
import org .springframework .graphql .RequestInput ;
36
+ import org .springframework .graphql .RequestOutput ;
35
37
import org .springframework .graphql .data .method .annotation .Argument ;
36
38
import org .springframework .graphql .data .method .annotation .MutationMapping ;
37
39
import org .springframework .graphql .data .method .annotation .QueryMapping ;
54
56
import org .springframework .stereotype .Repository ;
55
57
import org .springframework .test .annotation .DirtiesContext ;
56
58
import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
59
+ import org .springframework .util .AlternativeJdkIdGenerator ;
60
+ import org .springframework .util .IdGenerator ;
57
61
58
- import graphql .ExecutionResult ;
59
- import graphql .ExecutionResultImpl ;
60
62
import graphql .execution .reactive .SubscriptionPublisher ;
61
63
import reactor .core .publisher .Flux ;
62
64
import reactor .core .publisher .Mono ;
@@ -86,17 +88,26 @@ public class GraphQlMessageHandlerTests {
86
88
@ Autowired
87
89
private UpdateRepository updateRepository ;
88
90
91
+ @ Autowired
92
+ private IdGenerator idGenerator ;
93
+
94
+
89
95
@ Test
90
96
@ SuppressWarnings ("unchecked" )
91
97
void testHandleMessageForQueryWithRequestInputProvided () {
92
98
99
+ Locale locale = Locale .getDefault ();
100
+ String executionId = this .idGenerator .generateId ().toString ();
101
+ this .graphQlMessageHandler .setLocale (locale );
102
+ this .graphQlMessageHandler .setExecutionId (executionId );
103
+
93
104
StepVerifier verifier = StepVerifier .create (
94
105
Flux .from (this .resultChannel )
95
106
.map (Message ::getPayload )
96
- .cast (ExecutionResult .class )
107
+ .cast (RequestOutput .class )
97
108
)
98
109
.consumeNextWith (result -> {
99
- assertThat (result ).isInstanceOf (ExecutionResultImpl .class );
110
+ assertThat (result ).isInstanceOf (RequestOutput .class );
100
111
Map <String , Object > data = result .getData ();
101
112
Map <String , Object > testQuery = (Map <String , Object >) data .get ("testQuery" );
102
113
assertThat (testQuery .get ("id" )).isEqualTo ("test-data" );
@@ -106,7 +117,7 @@ void testHandleMessageForQueryWithRequestInputProvided() {
106
117
107
118
this .inputChannel .send (
108
119
MessageBuilder
109
- .withPayload (new RequestInput ("{ testQuery { id } }" , null , Collections .emptyMap ()))
120
+ .withPayload (new RequestInput ("{ testQuery { id } }" , null , Collections .emptyMap (), locale , executionId ))
110
121
.build ()
111
122
);
112
123
@@ -117,14 +128,17 @@ void testHandleMessageForQueryWithRequestInputProvided() {
117
128
@ SuppressWarnings ("unchecked" )
118
129
void testHandleMessageForQueryWithQueryProvided () {
119
130
131
+ String executionId = this .idGenerator .generateId ().toString ();
132
+ this .graphQlMessageHandler .setExecutionId (executionId );
133
+
120
134
String fakeQuery = "{ testQuery { id } }" ;
121
135
this .graphQlMessageHandler .setQuery (fakeQuery );
122
136
123
137
StepVerifier .create (
124
- Mono .from ((Mono <ExecutionResult >) this .graphQlMessageHandler .handleRequestMessage (MessageBuilder .withPayload (fakeQuery ).build ()))
138
+ Mono .from ((Mono <RequestOutput >) this .graphQlMessageHandler .handleRequestMessage (MessageBuilder .withPayload (fakeQuery ).build ()))
125
139
)
126
140
.consumeNextWith (result -> {
127
- assertThat (result ).isInstanceOf (ExecutionResultImpl .class );
141
+ assertThat (result ).isInstanceOf (RequestOutput .class );
128
142
Map <String , Object > data = result .getData ();
129
143
Map <String , Object > testQuery = (Map <String , Object >) data .get ("testQuery" );
130
144
assertThat (testQuery .get ("id" )).isEqualTo ("test-data" );
@@ -137,16 +151,19 @@ void testHandleMessageForQueryWithQueryProvided() {
137
151
@ SuppressWarnings ("unchecked" )
138
152
void testHandleMessageForMutationWithRequestInputProvided () {
139
153
154
+ String executionId = this .idGenerator .generateId ().toString ();
155
+ this .graphQlMessageHandler .setExecutionId (executionId );
156
+
140
157
String fakeId = UUID .randomUUID ().toString ();
141
158
Update expected = new Update (fakeId );
142
159
143
160
StepVerifier verifier = StepVerifier .create (
144
161
Flux .from (this .resultChannel )
145
162
.map (Message ::getPayload )
146
- .cast (ExecutionResult .class )
163
+ .cast (RequestOutput .class )
147
164
)
148
165
.consumeNextWith (result -> {
149
- assertThat (result ).isInstanceOf (ExecutionResultImpl .class );
166
+ assertThat (result ).isInstanceOf (RequestOutput .class );
150
167
Map <String , Object > data = result .getData ();
151
168
Map <String , Object > update = (Map <String , Object >) data .get ("update" );
152
169
assertThat (update .get ("id" )).isEqualTo (fakeId );
@@ -159,7 +176,7 @@ void testHandleMessageForMutationWithRequestInputProvided() {
159
176
160
177
this .inputChannel .send (
161
178
MessageBuilder
162
- .withPayload (new RequestInput ("mutation { update(id: \" " + fakeId + "\" ) { id } }" , null , Collections .emptyMap ()))
179
+ .withPayload (new RequestInput ("mutation { update(id: \" " + fakeId + "\" ) { id } }" , null , Collections .emptyMap (), null , executionId ))
163
180
.build ()
164
181
);
165
182
@@ -176,17 +193,20 @@ void testHandleMessageForMutationWithRequestInputProvided() {
176
193
@ SuppressWarnings ("unchecked" )
177
194
void testHandleMessageForSubscriptionWithRequestInputProvided () {
178
195
196
+ String executionId = this .idGenerator .generateId ().toString ();
197
+ this .graphQlMessageHandler .setExecutionId (executionId );
198
+
179
199
StepVerifier verifier = StepVerifier .create (
180
200
Flux .from (this .resultChannel )
181
201
.map (Message ::getPayload )
182
- .cast (ExecutionResult .class )
183
- .map ( ExecutionResult ::getData )
202
+ .cast (RequestOutput .class )
203
+ .mapNotNull ( RequestOutput ::getData )
184
204
.cast (SubscriptionPublisher .class )
185
205
.map (Flux ::from )
186
206
.flatMap (data -> data )
187
207
)
188
- .consumeNextWith (executionResult -> {
189
- Map <String , Object > results = (Map <String , Object >) executionResult .getData ();
208
+ .consumeNextWith (requestOutput -> {
209
+ Map <String , Object > results = (Map <String , Object >) requestOutput .getData ();
190
210
assertThat (results ).containsKey ("results" );
191
211
192
212
Map <String , Object > queryResult = (Map <String , Object >) results .get ("results" );
@@ -201,7 +221,7 @@ void testHandleMessageForSubscriptionWithRequestInputProvided() {
201
221
202
222
this .inputChannel .send (
203
223
MessageBuilder
204
- .withPayload (new RequestInput ("subscription { results { id } }" , null , Collections .emptyMap ()))
224
+ .withPayload (new RequestInput ("subscription { results { id } }" , null , Collections .emptyMap (), null , executionId ))
205
225
.build ()
206
226
);
207
227
@@ -383,6 +403,12 @@ AnnotatedControllerConfigurer annotatedDataFetcherConfigurer() {
383
403
return new AnnotatedControllerConfigurer ();
384
404
}
385
405
406
+ @ Bean
407
+ IdGenerator idGenerator () {
408
+
409
+ return new AlternativeJdkIdGenerator ();
410
+ }
411
+
386
412
}
387
413
388
414
static class QueryResult {
0 commit comments