Skip to content

Commit ba25265

Browse files
committed
refactor to reflect feedback
issue spring-projects#3501
1 parent 747f78b commit ba25265

File tree

4 files changed

+30
-825
lines changed

4 files changed

+30
-825
lines changed

spring-integration-graphql/src/test/java/org/springframework/integration/graphql/outbound/GraphQlMessageHandlerTests.java

+30-81
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2022 the original author or authors.
2+
* Copyright 2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020

2121
import java.time.Duration;
2222
import java.util.Collections;
23-
import java.util.Locale;
2423
import java.util.Map;
2524
import java.util.Objects;
2625
import java.util.UUID;
@@ -33,7 +32,6 @@
3332
import org.springframework.core.io.ClassPathResource;
3433
import org.springframework.graphql.GraphQlService;
3534
import org.springframework.graphql.RequestInput;
36-
import org.springframework.graphql.RequestOutput;
3735
import org.springframework.graphql.data.method.annotation.Argument;
3836
import org.springframework.graphql.data.method.annotation.MutationMapping;
3937
import org.springframework.graphql.data.method.annotation.QueryMapping;
@@ -56,9 +54,9 @@
5654
import org.springframework.stereotype.Repository;
5755
import org.springframework.test.annotation.DirtiesContext;
5856
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
59-
import org.springframework.util.AlternativeJdkIdGenerator;
60-
import org.springframework.util.IdGenerator;
6157

58+
import graphql.ExecutionResult;
59+
import graphql.ExecutionResultImpl;
6260
import graphql.execution.reactive.SubscriptionPublisher;
6361
import reactor.core.publisher.Flux;
6462
import reactor.core.publisher.Mono;
@@ -67,10 +65,10 @@
6765
/**
6866
*
6967
* @author Daniel Frey
70-
* @since 6.0
68+
*
7169
*/
72-
@SpringJUnitConfig
73-
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
70+
@SpringJUnitConfig(GraphQlMessageHandlerTests.TestConfig.class)
71+
@DirtiesContext
7472
public class GraphQlMessageHandlerTests {
7573

7674
@Autowired
@@ -82,42 +80,31 @@ public class GraphQlMessageHandlerTests {
8280
@Autowired
8381
private PollableChannel errorChannel;
8482

85-
@Autowired
86-
private GraphQlMessageHandler graphQlMessageHandler;
87-
8883
@Autowired
8984
private UpdateRepository updateRepository;
9085

91-
@Autowired
92-
private IdGenerator idGenerator;
93-
94-
9586
@Test
9687
@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() {
10389

10490
StepVerifier verifier = StepVerifier.create(
10591
Flux.from(this.resultChannel)
10692
.map(Message::getPayload)
107-
.cast(RequestOutput.class)
93+
.cast(ExecutionResult.class)
10894
)
10995
.consumeNextWith(result -> {
110-
assertThat(result).isInstanceOf(RequestOutput.class);
96+
assertThat(result).isInstanceOf(ExecutionResultImpl.class);
11197
Map<String, Object> data = result.getData();
11298
Map<String, Object> testQuery = (Map<String, Object>) data.get("testQuery");
11399
assertThat(testQuery.get("id")).isEqualTo("test-data");
114-
})
100+
}
101+
)
115102
.thenCancel()
116103
.verifyLater();
117104

118105
this.inputChannel.send(
119106
MessageBuilder
120-
.withPayload(new RequestInput("{ testQuery { id } }", null, Collections.emptyMap(), locale, executionId))
107+
.withPayload(new RequestInput("{ testQuery { id } }", null, Collections.emptyMap()))
121108
.build()
122109
);
123110

@@ -126,44 +113,18 @@ void testHandleMessageForQueryWithRequestInputProvided() {
126113

127114
@Test
128115
@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() {
156117

157118
String fakeId = UUID.randomUUID().toString();
158119
Update expected = new Update(fakeId);
159120

160121
StepVerifier verifier = StepVerifier.create(
161122
Flux.from(this.resultChannel)
162123
.map(Message::getPayload)
163-
.cast(RequestOutput.class)
124+
.cast(ExecutionResult.class)
164125
)
165126
.consumeNextWith(result -> {
166-
assertThat(result).isInstanceOf(RequestOutput.class);
127+
assertThat(result).isInstanceOf(ExecutionResultImpl.class);
167128
Map<String, Object> data = result.getData();
168129
Map<String, Object> update = (Map<String, Object>) data.get("update");
169130
assertThat(update.get("id")).isEqualTo(fakeId);
@@ -176,7 +137,7 @@ void testHandleMessageForMutationWithRequestInputProvided() {
176137

177138
this.inputChannel.send(
178139
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()))
180141
.build()
181142
);
182143

@@ -191,22 +152,19 @@ void testHandleMessageForMutationWithRequestInputProvided() {
191152

192153
@Test
193154
@SuppressWarnings("unchecked")
194-
void testHandleMessageForSubscriptionWithRequestInputProvided() {
195-
196-
String executionId = this.idGenerator.generateId().toString();
197-
this.graphQlMessageHandler.setExecutionId(executionId);
155+
void testHandleMessageForSubscription() {
198156

199157
StepVerifier verifier = StepVerifier.create(
200158
Flux.from(this.resultChannel)
201159
.map(Message::getPayload)
202-
.cast(RequestOutput.class)
203-
.mapNotNull(RequestOutput::getData)
160+
.cast(ExecutionResult.class)
161+
.map(ExecutionResult::getData)
204162
.cast(SubscriptionPublisher.class)
205163
.map(Flux::from)
206164
.flatMap(data -> data)
207165
)
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();
210168
assertThat(results).containsKey("results");
211169

212170
Map<String, Object> queryResult = (Map<String, Object>) results.get("results");
@@ -221,7 +179,7 @@ void testHandleMessageForSubscriptionWithRequestInputProvided() {
221179

222180
this.inputChannel.send(
223181
MessageBuilder
224-
.withPayload(new RequestInput("subscription { results { id } }", null, Collections.emptyMap(), null, executionId))
182+
.withPayload(new RequestInput("subscription { results { id } }", null, Collections.emptyMap()))
225183
.build()
226184
);
227185

@@ -233,7 +191,7 @@ void testHandleMessageForQueryWithInvalidPayload() {
233191

234192
this.inputChannel.send(
235193
MessageBuilder
236-
.withPayload(new Object())
194+
.withPayload("{ testQuery { id } }")
237195
.build()
238196
);
239197

@@ -244,16 +202,18 @@ void testHandleMessageForQueryWithInvalidPayload() {
244202
.isInstanceOf(MessageHandlingException.class)
245203
.satisfies((ex) -> assertThat((Exception) ex)
246204
.hasMessageContaining(
247-
"'queryExpression' must not be null"));
205+
"Message payload needs to be 'org.springframework.graphql.RequestInput'"));
248206

249207
}
250208

251209
@Test
252210
void testHandleMessageForMutationWithInvalidPayload() {
253211

212+
String fakeId = UUID.randomUUID().toString();
213+
254214
this.inputChannel.send(
255215
MessageBuilder
256-
.withPayload(new Object())
216+
.withPayload("mutation { update(id: \"" + fakeId + "\") { id } }")
257217
.build()
258218
);
259219

@@ -264,7 +224,7 @@ void testHandleMessageForMutationWithInvalidPayload() {
264224
.isInstanceOf(MessageHandlingException.class)
265225
.satisfies((ex) -> assertThat((Exception) ex)
266226
.hasMessageContaining(
267-
"'queryExpression' must not be null"));
227+
"Message payload needs to be 'org.springframework.graphql.RequestInput'"));
268228

269229
}
270230

@@ -273,7 +233,7 @@ void testHandleMessageForSubscriptionWithInvalidPayload() {
273233

274234
this.inputChannel.send(
275235
MessageBuilder
276-
.withPayload(new Object())
236+
.withPayload("subscription { results { id } }")
277237
.build()
278238
);
279239

@@ -284,7 +244,7 @@ void testHandleMessageForSubscriptionWithInvalidPayload() {
284244
.isInstanceOf(MessageHandlingException.class)
285245
.satisfies((ex) -> assertThat((Exception) ex)
286246
.hasMessageContaining(
287-
"'queryExpression' must not be null"));
247+
"Message payload needs to be 'org.springframework.graphql.RequestInput'"));
288248

289249
}
290250

@@ -302,11 +262,6 @@ public Mono<QueryResult> testQuery() {
302262
return Mono.just(new QueryResult("test-data"));
303263
}
304264

305-
@QueryMapping
306-
public Mono<QueryResult> testQueryById(@Argument String id) {
307-
return Mono.just(new QueryResult("test-data"));
308-
}
309-
310265
@MutationMapping
311266
public Mono<Update> update(@Argument String id) {
312267
return this.updateRepository.save(new Update(id));
@@ -403,12 +358,6 @@ AnnotatedControllerConfigurer annotatedDataFetcherConfigurer() {
403358
return new AnnotatedControllerConfigurer();
404359
}
405360

406-
@Bean
407-
IdGenerator idGenerator() {
408-
409-
return new AlternativeJdkIdGenerator();
410-
}
411-
412361
}
413362

414363
static class QueryResult {

0 commit comments

Comments
 (0)