mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-22 22:19:03 +00:00
Option to supply client builder in HttpServiceGroupConfigurer
Closes gh-35707
This commit is contained in:
+50
-20
@@ -20,8 +20,10 @@ import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestClient;
|
||||
@@ -31,8 +33,14 @@ import org.springframework.web.service.registry.echo.EchoA;
|
||||
import org.springframework.web.service.registry.echo.EchoB;
|
||||
import org.springframework.web.service.registry.greeting.GreetingA;
|
||||
import org.springframework.web.service.registry.greeting.GreetingB;
|
||||
import org.springframework.web.testfixture.http.client.MockClientHttpRequest;
|
||||
import org.springframework.web.testfixture.http.client.MockClientHttpResponse;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.web.service.registry.HttpServiceGroup.ClientType.REST_CLIENT;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link HttpServiceProxyRegistryFactoryBean}.
|
||||
@@ -42,43 +50,65 @@ public class HttpServiceProxyRegistryFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
void twoGroups() {
|
||||
|
||||
GroupsMetadata groupsMetadata = new GroupsMetadata();
|
||||
|
||||
String echoName = "echo";
|
||||
String greetingName = "greeting";
|
||||
GroupsMetadata groupsMetadata = new GroupsMetadata();
|
||||
|
||||
groupsMetadata.getOrCreateGroup(echoName, HttpServiceGroup.ClientType.REST_CLIENT)
|
||||
.httpServiceTypeNames().addAll(List.of(EchoA.class.getName(), EchoB.class.getName()));
|
||||
List<String> echoServices = List.of(EchoA.class.getName(), EchoB.class.getName());
|
||||
groupsMetadata.getOrCreateGroup(echoName, REST_CLIENT).httpServiceTypeNames().addAll(echoServices);
|
||||
|
||||
groupsMetadata.getOrCreateGroup(greetingName, HttpServiceGroup.ClientType.REST_CLIENT)
|
||||
.httpServiceTypeNames().addAll(List.of(GreetingA.class.getName(), GreetingB.class.getName()));
|
||||
List<String> greetingServices = List.of(GreetingA.class.getName(), GreetingB.class.getName());
|
||||
groupsMetadata.getOrCreateGroup(greetingName, REST_CLIENT).httpServiceTypeNames().addAll(greetingServices);
|
||||
|
||||
Predicate<HttpServiceGroup> echoFilter = group -> group.name().equals(echoName);
|
||||
Predicate<HttpServiceGroup> greetingFilter = group -> group.name().equals(greetingName);
|
||||
TestConfigurer groupConfigurer = new TestConfigurer(List.of(echoFilter, greetingFilter));
|
||||
|
||||
TestConfigurer testConfigurer = new TestConfigurer(List.of(echoFilter, greetingFilter));
|
||||
|
||||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
||||
applicationContext.registerBean(TestConfigurer.class, () -> testConfigurer);
|
||||
applicationContext.refresh();
|
||||
|
||||
HttpServiceProxyRegistryFactoryBean factoryBean = new HttpServiceProxyRegistryFactoryBean(groupsMetadata);
|
||||
factoryBean.setApplicationContext(applicationContext);
|
||||
factoryBean.setBeanClassLoader(getClass().getClassLoader());
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
HttpServiceProxyRegistry registry = factoryBean.getObject();
|
||||
HttpServiceProxyRegistry registry = initProxyRegistry(groupConfigurer, groupsMetadata);
|
||||
assertThat(registry.getGroupNames()).containsExactlyInAnyOrder(echoName, greetingName);
|
||||
assertThat(registry.getClientTypesInGroup(echoName)).containsExactlyInAnyOrder(EchoA.class, EchoB.class);
|
||||
assertThat(registry.getClientTypesInGroup(greetingName)).containsExactlyInAnyOrder(GreetingA.class, GreetingB.class);
|
||||
|
||||
assertThat(testConfigurer.invocations)
|
||||
assertThat(groupConfigurer.invocations)
|
||||
.containsKeys(echoFilter, greetingFilter)
|
||||
.containsEntry(echoFilter, List.of(echoName))
|
||||
.containsEntry(greetingFilter, List.of(greetingName));
|
||||
}
|
||||
|
||||
@Test
|
||||
void initializeClientBuilder() throws Exception {
|
||||
GroupsMetadata groupsMetadata = new GroupsMetadata();
|
||||
groupsMetadata.getOrCreateGroup("echo", REST_CLIENT).httpServiceTypeNames().add(EchoA.class.getName());
|
||||
|
||||
ClientHttpRequestFactory requestFactory = Mockito.mock(ClientHttpRequestFactory.class);
|
||||
MockClientHttpRequest request = new MockClientHttpRequest();
|
||||
request.setResponse(new MockClientHttpResponse());
|
||||
when(requestFactory.createRequest(any(), any())).thenReturn(request);
|
||||
|
||||
RestClient.Builder clientBuilder = RestClient.builder().baseUrl("/").requestFactory(requestFactory);
|
||||
RestClientHttpServiceGroupConfigurer groupConfigurer = groups -> groups.forEachClient(group -> clientBuilder);
|
||||
|
||||
HttpServiceProxyRegistry registry = initProxyRegistry(groupConfigurer, groupsMetadata);
|
||||
registry.getClient(EchoA.class).handle("foo");
|
||||
|
||||
verify(requestFactory);
|
||||
}
|
||||
|
||||
private HttpServiceProxyRegistry initProxyRegistry(
|
||||
RestClientHttpServiceGroupConfigurer groupConfigurer, GroupsMetadata groupsMetadata) {
|
||||
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.registerBean(RestClientHttpServiceGroupConfigurer.class, () -> groupConfigurer);
|
||||
context.refresh();
|
||||
|
||||
HttpServiceProxyRegistryFactoryBean factoryBean = new HttpServiceProxyRegistryFactoryBean(groupsMetadata);
|
||||
factoryBean.setApplicationContext(context);
|
||||
factoryBean.setBeanClassLoader(getClass().getClassLoader());
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
return factoryBean.getObject();
|
||||
}
|
||||
|
||||
|
||||
private static class TestConfigurer implements RestClientHttpServiceGroupConfigurer {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user