diff --git a/backend/src/main/java/com/github/tingshi2015/backend/exception/GlobalExceptionHandler.java b/backend/src/main/java/com/github/tingshi2015/backend/exception/GlobalExceptionHandler.java index 9bedc46..820d39b 100644 --- a/backend/src/main/java/com/github/tingshi2015/backend/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/com/github/tingshi2015/backend/exception/GlobalExceptionHandler.java @@ -15,4 +15,10 @@ public class GlobalExceptionHandler { public ErrorMessage handleNoSuchElementException(NoSuchElementException exception){ return new ErrorMessage(exception.getMessage()); } + + @ExceptionHandler(InvalidReminderException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ErrorMessage handleInvalidReminderException(InvalidReminderException exception){ + return new ErrorMessage(exception.getMessage()); + } } diff --git a/backend/src/main/java/com/github/tingshi2015/backend/exception/InvalidReminderException.java b/backend/src/main/java/com/github/tingshi2015/backend/exception/InvalidReminderException.java new file mode 100644 index 0000000..bf5127d --- /dev/null +++ b/backend/src/main/java/com/github/tingshi2015/backend/exception/InvalidReminderException.java @@ -0,0 +1,7 @@ +package com.github.tingshi2015.backend.exception; + +public class InvalidReminderException extends RuntimeException{ + public InvalidReminderException(String message){ + super(message); + } +} diff --git a/backend/src/main/java/com/github/tingshi2015/backend/reminder/ReminderService.java b/backend/src/main/java/com/github/tingshi2015/backend/reminder/ReminderService.java index 2508cbc..f969d07 100644 --- a/backend/src/main/java/com/github/tingshi2015/backend/reminder/ReminderService.java +++ b/backend/src/main/java/com/github/tingshi2015/backend/reminder/ReminderService.java @@ -1,5 +1,6 @@ package com.github.tingshi2015.backend.reminder; +import com.github.tingshi2015.backend.exception.InvalidReminderException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -30,6 +31,9 @@ public List getAllReminders() { } public Reminder createAReminder(ReminderDTO reminderDTO) { + if(reminderDTO.name() == null && reminderDTO.time() == null && reminderDTO.date() == null){ + throw new InvalidReminderException("Reminder must have at least one non-null filed!"); + } Reminder reminderToRepo = convertToEntity(reminderDTO); return reminderRepository.save(reminderToRepo); } diff --git a/backend/src/test/java/com/github/tingshi2015/backend/reminder/ReminderServiceTest.java b/backend/src/test/java/com/github/tingshi2015/backend/reminder/ReminderServiceTest.java index 7b51221..6e21c89 100644 --- a/backend/src/test/java/com/github/tingshi2015/backend/reminder/ReminderServiceTest.java +++ b/backend/src/test/java/com/github/tingshi2015/backend/reminder/ReminderServiceTest.java @@ -1,5 +1,6 @@ package com.github.tingshi2015.backend.reminder; +import com.github.tingshi2015.backend.exception.InvalidReminderException; import org.junit.jupiter.api.Test; import java.time.LocalDate; @@ -36,7 +37,7 @@ void getAllReminders() { } @Test - void createAReminder() { + void createAReminder_withValidFields_shouldSaveReminder() { //GIVEN ReminderDTO newReminder = new ReminderDTO("name1", LocalTime.of(17, 0, 0), LocalDate.of(2024, 6, 25)); Reminder reminderToSave = new Reminder("id1", "name1", LocalTime.of(17, 0, 0), LocalDate.of(2024, 6, 25)); @@ -51,6 +52,42 @@ void createAReminder() { verify(idService).randomId(); verify(reminderRepository).save(reminderToSave); assertEquals(reminderToSave, actual); + assertNotNull(actual); + } + + @Test + void createAReminder_withAllFieldsNull_shouldThrowInvalidReminderException() { + //GIVEN + ReminderDTO newReminder = new ReminderDTO(null, null, null); + + //WHEN + InvalidReminderException exception = assertThrows(InvalidReminderException.class, () -> reminderService.createAReminder(newReminder)); + + + //THEN + verify(reminderRepository, never()).save(any(Reminder.class)); + assertEquals("Reminder must have at least one non-null filed!", exception.getMessage()); + + } + + @Test + void createAReminder_withNullName_shouldSaveReminder() { + //GIVEN + ReminderDTO newReminder = new ReminderDTO(null, LocalTime.of(17, 0, 0), LocalDate.now()); + Reminder reminderToSave = new Reminder("id1", null, LocalTime.of(17, 0, 0), LocalDate.now()); + + when(idService.randomId()).thenReturn("id1"); + when(reminderRepository.save(any(Reminder.class))).thenReturn(reminderToSave); + + //WHEN + Reminder actual = reminderService.createAReminder(newReminder); + + //THEN + verify(idService).randomId(); + verify(reminderRepository).save(reminderToSave); + assertEquals(reminderToSave, actual); + assertNotNull(actual); + }