TSK-1135 Removed JUnit 4 dependency in taskana-simplehistory-spring-test

TSK-1135 Changed tests to assertThatThrownBy tests
This commit is contained in:
Sofie Hofmann 2020-02-20 13:16:17 +01:00
parent 9d54d9f526
commit 6358f52dda
3 changed files with 71 additions and 82 deletions

View File

@ -82,12 +82,6 @@
<version>${version.junit.jupiter}</version> <version>${version.junit.jupiter}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${version.junit.vintage.engine}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>

View File

@ -1,9 +1,7 @@
package pro.taskana; package pro.taskana;
import static org.junit.Assert.assertEquals; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull; import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -11,9 +9,9 @@ import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collections; import java.util.Collections;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -29,9 +27,8 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -42,7 +39,7 @@ import pro.taskana.rest.simplehistory.sampledata.SampleDataGenerator;
/** Controller for integration test. */ /** Controller for integration test. */
@EnableAutoConfiguration @EnableAutoConfiguration
@RunWith(SpringRunner.class) @ExtendWith(SpringExtension.class)
@SpringBootTest( @SpringBootTest(
classes = {TaskHistoryRestConfiguration.class}, classes = {TaskHistoryRestConfiguration.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ -64,8 +61,8 @@ public class TaskHistoryEventControllerIntTest {
@Autowired private DataSource dataSource; @Autowired private DataSource dataSource;
@Before @BeforeEach
public void before() { public void beforeEach() {
template = getRestTemplate(); template = getRestTemplate();
SampleDataGenerator sampleDataGenerator; SampleDataGenerator sampleDataGenerator;
try { try {
@ -84,8 +81,8 @@ public class TaskHistoryEventControllerIntTest {
HttpMethod.GET, HttpMethod.GET,
request, request,
ParameterizedTypeReference.forType(TaskHistoryEventListResource.class)); ParameterizedTypeReference.forType(TaskHistoryEventListResource.class));
assertNotNull(response.getBody().getLink(Link.REL_SELF)); assertThat(response.getBody().getLink(Link.REL_SELF)).isNotNull();
assertEquals(50, response.getBody().getContent().size()); assertThat(response.getBody().getContent()).hasSize(50);
} }
@Test @Test
@ -98,9 +95,9 @@ public class TaskHistoryEventControllerIntTest {
HttpMethod.GET, HttpMethod.GET,
request, request,
ParameterizedTypeReference.forType(TaskHistoryEventListResource.class)); ParameterizedTypeReference.forType(TaskHistoryEventListResource.class));
assertNotNull(response.getBody().getLink(Link.REL_SELF)); assertThat(response.getBody().getLink(Link.REL_SELF)).isNotNull();
assertEquals(3, response.getBody().getContent().size()); assertThat(response.getBody().getContent()).hasSize(3);
assertTrue(response.getBody().getLink(Link.REL_SELF).getHref().endsWith(parameters)); assertThat(response.getBody().getLink(Link.REL_SELF).getHref().endsWith(parameters)).isTrue();
} }
@Test @Test
@ -115,42 +112,42 @@ public class TaskHistoryEventControllerIntTest {
request, request,
ParameterizedTypeReference.forType(TaskHistoryEventListResource.class)); ParameterizedTypeReference.forType(TaskHistoryEventListResource.class));
assertNotNull(response.getBody().getLink(Link.REL_SELF)); assertThat(response.getBody().getLink(Link.REL_SELF)).isNotNull();
assertNotNull(response.getBody().getLinks()); assertThat(response.getBody().getLinks()).isNotNull();
assertNotNull(response.getBody().getMetadata()); assertThat(response.getBody().getMetadata()).isNotNull();
assertEquals(1, response.getBody().getContent().size()); assertThat(response.getBody().getContent()).hasSize(1);
} }
@Test @Test
public void testThrowsExceptionIfInvalidFilterIsUsed() { public void testThrowsExceptionIfInvalidFilterIsUsed() {
try { assertThatThrownBy(
() ->
template.exchange( template.exchange(
server + port + "/api/v1/task-history-event?invalid=BPI:01", server + port + "/api/v1/task-history-event?invalid=BPI:01",
HttpMethod.GET, HttpMethod.GET,
request, request,
ParameterizedTypeReference.forType(TaskHistoryEventListResource.class)); ParameterizedTypeReference.forType(TaskHistoryEventListResource.class)))
fail(); .isInstanceOf(HttpClientErrorException.class)
} catch (HttpClientErrorException e) { .hasMessageContaining("[invalid]")
assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode()); .extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
assertTrue(e.getResponseBodyAsString().contains("[invalid]")); .isEqualTo(HttpStatus.BAD_REQUEST);
}
} }
@Test @Test
public void testGetHistoryEventOfDate() { public void testGetHistoryEventOfDate() {
String currentTime = LocalDateTime.now().toString(); String currentTime = LocalDateTime.now().toString();
final String finalCurrentTime = currentTime;
try { assertThatThrownBy(
() ->
template.exchange( template.exchange(
server + port + "/api/v1/task-history-event?created=" + currentTime, server + port + "/api/v1/task-history-event?created=" + finalCurrentTime,
HttpMethod.GET, HttpMethod.GET,
request, request,
ParameterizedTypeReference.forType(TaskHistoryEventListResource.class)); ParameterizedTypeReference.forType(TaskHistoryEventListResource.class)))
fail(); .isInstanceOf(HttpClientErrorException.class)
} catch (HttpClientErrorException e) { .hasMessageContaining(currentTime)
assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode()); .extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
assertTrue(e.getResponseBodyAsString().contains(currentTime)); .isEqualTo(HttpStatus.BAD_REQUEST);
}
// correct Format 'yyyy-MM-dd' // correct Format 'yyyy-MM-dd'
currentTime = currentTime.substring(0, 10); currentTime = currentTime.substring(0, 10);
@ -160,8 +157,8 @@ public class TaskHistoryEventControllerIntTest {
HttpMethod.GET, HttpMethod.GET,
request, request,
ParameterizedTypeReference.forType(TaskHistoryEventListResource.class)); ParameterizedTypeReference.forType(TaskHistoryEventListResource.class));
assertNotNull(response.getBody().getLink(Link.REL_SELF)); assertThat(response.getBody().getLink(Link.REL_SELF)).isNotNull();
assertEquals(25, response.getBody().getContent().size()); assertThat(response.getBody().getContent()).hasSize(25);
} }
@Test @Test
@ -175,21 +172,21 @@ public class TaskHistoryEventControllerIntTest {
request, request,
ParameterizedTypeReference.forType(TaskHistoryEventListResource.class)); ParameterizedTypeReference.forType(TaskHistoryEventListResource.class));
assertEquals(2, response.getBody().getContent().size()); assertThat(response.getBody().getContent()).hasSize(2);
assertEquals( assertThat(response.getBody().getContent().iterator().next().getWorkbasketKey())
"WBI:100000000000000000000000000000000002", .isEqualTo("WBI:100000000000000000000000000000000002");
response.getBody().getContent().iterator().next().getWorkbasketKey()); assertThat(response.getBody().getLink(Link.REL_SELF)).isNotNull();
assertNotNull(response.getBody().getLink(Link.REL_SELF)); assertThat(response.getBody().getLink(Link.REL_SELF).getHref().endsWith(parameters)).isTrue();
assertTrue(response.getBody().getLink(Link.REL_SELF).getHref().endsWith(parameters)); assertThat(response.getBody().getLink("allTaskHistoryEvent")).isNotNull();
assertNotNull(response.getBody().getLink("allTaskHistoryEvent")); assertThat(
assertTrue(
response response
.getBody() .getBody()
.getLink("allTaskHistoryEvent") .getLink("allTaskHistoryEvent")
.getHref() .getHref()
.endsWith("/api/v1/task-history-event")); .endsWith("/api/v1/task-history-event"))
assertNotNull(response.getBody().getLink(Link.REL_FIRST)); .isTrue();
assertNotNull(response.getBody().getLink(Link.REL_LAST)); assertThat(response.getBody().getLink(Link.REL_FIRST)).isNotNull();
assertThat(response.getBody().getLink(Link.REL_LAST)).isNotNull();
} }
/** /**
@ -206,8 +203,6 @@ public class TaskHistoryEventControllerIntTest {
converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json")); converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
converter.setObjectMapper(mapper); converter.setObjectMapper(mapper);
RestTemplate template = return new RestTemplate(Collections.singletonList(converter));
new RestTemplate(Collections.<HttpMessageConverter<?>>singletonList(converter));
return template;
} }
} }

View File

@ -10,18 +10,18 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.response
import static org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath; import static org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath;
import java.util.HashMap; import java.util.HashMap;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Rule; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.restdocs.JUnitRestDocumentation; import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.restdocs.payload.FieldDescriptor; import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@ -30,33 +30,33 @@ import org.springframework.web.context.WebApplicationContext;
import pro.taskana.rest.simplehistory.TaskHistoryRestConfiguration; import pro.taskana.rest.simplehistory.TaskHistoryRestConfiguration;
/** Generate documentation for the history event controller. */ /** Generate documentation for the history event controller. */
@RunWith(SpringRunner.class)
@SpringBootTest( @SpringBootTest(
classes = TaskHistoryRestConfiguration.class, classes = TaskHistoryRestConfiguration.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
public class TaskHistoryEventControllerRestDocumentation { public class TaskHistoryEventControllerRestDocumentation {
@Rule public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(); public RestDocumentationExtension restDocumentation = new RestDocumentationExtension();
@LocalServerPort int port; @LocalServerPort int port;
@Autowired private WebApplicationContext context; @Autowired private WebApplicationContext context;
private MockMvc mockMvc; private MockMvc mockMvc;
private HashMap<String, String> taskHistoryEventFieldDescriptionsMap = private HashMap<String, String> taskHistoryEventFieldDescriptionsMap = new HashMap<>();
new HashMap<String, String>();
private FieldDescriptor[] allTaskHistoryEventFieldDescriptors; private FieldDescriptor[] allTaskHistoryEventFieldDescriptors;
private FieldDescriptor[] taskHistoryEventFieldDescriptors; private FieldDescriptor[] taskHistoryEventFieldDescriptors;
@Before @BeforeEach
public void setUp() { public void setUp() {
document("{methodName}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint())); document("{methodName}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()));
this.mockMvc = this.mockMvc =
MockMvcBuilders.webAppContextSetup(this.context) MockMvcBuilders.webAppContextSetup(this.context)
.apply( .apply(
documentationConfiguration(this.restDocumentation) documentationConfiguration(
(RestDocumentationContextProvider) this.restDocumentation)
.operationPreprocessors() .operationPreprocessors()
.withResponseDefaults(prettyPrint()) .withResponseDefaults(prettyPrint())
.withRequestDefaults(prettyPrint())) .withRequestDefaults(prettyPrint()))