diff --git a/src/main/java/uk/gov/hmcts/juror/api/moj/exception/MojException.java b/src/main/java/uk/gov/hmcts/juror/api/moj/exception/MojException.java index 8a2b62713..4bd59f282 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/moj/exception/MojException.java +++ b/src/main/java/uk/gov/hmcts/juror/api/moj/exception/MojException.java @@ -94,7 +94,8 @@ public enum ErrorCode { JUROR_HAS_BEEN_DEFERRED_BEFORE, COULD_NOT_FIND_ENOUGH_VOTERS, COULD_NOT_FIND_ENOUGH_ELIGIBLE_VOTERS, - DAY_ALREADY_CONFIRMED + DAY_ALREADY_CONFIRMED, + CANNOT_DEFER_TO_EXISTING_POOL } } diff --git a/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceImpl.java b/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceImpl.java index 711d61ea4..4e6eef3c8 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceImpl.java @@ -176,6 +176,8 @@ public void processJurorDeferral(BureauJwtPayload payload, String jurorNumber, JurorPool jurorPool = jurorPoolService.getJurorPoolFromUser(jurorNumber); JurorPoolUtils.checkOwnershipForCurrentUser(jurorPool, payload.getOwner()); + validateJurorPool(deferralReasonDto, jurorPool); + // if not empty then we need to move the juror to the active pool if (!StringUtils.isEmpty(deferralReasonDto.poolNumber)) { // update old record @@ -224,6 +226,9 @@ public void changeJurorDeferralDate(BureauJwtPayload payload, String jurorNumber DeferralReasonRequestDto deferralReasonDto) { String auditorUsername = payload.getLogin(); JurorPool jurorPool = jurorPoolService.getJurorPoolFromUser(jurorNumber); + + validateJurorPool(deferralReasonDto, jurorPool); + JurorPoolUtils.checkOwnershipForCurrentUser(jurorPool, payload.getOwner()); // if not empty then we need to move the juror to the active pool @@ -647,6 +652,13 @@ public void setDeletedDeferralJurorPool(JurorPool jurorPool, String auditorUsern jurorRepository.save(juror); } + private void validateJurorPool(DeferralReasonRequestDto deferralReasonDto, JurorPool jurorPool) { + if (jurorPool.getPoolNumber().equalsIgnoreCase(deferralReasonDto.getPoolNumber())) { + throw new MojException.BusinessRuleViolation("Cannot change deferral to the existing pool", + MojException.BusinessRuleViolation.ErrorCode.CANNOT_DEFER_TO_EXISTING_POOL); + } + } + private void printDeferralLetter(String owner, JurorPool jurorPool) { // send letter via bulk print for Bureau users only if (JurorDigitalApplication.JUROR_OWNER.equals(owner)) { diff --git a/src/test/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceTest.java b/src/test/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceTest.java index 7bc028d32..6f644aa29 100644 --- a/src/test/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceTest.java +++ b/src/test/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceTest.java @@ -8,6 +8,7 @@ import com.querydsl.core.types.Predicate; import com.querydsl.core.types.dsl.BooleanExpression; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -726,7 +727,7 @@ void useCourtDeferralsDeferralsUsedNoJurorPool() { @Test @SuppressWarnings("PMD.JUnitAssertionsShouldIncludeMessage") - void useCourtDeferrals_deferralsUsed_noPoolRequest() { + void useCourtDeferralsDeferralsUsedNoPoolRequest() { String courtLocation = "415"; LocalDate newAttendanceDate = LocalDate.now(); @@ -774,7 +775,7 @@ void useCourtDeferrals_deferralsUsed_noPoolRequest() { } @Test - void useBureauDeferrals_deferralsUsed_happyPath() { + void useBureauDeferralsDeferralsUsedHappyPath() { LocalDate oldAttendanceDate = LocalDate.of(2022, 6, 6); LocalDate newAttendanceDate = LocalDate.now(); String courtLocation = "415"; @@ -830,7 +831,7 @@ void useBureauDeferrals_deferralsUsed_happyPath() { } @Test - void useBureauDeferrals_deferralsUsed_noJurorPool() { + void useBureauDeferralsDeferralsUsedNoJurorPool() { LocalDate oldAttendanceDate = LocalDate.of(2022, 6, 6); LocalDate newAttendanceDate = LocalDate.now(); String courtLocation = "415"; @@ -876,7 +877,7 @@ void useBureauDeferrals_deferralsUsed_noJurorPool() { } @Test - void useBureauDeferrals_deferralsUsed_noPoolRequest() { + void useBureauDeferralsDeferralsUsedNoPoolRequest() { String courtLocation = "415"; LocalDate newAttendanceDate = LocalDate.now(); @@ -972,7 +973,7 @@ private void verifyLettersHappyPathTest() { } @Test - void processJuror_deferral_digital_happy_path_moveToActivePool() { + void processJurorDeferralDigitalHappyPathMoveToActivePool() { TestUtils.mockBureauUser(); LocalDate newAttendanceDate = LocalDate.now(); LocalDate oldAttendanceDate = LocalDate.of(2022, 6, 6); @@ -1010,8 +1011,48 @@ void processJuror_deferral_digital_happy_path_moveToActivePool() { } + + @Test + void processJurorDeferralDigitalUnhappyMoveToCurrentPool() { + TestUtils.mockBureauUser(); + LocalDate oldAttendanceDate = LocalDate.of(2022, 6, 6); + final BureauJwtPayload bureauPayload = TestUtils.createJwt("400", "BUREAU_USER"); + String jurorNumber = "123456789"; + final PoolRequest oldPoolRequest = createPoolRequest("400", "111111112", "415", + oldAttendanceDate + ); + Juror juror = new Juror(); + juror.setJurorNumber(jurorNumber); + JurorPool jurorPool = createJurorPool(jurorNumber); + jurorPool.setOwner("400"); + jurorPool.setPool(oldPoolRequest); + jurorPool.setJuror(juror); + + JurorStatus jurorStatus = new JurorStatus(); + jurorStatus.setStatus(IJurorStatus.RESPONDED); + + DigitalResponse digitalResponse = new DigitalResponse(); + digitalResponse.setJurorNumber(jurorNumber); + + DeferralReasonRequestDto dto = createDeferralReasonRequestDtoToActivePool(ReplyMethod.DIGITAL); + dto.setReplyMethod(ReplyMethod.DIGITAL); + dto.setPoolNumber(oldPoolRequest.getPoolNumber()); + + when(jurorPoolService.getJurorPoolFromUser(jurorNumber)).thenReturn(jurorPool); + + MojException.BusinessRuleViolation exception = Assertions.assertThrows(MojException.BusinessRuleViolation.class, + () -> manageDeferralsService.processJurorDeferral(bureauPayload, jurorNumber, dto), + "Exception should be thrown"); + + assertThat(exception).isNotNull(); + assertThat(exception.getErrorCode()).isEqualTo(MojException.BusinessRuleViolation + .ErrorCode.CANNOT_DEFER_TO_EXISTING_POOL); + assertThat(exception.getMessage()).isEqualTo( + "Cannot change deferral to the existing pool"); + } + @Test - void changeDeferralDate_happy_path_moveToActivePool() { + void changeDeferralDateHappyPathMoveToActivePool() { TestUtils.mockBureauUser(); LocalDate newAttendanceDate = LocalDate.now(); LocalDate oldAttendanceDate = LocalDate.of(2022, 6, 6); @@ -1044,6 +1085,46 @@ void changeDeferralDate_happy_path_moveToActivePool() { verifyMoveToActivePoolTest(); } + @Test + void changeDeferralDateUnhappyMoveToCurrentPool() { + TestUtils.mockBureauUser(); + LocalDate oldAttendanceDate = LocalDate.of(2022, 6, 6); + final BureauJwtPayload bureauPayload = TestUtils.createJwt("400", "BUREAU_USER"); + String jurorNumber = "123456789"; + final PoolRequest oldPoolRequest = createPoolRequest("400", + "111111111", "415", oldAttendanceDate + ); + Juror juror = new Juror(); + juror.setJurorNumber(jurorNumber); + JurorPool jurorPool = createJurorPool(jurorNumber); + jurorPool.setOwner("400"); + jurorPool.setPool(oldPoolRequest); + jurorPool.setJuror(juror); + + JurorStatus jurorStatus = new JurorStatus(); + jurorStatus.setStatus(IJurorStatus.RESPONDED); + + DigitalResponse digitalResponse = new DigitalResponse(); + digitalResponse.setJurorNumber(jurorNumber); + + DeferralReasonRequestDto dto = createDeferralReasonRequestDtoToActivePool(ReplyMethod.DIGITAL); + dto.setReplyMethod(ReplyMethod.DIGITAL); + dto.setPoolNumber(oldPoolRequest.getPoolNumber()); + + when(jurorPoolService.getJurorPoolFromUser(jurorNumber)).thenReturn(jurorPool); + + MojException.BusinessRuleViolation exception = assertThrows(MojException.BusinessRuleViolation.class, + () -> manageDeferralsService.changeJurorDeferralDate(bureauPayload, jurorNumber, dto), + "Exception should be thrown"); + + assertThat(exception).isNotNull(); + assertThat(exception.getErrorCode()).isEqualTo(MojException.BusinessRuleViolation + .ErrorCode.CANNOT_DEFER_TO_EXISTING_POOL); + assertThat(exception.getMessage()).isEqualTo( + "Cannot change deferral to the existing pool"); + + } + @Test void changeDeferralDate_happy_path_moveToActivePool_RemoveFromDeferralMaintenance() { TestUtils.mockBureauUser(); @@ -1081,7 +1162,7 @@ void changeDeferralDate_happy_path_moveToActivePool_RemoveFromDeferralMaintenanc } @Test - void changeDeferralDate_happy_path_moveToDeferralMaintenance() { + void changeDeferralDateHappyPathMoveToDeferralMaintenance() { TestUtils.mockBureauUser(); final BureauJwtPayload bureauPayload = TestUtils.createJwt("400", "BUREAU_USER"); String jurorNumber = "123456789";