Skip to content

Commit

Permalink
Merge pull request #23 from LandvibeDev/tc/get-rooms
Browse files Browse the repository at this point in the history
채팅방 목록 내려주는 api 관련 tc 추가 (#20)
  • Loading branch information
KwonYeKyeong authored Aug 29, 2020
2 parents 04e23e6 + c42dd34 commit 4f65af2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
Empty file added apps/h2db/crm
Empty file.
7 changes: 7 additions & 0 deletions src/main/java/web/chat/backend/entity/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,10 +34,9 @@
@RequiredArgsConstructor
@SpringBootTest
class RoomControllerIntegrationTest {
MockMvc mockMvc;
final ObjectMapper objectMapper;

final RoomController roomController;
MockMvc mockMvc;

@BeforeEach
void setup() {
Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/web/chat/backend/controller/RoomControllerTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Room> 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
Expand Down Expand Up @@ -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());

}
}
2 changes: 1 addition & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4f65af2

Please sign in to comment.