-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 변경된 피드 컨트롤러에 맞게 테스트 수정 - 경로 변수 및 입력 값 검증 테스트 추가 Related to: #21
- Loading branch information
1 parent
11815f2
commit 2697ff1
Showing
1 changed file
with
112 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.prgrms2.java.bitta.feed.controller.FeedController; | ||
import com.prgrms2.java.bitta.feed.dto.FeedDTO; | ||
import com.prgrms2.java.bitta.feed.exception.FeedException; | ||
import com.prgrms2.java.bitta.feed.service.FeedService; | ||
import com.prgrms2.java.bitta.token.util.JWTUtil; | ||
import org.junit.jupiter.api.BeforeAll; | ||
|
@@ -22,10 +23,11 @@ | |
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
|
@@ -46,38 +48,38 @@ public class FeedControllerTests { | |
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
private static FeedDTO feedDTO; | ||
private FeedDTO feedDTO; | ||
|
||
private static List<FeedDTO> feedDTOList; | ||
private List<FeedDTO> feedDTOList; | ||
|
||
@BeforeAll | ||
static void initialize() { | ||
void initialize() { | ||
feedDTO = FeedDTO.builder() | ||
.feedId(1L) | ||
.id(1L) | ||
.title("Title") | ||
.content("Content") | ||
.createdAt(LocalDateTime.now()) | ||
.memberId(1L) | ||
.email("[email protected]") | ||
.build(); | ||
|
||
feedDTOList = new ArrayList<>(); | ||
IntStream.rangeClosed(1, 5).forEach(i -> feedDTOList.add(FeedDTO.builder() | ||
.feedId((long) i) | ||
.id((long) i) | ||
.title("Title" + i) | ||
.content("Content" + i) | ||
.createdAt(LocalDateTime.now()) | ||
.memberId((long) i) | ||
.email("[email protected]") | ||
.build())); | ||
} | ||
|
||
private ResultMatcher[] generateResultMatchers(FeedDTO feedDTO) { | ||
List<ResultMatcher> resultMatchers = new ArrayList<>(); | ||
String prefix = "$."; | ||
String prefix = "$.result."; | ||
|
||
resultMatchers.add(jsonPath(prefix + "feedId").value(feedDTO.getFeedId())); | ||
resultMatchers.add(jsonPath(prefix + "id").value(feedDTO.getId())); | ||
resultMatchers.add(jsonPath(prefix + "title").value(feedDTO.getTitle())); | ||
resultMatchers.add(jsonPath(prefix + "content").value(feedDTO.getContent())); | ||
resultMatchers.add(jsonPath(prefix + "memberId").value(feedDTO.getMemberId())); | ||
resultMatchers.add(jsonPath(prefix + "email").value(feedDTO.getEmail())); | ||
resultMatchers.add(jsonPath(prefix + "createdAt").value(feedDTO.getCreatedAt())); | ||
|
||
return resultMatchers.toArray(ResultMatcher[]::new); | ||
|
@@ -88,12 +90,12 @@ private ResultMatcher[] generateResultMatchers(List<FeedDTO> feedDTOList) { | |
|
||
for (int i = 0; i < feedDTOList.size(); i++) { | ||
FeedDTO feedDTO = feedDTOList.get(i); | ||
String prefix = String.format("$[%d].", i); | ||
String prefix = String.format("$result.[%d].", i); | ||
|
||
resultMatchers.add(jsonPath(prefix + "feedId").value(feedDTO.getFeedId())); | ||
resultMatchers.add(jsonPath(prefix + "id").value(feedDTO.getId())); | ||
resultMatchers.add(jsonPath(prefix + "title").value(feedDTO.getTitle())); | ||
resultMatchers.add(jsonPath(prefix + "content").value(feedDTO.getContent())); | ||
resultMatchers.add(jsonPath(prefix + "memberId").value(feedDTO.getMemberId())); | ||
resultMatchers.add(jsonPath(prefix + "email").value(feedDTO.getEmail())); | ||
resultMatchers.add(jsonPath(prefix + "createdAt").value(feedDTO.getCreatedAt())); | ||
} | ||
|
||
|
@@ -109,6 +111,8 @@ void getFeed_FeedExists_ReturnList() throws Exception { | |
mockMvc.perform(get("/api/v1/feed")) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.message") | ||
.value("피드를 성공적으로 조회했습니다.")) | ||
.andExpectAll(generateResultMatchers(feedDTOList)); | ||
} | ||
|
||
|
@@ -120,65 +124,93 @@ void getFeed_FeedNotExists_NotFound() throws Exception { | |
|
||
mockMvc.perform(get("/api/v1/feed")) | ||
.andDo(print()) | ||
.andExpect(status().isNotFound()); | ||
.andExpect(status().isNotFound()) | ||
.andExpect(jsonPath("$.error") | ||
.value("피드가 존재하지 않습니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 단일 조회 (성공)") | ||
void getFeedById_FeedExists_ReturnFeed() throws Exception { | ||
given(feedService.read(anyLong())) | ||
.willReturn(Optional.of(feedDTO)); | ||
.willReturn(feedDTO); | ||
|
||
mockMvc.perform(get("/api/v1/feed/1")) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.message") | ||
.value("피드를 성공적으로 조회했습니다.")) | ||
.andExpectAll(generateResultMatchers(feedDTO)); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 단일 조회 (실패) :: 검색 결과 없음") | ||
void getFeedById_FeedNotExists_HttpStatusNotFound() throws Exception { | ||
given(feedService.read(anyLong())) | ||
.willReturn(Optional.empty()); | ||
.willThrow(FeedException.CANNOT_FOUND.get()); | ||
|
||
mockMvc.perform(get("/api/v1/feed/1")) | ||
.andDo(print()) | ||
.andExpect(status().isNotFound()); | ||
.andExpect(status().isNotFound()) | ||
.andExpect(jsonPath("$.error") | ||
.value("피드가 존재하지 않습니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 등록 (성공)") | ||
void createFeed_FeedNotDuplicated_ReturnMessage() throws Exception { | ||
given(feedService.insert(any(FeedDTO.class))) | ||
.willReturn("Result Message"); | ||
doNothing().when(feedService).insert(any(FeedDTO.class)); | ||
|
||
String content = objectMapper.writeValueAsString(feedDTO); | ||
|
||
mockMvc.perform(post("/api/v1/feed/1") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
mockMvc.perform(post("/api/v1/feed") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.message").value("Result Message")); | ||
.andExpect(status().isCreated()) | ||
.andExpect(jsonPath("$.message") | ||
.value("피드가 등록되었습니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 등록 (실패) :: DTO 검증 실패") | ||
void createFeed_FeedDtoIsEmpty_HttpStatusNotFound() throws Exception { | ||
@DisplayName("피드 등록 (실패) :: ID 값이 존재함") | ||
void createFeed_IdExistsInDto_HttpStatusBadRequest() throws Exception { | ||
doThrow(FeedException.BAD_REQUEST.get()) | ||
.when(feedService).insert(any(FeedDTO.class)); | ||
|
||
String content = objectMapper.writeValueAsString(FeedDTO.builder().build()); | ||
|
||
mockMvc.perform(post("/api/v1/feed/1") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
mockMvc.perform(post("/api/v1/feed") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
.andDo(print()) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.error") | ||
.value("잘못된 요청입니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 등록 (실패) :: DTO 검증 실패") | ||
void createFeed_WrongRequestDto_ThrowMethodArgumentNotValidException() throws Exception { | ||
FeedDTO emptyDto = FeedDTO.builder().build(); | ||
|
||
String content = objectMapper.writeValueAsString(emptyDto); | ||
|
||
mockMvc.perform(put("/api/v1/feed") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
.andDo(print()) | ||
.andExpect(status().isNotFound()); | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.error") | ||
.value("잘못된 요청입니다.")) | ||
.andExpect(jsonPath("$.reason") | ||
.value("제목은 비워둘 수 없습니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 수정 (성공)") | ||
void modifyFeed_FeedDtoIsNotEmpty_ReturnFeedDto() throws Exception { | ||
given(feedService.update(any(FeedDTO.class))) | ||
.willReturn(Optional.of(feedDTO)); | ||
doNothing().when(feedService).update(any(FeedDTO.class)); | ||
|
||
String content = objectMapper.writeValueAsString(feedDTO); | ||
|
||
|
@@ -187,43 +219,79 @@ void modifyFeed_FeedDtoIsNotEmpty_ReturnFeedDto() throws Exception { | |
.content(content)) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpectAll(generateResultMatchers(feedDTO)); | ||
.andExpect(jsonPath("$.message") | ||
.value("피드가 수정되었습니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 수정 (실패) :: DTO 검증 실패") | ||
@DisplayName("피드 수정 (실패) :: 검색 결과 없음") | ||
void modifyFeed_FeedDtoIsNotEmpty_HttpStatusBadRequest() throws Exception { | ||
given(feedService.update(any(FeedDTO.class))) | ||
.willReturn(Optional.empty()); | ||
doThrow(FeedException.CANNOT_FOUND.get()) | ||
.when(feedService).update(any(FeedDTO.class)); | ||
|
||
String content = objectMapper.writeValueAsString(feedDTO); | ||
|
||
mockMvc.perform(put("/api/v1/feed/1") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
.andDo(print()) | ||
.andExpect(status().isBadRequest()); | ||
.andExpect(status().isNotFound()) | ||
.andExpect(jsonPath("$.error") | ||
.value("피드가 존재하지 않습니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 수정 (실패) :: DTO 검증 실패") | ||
void modifyFeed_WrongRequestDto_ThrowMethodArgumentNotValidException() throws Exception { | ||
FeedDTO emptyDto = FeedDTO.builder().build(); | ||
|
||
String content = objectMapper.writeValueAsString(emptyDto); | ||
|
||
mockMvc.perform(put("/api/v1/feed/1") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
.andDo(print()) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.error") | ||
.value("잘못된 요청입니다.")) | ||
.andExpect(jsonPath("$.reason") | ||
.value("제목은 비워둘 수 없습니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 삭제 (성공)") | ||
void deleteFeed_FeedExists_HttpStatusNoContent() throws Exception { | ||
given(feedService.delete(anyLong())) | ||
.willReturn(true); | ||
doNothing().when(feedService).delete(anyLong()); | ||
|
||
mockMvc.perform(delete("/api/v1/feed/1")) | ||
.andDo(print()) | ||
.andExpect(status().isNoContent()); | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.message") | ||
.value("피드가 삭제되었습니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 삭제 (실패) :: 검색 결과 없음") | ||
void deleteFeed_FeedNotExists_HttpStatusNotFound() throws Exception { | ||
given(feedService.delete(anyLong())) | ||
.willReturn(false); | ||
doThrow(FeedException.CANNOT_DELETE.get()) | ||
.when(feedService).delete(anyLong()); | ||
|
||
mockMvc.perform(delete("/api/v1/feed/1")) | ||
.andDo(print()) | ||
.andExpect(status().isNotFound()); | ||
.andExpect(status().isNotFound()) | ||
.andExpect(jsonPath("$.error") | ||
.value("삭제할 피드가 존재하지 않습니다")); | ||
} | ||
|
||
@Test | ||
@DisplayName("피드 삭제 (실패) :: 잘못된 경로 변수") | ||
void deleteFeed_WrongPathVariable_ThrowConstraintViolationException() throws Exception { | ||
mockMvc.perform(delete("/api/v1/feed/-1")) | ||
.andDo(print()) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.error") | ||
.value("잘못된 요청입니다.")) | ||
.andExpect(jsonPath("$.reason") | ||
.value("ID는 음수가 될 수 없습니다.")); | ||
} | ||
} |