From e39e7f1d599292a0d10f99ca91da2a9d1f113d1f Mon Sep 17 00:00:00 2001 From: parksey Date: Sun, 26 Nov 2023 03:45:30 +0900 Subject: [PATCH 1/7] =?UTF-8?q?test:=20=EC=82=AD=EC=A0=9C=EB=90=9C=20?= =?UTF-8?q?=ED=9A=8C=EC=9B=90=20=EC=A1=B0=ED=9A=8C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/member/MemberRepositoryTest.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java b/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java index ff6d4403..594457d1 100644 --- a/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java @@ -2,6 +2,7 @@ import static org.assertj.core.api.Assertions.*; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -9,6 +10,7 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; import com.moabam.api.application.member.MemberMapper; import com.moabam.api.domain.member.repository.BadgeRepository; @@ -27,6 +29,9 @@ import com.moabam.support.fixture.ParticipantFixture; import com.moabam.support.fixture.RoomFixture; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; + @QuerydslRepositoryTest class MemberRepositoryTest { @@ -45,6 +50,9 @@ class MemberRepositoryTest { @Autowired ParticipantRepository participantRepository; + @PersistenceContext + EntityManager entityManager; + @DisplayName("회원 생성 테스트") @Test void test() { @@ -163,4 +171,27 @@ void no_badges_search_success() { assertThat(memberInfoSearchResponse.badges()).isEmpty(); } } + + @DisplayName("삭제된 회원 찾기 테스트") + @Transactional + @Test + void findMemberTest() { + // Given + Member member = MemberFixture.member(); + + // When + memberRepository.save(member); + + member.delete(LocalDateTime.now()); + memberRepository.flush(); + memberRepository.delete(member); + + memberRepository.flush(); + + // then + Optional deletedMember = memberSearchRepository.findMember(member.getId(), false); + + assertThat(deletedMember).isPresent(); + assertThat(deletedMember.get().getSocialId()).contains("delete"); + } } From 0e339ffa25014d3615e35340e4ce68f4f33cc55f Mon Sep 17 00:00:00 2001 From: parksey Date: Sun, 26 Nov 2023 03:55:07 +0900 Subject: [PATCH 2/7] =?UTF-8?q?refactor:=20=ED=9A=8C=EC=9B=90=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/moabam/api/application/bug/BugService.java | 2 +- .../com/moabam/api/application/item/ItemService.java | 2 +- .../moabam/api/application/member/MemberService.java | 4 ++-- .../api/application/room/CertificationService.java | 2 +- .../com/moabam/api/application/room/RoomService.java | 12 ++++++------ .../moabam/api/application/bug/BugServiceTest.java | 2 +- .../moabam/api/application/item/ItemServiceTest.java | 4 ++-- .../application/room/CertificationServiceTest.java | 4 ++-- .../moabam/api/application/room/RoomServiceTest.java | 6 +++--- .../api/domain/member/MemberRepositoryTest.java | 11 +++++++++-- .../moabam/api/presentation/BugControllerTest.java | 2 +- .../moabam/api/presentation/ItemControllerTest.java | 2 +- 12 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/moabam/api/application/bug/BugService.java b/src/main/java/com/moabam/api/application/bug/BugService.java index de07033f..e2a000d2 100644 --- a/src/main/java/com/moabam/api/application/bug/BugService.java +++ b/src/main/java/com/moabam/api/application/bug/BugService.java @@ -38,7 +38,7 @@ public class BugService { private final PaymentRepository paymentRepository; public BugResponse getBug(Long memberId) { - Member member = memberService.getById(memberId); + Member member = memberService.findMember(memberId); return BugMapper.toBugResponse(member.getBug()); } diff --git a/src/main/java/com/moabam/api/application/item/ItemService.java b/src/main/java/com/moabam/api/application/item/ItemService.java index 3a024c2e..a0d5015f 100644 --- a/src/main/java/com/moabam/api/application/item/ItemService.java +++ b/src/main/java/com/moabam/api/application/item/ItemService.java @@ -49,7 +49,7 @@ public ItemsResponse getItems(Long memberId, ItemType type) { @Transactional public void purchaseItem(Long memberId, Long itemId, PurchaseItemRequest request) { Item item = getItem(itemId); - Member member = memberService.getById(memberId); + Member member = memberService.findMember(memberId); validateAlreadyPurchased(memberId, itemId); item.validatePurchasable(request.bugType(), member.getLevel()); diff --git a/src/main/java/com/moabam/api/application/member/MemberService.java b/src/main/java/com/moabam/api/application/member/MemberService.java index b54bdfc6..ef460b49 100644 --- a/src/main/java/com/moabam/api/application/member/MemberService.java +++ b/src/main/java/com/moabam/api/application/member/MemberService.java @@ -38,8 +38,8 @@ public class MemberService { private final MemberSearchRepository memberSearchRepository; private final ClockHolder clockHolder; - public Member getById(Long memberId) { - return memberRepository.findById(memberId) + public Member findMember(Long memberId) { + return memberSearchRepository.findMember(memberId) .orElseThrow(() -> new NotFoundException(MEMBER_NOT_FOUND)); } diff --git a/src/main/java/com/moabam/api/application/room/CertificationService.java b/src/main/java/com/moabam/api/application/room/CertificationService.java index 60d4b56d..ad7588d9 100644 --- a/src/main/java/com/moabam/api/application/room/CertificationService.java +++ b/src/main/java/com/moabam/api/application/room/CertificationService.java @@ -61,7 +61,7 @@ public void certifyRoom(Long memberId, Long roomId, List imageUrls) { Participant participant = participantSearchRepository.findOne(memberId, roomId) .orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND)); Room room = participant.getRoom(); - Member member = memberService.getById(memberId); + Member member = memberService.findMember(memberId); BugType bugType = switch (room.getRoomType()) { case MORNING -> BugType.MORNING; case NIGHT -> BugType.NIGHT; diff --git a/src/main/java/com/moabam/api/application/room/RoomService.java b/src/main/java/com/moabam/api/application/room/RoomService.java index 50592ead..f21090a1 100644 --- a/src/main/java/com/moabam/api/application/room/RoomService.java +++ b/src/main/java/com/moabam/api/application/room/RoomService.java @@ -57,7 +57,7 @@ public Long createRoom(Long memberId, String nickname, CreateRoomRequest createR validateEnteredRoomCount(memberId, room.getRoomType()); - Member member = memberService.getById(memberId); + Member member = memberService.findMember(memberId); member.enterRoom(room.getRoomType()); participant.enableManager(); room.changeManagerNickname(nickname); @@ -93,7 +93,7 @@ public void enterRoom(Long memberId, Long roomId, EnterRoomRequest enterRoomRequ Room room = roomRepository.findById(roomId).orElseThrow(() -> new NotFoundException(ROOM_NOT_FOUND)); validateRoomEnter(memberId, enterRoomRequest.password(), room); - Member member = memberService.getById(memberId); + Member member = memberService.findMember(memberId); member.enterRoom(room.getRoomType()); room.increaseCurrentUserCount(); @@ -108,7 +108,7 @@ public void exitRoom(Long memberId, Long roomId) { validateRoomExit(participant, room); - Member member = memberService.getById(memberId); + Member member = memberService.findMember(memberId); member.exitRoom(room.getRoomType()); participant.removeRoom(); @@ -130,7 +130,7 @@ public void mandateManager(Long managerId, Long roomId, Long memberId) { validateManagerAuthorization(managerParticipant); Room room = managerParticipant.getRoom(); - Member member = memberService.getById(memberParticipant.getMemberId()); + Member member = memberService.findMember(memberParticipant.getMemberId()); room.changeManagerNickname(member.getNickname()); managerParticipant.disableManager(); @@ -147,7 +147,7 @@ public void deportParticipant(Long managerId, Long roomId, Long memberId) { participantRepository.delete(memberParticipant); room.decreaseCurrentUserCount(); - Member member = memberService.getById(memberId); + Member member = memberService.findMember(memberId); member.exitRoom(room.getRoomType()); } @@ -180,7 +180,7 @@ private void validateRoomEnter(Long memberId, String requestPassword, Room room) } private void validateEnteredRoomCount(Long memberId, RoomType roomType) { - Member member = memberService.getById(memberId); + Member member = memberService.findMember(memberId); if (roomType.equals(MORNING) && member.getCurrentMorningCount() >= 3) { throw new BadRequestException(MEMBER_ROOM_EXCEED); diff --git a/src/test/java/com/moabam/api/application/bug/BugServiceTest.java b/src/test/java/com/moabam/api/application/bug/BugServiceTest.java index 0d4bce1f..0acc0366 100644 --- a/src/test/java/com/moabam/api/application/bug/BugServiceTest.java +++ b/src/test/java/com/moabam/api/application/bug/BugServiceTest.java @@ -59,7 +59,7 @@ void get_bug_success() { // given Long memberId = 1L; Member member = member(); - given(memberService.getById(memberId)).willReturn(member); + given(memberService.findMember(memberId)).willReturn(member); // when BugResponse response = bugService.getBug(memberId); diff --git a/src/test/java/com/moabam/api/application/item/ItemServiceTest.java b/src/test/java/com/moabam/api/application/item/ItemServiceTest.java index c0f06cab..6abe441d 100644 --- a/src/test/java/com/moabam/api/application/item/ItemServiceTest.java +++ b/src/test/java/com/moabam/api/application/item/ItemServiceTest.java @@ -98,7 +98,7 @@ void success() { PurchaseItemRequest request = new PurchaseItemRequest(BugType.GOLDEN); Member member = member(); Item item = nightMageSkin(); - given(memberService.getById(memberId)).willReturn(member); + given(memberService.findMember(memberId)).willReturn(member); given(itemRepository.findById(itemId)).willReturn(Optional.of(item)); given(inventorySearchRepository.findOne(memberId, itemId)).willReturn(Optional.empty()); @@ -106,7 +106,7 @@ void success() { itemService.purchaseItem(memberId, itemId, request); // Then - verify(memberService).getById(memberId); + verify(memberService).findMember(memberId); verify(itemRepository).findById(itemId); verify(inventorySearchRepository).findOne(memberId, itemId); verify(inventoryRepository).save(any(Inventory.class)); diff --git a/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java b/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java index ee3bacb1..19af78e5 100644 --- a/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java +++ b/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java @@ -124,7 +124,7 @@ void already_certified_room_routine_success() { given(clockHolder.times()).willReturn(LocalDateTime.now().withHour(9).withMinute(58)); given(clockHolder.date()).willReturn(today); given(participantSearchRepository.findOne(memberId, roomId)).willReturn(Optional.of(participant)); - given(memberService.getById(memberId)).willReturn(member1); + given(memberService.findMember(memberId)).willReturn(member1); given(routineRepository.findById(1L)).willReturn(Optional.of(routines.get(0))); given(routineRepository.findById(2L)).willReturn(Optional.of(routines.get(1))); given(certificationsSearchRepository.findDailyRoomCertification(roomId, today)).willReturn( @@ -152,7 +152,7 @@ void not_certified_room_routine_success() { given(clockHolder.times()).willReturn(LocalDateTime.now().withHour(9).withMinute(58)); given(clockHolder.date()).willReturn(today); given(participantSearchRepository.findOne(memberId, roomId)).willReturn(Optional.of(participant)); - given(memberService.getById(memberId)).willReturn(member1); + given(memberService.findMember(memberId)).willReturn(member1); given(routineRepository.findById(1L)).willReturn(Optional.of(routines.get(0))); given(routineRepository.findById(2L)).willReturn(Optional.of(routines.get(1))); given(certificationsSearchRepository.findDailyRoomCertification(roomId, today)) diff --git a/src/test/java/com/moabam/api/application/room/RoomServiceTest.java b/src/test/java/com/moabam/api/application/room/RoomServiceTest.java index b0038193..c2921883 100644 --- a/src/test/java/com/moabam/api/application/room/RoomServiceTest.java +++ b/src/test/java/com/moabam/api/application/room/RoomServiceTest.java @@ -67,7 +67,7 @@ void create_room_no_password_success() { Room expectedRoom = RoomMapper.toRoomEntity(createRoomRequest); given(roomRepository.save(any(Room.class))).willReturn(expectedRoom); - given(memberService.getById(1L)).willReturn(member); + given(memberService.findMember(1L)).willReturn(member); // when Long result = roomService.createRoom(1L, "닉네임", createRoomRequest); @@ -95,7 +95,7 @@ void create_room_with_password_success() { Room expectedRoom = RoomMapper.toRoomEntity(createRoomRequest); given(roomRepository.save(any(Room.class))).willReturn(expectedRoom); - given(memberService.getById(1L)).willReturn(member); + given(memberService.findMember(1L)).willReturn(member); // when Long result = roomService.createRoom(1L, "닉네임", createRoomRequest); @@ -128,7 +128,7 @@ void room_manager_mandate_success() { Optional.of(memberParticipant)); given(participantSearchRepository.findOne(managerId, room.getId())).willReturn( Optional.of(managerParticipant)); - given(memberService.getById(2L)).willReturn(member); + given(memberService.findMember(2L)).willReturn(member); // when roomService.mandateManager(managerId, room.getId(), memberId); diff --git a/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java b/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java index 594457d1..2804e68c 100644 --- a/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Optional; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -191,7 +192,13 @@ void findMemberTest() { // then Optional deletedMember = memberSearchRepository.findMember(member.getId(), false); - assertThat(deletedMember).isPresent(); - assertThat(deletedMember.get().getSocialId()).contains("delete"); + Assertions.assertAll( + () -> assertThat(deletedMember).isPresent(), + () -> { + Member delete = deletedMember.get(); + assertThat(delete.getSocialId()).contains("delete"); + assertThat(delete.getDeletedAt()).isNotNull(); + } + ); } } diff --git a/src/test/java/com/moabam/api/presentation/BugControllerTest.java b/src/test/java/com/moabam/api/presentation/BugControllerTest.java index 34025882..fa041b92 100644 --- a/src/test/java/com/moabam/api/presentation/BugControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/BugControllerTest.java @@ -68,7 +68,7 @@ void get_bug_success() throws Exception { // given Long memberId = getAuthMember().id(); BugResponse expected = BugMapper.toBugResponse(bug()); - given(memberService.getById(memberId)).willReturn(member()); + given(memberService.findMember(memberId)).willReturn(member()); // expected String content = mockMvc.perform(get("/bugs") diff --git a/src/test/java/com/moabam/api/presentation/ItemControllerTest.java b/src/test/java/com/moabam/api/presentation/ItemControllerTest.java index 87168c4a..b637e8f7 100644 --- a/src/test/java/com/moabam/api/presentation/ItemControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/ItemControllerTest.java @@ -114,7 +114,7 @@ void success() throws Exception { Long memberId = getAuthMember().id(); Item item = itemRepository.save(nightMageSkin()); PurchaseItemRequest request = new PurchaseItemRequest(BugType.NIGHT); - given(memberService.getById(memberId)).willReturn(member()); + given(memberService.findMember(memberId)).willReturn(member()); // expected mockMvc.perform(post("/items/{itemId}/purchase", item.getId()) From 3c10ab6e922998af30c146a04c64f08678d4603a Mon Sep 17 00:00:00 2001 From: parksey Date: Sun, 26 Nov 2023 05:42:11 +0900 Subject: [PATCH 3/7] =?UTF-8?q?feat:=20=EC=8B=A0=EA=B3=A0=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/application/report/ReportMapper.java | 23 ++++++ .../api/application/report/ReportService.java | 48 +++++++++++ .../room/CertificationService.java | 10 ++- .../api/application/room/RoomService.java | 5 ++ .../com/moabam/api/domain/report/Report.java | 55 +++++++++++++ .../report/repository/ReportRepository.java | 9 +++ .../moabam/api/dto/report/ReportRequest.java | 12 +++ .../global/error/model/ErrorMessage.java | 3 + .../application/report/ReportServiceTest.java | 81 +++++++++++++++++++ .../moabam/support/fixture/ReportFixture.java | 26 ++++++ 10 files changed, 268 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/moabam/api/application/report/ReportMapper.java create mode 100644 src/main/java/com/moabam/api/application/report/ReportService.java create mode 100644 src/main/java/com/moabam/api/domain/report/Report.java create mode 100644 src/main/java/com/moabam/api/domain/report/repository/ReportRepository.java create mode 100644 src/main/java/com/moabam/api/dto/report/ReportRequest.java create mode 100644 src/test/java/com/moabam/api/application/report/ReportServiceTest.java create mode 100644 src/test/java/com/moabam/support/fixture/ReportFixture.java diff --git a/src/main/java/com/moabam/api/application/report/ReportMapper.java b/src/main/java/com/moabam/api/application/report/ReportMapper.java new file mode 100644 index 00000000..7cada261 --- /dev/null +++ b/src/main/java/com/moabam/api/application/report/ReportMapper.java @@ -0,0 +1,23 @@ +package com.moabam.api.application.report; + +import com.moabam.api.domain.report.Report; +import com.moabam.api.domain.room.Certification; +import com.moabam.api.domain.room.Room; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ReportMapper { + + public static Report toReport(Long reporterId, Long reportedMemberId, + Room room, Certification certification, String description) { + return Report.builder() + .reporterId(reporterId) + .reportedMemberId(reportedMemberId) + .certification(certification) + .room(room) + .description(description) + .build(); + } +} diff --git a/src/main/java/com/moabam/api/application/report/ReportService.java b/src/main/java/com/moabam/api/application/report/ReportService.java new file mode 100644 index 00000000..303402ea --- /dev/null +++ b/src/main/java/com/moabam/api/application/report/ReportService.java @@ -0,0 +1,48 @@ +package com.moabam.api.application.report; + +import static java.util.Objects.*; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.moabam.api.application.member.MemberService; +import com.moabam.api.application.room.CertificationService; +import com.moabam.api.application.room.RoomService; +import com.moabam.api.domain.member.Member; +import com.moabam.api.domain.report.Report; +import com.moabam.api.domain.report.repository.ReportRepository; +import com.moabam.api.domain.room.Certification; +import com.moabam.api.domain.room.Room; +import com.moabam.api.dto.report.ReportRequest; +import com.moabam.global.auth.model.AuthMember; +import com.moabam.global.error.exception.BadRequestException; +import com.moabam.global.error.model.ErrorMessage; + +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +public class ReportService { + + private final MemberService memberService; + private final RoomService roomService; + private final CertificationService certificationService; + private final ReportRepository reportRepository; + + @Transactional + public void report(AuthMember authMember, ReportRequest reportRequest) { + validateNoReportSubject(reportRequest.roomId(), reportRequest.certificationId()); + Member reportedMember = memberService.findMember(reportRequest.reportedId()); + Room room = roomService.findRoom(reportRequest.roomId()); + Certification certification = certificationService.findCertification(reportRequest.certificationId()); + Report report = ReportMapper.toReport(authMember.id(), reportedMember.getId(), + room, certification, reportRequest.description()); + reportRepository.save(report); + } + + private void validateNoReportSubject(Long roomId, Long certificationId) { + if (isNull(roomId) && isNull(certificationId)) { + throw new BadRequestException(ErrorMessage.REPORT_REQUEST_ERROR); + } + } +} diff --git a/src/main/java/com/moabam/api/application/room/CertificationService.java b/src/main/java/com/moabam/api/application/room/CertificationService.java index ad7588d9..9085a3a5 100644 --- a/src/main/java/com/moabam/api/application/room/CertificationService.java +++ b/src/main/java/com/moabam/api/application/room/CertificationService.java @@ -1,9 +1,6 @@ package com.moabam.api.application.room; -import static com.moabam.global.error.model.ErrorMessage.DUPLICATED_DAILY_MEMBER_CERTIFICATION; -import static com.moabam.global.error.model.ErrorMessage.INVALID_CERTIFY_TIME; -import static com.moabam.global.error.model.ErrorMessage.PARTICIPANT_NOT_FOUND; -import static com.moabam.global.error.model.ErrorMessage.ROUTINE_NOT_FOUND; +import static com.moabam.global.error.model.ErrorMessage.*; import java.time.LocalDate; import java.time.LocalDateTime; @@ -93,6 +90,11 @@ public boolean existsRoomCertification(Long roomId, LocalDate date) { return dailyRoomCertificationRepository.existsByRoomIdAndCertifiedAt(roomId, date); } + public Certification findCertification(Long certificationId) { + return certificationRepository.findById(certificationId) + .orElseThrow(() -> new NotFoundException(CERTIFICATION_NOT_FOUND)); + } + private void validateCertifyTime(LocalDateTime now, int certifyTime) { LocalTime targetTime = LocalTime.of(certifyTime, 0); LocalDateTime minusTenMinutes = LocalDateTime.of(now.toLocalDate(), targetTime).minusMinutes(10); diff --git a/src/main/java/com/moabam/api/application/room/RoomService.java b/src/main/java/com/moabam/api/application/room/RoomService.java index f21090a1..39a2b21e 100644 --- a/src/main/java/com/moabam/api/application/room/RoomService.java +++ b/src/main/java/com/moabam/api/application/room/RoomService.java @@ -157,6 +157,11 @@ public void validateRoomById(Long roomId) { } } + public Room findRoom(Long roomId) { + return roomRepository.findById(roomId) + .orElseThrow(() -> new NotFoundException(ROOM_NOT_FOUND)); + } + private Participant getParticipant(Long memberId, Long roomId) { return participantSearchRepository.findOne(memberId, roomId) .orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND)); diff --git a/src/main/java/com/moabam/api/domain/report/Report.java b/src/main/java/com/moabam/api/domain/report/Report.java new file mode 100644 index 00000000..922e6ba8 --- /dev/null +++ b/src/main/java/com/moabam/api/domain/report/Report.java @@ -0,0 +1,55 @@ +package com.moabam.api.domain.report; + +import static java.util.Objects.*; + +import com.moabam.api.domain.room.Certification; +import com.moabam.api.domain.room.Room; +import com.moabam.global.common.entity.BaseTimeEntity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Table(name = "reports") +@Entity +public class Report extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "reporter_id", nullable = false, updatable = false) + private Long reporterId; + + @Column(name = "reported_member_id", nullable = false, updatable = false) + private Long reportedMemberId; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "room_id", updatable = false) + private Room room; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "certification_id", updatable = false) + private Certification certification; + + @Column(name = "description") + private String description; + + @Builder + private Report(Long reporterId, Long reportedMemberId, Room room, Certification certification, String description) { + this.reporterId = requireNonNull(reporterId); + this.reportedMemberId = requireNonNull(reportedMemberId); + this.room = room; + this.certification = certification; + } +} diff --git a/src/main/java/com/moabam/api/domain/report/repository/ReportRepository.java b/src/main/java/com/moabam/api/domain/report/repository/ReportRepository.java new file mode 100644 index 00000000..1655fbbc --- /dev/null +++ b/src/main/java/com/moabam/api/domain/report/repository/ReportRepository.java @@ -0,0 +1,9 @@ +package com.moabam.api.domain.report.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.moabam.api.domain.report.Report; + +public interface ReportRepository extends JpaRepository { + +} diff --git a/src/main/java/com/moabam/api/dto/report/ReportRequest.java b/src/main/java/com/moabam/api/dto/report/ReportRequest.java new file mode 100644 index 00000000..fdccf2e3 --- /dev/null +++ b/src/main/java/com/moabam/api/dto/report/ReportRequest.java @@ -0,0 +1,12 @@ +package com.moabam.api.dto.report; + +import jakarta.validation.constraints.NotNull; + +public record ReportRequest( + @NotNull Long reportedId, + Long roomId, + Long certificationId, + String description +) { + +} diff --git a/src/main/java/com/moabam/global/error/model/ErrorMessage.java b/src/main/java/com/moabam/global/error/model/ErrorMessage.java index ddc96335..678391f5 100644 --- a/src/main/java/com/moabam/global/error/model/ErrorMessage.java +++ b/src/main/java/com/moabam/global/error/model/ErrorMessage.java @@ -13,6 +13,8 @@ public enum ErrorMessage { NOT_FOUND_AVAILABLE_PORT("사용 가능한 포트를 찾을 수 없습니다. (10000 ~ 65535)"), ERROR_EXECUTING_EMBEDDED_REDIS("Embedded Redis 실행 중 오류가 발생했습니다."), + REPORT_REQUEST_ERROR("신고 요청하고자 하는 방이나 대상이 존재하지 않습니다."), + ROOM_NOT_FOUND("존재하지 않는 방 입니다."), ROOM_MAX_USER_COUNT_MODIFY_FAIL("잘못된 최대 인원수 설정입니다."), ROOM_MODIFY_UNAUTHORIZED_REQUEST("방장이 아닌 사용자는 방을 수정할 수 없습니다."), @@ -26,6 +28,7 @@ public enum ErrorMessage { ROUTINE_NOT_FOUND("루틴을 찾을 수 없습니다"), INVALID_REQUEST_URL("잘못된 URL 요청입니다."), INVALID_CERTIFY_TIME("현재 인증 시간이 아닙니다."), + CERTIFICATION_NOT_FOUND("인증 정보가 없습니다."), LOGIN_FAILED("로그인에 실패했습니다."), REQUEST_FAILED("네트워크 접근 실패입니다."), diff --git a/src/test/java/com/moabam/api/application/report/ReportServiceTest.java b/src/test/java/com/moabam/api/application/report/ReportServiceTest.java new file mode 100644 index 00000000..740c22bc --- /dev/null +++ b/src/test/java/com/moabam/api/application/report/ReportServiceTest.java @@ -0,0 +1,81 @@ +package com.moabam.api.application.report; + +import static com.moabam.global.error.model.ErrorMessage.*; +import static org.assertj.core.api.Assertions.*; +import static org.mockito.BDDMockito.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.moabam.api.application.member.MemberService; +import com.moabam.api.application.room.CertificationService; +import com.moabam.api.application.room.RoomService; +import com.moabam.api.domain.member.Member; +import com.moabam.api.domain.report.repository.ReportRepository; +import com.moabam.api.domain.room.Certification; +import com.moabam.api.domain.room.Room; +import com.moabam.api.domain.room.Routine; +import com.moabam.api.dto.report.ReportRequest; +import com.moabam.global.auth.model.AuthMember; +import com.moabam.global.error.exception.BadRequestException; +import com.moabam.support.annotation.WithMember; +import com.moabam.support.common.FilterProcessExtension; +import com.moabam.support.fixture.MemberFixture; +import com.moabam.support.fixture.ReportFixture; +import com.moabam.support.fixture.RoomFixture; + +@ExtendWith({MockitoExtension.class, FilterProcessExtension.class}) +public class ReportServiceTest { + + @InjectMocks + ReportService reportService; + + @Mock + CertificationService certificationService; + + @Mock + RoomService roomService; + + @Mock + MemberService memberService; + + @Mock + ReportRepository reportRepository; + + @DisplayName("신고 대상이 없어서 실패") + @Test + void no_report_subject_fail(@WithMember AuthMember authMember) { + // given + ReportRequest reportRequest = new ReportRequest(5L, null, null, "st"); + + // When + Then + assertThatThrownBy(() -> reportService.report(authMember, reportRequest)) + .isInstanceOf(BadRequestException.class) + .hasMessage(REPORT_REQUEST_ERROR.getMessage()); + } + + @DisplayName("신고 성공") + @Test + void report_success(@WithMember AuthMember authMember) { + // given + Room room = RoomFixture.room(); + Routine routine = RoomFixture.routine(room, "ets"); + Certification certification = RoomFixture.certification(routine); + ReportRequest reportRequest = ReportFixture.reportRequest(); + Member member = spy(MemberFixture.member()); + + given(member.getId()).willReturn(authMember.id()); + given(memberService.findMember(reportRequest.reportedId())).willReturn(member); + given(roomService.findRoom(reportRequest.roomId())).willReturn(RoomFixture.room()); + given(certificationService.findCertification(reportRequest.certificationId())) + .willReturn(certification); + + // When + Then + assertThatNoException() + .isThrownBy(() -> reportService.report(authMember, reportRequest)); + } +} diff --git a/src/test/java/com/moabam/support/fixture/ReportFixture.java b/src/test/java/com/moabam/support/fixture/ReportFixture.java new file mode 100644 index 00000000..2d9c9ae7 --- /dev/null +++ b/src/test/java/com/moabam/support/fixture/ReportFixture.java @@ -0,0 +1,26 @@ +package com.moabam.support.fixture; + +import com.moabam.api.domain.report.Report; +import com.moabam.api.domain.room.Certification; +import com.moabam.api.domain.room.Room; +import com.moabam.api.dto.report.ReportRequest; + +public class ReportFixture { + + private static Long reportedId = 99L; + private static Long roomId = 1L; + private static Long certificationId = 1L; + + public static Report report(Room room, Certification certification) { + return Report.builder() + .reporterId(1L) + .reportedMemberId(2L) + .room(room) + .certification(certification) + .build(); + } + + public static ReportRequest reportRequest() { + return new ReportRequest(reportedId, roomId, certificationId, "description"); + } +} From cfec3f81faf9f09bc5c063e230cb74e9ca4d92b4 Mon Sep 17 00:00:00 2001 From: parksey Date: Sun, 26 Nov 2023 15:45:22 +0900 Subject: [PATCH 4/7] =?UTF-8?q?refactor:=20=EC=8B=A0=EA=B3=A0=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95=20=EB=B0=8F?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/application/report/ReportService.java | 20 ++++++++++--- .../com/moabam/api/domain/report/Report.java | 3 +- .../application/report/ReportServiceTest.java | 28 +++++++++++++------ .../moabam/support/fixture/ReportFixture.java | 4 +++ 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/moabam/api/application/report/ReportService.java b/src/main/java/com/moabam/api/application/report/ReportService.java index 303402ea..f626383d 100644 --- a/src/main/java/com/moabam/api/application/report/ReportService.java +++ b/src/main/java/com/moabam/api/application/report/ReportService.java @@ -32,12 +32,24 @@ public class ReportService { @Transactional public void report(AuthMember authMember, ReportRequest reportRequest) { validateNoReportSubject(reportRequest.roomId(), reportRequest.certificationId()); + Report report = createReport(authMember.id(), reportRequest); + reportRepository.save(report); + } + + private Report createReport(Long reporterId, ReportRequest reportRequest) { Member reportedMember = memberService.findMember(reportRequest.reportedId()); + + if (nonNull(reportRequest.certificationId())) { + Certification certification = certificationService.findCertification(reportRequest.certificationId()); + + return ReportMapper.toReport(reporterId, reportedMember.getId(), + null, certification, reportRequest.description()); + } + Room room = roomService.findRoom(reportRequest.roomId()); - Certification certification = certificationService.findCertification(reportRequest.certificationId()); - Report report = ReportMapper.toReport(authMember.id(), reportedMember.getId(), - room, certification, reportRequest.description()); - reportRepository.save(report); + + return ReportMapper.toReport(reporterId, reportedMember.getId(), + room, null, reportRequest.description()); } private void validateNoReportSubject(Long roomId, Long certificationId) { diff --git a/src/main/java/com/moabam/api/domain/report/Report.java b/src/main/java/com/moabam/api/domain/report/Report.java index 922e6ba8..4e120055 100644 --- a/src/main/java/com/moabam/api/domain/report/Report.java +++ b/src/main/java/com/moabam/api/domain/report/Report.java @@ -20,7 +20,7 @@ import lombok.NoArgsConstructor; @NoArgsConstructor(access = AccessLevel.PROTECTED) -@Table(name = "reports") +@Table(name = "report") @Entity public class Report extends BaseTimeEntity { @@ -51,5 +51,6 @@ private Report(Long reporterId, Long reportedMemberId, Room room, Certification this.reportedMemberId = requireNonNull(reportedMemberId); this.room = room; this.certification = certification; + this.description = description; } } diff --git a/src/test/java/com/moabam/api/application/report/ReportServiceTest.java b/src/test/java/com/moabam/api/application/report/ReportServiceTest.java index 740c22bc..25b56d96 100644 --- a/src/test/java/com/moabam/api/application/report/ReportServiceTest.java +++ b/src/test/java/com/moabam/api/application/report/ReportServiceTest.java @@ -7,6 +7,8 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -29,7 +31,7 @@ import com.moabam.support.fixture.RoomFixture; @ExtendWith({MockitoExtension.class, FilterProcessExtension.class}) -public class ReportServiceTest { +class ReportServiceTest { @InjectMocks ReportService reportService; @@ -59,20 +61,30 @@ void no_report_subject_fail(@WithMember AuthMember authMember) { } @DisplayName("신고 성공") - @Test - void report_success(@WithMember AuthMember authMember) { + @ParameterizedTest + @CsvSource({"true, false", "false, true"}) + void report_success(boolean roomFilter, boolean certificationFilter, @WithMember AuthMember authMember) { // given Room room = RoomFixture.room(); Routine routine = RoomFixture.routine(room, "ets"); Certification certification = RoomFixture.certification(routine); - ReportRequest reportRequest = ReportFixture.reportRequest(); Member member = spy(MemberFixture.member()); - given(member.getId()).willReturn(authMember.id()); + Long roomId = null; + Long certificationId = null; + + if (roomFilter) { + given(roomService.findRoom(any())).willReturn(RoomFixture.room()); + roomId = 1L; + } + if (certificationFilter) { + given(certificationService.findCertification(any())).willReturn(certification); + certificationId = 1L; + } + + ReportRequest reportRequest = ReportFixture.reportRequest(2L, roomId, certificationId); + given(member.getId()).willReturn(2L); given(memberService.findMember(reportRequest.reportedId())).willReturn(member); - given(roomService.findRoom(reportRequest.roomId())).willReturn(RoomFixture.room()); - given(certificationService.findCertification(reportRequest.certificationId())) - .willReturn(certification); // When + Then assertThatNoException() diff --git a/src/test/java/com/moabam/support/fixture/ReportFixture.java b/src/test/java/com/moabam/support/fixture/ReportFixture.java index 2d9c9ae7..a3d48301 100644 --- a/src/test/java/com/moabam/support/fixture/ReportFixture.java +++ b/src/test/java/com/moabam/support/fixture/ReportFixture.java @@ -23,4 +23,8 @@ public static Report report(Room room, Certification certification) { public static ReportRequest reportRequest() { return new ReportRequest(reportedId, roomId, certificationId, "description"); } + + public static ReportRequest reportRequest(Long reportedId, Long roomId, Long certificationId) { + return new ReportRequest(reportedId, roomId, certificationId, "description"); + } } From e9679dd11745936921f12d98e2d2d3f98314938e Mon Sep 17 00:00:00 2001 From: parksey Date: Sun, 26 Nov 2023 15:48:52 +0900 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20=EC=8B=A0=EA=B3=A0=20api=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/presentation/ReportController.java | 30 +++ .../presentation/ReportControllerTest.java | 172 ++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 src/main/java/com/moabam/api/presentation/ReportController.java create mode 100644 src/test/java/com/moabam/api/presentation/ReportControllerTest.java diff --git a/src/main/java/com/moabam/api/presentation/ReportController.java b/src/main/java/com/moabam/api/presentation/ReportController.java new file mode 100644 index 00000000..cd23d3fd --- /dev/null +++ b/src/main/java/com/moabam/api/presentation/ReportController.java @@ -0,0 +1,30 @@ +package com.moabam.api.presentation; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import com.moabam.api.application.report.ReportService; +import com.moabam.api.dto.report.ReportRequest; +import com.moabam.global.auth.annotation.Auth; +import com.moabam.global.auth.model.AuthMember; + +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; + +@RestController +@RequestMapping("/reports") +@RequiredArgsConstructor +public class ReportController { + + private final ReportService reportService; + + @PostMapping + @ResponseStatus(HttpStatus.OK) + public void reports(@Auth AuthMember authMember, @Valid @RequestBody ReportRequest reportRequest) { + reportService.report(authMember, reportRequest); + } +} diff --git a/src/test/java/com/moabam/api/presentation/ReportControllerTest.java b/src/test/java/com/moabam/api/presentation/ReportControllerTest.java new file mode 100644 index 00000000..e18e59df --- /dev/null +++ b/src/test/java/com/moabam/api/presentation/ReportControllerTest.java @@ -0,0 +1,172 @@ +package com.moabam.api.presentation; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.time.LocalDateTime; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.transaction.annotation.Transactional; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.moabam.api.domain.member.Member; +import com.moabam.api.domain.member.repository.MemberRepository; +import com.moabam.api.domain.room.Certification; +import com.moabam.api.domain.room.Room; +import com.moabam.api.domain.room.Routine; +import com.moabam.api.domain.room.repository.CertificationRepository; +import com.moabam.api.domain.room.repository.RoomRepository; +import com.moabam.api.domain.room.repository.RoutineRepository; +import com.moabam.api.dto.report.ReportRequest; +import com.moabam.support.annotation.WithMember; +import com.moabam.support.common.WithoutFilterSupporter; +import com.moabam.support.fixture.MemberFixture; +import com.moabam.support.fixture.ReportFixture; +import com.moabam.support.fixture.RoomFixture; + +import jakarta.persistence.EntityManagerFactory; + +@Transactional +@SpringBootTest +@AutoConfigureMockMvc +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class ReportControllerTest extends WithoutFilterSupporter { + + @Autowired + MockMvc mockMvc; + + @Autowired + ObjectMapper objectMapper; + + @Autowired + MemberRepository memberRepository; + + @Autowired + RoomRepository roomRepository; + + @Autowired + CertificationRepository certificationRepository; + + @Autowired + RoutineRepository routineRepository; + + @Autowired + EntityManagerFactory entityManagerFactory; + + Member reportedMember; + + @BeforeAll + void setUp() { + reportedMember = MemberFixture.member(); + memberRepository.save(reportedMember); + } + + @DisplayName("방이나 인증 하나 신고") + @WithMember + @ParameterizedTest + @CsvSource({"true, false", "false, true", "true, true"}) + void reports_success(boolean roomFilter, boolean certificationFilter) throws Exception { + // given + String content = "내용"; + Room room = RoomFixture.room(); + Routine routine = RoomFixture.routine(room, content); + Certification certification = RoomFixture.certification(routine); + roomRepository.save(room); + routineRepository.save(routine); + certificationRepository.save(certification); + + Long roomId = null; + Long certificationId = null; + + if (roomFilter) { + roomId = room.getId(); + } + if (certificationFilter) { + certificationId = certification.getId(); + } + + ReportRequest reportRequest = ReportFixture.reportRequest(reportedMember.getId(), roomId, certificationId); + String request = objectMapper.writeValueAsString(reportRequest); + + // expected + mockMvc.perform(post("/reports") + .contentType(MediaType.APPLICATION_JSON) + .content(request)) + .andExpect(status().is2xxSuccessful()); + } + + @DisplayName("방과 인증 값 둘 다 들어오지 않는다면 테스트 실패") + @WithMember + @Test + void reports_failBy_subject_null() throws Exception { + // given + ReportRequest reportRequest = ReportFixture.reportRequest(123L, null, null); + String request = objectMapper.writeValueAsString(reportRequest); + + // expected + mockMvc.perform(post("/reports") + .contentType(MediaType.APPLICATION_JSON) + .content(request)) + .andExpect(status().isBadRequest()); + } + + @DisplayName("회원 조회 실패로 신고 실패") + @WithMember + @Test + void reports_failBy_member() throws Exception { + // given + Member newMember = MemberFixture.member("9999", "n"); + memberRepository.save(newMember); + + newMember.delete(LocalDateTime.now()); + memberRepository.flush(); + memberRepository.delete(newMember); + memberRepository.flush(); + + ReportRequest reportRequest = ReportFixture.reportRequest(newMember.getId(), 1L, 1L); + String request = objectMapper.writeValueAsString(reportRequest); + + // expected + mockMvc.perform(post("/reports") + .contentType(MediaType.APPLICATION_JSON) + .content(request)) + .andExpect(status().is4xxClientError()); + } + + @DisplayName("방이나 인증 하나 신고 실패") + @WithMember + @ParameterizedTest + @CsvSource({"true, false", "false, true", "true, true"}) + void reports_failBy_room_certification(boolean roomFilter, boolean certificationFilter) throws Exception { + // given + Long roomId = null; + Long certificationId = null; + + if (roomFilter) { + roomId = 12394L; + } + if (certificationFilter) { + certificationId = 123415L; + } + + ReportRequest reportRequest = ReportFixture.reportRequest(reportedMember.getId(), roomId, + certificationId); + String request = objectMapper.writeValueAsString(reportRequest); + + // expected + mockMvc.perform(post("/reports") + .contentType(MediaType.APPLICATION_JSON) + .content(request)) + .andExpect(status().is4xxClientError()); + } +} From b2ba6ebc4a82beaa8d5bf0bd3df87e26affcbdb7 Mon Sep 17 00:00:00 2001 From: parksey Date: Sun, 26 Nov 2023 15:50:58 +0900 Subject: [PATCH 6/7] =?UTF-8?q?fix:=20=ED=86=B5=ED=95=A9=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EA=B0=84=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=EB=B0=8F=20index=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/common/ClearDataExtension.java | 15 ++++++ .../support/common/DataCleanResolver.java | 54 +++++++++++++++++++ .../common/WithoutFilterSupporter.java | 8 +-- 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/moabam/support/common/ClearDataExtension.java create mode 100644 src/test/java/com/moabam/support/common/DataCleanResolver.java diff --git a/src/test/java/com/moabam/support/common/ClearDataExtension.java b/src/test/java/com/moabam/support/common/ClearDataExtension.java new file mode 100644 index 00000000..200a2502 --- /dev/null +++ b/src/test/java/com/moabam/support/common/ClearDataExtension.java @@ -0,0 +1,15 @@ +package com.moabam.support.common; + +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +public class ClearDataExtension implements AfterAllCallback { + + @Override + public void afterAll(ExtensionContext context) { + DataCleanResolver dataCleanResolver = + SpringExtension.getApplicationContext(context).getBean(DataCleanResolver.class); + dataCleanResolver.clean(); + } +} diff --git a/src/test/java/com/moabam/support/common/DataCleanResolver.java b/src/test/java/com/moabam/support/common/DataCleanResolver.java new file mode 100644 index 00000000..e2f8b780 --- /dev/null +++ b/src/test/java/com/moabam/support/common/DataCleanResolver.java @@ -0,0 +1,54 @@ +package com.moabam.support.common; + +import java.util.List; + +import javax.annotation.Nullable; + +import org.springframework.boot.test.context.TestComponent; +import org.springframework.transaction.annotation.Transactional; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.Query; + +@TestComponent +public class DataCleanResolver { + + private EntityManager entityManager; + + public DataCleanResolver(@Nullable EntityManager entityManager) { + this.entityManager = entityManager; + } + + @Transactional + public void clean() { + if (entityManager == null) { + return; + } + + List tableInfos = getTableInfos(); + doClean(tableInfos); + entityManager.clear(); + } + + private List getTableInfos() { + List tableInfos = entityManager.createNativeQuery("show tables").getResultList(); + + return tableInfos.stream() + .map(tableInfo -> (String)tableInfo[0]) + .toList(); + } + + private void doClean(List tableInfos) { + setForeignKeyCheck(false); + tableInfos.stream() + .map(tableInfo -> entityManager.createNativeQuery( + String.format("TRUNCATE TABLE %s RESTART IDENTITY", tableInfo))) + .forEach(Query::executeUpdate); + setForeignKeyCheck(true); + } + + private void setForeignKeyCheck(boolean data) { + entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY " + data) + .executeUpdate(); + } +} diff --git a/src/test/java/com/moabam/support/common/WithoutFilterSupporter.java b/src/test/java/com/moabam/support/common/WithoutFilterSupporter.java index ae2b827a..a381738f 100644 --- a/src/test/java/com/moabam/support/common/WithoutFilterSupporter.java +++ b/src/test/java/com/moabam/support/common/WithoutFilterSupporter.java @@ -1,7 +1,7 @@ package com.moabam.support.common; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.willReturn; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.BDDMockito.*; import java.util.Optional; @@ -9,13 +9,15 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.context.annotation.Import; import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; import com.moabam.api.domain.member.Role; import com.moabam.global.auth.filter.CorsFilter; import com.moabam.global.auth.handler.PathResolver; -@ExtendWith({FilterProcessExtension.class}) +@Import(DataCleanResolver.class) +@ExtendWith({FilterProcessExtension.class, ClearDataExtension.class}) public class WithoutFilterSupporter { @MockBean From 35ab8d41b45d16dda3badea1abab4a8ba8a11a4b Mon Sep 17 00:00:00 2001 From: parksey Date: Sun, 26 Nov 2023 17:12:14 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20CsvSource=20null=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../moabam/api/application/room/RoomService.java | 11 ++--------- .../api/presentation/MemberControllerTest.java | 1 - .../api/presentation/ReportControllerTest.java | 14 ++------------ 3 files changed, 4 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/moabam/api/application/room/RoomService.java b/src/main/java/com/moabam/api/application/room/RoomService.java index 39a2b21e..ff82d9dd 100644 --- a/src/main/java/com/moabam/api/application/room/RoomService.java +++ b/src/main/java/com/moabam/api/application/room/RoomService.java @@ -1,14 +1,7 @@ package com.moabam.api.application.room; -import static com.moabam.api.domain.room.RoomType.MORNING; -import static com.moabam.api.domain.room.RoomType.NIGHT; -import static com.moabam.global.error.model.ErrorMessage.MEMBER_ROOM_EXCEED; -import static com.moabam.global.error.model.ErrorMessage.PARTICIPANT_NOT_FOUND; -import static com.moabam.global.error.model.ErrorMessage.ROOM_EXIT_MANAGER_FAIL; -import static com.moabam.global.error.model.ErrorMessage.ROOM_MAX_USER_REACHED; -import static com.moabam.global.error.model.ErrorMessage.ROOM_MODIFY_UNAUTHORIZED_REQUEST; -import static com.moabam.global.error.model.ErrorMessage.ROOM_NOT_FOUND; -import static com.moabam.global.error.model.ErrorMessage.WRONG_ROOM_PASSWORD; +import static com.moabam.api.domain.room.RoomType.*; +import static com.moabam.global.error.model.ErrorMessage.*; import java.util.List; diff --git a/src/test/java/com/moabam/api/presentation/MemberControllerTest.java b/src/test/java/com/moabam/api/presentation/MemberControllerTest.java index 8fc82662..3edaee7a 100644 --- a/src/test/java/com/moabam/api/presentation/MemberControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/MemberControllerTest.java @@ -415,6 +415,5 @@ void search_member_failBy_default_skin_size() throws Exception { // expected mockMvc.perform(get("/members/{memberId}", 123L)) .andExpect(status().is4xxClientError()); - } } diff --git a/src/test/java/com/moabam/api/presentation/ReportControllerTest.java b/src/test/java/com/moabam/api/presentation/ReportControllerTest.java index e18e59df..fd724c07 100644 --- a/src/test/java/com/moabam/api/presentation/ReportControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/ReportControllerTest.java @@ -146,19 +146,9 @@ void reports_failBy_member() throws Exception { @DisplayName("방이나 인증 하나 신고 실패") @WithMember @ParameterizedTest - @CsvSource({"true, false", "false, true", "true, true"}) - void reports_failBy_room_certification(boolean roomFilter, boolean certificationFilter) throws Exception { + @CsvSource({"12394,", ",123415", "12394, 123415"}) + void reports_failBy_room_certification(Long roomId, Long certificationId) throws Exception { // given - Long roomId = null; - Long certificationId = null; - - if (roomFilter) { - roomId = 12394L; - } - if (certificationFilter) { - certificationId = 123415L; - } - ReportRequest reportRequest = ReportFixture.reportRequest(reportedMember.getId(), roomId, certificationId); String request = objectMapper.writeValueAsString(reportRequest);