diff --git a/CHANGELOG.md b/CHANGELOG.md index 276d92904..5457edf88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,3 +16,4 @@ - [Optimize: change RouteArgument.buildCustom to RouteArgument.fromLabel](https://github.com/Tencent/spring-cloud-tencent/pull/750) - [Optimize: support delete config for ConfigurationProperties bean](https://github.com/Tencent/spring-cloud-tencent/pull/755) - [Bugfix: get service instances by Flux.blockLast() to resolve concurrent problem](https://github.com/Tencent/spring-cloud-tencent/pull/763) +- [Test:add sct-stater-polaris-router juint.](https://github.com/Tencent/spring-cloud-tencent/pull/767) diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java index b12a0f81a..9ab1ed66d 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java @@ -114,7 +114,6 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI return doRouter(allServers, routerContext); } - //set method to public for unit test PolarisRouterContext buildRouterContext(HttpHeaders headers) { Collection labelHeaderValues = headers.get(RouterConstant.ROUTER_LABEL_HEADER); diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplierTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplierTest.java index 67896c05a..6f1fed719 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplierTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplierTest.java @@ -40,6 +40,7 @@ import com.tencent.cloud.polaris.router.interceptor.MetadataRouterRequestInterce import com.tencent.cloud.polaris.router.interceptor.NearbyRouterRequestInterceptor; import com.tencent.cloud.polaris.router.interceptor.RuleBasedRouterRequestInterceptor; import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor; +import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.api.pojo.DefaultInstance; import com.tencent.polaris.api.pojo.DefaultServiceInstances; import com.tencent.polaris.api.pojo.Instance; @@ -55,6 +56,7 @@ import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockedStatic; @@ -63,8 +65,14 @@ import org.mockito.junit.MockitoJUnitRunner; import reactor.core.publisher.Flux; import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.loadbalancer.DefaultRequest; +import org.springframework.cloud.client.loadbalancer.RequestData; +import org.springframework.cloud.client.loadbalancer.RequestDataContext; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; +import org.springframework.http.HttpHeaders; +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @@ -228,6 +236,53 @@ public class PolarisRouterServiceInstanceListSupplierTest { } } + @Test + public void buildRouterContext() { + PolarisRouterServiceInstanceListSupplier polarisSupplier = new PolarisRouterServiceInstanceListSupplier( + delegate, routerAPI, requestInterceptors, null); + + HttpHeaders headers = new HttpHeaders(); + PolarisRouterContext context = polarisSupplier.buildRouterContext(headers); + Assert.assertNull(context); + + // mock + try (MockedStatic mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) { + mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn("mock-value"); + MetadataContextHolder.set(new MetadataContext()); + + headers = new HttpHeaders(); + headers.add(RouterConstant.ROUTER_LABEL_HEADER, "{\"k1\":\"v1\"}"); + PolarisRouterContext routerContext = polarisSupplier.buildRouterContext(headers); + assertThat(routerContext.getLabel("k1")).isEqualTo("v1"); + } + } + + @Test + public void testGet01() { + PolarisRouterServiceInstanceListSupplier polarisSupplier = new PolarisRouterServiceInstanceListSupplier( + delegate, routerAPI, requestInterceptors, null); + Assertions.assertThrows(PolarisException.class, () -> polarisSupplier.get()); + } + + @Test + public void testGet02() { + try (MockedStatic mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) { + mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())) + .thenReturn(testCallerService); + + PolarisRouterServiceInstanceListSupplier polarisSupplier = new PolarisRouterServiceInstanceListSupplier( + delegate, routerAPI, requestInterceptors, null); + + MockServerHttpRequest httpRequest = MockServerHttpRequest.get("/" + testCalleeService + "/users") + .header("k1", "v1") + .queryParam("userid", "zhangsan") + .build(); + RequestDataContext requestDataContext = new RequestDataContext(new RequestData(httpRequest), "blue"); + DefaultRequest request = new DefaultRequest(requestDataContext); + Assert.assertNull(polarisSupplier.get(request)); + } + } + private void setTransitiveMetadata() { if (initTransitiveMetadata.compareAndSet(false, true)) { // mock transitive metadata diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/FeignAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/FeignAutoConfigurationTest.java new file mode 100644 index 000000000..e8f116617 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/FeignAutoConfigurationTest.java @@ -0,0 +1,52 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.config; + +import com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.router.feign.RouterLabelFeignInterceptor; +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * test for {@link FeignAutoConfiguration}. + * @author dongyinuo + */ +public class FeignAutoConfigurationTest { + + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + MetadataAutoConfiguration.class, + RouterAutoConfiguration.class, + PolarisContextAutoConfiguration.class, + FeignAutoConfiguration.class)); + + @Test + public void routerLabelInterceptor() { + contextRunner.run(context -> { + assertThat(context).hasSingleBean(FeignAutoConfiguration.class); + assertThat(context).hasSingleBean(RouterLabelFeignInterceptor.class); + }); + } + +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/LoadBalancerConfigurationTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/LoadBalancerConfigurationTest.java new file mode 100644 index 000000000..f195294ee --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/LoadBalancerConfigurationTest.java @@ -0,0 +1,91 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.config; + + +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration; +import com.tencent.cloud.polaris.router.PolarisRouterServiceInstanceListSupplier; +import com.tencent.polaris.router.api.core.RouterAPI; +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; +import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClient; +import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties; +import org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryClient; +import org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryProperties; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * test for {@link LoadBalancerConfiguration}. + * @author dongyinuo + */ +public class LoadBalancerConfigurationTest { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); + + @Test + public void testLoadBalancerConfiguration() { + contextRunner.withConfiguration(AutoConfigurations.of( + PolarisLoadBalancerAutoConfiguration.class, + PolarisContextAutoConfiguration.class, + LoadBalancerConfiguration.class)) + .run(context -> { + assertThat(context).hasSingleBean(LoadBalancerConfiguration.class); + }); + } + + @Test + public void testPolarisReactiveSupportConfiguration() { + contextRunner.withConfiguration(AutoConfigurations.of( + LoadBalancerConfiguration.PolarisReactiveSupportConfiguration.class, + PolarisLoadBalancerAutoConfiguration.class, + PolarisContextAutoConfiguration.class)) + .withBean(SimpleReactiveDiscoveryProperties.class) + .withBean(SimpleReactiveDiscoveryClient.class) + .run(context -> { + assertThat(context).hasSingleBean(LoadBalancerConfiguration.PolarisReactiveSupportConfiguration.class); + assertThat(context).hasSingleBean(RouterAPI.class); + assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(PolarisRouterServiceInstanceListSupplier.class); + }); + } + + @Test + public void testPolarisBlockingSupportConfiguration() { + contextRunner.withConfiguration(AutoConfigurations.of( + PolarisLoadBalancerAutoConfiguration.class, + PolarisContextAutoConfiguration.class, + LoadBalancerConfiguration.PolarisBlockingSupportConfiguration.class + )) + .withBean(SimpleDiscoveryProperties.class) + .withBean(SimpleDiscoveryClient.class) + .run(context -> { + assertThat(context).hasSingleBean(LoadBalancerConfiguration.PolarisBlockingSupportConfiguration.class); + assertThat(context).hasSingleBean(DiscoveryClient.class); + assertThat(context).hasSingleBean(RouterAPI.class); + assertThat(context).hasSingleBean(PolarisRouterServiceInstanceListSupplier.class); + }); + } + +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterAutoConfigurationTests.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterAutoConfigurationTests.java new file mode 100644 index 000000000..ea46c775b --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterAutoConfigurationTests.java @@ -0,0 +1,62 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.config; + +import com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * test for {@link RouterAutoConfiguration }. + * @author dongyinuo + */ +public class RouterAutoConfigurationTests { + + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + TestRestTemplatesConfiguration.class, + MetadataAutoConfiguration.class, + RouterAutoConfiguration.class, + PolarisContextAutoConfiguration.class, + RouterAutoConfiguration.RouterLabelRestTemplateConfig.class)); + + @Test + public void testRouterLabelRestTemplateConfig() { + contextRunner.run(context -> { + assertThat(context).hasSingleBean(RouterAutoConfiguration.RouterLabelRestTemplateConfig.class); + }); + } + + @Configuration + static class TestRestTemplatesConfiguration { + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + } + +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisMetadataRouterPropertiesTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisMetadataRouterPropertiesTest.java new file mode 100644 index 000000000..d5f2fbec9 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisMetadataRouterPropertiesTest.java @@ -0,0 +1,54 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.config.properties; + +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * test for {@link PolarisMetadataRouterProperties}. + */ +public class PolarisMetadataRouterPropertiesTest { + + PolarisMetadataRouterProperties properties; + + @Before + public void setUp() { + properties = new PolarisMetadataRouterProperties(); + } + + @Test + public void isEnabled() { + assertThat(properties.isEnabled()).isEqualTo(true); + } + + @Test + public void setEnabled() { + properties.setEnabled(false); + assertThat(properties.isEnabled()).isEqualTo(false); + } + + @Test + public void testToString() { + assertThat(properties.toString()) + .isEqualTo("PolarisMetadataRouterProperties{enabled=true}"); + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java new file mode 100644 index 000000000..35c98adff --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java @@ -0,0 +1,56 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.config.properties; + + +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + + +/** + * test for {@link PolarisNearByRouterProperties}. + */ +public class PolarisNearByRouterPropertiesTest { + + PolarisNearByRouterProperties properties; + + @Before + public void setUp() { + properties = new PolarisNearByRouterProperties(); + } + + @Test + public void isEnabled() { + assertThat(properties.isEnabled()).isEqualTo(true); + } + + @Test + public void setEnabled() { + properties.setEnabled(false); + assertThat(properties.isEnabled()).isEqualTo(false); + } + + @Test + public void testToString() { + assertThat(properties.toString()) + .isEqualTo("PolarisNearByRouterProperties{enabled=true}"); + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisRuleBasedRouterPropertiesTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisRuleBasedRouterPropertiesTest.java new file mode 100644 index 000000000..6312803dd --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisRuleBasedRouterPropertiesTest.java @@ -0,0 +1,55 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.config.properties; + +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * test for {@link PolarisRuleBasedRouterProperties}. + */ +public class PolarisRuleBasedRouterPropertiesTest { + + PolarisRuleBasedRouterProperties properties; + + @Before + public void setUp() { + properties = new PolarisRuleBasedRouterProperties(); + } + + @Test + public void isEnabled() { + assertThat(properties.isEnabled()).isEqualTo(true); + } + + @Test + public void setEnabled() { + properties.setEnabled(false); + assertThat(properties.isEnabled()).isEqualTo(false); + } + + @Test + public void testToString() { + assertThat(properties.toString()) + .isEqualTo("PolarisNearByRouterProperties{enabled=true}"); + } + +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java new file mode 100644 index 000000000..b5f8912f6 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java @@ -0,0 +1,65 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.router.endpoint; + +import com.tencent.cloud.polaris.context.ServiceRuleManager; +import com.tencent.polaris.client.api.SDKContext; +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * test for {@link PolarisRouterEndpointAutoConfiguration}. + * @author dongyinuo + */ +public class PolarisRouterEndpointAutoConfigurationTests { + + private ServiceRuleManager serviceRuleManager; + + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + TestServiceRuleManagerConfiguration.class, + PolarisRouterEndpointAutoConfiguration.class)) + .withPropertyValues("endpoints.polaris-router.enabled=true"); + + @Test + public void polarisRouterEndpoint() { + contextRunner.run(context -> { + assertThat(context).hasSingleBean(PolarisRouterEndpointAutoConfiguration.class); + }); + } + + @Configuration + static class TestServiceRuleManagerConfiguration { + + @Bean + public ServiceRuleManager serviceRuleManager(SDKContext sdkContext) { + return new ServiceRuleManager(sdkContext); + } + + @Bean + public SDKContext sdkContext() { + return SDKContext.initContext(); + } + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java index 644dd6c8c..a7fec833f 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java @@ -47,6 +47,7 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @@ -73,6 +74,8 @@ public class RouterLabelFeignInterceptorTest { Collections.singletonList(routerLabelResolver), staticMetadataManager, routerRuleLabelResolver, polarisContextProperties); + assertThat(routerLabelFeignInterceptor.getOrder()).isEqualTo(0); + // mock request template RequestTemplate requestTemplate = new RequestTemplate(); String headerUidKey = "uid"; diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerRequestTests.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerRequestTests.java new file mode 100644 index 000000000..1e36d6f12 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerRequestTests.java @@ -0,0 +1,59 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.resttemplate; + +import org.junit.Test; + +import org.springframework.cloud.client.DefaultServiceInstance; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest; +import org.springframework.http.HttpRequest; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +/** + * test for {@link PolarisLoadBalancerRequest}. + * @author dongyinuo + */ +public class PolarisLoadBalancerRequestTests { + + @Test + public void test() throws Exception { + String calleeService = "calleeService"; + HttpRequest request = new RouterLabelRestTemplateInterceptorTest.MockedHttpRequest("http://" + calleeService + "/user/get"); + MockLoadBalancerRequest mockLoadBalancerRequest = new MockLoadBalancerRequest(); + PolarisLoadBalancerRequest polarisLoadBalancerRequest = new PolarisLoadBalancerRequest<>(request, mockLoadBalancerRequest); + + DefaultServiceInstance serviceInstance = new DefaultServiceInstance(); + serviceInstance.setServiceId(calleeService); + ServiceInstance apply = polarisLoadBalancerRequest.apply(serviceInstance); + assertThat(apply.getServiceId()).isEqualTo(calleeService); + assertThat(polarisLoadBalancerRequest.getRequest()).isEqualTo(request); + assertThat(polarisLoadBalancerRequest.getDelegate()).isEqualTo(mockLoadBalancerRequest); + } + + static class MockLoadBalancerRequest implements LoadBalancerRequest { + + @Override + public Object apply(ServiceInstance instance) throws Exception { + return instance; + } + } + +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java index 7226f7e4d..f21210de0 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java @@ -39,7 +39,6 @@ import com.tencent.cloud.common.util.expresstion.SpringWebExpressionLabelUtils; import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.cloud.polaris.router.RouterRuleLabelResolver; import com.tencent.cloud.polaris.router.spi.SpringWebRouterLabelResolver; -import org.assertj.core.api.Assertions; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -49,6 +48,7 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.core.Ordered; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpRequest; @@ -58,6 +58,7 @@ import org.springframework.http.client.ClientHttpResponse; import org.springframework.mock.http.client.MockClientHttpResponse; import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; @@ -145,6 +146,8 @@ public class RouterLabelRestTemplateInterceptorTest { ClientHttpResponse mockedResponse = new MockClientHttpResponse(new byte[] {}, HttpStatus.OK); when(clientHttpRequestExecution.execute(eq(request), any())).thenReturn(mockedResponse); + assertThat(routerLabelRestTemplateInterceptor.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE); + routerLabelRestTemplateInterceptor.intercept(request, null, clientHttpRequestExecution); verify(staticMetadataManager).getMergedStaticMetadata(); @@ -154,11 +157,11 @@ public class RouterLabelRestTemplateInterceptorTest { Map headers = JacksonUtils.deserialize2Map(URLDecoder.decode(Objects.requireNonNull(request.getHeaders() .get(RouterConstant.ROUTER_LABEL_HEADER)).get(0), "UTF-8")); - Assertions.assertThat("v1").isEqualTo(headers.get("k1")); - Assertions.assertThat("v22").isEqualTo(headers.get("k2")); - Assertions.assertThat("v4").isEqualTo(headers.get("k4")); - Assertions.assertThat("GET").isEqualTo(headers.get("${http.method}")); - Assertions.assertThat("/user/get").isEqualTo(headers.get("${http.uri}")); + assertThat("v1").isEqualTo(headers.get("k1")); + assertThat("v22").isEqualTo(headers.get("k2")); + assertThat("v4").isEqualTo(headers.get("k4")); + assertThat("GET").isEqualTo(headers.get("${http.method}")); + assertThat("/user/get").isEqualTo(headers.get("${http.uri}")); String encodedLabelsContent; try { encodedLabelsContent = URLEncoder.encode(JacksonUtils.serialize2Json(routerLabels), UTF_8); @@ -166,7 +169,7 @@ public class RouterLabelRestTemplateInterceptorTest { catch (UnsupportedEncodingException e) { throw new RuntimeException("unsupported charset exception " + UTF_8); } - Assertions.assertThat(mockedResponse.getHeaders().get(RouterConstant.ROUTER_LABEL_HEADER).get(0)) + assertThat(mockedResponse.getHeaders().get(RouterConstant.ROUTER_LABEL_HEADER).get(0)) .isEqualTo(encodedLabelsContent); } diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilterTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilterTest.java index 4c7ee91fc..18a812cb2 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilterTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilterTest.java @@ -19,6 +19,7 @@ package com.tencent.cloud.polaris.router.scg; import java.io.UnsupportedEncodingException; +import java.net.URI; import java.net.URLDecoder; import java.util.HashMap; import java.util.List; @@ -45,18 +46,28 @@ import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import reactor.core.publisher.Mono; import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties; import org.springframework.cloud.gateway.config.GatewayLoadBalancerProperties; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier; +import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer; +import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer; import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; +import org.springframework.cloud.loadbalancer.support.SimpleObjectProvider; import org.springframework.http.HttpHeaders; import org.springframework.mock.http.server.reactive.MockServerHttpRequest; import org.springframework.mock.web.server.MockServerWebExchange; import org.springframework.util.CollectionUtils; +import org.springframework.web.server.ServerWebExchange; import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR; /** * Test for ${@link PolarisReactiveLoadBalancerClientFilter}. @@ -92,7 +103,7 @@ public class PolarisReactiveLoadBalancerClientFilterTest { mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())) .thenReturn(callerService); - MetadataContext metadataContext = Mockito.mock(MetadataContext.class); + MetadataContext metadataContext = mock(MetadataContext.class); // mock transitive metadata Map transitiveLabels = new HashMap<>(); @@ -146,4 +157,40 @@ public class PolarisReactiveLoadBalancerClientFilterTest { Assert.assertEquals("v1", routerLabels.get("t1")); Assert.assertEquals("v2", routerLabels.get("t2")); } + + @Test + public void testFilter01() throws Exception { + PolarisReactiveLoadBalancerClientFilter filter = new PolarisReactiveLoadBalancerClientFilter(loadBalancerClientFactory, + gatewayLoadBalancerProperties, loadBalancerProperties, staticMetadataManager, routerRuleLabelResolver, + Lists.newArrayList(routerLabelResolver), polarisContextProperties); + + MockServerHttpRequest request = MockServerHttpRequest.get("/" + calleeService + "/users").build(); + MockServerWebExchange exchange = new MockServerWebExchange.Builder(request).build(); + + // mock no lb + EmptyGatewayFilterChain chain = new EmptyGatewayFilterChain(); + Mono ret = filter.filter(exchange, chain); + Assert.assertEquals(ret, Mono.empty()); + + // mock with lb + exchange = new MockServerWebExchange.Builder(request).build(); + exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, new URI("https://" + calleeService + ":8091")); + exchange.getAttributes().put(GATEWAY_SCHEME_PREFIX_ATTR, "lb"); + + NoopServiceInstanceListSupplier serviceInstanceListSupplier = new NoopServiceInstanceListSupplier(); + RoundRobinLoadBalancer roundRobinLoadBalancer = new RoundRobinLoadBalancer(new SimpleObjectProvider<>(serviceInstanceListSupplier), calleeService); + + when(loadBalancerClientFactory.getInstance(calleeService, ReactorServiceInstanceLoadBalancer.class)).thenReturn(roundRobinLoadBalancer); + filter.filter(exchange, chain); + + } + + static class EmptyGatewayFilterChain implements GatewayFilterChain { + + @Override + public Mono filter(ServerWebExchange exchange) { + return Mono.empty(); + } + } + }