diff --git a/src/main/java/com/dnd/runus/application/challenge/ChallengeService.java b/src/main/java/com/dnd/runus/application/challenge/ChallengeService.java index 03a6bdfd..9cdbdf94 100644 --- a/src/main/java/com/dnd/runus/application/challenge/ChallengeService.java +++ b/src/main/java/com/dnd/runus/application/challenge/ChallengeService.java @@ -8,7 +8,10 @@ import java.time.LocalDate; import java.time.OffsetDateTime; +import java.util.Collections; import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; import static com.dnd.runus.global.constant.TimeConstant.SERVER_TIMEZONE_ID; @@ -26,15 +29,22 @@ public List getChallenges(long memberId) { .toOffsetDateTime(); OffsetDateTime yesterday = todayMidnight.minusDays(1); + List challengesResponses; // 어제 기록이 없으면 if (!runningRecordRepository.hasByMemberIdAndStartAtBetween(memberId, yesterday, todayMidnight)) { - return challengeRepository.findAllIsNotDefeatYesterday().stream() + challengesResponses = challengeRepository.findAllIsNotDefeatYesterday().stream() .map(ChallengesResponse::from) - .toList(); + .collect(Collectors.toList()); + } else { + challengesResponses = challengeRepository.findAllChallenges().stream() + .map(ChallengesResponse::from) + .collect(Collectors.toList()); } - return challengeRepository.findAllChallenges().stream() - .map(ChallengesResponse::from) - .toList(); + // 랜덤으로 2개 리턴 + Random randomWithSeed = new Random(todayMidnight.toEpochSecond()); + Collections.shuffle(challengesResponses, randomWithSeed); + + return challengesResponses.subList(0, 2); } } diff --git a/src/test/java/com/dnd/runus/application/challenge/ChallengeDataServiceTest.java b/src/test/java/com/dnd/runus/application/challenge/ChallengeServiceTest.java similarity index 89% rename from src/test/java/com/dnd/runus/application/challenge/ChallengeDataServiceTest.java rename to src/test/java/com/dnd/runus/application/challenge/ChallengeServiceTest.java index cf117bb0..80b4c88b 100644 --- a/src/test/java/com/dnd/runus/application/challenge/ChallengeDataServiceTest.java +++ b/src/test/java/com/dnd/runus/application/challenge/ChallengeServiceTest.java @@ -18,11 +18,11 @@ import java.util.List; import static com.dnd.runus.global.constant.TimeConstant.SERVER_TIMEZONE_ID; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; @ExtendWith(MockitoExtension.class) -class ChallengeDataServiceTest { +class ChallengeServiceTest { @Mock private RunningRecordRepository runningRecordRepository; @@ -44,7 +44,7 @@ void setUp() { member = new Member(MemberRole.USER, "nickname"); } - @DisplayName("어제 기록이 있는경우 챌린지 리스트 조회 : 챌린지 name에 '어제'값이 포함한 값이 있어야함") + @DisplayName("어제 기록이 있는경우 챌린지 리스트 조회 : 챌린지 리스트 크기가 2이어야함") @Test void getChallengesWithYesterdayRecords() { // given @@ -65,10 +65,10 @@ void getChallengesWithYesterdayRecords() { List challenges = challengeService.getChallenges(member.memberId()); // then - assertTrue(challenges.stream().anyMatch(c -> c.title().contains("어제"))); + assertThat(challenges.size()).isEqualTo(2); } - @DisplayName("어제 기록이 없는 경우 챌린지 리스트 조회 : 챌린지 name에 '어제'값이 포함한 값이 없어야함") + @DisplayName("어제 기록이 없는 경우 챌린지 리스트 조회 : 챌린지 리스트 크기가 2이어야함") @Test void getChallengesWithoutYesterdayRecords() { // given @@ -85,6 +85,6 @@ void getChallengesWithoutYesterdayRecords() { List challenges = challengeService.getChallenges(member.memberId()); // then - assertTrue(challenges.stream().noneMatch(c -> c.title().contains("어제"))); + assertThat(challenges.size()).isEqualTo(2); } }