From 8e90d676245a1713beaa5923040e7b1bd537f45d Mon Sep 17 00:00:00 2001 From: masoudarvishian Date: Fri, 10 May 2024 16:59:43 +0200 Subject: [PATCH] Added a test case to handle an edge case when start date and end date are equal --- .../application/services/JobServiceImpl.java | 5 +++++ .../challenge/rest_api/controller/JobController.java | 2 +- .../rest_api/controller/ShiftController.java | 2 +- .../exceptionhandler/GlobalExceptionHandler.java | 6 ++++++ .../application/services/JobServiceTests.java | 11 +++++++++++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java b/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java index a7cd014..1999c7c 100644 --- a/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java +++ b/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java @@ -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"); diff --git a/src/main/java/com/zenjob/challenge/rest_api/controller/JobController.java b/src/main/java/com/zenjob/challenge/rest_api/controller/JobController.java index 24c8517..d3033f8 100644 --- a/src/main/java/com/zenjob/challenge/rest_api/controller/JobController.java +++ b/src/main/java/com/zenjob/challenge/rest_api/controller/JobController.java @@ -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; diff --git a/src/main/java/com/zenjob/challenge/rest_api/controller/ShiftController.java b/src/main/java/com/zenjob/challenge/rest_api/controller/ShiftController.java index fea750d..6ee44ce 100644 --- a/src/main/java/com/zenjob/challenge/rest_api/controller/ShiftController.java +++ b/src/main/java/com/zenjob/challenge/rest_api/controller/ShiftController.java @@ -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; diff --git a/src/main/java/com/zenjob/challenge/rest_api/exceptionhandler/GlobalExceptionHandler.java b/src/main/java/com/zenjob/challenge/rest_api/exceptionhandler/GlobalExceptionHandler.java index 6e336d8..10fb528 100644 --- a/src/main/java/com/zenjob/challenge/rest_api/exceptionhandler/GlobalExceptionHandler.java +++ b/src/main/java/com/zenjob/challenge/rest_api/exceptionhandler/GlobalExceptionHandler.java @@ -38,6 +38,12 @@ ResponseEntity handleCustomException(NotFoundException ex) { return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND); } + @ExceptionHandler(IllegalArgumentException.class) + ResponseEntity handleCustomException(IllegalArgumentException ex) { + ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.BAD_REQUEST.value(), ex.getMessage()); + return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); + } + @ExceptionHandler(Exception.class) ResponseEntity handleAllExceptions(Exception ex) { ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage()); diff --git a/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java b/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java index f33e6d2..7db2e36 100644 --- a/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java +++ b/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java @@ -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