Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaMaciaszek committed Oct 14, 2024
1 parent 84babc1 commit 941747d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestClient;

Expand All @@ -40,17 +39,17 @@
properties = "spring.cloud.loadbalancer.retry.enabled=false")
public class LoadBalancedRestClientIntegrationTests {

private final RestClient client;
private final RestClient.Builder restClientBuilder;

@Autowired
ApplicationContext context;

public LoadBalancedRestClientIntegrationTests(@Autowired RestClient.Builder clientBuilder) {
this.client = clientBuilder.build();
public LoadBalancedRestClientIntegrationTests(@Autowired RestClient.Builder restClientBuilder) {
this.restClientBuilder = restClientBuilder;
}

@Test
void shouldBuildLoadBalancedRestClientInConstructor() {
RestClient client = restClientBuilder.build();

// Interceptors are not visible in RestClient
assertThatThrownBy(() -> client.get().uri("http://test-service").retrieve())
.hasMessage("LoadBalancerInterceptor invoked.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package org.springframework.cloud.client.loadbalancer;

import java.net.URI;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

Expand All @@ -39,9 +41,6 @@ public class LoadBalancedRestTemplateBuilderIntegrationTests {

private final RestTemplateBuilder restTemplateBuilder;

@Autowired
ApplicationContext context;

public LoadBalancedRestTemplateBuilderIntegrationTests(@Autowired RestTemplateBuilder restTemplateBuilder) {
this.restTemplateBuilder = restTemplateBuilder;
}
Expand All @@ -51,7 +50,12 @@ void shouldBuildLoadBalancedRestTemplate() {
RestTemplate restTemplate = restTemplateBuilder.build();

assertThat(restTemplate.getInterceptors()).hasSize(1);
assertThat(restTemplate.getInterceptors().get(0)).isInstanceOf(BlockingLoadBalancerInterceptor.class);
assertThat(restTemplate.getInterceptors()
.get(0)).isInstanceOf(DeferringLoadBalancerInterceptor.class);
assertThat(((DeferringLoadBalancerInterceptor) restTemplate.getInterceptors()
.get(0))
.getLoadBalancerInterceptorProvider()
.getObject()).isInstanceOf(BlockingLoadBalancerInterceptor.class);
}

@SpringBootConfiguration
Expand All @@ -63,6 +67,38 @@ RestTemplateBuilder restTemplateBuilder() {
return new RestTemplateBuilder();
}

@Bean
LoadBalancerClient testLoadBalancerClient() {
return new LoadBalancerClient() {
@Override
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) {
throw new UnsupportedOperationException("LoadBalancerInterceptor invoked.");
}

@Override
public <T> T execute(String serviceId, ServiceInstance serviceInstance,
LoadBalancerRequest<T> request) {
throw new UnsupportedOperationException("LoadBalancerInterceptor invoked.");
}

@Override
public URI reconstructURI(ServiceInstance instance, URI original) {
throw new UnsupportedOperationException("LoadBalancerInterceptor invoked.");
}

@Override
public ServiceInstance choose(String serviceId) {
throw new UnsupportedOperationException("LoadBalancerInterceptor invoked.");
}

@Override
public <T> ServiceInstance choose(String serviceId, Request<T> request) {
throw new UnsupportedOperationException("LoadBalancerInterceptor invoked.");
}
};
}

}

}

}

0 comments on commit 941747d

Please sign in to comment.