diff --git a/httpClients/boot-rest-template/pom.xml b/httpClients/boot-rest-template/pom.xml index 400a4dfa4..a4e09309b 100644 --- a/httpClients/boot-rest-template/pom.xml +++ b/httpClients/boot-rest-template/pom.xml @@ -20,7 +20,7 @@ 21 2.3.0 - + ${project.build.directory}/test-results 2.43.0 9.0.9 @@ -331,25 +331,25 @@ - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + diff --git a/httpClients/boot-restclient/pom.xml b/httpClients/boot-restclient/pom.xml index 739b3e474..347932d7e 100644 --- a/httpClients/boot-restclient/pom.xml +++ b/httpClients/boot-restclient/pom.xml @@ -86,6 +86,49 @@ + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + alphabetical + ${junit.utReportFolder} + + **/*IT* + **/*IntTest* + **/*IntegrationTest* + + + + + org.apache.maven.plugins + maven-failsafe-plugin + ${maven-failsafe-plugin.version} + + ${project.build.outputDirectory} + alphabetical + ${junit.itReportFolder} + + **/*IT* + **/*IntTest* + **/*IntegrationTest* + + + + + integration-test + + integration-test + + + + verify + + verify + + + + diff --git a/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/model/response/PostDto.java b/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/model/response/PostDto.java index 9b0719b5c..5109c4a21 100644 --- a/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/model/response/PostDto.java +++ b/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/model/response/PostDto.java @@ -6,8 +6,4 @@ public record PostDto( Long userId, Long id, @NotBlank(message = "title can't be blank") String title, - String body) { - public PostDto withId(Long id) { - return new PostDto(userId(), id, title(), body()); - } -} + String body) {} diff --git a/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerIntTest.java b/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerIntTest.java index 73adac15e..2983e874a 100644 --- a/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerIntTest.java +++ b/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerIntTest.java @@ -1,14 +1,20 @@ package com.example.restclient.bootrestclient.web.controllers; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.is; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.Matchers.hasSize; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import com.example.restclient.bootrestclient.model.response.PostDto; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; 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.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; @SpringBootTest @@ -16,6 +22,7 @@ class PostControllerIntTest { @Autowired private MockMvc mockMvc; + @Autowired private ObjectMapper objectMapper; @Test void shouldFindPostById() throws Exception { @@ -36,4 +43,73 @@ void shouldFindPostById() throws Exception { is( "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"))); } + + @Test + void shouldCreateNewPost() throws Exception { + PostDto postDto = new PostDto(1L, null, "First Title", "First Body"); + this.mockMvc + .perform( + post("/api/posts") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(postDto))) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.id", notNullValue())) + .andExpect(jsonPath("$.title", is(postDto.title()))) + .andExpect(jsonPath("$.body", is(postDto.body()))) + .andExpect(jsonPath("$.userId", is(postDto.userId()), Long.class)); + } + + @Test + void shouldReturn400WhenCreateNewPostWithoutTitle() throws Exception { + PostDto post = new PostDto(1L, null, null, "First Body"); + + this.mockMvc + .perform( + post("/api/posts") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(post))) + .andExpect(status().isBadRequest()) + .andExpect( + header().string( + HttpHeaders.CONTENT_TYPE, + is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) + .andExpect(jsonPath("$.type", is("about:blank"))) + .andExpect(jsonPath("$.title", is("Constraint Violation"))) + .andExpect(jsonPath("$.status", is(400))) + .andExpect(jsonPath("$.detail", is("Invalid request content."))) + .andExpect(jsonPath("$.instance", is("/api/posts"))) + .andExpect(jsonPath("$.violations", hasSize(1))) + .andExpect(jsonPath("$.violations[0].field", is("title"))) + .andExpect(jsonPath("$.violations[0].message", is("title can't be blank"))) + .andReturn(); + } + + @Test + void shouldUpdatePost() throws Exception { + PostDto postDto = new PostDto(1L, 1L, "First Title", "First Body"); + + this.mockMvc + .perform( + put("/api/posts/{id}", postDto.id()) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(postDto))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", is(postDto.id()), Long.class)) + .andExpect(jsonPath("$.title", is(postDto.title()))) + .andExpect(jsonPath("$.body", is(postDto.body()))) + .andExpect(jsonPath("$.userId", is(postDto.userId()), Long.class)); + } + + @Test + void shouldDeletePost() throws Exception { + + String response = + this.mockMvc + .perform(delete("/api/posts/{id}", 50)) + .andExpect(status().isOk()) + .andReturn() + .getResponse() + .getContentAsString(); + assertThat(response).isEqualTo("{}"); + } }