Upgrade samples to match the GraphQlTest updates

See gh-310
This commit is contained in:
rstoyanchev
2022-03-09 12:47:08 +00:00
parent 19793a6e74
commit 4818ee6c81
14 changed files with 125 additions and 175 deletions

View File

@@ -1,35 +0,0 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql;
import reactor.core.publisher.Mono;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
/**
* WebFilter that inserts a key-value pair into the Reactor context which is transferred
* to and accessible in Reactor data fetchers.
*/
public class ContextWebFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange).contextWrite(context -> context.put("name", "007"));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,24 +33,17 @@ public class DataRepository {
}
public Mono<String> getGreeting() {
return Mono.deferContextual(context -> {
Object name = context.get("name");
return Mono.delay(Duration.ofMillis(50)).map(aLong -> "Hello " + name);
});
return Mono.delay(Duration.ofMillis(50)).map(aLong -> "Hello!");
}
public Flux<String> getGreetings() {
return Mono.delay(Duration.ofMillis(50)).flatMapMany(aLong -> Flux.deferContextual(context -> {
String name = context.get("name");
return Flux.just("Hi", "Bonjour", "Hola", "Ciao", "Zdravo").map(s -> s + " " + name);
}));
return Mono.delay(Duration.ofMillis(50))
.flatMapMany(aLong -> Flux.just("Hi!", "Bonjour!", "Hola!", "Ciao!", "Zdravo!"));
}
public Flux<String> getGreetingsStream() {
return Mono.delay(Duration.ofMillis(50)).flatMapMany(aLong -> Flux.deferContextual(context -> {
String name = context.get("name");
return Flux.just("Hi", "Bonjour", "Hola", "Ciao", "Zdravo").map(s -> s + " " + name);
}));
return Mono.delay(Duration.ofMillis(50))
.flatMapMany(aLong -> Flux.just("Hi!", "Bonjour!", "Hola!", "Ciao!", "Zdravo!"));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package io.spring.sample.graphql;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SampleApplication {
@@ -27,9 +26,4 @@ public class SampleApplication {
SpringApplication.run(SampleApplication.class, args);
}
@Bean
ContextWebFilter reactorContextWebFilter() {
return new ContextWebFilter();
}
}

View File

@@ -1,37 +0,0 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Collaborator and additional components for {@link SampleController} slice tests.
*/
@Configuration
class TestConfig {
@Bean
public ContextWebFilter contextWebFilter() {
return new ContextWebFilter();
}
@Bean
public DataRepository dataRepository() {
return new DataRepository();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,8 +15,6 @@
*/
package io.spring.sample.graphql;
import graphql.GraphQL;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
@@ -24,49 +22,44 @@ import reactor.test.StepVerifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
import org.springframework.context.annotation.Import;
import org.springframework.graphql.GraphQlService;
import org.springframework.graphql.test.tester.GraphQlTester;
/**
* GraphQL subscription tests directly via {@link GraphQL}.
* GraphQL over WebSocket subscription tests.
*/
@GraphQlTest(SampleController.class)
@Import(TestConfig.class)
public class SubscriptionTests {
@Import(DataRepository.class)
public class WebFluxWebSocketSampleSubscriptionTests {
@Autowired
private GraphQlTester graphQlTester;
@BeforeEach
public void setUp(@Autowired GraphQlService service) {
this.graphQlTester = GraphQlTester.create(requestInput ->
service.execute(requestInput).contextWrite(context -> context.put("name", "James")));
}
@Test
void subscriptionWithEntityPath() {
Flux<String> result = this.graphQlTester.query("subscription { greetings }")
Flux<String> result = this.graphQlTester.document("subscription { greetings }")
.executeSubscription()
.toFlux("greetings", String.class);
StepVerifier.create(result)
.expectNext("Hi James")
.expectNext("Bonjour James")
.expectNext("Hola James")
.expectNext("Ciao James")
.expectNext("Zdravo James")
.expectNext("Hi!")
.expectNext("Bonjour!")
.expectNext("Hola!")
.expectNext("Ciao!")
.expectNext("Zdravo!")
.verifyComplete();
}
@Test
void subscriptionWithResponseSpec() {
Flux<GraphQlTester.ResponseSpec> result = this.graphQlTester.query("subscription { greetings }")
void subscriptionWithResponse() {
Flux<GraphQlTester.Response> result = this.graphQlTester.document("subscription { greetings }")
.executeSubscription()
.toFlux();
StepVerifier.create(result)
.consumeNextWith(spec -> spec.path("greetings").valueExists())
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Bonjour James\""))
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Hola James\""))
.consumeNextWith(response -> response.path("greetings").valueExists())
.consumeNextWith(response -> response.path("greetings").matchesJson("\"Bonjour!\""))
.consumeNextWith(response -> response.path("greetings").matchesJson("\"Hola!\""))
.expectNextCount(2)
.verifyComplete();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,47 +15,40 @@
*/
package io.spring.sample.graphql;
import graphql.GraphQL;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
import org.springframework.context.annotation.Import;
import org.springframework.graphql.GraphQlService;
import org.springframework.graphql.test.tester.GraphQlTester;
/**
* GraphQL query tests directly via {@link GraphQL}.
* GraphQL over WebSocket single response tests.
*/
@GraphQlTest(SampleController.class)
@Import(TestConfig.class)
public class QueryTests {
@Import(DataRepository.class)
public class WebFluxWebSocketSampleTests {
@Autowired
private GraphQlTester graphQlTester;
@BeforeEach
public void setUp(@Autowired GraphQlService service) {
this.graphQlTester = GraphQlTester.create(requestInput ->
service.execute(requestInput).contextWrite(context -> context.put("name", "James")));
}
@Test
void greetingMono() {
this.graphQlTester.query("{greetingMono}")
this.graphQlTester.document("{greetingMono}")
.execute()
.path("greetingMono")
.entity(String.class)
.isEqualTo("Hello James");
.isEqualTo("Hello!");
}
@Test
void greetingsFlux() {
this.graphQlTester.query("{greetingsFlux}")
this.graphQlTester.document("{greetingsFlux}")
.execute()
.path("greetingsFlux")
.entityList(String.class)
.containsExactly("Hi James", "Bonjour James", "Hola James", "Ciao James", "Zdravo James");
.containsExactly("Hi!", "Bonjour!", "Hola!", "Ciao!", "Zdravo!");
}
}