|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.service.registry; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.function.Predicate; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 25 | +import org.springframework.util.LinkedMultiValueMap; |
| 26 | +import org.springframework.util.MultiValueMap; |
| 27 | +import org.springframework.web.client.RestClient; |
| 28 | +import org.springframework.web.client.support.RestClientHttpServiceGroupConfigurer; |
| 29 | +import org.springframework.web.service.registry.echo.EchoA; |
| 30 | +import org.springframework.web.service.registry.echo.EchoB; |
| 31 | +import org.springframework.web.service.registry.greeting.GreetingA; |
| 32 | +import org.springframework.web.service.registry.greeting.GreetingB; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * Unit tests for {@link HttpServiceProxyRegistryFactoryBean}. |
| 38 | + * @author Rossen Stoyanchev |
| 39 | + */ |
| 40 | +public class HttpServiceProxyRegistryFactoryBeanTests { |
| 41 | + |
| 42 | + @Test |
| 43 | + void twoGroups() { |
| 44 | + |
| 45 | + GroupsMetadata groupsMetadata = new GroupsMetadata(); |
| 46 | + |
| 47 | + String echoName = "echo"; |
| 48 | + String greetingName = "greeting"; |
| 49 | + |
| 50 | + groupsMetadata.getOrCreateGroup(echoName, HttpServiceGroup.ClientType.REST_CLIENT) |
| 51 | + .httpServiceTypeNames().addAll(List.of(EchoA.class.getName(), EchoB.class.getName())); |
| 52 | + |
| 53 | + groupsMetadata.getOrCreateGroup(greetingName, HttpServiceGroup.ClientType.REST_CLIENT) |
| 54 | + .httpServiceTypeNames().addAll(List.of(GreetingA.class.getName(), GreetingB.class.getName())); |
| 55 | + |
| 56 | + Predicate<HttpServiceGroup> echoFilter = group -> group.name().equals(echoName); |
| 57 | + Predicate<HttpServiceGroup> greetingFilter = group -> group.name().equals(greetingName); |
| 58 | + |
| 59 | + TestConfigurer testConfigurer = new TestConfigurer(List.of(echoFilter, greetingFilter)); |
| 60 | + |
| 61 | + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); |
| 62 | + applicationContext.registerBean(TestConfigurer.class, () -> testConfigurer); |
| 63 | + applicationContext.refresh(); |
| 64 | + |
| 65 | + HttpServiceProxyRegistryFactoryBean factoryBean = new HttpServiceProxyRegistryFactoryBean(groupsMetadata); |
| 66 | + factoryBean.setApplicationContext(applicationContext); |
| 67 | + factoryBean.setBeanClassLoader(getClass().getClassLoader()); |
| 68 | + factoryBean.afterPropertiesSet(); |
| 69 | + |
| 70 | + HttpServiceProxyRegistry registry = factoryBean.getObject(); |
| 71 | + assertThat(registry.getGroupNames()).containsExactlyInAnyOrder(echoName, greetingName); |
| 72 | + assertThat(registry.getClientTypesInGroup(echoName)).containsExactlyInAnyOrder(EchoA.class, EchoB.class); |
| 73 | + assertThat(registry.getClientTypesInGroup(greetingName)).containsExactlyInAnyOrder(GreetingA.class, GreetingB.class); |
| 74 | + |
| 75 | + assertThat(testConfigurer.invocations) |
| 76 | + .containsKeys(echoFilter, greetingFilter) |
| 77 | + .containsEntry(echoFilter, List.of(echoName)) |
| 78 | + .containsEntry(greetingFilter, List.of(greetingName)); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + private static class TestConfigurer implements RestClientHttpServiceGroupConfigurer { |
| 83 | + |
| 84 | + private final List<Predicate<HttpServiceGroup>> filters; |
| 85 | + |
| 86 | + private final MultiValueMap<Predicate<HttpServiceGroup>, String> invocations = new LinkedMultiValueMap<>(); |
| 87 | + |
| 88 | + TestConfigurer(List<Predicate<HttpServiceGroup>> filters) { |
| 89 | + this.filters = filters; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void configureGroups(Groups<RestClient.Builder> groups) { |
| 94 | + filters.forEach(filter -> groups.filter(filter) |
| 95 | + .configureClient((group, builder) -> invocations.add(filter, group.name()))); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments