Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaMaciaszek committed Nov 6, 2024
1 parent fbdd065 commit 0a1f4bb
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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());
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -100,33 +92,7 @@ public RestTemplateTransportClientFactories forceRestTemplateTransportClientFact
@SpringBootConfiguration
@EnableAutoConfiguration
@EnableEurekaServer
public static class RestTemplateTestEurekaServer {

}

private static class RestTemplateBuilderObjectProvider implements ObjectProvider<RestTemplateBuilder> {

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 {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 0a1f4bb

Please sign in to comment.