Skip to content

Commit

Permalink
test : 테스트 수정
Browse files Browse the repository at this point in the history
- 로직 변경으로 인한 테스트 수정
  • Loading branch information
seokho-1116 committed Jun 6, 2024
1 parent 091815b commit b0ec7ca
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -54,23 +58,6 @@ private static Address getTestAddress() {
.build();
}

/**
* 그룹 캡슐의 테스트 픽스처들을 생성한다
*
* @param size 테스트 픽스처를 만들 캡슐의 개수
* @param member 그룹 캡슐을 생성할 멤버
* @param capsuleSkin 캡슐 스킨
* @param group 그룹
* @return 그룹 캡슐 테스트 픽스처
*/
public static List<Capsule> groupCapsules(int size, Member member, CapsuleSkin capsuleSkin,
Group group) {
return IntStream
.range(0, size)
.mapToObj(i -> groupCapsule(member, capsuleSkin, group))
.toList();
}

/**
* 그룹 캡슐의 테스트 픽스처를 생성한다
*
Expand All @@ -93,53 +80,140 @@ 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)
.address(getTestAddress())
.point(geoTransformManager.changePoint4326To3857(TEST_LATITUDE, TEST_LONGITUDE))
.member(member)
.capsuleSkin(capsuleSkin)
.group(group)
.group(group);
}

public static Optional<Capsule> 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<Capsule> groupCapsuleNotAllMemberOpen(
Long memberId,
Long capsuleId,
List<Member> groupMembers
) {
CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId);
Capsule capsule = capsuleBuilder.dueDate(ZonedDateTime.now(ZoneId.of("UTC")))
.build();

List<GroupCapsuleOpen> groupCapsuleOpens = GroupCapsuleOpenFixture.groupCapsuleOpens(false,
capsule, groupMembers);
setFieldValue(capsule, "id", capsuleId);
setFieldValue(capsule, "groupCapsuleOpens", groupCapsuleOpens);

return Optional.ofNullable(capsule);
}

public static Optional<Capsule> groupCapsuleHalfMemberOpen(
Long memberId,
Long capsuleId,
List<Member> groupMembers
) {
CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId);
Capsule capsule = capsuleBuilder.dueDate(ZonedDateTime.now(ZoneId.of("UTC")))
.build();

List<GroupCapsuleOpen> groupCapsuleOpens = GroupCapsuleOpenFixture.groupCapsuleOpensNotAllOpened(
capsule, groupMembers);
setFieldValue(capsule, "id", capsuleId);
setFieldValue(capsule, "groupCapsuleOpens", groupCapsuleOpens);
return Optional.ofNullable(capsule);
}

public static Optional<Capsule> 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<Capsule> groupCapsuleAllMemberOpen(
Long memberId,
Long capsuleId,
List<Member> groupMembers
) {
CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId);
Capsule capsule = capsuleBuilder.dueDate(ZonedDateTime.now(ZoneId.of("UTC")))
.build();

List<GroupCapsuleOpen> groupCapsuleOpens = GroupCapsuleOpenFixture.groupCapsuleOpens(true,
capsule, groupMembers);
setFieldValue(capsule, "id", capsuleId);
setFieldValue(capsule, "groupCapsuleOpens", groupCapsuleOpens);

return Optional.ofNullable(capsule);
}

public static Optional<Capsule> groupCapsuleExcludeSpecificMember(
Long memberId,
Long capsuleId,
List<Member> groupMembers
) {
CapsuleBuilder capsuleBuilder = getCapsuleBuilder(memberId);
Capsule capsule = capsuleBuilder.dueDate(ZonedDateTime.now(ZoneId.of("UTC")))
.build();

List<GroupCapsuleOpen> groupCapsuleOpens = GroupCapsuleOpenFixture.groupCapsuleOpensNotOpenSpecificMemberId(
capsule, groupMembers, memberId);
setFieldValue(capsule, "id", capsuleId);
setFieldValue(capsule, "groupCapsuleOpens", groupCapsuleOpens);

return Optional.ofNullable(capsule);
}

public static Optional<Capsule> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,30 @@ public static List<GroupCapsuleOpen> groupCapsuleOpensNotAllOpened(
.flatMap(Collection::stream)
.toList();
}

public static List<GroupCapsuleOpen> groupCapsuleOpensNotOpenSpecificMemberId(
Capsule capsule,
List<Member> groupMembers,
Long memberId
) {
List<Member> 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<GroupCapsuleOpen> result = new ArrayList<>();
result.add(GroupCapsuleOpen.createOf(specificMember, capsule, false));
result.addAll(groupCapsuleOpens(true, capsule, filteredMember));

return result;
}
}
Loading

0 comments on commit b0ec7ca

Please sign in to comment.