Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.

Commit d3034e7

Browse files
committed
testing
1 parent 1e452da commit d3034e7

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
21
version = "1.0"
32

43
def springBootVersion = "2.1.0.RELEASE"
54

65
dependencies {
76
compile "org.springframework.boot:spring-boot-autoconfigure:$springBootVersion"
87
compile project(':graphql-java-spring-webflux')
8+
9+
testImplementation("org.springframework.boot:spring-boot-starter-webflux:$springBootVersion")
10+
testImplementation("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
911
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package graphql.spring.web.reactive;
2+
3+
import graphql.ExecutionInput;
4+
import graphql.ExecutionResultImpl;
5+
import graphql.GraphQL;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.mockito.ArgumentCaptor;
9+
import org.mockito.Mockito;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
import org.springframework.test.web.reactive.server.WebTestClient;
14+
15+
import java.util.concurrent.CompletableFuture;
16+
17+
import static org.hamcrest.CoreMatchers.is;
18+
import static org.junit.Assert.assertThat;
19+
20+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = IntegrationTestConfig.class)
21+
@RunWith(SpringRunner.class)
22+
public class IntegrationTest {
23+
24+
@Autowired
25+
private WebTestClient webClient;
26+
27+
@Autowired
28+
GraphQL graphql;
29+
30+
@Test
31+
public void endpointIsAvailable() {
32+
String query = "{foo}";
33+
34+
ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
35+
.data("bar")
36+
.build();
37+
CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
38+
ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
39+
Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
40+
41+
this.webClient.get().uri("/graphql?query={query}", query).exchange().expectStatus().isOk()
42+
.expectBody(String.class).isEqualTo("{\"data\":\"bar\"}");
43+
44+
assertThat(captor.getValue().getQuery(), is(query));
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package graphql.spring.web.reactive;
2+
3+
import graphql.GraphQL;
4+
import org.mockito.Mockito;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
8+
@SpringBootApplication
9+
public class IntegrationTestConfig {
10+
11+
12+
@Bean
13+
public GraphQL graphQL() {
14+
return Mockito.mock(GraphQL.class);
15+
}
16+
17+
}
18+

graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/GraphQLController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import reactor.core.publisher.Mono;
1919

2020
import java.io.IOException;
21+
import java.util.Collections;
2122
import java.util.Map;
2223

2324
@RestController
@@ -61,6 +62,7 @@ public Object graphqlGET(
6162
}
6263

6364
private Map<String, Object> convertVariablesJson(String jsonMap) {
65+
if (jsonMap == null) return Collections.emptyMap();
6466
try {
6567
return objectMapper.readValue(jsonMap, Map.class);
6668
} catch (IOException e) {

graphql-java-spring-webflux/src/test/java/graphql/spring/web/reactive/components/GraphQLControllerTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,30 @@ public void testGetRequest() throws Exception {
111111
assertThat(captor.getValue().getVariables(), is(variables));
112112
assertThat(captor.getValue().getOperationName(), is(operationName));
113113
}
114+
115+
@Test
116+
public void testSimpleGetRequest() throws Exception {
117+
String query = "{foo}";
118+
String queryString = URLEncoder.encode(query, "UTF-8");
119+
120+
ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
121+
.data("bar")
122+
.build();
123+
CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
124+
ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
125+
Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
126+
127+
client.get().uri(uriBuilder -> uriBuilder.path("/graphql")
128+
.queryParam("query", queryString)
129+
.build())
130+
.accept(MediaType.APPLICATION_JSON_UTF8)
131+
.exchange()
132+
.expectStatus().isOk()
133+
.expectBody()
134+
.jsonPath("data").isEqualTo("bar");
135+
136+
assertThat(captor.getAllValues().size(), is(1));
137+
assertThat(captor.getValue().getQuery(), is(query));
138+
}
139+
114140
}

0 commit comments

Comments
 (0)