Use AssertJ in spring-boot-samples

See gh-5083
This commit is contained in:
Phillip Webb
2016-02-06 14:53:10 -08:00
parent 962a598531
commit 1cc1fc6431
97 changed files with 580 additions and 733 deletions

View File

@@ -33,8 +33,7 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for endpoints configuration.
@@ -59,11 +58,11 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/oops", Map.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("None", body.get("error"));
assertEquals(999, body.get("status"));
assertThat(body.get("error")).isEqualTo("None");
assertThat(body.get("status")).isEqualTo(999);
}
@Test
@@ -71,12 +70,9 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/admin/health",
String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"status\":\"UP\""));
System.err.println(entity.getBody());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"hello\":\"world\""));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
assertThat(entity.getBody()).contains("\"hello\":\"world\"");
}
private String getPassword() {

View File

@@ -32,8 +32,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for separate management and main service ports.
@@ -61,10 +60,10 @@ public class InsecureManagementPortAndPathSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Hello Phil", body.get("message"));
assertThat(body.get("message")).isEqualTo("Hello Phil");
}
@Test
@@ -73,7 +72,7 @@ public class InsecureManagementPortAndPathSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/admin/metrics", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
}
@Test
@@ -81,9 +80,8 @@ public class InsecureManagementPortAndPathSampleActuatorApplicationTests {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/admin/health",
String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"status\":\"UP\""));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
}
@Test
@@ -91,9 +89,8 @@ public class InsecureManagementPortAndPathSampleActuatorApplicationTests {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/admin/missing",
String.class);
assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"status\":404"));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(entity.getBody()).contains("\"status\":404");
}
private String getPassword() {

View File

@@ -31,9 +31,7 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for insecured service endpoints (even with Spring Security on
@@ -56,12 +54,11 @@ public class InsecureManagementSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Wrong body: " + body, "Unauthorized", body.get("error"));
assertFalse("Wrong headers: " + entity.getHeaders(),
entity.getHeaders().containsKey("Set-Cookie"));
assertThat(body.get("error")).isEqualTo("Unauthorized");
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
}
@Test
@@ -75,11 +72,10 @@ public class InsecureManagementSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertTrue("Wrong body: " + body,
body.containsKey("counter.status.401.unmapped"));
assertThat(body).containsKey("counter.status.401.unmapped");
}
}

View File

@@ -30,8 +30,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for insecured service endpoints (even with Spring Security on
@@ -53,12 +52,11 @@ public class InsecureSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Hello Phil", body.get("message"));
assertFalse("Wrong headers: " + entity.getHeaders(),
entity.getHeaders().containsKey("Set-Cookie"));
assertThat(body.get("message")).isEqualTo("Hello Phil");
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
}
}

View File

@@ -30,8 +30,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for separate management and main service ports.
@@ -56,7 +55,7 @@ public class ManagementAddressActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}
@Test
@@ -64,9 +63,8 @@ public class ManagementAddressActuatorApplicationTests {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/admin/health",
String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"status\":\"UP\""));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
}
}

View File

@@ -30,9 +30,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for endpoints configuration.
@@ -52,9 +50,8 @@ public class ManagementPathSampleActuatorApplicationTests {
public void testHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/admin/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"status\":\"UP\""));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
}
@Test
@@ -62,12 +59,11 @@ public class ManagementPathSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Wrong body: " + body, "Unauthorized", body.get("error"));
assertFalse("Wrong headers: " + entity.getHeaders(),
entity.getHeaders().containsKey("Set-Cookie"));
assertThat(body.get("error")).isEqualTo("Unauthorized");
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
}
}

View File

@@ -32,8 +32,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for separate management and main service ports.
@@ -61,10 +60,10 @@ public class ManagementPortAndPathSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Hello Phil", body.get("message"));
assertThat(body.get("message")).isEqualTo("Hello Phil");
}
@Test
@@ -73,7 +72,7 @@ public class ManagementPortAndPathSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/admin/metrics", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}
@Test
@@ -81,9 +80,8 @@ public class ManagementPortAndPathSampleActuatorApplicationTests {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/admin/health",
String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"status\":\"UP\""));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
}
@Test
@@ -92,9 +90,8 @@ public class ManagementPortAndPathSampleActuatorApplicationTests {
.getForEntity(
"http://localhost:" + this.managementPort + "/admin/missing",
String.class);
assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"status\":404"));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(entity.getBody()).contains("\"status\":404");
}
@Test
@@ -102,10 +99,10 @@ public class ManagementPortAndPathSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/error", Map.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals(999, body.get("status"));
assertThat(body.get("status")).isEqualTo(999);
}
@Test
@@ -113,11 +110,10 @@ public class ManagementPortAndPathSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/error", Map.class);
// TODO: should be 500?
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals(999, body.get("status"));
assertThat(body.get("status")).isEqualTo(999);
}
private String getPassword() {

View File

@@ -32,8 +32,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for separate management and main service ports.
@@ -60,10 +59,10 @@ public class ManagementPortSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Hello Phil", body.get("message"));
assertThat(body.get("message")).isEqualTo("Hello Phil");
}
@Test
@@ -72,16 +71,15 @@ public class ManagementPortSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/metrics", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}
@Test
public void testHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"status\":\"UP\""));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
}
@Test
@@ -89,10 +87,10 @@ public class ManagementPortSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/error", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals(999, body.get("status"));
assertThat(body.get("status")).isEqualTo(999);
}
private String getPassword() {

View File

@@ -32,7 +32,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for switching off management endpoints.
@@ -56,10 +56,10 @@ public class NoManagementSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Hello Phil", body.get("message"));
assertThat(body.get("message")).isEqualTo("Hello Phil");
}
@Test
@@ -68,7 +68,7 @@ public class NoManagementSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class);
assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
private String getPassword() {

View File

@@ -28,8 +28,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for /health with {@code endpoints.health.sensitive=false}.
@@ -49,9 +48,8 @@ public class NonSensitiveHealthTests {
public void testSecureHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertFalse("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"hello\":1"));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).doesNotContain("\"hello\":1");
}
}

View File

@@ -38,10 +38,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Basic integration tests for service demo application.
@@ -65,12 +62,11 @@ public class SampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Wrong body: " + body, "Unauthorized", body.get("error"));
assertFalse("Wrong headers: " + entity.getHeaders(),
entity.getHeaders().containsKey("Set-Cookie"));
assertThat(body.get("error")).isEqualTo("Unauthorized");
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
}
@Test
@@ -78,16 +74,16 @@ public class SampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/metrics/", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/metrics/foo", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/metrics.json", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}
@Test
@@ -95,10 +91,10 @@ public class SampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Hello Phil", body.get("message"));
assertThat(body.get("message")).isEqualTo("Hello Phil");
}
@Test
@@ -107,10 +103,10 @@ public class SampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertTrue("Wrong body: " + body, body.containsKey("counter.status.200.root"));
assertThat(body).containsKey("counter.status.200.root");
}
@Test
@@ -118,49 +114,45 @@ public class SampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/env", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertTrue("Wrong body: " + body, body.containsKey("systemProperties"));
assertThat(body).containsKey("systemProperties");
}
@Test
public void testHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"status\":\"UP\""));
assertFalse("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"hello\":\"1\""));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
assertThat(entity.getBody()).doesNotContain("\"hello\":\"1\"");
}
@Test
public void testSecureHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"hello\":1"));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"hello\":1");
}
@Test
public void testInfo() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/info", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(), entity.getBody()
.contains("\"artifact\":\"spring-boot-sample-actuator\""));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody())
.contains("\"artifact\":\"spring-boot-sample-actuator\"");
}
@Test
public void testErrorPage() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/foo", String.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
String body = entity.getBody();
assertNotNull(body);
assertTrue("Wrong body: " + body, body.contains("\"error\":"));
assertThat(body).contains("\"error\":");
}
@Test
@@ -171,11 +163,10 @@ public class SampleActuatorApplicationTests {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.exchange("http://localhost:" + this.port + "/foo", HttpMethod.GET,
request, String.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
String body = entity.getBody();
assertNotNull("Body was null", body);
assertTrue("Wrong body: " + body,
body.contains("This application has no explicit mapping for /error"));
assertThat(body).as("Body was null").isNotNull();
assertThat(body).contains("This application has no explicit mapping for /error");
}
@Test
@@ -185,14 +176,14 @@ public class SampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<List> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/trace", List.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
List<Map<String, Object>> list = entity.getBody();
Map<String, Object> trace = list.get(list.size() - 1);
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) trace
.get("info")).get("headers")).get("response");
assertEquals("200", map.get("status"));
assertThat(map.get("status")).isEqualTo("200");
}
@Test
@@ -200,24 +191,23 @@ public class SampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/error", Map.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("None", body.get("error"));
assertEquals(999, body.get("status"));
assertThat(body.get("error")).isEqualTo("None");
assertThat(body.get("status")).isEqualTo(999);
}
@Test
@SuppressWarnings("unchecked")
public void testBeans() throws Exception {
@SuppressWarnings("rawtypes")
ResponseEntity<List> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/beans", List.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals(1, entity.getBody().size());
@SuppressWarnings("unchecked")
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).hasSize(1);
Map<String, Object> body = (Map<String, Object>) entity.getBody().get(0);
assertTrue("Wrong body: " + body,
((String) body.get("context")).startsWith("application"));
assertThat(((String) body.get("context"))).startsWith("application");
}
@Test
@@ -226,11 +216,10 @@ public class SampleActuatorApplicationTests {
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/configprops",
Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertTrue("Wrong body: " + body,
body.containsKey("spring.datasource.CONFIGURATION_PROPERTIES"));
assertThat(body).containsKey("spring.datasource.CONFIGURATION_PROPERTIES");
}
private String getPassword() {

View File

@@ -30,8 +30,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for insecured service endpoints (even with Spring Security on
@@ -54,12 +53,11 @@ public class ServletPathInsecureSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/spring/", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Hello Phil", body.get("message"));
assertFalse("Wrong headers: " + entity.getHeaders(),
entity.getHeaders().containsKey("Set-Cookie"));
assertThat(body.get("message")).isEqualTo("Hello Phil");
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
}
@Test
@@ -67,7 +65,7 @@ public class ServletPathInsecureSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/spring/metrics", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}
}

View File

@@ -30,9 +30,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for endpoints configuration.
@@ -54,20 +52,19 @@ public class ServletPathSampleActuatorApplicationTests {
ResponseEntity<Map> entity = new TestRestTemplate("user", "password")
.getForEntity("http://localhost:" + this.port + "/spring/error",
Map.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("None", body.get("error"));
assertEquals(999, body.get("status"));
assertThat(body.get("error")).isEqualTo("None");
assertThat(body.get("status")).isEqualTo(999);
}
@Test
public void testHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/spring/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body: " + entity.getBody(),
entity.getBody().contains("\"status\":\"UP\""));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
}
@Test
@@ -75,12 +72,11 @@ public class ServletPathSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/spring/", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Wrong body: " + body, "Unauthorized", body.get("error"));
assertFalse("Wrong headers: " + entity.getHeaders(),
entity.getHeaders().containsKey("Set-Cookie"));
assertThat(body.get("error")).isEqualTo("Unauthorized");
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
}
}

View File

@@ -32,8 +32,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for separate management and main service ports.
@@ -57,10 +56,10 @@ public class ShutdownSampleActuatorApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Hello Phil", body.get("message"));
assertThat(body.get("message")).isEqualTo("Hello Phil");
}
@Test
@@ -69,11 +68,10 @@ public class ShutdownSampleActuatorApplicationTests {
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.postForEntity("http://localhost:" + this.port + "/shutdown", null,
Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertTrue("Wrong body: " + body,
((String) body.get("message")).contains("Shutting down"));
assertThat(((String) body.get("message"))).contains("Shutting down");
}
private String getPassword() {