From 80cd9012005906344f771aafc6d09f0c4b684421 Mon Sep 17 00:00:00 2001 From: tingSHI Date: Fri, 5 Jul 2024 17:37:59 +0200 Subject: [PATCH 1/3] Backend: update for search - Add InvalidReminderException for createAReminder, to avoid creating a empty-Reminder --- .../backend/exception/GlobalExceptionHandler.java | 6 ++++++ .../backend/exception/InvalidReminderException.java | 7 +++++++ .../tingshi2015/backend/reminder/ReminderService.java | 4 ++++ 3 files changed, 17 insertions(+) create mode 100644 backend/src/main/java/com/github/tingshi2015/backend/exception/InvalidReminderException.java 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); } From 868566e84ef2e1f76e8cf46d01d18a66979ab15f Mon Sep 17 00:00:00 2001 From: tingSHI Date: Fri, 5 Jul 2024 18:30:25 +0200 Subject: [PATCH 2/3] Backend: -Add Unit Test for CreatAReminder withAllFieldsNull --- .../backend/reminder/ReminderServiceTest.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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..c6b9662 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,23 @@ 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()); + } From fae25c530cd7aae8bb1e354831b53882259f7190 Mon Sep 17 00:00:00 2001 From: tingSHI Date: Fri, 5 Jul 2024 18:54:51 +0200 Subject: [PATCH 3/3] Backend: -Add Unit Test for CreatAReminder withNullName -Delete insignificant {} for lambda! --- .../backend/reminder/ReminderServiceTest.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) 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 c6b9662..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 @@ -61,9 +61,8 @@ void createAReminder_withAllFieldsNull_shouldThrowInvalidReminderException() { ReminderDTO newReminder = new ReminderDTO(null, null, null); //WHEN - InvalidReminderException exception = assertThrows(InvalidReminderException.class, () -> { - reminderService.createAReminder(newReminder); - }); + InvalidReminderException exception = assertThrows(InvalidReminderException.class, () -> reminderService.createAReminder(newReminder)); + //THEN verify(reminderRepository, never()).save(any(Reminder.class)); @@ -71,6 +70,26 @@ void createAReminder_withAllFieldsNull_shouldThrowInvalidReminderException() { } + @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); + + } + @Test void deleteAReminder_shouldInvokeRepositoryDeleteById_withValidId() {