From 5da1366949712d5bf587b621244835fed08848c1 Mon Sep 17 00:00:00 2001 From: ahyoon99 Date: Sat, 29 Aug 2020 18:01:59 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20api=20=EA=B4=80=EB=A0=A8=20tc=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#21)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/web/chat/backend/entity/Room.java | 8 ++++ .../RoomControllerIntegrationTest.java | 19 +++++++- .../controller/RoomControllerTest.java | 45 ++++++++++++++++++- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/main/java/web/chat/backend/entity/Room.java b/src/main/java/web/chat/backend/entity/Room.java index 9f3037a..3c3057a 100644 --- a/src/main/java/web/chat/backend/entity/Room.java +++ b/src/main/java/web/chat/backend/entity/Room.java @@ -7,12 +7,20 @@ import javax.persistence.Id; import javax.validation.constraints.Size; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; +@NoArgsConstructor +@AllArgsConstructor(access = AccessLevel.PRIVATE) @Getter @Setter @Entity +@Builder + public class Room { @Id diff --git a/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java b/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java index 5e5e3d9..6ea93d5 100644 --- a/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java +++ b/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java @@ -14,14 +14,18 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; import org.springframework.test.context.TestConstructor; import org.springframework.test.context.jdbc.Sql; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.filter.CharacterEncodingFilter; import com.fasterxml.jackson.databind.ObjectMapper; + import lombok.RequiredArgsConstructor; +import web.chat.backend.controller.request.RoomRequest; import web.chat.backend.exception.NotFoundException; /** @@ -32,10 +36,9 @@ @RequiredArgsConstructor @SpringBootTest class RoomControllerIntegrationTest { - MockMvc mockMvc; final ObjectMapper objectMapper; - final RoomController roomController; + MockMvc mockMvc; @BeforeEach void setup() { @@ -82,8 +85,20 @@ void getRooms() throws Exception { @Test void createRoom() throws Exception { + RoomRequest req = new RoomRequest(); + req.setTitle("title"); + + final String body = objectMapper.writeValueAsString(req); + // when + ResultActions action = mockMvc.perform( + post("/api/rooms") + .contentType(MediaType.APPLICATION_JSON) + .content(body)); // then + action.andExpect(status().isCreated()) + .andDo(print()); + } } diff --git a/src/test/java/web/chat/backend/controller/RoomControllerTest.java b/src/test/java/web/chat/backend/controller/RoomControllerTest.java index 55d3591..d6201c8 100644 --- a/src/test/java/web/chat/backend/controller/RoomControllerTest.java +++ b/src/test/java/web/chat/backend/controller/RoomControllerTest.java @@ -1,14 +1,28 @@ package web.chat.backend.controller; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.BDDMockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; import org.springframework.test.context.TestConstructor; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.web.bind.MethodArgumentNotValidException; import com.fasterxml.jackson.databind.ObjectMapper; + import lombok.RequiredArgsConstructor; +import web.chat.backend.controller.request.RoomRequest; +import web.chat.backend.controller.response.RoomResponse; +import web.chat.backend.entity.Room; import web.chat.backend.service.MessageService; import web.chat.backend.service.RoomService; @@ -40,10 +54,29 @@ void getRooms() throws Exception { void createRoom() throws Exception { // given + RoomRequest req = new RoomRequest(); + req.setTitle("title1"); + + final String body = objectMapper.writeValueAsString(req); + + Room room = Room.builder() + .title(req.getTitle()) + .build(); + + String expected = objectMapper.writeValueAsString(RoomResponse.create(room)); + + given(roomService.createRoom(any())).willReturn(room); // when + ResultActions action = mockMvc.perform( + post("/api/rooms") + .contentType(MediaType.APPLICATION_JSON) + .content(body)); // then + action.andExpect(status().isCreated()) + .andExpect(content().string(expected)) + .andDo(print()); } @DisplayName("Room의 title 길이 2미만 불가능") @@ -51,10 +84,20 @@ void createRoom() throws Exception { void createRoom_titleLengthLessThan_2() throws Exception { // given + RoomRequest req = new RoomRequest(); + req.setTitle("1"); - // when + final String body = objectMapper.writeValueAsString(req); + // when + ResultActions action = mockMvc.perform( + post("/api/rooms") + .contentType(MediaType.APPLICATION_JSON) + .content(body)); // then + action.andExpect(status().is4xxClientError()) + .andExpect(result -> assertTrue(result.getResolvedException() instanceof MethodArgumentNotValidException)) + .andDo(print()); } @DisplayName("Room의 title 길이 20초과 불가능") From a8cf024ff131c9c8a10e0da2fb844b364a17ff82 Mon Sep 17 00:00:00 2001 From: ahyoon99 Date: Sat, 29 Aug 2020 19:56:20 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20api=20=EA=B4=80=EB=A0=A8=20tc=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80(#21)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/resources/application.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index bf608b7..a2a1d6c 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -1,7 +1,7 @@ spring: datasource: driver-class-name: org.h2.Driver - url: jdbc:h2:~/apps/h2db/crm:webchat;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE + url: jdbc:h2:file:~/apps/h2db/csrm/webchat;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE username: sa password: sa sql-script-encoding: UTF-8