Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: update for search #23

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.tingshi2015.backend.exception;

public class InvalidReminderException extends RuntimeException{
public InvalidReminderException(String message){
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -30,6 +31,9 @@ public List<Reminder> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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);

}


Expand Down
Loading