Add tests to webmvc-http sample

This commit is contained in:
Rossen Stoyanchev
2021-03-11 17:12:49 +00:00
parent b48cacb10a
commit 3843e83d43
6 changed files with 231 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework:spring-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {

View File

@@ -0,0 +1,68 @@
/*
* 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.project;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
class JsonRequest {
private final String query;
@Nullable
private final String operationName;
private final Map<String, Object> variables;
private JsonRequest(String query, @Nullable String operationName, Map<String, Object> variables) {
this.query = query;
this.operationName = operationName;
this.variables = (!CollectionUtils.isEmpty(variables) ?
new LinkedHashMap<>(variables) : Collections.emptyMap());
}
public String getQuery() {
return query;
}
public String query() {
return this.query;
}
@Nullable
public String operationName() {
return this.operationName;
}
public Map<String, Object> variables() {
return this.variables;
}
public static JsonRequest create(String query) {
return new JsonRequest(query, null, Collections.emptyMap());
}
public static JsonRequest create(String query, String operationName, Map<String, Object> variables) {
return new JsonRequest(query, operationName, variables);
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.project;
import java.util.Collections;
import java.util.Map;
import org.springframework.lang.Nullable;
class JsonResponse<T> {
private Map<String, T> data = Collections.emptyMap();
private Map<String, Map<String, Object>> errors = Collections.emptyMap();
public void setData(@Nullable Map<String, T> data) {
this.data = data;
}
public T getDataEntry() {
return this.data.values().iterator().next();
}
public void setErrors(@Nullable Map<String, Map<String, Object>> errors) {
this.errors = errors;
}
public Map<String, Map<String, Object>> getErrors() {
return this.errors;
}
}

View File

@@ -0,0 +1,114 @@
/*
* 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.project;
import java.util.List;
import java.util.function.Consumer;
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.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.client.MockMvcWebTestClient;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Example tests using {@link WebTestClient} to connect to {@link MockMvc} as
* the "mock" server, i.e. without running an HTTP server.
*/
@SpringBootTest
@AutoConfigureMockMvc
public class MockWebTestClientTests {
private WebTestClient client;
@BeforeEach
public void setUp(@Autowired MockMvc mockMvc) {
this.client = MockMvcWebTestClient.bindTo(mockMvc)
.baseUrl("/graphql")
.defaultHeaders(headers -> headers.setContentType(MediaType.APPLICATION_JSON))
.build();
}
@Test
void jsonPath() {
String query = "{" +
" project(slug:\"spring-framework\") {" +
" releases {" +
" version" +
" }" +
" }" +
"}";
this.client.post().bodyValue(JsonRequest.create(query))
.exchange()
.expectStatus().isOk()
.expectBody().jsonPath("$.data.project.releases[*].version")
.value((Consumer<List<String>>) versions -> {
assertThat(versions).hasSizeGreaterThan(1);
});
}
@Test
void jsonContent() {
String query = "{" +
" project(slug:\"spring-framework\") {" +
" repositoryUrl" +
" }" +
"}";
String expectedJson = "{" +
" \"data\":{" +
" \"project\":{" +
" \"repositoryUrl\":\"http://github.com/spring-projects/spring-framework\"" +
" }" +
" }" +
"}";
this.client.post().bodyValue(JsonRequest.create(query))
.exchange()
.expectStatus().isOk()
.expectBody().json(expectedJson);
}
@Test
void decodedResponse() {
String query = "{" +
" project(slug:\"spring-framework\") {" +
" releases {" +
" version" +
" }" +
" }" +
"}";
this.client.post().bodyValue(JsonRequest.create(query))
.exchange()
.expectStatus().isOk()
.expectBody(new ParameterizedTypeReference<JsonResponse<Project>>() {})
.consumeWith(exchangeResult -> {
Project project = exchangeResult.getResponseBody().getDataEntry();
assertThat(project.getReleases()).hasSizeGreaterThan(1);
});
}
}

View File

@@ -0,0 +1 @@
logging.level.org.springframework.web=DEBUG

View File

@@ -1,5 +1,6 @@
pluginManagement {
repositories {
maven { url 'https://repo.spring.io/release' }
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
gradlePluginPortal()