Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 154f0ea

Browse files
authored
Merge pull request #382 from BlasiusSecundus/feature/subscription-testing
Add facilities to test subscriptions
2 parents 274e7c0 + f63531a commit 154f0ea

File tree

36 files changed

+1173
-1
lines changed

36 files changed

+1173
-1
lines changed

graphql-spring-boot-test-autoconfigure/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ dependencies {
2626
implementation("org.springframework.boot:spring-boot-starter-actuator")
2727
implementation("org.springframework.boot:spring-boot-starter-websocket")
2828
implementation("com.graphql-java-kickstart:graphql-java-tools:$LIB_GRAPHQL_JAVA_TOOLS_VER")
29+
30+
testImplementation "io.reactivex.rxjava2:rxjava"
2931
}
3032

3133
compileJava.dependsOn(processResources)

graphql-spring-boot-test-autoconfigure/src/main/java/com/graphql/spring/boot/test/GraphQLTestAutoConfiguration.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.graphql.spring.boot.test;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.springframework.beans.factory.annotation.Value;
35
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
46
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
57
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
68
import org.springframework.context.annotation.Bean;
79
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.core.env.Environment;
811

912
@Configuration
1013
@ConditionalOnWebApplication
@@ -17,4 +20,15 @@ public GraphQLTestTemplate graphQLTestUtils() {
1720
return new GraphQLTestTemplate();
1821
}
1922

23+
@Bean
24+
@ConditionalOnMissingBean
25+
public GraphQLTestSubscription graphQLTestSubscription(
26+
final Environment environment,
27+
final ObjectMapper objectMapper,
28+
@Value("${graphql.servlet.subscriptions.websocket.path:subscriptions}")
29+
final String subscriptionPath
30+
) {
31+
return new GraphQLTestSubscription(environment, objectMapper, subscriptionPath);
32+
}
33+
2034
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
import org.springframework.context.ApplicationContext;
6+
7+
import java.io.IOException;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
12+
public class GraphQLTestAutoConfigurationTestBase {
13+
14+
static final String FOO = "foo";
15+
16+
@Autowired
17+
ApplicationContext applicationContext;
18+
19+
void assertThatTestSubscriptionWorksCorrectly() {
20+
// GIVEN
21+
final GraphQLTestSubscription graphQLTestSubscription
22+
= applicationContext.getBean(GraphQLTestSubscription.class);
23+
// WHEN
24+
final GraphQLResponse graphQLResponse
25+
= graphQLTestSubscription.start("test-subscription.graphql").awaitAndGetNextResponse(1000);
26+
// THEN
27+
assertThat(graphQLResponse.get("$.data.testSubscription")).isEqualTo(FOO);
28+
}
29+
30+
void assertThatTestTemplateAutoConfigurationWorksCorrectly() throws IOException {
31+
// GIVEN
32+
final GraphQLTestTemplate graphQLTestTemplate
33+
= applicationContext.getBean(GraphQLTestTemplate.class);
34+
// WHEN
35+
final GraphQLResponse graphQLResponse
36+
= graphQLTestTemplate.postForResource("test-query.graphql");
37+
// THEN
38+
assertThat(graphQLResponse.get("$.data.testQuery")).isEqualTo(FOO);
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.test.context.ActiveProfiles;
6+
import org.springframework.test.util.ReflectionTestUtils;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
@ActiveProfiles({"test", "custom-subscription-path"})
11+
@DisplayName("Testing the auto-configuration of the GraphQLTestSubscription bean / custom subscription endpoint.")
12+
public class GraphQLTestSubscriptionAutoConfigurationCustomConfigTest extends GraphQLTestAutoConfigurationTestBase {
13+
14+
@Test
15+
@DisplayName("Should provide a GraphQLTestTemplate bean.")
16+
void shouldProvideGraphQLTestSubscriptionBean() {
17+
assertThatTestSubscriptionWorksCorrectly();
18+
assertThat(ReflectionTestUtils.getField(applicationContext.getBean(GraphQLTestSubscription.class),
19+
"subscriptionPath"))
20+
.as("Should use the configured subscription path.")
21+
.isEqualTo("/myCustomPath");
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
@DisplayName("Testing the auto-configuration of the GraphQLTestSubscription bean / default settings.")
7+
public class GraphQLTestSubscriptionAutoConfigurationDefaultConfigTest extends GraphQLTestAutoConfigurationTestBase {
8+
9+
@Test
10+
@DisplayName("Should provide a GraphQLTestTemplate bean.")
11+
void shouldProvideGraphQLTestSubscriptionBean() {
12+
assertThatTestSubscriptionWorksCorrectly();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.test.context.ActiveProfiles;
6+
import org.springframework.test.util.ReflectionTestUtils;
7+
8+
import java.io.IOException;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
@ActiveProfiles({"test", "custom-servlet-mapping"})
13+
@DisplayName("Testing auto-configuration of the GraphQLTestTemplate bean / custom servlet endpoint.")
14+
public class GraphQLTestTemplateAutoConfigurationCustomConfigTest extends GraphQLTestAutoConfigurationTestBase {
15+
16+
@Test
17+
@DisplayName("GraphQLTestTemplate bean should work properly.")
18+
void shouldProvideGraphQLTestTemplateBean() throws IOException {
19+
assertThatTestTemplateAutoConfigurationWorksCorrectly();
20+
assertThat(ReflectionTestUtils.getField(applicationContext.getBean(GraphQLTestTemplate.class),
21+
"graphqlMapping"))
22+
.as("Should use the configured servlet path.")
23+
.isEqualTo("/myCustomGraphQLEndpoint");
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.io.IOException;
7+
8+
@DisplayName("Testing auto-configuration of the GraphQLTestTemplate bean / default servlet endpoint.")
9+
public class GraphQLTestTemplateAutoConfigurationDefaultConfigTest extends GraphQLTestAutoConfigurationTestBase {
10+
11+
@Test
12+
@DisplayName("GraphQLTestTemplate bean should work properly.")
13+
void shouldProvideGraphQLTestTemplateBean() throws IOException {
14+
assertThatTestTemplateAutoConfigurationWorksCorrectly();
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class TestApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(TestApplication.class, args);
11+
}
12+
}
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.graphql.spring.boot.test.beans;
2+
3+
import graphql.kickstart.tools.GraphQLQueryResolver;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class TestQuery implements GraphQLQueryResolver {
8+
9+
public String testQuery() {
10+
return "foo";
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.graphql.spring.boot.test.beans;
2+
3+
import graphql.kickstart.tools.GraphQLSubscriptionResolver;
4+
import io.reactivex.Flowable;
5+
import org.springframework.stereotype.Service;
6+
7+
@Service
8+
public class TestSubscription implements GraphQLSubscriptionResolver {
9+
10+
public Flowable<String> testSubscription() {
11+
return Flowable.just("foo");
12+
}
13+
}

0 commit comments

Comments
 (0)