Skip to content

Commit

Permalink
Add tests for the client & server autoconfig and fix issue with cycli…
Browse files Browse the repository at this point in the history
…c dependency
  • Loading branch information
Johannes Stelzer committed Mar 18, 2015
1 parent d370971 commit 66173f5
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 31 deletions.
4 changes: 2 additions & 2 deletions spring-boot-admin-server/README.md
Expand Up @@ -39,7 +39,7 @@ One note: If you omit the Spring Boot Admin Client in you Client Applications yo
### Further configuration
Since the DiscoveryClient doesn't tell the management.context-path you can suffix the url for all discovered clients by setting ``spring.boot.admin.discovery.management.context-path``.

Explictly disable DiscoveryClient support by setting ``spring.boot.admin.discover.enable=false``.
Explictly disable DiscoveryClient support by setting ``spring.boot.admin.discover.enabled=false``.

## Hazelcast Support
Spring Boot Admin Server supports cluster replication with Hazelcast.
Expand Down Expand Up @@ -93,6 +93,6 @@ Write xml-config hazelcast-config.xml:
```

### Further configuration
Disable Hazelcast support by setting ``spring.boot.admin.hazelcast.enable=false``.
Disable Hazelcast support by setting ``spring.boot.admin.hazelcast.enabled=false``.

To alter the name of the Hazelcast-Map set ``spring.boot.admin.hazelcast.map= my-own-map-name``.
Expand Up @@ -46,6 +46,7 @@
import org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter;
import org.springframework.cloud.netflix.zuul.web.ZuulController;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
Expand All @@ -65,6 +66,8 @@

import de.codecentric.boot.admin.controller.RegistryController;
import de.codecentric.boot.admin.discovery.ApplicationDiscoveryListener;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
import de.codecentric.boot.admin.event.ClientApplicationUnregisteredEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.ApplicationIdGenerator;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
Expand All @@ -76,7 +79,7 @@
import de.codecentric.boot.admin.zuul.ApplicationRouteRefreshListener;

@Configuration
public class WebappConfig extends WebMvcConfigurerAdapter {
public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter {

/**
* Add JSON MessageConverter to send JSON objects to web clients.
Expand Down Expand Up @@ -213,7 +216,7 @@ public ZuulFilterInitializer zuulFilterInitializer() {
}

@Bean
ApplicationRouteRefreshListener applicationRouteRefreshListener() {
public ApplicationRouteRefreshListener applicationRouteRefreshListener() {
return new ApplicationRouteRefreshListener(routeLocator(), zuulHandlerMapping(routeLocator()));
}

Expand All @@ -235,9 +238,9 @@ public RoutesEndpoint zuulEndpoint() {

@Configuration
@ConditionalOnClass({ Hazelcast.class })
@ConditionalOnProperty(prefix = "spring.boot.admin.hazelcast", name = "enable", matchIfMissing = true)
@ConditionalOnProperty(prefix = "spring.boot.admin.hazelcast", name = "enabled", matchIfMissing = true)
@AutoConfigureBefore(SimpleStoreConfig.class)
public static class HazelcastStoreConfig {
public static class HazelcastStoreConfiguration {

@Value("${spring.boot.admin.hazelcast.map:spring-boot-admin-application-store}")
private String hazelcastMapName;
Expand Down Expand Up @@ -270,44 +273,36 @@ public EntryListener<String, Application> entryListener() {

private static class ApplicationEntryListener implements EntryListener<String, Application> {
@Autowired
private ZuulHandlerMapping zuulHandlerMapping;

@Autowired
private ApplicationRouteLocator routeLocator;

private void reset() {
routeLocator.resetRoutes();
zuulHandlerMapping.registerHandlers();
}
ApplicationContext context;

@Override
public void entryAdded(EntryEvent<String, Application> event) {
reset();
context.publishEvent(new ClientApplicationRegisteredEvent(this,event.getValue()));
}

@Override
public void entryRemoved(EntryEvent<String, Application> event) {
reset();
context.publishEvent(new ClientApplicationUnregisteredEvent(this,event.getValue()));
}

@Override
public void entryUpdated(EntryEvent<String, Application> event) {
reset();
context.publishEvent(new ClientApplicationRegisteredEvent(this,event.getValue()));
}

@Override
public void entryEvicted(EntryEvent<String, Application> event) {
reset();
context.publishEvent(new ClientApplicationRegisteredEvent(this,null));
}

@Override
public void mapEvicted(MapEvent event) {
reset();
context.publishEvent(new ClientApplicationRegisteredEvent(this,null));
}

@Override
public void mapCleared(MapEvent event) {
reset();
context.publishEvent(new ClientApplicationRegisteredEvent(this,null));
}
}
}
Expand All @@ -316,7 +311,7 @@ public void mapCleared(MapEvent event) {
@ConditionalOnClass({ DiscoveryClient.class })
@ConditionalOnProperty(prefix = "spring.boot.admin.discovery", name = "enabled", matchIfMissing = true)
@AutoConfigureAfter({ NoopDiscoveryClientAutoConfiguration.class })
public static class DiscoveryConfig {
public static class DiscoveryClientConfiguration {

@Value("${spring.boot.admin.discovery.management.context-path:}")
private String managementPath;
Expand All @@ -329,12 +324,10 @@ public static class DiscoveryConfig {

@Bean
ApplicationListener<ApplicationEvent> applicationDiscoveryListener() {
ApplicationDiscoveryListener listener = new ApplicationDiscoveryListener(
discoveryClient,
registry);
ApplicationDiscoveryListener listener = new ApplicationDiscoveryListener(discoveryClient, registry);
listener.setManagementContextPath(managementPath);
return listener;
}

}

}
Expand Up @@ -30,7 +30,7 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(WebappConfig.class)
@Import(AdminServerWebConfiguration.class)
public @interface EnableAdminServer {

}
Expand Up @@ -63,9 +63,9 @@ public static class TestAdminApplication {
public void setup() throws InterruptedException {
System.setProperty("hazelcast.wait.seconds.before.join", "0");
instance1 = (EmbeddedWebApplicationContext) SpringApplication.run(TestAdminApplication.class, new String[] {
"--server.port=0", "--spring.jmx.enabled=false", "--spring.boot.admin.hazelcast.enable=true" });
"--server.port=0", "--spring.jmx.enabled=false", "--spring.boot.admin.hazelcast.enabled=true" });
instance2 = (EmbeddedWebApplicationContext) SpringApplication.run(TestAdminApplication.class, new String[] {
"--server.port=0", "--spring.jmx.enabled=false", "--spring.boot.admin.hazelcast.enable=true" });
"--server.port=0", "--spring.jmx.enabled=false", "--spring.boot.admin.hazelcast.enabled=true" });
}

@After
Expand Down
@@ -0,0 +1,60 @@
package de.codecentric.boot.admin.config;

import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import de.codecentric.boot.admin.discovery.ApplicationDiscoveryListener;
import de.codecentric.boot.admin.registry.store.ApplicationStore;
import de.codecentric.boot.admin.registry.store.HazelcastApplicationStore;
import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;

public class AdminServerWebConfigurationTest {

private AnnotationConfigWebApplicationContext context;

@After
public void close() {
if (this.context != null) {
this.context.close();
}
}

@Test
public void simpleConfig() {
load("spring.boot.admin.hazelcast.enabled:false", "spring.boot.admin.discovery.enabled:false");
assertTrue(context.getBean(ApplicationStore.class) instanceof SimpleApplicationStore);
assertTrue(context.getBeansOfType(ApplicationDiscoveryListener.class).isEmpty());
}

@Test
public void hazelcastConfig() {
load("spring.boot.admin.hazelcast.enabled:true", "spring.boot.admin.discovery.enabled:false");
assertTrue(context.getBean(ApplicationStore.class) instanceof HazelcastApplicationStore);
assertTrue(context.getBeansOfType(ApplicationDiscoveryListener.class).isEmpty());
}

@Test
public void discoveryConfig() {
load("spring.boot.admin.hazelcast.enabled:false", "spring.boot.admin.discovery.enabled:true");
assertTrue(context.getBean(ApplicationStore.class) instanceof SimpleApplicationStore);
context.getBean(ApplicationDiscoveryListener.class);
}


private void load(String... environment) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(ServerPropertiesAutoConfiguration.class);
applicationContext.register(NoopDiscoveryClientAutoConfiguration.class);
applicationContext.register(AdminServerWebConfiguration.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
applicationContext.refresh();
this.context = applicationContext;
}

}
Expand Up @@ -2,5 +2,5 @@ server.port=8080
info.version=1.0.0
spring.application.name=spring-boot-admin-server-test
spring.boot.admin.url=http://localhost:8080
spring.boot.admin.hazelcast.enable=false
spring.boot.admin.discovery.enable=false
spring.boot.admin.hazelcast.enabled=false
spring.boot.admin.discovery.enabled=false
@@ -0,0 +1,62 @@
package de.codecentric.boot.admin.config;

import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import de.codecentric.boot.admin.actuate.LogfileMvcEndpoint;
import de.codecentric.boot.admin.services.SpringBootAdminRegistrator;

public class SpringBootAdminClientAutoConfigurationTest {

private AnnotationConfigWebApplicationContext context;

@After
public void close() {
if (this.context != null) {
this.context.close();
}
}

@Test
public void not_active() {
load();
assertTrue(context.getBeansOfType(SpringBootAdminRegistrator.class).isEmpty());
}

@Test
public void active_nologfile() {
load("spring.boot.admin.url:http://localhost:8081");
context.getBean(SpringBootAdminRegistrator.class);
assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty());
}

@Test
public void active_logfile() {
load("spring.boot.admin.url:http://localhost:8081", "logging.file:spring.log");
context.getBean(LogfileMvcEndpoint.class);
context.getBean(SpringBootAdminRegistrator.class);
}

@Test
public void active_logfile_supressed() {
load("spring.boot.admin.url:http://localhost:8081", "logging.file:spring.log",
"endpoints.logfile.enabled:false");
context.getBean(SpringBootAdminRegistrator.class);
assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty());
}

private void load(String... environment) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(ServerPropertiesAutoConfiguration.class);
applicationContext.register(SpringBootAdminClientAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
applicationContext.refresh();
this.context = applicationContext;
}

}

0 comments on commit 66173f5

Please sign in to comment.