From 0a1f4bb13c19ed600b27a0309709ef23cd2e894b Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Wed, 6 Nov 2024 19:13:15 +0100 Subject: [PATCH] Fix tests. --- .../netflix/eureka/EurekaClientTests.java | 8 +- .../eureka/RestClientEurekaClientTests.java | 104 ++++++++++++++++++ .../eureka/RestTemplateEurekaClientTests.java | 40 +------ ...coveryClientOptionalArgsConfiguration.java | 6 +- 4 files changed, 113 insertions(+), 45 deletions(-) create mode 100644 spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/RestClientEurekaClientTests.java diff --git a/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientTests.java b/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientTests.java index 732edac4d..48ba2da0c 100644 --- a/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientTests.java +++ b/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientTests.java @@ -22,7 +22,6 @@ import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.cloud.netflix.eureka.config.DiscoveryClientOptionalArgsConfiguration; import org.springframework.cloud.netflix.eureka.http.DefaultEurekaClientHttpRequestFactorySupplier; import org.springframework.cloud.netflix.eureka.http.RestTemplateDiscoveryClientOptionalArgs; import org.springframework.cloud.netflix.eureka.http.RestTemplateTransportClientFactories; @@ -33,7 +32,7 @@ public class EurekaClientTests extends BaseCertTests { - private static final Log log = LogFactory.getLog(EurekaClientTests.class); + private static final Log LOG = LogFactory.getLog(EurekaClientTests.class); static EurekaServerRunner server; @@ -44,7 +43,7 @@ public static void setupAll() { server = startEurekaServer(EurekaClientTests.TestEurekaServer.class); service = startService(server, EurekaClientTests.TestApp.class); assertThat(service.discoveryClientOptionalArgs()).isInstanceOf(RestTemplateDiscoveryClientOptionalArgs.class); - log.info("Successfully asserted that Jersey will be used"); + LOG.info("Successfully asserted that Jersey will be used"); waitForRegistration(() -> new EurekaClientTests().createEurekaClient()); } @@ -59,9 +58,8 @@ public static class TestApp { @Bean public RestTemplateTransportClientFactories forceRestTemplateTransportClientFactories( - DiscoveryClientOptionalArgsConfiguration configuration, RestTemplateDiscoveryClientOptionalArgs discoveryClientOptionalArgs) { - return configuration.restTemplateTransportClientFactories(discoveryClientOptionalArgs); + return new RestTemplateTransportClientFactories(discoveryClientOptionalArgs); } @Bean diff --git a/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/RestClientEurekaClientTests.java b/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/RestClientEurekaClientTests.java new file mode 100644 index 000000000..fe1e805e5 --- /dev/null +++ b/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/RestClientEurekaClientTests.java @@ -0,0 +1,104 @@ +/* + * Copyright 2018-2024 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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 org.springframework.cloud.netflix.eureka; + +import java.io.IOException; +import java.security.GeneralSecurityException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; + +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.configuration.TlsProperties; +import org.springframework.cloud.netflix.eureka.http.EurekaClientHttpRequestFactorySupplier; +import org.springframework.cloud.netflix.eureka.http.RestClientDiscoveryClientOptionalArgs; +import org.springframework.cloud.netflix.eureka.http.RestClientTransportClientFactories; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestClient; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.netflix.eureka.config.DiscoveryClientOptionalArgsConfiguration.setupTLS; + +/** + * @author Olga Maciaszek-Sharma + */ +public class RestClientEurekaClientTests extends BaseCertTests { + + private static final Log LOG = LogFactory.getLog(RestClientEurekaClientTests.class); + + private static EurekaServerRunner server; + + private static EurekaClientRunner service; + + @BeforeAll + public static void setupAll() { + server = startEurekaServer(TestEurekaServer.class); + service = startService(server, TestApp.class); + // Will use RestClient + assertThat(service.discoveryClientOptionalArgs()).isInstanceOf(RestClientDiscoveryClientOptionalArgs.class); + LOG.info("Successfully asserted that RestClient will be used"); + waitForRegistration(() -> new RestClientEurekaClientTests().createEurekaClient()); + } + + @AfterAll + public static void tearDownAll() { + stopService(service); + stopEurekaServer(server); + } + + @Override + EurekaClientRunner createEurekaClient() { + return new EurekaClientRunner(TestApp.class, server); + } + + @SpringBootConfiguration + @EnableAutoConfiguration + public static class TestApp { + + // Want to force reusing exactly the same bean as on production without excluding + // jersey from the classpath + @Bean + public RestClientDiscoveryClientOptionalArgs forceRestClientDiscoveryClientOptionalArgs( + TlsProperties tlsProperties, + EurekaClientHttpRequestFactorySupplier eurekaClientHttpRequestFactorySupplier) + throws GeneralSecurityException, IOException { + RestClientDiscoveryClientOptionalArgs result = new RestClientDiscoveryClientOptionalArgs( + eurekaClientHttpRequestFactorySupplier, RestClient::builder); + setupTLS(result, tlsProperties); + return result; + } + + @Bean + public RestClientTransportClientFactories forceRestClientTransportClientFactories( + RestClientDiscoveryClientOptionalArgs discoveryClientOptionalArgs) { + return new RestClientTransportClientFactories(discoveryClientOptionalArgs); + } + + } + + @SpringBootConfiguration + @EnableAutoConfiguration + @EnableEurekaServer + public static class TestEurekaServer { + + } + +} diff --git a/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/RestTemplateEurekaClientTests.java b/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/RestTemplateEurekaClientTests.java index e23178c4b..4df7f15ae 100644 --- a/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/RestTemplateEurekaClientTests.java +++ b/spring-cloud-netflix-eureka-client-tls-tests/src/test/java/org/springframework/cloud/netflix/eureka/RestTemplateEurekaClientTests.java @@ -24,13 +24,10 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.configuration.TlsProperties; -import org.springframework.cloud.netflix.eureka.config.DiscoveryClientOptionalArgsConfiguration; import org.springframework.cloud.netflix.eureka.http.EurekaClientHttpRequestFactorySupplier; import org.springframework.cloud.netflix.eureka.http.RestTemplateDiscoveryClientOptionalArgs; import org.springframework.cloud.netflix.eureka.http.RestTemplateTransportClientFactories; @@ -50,7 +47,7 @@ public class RestTemplateEurekaClientTests extends BaseCertTests { @BeforeAll public static void setupAll() { - server = startEurekaServer(RestTemplateEurekaClientTests.RestTemplateTestEurekaServer.class); + server = startEurekaServer(TestEurekaServer.class); service = startService(server, RestTemplateEurekaClientTests.RestTemplateTestApp.class); // Will use RestTemplate assertThat(service.discoveryClientOptionalArgs()).isInstanceOf(RestTemplateDiscoveryClientOptionalArgs.class); @@ -73,22 +70,17 @@ EurekaClientRunner createEurekaClient() { @EnableAutoConfiguration public static class RestTemplateTestApp { - // Want to force reusing exactly the same bean as on production without excluding - // jersey from the classpath @Bean public RestTemplateDiscoveryClientOptionalArgs forceRestTemplateDiscoveryClientOptionalArgs( TlsProperties tlsProperties, EurekaClientHttpRequestFactorySupplier eurekaClientHttpRequestFactorySupplier) throws GeneralSecurityException, IOException { RestTemplateDiscoveryClientOptionalArgs result = new RestTemplateDiscoveryClientOptionalArgs( - eurekaClientHttpRequestFactorySupplier, new RestTemplateBuilderObjectProvider() - ); + eurekaClientHttpRequestFactorySupplier, RestTemplateBuilder::new); setupTLS(result, tlsProperties); return result; } - // Want to force reusing exactly the same bean as on production without excluding - // jersey from the classpath @Bean public RestTemplateTransportClientFactories forceRestTemplateTransportClientFactories( RestTemplateDiscoveryClientOptionalArgs discoveryClientOptionalArgs) { @@ -100,33 +92,7 @@ public RestTemplateTransportClientFactories forceRestTemplateTransportClientFact @SpringBootConfiguration @EnableAutoConfiguration @EnableEurekaServer - public static class RestTemplateTestEurekaServer { - - } - - private static class RestTemplateBuilderObjectProvider implements ObjectProvider { - - private final RestTemplateBuilder builder = new RestTemplateBuilder(); - - @Override - public RestTemplateBuilder getObject(Object... args) throws BeansException { - return builder; - } - - @Override - public RestTemplateBuilder getIfAvailable() throws BeansException { - return builder; - } - - @Override - public RestTemplateBuilder getIfUnique() throws BeansException { - return builder; - } - - @Override - public RestTemplateBuilder getObject() throws BeansException { - return builder; - } + public static class TestEurekaServer { } diff --git a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/config/DiscoveryClientOptionalArgsConfiguration.java b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/config/DiscoveryClientOptionalArgsConfiguration.java index 1ede0fdd7..d3f1fffeb 100644 --- a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/config/DiscoveryClientOptionalArgsConfiguration.java +++ b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/config/DiscoveryClientOptionalArgsConfiguration.java @@ -61,7 +61,7 @@ * @author Wonchul Heo */ @Configuration(proxyBeanMethods = false) -@EnableConfigurationProperties({RestTemplateTimeoutProperties.class, RestClientTimeoutProperties.class}) +@EnableConfigurationProperties({ RestTemplateTimeoutProperties.class, RestClientTimeoutProperties.class }) public class DiscoveryClientOptionalArgsConfiguration { protected static final Log logger = LogFactory.getLog(DiscoveryClientOptionalArgsConfiguration.class); @@ -93,7 +93,7 @@ public static void setupTLS(AbstractDiscoveryClientOptionalArgs args, TlsProp static class RestTemplateConfiguration { @Bean - @ConditionalOnMissingBean(value = {AbstractDiscoveryClientOptionalArgs.class}, + @ConditionalOnMissingBean(value = { AbstractDiscoveryClientOptionalArgs.class }, search = SearchStrategy.CURRENT) public RestTemplateDiscoveryClientOptionalArgs restTemplateDiscoveryClientOptionalArgs( TlsProperties tlsProperties, @@ -110,7 +110,7 @@ public RestTemplateDiscoveryClientOptionalArgs restTemplateDiscoveryClientOption @Bean @Conditional(OnRestTemplatePresentAndEnabledCondition.class) - @ConditionalOnMissingBean(value = {TransportClientFactories.class}, search = SearchStrategy.CURRENT) + @ConditionalOnMissingBean(value = { TransportClientFactories.class }, search = SearchStrategy.CURRENT) public RestTemplateTransportClientFactories restTemplateTransportClientFactories( RestTemplateDiscoveryClientOptionalArgs optionalArgs) { return new RestTemplateTransportClientFactories(optionalArgs);