From 654361c71a9e37893be33af55d5cdfbfe57017da Mon Sep 17 00:00:00 2001 From: akikrahman1 Date: Thu, 12 Sep 2024 15:34:38 +0100 Subject: [PATCH 1/3] task/JM-8185 --- .../hmcts/juror/api/moj/exception/MojException.java | 3 ++- .../ManageDeferralsServiceImpl.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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)) { From ccebcc0acceb1f18968201ade17e1b200400d956 Mon Sep 17 00:00:00 2001 From: akikrahman1 Date: Tue, 17 Sep 2024 17:03:36 +0100 Subject: [PATCH 2/3] Added unit tests --- .../ManageDeferralsServiceTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) 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..25fb1681a 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; @@ -60,6 +61,7 @@ import uk.gov.hmcts.juror.api.moj.service.PoolMemberSequenceService; import uk.gov.hmcts.juror.api.moj.service.PrintDataService; import uk.gov.hmcts.juror.api.moj.service.SummonsReplyMergeService; +import uk.gov.hmcts.juror.api.moj.utils.SecurityUtil; import java.time.LocalDate; import java.time.LocalTime; @@ -1010,6 +1012,45 @@ void processJuror_deferral_digital_happy_path_moveToActivePool() { } + + @Test + void processJuror_deferral_digital_unhappy_moveToCurrentPool() { + 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() { TestUtils.mockBureauUser(); @@ -1044,6 +1085,45 @@ void changeDeferralDate_happy_path_moveToActivePool() { verifyMoveToActivePoolTest(); } + @Test + void changeDeferralDate_unhappy_moveToCurrentPool() { + 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 = Assertions.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(); From 20e6f2822e61777d80da576b23ef1bf4938a22c7 Mon Sep 17 00:00:00 2001 From: akikrahman1 Date: Wed, 18 Sep 2024 09:53:10 +0100 Subject: [PATCH 3/3] addressing PMD issues --- .../ManageDeferralsServiceTest.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) 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 25fb1681a..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 @@ -61,7 +61,6 @@ import uk.gov.hmcts.juror.api.moj.service.PoolMemberSequenceService; import uk.gov.hmcts.juror.api.moj.service.PrintDataService; import uk.gov.hmcts.juror.api.moj.service.SummonsReplyMergeService; -import uk.gov.hmcts.juror.api.moj.utils.SecurityUtil; import java.time.LocalDate; import java.time.LocalTime; @@ -728,7 +727,7 @@ void useCourtDeferralsDeferralsUsedNoJurorPool() { @Test @SuppressWarnings("PMD.JUnitAssertionsShouldIncludeMessage") - void useCourtDeferrals_deferralsUsed_noPoolRequest() { + void useCourtDeferralsDeferralsUsedNoPoolRequest() { String courtLocation = "415"; LocalDate newAttendanceDate = LocalDate.now(); @@ -776,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"; @@ -832,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"; @@ -878,7 +877,7 @@ void useBureauDeferrals_deferralsUsed_noJurorPool() { } @Test - void useBureauDeferrals_deferralsUsed_noPoolRequest() { + void useBureauDeferralsDeferralsUsedNoPoolRequest() { String courtLocation = "415"; LocalDate newAttendanceDate = LocalDate.now(); @@ -974,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); @@ -1014,7 +1013,7 @@ void processJuror_deferral_digital_happy_path_moveToActivePool() { @Test - void processJuror_deferral_digital_unhappy_moveToCurrentPool() { + void processJurorDeferralDigitalUnhappyMoveToCurrentPool() { TestUtils.mockBureauUser(); LocalDate oldAttendanceDate = LocalDate.of(2022, 6, 6); final BureauJwtPayload bureauPayload = TestUtils.createJwt("400", "BUREAU_USER"); @@ -1046,13 +1045,14 @@ void processJuror_deferral_digital_unhappy_moveToCurrentPool() { "Exception should be thrown"); assertThat(exception).isNotNull(); - assertThat(exception.getErrorCode()).isEqualTo(MojException.BusinessRuleViolation.ErrorCode.CANNOT_DEFER_TO_EXISTING_POOL); + 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); @@ -1086,7 +1086,7 @@ void changeDeferralDate_happy_path_moveToActivePool() { } @Test - void changeDeferralDate_unhappy_moveToCurrentPool() { + void changeDeferralDateUnhappyMoveToCurrentPool() { TestUtils.mockBureauUser(); LocalDate oldAttendanceDate = LocalDate.of(2022, 6, 6); final BureauJwtPayload bureauPayload = TestUtils.createJwt("400", "BUREAU_USER"); @@ -1113,12 +1113,13 @@ void changeDeferralDate_unhappy_moveToCurrentPool() { when(jurorPoolService.getJurorPoolFromUser(jurorNumber)).thenReturn(jurorPool); - MojException.BusinessRuleViolation exception = Assertions.assertThrows(MojException.BusinessRuleViolation.class, + 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.getErrorCode()).isEqualTo(MojException.BusinessRuleViolation + .ErrorCode.CANNOT_DEFER_TO_EXISTING_POOL); assertThat(exception.getMessage()).isEqualTo( "Cannot change deferral to the existing pool"); @@ -1161,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";