From 014915dd37d254f9a69081852e9c91fa0a5b3618 Mon Sep 17 00:00:00 2001 From: jiwonhan Date: Sun, 17 Nov 2024 16:19:28 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=B0=98=EB=B3=B5=20=EC=A3=BC=EA=B8=B0?= =?UTF-8?q?=20=3D=20Year=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20in=20CalendarServiceUtil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves: #38 --- .../calendar/CalendarServiceUtil.java | 19 +++++ .../calendar/CalendarServiceUtilTest.java | 70 +++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/application/wypl-core/src/main/java/com/wypl/wyplcore/calendar/CalendarServiceUtil.java b/application/wypl-core/src/main/java/com/wypl/wyplcore/calendar/CalendarServiceUtil.java index 8e0a615..b4c7747 100644 --- a/application/wypl-core/src/main/java/com/wypl/wyplcore/calendar/CalendarServiceUtil.java +++ b/application/wypl-core/src/main/java/com/wypl/wyplcore/calendar/CalendarServiceUtil.java @@ -151,6 +151,25 @@ private static List getMonthRepetitionSchedules(Schedule s return responses; } + private static List getYearRepetitionSchedule(Schedule schedule, LocalDate startDate, LocalDate endDate) { + List responses = new ArrayList<>(); + + startDate = getMaxDate(startDate, schedule.getRepetitionStartDate()); + endDate = getMinDate(endDate, schedule.getRepetitionEndDate()); + + // startDate와 같거나 가장 가까운 schedule.endDateTime의 날짜, 시간을 가진 LocalDateTime 생성 + LocalDateTime nearestEndDateTime = LocalDateTime.of(startDate.withDayOfYear(schedule.getEndDateTime().getDayOfYear()), schedule.getEndDateTime().toLocalTime()); + LocalDateTime nearestStartDateTime = nearestEndDateTime.minus(Duration.between(schedule.getStartDateTime(), schedule.getEndDateTime())); + + for(LocalDate date = nearestStartDateTime.toLocalDate(); !date.isAfter(endDate); date = date.plusYears(1)) { + LocalDateTime startDateTime = LocalDateTime.of(date, schedule.getStartDateTime().toLocalTime()); + LocalDateTime endDateTime = startDateTime.plus(Duration.between(schedule.getStartDateTime(), schedule.getEndDateTime())); + responses.add(ScheduleFindResponse.of(schedule, startDateTime, endDateTime)); + } + + return responses; + } + /** * repetitionDayOfWeek에 해당하는 요일이 선택되었는지 확인 * @param repetitionDayOfWeek of Schedule diff --git a/application/wypl-core/src/test/java/com/wypl/wyplcore/calendar/CalendarServiceUtilTest.java b/application/wypl-core/src/test/java/com/wypl/wyplcore/calendar/CalendarServiceUtilTest.java index 5df5569..b1a8702 100644 --- a/application/wypl-core/src/test/java/com/wypl/wyplcore/calendar/CalendarServiceUtilTest.java +++ b/application/wypl-core/src/test/java/com/wypl/wyplcore/calendar/CalendarServiceUtilTest.java @@ -25,6 +25,7 @@ class CalendarServiceUtilTest { private Schedule weekRepetitionScheduleWithDayOfWeek; private Schedule weekRepetitionScheduleWithoutDayOfWeek; private Schedule monthRepetitionSchedule; + private Schedule yearRepetitionSchedule; @BeforeEach void setUpDayRepetitionSchedule() { @@ -91,6 +92,22 @@ void setUpMonthRepetitionSchedule() { when(monthRepetitionSchedule.isRepetition()).thenReturn(true); } + @BeforeEach + void setUpYearRepetitionSchedule() { + yearRepetitionSchedule = mock(Schedule.class); + when(yearRepetitionSchedule.getId()).thenReturn(1L); + when(yearRepetitionSchedule.getTitle()).thenReturn(ScheduleFixture.YEARLY_SCHEDULE.getTitle()); + when(yearRepetitionSchedule.getDescription()).thenReturn(ScheduleFixture.YEARLY_SCHEDULE.getDescription()); + when(yearRepetitionSchedule.getStartDateTime()).thenReturn(ScheduleFixture.YEARLY_SCHEDULE.getStartDateTime()); + when(yearRepetitionSchedule.getEndDateTime()).thenReturn(ScheduleFixture.YEARLY_SCHEDULE.getEndDateTime()); + when(yearRepetitionSchedule.getRepetitionStartDate()).thenReturn(ScheduleFixture.YEARLY_SCHEDULE.getRepetitionStartDate()); + when(yearRepetitionSchedule.getRepetitionEndDate()).thenReturn(ScheduleFixture.YEARLY_SCHEDULE.getRepetitionEndDate()); + when(yearRepetitionSchedule.getRepetitionCycle()).thenReturn(ScheduleFixture.YEARLY_SCHEDULE.getRepetitionCycle()); + when(yearRepetitionSchedule.getDayOfWeek()).thenReturn(ScheduleFixture.YEARLY_SCHEDULE.getDayOfWeek()); + when(yearRepetitionSchedule.getWeekInterval()).thenReturn(ScheduleFixture.YEARLY_SCHEDULE.getWeekInterval()); + when(yearRepetitionSchedule.isRepetition()).thenReturn(true); + } + @Nested class DayRepetitionTest { @@ -316,4 +333,57 @@ void getSchedulesResponsesForMonth(){ } } + @Nested + class YearRepetitionTest{ + + @Test + @DisplayName("오늘 검색 조건으로 반복일정 조회 - 결과가 없는 경우") + void getSchedulesResponsesForNoResult(){ + + // given + LocalDate startDate = ScheduleFixture.YEARLY_SCHEDULE.getRepetitionStartDate().with(TemporalAdjusters.firstDayOfYear()); + + // when + List scheduleResponses = CalendarServiceUtil.getScheduleResponses( + yearRepetitionSchedule, startDate, startDate); + + // then + assertEquals(0, scheduleResponses.size()); + + } + + @Test + @DisplayName("오늘 검색 조건으로 반복일정 조회 - 결과가 1건인 경우") + void getSchedulesResponsesForToday(){ + + // given + LocalDate startDate = ScheduleFixture.YEARLY_SCHEDULE.getRepetitionStartDate().plusYears(1).plusDays(1); + + // when + List scheduleResponses = CalendarServiceUtil.getScheduleResponses( + yearRepetitionSchedule, startDate, startDate); + + // then + assertEquals(1, scheduleResponses.size()); + + } + + @Test + @DisplayName("Year 검색 조건으로 반복일정 조회") + void getSchedulesResponsesForYear(){ + + // given + LocalDate startDate = ScheduleFixture.YEARLY_SCHEDULE.getRepetitionStartDate().plusDays(1); + LocalDate endDate = startDate.plusYears(2); + + // when + List scheduleResponses = CalendarServiceUtil.getScheduleResponses( + yearRepetitionSchedule, startDate, endDate); + + // then + assertEquals(3, scheduleResponses.size()); + + } + } + } \ No newline at end of file