Skip to content

Commit 66173f5

Browse files
author
Johannes Stelzer
committed
Add tests for the client & server autoconfig and fix issue with cyclic dependency
1 parent d370971 commit 66173f5

File tree

7 files changed

+146
-31
lines changed

7 files changed

+146
-31
lines changed

spring-boot-admin-server/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ One note: If you omit the Spring Boot Admin Client in you Client Applications yo
3939
### Further configuration
4040
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``.
4141

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

4444
## Hazelcast Support
4545
Spring Boot Admin Server supports cluster replication with Hazelcast.
@@ -93,6 +93,6 @@ Write xml-config hazelcast-config.xml:
9393
```
9494

9595
### Further configuration
96-
Disable Hazelcast support by setting ``spring.boot.admin.hazelcast.enable=false``.
96+
Disable Hazelcast support by setting ``spring.boot.admin.hazelcast.enabled=false``.
9797

9898
To alter the name of the Hazelcast-Map set ``spring.boot.admin.hazelcast.map= my-own-map-name``.

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/config/WebappConfig.java renamed to spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/config/AdminServerWebConfiguration.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter;
4747
import org.springframework.cloud.netflix.zuul.web.ZuulController;
4848
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
49+
import org.springframework.context.ApplicationContext;
4950
import org.springframework.context.ApplicationEvent;
5051
import org.springframework.context.ApplicationListener;
5152
import org.springframework.context.annotation.Bean;
@@ -65,6 +66,8 @@
6566

6667
import de.codecentric.boot.admin.controller.RegistryController;
6768
import de.codecentric.boot.admin.discovery.ApplicationDiscoveryListener;
69+
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
70+
import de.codecentric.boot.admin.event.ClientApplicationUnregisteredEvent;
6871
import de.codecentric.boot.admin.model.Application;
6972
import de.codecentric.boot.admin.registry.ApplicationIdGenerator;
7073
import de.codecentric.boot.admin.registry.ApplicationRegistry;
@@ -76,7 +79,7 @@
7679
import de.codecentric.boot.admin.zuul.ApplicationRouteRefreshListener;
7780

7881
@Configuration
79-
public class WebappConfig extends WebMvcConfigurerAdapter {
82+
public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter {
8083

8184
/**
8285
* Add JSON MessageConverter to send JSON objects to web clients.
@@ -213,7 +216,7 @@ public ZuulFilterInitializer zuulFilterInitializer() {
213216
}
214217

215218
@Bean
216-
ApplicationRouteRefreshListener applicationRouteRefreshListener() {
219+
public ApplicationRouteRefreshListener applicationRouteRefreshListener() {
217220
return new ApplicationRouteRefreshListener(routeLocator(), zuulHandlerMapping(routeLocator()));
218221
}
219222

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

236239
@Configuration
237240
@ConditionalOnClass({ Hazelcast.class })
238-
@ConditionalOnProperty(prefix = "spring.boot.admin.hazelcast", name = "enable", matchIfMissing = true)
241+
@ConditionalOnProperty(prefix = "spring.boot.admin.hazelcast", name = "enabled", matchIfMissing = true)
239242
@AutoConfigureBefore(SimpleStoreConfig.class)
240-
public static class HazelcastStoreConfig {
243+
public static class HazelcastStoreConfiguration {
241244

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

271274
private static class ApplicationEntryListener implements EntryListener<String, Application> {
272275
@Autowired
273-
private ZuulHandlerMapping zuulHandlerMapping;
274-
275-
@Autowired
276-
private ApplicationRouteLocator routeLocator;
277-
278-
private void reset() {
279-
routeLocator.resetRoutes();
280-
zuulHandlerMapping.registerHandlers();
281-
}
276+
ApplicationContext context;
282277

283278
@Override
284279
public void entryAdded(EntryEvent<String, Application> event) {
285-
reset();
280+
context.publishEvent(new ClientApplicationRegisteredEvent(this,event.getValue()));
286281
}
287282

288283
@Override
289284
public void entryRemoved(EntryEvent<String, Application> event) {
290-
reset();
285+
context.publishEvent(new ClientApplicationUnregisteredEvent(this,event.getValue()));
291286
}
292287

293288
@Override
294289
public void entryUpdated(EntryEvent<String, Application> event) {
295-
reset();
290+
context.publishEvent(new ClientApplicationRegisteredEvent(this,event.getValue()));
296291
}
297292

298293
@Override
299294
public void entryEvicted(EntryEvent<String, Application> event) {
300-
reset();
295+
context.publishEvent(new ClientApplicationRegisteredEvent(this,null));
301296
}
302297

303298
@Override
304299
public void mapEvicted(MapEvent event) {
305-
reset();
300+
context.publishEvent(new ClientApplicationRegisteredEvent(this,null));
306301
}
307302

308303
@Override
309304
public void mapCleared(MapEvent event) {
310-
reset();
305+
context.publishEvent(new ClientApplicationRegisteredEvent(this,null));
311306
}
312307
}
313308
}
@@ -316,7 +311,7 @@ public void mapCleared(MapEvent event) {
316311
@ConditionalOnClass({ DiscoveryClient.class })
317312
@ConditionalOnProperty(prefix = "spring.boot.admin.discovery", name = "enabled", matchIfMissing = true)
318313
@AutoConfigureAfter({ NoopDiscoveryClientAutoConfiguration.class })
319-
public static class DiscoveryConfig {
314+
public static class DiscoveryClientConfiguration {
320315

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

330325
@Bean
331326
ApplicationListener<ApplicationEvent> applicationDiscoveryListener() {
332-
ApplicationDiscoveryListener listener = new ApplicationDiscoveryListener(
333-
discoveryClient,
334-
registry);
327+
ApplicationDiscoveryListener listener = new ApplicationDiscoveryListener(discoveryClient, registry);
335328
listener.setManagementContextPath(managementPath);
336329
return listener;
337330
}
338-
339331
}
332+
340333
}

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/config/EnableAdminServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
@Target(ElementType.TYPE)
3131
@Retention(RetentionPolicy.RUNTIME)
3232
@Documented
33-
@Import(WebappConfig.class)
33+
@Import(AdminServerWebConfiguration.class)
3434
public @interface EnableAdminServer {
3535

3636
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/AdminApplicationHazelcastTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public static class TestAdminApplication {
6363
public void setup() throws InterruptedException {
6464
System.setProperty("hazelcast.wait.seconds.before.join", "0");
6565
instance1 = (EmbeddedWebApplicationContext) SpringApplication.run(TestAdminApplication.class, new String[] {
66-
"--server.port=0", "--spring.jmx.enabled=false", "--spring.boot.admin.hazelcast.enable=true" });
66+
"--server.port=0", "--spring.jmx.enabled=false", "--spring.boot.admin.hazelcast.enabled=true" });
6767
instance2 = (EmbeddedWebApplicationContext) SpringApplication.run(TestAdminApplication.class, new String[] {
68-
"--server.port=0", "--spring.jmx.enabled=false", "--spring.boot.admin.hazelcast.enable=true" });
68+
"--server.port=0", "--spring.jmx.enabled=false", "--spring.boot.admin.hazelcast.enabled=true" });
6969
}
7070

7171
@After
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package de.codecentric.boot.admin.config;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.After;
6+
import org.junit.Test;
7+
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
8+
import org.springframework.boot.test.EnvironmentTestUtils;
9+
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
10+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
11+
12+
import de.codecentric.boot.admin.discovery.ApplicationDiscoveryListener;
13+
import de.codecentric.boot.admin.registry.store.ApplicationStore;
14+
import de.codecentric.boot.admin.registry.store.HazelcastApplicationStore;
15+
import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;
16+
17+
public class AdminServerWebConfigurationTest {
18+
19+
private AnnotationConfigWebApplicationContext context;
20+
21+
@After
22+
public void close() {
23+
if (this.context != null) {
24+
this.context.close();
25+
}
26+
}
27+
28+
@Test
29+
public void simpleConfig() {
30+
load("spring.boot.admin.hazelcast.enabled:false", "spring.boot.admin.discovery.enabled:false");
31+
assertTrue(context.getBean(ApplicationStore.class) instanceof SimpleApplicationStore);
32+
assertTrue(context.getBeansOfType(ApplicationDiscoveryListener.class).isEmpty());
33+
}
34+
35+
@Test
36+
public void hazelcastConfig() {
37+
load("spring.boot.admin.hazelcast.enabled:true", "spring.boot.admin.discovery.enabled:false");
38+
assertTrue(context.getBean(ApplicationStore.class) instanceof HazelcastApplicationStore);
39+
assertTrue(context.getBeansOfType(ApplicationDiscoveryListener.class).isEmpty());
40+
}
41+
42+
@Test
43+
public void discoveryConfig() {
44+
load("spring.boot.admin.hazelcast.enabled:false", "spring.boot.admin.discovery.enabled:true");
45+
assertTrue(context.getBean(ApplicationStore.class) instanceof SimpleApplicationStore);
46+
context.getBean(ApplicationDiscoveryListener.class);
47+
}
48+
49+
50+
private void load(String... environment) {
51+
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
52+
applicationContext.register(ServerPropertiesAutoConfiguration.class);
53+
applicationContext.register(NoopDiscoveryClientAutoConfiguration.class);
54+
applicationContext.register(AdminServerWebConfiguration.class);
55+
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
56+
applicationContext.refresh();
57+
this.context = applicationContext;
58+
}
59+
60+
}

spring-boot-admin-server/src/test/resources/application.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ server.port=8080
22
info.version=1.0.0
33
spring.application.name=spring-boot-admin-server-test
44
spring.boot.admin.url=http://localhost:8080
5-
spring.boot.admin.hazelcast.enable=false
6-
spring.boot.admin.discovery.enable=false
5+
spring.boot.admin.hazelcast.enabled=false
6+
spring.boot.admin.discovery.enabled=false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package de.codecentric.boot.admin.config;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.After;
6+
import org.junit.Test;
7+
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
8+
import org.springframework.boot.test.EnvironmentTestUtils;
9+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
10+
11+
import de.codecentric.boot.admin.actuate.LogfileMvcEndpoint;
12+
import de.codecentric.boot.admin.services.SpringBootAdminRegistrator;
13+
14+
public class SpringBootAdminClientAutoConfigurationTest {
15+
16+
private AnnotationConfigWebApplicationContext context;
17+
18+
@After
19+
public void close() {
20+
if (this.context != null) {
21+
this.context.close();
22+
}
23+
}
24+
25+
@Test
26+
public void not_active() {
27+
load();
28+
assertTrue(context.getBeansOfType(SpringBootAdminRegistrator.class).isEmpty());
29+
}
30+
31+
@Test
32+
public void active_nologfile() {
33+
load("spring.boot.admin.url:http://localhost:8081");
34+
context.getBean(SpringBootAdminRegistrator.class);
35+
assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty());
36+
}
37+
38+
@Test
39+
public void active_logfile() {
40+
load("spring.boot.admin.url:http://localhost:8081", "logging.file:spring.log");
41+
context.getBean(LogfileMvcEndpoint.class);
42+
context.getBean(SpringBootAdminRegistrator.class);
43+
}
44+
45+
@Test
46+
public void active_logfile_supressed() {
47+
load("spring.boot.admin.url:http://localhost:8081", "logging.file:spring.log",
48+
"endpoints.logfile.enabled:false");
49+
context.getBean(SpringBootAdminRegistrator.class);
50+
assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty());
51+
}
52+
53+
private void load(String... environment) {
54+
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
55+
applicationContext.register(ServerPropertiesAutoConfiguration.class);
56+
applicationContext.register(SpringBootAdminClientAutoConfiguration.class);
57+
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
58+
applicationContext.refresh();
59+
this.context = applicationContext;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)