forked from codesquad-issue-team-05/issue-tracker-max
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'be' into dev-be/feature/codesquad-issue-team-05#80
- Loading branch information
Showing
9 changed files
with
570 additions
and
15 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
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
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
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
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
121 changes: 121 additions & 0 deletions
121
be/issue/src/test/java/codesquad/issueTracker/comment/controller/CommentControllerTest.java
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package codesquad.issueTracker.comment.controller; | ||
|
||
|
||
import static codesquad.issueTracker.global.exception.SuccessCode.*; | ||
import static org.mockito.BDDMockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import annotation.ControllerTest; | ||
import codesquad.issueTracker.comment.dto.CommentRequestDto; | ||
import codesquad.issueTracker.comment.dto.CommentResponseDto; | ||
import codesquad.issueTracker.comment.fixture.CommentTestFixture; | ||
import codesquad.issueTracker.comment.service.CommentService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.List; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
|
||
@ControllerTest(CommentController.class) | ||
class CommentControllerTest extends CommentTestFixture { | ||
|
||
private final Log log = LogFactory.getLog(CommentControllerTest.class); | ||
|
||
private List<CommentResponseDto> commentResponseDtosFixture; | ||
private CommentRequestDto commentRequestDtoFixture; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@MockBean | ||
private CommentService commentService; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
commentResponseDtosFixture = dummyCommentResponseDtos(); | ||
commentRequestDtoFixture = dummyCommentRequestDto(1); | ||
} | ||
|
||
@Test | ||
@DisplayName("모든 댓글 목록들을 반환한다.") | ||
public void getComments() throws Exception { | ||
//given | ||
given(commentService.getComments(any())).willReturn(commentResponseDtosFixture); | ||
|
||
//when | ||
ResultActions resultActions = mockMvc.perform(get("/api/issues/{issueId}/comments", 1L)); | ||
|
||
//then | ||
resultActions | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.status").value(SUCCESS.getStatus().getReasonPhrase())) | ||
.andExpect(jsonPath("$.message[0].id").value(commentResponseDtosFixture.get(0).getId())) | ||
.andExpect(jsonPath("$.message[0].createdAt").value(commentResponseDtosFixture.get(0).getCreatedAt().toString())) | ||
.andExpect(jsonPath("$.message[0].content").value(commentResponseDtosFixture.get(0).getContent())) | ||
.andExpect(jsonPath("$.message[0].writer.name").value(commentResponseDtosFixture.get(0).getWriter().getName())) | ||
.andExpect(jsonPath("$.message[0].writer.profileImg").value(commentResponseDtosFixture.get(0).getWriter().getProfileImg())); | ||
} | ||
|
||
@Test | ||
@DisplayName("이슈 댓글을 작성한다.") | ||
public void save() throws Exception { | ||
//given | ||
given(commentService.save(any(), any(), any())).willReturn(1L); | ||
|
||
//when | ||
ResultActions resultActions = mockMvc.perform(post("/api/issues/{issueId}/comments", 1L) | ||
.content(objectMapper.writeValueAsString(commentRequestDtoFixture)) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.requestAttr("userId", 1L)); | ||
|
||
//then | ||
resultActions.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.status").value(SUCCESS.getStatus().getReasonPhrase())) | ||
.andExpect(jsonPath("$.message").value(SUCCESS.getMessage())); | ||
} | ||
|
||
@Test | ||
@DisplayName("이슈 댓글을 수정한다.") | ||
public void modify() throws Exception { | ||
//given | ||
given(commentService.modify(any(), any())).willReturn(1L); | ||
|
||
//when | ||
ResultActions resultActions = mockMvc.perform(patch("/api/issues/comments/{commentId}", 1L) | ||
.content(objectMapper.writeValueAsString(commentRequestDtoFixture)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
//then | ||
resultActions.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.status").value(SUCCESS.getStatus().getReasonPhrase())) | ||
.andExpect(jsonPath("$.message").value(SUCCESS.getMessage())); | ||
} | ||
|
||
@Test | ||
@DisplayName("이슈 댓글을 삭제한다.") | ||
public void delete() throws Exception { | ||
//given | ||
given(commentService.delete(any())).willReturn(1L); | ||
|
||
//when | ||
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.delete("/api/issues/comments/{commentId}", 1L)); | ||
|
||
//then | ||
resultActions.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.status").value(SUCCESS.getStatus().getReasonPhrase())) | ||
.andExpect(jsonPath("$.message").value(SUCCESS.getMessage())); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
be/issue/src/test/java/codesquad/issueTracker/comment/fixture/CommentTestFixture.java
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package codesquad.issueTracker.comment.fixture; | ||
|
||
import codesquad.issueTracker.comment.domain.Comment; | ||
import codesquad.issueTracker.comment.dto.CommentRequestDto; | ||
import codesquad.issueTracker.comment.dto.CommentResponseDto; | ||
import codesquad.issueTracker.comment.vo.CommentUserVo; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public abstract class CommentTestFixture { | ||
public List<CommentResponseDto> dummyCommentResponseDtos() { | ||
return IntStream.range(1, 4) | ||
.mapToObj(this::makeCommentResponses) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private CommentResponseDto makeCommentResponses(int num) { | ||
Comment comment = dummyComment(num); | ||
CommentUserVo commentUserVo = makeCommentUser(); | ||
|
||
return CommentResponseDto.builder() | ||
.id((long) num) | ||
.content(comment.getContent()) | ||
.writer(commentUserVo) | ||
.createdAt(comment.getCreatedAt()) | ||
.build(); | ||
} | ||
|
||
public Comment dummyComment(int num) { | ||
return Comment.builder() | ||
.issueId(1L) | ||
.userId(1L) | ||
.content("comment test" + num) | ||
.createdAt(dummyLocalDateTime()) | ||
.build(); | ||
} | ||
|
||
private LocalDateTime dummyLocalDateTime() { | ||
return LocalDateTime.of(2023, 8, 12, 7, 23, 10); | ||
} | ||
|
||
private CommentUserVo makeCommentUser() { | ||
return CommentUserVo.builder() | ||
.name("sio") | ||
.profileImg("http://image.png") | ||
.build(); | ||
} | ||
|
||
public CommentRequestDto dummyCommentRequestDto(int num) { | ||
return new CommentRequestDto("comment test" + num); | ||
} | ||
|
||
public List<CommentRequestDto> dummyCommentRequestDtos() { | ||
return IntStream.range(1, 4) | ||
.mapToObj(this::dummyCommentRequestDto) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Oops, something went wrong.