Bael 3329 - Updates to Spring Boot Admin (#7856)

* BAEL-3222 Example Code and update to spring-boot-admin examples

* BAEL-3329 Fix integration tests

* BAEL-3329 Fix integration tests
This commit is contained in:
Amy DeGregorio 2019-09-23 15:41:37 -04:00 committed by maibin
parent 7da66ebd84
commit 933b41c8b5
7 changed files with 52 additions and 39 deletions

View File

@ -15,7 +15,7 @@ import static org.junit.Assert.assertEquals;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ -36,20 +36,20 @@ public class SpringBootAdminClientApplicationIntegrationTest {
@Test
public void whenEnvironmentAvailable_ThenAdminServerPropertiesExist() {
assertEquals(environment.getProperty("spring.boot.admin.url"), "http://localhost:8080");
assertEquals(environment.getProperty("spring.boot.admin.username"), "admin");
assertEquals(environment.getProperty("spring.boot.admin.password"), "admin");
assertEquals(environment.getProperty("spring.boot.admin.client.url"), "http://localhost:8080");
assertEquals(environment.getProperty("spring.boot.admin.client.username"), "admin");
assertEquals(environment.getProperty("spring.boot.admin.client.password"), "admin");
}
@Test
public void whenHttpBasicAttempted_ThenSuccess() throws Exception {
mockMvc.perform(get("/env").with(httpBasic("client", "client")));
mockMvc.perform(get("/actuator/env").with(httpBasic("client", "client")));
}
@Test
public void whenInvalidHttpBasicAttempted_ThenUnauthorized() throws Exception {
mockMvc
.perform(get("/env").with(httpBasic("client", "invalid")))
.andExpect(status().isUnauthorized());
.perform(get("/actuator/env").with(httpBasic("client", "invalid")))
.andExpect(unauthenticated());
}
}

View File

@ -51,10 +51,12 @@
<version>${spring-boot-admin-starter-client.version}</version>
</dependency>
<!--mail notifications -->
<!--<dependency> -->
<!--<groupId>org.springframework.boot</groupId> -->
<!--<artifactId>spring-boot-starter-mail</artifactId> -->
<!--</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -1,32 +1,40 @@
package com.baeldung.springbootadminserver.configs;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.notify.CompositeNotifier;
import de.codecentric.boot.admin.server.notify.LoggingNotifier;
import de.codecentric.boot.admin.server.notify.Notifier;
import de.codecentric.boot.admin.server.notify.RemindingNotifier;
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
@Configuration
public class NotifierConfiguration {
@Autowired
private InstanceRepository repository;
private final InstanceRepository repository;
private final ObjectProvider<List<Notifier>> otherNotifiers;
// @Autowired private Notifier notifier;
@Bean
public LoggingNotifier notifier() {
return new LoggingNotifier(repository);
public NotifierConfiguration(InstanceRepository repository, ObjectProvider<List<Notifier>> otherNotifiers) {
this.repository = repository;
this.otherNotifiers = otherNotifiers;
}
@Bean
public FilteringNotifier filteringNotifier() {
return new FilteringNotifier(notifier(), repository);
CompositeNotifier delegate = new CompositeNotifier(this.otherNotifiers.getIfAvailable(Collections::emptyList));
return new FilteringNotifier(delegate, this.repository);
}
@Bean
public LoggingNotifier notifier() {
return new LoggingNotifier(repository);
}
@Primary

View File

@ -5,6 +5,7 @@ import java.util.UUID;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
@ -13,6 +14,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final AdminServerProperties adminServer;

View File

@ -16,14 +16,14 @@ management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#mail notifications
#spring.mail.host=smtp.gmail.com
#spring.mail.username=test@gmail.com
#spring.mail.password=password
#spring.mail.port=587
#spring.mail.properties.mail.smtp.auth=true
#spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.host=
spring.mail.username=
spring.mail.password=
spring.mail.port=
spring.mail.properties.mail.smtp.auth=
spring.mail.properties.mail.smtp.starttls.enable=
#spring.boot.admin.notify.mail.to=test@gmail.com
spring.boot.admin.notify.mail.to=
#hipchat notifications
#spring.boot.admin.notify.hipchat.auth-token=<generated_token>

View File

@ -1,10 +1,7 @@
package com.baeldung.springbootadminserver;
import com.baeldung.springbootadminserver.configs.NotifierConfiguration;
import de.codecentric.boot.admin.server.notify.Notifier;
import de.codecentric.boot.admin.server.notify.RemindingNotifier;
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
import static org.junit.Assert.assertNotEquals;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -13,11 +10,14 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertNotEquals;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
import com.baeldung.springbootadminserver.configs.NotifierConfiguration;
import de.codecentric.boot.admin.server.notify.Notifier;
import de.codecentric.boot.admin.server.notify.RemindingNotifier;
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { NotifierConfiguration.class }, webEnvironment = NONE)
@SpringBootTest(classes = { NotifierConfiguration.class, SpringBootAdminServerApplication.class }, webEnvironment = NONE)
public class NotifierConfigurationIntegrationTest {
@Autowired private ApplicationContext applicationContext;

View File

@ -14,6 +14,7 @@ import static org.springframework.security.test.web.servlet.request.SecurityMock
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ -51,21 +52,21 @@ public class WebSecurityConfigIntegrationTest {
.password("admin"));
mockMvc
.perform(get("/api/applications/"))
.perform(get("/applications/"))
.andExpect(status().is2xxSuccessful());
}
@Test
public void whenHttpBasicAttempted_ThenSuccess() throws Exception {
mockMvc.perform(get("/env").with(httpBasic("admin", "admin")));
mockMvc.perform(get("/actuator/env").with(httpBasic("admin", "admin")));
}
@Test
public void whenInvalidHttpBasicAttempted_ThenUnauthorized() throws Exception {
mockMvc
.perform(get("/env").with(httpBasic("admin", "invalid")))
.andExpect(status().isUnauthorized());
.perform(get("/actuator/env").with(httpBasic("admin", "invalid")))
.andExpect(unauthenticated());
}
}