JAVA-11420: Dissolve spring-boot-1 and spring-boot-2 inside (#12122)
spring-boot-modules
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.actuator;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.LocalManagementPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItems;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "management.port=0")
|
||||
public class CustomEndpointIntegrationTest {
|
||||
|
||||
@LocalManagementPort
|
||||
private int port;
|
||||
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenActuatorCustomEndpointWorks() throws IOException {
|
||||
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:" + port + "/customEndpoint", String.class);
|
||||
|
||||
assertThat(entity.getStatusCode(), is(HttpStatus.OK));
|
||||
|
||||
List<String> response = objectMapper.readValue(entity.getBody(), new TypeReference<ArrayList<String>>() {
|
||||
});
|
||||
|
||||
assertThat(response, hasItems("This is message 1", "This is message 2"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.actuator;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.LocalManagementPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.collection.IsMapContaining.hasEntry;
|
||||
import static org.hamcrest.collection.IsMapContaining.hasKey;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "management.port=0")
|
||||
public class HealthCheckIntegrationTest {
|
||||
|
||||
@LocalManagementPort
|
||||
private int port;
|
||||
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenActuatorHealthEndpointWorks() throws IOException {
|
||||
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:" + port + "/health", String.class);
|
||||
|
||||
assertThat(entity.getStatusCode(), is(HttpStatus.OK));
|
||||
|
||||
Map<String, Object> response = objectMapper.readValue(entity.getBody(), new TypeReference<LinkedHashMap<String, Object>>() {
|
||||
});
|
||||
|
||||
assertThat(response, hasEntry("status", "UP"));
|
||||
assertThat(response, hasKey("myHealthCheck"));
|
||||
assertThat(response, hasKey("diskSpace"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.actuator;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class HealthCheckUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCheckMethodReturnsZero_thenHealthMethodReturnsStatusUP() {
|
||||
HealthCheck healthCheck = Mockito.spy(new HealthCheck());
|
||||
when(healthCheck.check()).thenReturn(0);
|
||||
Health health = healthCheck.health();
|
||||
|
||||
assertThat(health.getStatus(), is(Status.UP));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckMethodReturnsOtherThanZero_thenHealthMethodReturnsStatusDOWN() {
|
||||
HealthCheck healthCheck = Mockito.spy(new HealthCheck());
|
||||
when(healthCheck.check()).thenReturn(-1);
|
||||
Health health = healthCheck.health();
|
||||
|
||||
assertThat(health.getStatus(), is(Status.DOWN));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.actuator;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.LocalManagementPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.hasEntry;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "management.port=0")
|
||||
public class LoginServiceIntegrationTest {
|
||||
|
||||
@LocalManagementPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private LoginServiceImpl loginService;
|
||||
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Test
|
||||
public void whenLoginIsAdmin_thenSuccessCounterIsIncremented() throws IOException {
|
||||
boolean success = loginService.login("admin", "secret".toCharArray());
|
||||
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:" + port + "/metrics", String.class);
|
||||
Map<String, Object> response = objectMapper.readValue(entity.getBody(), new TypeReference<HashMap<String, Object>>() {
|
||||
});
|
||||
|
||||
assertThat(success, is(true));
|
||||
assertThat(entity.getStatusCode(), is(HttpStatus.OK));
|
||||
assertThat(response, hasEntry("counter.login.success", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoginIsNotAdmin_thenFailureCounterIsIncremented() throws IOException {
|
||||
boolean success = loginService.login("user", "notsecret".toCharArray());
|
||||
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:" + port + "/metrics", String.class);
|
||||
Map<String, Object> response = objectMapper.readValue(entity.getBody(), new TypeReference<HashMap<String, Object>>() {
|
||||
});
|
||||
|
||||
assertThat(success, is(false));
|
||||
assertThat(entity.getStatusCode(), is(HttpStatus.OK));
|
||||
assertThat(response, hasEntry("counter.login.failure", 1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.actuator;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.metrics.CounterService;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = LoginServiceImpl.class)
|
||||
public class LoginServiceUnitTest {
|
||||
|
||||
@MockBean
|
||||
CounterService counterService;
|
||||
|
||||
@Autowired
|
||||
LoginServiceImpl loginService;
|
||||
|
||||
@Test
|
||||
public void whenLoginUserIsAdmin_thenSuccessCounterIsIncremented() {
|
||||
boolean loginResult = loginService.login("admin", "secret".toCharArray());
|
||||
assertThat(loginResult, is(true));
|
||||
verify(counterService, times(1)).increment("counter.login.success");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoginUserIsNotAdmin_thenFailureCounterIsIncremented() {
|
||||
boolean loginResult = loginService.login("user", "notsecret".toCharArray());
|
||||
assertThat(loginResult, is(false));
|
||||
verify(counterService, times(1)).increment("counter.login.failure");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user