From 13480d0f040982f92b0369a46bbed2d06bc90b8a Mon Sep 17 00:00:00 2001 From: seungsje <111065574+AreSain@users.noreply.github.com> Date: Tue, 26 Mar 2024 17:33:18 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20[Refactoring]=20&&=20?= =?UTF-8?q?=F0=9F=A7=AA=20[Test]=20Party=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20?= =?UTF-8?q?=EB=B0=8F=20=EC=A4=91=EB=B3=B5=EC=B2=98=EB=A6=AC=EB=A5=BC=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=EC=97=90=EB=9F=AC=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=EC=97=90=20=EB=94=B0=EB=A5=B8=20test=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80=20(#770)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/service/CommentAdminService.java | 7 +- .../admin/room/service/RoomAdminService.java | 6 +- .../request/CommentCreateReqDto.java | 2 +- .../user/comment/service/CommentService.java | 4 + .../category/CategoryAdminControllerTest.java | 20 ++--- .../comment/CommentAdminControllerTest.java | 55 ++++++------ .../report/ReportAdminControllerTest.java | 6 -- .../admin/room/RoomAdminControllerTest.java | 38 +++----- .../template/TemplateAdminControllerTest.java | 55 ++++++------ .../user/comment/CommentControllerTest.java | 88 +++++++++++-------- .../api/user/room/RoomControllerTest.java | 49 +++++------ .../java/gg/utils/exception/ErrorCode.java | 8 +- .../party/ChangeSameStatusException.java | 10 +++ .../party/RoomSameStatusException.java | 10 --- 14 files changed, 177 insertions(+), 181 deletions(-) create mode 100644 gg-utils/src/main/java/gg/utils/exception/party/ChangeSameStatusException.java delete mode 100644 gg-utils/src/main/java/gg/utils/exception/party/RoomSameStatusException.java diff --git a/gg-pingpong-api/src/main/java/gg/party/api/admin/comment/service/CommentAdminService.java b/gg-pingpong-api/src/main/java/gg/party/api/admin/comment/service/CommentAdminService.java index afebd50d0..b1399da3e 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/admin/comment/service/CommentAdminService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/admin/comment/service/CommentAdminService.java @@ -7,6 +7,7 @@ import gg.admin.repo.comment.CommentAdminRepository; import gg.data.party.Comment; import gg.party.api.admin.comment.controller.request.CommentUpdateAdminReqDto; +import gg.utils.exception.party.ChangeSameStatusException; import gg.utils.exception.party.CommentNotFoundException; import lombok.RequiredArgsConstructor; @@ -18,12 +19,16 @@ public class CommentAdminService { /** * 댓글 숨김 * @param commentId 댓글 번호 - * @exception CommentNotFoundException 유효하지 않은 댓글 + * @throws CommentNotFoundException 유효하지 않은 댓글 입력 - 404 + * @throws ChangeSameStatusException 같은 상태로 변경 - 409 */ @Transactional public void modifyHideComment(Long commentId, CommentUpdateAdminReqDto reqDto) { Comment comment = commentAdminRepository.findById(commentId) .orElseThrow(CommentNotFoundException::new); + if (reqDto.getIsHidden() == comment.isHidden()) { + throw new ChangeSameStatusException(); + } comment.updateHidden(reqDto.getIsHidden()); } } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/admin/room/service/RoomAdminService.java b/gg-pingpong-api/src/main/java/gg/party/api/admin/room/service/RoomAdminService.java index 8423f24ab..73be1256c 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/admin/room/service/RoomAdminService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/admin/room/service/RoomAdminService.java @@ -23,8 +23,8 @@ import gg.repo.party.CommentRepository; import gg.repo.party.RoomRepository; import gg.repo.party.UserRoomRepository; +import gg.utils.exception.party.ChangeSameStatusException; import gg.utils.exception.party.RoomNotFoundException; -import gg.utils.exception.party.RoomSameStatusException; import lombok.RequiredArgsConstructor; @Service @@ -39,7 +39,7 @@ public class RoomAdminService { * @param roomId 방 id * @param newStatus 바꿀 status * @exception RoomNotFoundException 유효하지 않은 방 입력 - 404 - * @exception RoomSameStatusException 같은 상태로 변경 -409 + * @exception ChangeSameStatusException 같은 상태로 변경 - 409 */ @Transactional public void modifyRoomStatus(Long roomId, RoomType newStatus) { @@ -47,7 +47,7 @@ public void modifyRoomStatus(Long roomId, RoomType newStatus) { .orElseThrow(RoomNotFoundException::new); if (room.getStatus() == newStatus) { - throw new RoomSameStatusException(); + throw new ChangeSameStatusException(); } room.updateRoomStatus(newStatus); diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/comment/controller/request/CommentCreateReqDto.java b/gg-pingpong-api/src/main/java/gg/party/api/user/comment/controller/request/CommentCreateReqDto.java index f946744fe..81d5ade4d 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/comment/controller/request/CommentCreateReqDto.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/comment/controller/request/CommentCreateReqDto.java @@ -11,7 +11,7 @@ public class CommentCreateReqDto { @Size(min = 1, max = 100) private String content; - public void saveContent(String content) { + public CommentCreateReqDto(String content) { this.content = content; } } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/comment/service/CommentService.java b/gg-pingpong-api/src/main/java/gg/party/api/user/comment/service/CommentService.java index 2d4106934..9202263e1 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/comment/service/CommentService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/comment/service/CommentService.java @@ -36,6 +36,10 @@ public class CommentService { /** * 댓글 생성 * @param roomId 방 번호 + * @throws RoomNotFoundException 방을 찾을 수 없음 - 404 + * @throws RoomNotOpenException 방이 열려있지 않음 - 400 + * @throws OnPenaltyException 패널티 상태의 유저 입력 - 403 + * @throws RoomNotParticipantException 방 참가자가 아님 - 400 * @param reqDto 댓글 정보 */ @Transactional diff --git a/gg-pingpong-api/src/test/java/gg/party/api/admin/category/CategoryAdminControllerTest.java b/gg-pingpong-api/src/test/java/gg/party/api/admin/category/CategoryAdminControllerTest.java index e6f2af8b6..ae5bd11ea 100644 --- a/gg-pingpong-api/src/test/java/gg/party/api/admin/category/CategoryAdminControllerTest.java +++ b/gg-pingpong-api/src/test/java/gg/party/api/admin/category/CategoryAdminControllerTest.java @@ -76,12 +76,12 @@ public void success() throws Exception { CategoryAddAdminReqDto categoryAddAdminReqDto = new CategoryAddAdminReqDto("category"); String jsonRequest = objectMapper.writeValueAsString(categoryAddAdminReqDto); //when - String contentAsString = mockMvc.perform(post(url) + mockMvc.perform(post(url) .contentType(MediaType.APPLICATION_JSON) .content(jsonRequest) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) .andExpect(status().isCreated()) - .andReturn().getResponse().getContentAsString(); + .andReturn().getResponse(); //then assertThat(categoryRepository.findAll()).size().isEqualTo(2); } @@ -95,11 +95,11 @@ public void fail() throws Exception { CategoryAddAdminReqDto categoryAddAdminReqDto = new CategoryAddAdminReqDto("category"); String jsonRequest = objectMapper.writeValueAsString(categoryAddAdminReqDto); //when & then - String contentAsString = mockMvc.perform(post(url) + mockMvc.perform(post(url) .contentType(MediaType.APPLICATION_JSON) .content(jsonRequest) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) - .andExpect(status().isConflict()).toString(); + .andExpect(status().isConflict()); } } @@ -124,11 +124,11 @@ public void success() throws Exception { String categoryID = testCategory.getId().toString(); String url = "/party/admin/categories/" + categoryID; //when - String contentAsString = mockMvc.perform(delete(url) + mockMvc.perform(delete(url) .contentType(MediaType.APPLICATION_JSON) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) .andExpect(status().isNoContent()) - .andReturn().getResponse().getContentAsString(); + .andReturn().getResponse(); //then assertThat(categoryRepository.findAll()).size().isEqualTo(1); assertThat(roomRepository.findById(testRoom.getId()).get().getCategory().getName()).isEqualTo("etc"); @@ -141,10 +141,10 @@ public void noCategoryFail() throws Exception { String categoryID = "10"; String url = "/party/admin/categories/" + categoryID; //when & then - String contentAsString = mockMvc.perform(delete(url) + mockMvc.perform(delete(url) .contentType(MediaType.APPLICATION_JSON) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) - .andExpect(status().isNotFound()).toString(); + .andExpect(status().isNotFound()); } @Test @@ -154,10 +154,10 @@ public void fail() throws Exception { String categoryID = defaultCategory.getId().toString(); String url = "/party/admin/categories/" + categoryID; //when & then - String contentAsString = mockMvc.perform(delete(url) + mockMvc.perform(delete(url) .contentType(MediaType.APPLICATION_JSON) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) - .andExpect(status().isBadRequest()).toString(); + .andExpect(status().isBadRequest()); } } } diff --git a/gg-pingpong-api/src/test/java/gg/party/api/admin/comment/CommentAdminControllerTest.java b/gg-pingpong-api/src/test/java/gg/party/api/admin/comment/CommentAdminControllerTest.java index a12feb07f..fe2064238 100644 --- a/gg-pingpong-api/src/test/java/gg/party/api/admin/comment/CommentAdminControllerTest.java +++ b/gg-pingpong-api/src/test/java/gg/party/api/admin/comment/CommentAdminControllerTest.java @@ -23,7 +23,6 @@ import gg.auth.utils.AuthTokenProvider; import gg.data.party.Category; import gg.data.party.Comment; -import gg.data.party.PartyPenalty; import gg.data.party.Room; import gg.data.party.UserRoom; import gg.data.party.type.RoomType; @@ -32,11 +31,6 @@ import gg.data.user.type.RoleType; import gg.data.user.type.SnsType; import gg.party.api.admin.comment.controller.request.CommentUpdateAdminReqDto; -import gg.repo.party.CategoryRepository; -import gg.repo.party.CommentRepository; -import gg.repo.party.PartyPenaltyRepository; -import gg.repo.party.RoomRepository; -import gg.repo.party.UserRoomRepository; import gg.utils.TestDataUtils; import gg.utils.annotation.IntegrationTest; import lombok.RequiredArgsConstructor; @@ -56,16 +50,6 @@ public class CommentAdminControllerTest { ObjectMapper objectMapper; @Autowired AuthTokenProvider tokenProvider; - @Autowired - RoomRepository roomRepository; - @Autowired - UserRoomRepository userRoomRepository; - @Autowired - CategoryRepository categoryRepository; - @Autowired - PartyPenaltyRepository partyPenaltyRepository; - @Autowired - CommentRepository commentRepository; User userTester; User reportedTester; User adminTester; @@ -84,8 +68,7 @@ void beforeEach() { RacketType.DUAL, SnsType.SLACK, RoleType.USER, "userImage"); reportedTester = testDataUtils.createNewImageUser("reportedTester", "reportedTester", RacketType.DUAL, SnsType.SLACK, RoleType.USER, "reportedImage"); - PartyPenalty testPenalty = testDataUtils.createNewPenalty(reportedTester, "test", "test", - LocalDateTime.now(), 60); + testDataUtils.createNewPenalty(reportedTester, "test", "test", LocalDateTime.now(), 60); adminTester = testDataUtils.createNewImageUser("adminTester", "adminTester", RacketType.DUAL, SnsType.SLACK, RoleType.ADMIN, "adminImage"); adminAccessToken = tokenProvider.createToken(adminTester.getId()); @@ -110,12 +93,13 @@ public void success() throws Exception { String url = "/party/admin/comments/" + commentId; CommentUpdateAdminReqDto commentUpdateAdminReqDto = new CommentUpdateAdminReqDto(TRUE); String requestBody = objectMapper.writeValueAsString(commentUpdateAdminReqDto); - //when && then - String contentAsString = mockMvc.perform(patch(url) + //when + mockMvc.perform(patch(url) .contentType(MediaType.APPLICATION_JSON) .content(requestBody) .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken)) - .andExpect(status().isNoContent()).toString(); + .andExpect(status().isNoContent()); + //then assertThat(testComment.isHidden()).isEqualTo(TRUE); } @@ -127,29 +111,46 @@ public void reportCommentSuccess() throws Exception { String url = "/party/admin/comments/" + commentId; CommentUpdateAdminReqDto commentUpdateAdminReqDto = new CommentUpdateAdminReqDto(FALSE); String requestBody = objectMapper.writeValueAsString(commentUpdateAdminReqDto); - //when && then - String contentAsString = mockMvc.perform(patch(url) + //when + mockMvc.perform(patch(url) .contentType(MediaType.APPLICATION_JSON) .content(requestBody) .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken)) - .andExpect(status().isNoContent()).toString(); + .andExpect(status().isNoContent()); //then assertThat(reportComment.isHidden()).isEqualTo(FALSE); } + @Test + @DisplayName("같은 내용으로 변경으로 인한 에러 409") + public void duplicationFail() throws Exception { + //given + String commentId = reportComment.getId().toString(); + String url = "/party/admin/comments/" + commentId; + CommentUpdateAdminReqDto commentUpdateAdminReqDto = new CommentUpdateAdminReqDto(TRUE); + String requestBody = objectMapper.writeValueAsString(commentUpdateAdminReqDto); + //when && then + mockMvc.perform(patch(url) + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody) + .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken)) + .andExpect(status().isConflict()); + } + @Test @DisplayName("없는 Comment로 인한 에러 404") public void fail() throws Exception { + //given String commentId = "1000"; String url = "/party/admin/comments/" + commentId; CommentUpdateAdminReqDto commentUpdateAdminReqDto = new CommentUpdateAdminReqDto(TRUE); String requestBody = objectMapper.writeValueAsString(commentUpdateAdminReqDto); - //given - String contentAsString = mockMvc.perform(patch(url) + //when && then + mockMvc.perform(patch(url) .contentType(MediaType.APPLICATION_JSON) .content(requestBody) .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken)) - .andExpect(status().isNotFound()).toString(); + .andExpect(status().isNotFound()); } } } diff --git a/gg-pingpong-api/src/test/java/gg/party/api/admin/report/ReportAdminControllerTest.java b/gg-pingpong-api/src/test/java/gg/party/api/admin/report/ReportAdminControllerTest.java index 4a7a2489d..e3248cf6b 100644 --- a/gg-pingpong-api/src/test/java/gg/party/api/admin/report/ReportAdminControllerTest.java +++ b/gg-pingpong-api/src/test/java/gg/party/api/admin/report/ReportAdminControllerTest.java @@ -31,8 +31,6 @@ import gg.party.api.admin.report.controller.response.CommentReportListResDto; import gg.party.api.admin.report.controller.response.RoomReportListResDto; import gg.party.api.admin.report.controller.response.UserReportListResDto; -import gg.party.api.admin.report.service.ReportAdminService; -import gg.repo.party.CategoryRepository; import gg.utils.TestDataUtils; import gg.utils.annotation.IntegrationTest; import lombok.RequiredArgsConstructor; @@ -52,10 +50,6 @@ public class ReportAdminControllerTest { ObjectMapper objectMapper; @Autowired AuthTokenProvider tokenProvider; - @Autowired - CategoryRepository categoryRepository; - @Autowired - ReportAdminService reportAdminService; User userTester; String userAccessToken; Category testCategory; diff --git a/gg-pingpong-api/src/test/java/gg/party/api/admin/room/RoomAdminControllerTest.java b/gg-pingpong-api/src/test/java/gg/party/api/admin/room/RoomAdminControllerTest.java index 8b33c53b0..3d0430cd2 100644 --- a/gg-pingpong-api/src/test/java/gg/party/api/admin/room/RoomAdminControllerTest.java +++ b/gg-pingpong-api/src/test/java/gg/party/api/admin/room/RoomAdminControllerTest.java @@ -22,7 +22,6 @@ import gg.auth.utils.AuthTokenProvider; import gg.data.party.Category; -import gg.data.party.PartyPenalty; import gg.data.party.Room; import gg.data.party.UserRoom; import gg.data.party.type.RoomType; @@ -35,10 +34,6 @@ import gg.party.api.admin.room.controller.response.AdminRoomDetailResDto; import gg.party.api.admin.room.controller.response.AdminRoomListResDto; import gg.party.api.user.room.controller.response.UserRoomResDto; -import gg.party.api.user.room.service.RoomManagementService; -import gg.repo.party.CategoryRepository; -import gg.repo.party.RoomRepository; -import gg.repo.party.UserRoomRepository; import gg.utils.TestDataUtils; import gg.utils.annotation.IntegrationTest; import lombok.RequiredArgsConstructor; @@ -58,14 +53,6 @@ public class RoomAdminControllerTest { ObjectMapper objectMapper; @Autowired AuthTokenProvider tokenProvider; - @Autowired - RoomRepository roomRepository; - @Autowired - UserRoomRepository userRoomRepository; - @Autowired - CategoryRepository categoryRepository; - @Autowired - RoomManagementService roomManagementService; User userTester; User adminTester; User reportedTester; @@ -114,12 +101,12 @@ public void success() throws Exception { RoomShowChangeReqDto roomShowChangeReqDto = new RoomShowChangeReqDto(RoomType.START.toString()); String jsonRequest = objectMapper.writeValueAsString(roomShowChangeReqDto); //when - String contentAsString = mockMvc.perform(patch(url) + mockMvc.perform(patch(url) .contentType(MediaType.APPLICATION_JSON) .content(jsonRequest) .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken)) .andExpect(status().isNoContent()) - .andReturn().getResponse().getContentAsString(); + .andReturn().getResponse(); //then assertThat(openRoom.getStatus()).isEqualTo(RoomType.START); } @@ -133,15 +120,15 @@ public void noRoomFail() throws Exception { RoomShowChangeReqDto roomShowChangeReqDto = new RoomShowChangeReqDto(RoomType.START.toString()); String jsonRequest = objectMapper.writeValueAsString(roomShowChangeReqDto); //when && then - String contentAsString = mockMvc.perform(patch(url) + mockMvc.perform(patch(url) .contentType(MediaType.APPLICATION_JSON) .content(jsonRequest) .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken)) - .andExpect(status().isNotFound()).toString(); + .andExpect(status().isNotFound()); } @Test - @DisplayName("동일한 Status 변경으로 인한 에러 400") + @DisplayName("동일한 Status 변경으로 인한 에러 409") public void sameStatusFail() throws Exception { //given String roomId = openRoom.getId().toString(); @@ -149,11 +136,11 @@ public void sameStatusFail() throws Exception { RoomShowChangeReqDto roomShowChangeReqDto = new RoomShowChangeReqDto(RoomType.OPEN.toString()); String jsonRequest = objectMapper.writeValueAsString(roomShowChangeReqDto); //when && then - String contentAsString = mockMvc.perform(patch(url) + mockMvc.perform(patch(url) .contentType(MediaType.APPLICATION_JSON) .content(jsonRequest) .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken)) - .andExpect(status().isBadRequest()).toString(); + .andExpect(status().isConflict()); } } @@ -168,7 +155,7 @@ void beforeEach() { reportedTester = testDataUtils.createNewUser("reportedTester", "reportedTester", RacketType.DUAL, SnsType.SLACK, RoleType.USER); reportedAccessToken = tokenProvider.createToken(reportedTester.getId()); - PartyPenalty testPenalty = testDataUtils.createNewPenalty(reportedTester, "test", "test", + testDataUtils.createNewPenalty(reportedTester, "test", "test", LocalDateTime.now(), 60); adminTester = testDataUtils.createNewUser("adminTester", "adminTester", RacketType.DUAL, SnsType.SLACK, RoleType.ADMIN); @@ -233,7 +220,7 @@ void beforeEach() { reportedTester = testDataUtils.createNewImageUser("reportedTester", "reportedTester", RacketType.DUAL, SnsType.SLACK, RoleType.USER, "reportedImage"); reportedAccessToken = tokenProvider.createToken(reportedTester.getId()); - PartyPenalty testPenalty = testDataUtils.createNewPenalty(reportedTester, "test", "test", + testDataUtils.createNewPenalty(reportedTester, "test", "test", LocalDateTime.now(), 60); adminTester = testDataUtils.createNewImageUser("adminTester", "adminTester", RacketType.DUAL, SnsType.SLACK, RoleType.ADMIN, "adminImage"); @@ -288,8 +275,7 @@ public void failRoomSuccess() throws Exception { Room failToNoUserRoom = testDataUtils.createNewRoom(userTester, userTester, testCategory, 1, 0, 3, 2, 180, RoomType.FAIL); - UserRoom testUserRoom = testDataUtils.createNewUserRoom(userTester, failToNoUserRoom, "testNickname", - FALSE); + testDataUtils.createNewUserRoom(userTester, failToNoUserRoom, "testNickname", FALSE); String roomId = failToNoUserRoom.getId().toString(); String url = "/party/admin/rooms/" + roomId; //when @@ -316,10 +302,10 @@ public void lastPageSuccess() throws Exception { String roomId = "1000"; String url = "/party/admin/rooms/" + roomId; //when - String contentAsString = mockMvc.perform(get(url) + mockMvc.perform(get(url) .contentType(MediaType.APPLICATION_JSON) .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminAccessToken)) - .andExpect(status().isNotFound()).toString(); + .andExpect(status().isNotFound()); } } } diff --git a/gg-pingpong-api/src/test/java/gg/party/api/admin/template/TemplateAdminControllerTest.java b/gg-pingpong-api/src/test/java/gg/party/api/admin/template/TemplateAdminControllerTest.java index 327d1258e..8afa21108 100644 --- a/gg-pingpong-api/src/test/java/gg/party/api/admin/template/TemplateAdminControllerTest.java +++ b/gg-pingpong-api/src/test/java/gg/party/api/admin/template/TemplateAdminControllerTest.java @@ -26,7 +26,6 @@ import gg.data.user.type.SnsType; import gg.party.api.admin.templates.controller.request.TemplateAdminCreateReqDto; import gg.party.api.admin.templates.controller.request.TemplateAdminUpdateReqDto; -import gg.party.api.admin.templates.service.TemplateAdminService; import gg.repo.party.CategoryRepository; import gg.repo.party.TemplateRepository; import gg.utils.TestDataUtils; @@ -51,8 +50,6 @@ public class TemplateAdminControllerTest { @Autowired CategoryRepository categoryRepository; @Autowired - TemplateAdminService templateAdminService; - @Autowired TemplateRepository templateRepository; User userTester; String userAccessToken; @@ -77,19 +74,19 @@ void beforeEach() { @Test @DisplayName("추가 성공 201") public void success() throws Exception { - String url = "/party/admin/templates"; //given + String url = "/party/admin/templates"; TemplateAdminCreateReqDto templateAdminCreateReqDto = new TemplateAdminCreateReqDto( testCategory.getId(), "gameName", 4, 2, 180, 180, "genre", "hard", "summary"); String jsonRequest = objectMapper.writeValueAsString(templateAdminCreateReqDto); //when - String contentAsString = mockMvc.perform(post(url) + mockMvc.perform(post(url) .contentType(MediaType.APPLICATION_JSON) .content(jsonRequest) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) .andExpect(status().isCreated()) - .andReturn().getResponse().getContentAsString(); + .andReturn().getResponse(); //then assertThat(templateRepository.findAll()).isNotNull(); } @@ -97,18 +94,18 @@ public void success() throws Exception { @Test @DisplayName("카테고리 없음으로 인한 추가 실패 404") public void fail() throws Exception { - String url = "/party/admin/templates"; //given + String url = "/party/admin/templates"; TemplateAdminCreateReqDto templateAdminCreateReqDto = new TemplateAdminCreateReqDto( 10L, "gameName", 4, 2, 180, 180, "genre", "hard", "summary"); String jsonRequest = objectMapper.writeValueAsString(templateAdminCreateReqDto); //when && then - String contentAsString = mockMvc.perform(post(url) + mockMvc.perform(post(url) .contentType(MediaType.APPLICATION_JSON) .content(jsonRequest) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) - .andExpect(status().isNotFound()).toString(); + .andExpect(status().isNotFound()); } } @@ -132,57 +129,57 @@ void beforeEach() { @Test @DisplayName("수정 성공 204") public void success() throws Exception { + //given String templateId = testTemplate.getId().toString(); String url = "/party/admin/templates/" + templateId; - //given Category newTestCategory = testDataUtils.createNewCategory("newCate"); TemplateAdminUpdateReqDto templateAdminUpdateReqDto = new TemplateAdminUpdateReqDto( newTestCategory.getId(), "newGameName", 8, 4, 90, 90, "newGenre", "easy", "newSummary"); String jsonRequest = objectMapper.writeValueAsString(templateAdminUpdateReqDto); //when - String contentAsString = mockMvc.perform(patch(url) + mockMvc.perform(patch(url) .contentType(MediaType.APPLICATION_JSON) .content(jsonRequest) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) .andExpect(status().isNoContent()) - .andReturn().getResponse().getContentAsString(); + .andReturn().getResponse(); //then - assertThat(testTemplate.getGameName()).isEqualTo("newGameName"); + assertThat(testTemplate.getGameName()).isEqualTo(templateAdminUpdateReqDto.getGameName()); assertThat(testTemplate.getCategory()).isEqualTo(newTestCategory); - assertThat(testTemplate.getMaxGamePeople()).isEqualTo(8); - assertThat(testTemplate.getMinGamePeople()).isEqualTo(4); - assertThat(testTemplate.getMaxGameTime()).isEqualTo(90); - assertThat(testTemplate.getMinGameTime()).isEqualTo(90); - assertThat(testTemplate.getGenre()).isEqualTo("newGenre"); - assertThat(testTemplate.getDifficulty()).isEqualTo("easy"); - assertThat(testTemplate.getSummary()).isEqualTo("newSummary"); + assertThat(testTemplate.getMaxGamePeople()).isEqualTo(templateAdminUpdateReqDto.getMaxGamePeople()); + assertThat(testTemplate.getMinGamePeople()).isEqualTo(templateAdminUpdateReqDto.getMinGamePeople()); + assertThat(testTemplate.getMaxGameTime()).isEqualTo(templateAdminUpdateReqDto.getMaxGameTime()); + assertThat(testTemplate.getMinGameTime()).isEqualTo(templateAdminUpdateReqDto.getMinGameTime()); + assertThat(testTemplate.getGenre()).isEqualTo(templateAdminUpdateReqDto.getGenre()); + assertThat(testTemplate.getDifficulty()).isEqualTo(templateAdminUpdateReqDto.getDifficulty()); + assertThat(testTemplate.getSummary()).isEqualTo(templateAdminUpdateReqDto.getSummary()); } @Test @DisplayName("카테고리 없음으로 인한 수정 실패 404") public void noCategoryFail() throws Exception { + //given String templateId = testTemplate.getId().toString(); String url = "/party/admin/templates/" + templateId; - //given TemplateAdminUpdateReqDto templateAdminUpdateReqDto = new TemplateAdminUpdateReqDto( - 10L, "newGameName", 8, 4, + 1000L, "newGameName", 8, 4, 90, 90, "newGenre", "easy", "newSummary"); String jsonRequest = objectMapper.writeValueAsString(templateAdminUpdateReqDto); //when && then - String contentAsString = mockMvc.perform(patch(url) + mockMvc.perform(patch(url) .contentType(MediaType.APPLICATION_JSON) .content(jsonRequest) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) - .andExpect(status().isNotFound()).toString(); + .andExpect(status().isNotFound()); } @Test @DisplayName("템플릿 없음으로 인한 수정 실패 404") public void noTemplateFail() throws Exception { - String templateId = "10"; - String url = "/party/admin/templates/" + templateId; //given + String templateId = "1000"; + String url = "/party/admin/templates/" + templateId; Category newTestCategory = testDataUtils.createNewCategory("newCate"); TemplateAdminUpdateReqDto templateAdminUpdateReqDto = new TemplateAdminUpdateReqDto( newTestCategory.getId(), "newGameName", 8, 4, @@ -217,9 +214,10 @@ void beforeEach() { @Test @DisplayName("삭제 성공 204") public void success() throws Exception { + //given String templateId = testTemplate.getId().toString(); String url = "/party/admin/templates/" + templateId; - //given && when + //when mockMvc.perform(delete(url) .contentType(MediaType.APPLICATION_JSON) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) @@ -231,9 +229,10 @@ public void success() throws Exception { @Test @DisplayName("템플릿 없음으로 인한 삭제 실패 404") public void fail() throws Exception { + //given String templateId = "10"; String url = "/party/admin/templates/" + templateId; - //given && when + //when && then mockMvc.perform(delete(url) .contentType(MediaType.APPLICATION_JSON) .header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) diff --git a/gg-pingpong-api/src/test/java/gg/party/api/user/comment/CommentControllerTest.java b/gg-pingpong-api/src/test/java/gg/party/api/user/comment/CommentControllerTest.java index 2a1968cba..9067afe96 100644 --- a/gg-pingpong-api/src/test/java/gg/party/api/user/comment/CommentControllerTest.java +++ b/gg-pingpong-api/src/test/java/gg/party/api/user/comment/CommentControllerTest.java @@ -21,9 +21,7 @@ import gg.auth.utils.AuthTokenProvider; import gg.data.party.Category; -import gg.data.party.PartyPenalty; import gg.data.party.Room; -import gg.data.party.UserRoom; import gg.data.party.type.RoomType; import gg.data.user.User; import gg.data.user.type.RacketType; @@ -55,11 +53,12 @@ public class CommentControllerTest { private CommentRepository commentRepository; @Autowired private RoomRepository roomRepository; - - private User userTester; + User userTester; User reportedTester; + User anotherUser; String userAccessToken; String reportedAccessToken; + String anotherAccessToken; Room openRoom; @BeforeEach @@ -68,13 +67,16 @@ void beforeEach() { RacketType.DUAL, SnsType.SLACK, RoleType.USER); reportedTester = testDataUtils.createNewUser("reportedTester", "reportedTester", RacketType.DUAL, SnsType.SLACK, RoleType.USER); - PartyPenalty testPenalty = testDataUtils.createNewPenalty(reportedTester, "penaltytype", "becauseTest", + anotherUser = testDataUtils.createNewUser("anotherUser", "anotherUser", + RacketType.DUAL, SnsType.SLACK, RoleType.USER); + testDataUtils.createNewPenalty(reportedTester, "penaltytype", "becauseTest", LocalDateTime.now(), 60); userAccessToken = tokenProvider.createToken(userTester.getId()); reportedAccessToken = tokenProvider.createToken(reportedTester.getId()); - Category category = testDataUtils.createNewCategory("tstcategry"); + anotherAccessToken = tokenProvider.createToken(anotherUser.getId()); + Category category = testDataUtils.createNewCategory("category"); openRoom = testDataUtils.createNewRoom(userTester, userTester, category, 1, 1, 10, 2, 120, RoomType.OPEN); - UserRoom userRoom = testDataUtils.createNewUserRoom(userTester, openRoom, "닉네임테스트", true); + testDataUtils.createNewUserRoom(userTester, openRoom, "닉네임테스트", true); } @Nested @@ -88,72 +90,86 @@ class CreateCommentTest { @Test @DisplayName("댓글 생성 성공 201") void createCommentSuccess() throws Exception { + //given String uri = "/party/rooms/{room_id}/comments"; - CommentCreateReqDto reqDto = new CommentCreateReqDto(); - reqDto.saveContent("이것은 댓글, 나는 success해야한다"); - + CommentCreateReqDto reqDto = new CommentCreateReqDto("testComment"); String content = objectMapper.writeValueAsString(reqDto); - + //when mockMvc.perform(post(uri, openRoom.getId()) .contentType(MediaType.APPLICATION_JSON) .content(content) .header("Authorization", "Bearer " + userAccessToken)) .andExpect(status().isCreated()); - + //then assertThat(commentRepository.findByRoomId(openRoom.getId())).hasSize(1); } - /** - * 신고된 유저일 경우 댓글 생성 실패하는지 테스트(403나오면 정상) - */ @Test - @DisplayName("신고된 사용자 댓글 생성 실패") - void createCommentFailForReportedUser() throws Exception { + @DisplayName("없는 방으로 인한 에러 404") + void roomNotFoundFail() throws Exception { + //given String uri = "/party/rooms/{room_id}/comments"; - CommentCreateReqDto reqDto = new CommentCreateReqDto(); - reqDto.saveContent("나는 신고된 유저, 댓글 생성 should fail"); - + CommentCreateReqDto reqDto = new CommentCreateReqDto("testComment"); String content = objectMapper.writeValueAsString(reqDto); + //when && then + mockMvc.perform(post(uri, 1000) + .contentType(MediaType.APPLICATION_JSON) + .content(content) + .header("Authorization", "Bearer " + userAccessToken)) + .andExpect(status().isNotFound()); + } + @Test + @DisplayName("신고된 사용자 댓글 생성 실패 403") + void createCommentFailForReportedUser() throws Exception { + //given + String uri = "/party/rooms/{room_id}/comments"; + CommentCreateReqDto reqDto = new CommentCreateReqDto("testComment"); + String content = objectMapper.writeValueAsString(reqDto); + //when && then mockMvc.perform(post(uri, openRoom.getId()) .contentType(MediaType.APPLICATION_JSON) .content(content) .header("Authorization", "Bearer " + reportedAccessToken)) .andExpect(status().isForbidden()); - - assertThat(commentRepository.findByRoomId(openRoom.getId())).isEmpty(); } - /** - * 방 상태가 시작전(OPEN)이 아닐때 댓글 생성 실패하는지 테스트 - * 오직 OPEN일때만 댓글 생성 가능 - */ @Test - @DisplayName("방 시작 전의 상황이 아닐때(START, FINISH, HIDDEN, FAIl) 댓글 생성 실패하는지") + @DisplayName("방의 STATUS가 OPEN이 아닐때(START, FINISH, HIDDEN, FAIl) 댓글 생성 실패 400") void createCommentFailWhenRoomNotOpen() throws Exception { + //given String uri = "/party/rooms/{room_id}/comments"; EnumSet notOpenStatuses = EnumSet.of(RoomType.START, RoomType.FINISH, RoomType.HIDDEN, RoomType.FAIL); - for (RoomType status : notOpenStatuses) { openRoom.updateRoomStatus(status); roomRepository.save(openRoom); - CommentCreateReqDto reqDto = new CommentCreateReqDto(); - reqDto.saveContent("This is a test comment."); - + CommentCreateReqDto reqDto = new CommentCreateReqDto("testComment"); String content = objectMapper.writeValueAsString(reqDto); - + //when && then mockMvc.perform(post(uri, openRoom.getId()) .contentType(MediaType.APPLICATION_JSON) .content(content) .header("Authorization", "Bearer " + userAccessToken)) - .andExpect( - status().isBadRequest()); - - assertThat(commentRepository.findByRoomId(openRoom.getId())).isEmpty(); + .andExpect(status().isBadRequest()); } } + + @Test + @DisplayName("방 참가자가 아님으로 인한 에러 400") + void roomNotParticipantUserFail() throws Exception { + //given + String uri = "/party/rooms/{room_id}/comments"; + CommentCreateReqDto reqDto = new CommentCreateReqDto("testComment"); + String content = objectMapper.writeValueAsString(reqDto); + //when && then + mockMvc.perform(post(uri, openRoom.getId()) + .contentType(MediaType.APPLICATION_JSON) + .content(content) + .header("Authorization", "Bearer " + anotherAccessToken)) + .andExpect(status().isBadRequest()); + } } } diff --git a/gg-pingpong-api/src/test/java/gg/party/api/user/room/RoomControllerTest.java b/gg-pingpong-api/src/test/java/gg/party/api/user/room/RoomControllerTest.java index 8ace15438..cbeeefe95 100644 --- a/gg-pingpong-api/src/test/java/gg/party/api/user/room/RoomControllerTest.java +++ b/gg-pingpong-api/src/test/java/gg/party/api/user/room/RoomControllerTest.java @@ -514,9 +514,8 @@ public void noRoomFail() throws Exception { String roomId = "1000"; String url = "/party/rooms/" + roomId + "/start"; // when && then - String contentAsString = mockMvc.perform( - post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) - .andExpect(status().isNotFound()).toString(); + mockMvc.perform(post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) + .andExpect(status().isNotFound()); } @Test @@ -526,9 +525,8 @@ public void notOpenFail() throws Exception { String roomId = startRoom.getId().toString(); String url = "/party/rooms/" + roomId + "/start"; // when && then - String contentAsString = mockMvc.perform( - post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) - .andExpect(status().isBadRequest()).toString(); + mockMvc.perform(post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) + .andExpect(status().isBadRequest()); } @Test @@ -540,9 +538,8 @@ public void notNotEnoughPeopleFail() throws Exception { String roomId = room.getId().toString(); String url = "/party/rooms/" + roomId + "/start"; // when && then - String contentAsString = mockMvc.perform( - post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) - .andExpect(status().isBadRequest()).toString(); + mockMvc.perform(post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) + .andExpect(status().isBadRequest()); } @Test @@ -552,9 +549,8 @@ public void notCurrentFail() throws Exception { String roomId = openRoom.getId().toString(); String url = "/party/rooms/" + roomId + "/start"; // when && then - String contentAsString = mockMvc.perform( - post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + anotherAccessToken)) - .andExpect(status().isBadRequest()).toString(); + mockMvc.perform(post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + anotherAccessToken)) + .andExpect(status().isBadRequest()); } @Test @@ -564,9 +560,8 @@ public void notHostFail() throws Exception { String roomId = openRoom.getId().toString(); String url = "/party/rooms/" + roomId + "/start"; // when && then - String contentAsString = mockMvc.perform( - post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + otherAccessToken)) - .andExpect(status().isForbidden()).toString(); + mockMvc.perform(post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + otherAccessToken)) + .andExpect(status().isForbidden()); } } @@ -642,9 +637,9 @@ public void penaltyUserFail() throws Exception { String roomId = rooms[0].getId().toString(); // openRoom String url = "/party/rooms/" + roomId; // when && then - String contentAsString = mockMvc.perform( + mockMvc.perform( post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + reportedAccessToken)) - .andExpect(status().isForbidden()).toString(); + .andExpect(status().isForbidden()); } @Test @@ -655,9 +650,8 @@ public void notOpenRoomFail() throws Exception { String roomId = (rooms[i].getId()).toString(); String url = "/party/rooms/" + roomId; // when && then - String contentAsString = mockMvc.perform( - post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + anotherAccessToken)) - .andExpect(status().isBadRequest()).toString(); + mockMvc.perform(post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + anotherAccessToken)) + .andExpect(status().isBadRequest()); } } @@ -668,9 +662,8 @@ public void alreadyInRoomFail() throws Exception { String roomId = rooms[0].getId().toString(); // openRoom String url = "/party/rooms/" + roomId; // when && then - String contentAsString = mockMvc.perform( - post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) - .andExpect(status().isConflict()).toString(); + mockMvc.perform(post(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) + .andExpect(status().isConflict()); } } @@ -757,9 +750,8 @@ public void notInFail() throws Exception { String roomId = rooms[0].getId().toString(); // openRoom String url = "/party/rooms/" + roomId; // when && then - String contentAsString = mockMvc.perform( - patch(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + otherAccessToken)) - .andExpect(status().isBadRequest()).toString(); + mockMvc.perform(patch(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + otherAccessToken)) + .andExpect(status().isBadRequest()); } @Test @@ -769,9 +761,8 @@ public void startFail() throws Exception { String startRoomId = rooms[1].getId().toString(); // startRoom String url = "/party/rooms/" + startRoomId; // when && then - String contentAsString = mockMvc.perform( - patch(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) - .andExpect(status().isBadRequest()).toString(); + mockMvc.perform(patch(url).header(HttpHeaders.AUTHORIZATION, "Bearer " + userAccessToken)) + .andExpect(status().isBadRequest()); } } } diff --git a/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java b/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java index c896b7703..56b4374fb 100644 --- a/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java +++ b/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java @@ -176,14 +176,14 @@ public enum ErrorCode { ROOM_NOT_ENOUGH_PEOPLE(400, "PT201", "시작할 수 있는 인원이 아닙니다."), DEFAULT_CATEGORY_NEED(400, "PT202", "기본 카테고리가 존재해야 합니다."), USER_NOT_HOST(400, "PT203", "방장이 아닙니다"), - ROOM_SAME_STATUS(400, "PT204", "이미 처리된 방 입니다."), - ROOM_NOT_OPEN(400, "PT205", "모집중인 방이 아닙니다."), - ROOM_NOT_PARTICIPANT(400, "PT206", "참여하지 않은 방 입니다."), - ROOM_MIN_MAX_PEOPLE(400, "PT207", "최소인원이 최대인원보다 큽니다."), + ROOM_NOT_OPEN(400, "PT204", "모집중인 방이 아닙니다."), + ROOM_NOT_PARTICIPANT(400, "PT205", "참여하지 않은 방 입니다."), + ROOM_MIN_MAX_PEOPLE(400, "PT206", "최소인원이 최대인원보다 큽니다."), USER_ALREADY_IN_ROOM(409, "PT301", "이미 참여한 방 입니다."), ALREADY_REPORTED(409, "PT302", "이미 신고한 요청입니다."), SELF_REPORT(409, "PT303", "자신을 신고할 수 없습니다."), CATEGORY_DUPLICATE(409, "PT304", "중복된 카테고리 입니다."), + CHANGE_SAME_STATUS(409, "PT305", "바꾸려는 항목과 동일합니다."), ON_PENALTY(403, "PT501", "패널티 상태입니다."); private final int status; diff --git a/gg-utils/src/main/java/gg/utils/exception/party/ChangeSameStatusException.java b/gg-utils/src/main/java/gg/utils/exception/party/ChangeSameStatusException.java new file mode 100644 index 000000000..3c5a61a5c --- /dev/null +++ b/gg-utils/src/main/java/gg/utils/exception/party/ChangeSameStatusException.java @@ -0,0 +1,10 @@ +package gg.utils.exception.party; + +import gg.utils.exception.ErrorCode; +import gg.utils.exception.custom.DuplicationException; + +public class ChangeSameStatusException extends DuplicationException { + public ChangeSameStatusException() { + super(ErrorCode.CHANGE_SAME_STATUS.getMessage(), ErrorCode.CHANGE_SAME_STATUS); + } +} diff --git a/gg-utils/src/main/java/gg/utils/exception/party/RoomSameStatusException.java b/gg-utils/src/main/java/gg/utils/exception/party/RoomSameStatusException.java deleted file mode 100644 index acb6e32e5..000000000 --- a/gg-utils/src/main/java/gg/utils/exception/party/RoomSameStatusException.java +++ /dev/null @@ -1,10 +0,0 @@ -package gg.utils.exception.party; - -import gg.utils.exception.ErrorCode; -import gg.utils.exception.custom.BusinessException; - -public class RoomSameStatusException extends BusinessException { - public RoomSameStatusException() { - super(ErrorCode.ROOM_SAME_STATUS.getMessage(), ErrorCode.ROOM_SAME_STATUS); - } -}