Stubbing service discovery out completely

without this change if you have a DiscoveryClient bean it's used for delegation. For tests with Stub Runner actually we don't want to delegate. There is an option however to turn on this delegation

fixes #82
This commit is contained in:
Marcin Grzejszczak
2016-09-23 10:40:57 +02:00
parent 01f4ad76be
commit 788b35aedb
4 changed files with 200 additions and 12 deletions

View File

@@ -115,7 +115,7 @@ class StubRunnerDiscoveryClient implements DiscoveryClient {
private List<ServiceInstance> getInstancesFromDelegate(String serviceId) {
try {
return this.delegate.getInstances(serviceId);
return new ArrayList<>(this.delegate.getInstances(serviceId));
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Failed to fetch instances from delegate", e);
@@ -134,15 +134,17 @@ class StubRunnerDiscoveryClient implements DiscoveryClient {
@Override
public List<String> getServices() {
List<String> list = new ArrayList<>();
List<String> services = getServicesFromDelegate();
RunningStubs runningStubs = this.stubFinder.findAllRunningStubs();
services.addAll(runningStubs.getAllServicesNames());
return services;
list.addAll(services);
list.addAll(runningStubs.getAllServicesNames());
return list;
}
private List<String> getServicesFromDelegate() {
try {
return this.delegate.getServices();
return new ArrayList<>(this.delegate.getServices());
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Failed to fetch services from delegate", e);

View File

@@ -19,7 +19,6 @@ package org.springframework.cloud.contract.stubrunner.spring.cloud;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
@@ -41,9 +40,10 @@ import org.springframework.context.annotation.Primary;
public class StubRunnerSpringCloudAutoConfiguration {
@Bean
@ConditionalOnBean(DiscoveryClient.class)
@Primary
@ConditionalOnBean(DiscoveryClient.class)
@ConditionalOnStubbedDiscoveryEnabled
@ConditionalOnProperty(value = "stubrunner.cloud.delegate.enabled", havingValue = "true")
public DiscoveryClient stubRunnerDiscoveryClientWrapper(DiscoveryClient discoveryClient,
StubFinder stubFinder,
StubMapperProperties stubMapperProperties,
@@ -53,9 +53,9 @@ public class StubRunnerSpringCloudAutoConfiguration {
@Bean
@Primary
@ConditionalOnMissingBean(DiscoveryClient.class)
@ConditionalOnStubbedDiscoveryEnabled
public DiscoveryClient stubRunnerDiscoveryClient(StubFinder stubFinder,
@ConditionalOnProperty(value = "stubrunner.cloud.delegate.enabled", havingValue = "false", matchIfMissing = true)
public DiscoveryClient noOpStubRunnerDiscoveryClient(StubFinder stubFinder,
StubMapperProperties stubMapperProperties,
@Value("${spring.application.name:unknown}") String springAppName) {
return new StubRunnerDiscoveryClient(stubFinder, stubMapperProperties, springAppName);