Refactor HttpServiceProxyFactory for use as a bean
Closes gh-28505
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.web.service.invoker;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -36,7 +37,15 @@ class CookieValueArgumentResolverTests {
|
||||
|
||||
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
|
||||
|
||||
private final Service service = HttpServiceProxyFactory.builder(this.client).build().createClient(Service.class);
|
||||
private Service service;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
proxyFactory.afterPropertiesSet();
|
||||
this.service = proxyFactory.createClient(Service.class);
|
||||
}
|
||||
|
||||
|
||||
// Base class functionality should be tested in NamedValueArgumentResolverTests.
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.service.invoker;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -34,7 +35,15 @@ public class HttpMethodArgumentResolverTests {
|
||||
|
||||
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
|
||||
|
||||
private final Service service = HttpServiceProxyFactory.builder(this.client).build().createClient(Service.class);
|
||||
private Service service;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
proxyFactory.afterPropertiesSet();
|
||||
this.service = proxyFactory.createClient(Service.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.Optional;
|
||||
import io.reactivex.rxjava3.core.Completable;
|
||||
import io.reactivex.rxjava3.core.Flowable;
|
||||
import io.reactivex.rxjava3.core.Single;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -56,9 +57,16 @@ public class HttpServiceMethodTests {
|
||||
private static final ParameterizedTypeReference<String> BODY_TYPE = new ParameterizedTypeReference<>() {};
|
||||
|
||||
|
||||
private final TestHttpClientAdapter clientAdapter = new TestHttpClientAdapter();
|
||||
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
|
||||
|
||||
private final HttpServiceProxyFactory proxyFactory = HttpServiceProxyFactory.builder(this.clientAdapter).build();
|
||||
private HttpServiceProxyFactory proxyFactory;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
this.proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
this.proxyFactory.afterPropertiesSet();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@@ -150,7 +158,7 @@ public class HttpServiceMethodTests {
|
||||
|
||||
service.performGet();
|
||||
|
||||
HttpRequestValues requestValues = this.clientAdapter.getRequestValues();
|
||||
HttpRequestValues requestValues = this.client.getRequestValues();
|
||||
assertThat(requestValues.getHttpMethod()).isEqualTo(HttpMethod.GET);
|
||||
assertThat(requestValues.getUriTemplate()).isEqualTo("");
|
||||
assertThat(requestValues.getHeaders().getContentType()).isNull();
|
||||
@@ -158,7 +166,7 @@ public class HttpServiceMethodTests {
|
||||
|
||||
service.performPost();
|
||||
|
||||
requestValues = this.clientAdapter.getRequestValues();
|
||||
requestValues = this.client.getRequestValues();
|
||||
assertThat(requestValues.getHttpMethod()).isEqualTo(HttpMethod.POST);
|
||||
assertThat(requestValues.getUriTemplate()).isEqualTo("/url");
|
||||
assertThat(requestValues.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
|
||||
@@ -166,17 +174,17 @@ public class HttpServiceMethodTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void typeAndMethodAnnotatedService() {
|
||||
void typeAndMethodAnnotatedService() throws Exception {
|
||||
|
||||
HttpServiceProxyFactory proxyFactory = HttpServiceProxyFactory.builder(this.clientAdapter)
|
||||
.setEmbeddedValueResolver(value -> (value.equals("${baseUrl}") ? "/base" : value))
|
||||
.build();
|
||||
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
proxyFactory.setEmbeddedValueResolver(value -> (value.equals("${baseUrl}") ? "/base" : value));
|
||||
proxyFactory.afterPropertiesSet();
|
||||
|
||||
MethodLevelAnnotatedService service = proxyFactory.createClient(TypeAndMethodLevelAnnotatedService.class);
|
||||
|
||||
service.performGet();
|
||||
|
||||
HttpRequestValues requestValues = this.clientAdapter.getRequestValues();
|
||||
HttpRequestValues requestValues = this.client.getRequestValues();
|
||||
assertThat(requestValues.getHttpMethod()).isEqualTo(HttpMethod.GET);
|
||||
assertThat(requestValues.getUriTemplate()).isEqualTo("/base");
|
||||
assertThat(requestValues.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_CBOR);
|
||||
@@ -184,7 +192,7 @@ public class HttpServiceMethodTests {
|
||||
|
||||
service.performPost();
|
||||
|
||||
requestValues = this.clientAdapter.getRequestValues();
|
||||
requestValues = this.client.getRequestValues();
|
||||
assertThat(requestValues.getHttpMethod()).isEqualTo(HttpMethod.POST);
|
||||
assertThat(requestValues.getUriTemplate()).isEqualTo("/base/url");
|
||||
assertThat(requestValues.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
|
||||
@@ -192,8 +200,8 @@ public class HttpServiceMethodTests {
|
||||
}
|
||||
|
||||
private void verifyClientInvocation(String methodName, @Nullable ParameterizedTypeReference<?> expectedBodyType) {
|
||||
assertThat((this.clientAdapter.getInvokedMethodName())).isEqualTo(methodName);
|
||||
assertThat(this.clientAdapter.getBodyType()).isEqualTo(expectedBodyType);
|
||||
assertThat((this.client.getInvokedMethodName())).isEqualTo(methodName);
|
||||
assertThat(this.client.getBodyType()).isEqualTo(expectedBodyType);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.groovy.util.Maps;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
@@ -53,11 +54,17 @@ class NamedValueArgumentResolverTests {
|
||||
|
||||
private final TestNamedValueArgumentResolver argumentResolver = new TestNamedValueArgumentResolver();
|
||||
|
||||
private final Service service =
|
||||
HttpServiceProxyFactory.builder(this.client)
|
||||
.addCustomResolver(this.argumentResolver)
|
||||
.build()
|
||||
.createClient(Service.class);
|
||||
private Service service;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
proxyFactory.addCustomArgumentResolver(this.argumentResolver);
|
||||
proxyFactory.afterPropertiesSet();
|
||||
|
||||
this.service = proxyFactory.createClient(Service.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.service.invoker;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -35,7 +36,15 @@ class PathVariableArgumentResolverTests {
|
||||
|
||||
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
|
||||
|
||||
private final Service service = HttpServiceProxyFactory.builder(this.client).build().createClient(Service.class);
|
||||
private Service service;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
proxyFactory.afterPropertiesSet();
|
||||
this.service = proxyFactory.createClient(Service.class);
|
||||
}
|
||||
|
||||
|
||||
// Base class functionality should be tested in NamedValueArgumentResolverTests.
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.service.invoker;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -34,7 +35,15 @@ class RequestAttributeArgumentResolverTests {
|
||||
|
||||
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
|
||||
|
||||
private final Service service = HttpServiceProxyFactory.builder(this.client).build().createClient(Service.class);
|
||||
private Service service;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
proxyFactory.afterPropertiesSet();
|
||||
this.service = proxyFactory.createClient(Service.class);
|
||||
}
|
||||
|
||||
|
||||
// Base class functionality should be tested in NamedValueArgumentResolverTests.
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.service.invoker;
|
||||
|
||||
import io.reactivex.rxjava3.core.Completable;
|
||||
import io.reactivex.rxjava3.core.Single;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -39,7 +40,15 @@ public class RequestBodyArgumentResolverTests {
|
||||
|
||||
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
|
||||
|
||||
private final Service service = HttpServiceProxyFactory.builder(this.client).build().createClient(Service.class);
|
||||
private Service service;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
proxyFactory.afterPropertiesSet();
|
||||
this.service = proxyFactory.createClient(Service.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.service.invoker;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -37,7 +38,15 @@ class RequestHeaderArgumentResolverTests {
|
||||
|
||||
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
|
||||
|
||||
private final Service service = HttpServiceProxyFactory.builder(this.client).build().createClient(Service.class);
|
||||
private Service service;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
proxyFactory.afterPropertiesSet();
|
||||
this.service = proxyFactory.createClient(Service.class);
|
||||
}
|
||||
|
||||
|
||||
// Base class functionality should be tested in NamedValueArgumentResolverTests.
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.service.invoker;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
@@ -39,7 +40,15 @@ public class RequestParamArgumentResolverTests {
|
||||
|
||||
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
|
||||
|
||||
private final Service service = HttpServiceProxyFactory.builder(this.client).build().createClient(Service.class);
|
||||
private Service service;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
proxyFactory.afterPropertiesSet();
|
||||
this.service = proxyFactory.createClient(Service.class);
|
||||
}
|
||||
|
||||
|
||||
// Base class functionality should be tested in NamedValueArgumentResolverTests.
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.service.invoker;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.web.service.annotation.GetExchange;
|
||||
@@ -34,7 +35,15 @@ public class UrlArgumentResolverTests {
|
||||
|
||||
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
|
||||
|
||||
private final Service service = HttpServiceProxyFactory.builder(this.client).build().createClient(Service.class);
|
||||
private Service service;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(this.client);
|
||||
proxyFactory.afterPropertiesSet();
|
||||
this.service = proxyFactory.createClient(Service.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user