|
|
|
@ -16,43 +16,56 @@
|
|
|
|
|
|
|
|
|
|
package sample.actuator.log4j2;
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
import org.junit.Rule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
|
|
|
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.test.annotation.DirtiesContext;
|
|
|
|
|
import org.springframework.boot.test.rule.OutputCapture;
|
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Basic integration tests for service demo application.
|
|
|
|
|
* Tests for {@link SampleActuatorLog4J2Application}.
|
|
|
|
|
*
|
|
|
|
|
* @author Dave Syer
|
|
|
|
|
* @@author Stephane Nicoll
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
|
|
|
|
@DirtiesContext
|
|
|
|
|
@SpringBootTest
|
|
|
|
|
@AutoConfigureMockMvc
|
|
|
|
|
public class SampleActuatorLog4J2ApplicationTests {
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LogManager.getLogger(SampleActuatorLog4J2ApplicationTests.class);
|
|
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
|
public OutputCapture output = new OutputCapture();
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private TestRestTemplate restTemplate;
|
|
|
|
|
private MockMvc mvc;
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testLogger() {
|
|
|
|
|
logger.info("Hello World");
|
|
|
|
|
this.output.expect(containsString("Hello World"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testHome() throws Exception {
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
|
ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class);
|
|
|
|
|
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
Map<String, Object> body = entity.getBody();
|
|
|
|
|
assertThat(body.get("message")).isEqualTo("Hello Daniel");
|
|
|
|
|
public void validateLoggersEndpoint() throws Exception {
|
|
|
|
|
this.mvc.perform(get("/loggers/org.apache.coyote.http11.Http11NioProtocol"))
|
|
|
|
|
.andExpect(status().isOk())
|
|
|
|
|
.andExpect(content().string(equalTo(
|
|
|
|
|
"{\"configuredLevel\":\"WARN\"," + "\"effectiveLevel\":\"WARN\"}")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|