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.boot.autoconfigure.web.WebClientAutoConfiguration.RestTemplateConfiguration;
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(RestTemplateConfiguration.class);
		applicationContext.register(ServerPropertiesAutoConfiguration.class);
		applicationContext.register(AdminServerWebConfiguration.class);
		applicationContext.register(DiscoveryClientConfiguration.class);

		applicationContext.refresh();
		this.context = applicationContext;
	}
}

@@ -223,16 +223,44 @@
 t.Test;%0A
+import org.mockito.Mockito;%0A
 import o
@@ -327,32 +327,32 @@
 oConfiguration;%0A
-
 import org.sprin
@@ -581,16 +581,83 @@
 stance;%0A
+import org.springframework.cloud.client.discovery.DiscoveryClient;%0A
 import o
@@ -745,156 +745,8 @@
 on;%0A
-import org.springframework.cloud.commons.util.UtilAutoConfiguration;%0Aimport org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration;%0A
 impo
@@ -937,24 +937,68 @@
 onContext;%0A%0A
+import com.netflix.discovery.EurekaClient;%0A%0A
 import de.co
@@ -1758,37 +1758,8 @@
 oad(
-UtilAutoConfiguration.class, 
 Eure
@@ -1766,33 +1766,22 @@
 kaClient
-Auto
 Config
-uration
 .class);
@@ -2190,17 +2190,16 @@
 ));%0A%09%7D%0A%0A
-%0A
 %09@Config
@@ -2529,24 +2529,24 @@
 instance) %7B%0A
-
 %09%09%09return nu
@@ -2557,16 +2557,281 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09@Configuration%0A%09protected static class EurekaClientConfig %7B%0A%09%09@Bean%0A%09%09public EurekaClient eurekaClient() %7B%0A%09%09%09return Mockito.mock(EurekaClient.class);%0A%09%09%7D%0A%0A%09%09@Bean%0A%09%09public DiscoveryClient discoveryClient() %7B%0A%09%09%09return Mockito.mock(DiscoveryClient.class);%0A%09%09%7D%0A%09%7D%0A%0A
 %09private

Fix test