Skip to content

Commit

Permalink
Added a test case to handle an edge case when start date and end date…
Browse files Browse the repository at this point in the history
… are equal
  • Loading branch information
masoudarvishian committed May 10, 2024
1 parent 8fcf796 commit 8e90d67
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ private static void setJobShifts(Job job, LocalDate startDate, LocalDate endDate
}

private static void validateDates(LocalDate startDate, LocalDate endDate) {

if (startDate.isEqual(endDate)) {
throw new IllegalArgumentException("Start date and end date should not be equal");
}

if (startDate.isBefore(LocalDate.now()))
throw new InvalidStartDateException("Start date cannot be before now");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.UUID;

@RestController
@RequestMapping(path = "/job")
@RequestMapping(path = "/api/v1/job")
@RequiredArgsConstructor
public class JobController {
private final JobService jobService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.stream.Collectors;

@RestController
@RequestMapping(path = "/shift")
@RequestMapping(path = "/api/v1/shift")
@RequiredArgsConstructor
public class ShiftController {
private final ShiftService shiftService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ ResponseEntity<Object> handleCustomException(NotFoundException ex) {
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(IllegalArgumentException.class)
ResponseEntity<Object> handleCustomException(IllegalArgumentException ex) {
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(Exception.class)
ResponseEntity<Object> handleAllExceptions(Exception ex) {
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public void start_date_cannot_be_in_the_past() {
jobService.createJob(UUID.randomUUID(), startDate, endDate));
}

@Test
public void start_date_should_not_be_equal_to_end_date() {
// given
LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.now();

// when - then
Assertions.assertThrows(IllegalArgumentException.class, () ->
jobService.createJob(UUID.randomUUID(), startDate, endDate));
}

@Test
public void the_end_date_should_be_after_the_start_date() {
// given
Expand Down

0 comments on commit 8e90d67

Please sign in to comment.