From be7db36ae2c6294160fde6c2cb1a36c13f7a2413 Mon Sep 17 00:00:00 2001 From: Jisu Lim <69844138+Ji-soo708@users.noreply.github.com> Date: Thu, 25 Apr 2024 08:27:12 +0900 Subject: [PATCH] =?UTF-8?q?[OING-320]=20refactor:=20=EA=B0=80=EC=A1=B1=20?= =?UTF-8?q?=EC=A4=91=20=EB=AA=87=EB=AA=85=EC=9D=B4=20=EB=8D=94=20=EC=98=AC?= =?UTF-8?q?=EB=A6=AC=EB=A9=B4=20=EB=AF=B8=EC=85=98=20=EC=B0=B8=EC=97=AC=20?= =?UTF-8?q?=EA=B0=80=EB=8A=A5=ED=95=9C=EC=A7=80=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=ED=95=98=EB=8A=94=20API=20=EB=A1=9C=EC=A7=81=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=84=B0=EB=A7=81=20(#242)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: refact calculateRemainingSurvivalPostCountUntilMissuonUnlocked logic * feature: add isActiveMember method and refact code * test: refact test code due to seperated logic * test: add calculateRemainingSurvivalPostCountUntilMissionUnlocked test code --- .../repository/PostRepositoryCustomImpl.java | 23 +++++---- .../repository/PostRepositoryCustomTest.java | 15 +++--- .../com/oing/controller/PostController.java | 2 +- .../oing/repository/PostRepositoryCustom.java | 5 +- .../java/com/oing/service/PostService.java | 8 ++- .../com/oing/service/PostServiceTest.java | 51 +++++++++++++++++++ 6 files changed, 84 insertions(+), 20 deletions(-) create mode 100644 post/src/test/java/com/oing/service/PostServiceTest.java diff --git a/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java b/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java index d68c5b5d..88fe8f6c 100644 --- a/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java +++ b/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java @@ -123,7 +123,7 @@ public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId) .select(member.count()) .from(member) .where(member.familyId.eq(familyId) - .and(member.deletedAt.isNull())) + .and(isActiveMember())) .fetchFirst(); long survivalPostCount = queryFactory @@ -139,27 +139,32 @@ public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId) return survivalPostCount >= totalFamilyMembers / 2; } - public int calculateRemainingSurvivalPostCount(String familyId) { - Long familyMemberCount = queryFactory + @Override + public int countFamilyMembersByFamilyId(String familyId) { + Long count = queryFactory .select(member.id.count()) .from(member) .where(member.familyId.eq(familyId) - .and(member.deletedAt.isNull())) + .and(isActiveMember())) .fetchFirst(); + return count.intValue(); + } + @Override + public int countTodaySurvivalPostsByFamilyId(String familyId) { LocalDate today = ZonedDateTime.now().toLocalDate(); - Long todaySurvivalCount = queryFactory + Long count = queryFactory .select(post.id.count()) .from(post) .where(post.familyId.eq(familyId), post.type.eq(PostType.SURVIVAL), dateExpr(post.createdAt).eq(today)) .fetchFirst(); + return count.intValue(); + } - int requiredSurvivalPostCount = familyMemberCount != null ? (int) Math.floor(familyMemberCount / 2.0) : 0; - int todaySurvivalPostCount = todaySurvivalCount != null ? todaySurvivalCount.intValue() : 0; - - return Math.max(requiredSurvivalPostCount - todaySurvivalPostCount, 0); + private BooleanExpression isActiveMember() { + return member.deletedAt.isNull(); } private DateTimeTemplate dateExpr(DateTimePath localDateTime) { diff --git a/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java b/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java index 6506c52a..649b8ee2 100644 --- a/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java +++ b/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java @@ -154,26 +154,27 @@ void setup() { assertThat(exists).isFalse(); } - void 가족_구성원_수가_짝수인_경우_남은_생존게시글_업로드_수를_확인한다() { + @Test + void 해당_가족_구성원이_오늘_올린_생존신고_게시글_수를_조회한다() { // given String familyId = testMember1.getFamilyId(); // when - int remainingSurvivalPostCount = postRepositoryCustomImpl.calculateRemainingSurvivalPostCount(familyId); + int todaySurvivalPostCount = postRepositoryCustomImpl.countTodaySurvivalPostsByFamilyId(familyId); // then - assertThat(remainingSurvivalPostCount).isEqualTo(1); + assertThat(todaySurvivalPostCount).isEqualTo(0); } @Test - void 가족_구성원_수가_홀수인_경우_남은_생존게시글_업로드_수를_확인한다() { + void 가족_구성원_수를_조회한다() { // given - String familyId = testMember3.getFamilyId(); + String familyId = testMember1.getFamilyId(); // when - int remainingSurvivalPostCount = postRepositoryCustomImpl.calculateRemainingSurvivalPostCount(familyId); + int familyMemberCount = postRepositoryCustomImpl.countFamilyMembersByFamilyId(familyId); // then - assertThat(remainingSurvivalPostCount).isEqualTo(0); + assertThat(familyMemberCount).isEqualTo(2); } } diff --git a/post/src/main/java/com/oing/controller/PostController.java b/post/src/main/java/com/oing/controller/PostController.java index 839e405e..39969fe2 100644 --- a/post/src/main/java/com/oing/controller/PostController.java +++ b/post/src/main/java/com/oing/controller/PostController.java @@ -116,7 +116,7 @@ public MissionAvailableStatusResponse getMissionAvailableStatus(String memberId, public RemainingSurvivalPostCountResponse getRemainingSurvivalPostCount(String memberId, String loginMemberId, String loginFamilyId) { validateMemberId(loginMemberId, memberId); - int remainingSurvivalPostCount = postService.calculateRemainingSurvivalPostCount(loginFamilyId); + int remainingSurvivalPostCount = postService.calculateRemainingSurvivalPostCountUntilMissionUnlocked(loginFamilyId); return new RemainingSurvivalPostCountResponse(remainingSurvivalPostCount); } diff --git a/post/src/main/java/com/oing/repository/PostRepositoryCustom.java b/post/src/main/java/com/oing/repository/PostRepositoryCustom.java index 36377133..85de5c0a 100644 --- a/post/src/main/java/com/oing/repository/PostRepositoryCustom.java +++ b/post/src/main/java/com/oing/repository/PostRepositoryCustom.java @@ -23,5 +23,8 @@ QueryResults searchPosts(int page, int size, LocalDate date, String member boolean existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(String memberId, String familyId, PostType type, LocalDate postDate); boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId); - int calculateRemainingSurvivalPostCount(String familyId); + + int countFamilyMembersByFamilyId(String familyId); + + int countTodaySurvivalPostsByFamilyId(String familyId); } diff --git a/post/src/main/java/com/oing/service/PostService.java b/post/src/main/java/com/oing/service/PostService.java index ec21da2b..27f00686 100644 --- a/post/src/main/java/com/oing/service/PostService.java +++ b/post/src/main/java/com/oing/service/PostService.java @@ -165,7 +165,11 @@ public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId) return postRepository.isCreatedSurvivalPostByMajority(date, familyId); } - public int calculateRemainingSurvivalPostCount(String familyId) { - return postRepository.calculateRemainingSurvivalPostCount(familyId); + public int calculateRemainingSurvivalPostCountUntilMissionUnlocked(String familyId) { + int familyMemberCount = postRepository.countFamilyMembersByFamilyId(familyId); + int requiredSurvivalPostCount = familyMemberCount / 2; + int todaySurvivalPostCount = postRepository.countTodaySurvivalPostsByFamilyId(familyId); + + return Math.max(requiredSurvivalPostCount - todaySurvivalPostCount, 0); } } diff --git a/post/src/test/java/com/oing/service/PostServiceTest.java b/post/src/test/java/com/oing/service/PostServiceTest.java new file mode 100644 index 00000000..39179aee --- /dev/null +++ b/post/src/test/java/com/oing/service/PostServiceTest.java @@ -0,0 +1,51 @@ +package com.oing.service; + +import com.oing.domain.Emoji; +import com.oing.domain.Post; +import com.oing.domain.PostType; +import com.oing.domain.Reaction; +import com.oing.dto.request.PostReactionRequest; +import com.oing.repository.PostRepository; +import com.oing.repository.ReactionRepository; +import com.oing.util.IdentityGenerator; +import com.oing.util.PreSignedUrlGenerator; +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 org.springframework.context.ApplicationEventPublisher; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class PostServiceTest { + + @InjectMocks + private PostService postService; + + @Mock + private PostRepository postRepository; + @Mock + private ApplicationEventPublisher applicationEventPublisher; + @Mock + private PreSignedUrlGenerator preSignedUrlGenerator; + @Mock + private MissionBridge missionBridge; + @Mock + private IdentityGenerator identityGenerator; + + @Test + void 미션_키_획득까지_남은_생존신고_게시글_업로드_수_조회_테스트() { + //given + String familyId = "1"; + + //when + when(postRepository.countFamilyMembersByFamilyId(familyId)).thenReturn(1); + when(postRepository.countTodaySurvivalPostsByFamilyId(familyId)).thenReturn(0); + + //then + assertEquals(0, postService.calculateRemainingSurvivalPostCountUntilMissionUnlocked(familyId)); + } +}