Commit e1eeb038 by Johannes Edmeier

Fix for not respecting custom ServiceInstanceConverter

fixes #209
parent 7e18972b
......@@ -17,18 +17,18 @@ package de.codecentric.boot.admin.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient.EurekaServiceInstance;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.netflix.discovery.EurekaClient;
import de.codecentric.boot.admin.discovery.ApplicationDiscoveryListener;
import de.codecentric.boot.admin.discovery.DefaultServiceInstanceConverter;
import de.codecentric.boot.admin.discovery.EurekaServiceInstanceConverter;
......@@ -58,23 +58,19 @@ public class DiscoveryClientConfiguration {
}
@Configuration
@ConditionalOnClass({ EurekaServiceInstance.class })
@AutoConfigureBefore(DefaultConverterConfiguration.class)
@ConditionalOnBean(EurekaClient.class)
public static class EurekaConverterConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnMissingBean({ ServiceInstanceConverter.class })
public EurekaServiceInstanceConverter serviceInstanceConverter() {
return new EurekaServiceInstanceConverter();
}
}
@Configuration
@Bean
@ConditionalOnMissingBean({ ServiceInstanceConverter.class })
@ConfigurationProperties(prefix = "spring.boot.admin.discovery.converter")
public static class DefaultConverterConfiguration {
@Bean
@ConditionalOnMissingBean
public DefaultServiceInstanceConverter serviceInstanceConverter() {
return new DefaultServiceInstanceConverter();
}
public DefaultServiceInstanceConverter serviceInstanceConverter() {
return new DefaultServiceInstanceConverter();
}
}
package de.codecentric.boot.admin.config;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import de.codecentric.boot.admin.discovery.DefaultServiceInstanceConverter;
import de.codecentric.boot.admin.discovery.EurekaServiceInstanceConverter;
import de.codecentric.boot.admin.discovery.ServiceInstanceConverter;
import de.codecentric.boot.admin.model.Application;
public class DiscoveryClientConfigurationTest {
private AnnotationConfigWebApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void defaultServiceInstanceConverter() {
load(NoopDiscoveryClientAutoConfiguration.class);
assertThat(context.getBean(ServiceInstanceConverter.class),
is(instanceOf(DefaultServiceInstanceConverter.class)));
}
@Test
public void eurekaServiceInstanceConverter() {
load(UtilAutoConfiguration.class, EurekaClientAutoConfiguration.class);
assertThat(context.getBean(ServiceInstanceConverter.class),
is(instanceOf(EurekaServiceInstanceConverter.class)));
}
@Test
public void customServiceInstanceConverter() {
load(NoopDiscoveryClientAutoConfiguration.class,
TestCustomServiceInstanceConverterConfig.class);
assertThat(context.getBean(ServiceInstanceConverter.class),
is(instanceOf(CustomServiceInstanceConverter.class)));
}
@Configuration
static class TestCustomServiceInstanceConverterConfig {
@Bean
public CustomServiceInstanceConverter converter() {
return new CustomServiceInstanceConverter();
}
}
static class CustomServiceInstanceConverter implements ServiceInstanceConverter {
@Override
public Application convert(ServiceInstance instance) {
return null;
}
}
private void load(Class<?>... configs) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
for (Class<?> config : configs) {
applicationContext.register(config);
}
applicationContext.register(PropertyPlaceholderAutoConfiguration.class);
applicationContext.register(ServerPropertiesAutoConfiguration.class);
applicationContext.register(AdminServerWebConfiguration.class);
applicationContext.register(DiscoveryClientConfiguration.class);
applicationContext.refresh();
this.context = applicationContext;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment