From 19b08c13bba5b5b8f09870a85ca34ca49c341c4e Mon Sep 17 00:00:00 2001 From: KwonYeKyeong Date: Sat, 29 Aug 2020 18:09:47 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EC=B1=84=ED=8C=85=EB=B0=A9=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EB=82=B4=EB=A0=A4=EC=A3=BC=EB=8A=94=20api=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20tc=20=EC=B6=94=EA=B0=80=20(#20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/web/chat/backend/entity/Room.java | 7 +++ .../RoomControllerIntegrationTest.java | 8 ++- .../controller/RoomControllerTest.java | 51 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 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..610f1c1 100644 --- a/src/main/java/web/chat/backend/entity/Room.java +++ b/src/main/java/web/chat/backend/entity/Room.java @@ -7,9 +7,16 @@ 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) +@Builder @Getter @Setter @Entity diff --git a/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java b/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java index 5e5e3d9..3a572e8 100644 --- a/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java +++ b/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java @@ -17,10 +17,12 @@ 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.exception.NotFoundException; @@ -32,10 +34,9 @@ @RequiredArgsConstructor @SpringBootTest class RoomControllerIntegrationTest { - MockMvc mockMvc; final ObjectMapper objectMapper; - final RoomController roomController; + MockMvc mockMvc; @BeforeEach void setup() { @@ -75,8 +76,11 @@ void shouldRespond400StatusCode_ifNotFoundRoom() throws Exception { void getRooms() throws Exception { // when + ResultActions resultActions = mockMvc.perform(get("/api/rooms")); // then + resultActions.andExpect(status().isOk()) + .andDo(print()); } @Test diff --git a/src/test/java/web/chat/backend/controller/RoomControllerTest.java b/src/test/java/web/chat/backend/controller/RoomControllerTest.java index 55d3591..8ce9d3d 100644 --- a/src/test/java/web/chat/backend/controller/RoomControllerTest.java +++ b/src/test/java/web/chat/backend/controller/RoomControllerTest.java @@ -1,14 +1,30 @@ package web.chat.backend.controller; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.*; +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 java.util.Arrays; +import java.util.List; + 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.entity.Room; import web.chat.backend.service.MessageService; import web.chat.backend.service.RoomService; @@ -30,10 +46,33 @@ class RoomControllerTest { void getRooms() throws Exception { // given + Room r1 = Room.builder() + .id(1L) + .title("test1") + .build(); + Room r2 = Room.builder() + .id(2L) + .title("test2") + .build(); + Room r3 = Room.builder() + .id(3L) + .title("test3") + .build(); + List roomList = Arrays.asList(r1, r2, r3); + + given(roomService.getRooms()).willReturn(roomList); // when + ResultActions resultActions = mockMvc.perform(get("/api/rooms")); // then + resultActions.andExpect(status().isOk()) + .andExpect(jsonPath("$.rooms", not(empty()))) + .andExpect(jsonPath("$.rooms", hasSize(3))) + .andExpect(jsonPath("$.rooms[0].id", is(1))) + .andExpect(jsonPath("$.rooms[1].id", is(2))) + .andExpect(jsonPath("$.rooms[2].id", is(3))) + .andDo(print()); } @Test @@ -62,9 +101,21 @@ void createRoom_titleLengthLessThan_2() throws Exception { void createRoom_titleLengthExceed_20() throws Exception { // given + RoomRequest req = new RoomRequest(); + req.setTitle("title length exceeds 20"); + + final String body = objectMapper.writeValueAsString(req); // when + ResultActions resultActions = mockMvc.perform( + post("/api/rooms") + .contentType(MediaType.APPLICATION_JSON) + .content(body)); // then + resultActions.andExpect(status().is4xxClientError()) + .andExpect(result -> assertTrue(result.getResolvedException() instanceof MethodArgumentNotValidException)) + .andDo(print()); + } } From c42dd34e6bba56b20125dd2b4aa2f1b1aae6d0f6 Mon Sep 17 00:00:00 2001 From: KwonYeKyeong Date: Sat, 29 Aug 2020 19:48:42 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20datasource=20url=20=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/h2db/crm | 0 src/test/resources/application.yml | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 apps/h2db/crm diff --git a/apps/h2db/crm b/apps/h2db/crm new file mode 100644 index 0000000..e69de29 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