diff --git a/backend/src/test/java/com/github/mdeluise/plantit/unit/controller/ReminderControllerUnitTests.java b/backend/src/test/java/com/github/mdeluise/plantit/unit/controller/ReminderControllerUnitTests.java index 3070a304..75d38068 100644 --- a/backend/src/test/java/com/github/mdeluise/plantit/unit/controller/ReminderControllerUnitTests.java +++ b/backend/src/test/java/com/github/mdeluise/plantit/unit/controller/ReminderControllerUnitTests.java @@ -1,7 +1,10 @@ package com.github.mdeluise.plantit.unit.controller; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Date; +import java.util.List; import com.github.mdeluise.plantit.exception.ResourceNotFoundException; import com.github.mdeluise.plantit.exception.UnauthorizedException; @@ -10,6 +13,9 @@ import com.github.mdeluise.plantit.reminder.ReminderDTO; import com.github.mdeluise.plantit.reminder.ReminderDTOConverter; import com.github.mdeluise.plantit.reminder.ReminderService; +import com.github.mdeluise.plantit.reminder.occurrence.ReminderOccurrence; +import com.github.mdeluise.plantit.reminder.occurrence.ReminderOccurrenceDTO; +import com.github.mdeluise.plantit.reminder.occurrence.ReminderOccurrenceDTOConverter; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -18,6 +24,11 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -28,6 +39,8 @@ class ReminderControllerUnitTests { private ReminderService reminderService; @Mock private ReminderDTOConverter reminderDTOConverter; + @Mock + private ReminderOccurrenceDTOConverter reminderOccurrenceDTOConverter; @InjectMocks private ReminderController reminderController; @@ -39,7 +52,7 @@ void testGetAllReminders() { final ReminderDTO reminderDto = new ReminderDTO(); Mockito.when(reminderService.getAll()).thenReturn(Collections.singletonList(reminder)); Mockito.when(reminderDTOConverter.convertToDTO(reminder)).thenReturn(reminderDto); - ResponseEntity> responseEntity = reminderController.getAll(); + final ResponseEntity> responseEntity = reminderController.getAll(); Assertions.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Assertions.assertEquals(1, responseEntity.getBody().size()); @@ -123,10 +136,29 @@ void testSaveInvalidReminder() { @DisplayName("Test updateInternal non-existing reminder") void testUpdateNonExistingReminder() { final long id = 1; - ReminderDTO reminderDTO = new ReminderDTO(); + final ReminderDTO reminderDTO = new ReminderDTO(); Mockito.when(reminderService.update(id, reminderDTOConverter.convertFromDTO(reminderDTO))) .thenThrow(ResourceNotFoundException.class); Assertions.assertThrows(ResourceNotFoundException.class, () -> reminderController.update(id, reminderDTO)); } + + + @Test + @DisplayName("Test get occurrences") + void testGetOccurrences() { + final Date from = new Date(); + final Date to = new Date(); + final Pageable pageable = PageRequest.of(0, 25, Sort.by("id")); + final List pageContent = new ArrayList<>(); + final ReminderOccurrence reminderOccurrence = new ReminderOccurrence(); + final ReminderOccurrenceDTO reminderOccurrenceDTO = new ReminderOccurrenceDTO(); + pageContent.add(reminderOccurrence); + final Page occurrences = new PageImpl<>(pageContent); + Mockito.when(reminderService.getAllOccurrences(null, null, from, to, pageable)).thenReturn(occurrences); + Mockito.when(reminderOccurrenceDTOConverter.convertToDTO(reminderOccurrence)).thenReturn(reminderOccurrenceDTO); + + Assertions.assertEquals(reminderController.getOccurrences(null, null, from, to, pageable) + .getBody().getTotalElements(), 1); + } } \ No newline at end of file diff --git a/backend/src/test/java/com/github/mdeluise/plantit/unit/service/ReminderOccurrenceServiceUnitTests.java b/backend/src/test/java/com/github/mdeluise/plantit/unit/service/ReminderOccurrenceServiceUnitTests.java new file mode 100644 index 00000000..6ba1912c --- /dev/null +++ b/backend/src/test/java/com/github/mdeluise/plantit/unit/service/ReminderOccurrenceServiceUnitTests.java @@ -0,0 +1,210 @@ +package com.github.mdeluise.plantit.unit.service; + +import java.util.Calendar; +import java.util.Collection; +import java.util.Date; +import java.util.Optional; + +import com.github.mdeluise.plantit.diary.entry.DiaryEntryService; +import com.github.mdeluise.plantit.diary.entry.DiaryEntryType; +import com.github.mdeluise.plantit.plant.Plant; +import com.github.mdeluise.plantit.reminder.Reminder; +import com.github.mdeluise.plantit.reminder.frequency.Frequency; +import com.github.mdeluise.plantit.reminder.frequency.Unit; +import com.github.mdeluise.plantit.reminder.occurrence.ReminderOccurrence; +import com.github.mdeluise.plantit.reminder.occurrence.ReminderOccurrenceService; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@DisplayName("Unit tests for ReminderOccurrenceService") +public class ReminderOccurrenceServiceUnitTests { + @Mock + private DiaryEntryService diaryEntryService; + @InjectMocks + private ReminderOccurrenceService service; + + + @Test + @DisplayName("Should get no occurrence if reminder starts after end date") + void shouldGetNoOccurrenceIfReminderStartsAfterEndDate() { + final Date start = new Date(); + final Date end = addToDate(new Date(), Calendar.SECOND, 1); + final Reminder reminder = new Reminder(); + reminder.setStart(addToDate(new Date(), Calendar.SECOND, 2)); + reminder.setEnabled(true); + + final Collection occurrences = service.getOccurrences(reminder, start, end); + + Assertions.assertThat(occurrences).isEmpty(); + } + + + @Test + @DisplayName("Should get no occurrence if reminder end before start date") + void shouldCreateReminderOccurrence() { + final Date reminderStartDate = addToDate(new Date(), Calendar.HOUR, -1); + final Date reminderEndDate = addToDate(new Date(), Calendar.MINUTE, -1); + final Date start = new Date(); + final Date end = addToDate(new Date(), Calendar.HOUR, 1); + final Reminder reminder = new Reminder(); + reminder.setStart(reminderStartDate); + reminder.setEnd(reminderEndDate); + reminder.setEnabled(true); + + final Collection occurrences = service.getOccurrences(reminder, start, end); + + Assertions.assertThat(occurrences).isEmpty(); + } + + + @Test + @DisplayName("Should get no occurrence if reminder is disabled") + void shouldGetNoOccurrenceIfReminderIsDisabled() { + final Date reminderStartDate = addToDate(new Date(), Calendar.DATE, -2); + final Date start = addToDate(reminderStartDate, Calendar.DATE, 1); + final Date end = addToDate(start, Calendar.DATE, 2); + final Frequency frequency = new Frequency(); + frequency.setUnit(Unit.DAYS); + frequency.setQuantity(1); + final long plantId = 42; + final DiaryEntryType type = DiaryEntryType.WATERING; + final Plant target = new Plant(); + target.setId(plantId); + final Reminder reminder = new Reminder(); + reminder.setStart(reminderStartDate); + reminder.setFrequency(frequency); + reminder.setEnabled(false); + reminder.setAction(type); + reminder.setTarget(target); + Mockito.when(diaryEntryService.getLast(plantId, type)).thenReturn(Optional.empty()); + + final Collection occurrences = service.getOccurrences(reminder, start, end); + + Assertions.assertThat(occurrences).isEmpty(); + } + + + @Test + @DisplayName("Should get the occurrences correctly when reminder has no end date") + void shouldGetOccurrencesCorrectlyWhenReminderHasNoEndDate() { + final Date reminderStartDate = addToDate(new Date(), Calendar.DATE, -2); + final Date start = addToDate(reminderStartDate, Calendar.DATE, 1); + final Date end = addToDate(start, Calendar.DATE, 2); + final Frequency frequency = new Frequency(); + frequency.setUnit(Unit.DAYS); + frequency.setQuantity(1); + final long plantId = 42; + final DiaryEntryType type = DiaryEntryType.WATERING; + final Plant target = new Plant(); + target.setId(plantId); + final Reminder reminder = new Reminder(); + reminder.setStart(reminderStartDate); + reminder.setFrequency(frequency); + reminder.setEnabled(true); + reminder.setAction(type); + reminder.setTarget(target); + Mockito.when(diaryEntryService.getLast(plantId, type)).thenReturn(Optional.empty()); + + final Collection occurrences = service.getOccurrences(reminder, start, end); + + Assertions.assertThat(occurrences).hasSize(1); + } + + + @Test + @DisplayName("Should get the occurrences correctly when reminder has end date") + void shouldGetOccurrencesCorrectlyWhenReminderHasEndDate() { + final Date reminderStartDate = addToDate(new Date(), Calendar.DATE, -4); + final Date reminderEndDate = addToDate(new Date(), Calendar.DATE, 2); + final Date start = addToDate(reminderStartDate, Calendar.DATE, 1); + final Date end = addToDate(start, Calendar.DATE, 2); + final Frequency frequency = new Frequency(); + frequency.setUnit(Unit.DAYS); + frequency.setQuantity(1); + final long plantId = 42; + final DiaryEntryType type = DiaryEntryType.WATERING; + final Plant target = new Plant(); + target.setId(plantId); + final Reminder reminder = new Reminder(); + reminder.setStart(reminderStartDate); + reminder.setEnd(reminderEndDate); + reminder.setFrequency(frequency); + reminder.setEnabled(true); + reminder.setAction(type); + reminder.setTarget(target); + Mockito.when(diaryEntryService.getLast(plantId, type)).thenReturn(Optional.empty()); + + final Collection occurrences = service.getOccurrences(reminder, start, end); + + Assertions.assertThat(occurrences).hasSize(1); + } + + + @Test + @DisplayName("Should get all the occurrences correctly") + void shouldGetAllOccurrencesCorrectly() { + final Date reminderStartDate = addToDate(new Date(), Calendar.MONTH, -2); + final Date start = addToDate(reminderStartDate, Calendar.MONTH, 2); + final Date end = addToDate(start, Calendar.MONTH, 3); + final Frequency frequency = new Frequency(); + frequency.setUnit(Unit.MONTHS); + frequency.setQuantity(1); + final long plantId = 42; + final DiaryEntryType type = DiaryEntryType.WATERING; + final Plant target = new Plant(); + target.setId(plantId); + final Reminder reminder = new Reminder(); + reminder.setStart(reminderStartDate); + reminder.setFrequency(frequency); + reminder.setEnabled(true); + reminder.setAction(type); + reminder.setTarget(target); + Mockito.when(diaryEntryService.getLast(plantId, type)).thenReturn(Optional.empty()); + + final Collection occurrences = service.getOccurrences(reminder, start, end); + + Assertions.assertThat(occurrences).hasSize(2); + } + + + @Test + @DisplayName("Should get all the occurrences correctly with different frequency amount") + void shouldGetAllOccurrencesCorrectlyWithDifferentFrequencyAmount() { + final Date reminderStartDate = addToDate(new Date(), Calendar.DATE, -2); + final Date start = addToDate(reminderStartDate, Calendar.DATE, 2); + final Date end = addToDate(start, Calendar.DATE, 3); + final Frequency frequency = new Frequency(); + frequency.setUnit(Unit.DAYS); + frequency.setQuantity(2); + final long plantId = 42; + final DiaryEntryType type = DiaryEntryType.WATERING; + final Plant target = new Plant(); + target.setId(plantId); + final Reminder reminder = new Reminder(); + reminder.setStart(reminderStartDate); + reminder.setFrequency(frequency); + reminder.setEnabled(true); + reminder.setAction(type); + reminder.setTarget(target); + Mockito.when(diaryEntryService.getLast(plantId, type)).thenReturn(Optional.empty()); + + final Collection occurrences = service.getOccurrences(reminder, start, end); + + Assertions.assertThat(occurrences).hasSize(1); + } + + + private Date addToDate(Date date, int unit, int amount) { + final Calendar c = Calendar.getInstance(); + c.setTime(date); + c.add(unit, amount); + return c.getTime(); + } +}