Skip to content

Commit

Permalink
feat: 반복 주기 = Year 조회 기능 구현 in CalendarServiceUtil
Browse files Browse the repository at this point in the history
Resolves: #38
  • Loading branch information
jiwon83 committed Nov 17, 2024
1 parent 979305d commit 014915d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ private static List<ScheduleFindResponse> getMonthRepetitionSchedules(Schedule s
return responses;
}

private static List<ScheduleFindResponse> getYearRepetitionSchedule(Schedule schedule, LocalDate startDate, LocalDate endDate) {
List<ScheduleFindResponse> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CalendarServiceUtilTest {
private Schedule weekRepetitionScheduleWithDayOfWeek;
private Schedule weekRepetitionScheduleWithoutDayOfWeek;
private Schedule monthRepetitionSchedule;
private Schedule yearRepetitionSchedule;

@BeforeEach
void setUpDayRepetitionSchedule() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<ScheduleFindResponse> 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<ScheduleFindResponse> 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<ScheduleFindResponse> scheduleResponses = CalendarServiceUtil.getScheduleResponses(
yearRepetitionSchedule, startDate, endDate);

// then
assertEquals(3, scheduleResponses.size());

}
}

}

0 comments on commit 014915d

Please sign in to comment.