|
| 1 | +package com.moing.backend.domain.missionComment.presentation; |
| 2 | + |
| 3 | +import com.moing.backend.config.CommonControllerTest; |
| 4 | +import com.moing.backend.domain.comment.application.dto.request.CreateCommentRequest; |
| 5 | +import com.moing.backend.domain.comment.application.dto.response.CommentBlocks; |
| 6 | +import com.moing.backend.domain.comment.application.dto.response.CreateCommentResponse; |
| 7 | +import com.moing.backend.domain.comment.application.dto.response.GetCommentResponse; |
| 8 | +import com.moing.backend.domain.missionComment.application.service.CreateMissionCommentUseCase; |
| 9 | +import com.moing.backend.domain.missionComment.application.service.DeleteMissionCommentUseCase; |
| 10 | +import com.moing.backend.domain.missionComment.application.service.GetMissionCommentUseCase; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 13 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; |
| 16 | +import org.springframework.test.web.servlet.ResultActions; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.BDDMockito.given; |
| 23 | +import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; |
| 24 | +import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders; |
| 25 | +import static org.springframework.restdocs.payload.PayloadDocumentation.*; |
| 26 | +import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; |
| 27 | +import static org.springframework.restdocs.request.RequestDocumentation.pathParameters; |
| 28 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 29 | + |
| 30 | +@WebMvcTest(MissionCommentController.class) |
| 31 | +public class MissionCommentControllerTest extends CommonControllerTest { |
| 32 | + |
| 33 | + @MockBean |
| 34 | + private CreateMissionCommentUseCase createMissionCommentUseCase; |
| 35 | + @MockBean |
| 36 | + private DeleteMissionCommentUseCase deleteMissionCommentUseCase; |
| 37 | + @MockBean |
| 38 | + private GetMissionCommentUseCase getMissionCommentUseCase; |
| 39 | + |
| 40 | + @Test |
| 41 | + public void create_mission_comment() throws Exception { |
| 42 | + |
| 43 | + //given |
| 44 | + Long teamId = 1L; |
| 45 | + Long missionArchiveId = 1L; |
| 46 | + CreateCommentRequest input = CreateCommentRequest.builder() |
| 47 | + .content("게시글 내용") |
| 48 | + .build(); |
| 49 | + |
| 50 | + String body = objectMapper.writeValueAsString(input); |
| 51 | + |
| 52 | + CreateCommentResponse output = CreateCommentResponse.builder() |
| 53 | + .commentId(1L) |
| 54 | + .build(); |
| 55 | + |
| 56 | + given(createMissionCommentUseCase.createBoardComment(any(), any(), any(), any())).willReturn(output); |
| 57 | + |
| 58 | + |
| 59 | + //when |
| 60 | + ResultActions actions = mockMvc.perform(RestDocumentationRequestBuilders. |
| 61 | + post("/api/{teamId}/{missionArchiveId}/mcomment", teamId, missionArchiveId) |
| 62 | + .header("Authorization", "Bearer ACCESS_TOKEN") |
| 63 | + .contentType(MediaType.APPLICATION_JSON) |
| 64 | + .content(body) |
| 65 | + ); |
| 66 | + |
| 67 | + //then |
| 68 | + actions |
| 69 | + .andExpect(status().isOk()) |
| 70 | + .andDo( |
| 71 | + restDocs.document( |
| 72 | + requestHeaders( |
| 73 | + headerWithName("Authorization").description("접근 토큰") |
| 74 | + ), |
| 75 | + pathParameters( |
| 76 | + parameterWithName("teamId").description("팀 아이디"), |
| 77 | + parameterWithName("missionArchiveId").description("미션 게시물 아이디") |
| 78 | + ), |
| 79 | + requestFields( |
| 80 | + fieldWithPath("content").description("댓글 내용") |
| 81 | + ), |
| 82 | + responseFields( |
| 83 | + fieldWithPath("isSuccess").description("true"), |
| 84 | + fieldWithPath("message").description("댓글을 생성했습니다"), |
| 85 | + fieldWithPath("data.commentId").description("생성한 boardCommentId") |
| 86 | + ) |
| 87 | + ) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void delete_mission_comment() throws Exception { |
| 93 | + |
| 94 | + //given |
| 95 | + Long teamId = 1L; |
| 96 | + Long boardId = 1L; |
| 97 | + Long missionArchiveId = 1L; |
| 98 | + |
| 99 | + |
| 100 | + //when |
| 101 | + ResultActions actions = mockMvc.perform(RestDocumentationRequestBuilders. |
| 102 | + delete("/api/{teamId}/{missionArchiveId}/mcomment/{boardCommentId}", teamId, boardId, missionArchiveId) |
| 103 | + .header("Authorization", "Bearer ACCESS_TOKEN") |
| 104 | + .contentType(MediaType.APPLICATION_JSON) |
| 105 | + ); |
| 106 | + |
| 107 | + //then |
| 108 | + actions |
| 109 | + .andExpect(status().isOk()) |
| 110 | + .andDo( |
| 111 | + restDocs.document( |
| 112 | + requestHeaders( |
| 113 | + headerWithName("Authorization").description("접근 토큰") |
| 114 | + ), |
| 115 | + pathParameters( |
| 116 | + parameterWithName("teamId").description("팀 아이디"), |
| 117 | + parameterWithName("missionArchiveId").description("미션 게시글 아이디"), |
| 118 | + parameterWithName("boardCommentId").description("댓글 아이디") |
| 119 | + ), |
| 120 | + responseFields( |
| 121 | + fieldWithPath("isSuccess").description("true"), |
| 122 | + fieldWithPath("message").description("댓글을 삭제했습니다") |
| 123 | + ) |
| 124 | + ) |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + @Test |
| 130 | + public void get_board_comment_all() throws Exception { |
| 131 | + //given |
| 132 | + List<CommentBlocks> commentBlocks = new ArrayList<>(); |
| 133 | + Long teamId = 1L; |
| 134 | + Long boardId = 1L; |
| 135 | + |
| 136 | + CommentBlocks commentBlock = CommentBlocks.builder() |
| 137 | + .commentId(1L) |
| 138 | + .content("댓글 내용") |
| 139 | + .writerIsLeader(true) |
| 140 | + .writerNickName("작성자 닉네임") |
| 141 | + .writerProfileImage("작성자 프로필 이미지") |
| 142 | + .writerIsDeleted(false) |
| 143 | + .isWriter(true) |
| 144 | + .createdDate("2023/12/05 23:29") |
| 145 | + .makerId(1L) |
| 146 | + .build(); |
| 147 | + |
| 148 | + commentBlocks.add(commentBlock); |
| 149 | + |
| 150 | + GetCommentResponse output = new GetCommentResponse(commentBlocks); |
| 151 | + |
| 152 | + given(getMissionCommentUseCase.getBoardCommentAll(any(), any(), any())).willReturn(output); |
| 153 | + |
| 154 | + |
| 155 | + //when |
| 156 | + ResultActions actions = mockMvc.perform(RestDocumentationRequestBuilders. |
| 157 | + get("/api/{teamId}/{missionArchiveId}/mcomment", teamId, boardId) |
| 158 | + .header("Authorization", "Bearer ACCESS_TOKEN") |
| 159 | + .contentType(MediaType.APPLICATION_JSON) |
| 160 | + ); |
| 161 | + |
| 162 | + |
| 163 | + //then |
| 164 | + actions |
| 165 | + .andExpect(status().isOk()) |
| 166 | + .andDo( |
| 167 | + restDocs.document( |
| 168 | + requestHeaders( |
| 169 | + headerWithName("Authorization").description("접근 토큰") |
| 170 | + ), |
| 171 | + pathParameters( |
| 172 | + parameterWithName("teamId").description("팀 아이디"), |
| 173 | + parameterWithName("missionArchiveId").description("게시글 아이디") |
| 174 | + ), |
| 175 | + responseFields( |
| 176 | + fieldWithPath("isSuccess").description("true"), |
| 177 | + fieldWithPath("message").description("댓글 목록을 모두 조회했습니다."), |
| 178 | + fieldWithPath("data.commentBlocks[].commentId").description("댓글 아이디"), |
| 179 | + fieldWithPath("data.commentBlocks[].content").description("댓글 내용"), |
| 180 | + fieldWithPath("data.commentBlocks[].writerIsLeader").description("작성자 소모임장 여부"), |
| 181 | + fieldWithPath("data.commentBlocks[].writerNickName").description("작성자 닉네임"), |
| 182 | + fieldWithPath("data.commentBlocks[].writerProfileImage").description("작성자 프로필 이미지"), |
| 183 | + fieldWithPath("data.commentBlocks[].writerIsDeleted").description("작성자 삭제 여부"), |
| 184 | + fieldWithPath("data.commentBlocks[].isWriter").description("댓글 작성자 여부"), |
| 185 | + fieldWithPath("data.commentBlocks[].createdDate").description("생성 시간"), |
| 186 | + fieldWithPath("data.commentBlocks[].makerId").description("작성자 Id") |
| 187 | + ) |
| 188 | + |
| 189 | + ) |
| 190 | + ); |
| 191 | + } |
| 192 | +} |
0 commit comments