split integration test into different test scenarios, one for each step in the webclient process

This commit is contained in:
Gerardo Roza 2021-01-27 12:28:41 -03:00
parent c477f22f27
commit 047dfdef25

View File

@ -33,8 +33,8 @@ import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec; import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec; import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec; import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
import org.springframework.web.reactive.function.client.WebClient.ResponseSpec; import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
import org.springframework.web.reactive.function.client.WebClient.UriSpec;
import org.springframework.web.reactive.function.client.WebClientRequestException; import org.springframework.web.reactive.function.client.WebClientRequestException;
import com.baeldung.web.reactive.client.Foo; import com.baeldung.web.reactive.client.Foo;
@ -53,9 +53,13 @@ public class WebClientIntegrationTest {
@LocalServerPort @LocalServerPort
private int port; private int port;
private static final String BODY_VALUE = "bodyValue";
private static final ParameterizedTypeReference<Map<String, String>> MAP_RESPONSE_REF = new ParameterizedTypeReference<Map<String, String>>() {
};
@Test @Test
public void givenDifferentScenarios_whenRequestsSent_thenObtainExpectedResponses() { public void givenDifferentWebClientCreationMethods_whenUsed_thenObtainExpectedResponse() {
// WebClient // WebClient creation
WebClient client1 = WebClient.create(); WebClient client1 = WebClient.create();
WebClient client2 = WebClient.create("http://localhost:" + port); WebClient client2 = WebClient.create("http://localhost:" + port);
WebClient client3 = WebClient.builder() WebClient client3 = WebClient.builder()
@ -65,58 +69,149 @@ public class WebClientIntegrationTest {
.defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080")) .defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
.build(); .build();
// request specification // response assertions
UriSpec<RequestBodySpec> uriSpecPost1 = client1.method(HttpMethod.POST); StepVerifier.create(retrieveResponse(client1.post()
UriSpec<RequestBodySpec> uriSpecPost2 = client2.post(); .uri("http://localhost:" + port + "/resource")))
UriSpec<?> requestGet = client3.get(); .expectNext("processed-bodyValue")
.verifyComplete();
StepVerifier.create(retrieveResponse(client2))
.expectNext("processed-bodyValue")
.verifyComplete();
StepVerifier.create(retrieveResponse(client3))
.expectNext("processed-bodyValue")
.verifyComplete();
// assert response without specifying URI
StepVerifier.create(retrieveResponse(client1))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused"))
.verify();
}
@Test
public void givenDifferentMethodSpecifications_whenUsed_thenObtainExpectedResponse() {
// request specification
RequestBodyUriSpec uriSpecPost1 = createDefaultClient().method(HttpMethod.POST);
RequestBodyUriSpec uriSpecPost2 = createDefaultClient().post();
RequestHeadersUriSpec<?> requestGet = createDefaultClient().get();
// response assertions
StepVerifier.create(retrieveResponse(uriSpecPost1))
.expectNext("processed-bodyValue")
.verifyComplete();
StepVerifier.create(retrieveResponse(uriSpecPost2))
.expectNext("processed-bodyValue")
.verifyComplete();
StepVerifier.create(retrieveGetResponse(requestGet))
.expectNextMatches(nextMap -> nextMap.get("field")
.equals("value"))
.verifyComplete();
}
@Test
public void givenDifferentUriSpecifications_whenUsed_thenObtainExpectedResponse() {
// uri specification // uri specification
RequestBodySpec bodySpecPost = uriSpecPost1.uri("http://localhost:" + port + "/resource"); RequestBodySpec bodySpecUsingString = createDefaultPostRequest().uri("/resource");
RequestBodySpec bodySpecPostMultipart = uriSpecPost2.uri(uriBuilder -> uriBuilder.pathSegment("resource-multipart") RequestBodySpec bodySpecUsingUriBuilder = createDefaultPostRequest().uri(uriBuilder -> uriBuilder.pathSegment("resource")
.build()); .build());
RequestBodySpec fooBodySpecPost = createDefaultPostRequest().uri("/resource-foo"); RequestBodySpec bodySpecusingURI = createDefaultPostRequest().uri(URI.create("http://localhost:" + port + "/resource"));
RequestBodySpec bodySpecOverridenBaseUri = client3.post() RequestBodySpec bodySpecOverridenBaseUri = createDefaultPostRequest().uri(URI.create("/resource"));
RequestBodySpec bodySpecOverridenBaseUri2 = WebClient.builder()
.baseUrl("http://localhost:" + port)
.build()
.post()
.uri(URI.create("/resource")); .uri(URI.create("/resource"));
// request body specifications // response assertions
String bodyValue = "bodyValue"; StepVerifier.create(retrieveResponse(bodySpecUsingString))
RequestHeadersSpec<?> headersSpecPost1 = bodySpecPost.body(BodyInserters.fromPublisher(Mono.just(bodyValue), String.class)); .expectNext("processed-bodyValue")
RequestHeadersSpec<?> headersSpecPost2 = createDefaultPostResourceRequest().body(BodyInserters.fromValue(bodyValue)); .verifyComplete();
RequestHeadersSpec<?> headersSpecPost3 = createDefaultPostResourceRequest().bodyValue(bodyValue); StepVerifier.create(retrieveResponse(bodySpecUsingUriBuilder))
RequestHeadersSpec<?> headersSpecFooPost = fooBodySpecPost.body(Mono.just(new Foo("fooName")), Foo.class); .expectNext("processed-bodyValue")
RequestHeadersSpec<?> headersSpecGet = requestGet.uri("/resource"); .verifyComplete();
StepVerifier.create(retrieveResponse(bodySpecusingURI))
.expectNext("processed-bodyValue")
.verifyComplete();
// assert sending request overriding base URI
StepVerifier.create(retrieveResponse(bodySpecOverridenBaseUri))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused"))
.verify();
StepVerifier.create(retrieveResponse(bodySpecOverridenBaseUri2))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused"))
.verify();
}
// request body specifications - using inserters @Test
public void givenDifferentBodySpecifications_whenUsed_thenObtainExpectedResponse() {
// request body specifications
RequestHeadersSpec<?> headersSpecPost1 = createDefaultPostResourceRequest().body(BodyInserters.fromPublisher(Mono.just(BODY_VALUE), String.class));
RequestHeadersSpec<?> headersSpecPost2 = createDefaultPostResourceRequest().body(BodyInserters.fromValue(BODY_VALUE));
RequestHeadersSpec<?> headersSpecPost3 = createDefaultPostResourceRequest().bodyValue(BODY_VALUE);
RequestHeadersSpec<?> headersSpecFooPost = createDefaultPostRequest().uri("/resource-foo")
.body(Mono.just(new Foo("fooName")), Foo.class);
BodyInserter<Object, ReactiveHttpOutputMessage> inserterPlainObject = BodyInserters.fromValue(new Object());
RequestHeadersSpec<?> headersSpecPlainObject = createDefaultPostResourceRequest().body(inserterPlainObject);
// request body specifications - using other inserter method (multipart request)
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("key1", "multipartValue1"); map.add("key1", "multipartValue1");
map.add("key2", "multipartValue2"); map.add("key2", "multipartValue2");
BodyInserter<MultiValueMap<String, Object>, ClientHttpRequest> inserterMultipart = BodyInserters.fromMultipartData(map); BodyInserter<MultiValueMap<String, Object>, ClientHttpRequest> inserterMultipart = BodyInserters.fromMultipartData(map);
BodyInserter<Object, ReactiveHttpOutputMessage> inserterObject = BodyInserters.fromValue(new Object()); RequestHeadersSpec<?> headersSpecInserterMultipart = createDefaultPostRequest().uri("/resource-multipart")
BodyInserter<String, ReactiveHttpOutputMessage> inserterString = BodyInserters.fromValue(bodyValue); .body(inserterMultipart);
RequestHeadersSpec<?> headersSpecInserterMultipart = bodySpecPostMultipart.body(inserterMultipart); // response assertions
RequestHeadersSpec<?> headersSpecInserterObject = createDefaultPostResourceRequest().body(inserterObject); StepVerifier.create(retrieveResponse(headersSpecPost1))
RequestHeadersSpec<?> headersSpecInserterString = createDefaultPostResourceRequest().body(inserterString); .expectNext("processed-bodyValue")
.verifyComplete();
StepVerifier.create(retrieveResponse(headersSpecPost2))
.expectNext("processed-bodyValue")
.verifyComplete();
StepVerifier.create(retrieveResponse(headersSpecPost3))
.expectNext("processed-bodyValue")
.verifyComplete();
StepVerifier.create(retrieveResponse(headersSpecFooPost))
.expectNext("processedFoo-fooName")
.verifyComplete();
StepVerifier.create(retrieveResponse(headersSpecInserterMultipart))
.expectNext("processed-multipartValue1-multipartValue2")
.verifyComplete();
// assert error plain `new Object()` as request body
StepVerifier.create(retrieveResponse(headersSpecPlainObject))
.expectError(CodecException.class)
.verify();
// assert response for request with no body
Mono<Map<String, String>> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(responseHandler -> {
assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
return responseHandler.bodyToMono(MAP_RESPONSE_REF);
});
StepVerifier.create(responsePostWithNoBody)
.expectNextMatches(nextMap -> nextMap.get("error")
.equals("Bad Request"))
.verifyComplete();
}
@Test
public void givenPostSpecifications_whenHeadersAdded_thenObtainExpectedResponse() {
// request header specification // request header specification
RequestHeadersSpec<?> headersSpecInserterStringWithHeaders = headersSpecInserterString.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) RequestHeadersSpec<?> headersSpecInserterStringWithHeaders = createDefaultPostResourceRequestResponse().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
.acceptCharset(StandardCharsets.UTF_8) .acceptCharset(StandardCharsets.UTF_8)
.ifNoneMatch("*") .ifNoneMatch("*")
.ifModifiedSince(ZonedDateTime.now()); .ifModifiedSince(ZonedDateTime.now());
// request // response assertions
ResponseSpec responseSpecPostString = headersSpecInserterStringWithHeaders.retrieve(); StepVerifier.create(retrieveResponse(headersSpecInserterStringWithHeaders))
.expectNext("processed-bodyValue")
.verifyComplete();
}
@Test
public void givenDifferentResponseSpecifications_whenUsed_thenObtainExpectedResponse() {
ResponseSpec responseSpecPostString = createDefaultPostResourceRequestResponse().retrieve();
Mono<String> responsePostString = responseSpecPostString.bodyToMono(String.class); Mono<String> responsePostString = responseSpecPostString.bodyToMono(String.class);
Mono<String> responsePostMultipart = headersSpecInserterMultipart.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) Mono<String> responsePostString2 = createDefaultPostResourceRequestResponse().exchangeToMono(response -> {
.retrieve()
.bodyToMono(String.class);
Mono<String> responsePostWithBody1 = headersSpecPost1.retrieve()
.bodyToMono(String.class);
Mono<String> responsePostWithBody2 = headersSpecPost2.retrieve()
.bodyToMono(String.class);
Mono<String> responsePostWithBody3 = headersSpecPost3.exchangeToMono(response -> {
if (response.statusCode() if (response.statusCode()
.equals(HttpStatus.OK)) { .equals(HttpStatus.OK)) {
return response.bodyToMono(String.class); return response.bodyToMono(String.class);
@ -128,55 +223,37 @@ public class WebClientIntegrationTest {
.flatMap(Mono::error); .flatMap(Mono::error);
} }
}); });
Mono<String> responsePostFoo = headersSpecFooPost.retrieve() Mono<String> responsePostNoBody = createDefaultPostResourceRequest().exchangeToMono(response -> {
.bodyToMono(String.class); if (response.statusCode()
ParameterizedTypeReference<Map<String, String>> ref = new ParameterizedTypeReference<Map<String, String>>() { .equals(HttpStatus.OK)) {
}; return response.bodyToMono(String.class);
Mono<Map<String, String>> responseGet = headersSpecGet.retrieve() } else if (response.statusCode()
.bodyToMono(ref); .is4xxClientError()) {
Mono<Map<String, String>> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(responseHandler -> { return Mono.just("Error response");
assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST); } else {
return responseHandler.bodyToMono(ref); return response.createException()
.flatMap(Mono::error);
}
}); });
Mono<String> responsePostOverridenBaseUri = bodySpecOverridenBaseUri.retrieve() Mono<Map<String, String>> responseGet = createDefaultClient().get()
.bodyToMono(String.class); .uri("/resource")
.retrieve()
.bodyToMono(MAP_RESPONSE_REF);
// response assertions // response assertions
StepVerifier.create(responsePostString) StepVerifier.create(responsePostString)
.expectNext("processed-bodyValue") .expectNext("processed-bodyValue")
.verifyComplete(); .verifyComplete();
StepVerifier.create(responsePostMultipart) StepVerifier.create(responsePostString2)
.expectNext("processed-multipartValue1-multipartValue2")
.verifyComplete();
StepVerifier.create(responsePostWithBody1)
.expectNext("processed-bodyValue") .expectNext("processed-bodyValue")
.verifyComplete(); .verifyComplete();
StepVerifier.create(responsePostWithBody2) StepVerifier.create(responsePostNoBody)
.expectNext("processed-bodyValue") .expectNext("Error response")
.verifyComplete();
StepVerifier.create(responsePostWithBody3)
.expectNext("processed-bodyValue")
.verifyComplete(); .verifyComplete();
StepVerifier.create(responseGet) StepVerifier.create(responseGet)
.expectNextMatches(nextMap -> nextMap.get("field") .expectNextMatches(nextMap -> nextMap.get("field")
.equals("value")) .equals("value"))
.verifyComplete(); .verifyComplete();
StepVerifier.create(responsePostFoo)
.expectNext("processedFoo-fooName")
.verifyComplete();
StepVerifier.create(responsePostWithNoBody)
.expectNextMatches(nextMap -> nextMap.get("error")
.equals("Bad Request"))
.verifyComplete();
// assert sending request overriding base uri
StepVerifier.create(responsePostOverridenBaseUri)
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused"))
.verify();
// assert error plain `new Object()` as request body
StepVerifier.create(headersSpecInserterObject.exchangeToMono(response -> response.bodyToMono(String.class)))
.expectError(CodecException.class)
.verify();
} }
@Test @Test
@ -202,12 +279,53 @@ public class WebClientIntegrationTest {
.verify(); .verify();
} }
// helper methods to create default instances
private WebClient createDefaultClient() {
return WebClient.create("http://localhost:" + port);
}
private RequestBodyUriSpec createDefaultPostRequest() { private RequestBodyUriSpec createDefaultPostRequest() {
return WebClient.create("http://localhost:" + port) return createDefaultClient().post();
.post();
} }
private RequestBodySpec createDefaultPostResourceRequest() { private RequestBodySpec createDefaultPostResourceRequest() {
return createDefaultPostRequest().uri("/resource"); return createDefaultPostRequest().uri("/resource");
} }
private RequestHeadersSpec<?> createDefaultPostResourceRequestResponse() {
return createDefaultPostResourceRequest().bodyValue(BODY_VALUE);
}
// helper methods to retrieve a response based on different steps of the process (specs)
private Mono<String> retrieveResponse(WebClient client) {
return client.post()
.uri("/resource")
.bodyValue(BODY_VALUE)
.retrieve()
.bodyToMono(String.class);
}
private Mono<String> retrieveResponse(RequestBodyUriSpec spec) {
return spec.uri("/resource")
.bodyValue(BODY_VALUE)
.retrieve()
.bodyToMono(String.class);
}
private Mono<Map<String, String>> retrieveGetResponse(RequestHeadersUriSpec<?> spec) {
return spec.uri("/resource")
.retrieve()
.bodyToMono(MAP_RESPONSE_REF);
}
private Mono<String> retrieveResponse(RequestBodySpec spec) {
return spec.bodyValue(BODY_VALUE)
.retrieve()
.bodyToMono(String.class);
}
private Mono<String> retrieveResponse(RequestHeadersSpec<?> spec) {
return spec.retrieve()
.bodyToMono(String.class);
}
} }