Skip to content

Commit

Permalink
Remove AsyncRestTemplate and related types
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Oct 8, 2021
1 parent 932291b commit e3b48c2
Show file tree
Hide file tree
Showing 40 changed files with 37 additions and 4,813 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* 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.
Expand All @@ -16,7 +16,6 @@

package org.springframework.test.web.client;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
Expand All @@ -39,18 +38,10 @@
/**
* A {@link ClientHttpRequestFactory} for requests executed via {@link MockMvc}.
*
* <p>As of 5.0 this class also implements
* {@link org.springframework.http.client.AsyncClientHttpRequestFactory
* AsyncClientHttpRequestFactory}. However note that
* {@link org.springframework.web.client.AsyncRestTemplate} and related classes
* have been deprecated at the same time.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
@SuppressWarnings("deprecation")
public class MockMvcClientHttpRequestFactory
implements ClientHttpRequestFactory, org.springframework.http.client.AsyncClientHttpRequestFactory {
public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory {

private final MockMvc mockMvc;

Expand All @@ -65,22 +56,12 @@ public MockMvcClientHttpRequestFactory(MockMvc mockMvc) {
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) {
return new MockClientHttpRequest(httpMethod, uri) {
@Override
public ClientHttpResponse executeInternal() throws IOException {
public ClientHttpResponse executeInternal() {
return getClientHttpResponse(httpMethod, uri, getHeaders(), getBodyAsBytes());
}
};
}

@Override
public org.springframework.http.client.AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod method) {
return new org.springframework.mock.http.client.MockAsyncClientHttpRequest(method, uri) {
@Override
protected ClientHttpResponse executeInternal() throws IOException {
return getClientHttpResponse(method, uri, getHeaders(), getBodyAsBytes());
}
};
}

private ClientHttpResponse getClientHttpResponse(
HttpMethod httpMethod, URI uri, HttpHeaders requestHeaders, byte[] requestBody) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.support.RestGatewaySupport;
Expand Down Expand Up @@ -64,7 +64,6 @@
* @author Rossen Stoyanchev
* @since 3.2
*/
@SuppressWarnings("deprecation")
public final class MockRestServiceServer {

private final RequestExpectationManager expectationManager;
Expand Down Expand Up @@ -147,18 +146,6 @@ public static MockRestServiceServerBuilder bindTo(RestTemplate restTemplate) {
return new DefaultBuilder(restTemplate);
}

/**
* Return a builder for a {@code MockRestServiceServer} that should be used
* to reply to the given {@code AsyncRestTemplate}.
* @since 4.3
* @deprecated see deprecation notice on
* {@link org.springframework.web.client.AsyncRestTemplate} itself
*/
@Deprecated
public static MockRestServiceServerBuilder bindTo(org.springframework.web.client.AsyncRestTemplate asyncRestTemplate) {
return new DefaultBuilder(asyncRestTemplate);
}

/**
* Return a builder for a {@code MockRestServiceServer} that should be used
* to reply to the given {@code RestGatewaySupport}.
Expand All @@ -179,18 +166,6 @@ public static MockRestServiceServer createServer(RestTemplate restTemplate) {
return bindTo(restTemplate).build();
}

/**
* A shortcut for {@code bindTo(asyncRestTemplate).build()}.
* @param asyncRestTemplate the AsyncRestTemplate to set up for mock testing
* @return the created mock server
* @deprecated see deprecation notice on
* {@link org.springframework.web.client.AsyncRestTemplate} itself
*/
@Deprecated
public static MockRestServiceServer createServer(org.springframework.web.client.AsyncRestTemplate asyncRestTemplate) {
return bindTo(asyncRestTemplate).build();
}

/**
* A shortcut for {@code bindTo(restGateway).build()}.
* @param restGateway the REST gateway to set up for mock testing
Expand Down Expand Up @@ -226,8 +201,8 @@ public interface MockRestServiceServerBuilder {

/**
* Build the {@code MockRestServiceServer} and set up the underlying
* {@code RestTemplate} or {@code AsyncRestTemplate} with a
* {@link ClientHttpRequestFactory} that creates mock requests.
* {@code RestTemplate} with a {@link ClientHttpRequestFactory} that
* creates mock requests.
*/
MockRestServiceServer build();

Expand All @@ -241,12 +216,8 @@ public interface MockRestServiceServerBuilder {

private static class DefaultBuilder implements MockRestServiceServerBuilder {

@Nullable
private final RestTemplate restTemplate;

@Nullable
private final org.springframework.web.client.AsyncRestTemplate asyncRestTemplate;

private boolean ignoreExpectOrder;

private boolean bufferContent;
Expand All @@ -255,13 +226,6 @@ private static class DefaultBuilder implements MockRestServiceServerBuilder {
public DefaultBuilder(RestTemplate restTemplate) {
Assert.notNull(restTemplate, "RestTemplate must not be null");
this.restTemplate = restTemplate;
this.asyncRestTemplate = null;
}

public DefaultBuilder(org.springframework.web.client.AsyncRestTemplate asyncRestTemplate) {
Assert.notNull(asyncRestTemplate, "AsyncRestTemplate must not be null");
this.restTemplate = null;
this.asyncRestTemplate = asyncRestTemplate;
}

@Override
Expand Down Expand Up @@ -290,16 +254,11 @@ public MockRestServiceServer build() {
public MockRestServiceServer build(RequestExpectationManager manager) {
MockRestServiceServer server = new MockRestServiceServer(manager);
MockClientHttpRequestFactory factory = server.new MockClientHttpRequestFactory();
if (this.restTemplate != null) {
if (this.bufferContent) {
this.restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
}
else {
this.restTemplate.setRequestFactory(factory);
}
if (this.bufferContent) {
this.restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
}
if (this.asyncRestTemplate != null) {
this.asyncRestTemplate.setAsyncRequestFactory(factory);
else {
this.restTemplate.setRequestFactory(factory);
}
return server;
}
Expand All @@ -310,28 +269,18 @@ public MockRestServiceServer build(RequestExpectationManager manager) {
* Mock ClientHttpRequestFactory that creates requests by iterating
* over the list of expected {@link DefaultRequestExpectation}'s.
*/
private class MockClientHttpRequestFactory implements ClientHttpRequestFactory,
org.springframework.http.client.AsyncClientHttpRequestFactory {
private class MockClientHttpRequestFactory implements ClientHttpRequestFactory {

@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) {
return createRequestInternal(uri, httpMethod);
}

@Override
public org.springframework.http.client.AsyncClientHttpRequest createAsyncRequest(
URI uri, HttpMethod httpMethod) {

return createRequestInternal(uri, httpMethod);
}

private org.springframework.mock.http.client.MockAsyncClientHttpRequest createRequestInternal(
URI uri, HttpMethod httpMethod) {

private MockClientHttpRequest createRequestInternal(URI uri, HttpMethod httpMethod) {
Assert.notNull(uri, "'uri' must not be null");
Assert.notNull(httpMethod, "'httpMethod' must not be null");

return new org.springframework.mock.http.client.MockAsyncClientHttpRequest(httpMethod, uri) {
return new MockClientHttpRequest(httpMethod, uri) {

@Override
protected ClientHttpResponse executeInternal() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* 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.
Expand All @@ -23,10 +23,10 @@

import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.mock.http.client.MockClientHttpRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.test.web.client.ExpectedCount.once;
import static org.springframework.test.web.client.ExpectedCount.twice;
Expand All @@ -44,15 +44,15 @@ public class DefaultRequestExpectationTests {
@Test
public void match() throws Exception {
RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo"));
expectation.match(createRequest(GET, "/foo"));
expectation.match(createRequest());
}

@Test
public void matchWithFailedExpectation() throws Exception {
public void matchWithFailedExpectation() {
RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo"));
expectation.andExpect(method(POST));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
expectation.match(createRequest(GET, "/foo")))
expectation.match(createRequest()))
.withMessageContaining("Unexpected HttpMethod expected:<POST> but was:<GET>");
}

Expand Down Expand Up @@ -81,10 +81,9 @@ public void isSatisfied() {
}


@SuppressWarnings("deprecation")
private ClientHttpRequest createRequest(HttpMethod method, String url) {
private ClientHttpRequest createRequest() {
try {
return new org.springframework.mock.http.client.MockAsyncClientHttpRequest(method, new URI(url));
return new MockClientHttpRequest(HttpMethod.GET, new URI("/foo"));
}
catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.mock.http.client.MockClientHttpRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand All @@ -42,7 +43,7 @@
*/
public class UnorderedRequestExpectationManagerTests {

private UnorderedRequestExpectationManager manager = new UnorderedRequestExpectationManager();
private final UnorderedRequestExpectationManager manager = new UnorderedRequestExpectationManager();


@Test
Expand All @@ -57,7 +58,7 @@ public void unexpectedRequest() throws Exception {
}

@Test
public void zeroExpectedRequests() throws Exception {
public void zeroExpectedRequests() {
this.manager.verify();
}

Expand Down Expand Up @@ -108,19 +109,18 @@ public void repeatedRequestsTooFew() throws Exception {
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/foo"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.manager.verify())
.withMessageContaining("3 request(s) executed:\n" +
"GET /bar\n" +
"GET /foo\n" +
"GET /foo\n");
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(this.manager::verify)
.withMessageContaining("3 request(s) executed:\n" +
"GET /bar\n" +
"GET /foo\n" +
"GET /foo\n");
}


@SuppressWarnings("deprecation")
private ClientHttpRequest createRequest(HttpMethod method, String url) {
try {
return new org.springframework.mock.http.client.MockAsyncClientHttpRequest(method, new URI(url));
return new MockClientHttpRequest(method, new URI(url));
}
catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
Expand Down
Loading

0 comments on commit e3b48c2

Please sign in to comment.