Commit 9166bb5f authored by Phillip Webb's avatar Phillip Webb

Polish

parent 75f8e8a5
...@@ -70,6 +70,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. ...@@ -70,6 +70,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@TestPropertySource(properties = "management.security.enabled=false") @TestPropertySource(properties = "management.security.enabled=false")
public class LoggersMvcEndpointTests { public class LoggersMvcEndpointTests {
private static final String PATH = "/application/loggers";
@Autowired @Autowired
private WebApplicationContext context; private WebApplicationContext context;
...@@ -99,48 +101,47 @@ public class LoggersMvcEndpointTests { ...@@ -99,48 +101,47 @@ public class LoggersMvcEndpointTests {
.singletonList(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG))); .singletonList(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG)));
String expected = "{\"levels\":[\"OFF\",\"FATAL\",\"ERROR\",\"WARN\",\"INFO\",\"DEBUG\",\"TRACE\"]," String expected = "{\"levels\":[\"OFF\",\"FATAL\",\"ERROR\",\"WARN\",\"INFO\",\"DEBUG\",\"TRACE\"],"
+ "\"loggers\":{\"ROOT\":{\"configuredLevel\":null,\"effectiveLevel\":\"DEBUG\"}}}"; + "\"loggers\":{\"ROOT\":{\"configuredLevel\":null,\"effectiveLevel\":\"DEBUG\"}}}";
this.mvc.perform(get("/application/loggers")).andExpect(status().isOk()) this.mvc.perform(get(PATH + "")).andExpect(status().isOk())
.andExpect(content().json(expected)); .andExpect(content().json(expected));
} }
@Test @Test
public void getLoggersWhenDisabledShouldReturnNotFound() throws Exception { public void getLoggersWhenDisabledShouldReturnNotFound() throws Exception {
this.context.getBean(LoggersEndpoint.class).setEnabled(false); this.context.getBean(LoggersEndpoint.class).setEnabled(false);
this.mvc.perform(get("/application/loggers")).andExpect(status().isNotFound()); this.mvc.perform(get(PATH + "")).andExpect(status().isNotFound());
} }
@Test @Test
public void getLoggerShouldReturnLogLevels() throws Exception { public void getLoggerShouldReturnLogLevels() throws Exception {
given(this.loggingSystem.getLoggerConfiguration("ROOT")) given(this.loggingSystem.getLoggerConfiguration("ROOT"))
.willReturn(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG)); .willReturn(new LoggerConfiguration("ROOT", null, LogLevel.DEBUG));
this.mvc.perform(get("/application/loggers/ROOT")).andExpect(status().isOk()) this.mvc.perform(get(PATH + "/ROOT")).andExpect(status().isOk())
.andExpect(content().string(equalTo( .andExpect(content().string(equalTo(
"{\"configuredLevel\":null," + "\"effectiveLevel\":\"DEBUG\"}"))); "{\"configuredLevel\":null,\"effectiveLevel\":\"DEBUG\"}")));
} }
@Test @Test
public void getLoggersRootWhenDisabledShouldReturnNotFound() throws Exception { public void getLoggersRootWhenDisabledShouldReturnNotFound() throws Exception {
this.context.getBean(LoggersEndpoint.class).setEnabled(false); this.context.getBean(LoggersEndpoint.class).setEnabled(false);
this.mvc.perform(get("/application/loggers/ROOT")) this.mvc.perform(get(PATH + "/ROOT")).andExpect(status().isNotFound());
.andExpect(status().isNotFound());
} }
@Test @Test
public void getLoggersWhenLoggerNotFoundShouldReturnNotFound() throws Exception { public void getLoggersWhenLoggerNotFoundShouldReturnNotFound() throws Exception {
this.mvc.perform(get("/application/loggers/com.does.not.exist")) this.mvc.perform(get(PATH + "/com.does.not.exist"))
.andExpect(status().isNotFound()); .andExpect(status().isNotFound());
} }
@Test @Test
public void contentTypeForGetDefaultsToActuatorV2Json() throws Exception { public void contentTypeForGetDefaultsToActuatorV2Json() throws Exception {
this.mvc.perform(get("/application/loggers")).andExpect(status().isOk()) this.mvc.perform(get(PATH + "")).andExpect(status().isOk())
.andExpect(header().string("Content-Type", .andExpect(header().string("Content-Type",
"application/vnd.spring-boot.actuator.v2+json;charset=UTF-8")); "application/vnd.spring-boot.actuator.v2+json;charset=UTF-8"));
} }
@Test @Test
public void contentTypeForGetCanBeApplicationJson() throws Exception { public void contentTypeForGetCanBeApplicationJson() throws Exception {
this.mvc.perform(get("/application/loggers").header(HttpHeaders.ACCEPT, this.mvc.perform(get(PATH + "").header(HttpHeaders.ACCEPT,
MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk()) MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk())
.andExpect(header().string("Content-Type", .andExpect(header().string("Content-Type",
MediaType.APPLICATION_JSON_UTF8_VALUE)); MediaType.APPLICATION_JSON_UTF8_VALUE));
...@@ -148,16 +149,14 @@ public class LoggersMvcEndpointTests { ...@@ -148,16 +149,14 @@ public class LoggersMvcEndpointTests {
@Test @Test
public void setLoggerUsingApplicationJsonShouldSetLogLevel() throws Exception { public void setLoggerUsingApplicationJsonShouldSetLogLevel() throws Exception {
this.mvc.perform( this.mvc.perform(post(PATH + "/ROOT").contentType(MediaType.APPLICATION_JSON)
post("/application/loggers/ROOT").contentType(MediaType.APPLICATION_JSON) .content("{\"configuredLevel\":\"debug\"}")).andExpect(status().isOk());
.content("{\"configuredLevel\":\"debug\"}"))
.andExpect(status().isOk());
verify(this.loggingSystem).setLogLevel("ROOT", LogLevel.DEBUG); verify(this.loggingSystem).setLogLevel("ROOT", LogLevel.DEBUG);
} }
@Test @Test
public void setLoggerUsingActuatorV2JsonShouldSetLogLevel() throws Exception { public void setLoggerUsingActuatorV2JsonShouldSetLogLevel() throws Exception {
this.mvc.perform(post("/application/loggers/ROOT") this.mvc.perform(post(PATH + "/ROOT")
.contentType(ActuatorMediaTypes.APPLICATION_ACTUATOR_V2_JSON) .contentType(ActuatorMediaTypes.APPLICATION_ACTUATOR_V2_JSON)
.content("{\"configuredLevel\":\"debug\"}")).andExpect(status().isOk()); .content("{\"configuredLevel\":\"debug\"}")).andExpect(status().isOk());
verify(this.loggingSystem).setLogLevel("ROOT", LogLevel.DEBUG); verify(this.loggingSystem).setLogLevel("ROOT", LogLevel.DEBUG);
...@@ -166,8 +165,7 @@ public class LoggersMvcEndpointTests { ...@@ -166,8 +165,7 @@ public class LoggersMvcEndpointTests {
@Test @Test
public void setLoggerWhenDisabledShouldReturnNotFound() throws Exception { public void setLoggerWhenDisabledShouldReturnNotFound() throws Exception {
this.context.getBean(LoggersEndpoint.class).setEnabled(false); this.context.getBean(LoggersEndpoint.class).setEnabled(false);
this.mvc.perform( this.mvc.perform(post(PATH + "/ROOT").contentType(MediaType.APPLICATION_JSON)
post("/application/loggers/ROOT").contentType(MediaType.APPLICATION_JSON)
.content("{\"configuredLevel\":\"DEBUG\"}")) .content("{\"configuredLevel\":\"DEBUG\"}"))
.andExpect(status().isNotFound()); .andExpect(status().isNotFound());
verifyZeroInteractions(this.loggingSystem); verifyZeroInteractions(this.loggingSystem);
...@@ -175,8 +173,7 @@ public class LoggersMvcEndpointTests { ...@@ -175,8 +173,7 @@ public class LoggersMvcEndpointTests {
@Test @Test
public void setLoggerWithWrongLogLevel() throws Exception { public void setLoggerWithWrongLogLevel() throws Exception {
this.mvc.perform( this.mvc.perform(post(PATH + "/ROOT").contentType(MediaType.APPLICATION_JSON)
post("/application/loggers/ROOT").contentType(MediaType.APPLICATION_JSON)
.content("{\"configuredLevel\":\"other\"}")) .content("{\"configuredLevel\":\"other\"}"))
.andExpect(status().is4xxClientError()); .andExpect(status().is4xxClientError());
verifyZeroInteractions(this.loggingSystem); verifyZeroInteractions(this.loggingSystem);
...@@ -187,9 +184,9 @@ public class LoggersMvcEndpointTests { ...@@ -187,9 +184,9 @@ public class LoggersMvcEndpointTests {
throws Exception { throws Exception {
given(this.loggingSystem.getLoggerConfiguration("com.png")) given(this.loggingSystem.getLoggerConfiguration("com.png"))
.willReturn(new LoggerConfiguration("com.png", null, LogLevel.DEBUG)); .willReturn(new LoggerConfiguration("com.png", null, LogLevel.DEBUG));
this.mvc.perform(get("/application/loggers/com.png")).andExpect(status().isOk()) this.mvc.perform(get(PATH + "/com.png")).andExpect(status().isOk())
.andExpect(content().string(equalTo( .andExpect(content().string(equalTo(
"{\"configuredLevel\":null," + "\"effectiveLevel\":\"DEBUG\"}"))); "{\"configuredLevel\":null,\"effectiveLevel\":\"DEBUG\"}")));
} }
@Configuration @Configuration
......
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