Skip to content

Commit

Permalink
Merge pull request #22 from LandvibeDev/tc/make-room
Browse files Browse the repository at this point in the history
채팅방 생성 api 관련 tc 추가 (#21)
  • Loading branch information
ahyoon99 authored Aug 29, 2020
2 parents 4f65af2 + 6b4b360 commit ce7b639
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/web/chat/backend/entity/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
@Getter
@Setter
@Entity
@Builder

public class Room {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand All @@ -24,6 +25,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.RequiredArgsConstructor;
import web.chat.backend.controller.request.RoomRequest;
import web.chat.backend.exception.NotFoundException;

/**
Expand Down Expand Up @@ -86,8 +88,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());

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package web.chat.backend.controller;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
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;

Expand All @@ -24,6 +27,8 @@

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;
Expand Down Expand Up @@ -79,21 +84,50 @@ 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미만 불가능")
@Test
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초과 불가능")
Expand Down

0 comments on commit ce7b639

Please sign in to comment.