From b0ec7ca1242a930d351b471c9ad80b049feb2a0a Mon Sep 17 00:00:00 2001 From: hong seokho Date: Thu, 6 Jun 2024 22:30:15 +0900 Subject: [PATCH] =?UTF-8?q?test=20:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로직 변경으로 인한 테스트 수정 --- .../common/fixture/domain/CapsuleFixture.java | 174 +++++++++++++----- .../domain/GroupCapsuleOpenFixture.java | 26 +++ .../service/GroupCapsuleServiceTest.java | 123 +++++-------- 3 files changed, 198 insertions(+), 125 deletions(-) diff --git a/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/CapsuleFixture.java b/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/CapsuleFixture.java index 83f029ad3..042ff78db 100644 --- a/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/CapsuleFixture.java +++ b/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/CapsuleFixture.java @@ -1,13 +1,17 @@ package site.timecapsulearchive.core.common.fixture.domain; import java.lang.reflect.Field; +import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.List; +import java.util.Optional; import java.util.stream.IntStream; import site.timecapsulearchive.core.common.dependency.UnitTestDependency; import site.timecapsulearchive.core.domain.capsule.entity.Address; import site.timecapsulearchive.core.domain.capsule.entity.Capsule; +import site.timecapsulearchive.core.domain.capsule.entity.Capsule.CapsuleBuilder; import site.timecapsulearchive.core.domain.capsule.entity.CapsuleType; +import site.timecapsulearchive.core.domain.capsule.entity.GroupCapsuleOpen; import site.timecapsulearchive.core.domain.capsuleskin.entity.CapsuleSkin; import site.timecapsulearchive.core.domain.group.entity.Group; import site.timecapsulearchive.core.domain.member.entity.Member; @@ -54,23 +58,6 @@ private static Address getTestAddress() { .build(); } - /** - * 그룹 캡슐의 테스트 픽스처들을 생성한다 - * - * @param size 테스트 픽스처를 만들 캡슐의 개수 - * @param member 그룹 캡슐을 생성할 멤버 - * @param capsuleSkin 캡슐 스킨 - * @param group 그룹 - * @return 그룹 캡슐 테스트 픽스처 - */ - public static List groupCapsules(int size, Member member, CapsuleSkin capsuleSkin, - Group group) { - return IntStream - .range(0, size) - .mapToObj(i -> groupCapsule(member, capsuleSkin, group)) - .toList(); - } - /** * 그룹 캡슐의 테스트 픽스처를 생성한다 * @@ -93,38 +80,19 @@ public static Capsule groupCapsule(Member member, CapsuleSkin capsuleSkin, Group .build(); } - public static Capsule groupCapsuleWithCapsuleId( - Member member, - CapsuleSkin capsuleSkin, - Group group, - Long capsuleId - ) { + private static void setFieldValue(Object instance, String fieldName, Object value) { try { - Capsule capsule = groupCapsule(member, capsuleSkin, group); - - setFieldValue(capsule, "id", capsuleId); - return capsule; + Field field = instance.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + field.set(instance, value); } catch (Exception e) { throw new RuntimeException(e); } } - private static void setFieldValue(Object instance, String fieldName, Object value) - throws NoSuchFieldException, IllegalAccessException { - Field field = instance.getClass().getDeclaredField(fieldName); - field.setAccessible(true); - field.set(instance, value); - } - - public static Capsule notGroupTimeCapsuleWithCapsuleId( - Member member, - CapsuleSkin capsuleSkin, - Group group, - Long capsuleId, - ZonedDateTime now - ) { - Capsule capsule = Capsule.builder() - .dueDate(now) + private static CapsuleBuilder defaultGroupCapsuleBuilder(Member member, CapsuleSkin capsuleSkin, + Group group) { + return Capsule.builder() .title("testTitle") .content("testContent") .type(CapsuleType.GROUP) @@ -132,14 +100,120 @@ public static Capsule notGroupTimeCapsuleWithCapsuleId( .point(geoTransformManager.changePoint4326To3857(TEST_LATITUDE, TEST_LONGITUDE)) .member(member) .capsuleSkin(capsuleSkin) - .group(group) + .group(group); + } + + public static Optional groupCapsuleSpecificTime( + Long memberId, + Long capsuleId, + ZonedDateTime zonedDateTime + ) { + CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId); + Capsule capsule = capsuleBuilder.dueDate(zonedDateTime) .build(); - try { - setFieldValue(capsule, "id", capsuleId); - return capsule; - } catch (Exception e) { - throw new RuntimeException(e); - } + setFieldValue(capsule, "id", capsuleId); + + return Optional.ofNullable(capsule); + } + + private static CapsuleBuilder getCapsuleBuilder(Long memberId) { + Member member = MemberFixture.memberWithMemberId(memberId); + CapsuleSkin capsuleSkin = CapsuleSkinFixture.capsuleSkin(member); + Group group = GroupFixture.group(); + + CapsuleBuilder capsuleBuilder = defaultGroupCapsuleBuilder(member, capsuleSkin, group); + return capsuleBuilder; + } + + public static Optional groupCapsuleNotAllMemberOpen( + Long memberId, + Long capsuleId, + List groupMembers + ) { + CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId); + Capsule capsule = capsuleBuilder.dueDate(ZonedDateTime.now(ZoneId.of("UTC"))) + .build(); + + List groupCapsuleOpens = GroupCapsuleOpenFixture.groupCapsuleOpens(false, + capsule, groupMembers); + setFieldValue(capsule, "id", capsuleId); + setFieldValue(capsule, "groupCapsuleOpens", groupCapsuleOpens); + + return Optional.ofNullable(capsule); + } + + public static Optional groupCapsuleHalfMemberOpen( + Long memberId, + Long capsuleId, + List groupMembers + ) { + CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId); + Capsule capsule = capsuleBuilder.dueDate(ZonedDateTime.now(ZoneId.of("UTC"))) + .build(); + + List groupCapsuleOpens = GroupCapsuleOpenFixture.groupCapsuleOpensNotAllOpened( + capsule, groupMembers); + setFieldValue(capsule, "id", capsuleId); + setFieldValue(capsule, "groupCapsuleOpens", groupCapsuleOpens); + return Optional.ofNullable(capsule); + } + + public static Optional groupCapsuleEmptyOpen( + Long memberId, + Long capsuleId + ) { + CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId); + Capsule capsule = capsuleBuilder.dueDate(ZonedDateTime.now(ZoneId.of("UTC"))) + .build(); + + setFieldValue(capsule, "id", capsuleId); + + return Optional.ofNullable(capsule); + } + + public static Optional groupCapsuleAllMemberOpen( + Long memberId, + Long capsuleId, + List groupMembers + ) { + CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId); + Capsule capsule = capsuleBuilder.dueDate(ZonedDateTime.now(ZoneId.of("UTC"))) + .build(); + + List groupCapsuleOpens = GroupCapsuleOpenFixture.groupCapsuleOpens(true, + capsule, groupMembers); + setFieldValue(capsule, "id", capsuleId); + setFieldValue(capsule, "groupCapsuleOpens", groupCapsuleOpens); + + return Optional.ofNullable(capsule); + } + + public static Optional groupCapsuleExcludeSpecificMember( + Long memberId, + Long capsuleId, + List groupMembers + ) { + CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId); + Capsule capsule = capsuleBuilder.dueDate(ZonedDateTime.now(ZoneId.of("UTC"))) + .build(); + + List groupCapsuleOpens = GroupCapsuleOpenFixture.groupCapsuleOpensNotOpenSpecificMemberId( + capsule, groupMembers, memberId); + setFieldValue(capsule, "id", capsuleId); + setFieldValue(capsule, "groupCapsuleOpens", groupCapsuleOpens); + + return Optional.ofNullable(capsule); + } + + public static Optional groupCapsuleAlreadyOpen(Long memberId, Long capsuleId) { + CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId); + Capsule capsule = capsuleBuilder.dueDate(ZonedDateTime.now(ZoneId.of("UTC"))) + .build(); + + setFieldValue(capsule, "id", capsuleId); + setFieldValue(capsule, "isOpened", true); + + return Optional.ofNullable(capsule); } } diff --git a/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/GroupCapsuleOpenFixture.java b/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/GroupCapsuleOpenFixture.java index 7b0c0c6bc..e718b048e 100644 --- a/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/GroupCapsuleOpenFixture.java +++ b/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/GroupCapsuleOpenFixture.java @@ -51,4 +51,30 @@ public static List groupCapsuleOpensNotAllOpened( .flatMap(Collection::stream) .toList(); } + + public static List groupCapsuleOpensNotOpenSpecificMemberId( + Capsule capsule, + List groupMembers, + Long memberId + ) { + List filteredMember = new ArrayList<>(); + Member specificMember = null; + for (Member member : groupMembers) { + if (member.getId().equals(memberId)) { + specificMember = member; + } else { + filteredMember.add(member); + } + } + + if (specificMember == null) { + throw new RuntimeException("멤버 리스트에 memberId를 가진 멤버가 존재하지 않습니다."); + } + + List result = new ArrayList<>(); + result.add(GroupCapsuleOpen.createOf(specificMember, capsule, false)); + result.addAll(groupCapsuleOpens(true, capsule, filteredMember)); + + return result; + } } diff --git a/backend/core/src/test/java/site/timecapsulearchive/core/domain/capsule/group_capsule/service/GroupCapsuleServiceTest.java b/backend/core/src/test/java/site/timecapsulearchive/core/domain/capsule/group_capsule/service/GroupCapsuleServiceTest.java index bb8b5333f..31e5d2ca5 100644 --- a/backend/core/src/test/java/site/timecapsulearchive/core/domain/capsule/group_capsule/service/GroupCapsuleServiceTest.java +++ b/backend/core/src/test/java/site/timecapsulearchive/core/domain/capsule/group_capsule/service/GroupCapsuleServiceTest.java @@ -8,14 +8,10 @@ import java.time.ZoneId; import java.time.ZonedDateTime; -import java.util.Collections; import java.util.List; import java.util.Optional; import org.junit.jupiter.api.Test; import site.timecapsulearchive.core.common.fixture.domain.CapsuleFixture; -import site.timecapsulearchive.core.common.fixture.domain.CapsuleSkinFixture; -import site.timecapsulearchive.core.common.fixture.domain.GroupCapsuleOpenFixture; -import site.timecapsulearchive.core.common.fixture.domain.GroupFixture; import site.timecapsulearchive.core.common.fixture.domain.MemberFixture; import site.timecapsulearchive.core.common.fixture.dto.CapsuleDtoFixture; import site.timecapsulearchive.core.domain.capsule.entity.Capsule; @@ -26,7 +22,6 @@ import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.CapsuleOpenStatus; import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleDetailDto; import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleOpenStateDto; -import site.timecapsulearchive.core.domain.capsule.group_capsule.repository.GroupCapsuleOpenRepository; import site.timecapsulearchive.core.domain.capsule.group_capsule.repository.GroupCapsuleQueryRepository; import site.timecapsulearchive.core.domain.group.data.dto.GroupMemberSummaryDto; import site.timecapsulearchive.core.domain.member.entity.Member; @@ -41,11 +36,9 @@ class GroupCapsuleServiceTest { private final CapsuleRepository capsuleRepository = mock(CapsuleRepository.class); private final GroupCapsuleQueryRepository groupCapsuleQueryRepository = mock( GroupCapsuleQueryRepository.class); - private final GroupCapsuleOpenRepository groupCapsuleOpenRepository = mock( - GroupCapsuleOpenRepository.class); private final GroupCapsuleService groupCapsuleService = new GroupCapsuleService( - capsuleRepository, groupCapsuleQueryRepository, groupCapsuleOpenRepository); + capsuleRepository, groupCapsuleQueryRepository); @Test void 개봉된_그룹_캡슐의_상세_내용을_볼_수_있다() { @@ -203,7 +196,7 @@ class GroupCapsuleServiceTest { @Test void 그룹_캡슐이_없는_경우_그룹_캡슐_개봉_시_예외가_발생한다() { //given - given(capsuleRepository.findGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) + given(capsuleRepository.findNotOpenedGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) .willReturn(Optional.empty()); //when @@ -217,9 +210,9 @@ class GroupCapsuleServiceTest { void 개봉일이_지나지_않아_그룹_캡슐을_열_수_없는_경우_캡슐은_개봉되지_않는다() { //given ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC")); - Optional groupCapsule = getGroupCapsuleSpecificTime(memberId, capsuleId, - now.plusYears(5)); - given(capsuleRepository.findGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) + Optional groupCapsule = CapsuleFixture.groupCapsuleSpecificTime(memberId, + capsuleId, now.plusYears(5)); + given(capsuleRepository.findNotOpenedGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) .willReturn(groupCapsule); //when @@ -237,8 +230,9 @@ class GroupCapsuleServiceTest { @Test void 타임캡슐이_아닌_경우_그룹_캡슐_개봉_시_그룹_캡슐은_개봉된다() { //given - Optional groupCapsule = getGroupCapsuleSpecificTime(memberId, capsuleId, null); - given(capsuleRepository.findGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) + Optional groupCapsule = CapsuleFixture.groupCapsuleSpecificTime(memberId, + capsuleId, null); + given(capsuleRepository.findNotOpenedGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) .willReturn(groupCapsule); //when @@ -253,37 +247,16 @@ class GroupCapsuleServiceTest { }); } - private Optional getGroupCapsuleSpecificTime( - Long memberId, - Long capsuleId, - ZonedDateTime now - ) { - Member member = MemberFixture.memberWithMemberId(memberId); - - return Optional.ofNullable( - CapsuleFixture.notGroupTimeCapsuleWithCapsuleId( - member, - CapsuleSkinFixture.capsuleSkin(member), - GroupFixture.group(), - capsuleId, - now - ) - ); - } - @Test void 모든_그룹원이_캡슐을_개봉하지_않은_경우_캡슐은_개봉되지_않는다() { //given - Optional groupCapsule = getGroupCapsule(memberId, capsuleId); - given(capsuleRepository.findGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) + List groupMembers = MemberFixture.membersWithMemberId(memberId.intValue(), 4); + Optional groupCapsule = CapsuleFixture.groupCapsuleNotAllMemberOpen(memberId, + capsuleId, + groupMembers); + given(capsuleRepository.findNotOpenedGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) .willReturn(groupCapsule); - List groupMembers = MemberFixture.membersWithMemberId(memberId.intValue() + 2, 4); - given(groupCapsuleOpenRepository.findByCapsuleId(capsuleId)) - .willReturn( - GroupCapsuleOpenFixture.groupCapsuleOpens(false, groupCapsule.get(), groupMembers) - ); - //when GroupCapsuleOpenStateDto groupCapsuleOpenStateDto = groupCapsuleService.openGroupCapsule( memberId, capsuleId); @@ -292,37 +265,20 @@ private Optional getGroupCapsuleSpecificTime( assertSoftly(softly -> { softly.assertThat(groupCapsule.get().getIsOpened()).isFalse(); softly.assertThat(groupCapsuleOpenStateDto.capsuleOpenStatus()).isEqualTo( - CapsuleOpenStatus.OPEN); + CapsuleOpenStatus.NOT_OPEN); }); } - private Optional getGroupCapsule(Long memberId, Long capsuleId) { - Member member = MemberFixture.memberWithMemberId(memberId); - - return Optional.ofNullable( - CapsuleFixture.groupCapsuleWithCapsuleId( - member, - CapsuleSkinFixture.capsuleSkin(member), - GroupFixture.group(), - capsuleId - ) - ); - } - @Test void 일부_그룹원이_캡슐을_개봉한_경우_캡슐은_개봉되지_않는다() { //given - Optional groupCapsule = getGroupCapsule(memberId, capsuleId); - given(capsuleRepository.findGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) + List groupMembers = MemberFixture.membersWithMemberId(memberId.intValue(), 4); + Optional groupCapsule = CapsuleFixture.groupCapsuleHalfMemberOpen(memberId, + capsuleId, + groupMembers); + given(capsuleRepository.findNotOpenedGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) .willReturn(groupCapsule); - List groupMembers = MemberFixture.membersWithMemberId(memberId.intValue() + 2, 4); - given(groupCapsuleOpenRepository.findByCapsuleId(capsuleId)) - .willReturn( - GroupCapsuleOpenFixture.groupCapsuleOpensNotAllOpened(groupCapsule.get(), - groupMembers) - ); - //when GroupCapsuleOpenStateDto groupCapsuleOpenStateDto = groupCapsuleService.openGroupCapsule( memberId, capsuleId); @@ -331,18 +287,16 @@ private Optional getGroupCapsule(Long memberId, Long capsuleId) { assertSoftly(softly -> { softly.assertThat(groupCapsule.get().getIsOpened()).isFalse(); softly.assertThat(groupCapsuleOpenStateDto.capsuleOpenStatus()).isEqualTo( - CapsuleOpenStatus.OPEN); + CapsuleOpenStatus.NOT_OPEN); }); } @Test void 그룹_캡슐_개봉이_없는_경우_예외가_발생한다() { //given - Optional groupCapsule = getGroupCapsule(memberId, capsuleId); - given(capsuleRepository.findGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) + Optional groupCapsule = CapsuleFixture.groupCapsuleEmptyOpen(memberId, capsuleId); + given(capsuleRepository.findNotOpenedGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) .willReturn(groupCapsule); - given(groupCapsuleOpenRepository.findByCapsuleId(capsuleId)).willReturn( - Collections.emptyList()); //when //then @@ -354,15 +308,34 @@ private Optional getGroupCapsule(Long memberId, Long capsuleId) { @Test void 모든_그룹원이_캡슐을_개봉한_경우_캡슐은_개봉된다() { //given - Optional groupCapsule = getGroupCapsule(memberId, capsuleId); - given(capsuleRepository.findGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) + List groupMembers = MemberFixture.membersWithMemberId(memberId.intValue(), 4); + Optional groupCapsule = CapsuleFixture.groupCapsuleAllMemberOpen(memberId, + capsuleId, + groupMembers); + given(capsuleRepository.findNotOpenedGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) .willReturn(groupCapsule); - List groupMembers = MemberFixture.membersWithMemberId(memberId.intValue() + 2, 4); - given(groupCapsuleOpenRepository.findByCapsuleId(capsuleId)) - .willReturn( - GroupCapsuleOpenFixture.groupCapsuleOpens(true, groupCapsule.get(), groupMembers) - ); + //when + GroupCapsuleOpenStateDto groupCapsuleOpenStateDto = groupCapsuleService.openGroupCapsule( + memberId, capsuleId); + + //then + assertSoftly(softly -> { + softly.assertThat(groupCapsule.get().getIsOpened()).isTrue(); + softly.assertThat(groupCapsuleOpenStateDto.capsuleOpenStatus()).isEqualTo( + CapsuleOpenStatus.OPEN); + }); + } + + @Test + void 사용자를_제외한_그룹원이_캡슐을_개봉한_경우_그룹_캡슐_개봉을_시도하면_캡슐은_개봉된다() { + //given + List groupMembers = MemberFixture.membersWithMemberId(memberId.intValue(), 4); + Optional groupCapsule = CapsuleFixture.groupCapsuleExcludeSpecificMember(memberId, + capsuleId, + groupMembers); + given(capsuleRepository.findNotOpenedGroupCapsuleByMemberIdAndCapsuleId(anyLong(), anyLong())) + .willReturn(groupCapsule); //when GroupCapsuleOpenStateDto groupCapsuleOpenStateDto = groupCapsuleService.openGroupCapsule(