From 66d9101fa601e904dc0062099ffbda5648c976e6 Mon Sep 17 00:00:00 2001 From: chris-bellingham Date: Wed, 11 Dec 2024 09:56:30 +0000 Subject: [PATCH 01/10] DMP-3534: Adjust retention confidence score numerical values --- .../enums/RetentionConfidenceScoreEnum.java | 2 +- ...tedObjectsSingleCaseProcessorImplTest.java | 36 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/uk/gov/hmcts/darts/retention/enums/RetentionConfidenceScoreEnum.java b/src/main/java/uk/gov/hmcts/darts/retention/enums/RetentionConfidenceScoreEnum.java index 6d8b163c78..a4638d7d4c 100644 --- a/src/main/java/uk/gov/hmcts/darts/retention/enums/RetentionConfidenceScoreEnum.java +++ b/src/main/java/uk/gov/hmcts/darts/retention/enums/RetentionConfidenceScoreEnum.java @@ -7,7 +7,7 @@ @RequiredArgsConstructor public enum RetentionConfidenceScoreEnum { - CASE_PERFECTLY_CLOSED(1), CASE_NOT_PERFECTLY_CLOSED(2); + CASE_PERFECTLY_CLOSED(0), CASE_NOT_PERFECTLY_CLOSED(1); private final Integer id; } \ No newline at end of file diff --git a/src/test/java/uk/gov/hmcts/darts/retention/service/impl/ApplyRetentionCaseAssociatedObjectsSingleCaseProcessorImplTest.java b/src/test/java/uk/gov/hmcts/darts/retention/service/impl/ApplyRetentionCaseAssociatedObjectsSingleCaseProcessorImplTest.java index 734127428a..726a81c0c0 100644 --- a/src/test/java/uk/gov/hmcts/darts/retention/service/impl/ApplyRetentionCaseAssociatedObjectsSingleCaseProcessorImplTest.java +++ b/src/test/java/uk/gov/hmcts/darts/retention/service/impl/ApplyRetentionCaseAssociatedObjectsSingleCaseProcessorImplTest.java @@ -241,13 +241,13 @@ void processApplyRetentionToCaseAssociatedObjectsForMediaWhereMultipleCasesIsPer caseObjectsProcessor.processApplyRetentionToCaseAssociatedObjects(case1PerfectlyClosed.getId()); // then - assertEquals(1, mediaA1.getRetConfScore()); + assertEquals(0, mediaA1.getRetConfScore()); assertNull(mediaA1.getRetConfReason()); - assertEquals(1, mediaA2.getRetConfScore()); + assertEquals(0, mediaA2.getRetConfScore()); assertNull(mediaA2.getRetConfReason()); - assertEquals(1, mediaB1.getRetConfScore()); + assertEquals(0, mediaB1.getRetConfScore()); assertNull(mediaB1.getRetConfReason()); } @@ -292,13 +292,13 @@ void processApplyRetentionToCaseAssociatedObjectsForMediaWhereMultipleCasesIsPer caseObjectsProcessor.processApplyRetentionToCaseAssociatedObjects(case1PerfectlyClosed.getId()); // then - assertEquals(1, mediaA1.getRetConfScore()); + assertEquals(0, mediaA1.getRetConfScore()); assertNull(mediaA1.getRetConfReason()); - assertEquals(1, mediaA2.getRetConfScore()); + assertEquals(0, mediaA2.getRetConfScore()); assertNull(mediaA2.getRetConfReason()); - assertEquals(1, mediaB1.getRetConfScore()); + assertEquals(0, mediaB1.getRetConfScore()); assertNull(mediaB1.getRetConfReason()); } @@ -334,14 +334,14 @@ void processApplyRetentionToCaseAssociatedObjectsForMediaWhereMultipleCasesIsNot caseObjectsProcessor.processApplyRetentionToCaseAssociatedObjects(case3NotPerfectlyClosed.getId()); // then - assertEquals(2, mediaA1.getRetConfScore()); + assertEquals(1, mediaA1.getRetConfScore()); String expectedResult = "{\\\"ret_conf_applied_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"cases\\\":[{\\\"courthouse\\\":\\\"CASE_COURTHOUSE\\\",\\\"case_number\\\":\\\"case3\\\",\\\"ret_conf_updated_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"ret_conf_reason\\\":\\\"AGED_CASE\\\"}]}"; assertEquals(expectedResult, mediaA1.getRetConfReason()); - assertEquals(2, mediaA2.getRetConfScore()); + assertEquals(1, mediaA2.getRetConfScore()); assertEquals(expectedResult, mediaA2.getRetConfReason()); - assertEquals(2, mediaB1.getRetConfScore()); + assertEquals(1, mediaB1.getRetConfScore()); String expectedResult2 = "{\\\"ret_conf_applied_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"cases\\\":[{\\\"courthouse\\\":\\\"CASE_COURTHOUSE\\\",\\\"case_number\\\":\\\"case3\\\",\\\"ret_conf_updated_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"ret_conf_reason\\\":\\\"AGED_CASE\\\"},{\\\"courthouse\\\":\\\"CASE_COURTHOUSE\\\",\\\"case_number\\\":\\\"case4\\\",\\\"ret_conf_updated_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"ret_conf_reason\\\":\\\"AGED_CASE\\\"}]}"; assertEquals(expectedResult2, mediaB1.getRetConfReason()); @@ -378,11 +378,11 @@ void processApplyRetentionToCaseAssociatedObjectsForMediaWhereOneCaseIsPerfectly caseObjectsProcessor.processApplyRetentionToCaseAssociatedObjects(case1PerfectlyClosed.getId()); // then - assertEquals(1, mediaA1.getRetConfScore()); + assertEquals(0, mediaA1.getRetConfScore()); - assertEquals(1, mediaA2.getRetConfScore()); + assertEquals(0, mediaA2.getRetConfScore()); - assertEquals(2, mediaB1.getRetConfScore()); + assertEquals(1, mediaB1.getRetConfScore()); String expectedResult = "{\\\"ret_conf_applied_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"cases\\\":[{\\\"courthouse\\\":\\\"CASE_COURTHOUSE\\\",\\\"case_number\\\":\\\"case4\\\",\\\"ret_conf_updated_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"ret_conf_reason\\\":\\\"AGED_CASE\\\"}]}"; assertEquals(expectedResult, mediaB1.getRetConfReason()); @@ -423,9 +423,9 @@ void processApplyRetentionToCaseAssociatedObjectsForAnnotationDocumentWhereOneCa caseObjectsProcessor.processApplyRetentionToCaseAssociatedObjects(case1PerfectlyClosed.getId()); // then - assertEquals(1, annotationDocumentA1.getRetConfScore()); + assertEquals(0, annotationDocumentA1.getRetConfScore()); - assertEquals(2, annotationDocumentB1.getRetConfScore()); + assertEquals(1, annotationDocumentB1.getRetConfScore()); String expectedResult = "{\\\"ret_conf_applied_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"cases\\\":[{\\\"courthouse\\\":\\\"CASE_COURTHOUSE\\\",\\\"case_number\\\":\\\"case4\\\",\\\"ret_conf_updated_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"ret_conf_reason\\\":\\\"AGED_CASE\\\"}]}"; assertEquals(expectedResult, annotationDocumentB1.getRetConfReason()); @@ -470,9 +470,9 @@ void processApplyRetentionToCaseAssociatedObjectsForTranscriptionDocumentWhereOn caseObjectsProcessor.processApplyRetentionToCaseAssociatedObjects(case1PerfectlyClosed.getId()); // then - assertEquals(1, transcriptionDocumentA1.getRetConfScore()); + assertEquals(0, transcriptionDocumentA1.getRetConfScore()); - assertEquals(2, transcriptionDocumentB1.getRetConfScore()); + assertEquals(1, transcriptionDocumentB1.getRetConfScore()); String expectedResult = "{\\\"ret_conf_applied_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"cases\\\":[{\\\"courthouse\\\":\\\"CASE_COURTHOUSE\\\",\\\"case_number\\\":\\\"case4\\\",\\\"ret_conf_updated_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"ret_conf_reason\\\":\\\"AGED_CASE\\\"}]}"; assertEquals(expectedResult, transcriptionDocumentB1.getRetConfReason()); @@ -497,7 +497,7 @@ void processApplyRetentionToCaseAssociatedObjectsForCaseDocumentWhereCaseIsPerfe caseObjectsProcessor.processApplyRetentionToCaseAssociatedObjects(case1PerfectlyClosed.getId()); // then - assertEquals(1, caseDocument.getRetConfScore()); + assertEquals(0, caseDocument.getRetConfScore()); assertNull(caseDocument.getRetConfReason()); } @@ -524,7 +524,7 @@ void processApplyRetentionToCaseAssociatedObjectsForCaseDocumentWhereCaseIsNotPe caseObjectsProcessor.processApplyRetentionToCaseAssociatedObjects(case4NotPerfectlyClosed.getId()); // then - assertEquals(2, caseDocument.getRetConfScore()); + assertEquals(1, caseDocument.getRetConfScore()); String expectedResult = "{\\\"ret_conf_applied_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"cases\\\":[{\\\"courthouse\\\":\\\"CASE_COURTHOUSE\\\",\\\"case_number\\\":\\\"case4\\\",\\\"ret_conf_updated_ts\\\":\\\"2024-06-20T10:00:00Z\\\",\\\"ret_conf_reason\\\":\\\"AGED_CASE\\\"}]}"; assertEquals(expectedResult, caseDocument.getRetConfReason()); From 80820195eede086f5bded0f79a870af06e5e3ff7 Mon Sep 17 00:00:00 2001 From: chris-bellingham Date: Fri, 13 Dec 2024 15:58:56 +0000 Subject: [PATCH 02/10] DMP-3534: Refactor production code to centralise case retention update logic per lookup table --- .../impl/CloseOldCasesProcessorImpl.java | 16 ++++---- ...tentionConfidenceCategoryMapperEntity.java | 9 +++-- ...ionConfidenceCategoryMapperRepository.java | 6 +++ .../service/handler/StopAndCloseHandler.java | 13 ++++--- .../darts/retention/api/RetentionApi.java | 9 ++++- .../retention/api/impl/RetentionApiImpl.java | 17 ++++++-- .../service/RetentionPostService.java | 9 ++++- .../retention/service/RetentionService.java | 3 ++ .../impl/RetentionPostServiceImpl.java | 39 ++++++++----------- .../service/impl/RetentionServiceImpl.java | 25 ++++++++++++ 10 files changed, 99 insertions(+), 47 deletions(-) diff --git a/src/main/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImpl.java b/src/main/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImpl.java index e6778f0fa4..175a3999d2 100644 --- a/src/main/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImpl.java +++ b/src/main/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImpl.java @@ -25,6 +25,7 @@ import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.enums.RetentionPolicyEnum; import uk.gov.hmcts.darts.retention.helper.RetentionDateHelper; +import uk.gov.hmcts.darts.retention.service.RetentionService; import uk.gov.hmcts.darts.retentions.model.PostRetentionRequest; import java.time.LocalDate; @@ -36,8 +37,6 @@ import java.util.Optional; import static java.lang.Boolean.TRUE; -import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum.AGED_CASE; -import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum.CASE_NOT_PERFECTLY_CLOSED; @Service @RequiredArgsConstructor @@ -47,6 +46,7 @@ public class CloseOldCasesProcessorImpl implements CloseOldCasesProcessor { private final CloseOldCasesProcessorImpl.CloseCaseProcessor caseProcessor; private final CaseRepository caseRepository; + private final RetentionService retentionService; // TODO Call via an API layer private final AuthorisationApi authorisationApi; @@ -76,7 +76,7 @@ public void closeCases(int batchSize) { @Service @RequiredArgsConstructor(onConstructor = @__(@Autowired)) - public static class CloseCaseProcessor { + public class CloseCaseProcessor { private static final String CLOSE_CASE_RETENTION_COMMENT = "CloseOldCases Automated job setting retention period to Default"; private final CaseService caseService; private final CaseRetentionRepository caseRetentionRepository; @@ -135,9 +135,8 @@ private void closeCaseInDbAndAddRetention(CourtCaseEntity courtCase, OffsetDateT UserAccountEntity userAccount) { courtCase.setClosed(TRUE); courtCase.setCaseClosedTimestamp(caseClosedDate); - courtCase.setRetConfReason(AGED_CASE); - courtCase.setRetConfScore(CASE_NOT_PERFECTLY_CLOSED); - courtCase.setRetConfUpdatedTs(currentTimeHelper.currentOffsetDateTime()); + final RetentionConfidenceCategoryEnum retentionConfidenceCategory = RetentionConfidenceCategoryEnum.AGED_CASE; + courtCase = retentionService.updateCourtCaseConfidenceAttributesForRetention(courtCase, retentionConfidenceCategory); caseService.saveCase(courtCase); log.info("Closed court case id {}", courtCase.getId()); @@ -148,10 +147,9 @@ private void closeCaseInDbAndAddRetention(CourtCaseEntity courtCase, OffsetDateT postRetentionRequest.setRetentionDate(retentionDate); CaseRetentionEntity retentionEntity = retentionApi.createRetention(postRetentionRequest, courtCase, retentionDate, userAccount, - CaseRetentionStatus.PENDING); - retentionEntity.setConfidenceCategory(RetentionConfidenceCategoryEnum.AGED_CASE); + CaseRetentionStatus.PENDING, retentionConfidenceCategory); caseRetentionRepository.save(retentionEntity); - log.info("Set retention date {} and confidence category {} for case id {}", retentionDate, RetentionConfidenceCategoryEnum.AGED_CASE, + log.info("Set retention date {} and confidence category {} for case id {}", retentionDate, retentionConfidenceCategory, courtCase.getId()); } } diff --git a/src/main/java/uk/gov/hmcts/darts/common/entity/RetentionConfidenceCategoryMapperEntity.java b/src/main/java/uk/gov/hmcts/darts/common/entity/RetentionConfidenceCategoryMapperEntity.java index 1793bac0a5..4697a8faa9 100644 --- a/src/main/java/uk/gov/hmcts/darts/common/entity/RetentionConfidenceCategoryMapperEntity.java +++ b/src/main/java/uk/gov/hmcts/darts/common/entity/RetentionConfidenceCategoryMapperEntity.java @@ -11,6 +11,9 @@ import lombok.Getter; import lombok.Setter; import uk.gov.hmcts.darts.common.entity.base.MandatoryCreatedModifiedBaseEntity; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; @Entity @Table(name = "retention_confidence_category_mapper") @@ -26,13 +29,13 @@ public class RetentionConfidenceCategoryMapperEntity extends MandatoryCreatedMod private Integer id; @Column(name = "ret_conf_score") - private Integer confidenceScore; + private RetentionConfidenceScoreEnum confidenceScore; @Column(name = "ret_conf_reason") - private String confidenceReason; + private RetentionConfidenceReasonEnum confidenceReason; @Column(name = "confidence_category") - private Integer confidenceCategory; + private RetentionConfidenceCategoryEnum confidenceCategory; @Column(name = "description") private String description; diff --git a/src/main/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepository.java b/src/main/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepository.java index 4f2fd031f3..7af48909d6 100644 --- a/src/main/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepository.java +++ b/src/main/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepository.java @@ -3,7 +3,13 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import uk.gov.hmcts.darts.common.entity.RetentionConfidenceCategoryMapperEntity; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; + +import java.util.Optional; @Repository public interface RetentionConfidenceCategoryMapperRepository extends JpaRepository { + + Optional findByConfidenceCategory(RetentionConfidenceCategoryEnum confidenceCategory); + } diff --git a/src/main/java/uk/gov/hmcts/darts/event/service/handler/StopAndCloseHandler.java b/src/main/java/uk/gov/hmcts/darts/event/service/handler/StopAndCloseHandler.java index dbbaa2bbf5..c36456e6ae 100644 --- a/src/main/java/uk/gov/hmcts/darts/event/service/handler/StopAndCloseHandler.java +++ b/src/main/java/uk/gov/hmcts/darts/event/service/handler/StopAndCloseHandler.java @@ -34,6 +34,7 @@ import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; import uk.gov.hmcts.darts.retention.enums.RetentionPolicyEnum; +import uk.gov.hmcts.darts.retention.service.RetentionService; import uk.gov.hmcts.darts.util.DataUtil; import java.time.LocalDate; @@ -54,6 +55,7 @@ public class StopAndCloseHandler extends EventHandlerBase { private final CaseRetentionRepository caseRetentionRepository; private final RetentionApi retentionApi; private final CaseManagementRetentionService caseManagementRetentionService; + private final RetentionService retentionService; // TODO Call via an API layer private final AuthorisationApi authorisationApi; private final CurrentTimeHelper currentTimeHelper; @@ -73,7 +75,7 @@ public StopAndCloseHandler(RetrieveCoreObjectService retrieveCoreObjectService, AuthorisationApi authorisationApi, LogApi logApi, CaseManagementRetentionService caseManagementRetentionService, - EventPersistenceService eventPersistenceService, + EventPersistenceService eventPersistenceService, RetentionService retentionService, CurrentTimeHelper currentTimeHelper) { super(retrieveCoreObjectService, eventRepository, hearingRepository, caseRepository, eventPublisher, logApi, eventPersistenceService); this.darNotifyService = darNotifyService; @@ -81,6 +83,7 @@ public StopAndCloseHandler(RetrieveCoreObjectService retrieveCoreObjectService, this.caseManagementRetentionService = caseManagementRetentionService; this.retentionApi = retentionApi; this.authorisationApi = authorisationApi; + this.retentionService = retentionService; this.currentTimeHelper = currentTimeHelper; } @@ -149,9 +152,7 @@ private void closeCase(DartsEvent dartsEvent, CourtCaseEntity courtCase) { courtCase.setClosed(TRUE); courtCase.setCaseClosedTimestamp(dartsEvent.getDateTime()); courtCase.setLastModifiedBy(authorisationApi.getCurrentUser()); - courtCase.setRetConfScore(CASE_PERFECTLY_CLOSED); - courtCase.setRetConfReason(RetentionConfidenceReasonEnum.CASE_CLOSED); - courtCase.setRetConfUpdatedTs(currentTimeHelper.currentOffsetDateTime()); + courtCase = retentionService.updateCourtCaseConfidenceAttributesForRetention(courtCase, RetentionConfidenceCategoryEnum.CASE_CLOSED); caseRepository.saveAndFlush(courtCase); } } @@ -178,7 +179,9 @@ private void updateExistingRetention(CaseManagementRetentionEntity caseManagemen caseRetentionRepository.save(existingCaseRetention); } - private void createRetention(CaseManagementRetentionEntity caseManagementRetentionEntity, CreatedHearingAndEvent hearingAndEvent, DartsEvent dartsEvent) { + private void createRetention(CaseManagementRetentionEntity caseManagementRetentionEntity, + CreatedHearingAndEvent hearingAndEvent, + DartsEvent dartsEvent) { DartsEventRetentionPolicy dartsEventRetentionPolicy = dartsEvent.getRetentionPolicy(); CourtCaseEntity courtCase = hearingAndEvent.getHearingEntity().getCourtCase(); diff --git a/src/main/java/uk/gov/hmcts/darts/retention/api/RetentionApi.java b/src/main/java/uk/gov/hmcts/darts/retention/api/RetentionApi.java index e65e3e5044..8b937c58a6 100644 --- a/src/main/java/uk/gov/hmcts/darts/retention/api/RetentionApi.java +++ b/src/main/java/uk/gov/hmcts/darts/retention/api/RetentionApi.java @@ -5,6 +5,7 @@ import uk.gov.hmcts.darts.common.entity.RetentionPolicyTypeEntity; import uk.gov.hmcts.darts.common.entity.UserAccountEntity; import uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retentions.model.PostRetentionRequest; import java.time.LocalDate; @@ -12,6 +13,10 @@ public interface RetentionApi { LocalDate applyPolicyStringToDate(LocalDate dateToAppend, String policyString, RetentionPolicyTypeEntity retentionPolicyType); - CaseRetentionEntity createRetention(PostRetentionRequest postRetentionRequest, CourtCaseEntity courtCase, LocalDate newRetentionDate, - UserAccountEntity userAccount, CaseRetentionStatus caseRetentionStatus); + CaseRetentionEntity createRetention(PostRetentionRequest postRetentionRequest, + CourtCaseEntity courtCase, + LocalDate newRetentionDate, + UserAccountEntity userAccount, + CaseRetentionStatus caseRetentionStatus, + RetentionConfidenceCategoryEnum retentionConfidenceCategory); } diff --git a/src/main/java/uk/gov/hmcts/darts/retention/api/impl/RetentionApiImpl.java b/src/main/java/uk/gov/hmcts/darts/retention/api/impl/RetentionApiImpl.java index 11001bad19..85569966d9 100644 --- a/src/main/java/uk/gov/hmcts/darts/retention/api/impl/RetentionApiImpl.java +++ b/src/main/java/uk/gov/hmcts/darts/retention/api/impl/RetentionApiImpl.java @@ -8,6 +8,7 @@ import uk.gov.hmcts.darts.common.entity.UserAccountEntity; import uk.gov.hmcts.darts.retention.api.RetentionApi; import uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.helper.RetentionDateHelper; import uk.gov.hmcts.darts.retention.service.RetentionPostService; import uk.gov.hmcts.darts.retentions.model.PostRetentionRequest; @@ -27,9 +28,17 @@ public LocalDate applyPolicyStringToDate(LocalDate dateToAppend, String policySt } @Override - public CaseRetentionEntity createRetention(PostRetentionRequest postRetentionRequest, CourtCaseEntity courtCase, LocalDate newRetentionDate, - UserAccountEntity userAccount, CaseRetentionStatus caseRetentionStatus) { - return retentionPostService.createNewCaseRetention(postRetentionRequest, courtCase, - newRetentionDate, userAccount, caseRetentionStatus); + public CaseRetentionEntity createRetention(PostRetentionRequest postRetentionRequest, + CourtCaseEntity courtCase, + LocalDate newRetentionDate, + UserAccountEntity userAccount, + CaseRetentionStatus caseRetentionStatus, + RetentionConfidenceCategoryEnum retentionConfidenceCategory) { + return retentionPostService.createNewCaseRetention(postRetentionRequest, + courtCase, + newRetentionDate, + userAccount, + caseRetentionStatus, + retentionConfidenceCategory); } } diff --git a/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionPostService.java b/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionPostService.java index af3c2c3875..2878577b8d 100644 --- a/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionPostService.java +++ b/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionPostService.java @@ -4,6 +4,7 @@ import uk.gov.hmcts.darts.common.entity.CourtCaseEntity; import uk.gov.hmcts.darts.common.entity.UserAccountEntity; import uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retentions.model.PostRetentionRequest; import uk.gov.hmcts.darts.retentions.model.PostRetentionResponse; @@ -13,6 +14,10 @@ public interface RetentionPostService { PostRetentionResponse postRetention(Boolean validateOnly, PostRetentionRequest postRetentionRequest); - CaseRetentionEntity createNewCaseRetention(PostRetentionRequest postRetentionRequest, CourtCaseEntity courtCase, - LocalDate newRetentionDate, UserAccountEntity userAccount, CaseRetentionStatus caseRetentionStatus); + CaseRetentionEntity createNewCaseRetention(PostRetentionRequest postRetentionRequest, + CourtCaseEntity courtCase, + LocalDate newRetentionDate, + UserAccountEntity userAccount, + CaseRetentionStatus caseRetentionStatus, + RetentionConfidenceCategoryEnum retentionConfidenceCategory); } diff --git a/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionService.java b/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionService.java index 52df3426c2..dd03a104b6 100644 --- a/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionService.java +++ b/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionService.java @@ -1,5 +1,7 @@ package uk.gov.hmcts.darts.retention.service; +import uk.gov.hmcts.darts.common.entity.CourtCaseEntity; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retentions.model.GetCaseRetentionsResponse; import java.util.List; @@ -8,4 +10,5 @@ public interface RetentionService { List getCaseRetentions(Integer caseId); + CourtCaseEntity updateCourtCaseConfidenceAttributesForRetention(CourtCaseEntity courtCase, RetentionConfidenceCategoryEnum confidenceCategory); } diff --git a/src/main/java/uk/gov/hmcts/darts/retention/service/impl/RetentionPostServiceImpl.java b/src/main/java/uk/gov/hmcts/darts/retention/service/impl/RetentionPostServiceImpl.java index 81f0aef91f..e988d82180 100644 --- a/src/main/java/uk/gov/hmcts/darts/retention/service/impl/RetentionPostServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/darts/retention/service/impl/RetentionPostServiceImpl.java @@ -23,6 +23,7 @@ import uk.gov.hmcts.darts.retention.exception.RetentionApiError; import uk.gov.hmcts.darts.retention.helper.RetentionDateHelper; import uk.gov.hmcts.darts.retention.service.RetentionPostService; +import uk.gov.hmcts.darts.retention.service.RetentionService; import uk.gov.hmcts.darts.retentions.model.PostRetentionRequest; import uk.gov.hmcts.darts.retentions.model.PostRetentionResponse; @@ -38,8 +39,6 @@ import static uk.gov.hmcts.darts.common.enums.SecurityRoleEnum.JUDICIARY; import static uk.gov.hmcts.darts.common.enums.SecurityRoleEnum.SUPER_ADMIN; import static uk.gov.hmcts.darts.common.enums.SecurityRoleEnum.SUPER_USER; -import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum.MANUAL_OVERRIDE; -import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED; @Service @RequiredArgsConstructor @@ -50,11 +49,12 @@ public class RetentionPostServiceImpl implements RetentionPostService { private final CaseRepository caseRepository; private final CaseRetentionRepository caseRetentionRepository; - private final AuthorisationApi authorisationApi; private final RetentionPolicyTypeRepository retentionPolicyTypeRepository; + private final AuthorisationApi authorisationApi; private final CurrentTimeHelper currentTimeHelper; private final AuditApi auditApi; private final RetentionDateHelper retentionDateHelper; + private final RetentionService retentionService; @Override public PostRetentionResponse postRetention(Boolean validateOnly, PostRetentionRequest postRetentionRequest) { @@ -78,8 +78,13 @@ public PostRetentionResponse postRetention(Boolean validateOnly, PostRetentionRe } if (BooleanUtils.isNotTrue(validateOnly)) { - createNewCaseRetention(postRetentionRequest, courtCase, newRetentionDate); - updateCourtCaseConfidenceAttributesForRetention(courtCase); + CaseRetentionEntity caseRetention = createNewCaseRetention(postRetentionRequest, + courtCase, + newRetentionDate, + authorisationApi.getCurrentUser(), + CaseRetentionStatus.COMPLETE, + RetentionConfidenceCategoryEnum.MANUAL_OVERRIDE); + retentionService.updateCourtCaseConfidenceAttributesForRetention(courtCase, caseRetention.getConfidenceCategory()); } PostRetentionResponse response = new PostRetentionResponse(); @@ -176,8 +181,12 @@ private CaseRetentionEntity getLatestCompletedCaseRetention(CourtCaseEntity cour } @Override - public CaseRetentionEntity createNewCaseRetention(PostRetentionRequest postRetentionRequest, CourtCaseEntity courtCase, - LocalDate newRetentionDate, UserAccountEntity userAccount, CaseRetentionStatus caseRetentionStatus) { + public CaseRetentionEntity createNewCaseRetention(PostRetentionRequest postRetentionRequest, + CourtCaseEntity courtCase, + LocalDate newRetentionDate, + UserAccountEntity userAccount, + CaseRetentionStatus caseRetentionStatus, + RetentionConfidenceCategoryEnum retentionConfidenceCategory) { CaseRetentionEntity caseRetention = new CaseRetentionEntity(); caseRetention.setCourtCase(courtCase); caseRetention.setLastModifiedBy(userAccount); @@ -188,7 +197,7 @@ public CaseRetentionEntity createNewCaseRetention(PostRetentionRequest postReten caseRetention.setCurrentState(String.valueOf(caseRetentionStatus)); caseRetention.setRetainUntilAppliedOn(currentTimeHelper.currentOffsetDateTime()); caseRetention.setRetentionPolicyType(getRetentionPolicy(postRetentionRequest.getIsPermanentRetention())); - caseRetention.setConfidenceCategory(RetentionConfidenceCategoryEnum.MANUAL_OVERRIDE); + caseRetention.setConfidenceCategory(retentionConfidenceCategory); caseRetentionRepository.saveAndFlush(caseRetention); auditApi.record( @@ -199,13 +208,6 @@ public CaseRetentionEntity createNewCaseRetention(PostRetentionRequest postReten return caseRetention; } - private CaseRetentionEntity createNewCaseRetention(PostRetentionRequest postRetentionRequest, CourtCaseEntity courtCase, - LocalDate newRetentionDate) { - UserAccountEntity currentUser = authorisationApi.getCurrentUser(); - return createNewCaseRetention(postRetentionRequest, courtCase, - newRetentionDate, currentUser, CaseRetentionStatus.COMPLETE); - } - private RetentionPolicyTypeEntity getRetentionPolicy(Boolean isPermanent) { String policyKey; @@ -231,11 +233,4 @@ private RetentionPolicyTypeEntity getRetentionPolicy(Boolean isPermanent) { return retentionPolicyList.get(0); } - private void updateCourtCaseConfidenceAttributesForRetention(CourtCaseEntity courtCase) { - courtCase.setRetConfScore(CASE_PERFECTLY_CLOSED); - courtCase.setRetConfReason(MANUAL_OVERRIDE); - courtCase.setRetConfUpdatedTs(currentTimeHelper.currentOffsetDateTime()); - caseRepository.save(courtCase); - } - } \ No newline at end of file diff --git a/src/main/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImpl.java b/src/main/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImpl.java index 7ec484fa3b..425dcbc876 100644 --- a/src/main/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImpl.java @@ -4,11 +4,17 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import uk.gov.hmcts.darts.common.entity.CaseRetentionEntity; +import uk.gov.hmcts.darts.common.entity.CourtCaseEntity; +import uk.gov.hmcts.darts.common.repository.CaseRepository; import uk.gov.hmcts.darts.common.repository.CaseRetentionRepository; +import uk.gov.hmcts.darts.common.repository.RetentionConfidenceCategoryMapperRepository; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.mapper.RetentionMapper; import uk.gov.hmcts.darts.retention.service.RetentionService; import uk.gov.hmcts.darts.retentions.model.GetCaseRetentionsResponse; +import java.time.Clock; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.List; @@ -18,7 +24,10 @@ public class RetentionServiceImpl implements RetentionService { private final CaseRetentionRepository caseRetentionRepository; + private final RetentionConfidenceCategoryMapperRepository retentionConfidenceCategoryMapperRepository; + private final CaseRepository caseRepository; private final RetentionMapper retentionMapper; + private final Clock clock; @Override public List getCaseRetentions(Integer caseId) { @@ -32,4 +41,20 @@ public List getCaseRetentions(Integer caseId) { return caseRetentions; } + @Override + public CourtCaseEntity updateCourtCaseConfidenceAttributesForRetention(CourtCaseEntity courtCase, + RetentionConfidenceCategoryEnum confidenceCategory) { + retentionConfidenceCategoryMapperRepository.findByConfidenceCategory(confidenceCategory) + .ifPresentOrElse(categoryMapperEntity -> { + courtCase.setRetConfScore(categoryMapperEntity.getConfidenceScore()); + courtCase.setRetConfReason(categoryMapperEntity.getConfidenceReason()); + }, () -> { + courtCase.setRetConfScore(null); + courtCase.setRetConfReason(null); + }); + + courtCase.setRetConfUpdatedTs(OffsetDateTime.now(clock)); + return caseRepository.save(courtCase); + } + } From 6110641c98221f1439767ac38503cb71b99f85b3 Mon Sep 17 00:00:00 2001 From: chris-bellingham Date: Wed, 18 Dec 2024 12:12:21 +0000 Subject: [PATCH 03/10] DMP-3534: Integration tests --- .../service/CloseOldCasesProcessorTest.java | 93 +++++++++- .../service/impl/StopAndCloseHandlerTest.java | 29 ++++ ...tentionControllerPostRetentionIntTest.java | 92 ++++++++++ .../testutils/stubs/DartsDatabaseStub.java | 3 + .../testutils/stubs/DartsPersistence.java | 19 ++ .../impl/CloseOldCasesProcessorImpl.java | 8 +- .../service/handler/StopAndCloseHandler.java | 15 +- .../darts/retention/api/RetentionApi.java | 2 + .../retention/api/impl/RetentionApiImpl.java | 8 + .../retention/service/RetentionService.java | 1 + .../impl/CloseOldCasesProcessorImplTest.java | 18 +- .../impl/RetentionPostServiceImplTest.java | 28 +-- .../impl/RetentionServiceImplTest.java | 162 ++++++++++++++++++ .../test/common/data/PersistableFactory.java | 5 + ...ntionConfidenceCategoryMapperTestData.java | 42 +++++ ...tentionConfidenceCategoryMapperEntity.java | 67 ++++++++ 16 files changed, 533 insertions(+), 59 deletions(-) create mode 100644 src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImplTest.java create mode 100644 src/testCommon/java/uk/gov/hmcts/darts/test/common/data/RetentionConfidenceCategoryMapperTestData.java create mode 100644 src/testCommon/java/uk/gov/hmcts/darts/test/common/data/builder/TestRetentionConfidenceCategoryMapperEntity.java diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/cases/service/CloseOldCasesProcessorTest.java b/src/integrationTest/java/uk/gov/hmcts/darts/cases/service/CloseOldCasesProcessorTest.java index d8bd3fefea..43db6b6d06 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/cases/service/CloseOldCasesProcessorTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/cases/service/CloseOldCasesProcessorTest.java @@ -4,7 +4,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; @@ -13,14 +15,18 @@ import uk.gov.hmcts.darts.common.entity.EventEntity; import uk.gov.hmcts.darts.common.entity.HearingEntity; import uk.gov.hmcts.darts.common.entity.MediaEntity; -import uk.gov.hmcts.darts.common.helper.CurrentTimeHelper; import uk.gov.hmcts.darts.common.util.DateConverterUtil; import uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus; import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; +import uk.gov.hmcts.darts.test.common.data.PersistableFactory; +import uk.gov.hmcts.darts.test.common.data.RetentionConfidenceCategoryMapperTestData; +import uk.gov.hmcts.darts.test.common.data.builder.TestRetentionConfidenceCategoryMapperEntity; import uk.gov.hmcts.darts.testutils.IntegrationBase; +import uk.gov.hmcts.darts.testutils.stubs.DartsPersistence; +import java.time.Clock; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; @@ -32,9 +38,9 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.lenient; @Slf4j +@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true") // To override Clock bean class CloseOldCasesProcessorTest extends IntegrationBase { @Autowired CloseOldCasesProcessor closeOldCasesProcessor; @@ -44,8 +50,16 @@ class CloseOldCasesProcessorTest extends IntegrationBase { private static final String REQUESTER_EMAIL = "test.user@example.com"; private static final OffsetDateTime CURRENT_DATE_TIME = OffsetDateTime.of(2024, 10, 1, 10, 0, 0, 0, ZoneOffset.UTC); - @MockBean - private CurrentTimeHelper currentTimeHelper; + @Autowired + private DartsPersistence dartsPersistence; + + @TestConfiguration + public static class ClockConfig { + @Bean + public Clock clock() { + return Clock.fixed(CURRENT_DATE_TIME.toInstant(), ZoneOffset.UTC); + } + } @BeforeEach void beforeEach() { @@ -56,12 +70,23 @@ void beforeEach() { .build(); SecurityContextHolder.getContext().setAuthentication(new JwtAuthenticationToken(jwt)); dartsDatabase.createTestUserAccount(); + } + + private void createAndSaveRetentionConfidenceCategoryMappings() { + RetentionConfidenceCategoryMapperTestData testData = PersistableFactory.getRetentionConfidenceCategoryMapperTestData(); - lenient().when(currentTimeHelper.currentOffsetDateTime()).thenReturn(CURRENT_DATE_TIME); + TestRetentionConfidenceCategoryMapperEntity agedCaseMappingEntity = testData.someMinimalBuilder() + .confidenceCategory(RetentionConfidenceCategoryEnum.AGED_CASE) + .confidenceReason(RetentionConfidenceReasonEnum.AGED_CASE) + .confidenceScore(RetentionConfidenceScoreEnum.CASE_NOT_PERFECTLY_CLOSED) + .build(); + dartsPersistence.save(agedCaseMappingEntity.getEntity()); } @Test void givenClosedEventsUseDateAsClosedDate() { + createAndSaveRetentionConfidenceCategoryMappings(); + HearingEntity hearing = dartsDatabase.createHearing("a_courthouse", "1", "1078", LocalDateTime.now().minusYears(7).plusMonths(3)); OffsetDateTime closeDate = OffsetDateTime.now().minusYears(7); @@ -95,8 +120,50 @@ void givenClosedEventsUseDateAsClosedDate() { assertEquals(RetentionConfidenceCategoryEnum.AGED_CASE, caseRetentionEntity.getConfidenceCategory()); } + @Test + void closeCases_shouldCloseCases_andUseDateAsClosedDate_andSetNullConfidenceReasonAndScore_whenNoConfidenceMappingExists() { + // Given createAndSaveRetentionConfidenceCategoryMappings() is not invoked, so no confidence mappings exist in the DB + + // And + HearingEntity hearing = dartsDatabase.createHearing("a_courthouse", "1", "1078", LocalDateTime.now().minusYears(7).plusMonths(3)); + + OffsetDateTime closeDate = OffsetDateTime.now().minusYears(7); + + EventEntity eventEntity1 = dartsDatabase.getEventStub().createEvent(hearing, 8);//Re-examination + eventEntity1.setCreatedDateTime(OffsetDateTime.now().minusYears(7).plusDays(10)); + EventEntity eventEntity2 = dartsDatabase.getEventStub().createEvent(hearing, 214);//case closed + eventEntity2.setCreatedDateTime(closeDate); + EventEntity eventEntity3 = dartsDatabase.getEventStub().createEvent(hearing, 23);//Application: No case to answer + eventEntity3.setCreatedDateTime(OffsetDateTime.now().minusYears(7).plusDays(5)); + dartsDatabase.saveAll(eventEntity1, eventEntity2, eventEntity3); + + CourtCaseEntity courtCaseEntity = hearing.getCourtCase(); + courtCaseEntity.setCreatedDateTime(OffsetDateTime.now().minusYears(10)); + dartsDatabase.getCaseRepository().save(courtCaseEntity); + assertFalse(courtCaseEntity.getClosed()); + + // When + closeOldCasesProcessor.closeCases(BATCH_SIZE); + + // Then + CourtCaseEntity updatedCourtCaseEntity = dartsDatabase.getCaseRepository().findById(courtCaseEntity.getId()).orElse(null); + assert updatedCourtCaseEntity != null; + assertTrue(updatedCourtCaseEntity.getClosed()); + assertEquals(closeDate.truncatedTo(ChronoUnit.MINUTES), + updatedCourtCaseEntity.getCaseClosedTimestamp().truncatedTo(ChronoUnit.MINUTES)); + assertNull(updatedCourtCaseEntity.getRetConfScore()); + assertNull(updatedCourtCaseEntity.getRetConfReason()); + assertEquals(CURRENT_DATE_TIME, updatedCourtCaseEntity.getRetConfUpdatedTs()); + CaseRetentionEntity caseRetentionEntity = dartsDatabase.getCaseRetentionRepository().findAll().get(0); + assertEquals(courtCaseEntity.getId(), caseRetentionEntity.getCourtCase().getId()); + assertEquals(closeDate.plusYears(7).truncatedTo(ChronoUnit.DAYS), caseRetentionEntity.getRetainUntil()); + assertEquals(RetentionConfidenceCategoryEnum.AGED_CASE, caseRetentionEntity.getConfidenceCategory()); + } + @Test void givenEventsUseLatestDateAsClosedDate() { + createAndSaveRetentionConfidenceCategoryMappings(); + HearingEntity hearing = dartsDatabase.createHearing("a_courthouse", "1", "1078", LocalDateTime.now().minusYears(7)); OffsetDateTime closeDate = OffsetDateTime.now().minusYears(7); @@ -129,6 +196,8 @@ void givenEventsUseLatestDateAsClosedDate() { @Test void givenOneEventUseLatestDateAsClosedDate() { + createAndSaveRetentionConfidenceCategoryMappings(); + HearingEntity hearing = dartsDatabase.createHearing("a_courthouse", "1", "1078", LocalDateTime.now().minusYears(7)); OffsetDateTime closeDate = OffsetDateTime.now().minusYears(7); @@ -159,6 +228,8 @@ void givenOneEventUseLatestDateAsClosedDate() { @Test void givenAudioUseDateAsClosedDate() { + createAndSaveRetentionConfidenceCategoryMappings(); + OffsetDateTime closeDate = OffsetDateTime.now().minusYears(6).plusDays(2); MediaEntity mediaEntity1 = dartsDatabase.createMediaEntity("acourthosue", "1", OffsetDateTime.now().minusYears(7), OffsetDateTime.now().minusYears(7).plusMinutes(20), 1); @@ -198,6 +269,8 @@ void givenAudioUseDateAsClosedDate() { @Test void givenOnlyHearingUseDateAsClosedDate() { + createAndSaveRetentionConfidenceCategoryMappings(); + LocalDateTime closeDate = DateConverterUtil.toLocalDateTime(OffsetDateTime.now().minusYears(7)); dartsDatabase.createHearing("a_courthouse", "1", "1078", closeDate.minusDays(10)); HearingEntity hearing = dartsDatabase.createHearing("a_courthouse", "1", "1078", closeDate); @@ -224,6 +297,8 @@ void givenOnlyHearingUseDateAsClosedDate() { @Test void givenNoDataUseCreatedDateAsClosedDate() { + createAndSaveRetentionConfidenceCategoryMappings(); + OffsetDateTime closeDate = OffsetDateTime.now().minusYears(7); CourtCaseEntity courtCaseEntity = dartsDatabase.createCase("a_courthouse", "019278"); courtCaseEntity.setCreatedDateTime(closeDate); @@ -245,6 +320,8 @@ void givenNoDataUseCreatedDateAsClosedDate() { @Test void givenNoDataUseCreatedDateAsClosedDateUseBatchSizeTwo() { + createAndSaveRetentionConfidenceCategoryMappings(); + // given OffsetDateTime closeDate1 = OffsetDateTime.now().minusYears(9); CourtCaseEntity courtCaseEntity1 = dartsDatabase.createCase("a_courthouse", "019278"); @@ -290,6 +367,8 @@ void givenNoDataUseCreatedDateAsClosedDateUseBatchSizeTwo() { @Test void givenRetentionPolicyDoNotClose() { + createAndSaveRetentionConfidenceCategoryMappings(); + CourtCaseEntity courtCaseEntity = dartsDatabase.createCase("a_courthouse", "019278"); courtCaseEntity.setCreatedDateTime(OffsetDateTime.now().minusYears(7)); dartsDatabase.getCaseRepository().save(courtCaseEntity); @@ -313,6 +392,8 @@ void givenRetentionPolicyDoNotClose() { @Test void givenNotSixYearsOldDoNotClose() { + createAndSaveRetentionConfidenceCategoryMappings(); + CourtCaseEntity courtCaseEntity = dartsDatabase.createCase("a_courthouse", "019278"); courtCaseEntity.setCreatedDateTime(OffsetDateTime.now().minusYears(5).minusDays(360)); dartsDatabase.getCaseRepository().save(courtCaseEntity); diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/event/service/impl/StopAndCloseHandlerTest.java b/src/integrationTest/java/uk/gov/hmcts/darts/event/service/impl/StopAndCloseHandlerTest.java index 069d5efca6..2be2c3d6c0 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/event/service/impl/StopAndCloseHandlerTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/event/service/impl/StopAndCloseHandlerTest.java @@ -4,7 +4,10 @@ import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; import uk.gov.hmcts.darts.authorisation.component.UserIdentity; import uk.gov.hmcts.darts.common.entity.CaseManagementRetentionEntity; import uk.gov.hmcts.darts.common.entity.CaseRetentionEntity; @@ -21,11 +24,16 @@ import uk.gov.hmcts.darts.event.model.DartsEvent; import uk.gov.hmcts.darts.event.model.DartsEventRetentionPolicy; import uk.gov.hmcts.darts.event.service.EventDispatcher; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; import uk.gov.hmcts.darts.retention.service.ApplyRetentionProcessor; +import uk.gov.hmcts.darts.test.common.data.PersistableFactory; +import uk.gov.hmcts.darts.test.common.data.RetentionConfidenceCategoryMapperTestData; +import uk.gov.hmcts.darts.test.common.data.builder.TestRetentionConfidenceCategoryMapperEntity; import uk.gov.hmcts.darts.testutils.stubs.NodeRegisterStub; +import java.time.Clock; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; @@ -50,6 +58,7 @@ import static uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus.PENDING; import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum.CASE_CLOSED; +@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true") // To override Clock bean class StopAndCloseHandlerTest extends HandlerTestData { private static final String ARCHIVE_CASE_EVENT_TYPE = "3000"; @@ -76,6 +85,13 @@ class StopAndCloseHandlerTest extends HandlerTestData { @Mock private CaseRetentionRepository caseRetentionRepository; + @TestConfiguration + public static class ClockConfig { + @Bean + public Clock clock() { + return Clock.fixed(CURRENT_DATE_TIME.toInstant(), ZoneOffset.UTC); + } + } @BeforeEach public void setupStubs() { @@ -87,6 +103,19 @@ public void setupStubs() { CourtroomEntity courtroom = dartsDatabase.createCourtroomUnlessExists(SOME_COURTHOUSE, SOME_ROOM); nodeRegisterStub.setupNodeRegistry(courtroom); dartsGateway.darNotificationReturnsSuccess(); + + createAndSaveRetentionConfidenceCategoryMappings(); + } + + private void createAndSaveRetentionConfidenceCategoryMappings() { + RetentionConfidenceCategoryMapperTestData testData = PersistableFactory.getRetentionConfidenceCategoryMapperTestData(); + + TestRetentionConfidenceCategoryMapperEntity closedMappingEntity = testData.someMinimalBuilder() + .confidenceCategory(RetentionConfidenceCategoryEnum.CASE_CLOSED) + .confidenceReason(RetentionConfidenceReasonEnum.CASE_CLOSED) + .confidenceScore(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED) + .build(); + dartsPersistence.save(closedMappingEntity.getEntity()); } @Test diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/retention/controller/RetentionControllerPostRetentionIntTest.java b/src/integrationTest/java/uk/gov/hmcts/darts/retention/controller/RetentionControllerPostRetentionIntTest.java index 17f1b8b7c9..de9dbdfb5b 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/retention/controller/RetentionControllerPostRetentionIntTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/retention/controller/RetentionControllerPostRetentionIntTest.java @@ -6,7 +6,10 @@ import org.skyscreamer.jsonassert.JSONCompareMode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; @@ -17,9 +20,15 @@ import uk.gov.hmcts.darts.common.entity.UserAccountEntity; import uk.gov.hmcts.darts.common.helper.CurrentTimeHelper; import uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; +import uk.gov.hmcts.darts.test.common.data.PersistableFactory; +import uk.gov.hmcts.darts.test.common.data.RetentionConfidenceCategoryMapperTestData; +import uk.gov.hmcts.darts.test.common.data.builder.TestRetentionConfidenceCategoryMapperEntity; import uk.gov.hmcts.darts.testutils.IntegrationBase; +import java.time.Clock; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.util.Optional; @@ -27,12 +36,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum.MANUAL_OVERRIDE; import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED; +@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true") // To override Clock bean @AutoConfigureMockMvc class RetentionControllerPostRetentionIntTest extends IntegrationBase { @Autowired @@ -50,13 +61,33 @@ class RetentionControllerPostRetentionIntTest extends IntegrationBase { private static final String SOME_CASE_NUMBER = "12345"; private static final OffsetDateTime CURRENT_DATE_TIME = OffsetDateTime.of(2024, 10, 1, 10, 0, 0, 0, ZoneOffset.UTC); + @TestConfiguration + public static class ClockConfig { + @Bean + public Clock clock() { + return Clock.fixed(CURRENT_DATE_TIME.toInstant(), ZoneOffset.UTC); + } + } + @BeforeEach void setUp() { when(currentTimeHelper.currentOffsetDateTime()).thenReturn(CURRENT_DATE_TIME); } + private void createAndSaveRetentionConfidenceCategoryMappings() { + RetentionConfidenceCategoryMapperTestData testData = PersistableFactory.getRetentionConfidenceCategoryMapperTestData(); + + TestRetentionConfidenceCategoryMapperEntity manualOverrideMappingEntity = testData.someMinimalBuilder() + .confidenceCategory(RetentionConfidenceCategoryEnum.MANUAL_OVERRIDE) + .confidenceReason(RetentionConfidenceReasonEnum.MANUAL_OVERRIDE) + .confidenceScore(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED) + .build(); + dartsPersistence.save(manualOverrideMappingEntity.getEntity()); + } + @Test void happyPath() throws Exception { + createAndSaveRetentionConfidenceCategoryMappings(); UserAccountEntity testUser = dartsDatabase.getUserAccountStub() .createAuthorisedIntegrationTestUser(SOME_COURTHOUSE); @@ -104,8 +135,63 @@ void happyPath() throws Exception { assertThat(actualCourtCase.getRetConfUpdatedTs()).isEqualTo(CURRENT_DATE_TIME); } + @Test + void postRetentions_shouldSucceed_andSetNullConfidenceScoreAndReason_whenNoConfidenceMappingExists() throws Exception { + // Given createAndSaveRetentionConfidenceCategoryMappings() is not invoked, so no confidence mappings exist in the DB + + // And + UserAccountEntity testUser = dartsDatabase.getUserAccountStub() + .createAuthorisedIntegrationTestUser(SOME_COURTHOUSE); + when(mockUserIdentity.getUserAccount()).thenReturn(testUser); + + CourtCaseEntity courtCase = dartsDatabase.createCase( + SOME_COURTHOUSE, + SOME_CASE_NUMBER + ); + courtCase.setCaseClosedTimestamp(OffsetDateTime.of(2020, 10, 10, 10, 0, 0, 0, ZoneOffset.UTC)); + courtCase.setClosed(true); + dartsDatabase.save(courtCase); + OffsetDateTime retainUntilDate = OffsetDateTime.parse("2023-01-01T12:00Z"); + + dartsDatabase.createCaseRetentionObject(courtCase, CaseRetentionStatus.COMPLETE, retainUntilDate, false); + + String requestBody = """ + { + "case_id": <>, + "retention_date": "2024-05-20", + "is_permanent_retention": false, + "comments": "string" + }"""; + + requestBody = requestBody.replace("<>", courtCase.getId().toString()); + MockHttpServletRequestBuilder requestBuilder = post(ENDPOINT_URL) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(requestBody); + + // When + String actualResponse = mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn().getResponse().getContentAsString(); + + // Then + Optional latestCompletedRetention = dartsDatabase.getCaseRetentionRepository().findLatestCompletedRetention(courtCase); + assertEquals(OffsetDateTime.parse("2024-05-20T00:00Z"), latestCompletedRetention.get().getRetainUntil()); + assertThat(latestCompletedRetention.get().getConfidenceCategory()).isEqualTo(MANUAL_OVERRIDE); + + String expectedResponse = """ + { + "retention_date": "2024-05-20" + } + """; + JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE); + CourtCaseEntity actualCourtCase = dartsDatabase.getCaseRepository().findById(courtCase.getId()).get(); + assertNull(actualCourtCase.getRetConfScore()); + assertNull(actualCourtCase.getRetConfReason()); + assertThat(actualCourtCase.getRetConfUpdatedTs()).isEqualTo(CURRENT_DATE_TIME); + } + @Test void happyPath_validateOnly() throws Exception { + createAndSaveRetentionConfidenceCategoryMappings(); UserAccountEntity testUser = dartsDatabase.getUserAccountStub() .createAuthorisedIntegrationTestUser(SOME_COURTHOUSE); @@ -152,6 +238,8 @@ void happyPath_validateOnly() throws Exception { @Test void happyPath_judgeReducingRetention() throws Exception { + createAndSaveRetentionConfidenceCategoryMappings(); + CourtCaseEntity courtCase = dartsDatabase.createCase( SOME_COURTHOUSE, SOME_CASE_NUMBER @@ -196,6 +284,8 @@ void happyPath_judgeReducingRetention() throws Exception { @Test void givenARetentionDateEarlierThanLastAutomatedThenThrow422() throws Exception { + createAndSaveRetentionConfidenceCategoryMappings(); + CourtCaseEntity courtCase = dartsDatabase.createCase( SOME_COURTHOUSE, SOME_CASE_NUMBER @@ -239,6 +329,8 @@ void givenARetentionDateEarlierThanLastAutomatedThenThrow422() throws Exception @Test void givenARetentionDateLaterThanMaxRetentionThenThrow422() throws Exception { + createAndSaveRetentionConfidenceCategoryMappings(); + CourtCaseEntity courtCase = dartsDatabase.createCase( SOME_COURTHOUSE, SOME_CASE_NUMBER diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsDatabaseStub.java b/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsDatabaseStub.java index b8d2475e58..1f0513446c 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsDatabaseStub.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsDatabaseStub.java @@ -85,6 +85,7 @@ import uk.gov.hmcts.darts.common.repository.ObjectStateRecordRepository; import uk.gov.hmcts.darts.common.repository.ProsecutorRepository; import uk.gov.hmcts.darts.common.repository.RegionRepository; +import uk.gov.hmcts.darts.common.repository.RetentionConfidenceCategoryMapperRepository; import uk.gov.hmcts.darts.common.repository.RetentionPolicyTypeRepository; import uk.gov.hmcts.darts.common.repository.SecurityGroupRepository; import uk.gov.hmcts.darts.common.repository.SecurityRoleRepository; @@ -201,6 +202,7 @@ public class DartsDatabaseStub { private final AutomatedTaskRepository automatedTaskRepository; private final ObjectAdminActionRepository objectAdminActionRepository; private final EventLinkedCaseRepository eventLinkedCaseRepository; + private final RetentionConfidenceCategoryMapperRepository retentionConfidenceCategoryMapperRepository; private final AnnotationStub annotationStub; private final AuditStub auditStub; @@ -306,6 +308,7 @@ public void clearDatabaseInThisOrder() { annotationRepository.deleteAll(); transcriptionRepository.deleteAll(); transcriptionWorkflowRepository.deleteAll(); + retentionConfidenceCategoryMapperRepository.deleteAll(); } @SafeVarargs diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsPersistence.java b/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsPersistence.java index 6d4938e845..6b6b3fe224 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsPersistence.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsPersistence.java @@ -29,6 +29,7 @@ import uk.gov.hmcts.darts.common.entity.MediaLinkedCaseEntity; import uk.gov.hmcts.darts.common.entity.ObjectAdminActionEntity; import uk.gov.hmcts.darts.common.entity.ProsecutorEntity; +import uk.gov.hmcts.darts.common.entity.RetentionConfidenceCategoryMapperEntity; import uk.gov.hmcts.darts.common.entity.RetentionPolicyTypeEntity; import uk.gov.hmcts.darts.common.entity.TranscriptionCommentEntity; import uk.gov.hmcts.darts.common.entity.TranscriptionDocumentEntity; @@ -72,6 +73,7 @@ import uk.gov.hmcts.darts.common.repository.ObjectRecordStatusRepository; import uk.gov.hmcts.darts.common.repository.ProsecutorRepository; import uk.gov.hmcts.darts.common.repository.RegionRepository; +import uk.gov.hmcts.darts.common.repository.RetentionConfidenceCategoryMapperRepository; import uk.gov.hmcts.darts.common.repository.RetentionPolicyTypeRepository; import uk.gov.hmcts.darts.common.repository.SecurityGroupRepository; import uk.gov.hmcts.darts.common.repository.SecurityRoleRepository; @@ -150,11 +152,28 @@ public class DartsPersistence { private final AutomatedTaskRepository automatedTaskRepository; private final ObjectAdminActionRepository objectAdminActionRepository; private final EventLinkedCaseRepository eventLinkedCaseRepository; + private final RetentionConfidenceCategoryMapperRepository retentionConfidenceCategoryMapperRepository; private final EntityManager entityManager; private final CurrentTimeHelper currentTimeHelper; private final TransactionalUtil transactionalUtil; + @Transactional + public RetentionConfidenceCategoryMapperEntity save(RetentionConfidenceCategoryMapperEntity retentionConfidenceCategoryMapperEntity) { + retentionConfidenceCategoryMapperEntity = (RetentionConfidenceCategoryMapperEntity) preCheckPersist(retentionConfidenceCategoryMapperEntity); + + if (retentionConfidenceCategoryMapperEntity.getId() == null) { + retentionConfidenceCategoryMapperEntity.setLastModifiedBy(save(retentionConfidenceCategoryMapperEntity.getLastModifiedBy())); + retentionConfidenceCategoryMapperEntity.setCreatedBy(save(retentionConfidenceCategoryMapperEntity.getCreatedBy())); + + retentionConfidenceCategoryMapperEntity = retentionConfidenceCategoryMapperRepository.save(retentionConfidenceCategoryMapperEntity); + } else { + retentionConfidenceCategoryMapperEntity = entityManager.merge(retentionConfidenceCategoryMapperEntity); + } + + return retentionConfidenceCategoryMapperEntity; + } + @Transactional @SuppressWarnings("PMD.AvoidReassigningParameters") public HearingEntity save(HearingEntity hearing) { diff --git a/src/main/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImpl.java b/src/main/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImpl.java index 175a3999d2..810e6d45c9 100644 --- a/src/main/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImpl.java +++ b/src/main/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImpl.java @@ -17,7 +17,6 @@ import uk.gov.hmcts.darts.common.entity.HearingEntity; import uk.gov.hmcts.darts.common.entity.MediaEntity; import uk.gov.hmcts.darts.common.entity.UserAccountEntity; -import uk.gov.hmcts.darts.common.helper.CurrentTimeHelper; import uk.gov.hmcts.darts.common.repository.CaseRepository; import uk.gov.hmcts.darts.common.repository.CaseRetentionRepository; import uk.gov.hmcts.darts.retention.api.RetentionApi; @@ -25,7 +24,6 @@ import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.enums.RetentionPolicyEnum; import uk.gov.hmcts.darts.retention.helper.RetentionDateHelper; -import uk.gov.hmcts.darts.retention.service.RetentionService; import uk.gov.hmcts.darts.retentions.model.PostRetentionRequest; import java.time.LocalDate; @@ -46,7 +44,6 @@ public class CloseOldCasesProcessorImpl implements CloseOldCasesProcessor { private final CloseOldCasesProcessorImpl.CloseCaseProcessor caseProcessor; private final CaseRepository caseRepository; - private final RetentionService retentionService; // TODO Call via an API layer private final AuthorisationApi authorisationApi; @@ -76,13 +73,12 @@ public void closeCases(int batchSize) { @Service @RequiredArgsConstructor(onConstructor = @__(@Autowired)) - public class CloseCaseProcessor { + static class CloseCaseProcessor { private static final String CLOSE_CASE_RETENTION_COMMENT = "CloseOldCases Automated job setting retention period to Default"; private final CaseService caseService; private final CaseRetentionRepository caseRetentionRepository; private final RetentionApi retentionApi; private final RetentionDateHelper retentionDateHelper; - private final CurrentTimeHelper currentTimeHelper; @Value("#{'${darts.retention.close-events}'.split(',')}") private List closeEvents; @@ -136,7 +132,7 @@ private void closeCaseInDbAndAddRetention(CourtCaseEntity courtCase, OffsetDateT courtCase.setClosed(TRUE); courtCase.setCaseClosedTimestamp(caseClosedDate); final RetentionConfidenceCategoryEnum retentionConfidenceCategory = RetentionConfidenceCategoryEnum.AGED_CASE; - courtCase = retentionService.updateCourtCaseConfidenceAttributesForRetention(courtCase, retentionConfidenceCategory); + courtCase = retentionApi.updateCourtCaseConfidenceAttributesForRetention(courtCase, retentionConfidenceCategory); caseService.saveCase(courtCase); log.info("Closed court case id {}", courtCase.getId()); diff --git a/src/main/java/uk/gov/hmcts/darts/event/service/handler/StopAndCloseHandler.java b/src/main/java/uk/gov/hmcts/darts/event/service/handler/StopAndCloseHandler.java index c36456e6ae..70cdca7076 100644 --- a/src/main/java/uk/gov/hmcts/darts/event/service/handler/StopAndCloseHandler.java +++ b/src/main/java/uk/gov/hmcts/darts/event/service/handler/StopAndCloseHandler.java @@ -12,7 +12,6 @@ import uk.gov.hmcts.darts.common.entity.CourtCaseEntity; import uk.gov.hmcts.darts.common.entity.EventHandlerEntity; import uk.gov.hmcts.darts.common.entity.UserAccountEntity; -import uk.gov.hmcts.darts.common.helper.CurrentTimeHelper; import uk.gov.hmcts.darts.common.repository.CaseRepository; import uk.gov.hmcts.darts.common.repository.CaseRetentionRepository; import uk.gov.hmcts.darts.common.repository.EventRepository; @@ -32,9 +31,7 @@ import uk.gov.hmcts.darts.retention.api.RetentionApi; import uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus; import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; -import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; import uk.gov.hmcts.darts.retention.enums.RetentionPolicyEnum; -import uk.gov.hmcts.darts.retention.service.RetentionService; import uk.gov.hmcts.darts.util.DataUtil; import java.time.LocalDate; @@ -45,7 +42,6 @@ import static java.lang.Boolean.TRUE; import static uk.gov.hmcts.darts.event.enums.DarNotifyType.STOP_RECORDING; -import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED; @Service @Slf4j @@ -55,10 +51,7 @@ public class StopAndCloseHandler extends EventHandlerBase { private final CaseRetentionRepository caseRetentionRepository; private final RetentionApi retentionApi; private final CaseManagementRetentionService caseManagementRetentionService; - private final RetentionService retentionService; // TODO Call via an API layer private final AuthorisationApi authorisationApi; - private final CurrentTimeHelper currentTimeHelper; - @Value("${darts.retention.overridable-fixed-policy-keys}") List overridableFixedPolicyKeys; @@ -75,19 +68,15 @@ public StopAndCloseHandler(RetrieveCoreObjectService retrieveCoreObjectService, AuthorisationApi authorisationApi, LogApi logApi, CaseManagementRetentionService caseManagementRetentionService, - EventPersistenceService eventPersistenceService, RetentionService retentionService, - CurrentTimeHelper currentTimeHelper) { + EventPersistenceService eventPersistenceService) { super(retrieveCoreObjectService, eventRepository, hearingRepository, caseRepository, eventPublisher, logApi, eventPersistenceService); this.darNotifyService = darNotifyService; this.caseRetentionRepository = caseRetentionRepository; this.caseManagementRetentionService = caseManagementRetentionService; this.retentionApi = retentionApi; this.authorisationApi = authorisationApi; - this.retentionService = retentionService; - this.currentTimeHelper = currentTimeHelper; } - @Override @Transactional @SuppressWarnings("PMD.EmptyCatchBlock") @@ -152,7 +141,7 @@ private void closeCase(DartsEvent dartsEvent, CourtCaseEntity courtCase) { courtCase.setClosed(TRUE); courtCase.setCaseClosedTimestamp(dartsEvent.getDateTime()); courtCase.setLastModifiedBy(authorisationApi.getCurrentUser()); - courtCase = retentionService.updateCourtCaseConfidenceAttributesForRetention(courtCase, RetentionConfidenceCategoryEnum.CASE_CLOSED); + courtCase = retentionApi.updateCourtCaseConfidenceAttributesForRetention(courtCase, RetentionConfidenceCategoryEnum.CASE_CLOSED); caseRepository.saveAndFlush(courtCase); } } diff --git a/src/main/java/uk/gov/hmcts/darts/retention/api/RetentionApi.java b/src/main/java/uk/gov/hmcts/darts/retention/api/RetentionApi.java index 8b937c58a6..807bc72efb 100644 --- a/src/main/java/uk/gov/hmcts/darts/retention/api/RetentionApi.java +++ b/src/main/java/uk/gov/hmcts/darts/retention/api/RetentionApi.java @@ -19,4 +19,6 @@ CaseRetentionEntity createRetention(PostRetentionRequest postRetentionRequest, UserAccountEntity userAccount, CaseRetentionStatus caseRetentionStatus, RetentionConfidenceCategoryEnum retentionConfidenceCategory); + + CourtCaseEntity updateCourtCaseConfidenceAttributesForRetention(CourtCaseEntity courtCase, RetentionConfidenceCategoryEnum confidenceCategory); } diff --git a/src/main/java/uk/gov/hmcts/darts/retention/api/impl/RetentionApiImpl.java b/src/main/java/uk/gov/hmcts/darts/retention/api/impl/RetentionApiImpl.java index 85569966d9..3da7b3baaf 100644 --- a/src/main/java/uk/gov/hmcts/darts/retention/api/impl/RetentionApiImpl.java +++ b/src/main/java/uk/gov/hmcts/darts/retention/api/impl/RetentionApiImpl.java @@ -11,6 +11,7 @@ import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.helper.RetentionDateHelper; import uk.gov.hmcts.darts.retention.service.RetentionPostService; +import uk.gov.hmcts.darts.retention.service.RetentionService; import uk.gov.hmcts.darts.retentions.model.PostRetentionRequest; import java.time.LocalDate; @@ -21,6 +22,7 @@ public class RetentionApiImpl implements RetentionApi { private final RetentionDateHelper retentionDateHelper; private final RetentionPostService retentionPostService; + private final RetentionService retentionService; @Override public LocalDate applyPolicyStringToDate(LocalDate dateToAppend, String policyString, RetentionPolicyTypeEntity retentionPolicyType) { @@ -41,4 +43,10 @@ public CaseRetentionEntity createRetention(PostRetentionRequest postRetentionReq caseRetentionStatus, retentionConfidenceCategory); } + + @Override + public CourtCaseEntity updateCourtCaseConfidenceAttributesForRetention(CourtCaseEntity courtCase, RetentionConfidenceCategoryEnum confidenceCategory) { + return retentionService.updateCourtCaseConfidenceAttributesForRetention(courtCase, confidenceCategory); + } + } diff --git a/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionService.java b/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionService.java index dd03a104b6..d22f1b346d 100644 --- a/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionService.java +++ b/src/main/java/uk/gov/hmcts/darts/retention/service/RetentionService.java @@ -11,4 +11,5 @@ public interface RetentionService { List getCaseRetentions(Integer caseId); CourtCaseEntity updateCourtCaseConfidenceAttributesForRetention(CourtCaseEntity courtCase, RetentionConfidenceCategoryEnum confidenceCategory); + } diff --git a/src/test/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImplTest.java b/src/test/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImplTest.java index 0693162a6e..ef9dc2f922 100644 --- a/src/test/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImplTest.java +++ b/src/test/java/uk/gov/hmcts/darts/cases/service/impl/CloseOldCasesProcessorImplTest.java @@ -18,6 +18,7 @@ import uk.gov.hmcts.darts.common.util.CommonTestDataUtil; import uk.gov.hmcts.darts.common.util.DateConverterUtil; import uk.gov.hmcts.darts.retention.api.RetentionApi; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.helper.RetentionDateHelper; import java.time.LocalDateTime; @@ -25,11 +26,10 @@ import java.time.ZoneOffset; import java.util.List; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.when; @@ -66,18 +66,16 @@ void setUp() { caseService, caseRetentionRepository, retentionApi, - retentionDateHelper, - currentTimeHelper + retentionDateHelper ); closeOldCasesProcessor = new CloseOldCasesProcessorImpl(caseProcessor, caseRepository, authorisationApi); lenient().when(currentTimeHelper.currentOffsetDateTime()).thenReturn(CURRENT_DATE_TIME); - } @Test - void closeCases() { + void closeCases_shouldCloseTheExpectedCases_andSetExpectedRetentionConfidence() { // given LocalDateTime hearingDate = DateConverterUtil.toLocalDateTime(OffsetDateTime.now().minusYears(7)); OffsetDateTime createdDate = OffsetDateTime.now().minusYears(7); @@ -94,16 +92,17 @@ void closeCases() { when(caseService.getCourtCaseById(1)).thenReturn(courtCase); CaseRetentionEntity caseRetention = createRetentionEntity(courtCase, userAccountEntity); - when(retentionApi.createRetention(any(), any(), any(), any(), any())).thenReturn(caseRetention); + when(retentionApi.createRetention(any(), any(), any(), any(), any(), any())).thenReturn(caseRetention); assertFalse(courtCase.getClosed()); - assertNull(courtCase.getRetConfUpdatedTs()); + + when(retentionApi.updateCourtCaseConfidenceAttributesForRetention(any(), eq(RetentionConfidenceCategoryEnum.AGED_CASE))) + .thenReturn(courtCase); // when closeOldCasesProcessor.closeCases(2); // then assertTrue(courtCase.getClosed()); - assertEquals(CURRENT_DATE_TIME, courtCase.getRetConfUpdatedTs()); } public static CaseRetentionEntity createRetentionEntity(CourtCaseEntity courtCase, UserAccountEntity userAccount) { @@ -114,4 +113,5 @@ public static CaseRetentionEntity createRetentionEntity(CourtCaseEntity courtCas caseRetention.setSubmittedBy(userAccount); return caseRetention; } + } \ No newline at end of file diff --git a/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionPostServiceImplTest.java b/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionPostServiceImplTest.java index 12db71a995..e539ae745d 100644 --- a/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionPostServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionPostServiceImplTest.java @@ -22,10 +22,9 @@ import uk.gov.hmcts.darts.common.repository.RetentionPolicyTypeRepository; import uk.gov.hmcts.darts.common.util.CommonTestDataUtil; import uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus; -import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; -import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; import uk.gov.hmcts.darts.retention.enums.RetentionPolicyEnum; import uk.gov.hmcts.darts.retention.helper.RetentionDateHelper; +import uk.gov.hmcts.darts.retention.service.RetentionService; import uk.gov.hmcts.darts.retentions.model.PostRetentionRequest; import uk.gov.hmcts.darts.retentions.model.PostRetentionResponse; @@ -66,15 +65,14 @@ class RetentionPostServiceImplTest { private CurrentTimeHelper currentTimeHelper; @Mock private AuditApi auditApi; + @Mock + private RetentionService retentionService; @Mock private RetentionDateHelper retentionDateHelper; @Captor ArgumentCaptor caseRetentionEntityArgumentCaptor; - @Captor - ArgumentCaptor courtCaseEntityArgumentCaptor; - private void setupStubs() { CourtCaseEntity courtCase = CommonTestDataUtil.createCase("1"); @@ -305,11 +303,6 @@ void ok_BeforeCurrentRetentionDate_Judge() { assertEquals(10, savedRetention.getCreatedBy().getId()); assertEquals(RetentionPolicyEnum.MANUAL.getPolicyKey(), savedRetention.getRetentionPolicyType().getFixedPolicyKey()); assertEquals(MANUAL_OVERRIDE, savedRetention.getConfidenceCategory()); - verify(caseRepository).save(courtCaseEntityArgumentCaptor.capture()); - CourtCaseEntity savedCourtCase = courtCaseEntityArgumentCaptor.getValue(); - assertEquals(RetentionConfidenceReasonEnum.MANUAL_OVERRIDE, savedCourtCase.getRetConfReason()); - assertEquals(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED, savedCourtCase.getRetConfScore()); - assertEquals(CURRENT_DATE_TIME, savedCourtCase.getRetConfUpdatedTs()); } @Test @@ -332,11 +325,6 @@ void happy_path_increaseTime() { assertEquals(10, savedRetention.getCreatedBy().getId()); assertEquals(RetentionPolicyEnum.MANUAL.getPolicyKey(), savedRetention.getRetentionPolicyType().getFixedPolicyKey()); assertEquals(MANUAL_OVERRIDE, savedRetention.getConfidenceCategory()); - verify(caseRepository).save(courtCaseEntityArgumentCaptor.capture()); - CourtCaseEntity savedCourtCase = courtCaseEntityArgumentCaptor.getValue(); - assertEquals(RetentionConfidenceReasonEnum.MANUAL_OVERRIDE, savedCourtCase.getRetConfReason()); - assertEquals(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED, savedCourtCase.getRetConfScore()); - assertEquals(CURRENT_DATE_TIME, savedCourtCase.getRetConfUpdatedTs()); } @Test @@ -360,11 +348,6 @@ void happy_path_sameDateAsLastAutomated() { assertEquals(10, savedRetention.getCreatedBy().getId()); assertEquals(RetentionPolicyEnum.MANUAL.getPolicyKey(), savedRetention.getRetentionPolicyType().getFixedPolicyKey()); assertEquals(MANUAL_OVERRIDE, savedRetention.getConfidenceCategory()); - verify(caseRepository).save(courtCaseEntityArgumentCaptor.capture()); - CourtCaseEntity savedCourtCase = courtCaseEntityArgumentCaptor.getValue(); - assertEquals(RetentionConfidenceReasonEnum.MANUAL_OVERRIDE, savedCourtCase.getRetConfReason()); - assertEquals(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED, savedCourtCase.getRetConfScore()); - assertEquals(CURRENT_DATE_TIME, savedCourtCase.getRetConfUpdatedTs()); } @Test @@ -403,11 +386,6 @@ void happy_path_permanent() { assertEquals(10, savedRetention.getCreatedBy().getId()); assertEquals(RetentionPolicyEnum.PERMANENT.getPolicyKey(), savedRetention.getRetentionPolicyType().getFixedPolicyKey()); assertEquals(MANUAL_OVERRIDE, savedRetention.getConfidenceCategory()); - verify(caseRepository).save(courtCaseEntityArgumentCaptor.capture()); - CourtCaseEntity savedCourtCase = courtCaseEntityArgumentCaptor.getValue(); - assertEquals(RetentionConfidenceReasonEnum.MANUAL_OVERRIDE, savedCourtCase.getRetConfReason()); - assertEquals(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED, savedCourtCase.getRetConfScore()); - assertEquals(CURRENT_DATE_TIME, savedCourtCase.getRetConfUpdatedTs()); } } \ No newline at end of file diff --git a/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImplTest.java b/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImplTest.java new file mode 100644 index 0000000000..9edf560679 --- /dev/null +++ b/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImplTest.java @@ -0,0 +1,162 @@ +package uk.gov.hmcts.darts.retention.service.impl; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import uk.gov.hmcts.darts.common.entity.CaseRetentionEntity; +import uk.gov.hmcts.darts.common.entity.CourtCaseEntity; +import uk.gov.hmcts.darts.common.entity.RetentionConfidenceCategoryMapperEntity; +import uk.gov.hmcts.darts.common.repository.CaseRepository; +import uk.gov.hmcts.darts.common.repository.CaseRetentionRepository; +import uk.gov.hmcts.darts.common.repository.RetentionConfidenceCategoryMapperRepository; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; +import uk.gov.hmcts.darts.retention.mapper.RetentionMapper; +import uk.gov.hmcts.darts.retention.service.RetentionService; + +import java.time.Clock; +import java.time.Instant; +import java.time.ZoneId; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class RetentionServiceImplTest { + + @Mock + private CaseRetentionRepository caseRetentionRepository; + @Mock + private RetentionConfidenceCategoryMapperRepository retentionConfidenceCategoryMapperRepository; + @Mock + private CaseRepository caseRepository; + @Mock + private RetentionMapper retentionMapper; + + private RetentionService retentionService; + + public static final String FIXED_DATE_TIME = "2024-01-01T00:00:00Z"; + + @BeforeEach + void setUp() { + Clock clock = Clock.fixed(Instant.parse(FIXED_DATE_TIME), + ZoneId.of("UTC")); + + retentionService = new RetentionServiceImpl(caseRetentionRepository, + retentionConfidenceCategoryMapperRepository, + caseRepository, + retentionMapper, + clock); + } + + @Nested + class GetCaseRetentionsTest { + + private static final int CASE_ID = 1; + + @Test + void shouldReturnCaseRetentions_whenCaseRetentionsExist() { + // Given + var caseRetentionEntity = new CaseRetentionEntity(); + when(caseRetentionRepository.findByCaseId(eq(CASE_ID))) + .thenReturn(List.of(caseRetentionEntity)); + + // When + var caseRetentions = retentionService.getCaseRetentions(CASE_ID); + + // Then + verify(retentionMapper).mapToCaseRetention(caseRetentionEntity); + assertEquals(1, caseRetentions.size()); + } + + @Test + void shouldReturnEmptyList_whenCaseRetentionsDoNotExist() { + // Given + when(caseRetentionRepository.findByCaseId(any())) + .thenReturn(List.of()); + + // When + var caseRetentions = retentionService.getCaseRetentions(CASE_ID); + + // Then + verify(retentionMapper, never()).mapToCaseRetention(any()); + assertEquals(0, caseRetentions.size()); + } + + } + + @Nested + class UpdateCourtCaseConfidenceAttributesForRetentionTest { + + @Captor + private ArgumentCaptor caseEntityCaptor; + + @Test + void shouldUpdateConfidenceAttributes_whenConfidenceMappingExistsInDB() { + // Given + RetentionConfidenceCategoryMapperEntity confidenceMapping = createConfidenceMapping(); + when(retentionConfidenceCategoryMapperRepository.findByConfidenceCategory(eq(RetentionConfidenceCategoryEnum.CASE_CLOSED))) + .thenReturn(Optional.of(confidenceMapping)); + + var courtCaseEntity = new CourtCaseEntity(); + + // When + retentionService.updateCourtCaseConfidenceAttributesForRetention(courtCaseEntity, + RetentionConfidenceCategoryEnum.CASE_CLOSED); + + // Then + verify(caseRepository).save(caseEntityCaptor.capture()); + CourtCaseEntity mutatedCourtCaseToBePersisted = caseEntityCaptor.getValue(); + + assertEquals(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED, mutatedCourtCaseToBePersisted.getRetConfScore()); + assertEquals(RetentionConfidenceReasonEnum.CASE_CLOSED, mutatedCourtCaseToBePersisted.getRetConfReason()); + assertEquals(Instant.parse(FIXED_DATE_TIME), mutatedCourtCaseToBePersisted.getRetConfUpdatedTs().toInstant()); + } + + @Test + void shouldSetNullScoreAndReason_whenConfidenceMappingDoesNotExistInDB() { + // Given + when(retentionConfidenceCategoryMapperRepository.findByConfidenceCategory(any())) + .thenReturn(Optional.empty()); + + var courtCaseEntity = new CourtCaseEntity(); + + // When + retentionService.updateCourtCaseConfidenceAttributesForRetention(courtCaseEntity, + RetentionConfidenceCategoryEnum.CASE_CLOSED); + + // Then + verify(caseRepository).save(caseEntityCaptor.capture()); + CourtCaseEntity mutatedCourtCaseToBePersisted = caseEntityCaptor.getValue(); + + assertNull(mutatedCourtCaseToBePersisted.getRetConfScore()); + assertNull(mutatedCourtCaseToBePersisted.getRetConfReason()); + assertEquals(Instant.parse(FIXED_DATE_TIME), mutatedCourtCaseToBePersisted.getRetConfUpdatedTs().toInstant()); + } + + private RetentionConfidenceCategoryMapperEntity createConfidenceMapping() { + var mappingEntity = new RetentionConfidenceCategoryMapperEntity(); + mappingEntity.setConfidenceCategory(RetentionConfidenceCategoryEnum.CASE_CLOSED); + + mappingEntity.setConfidenceScore(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED); + mappingEntity.setConfidenceReason(RetentionConfidenceReasonEnum.CASE_CLOSED); + + return mappingEntity; + } + + } + +} diff --git a/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/PersistableFactory.java b/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/PersistableFactory.java index 7e8eedd1a6..fbe54aec15 100644 --- a/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/PersistableFactory.java +++ b/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/PersistableFactory.java @@ -65,4 +65,9 @@ public static HearingTestData getHearingTestData() { public static ArmRpoExecutionDetailTestData getArmRpoExecutionDetailTestData() { return new ArmRpoExecutionDetailTestData(); } + + public static RetentionConfidenceCategoryMapperTestData getRetentionConfidenceCategoryMapperTestData() { + return new RetentionConfidenceCategoryMapperTestData(); + } + } \ No newline at end of file diff --git a/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/RetentionConfidenceCategoryMapperTestData.java b/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/RetentionConfidenceCategoryMapperTestData.java new file mode 100644 index 0000000000..f1ba52757c --- /dev/null +++ b/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/RetentionConfidenceCategoryMapperTestData.java @@ -0,0 +1,42 @@ +package uk.gov.hmcts.darts.test.common.data; + +import uk.gov.hmcts.darts.common.entity.RetentionConfidenceCategoryMapperEntity; +import uk.gov.hmcts.darts.common.entity.UserAccountEntity; +import uk.gov.hmcts.darts.test.common.data.builder.TestRetentionConfidenceCategoryMapperEntity; + +import java.time.OffsetDateTime; + +public class RetentionConfidenceCategoryMapperTestData + implements Persistable { + + private static final UserAccountEntity USER_ACCOUNT = UserAccountTestData.minimalUserAccount(); + + @Override + public RetentionConfidenceCategoryMapperEntity someMinimal() { + return someMinimalBuilderHolder().getBuilder() + .build(); + } + + @Override + public TestRetentionConfidenceCategoryMapperEntity.TestRetentionConfidenceCategoryMapperRetrieve someMinimalBuilderHolder() { + final var retrieve = new TestRetentionConfidenceCategoryMapperEntity.TestRetentionConfidenceCategoryMapperRetrieve(); + + final OffsetDateTime someDateTime = OffsetDateTime.now(); + + retrieve.getBuilder() + .createdAt(someDateTime) + .createdBy(USER_ACCOUNT) + .lastModifiedAt(someDateTime) + .lastModifiedBy(USER_ACCOUNT); + + return retrieve; + } + + @Override + public TestRetentionConfidenceCategoryMapperEntity.TestRetentionConfidenceCategoryMapperEntityBuilder someMinimalBuilder() { + return someMinimalBuilderHolder().getBuilder(); + } + +} diff --git a/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/builder/TestRetentionConfidenceCategoryMapperEntity.java b/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/builder/TestRetentionConfidenceCategoryMapperEntity.java new file mode 100644 index 0000000000..baf06d29dd --- /dev/null +++ b/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/builder/TestRetentionConfidenceCategoryMapperEntity.java @@ -0,0 +1,67 @@ +package uk.gov.hmcts.darts.test.common.data.builder; + +import lombok.Builder; +import lombok.RequiredArgsConstructor; +import org.apache.commons.beanutils.BeanUtils; +import org.hibernate.AssertionFailure; +import uk.gov.hmcts.darts.common.entity.RetentionConfidenceCategoryMapperEntity; +import uk.gov.hmcts.darts.common.entity.UserAccountEntity; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; + +import java.lang.reflect.InvocationTargetException; +import java.time.OffsetDateTime; + +@RequiredArgsConstructor +public class TestRetentionConfidenceCategoryMapperEntity extends RetentionConfidenceCategoryMapperEntity + implements DbInsertable { + + @Builder + public TestRetentionConfidenceCategoryMapperEntity(Integer id, RetentionConfidenceScoreEnum confidenceScore, + RetentionConfidenceReasonEnum confidenceReason, + RetentionConfidenceCategoryEnum confidenceCategory, + String description, OffsetDateTime createdAt, + OffsetDateTime lastModifiedAt, UserAccountEntity createdBy, + UserAccountEntity lastModifiedBy) { + setId(id); + setConfidenceScore(confidenceScore); + setConfidenceReason(confidenceReason); + setConfidenceCategory(confidenceCategory); + setDescription(description); + setCreatedDateTime(createdAt); + setLastModifiedDateTime(lastModifiedAt); + setCreatedBy(createdBy); + setLastModifiedBy(lastModifiedBy); + } + + @Override + public RetentionConfidenceCategoryMapperEntity getEntity() { + try { + RetentionConfidenceCategoryMapperEntity retentionConfidenceCategoryMapperEntity = new RetentionConfidenceCategoryMapperEntity(); + BeanUtils.copyProperties(retentionConfidenceCategoryMapperEntity, this); + return retentionConfidenceCategoryMapperEntity; + } catch (IllegalAccessException | InvocationTargetException e) { + throw new AssertionFailure("Assumed that there would be no error on mapping data", e); + } + } + + public static class TestRetentionConfidenceCategoryMapperRetrieve + implements BuilderHolder { + + private final TestRetentionConfidenceCategoryMapperEntity.TestRetentionConfidenceCategoryMapperEntityBuilder builder = + TestRetentionConfidenceCategoryMapperEntity.builder(); + + @Override + public TestRetentionConfidenceCategoryMapperEntity build() { + return builder.build(); + } + + @Override + public TestRetentionConfidenceCategoryMapperEntity.TestRetentionConfidenceCategoryMapperEntityBuilder getBuilder() { + return builder; + } + } + +} From 93fb0b9f6e0dc78d5c7f0e09a68b36dcc3e5b2d1 Mon Sep 17 00:00:00 2001 From: chris-bellingham Date: Wed, 18 Dec 2024 12:53:24 +0000 Subject: [PATCH 04/10] DMP-3534: Address PMD issues --- .../service/impl/StopAndCloseHandlerTest.java | 13 ++++++------- .../RetentionControllerPostRetentionIntTest.java | 6 ++---- .../darts/testutils/stubs/DartsPersistence.java | 1 + .../service/impl/RetentionServiceImplTest.java | 14 ++++++++------ ...estRetentionConfidenceCategoryMapperEntity.java | 2 ++ 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/event/service/impl/StopAndCloseHandlerTest.java b/src/integrationTest/java/uk/gov/hmcts/darts/event/service/impl/StopAndCloseHandlerTest.java index 2be2c3d6c0..85a98a0dac 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/event/service/impl/StopAndCloseHandlerTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/event/service/impl/StopAndCloseHandlerTest.java @@ -24,9 +24,7 @@ import uk.gov.hmcts.darts.event.model.DartsEvent; import uk.gov.hmcts.darts.event.model.DartsEventRetentionPolicy; import uk.gov.hmcts.darts.event.service.EventDispatcher; -import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; -import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; import uk.gov.hmcts.darts.retention.service.ApplyRetentionProcessor; import uk.gov.hmcts.darts.test.common.data.PersistableFactory; import uk.gov.hmcts.darts.test.common.data.RetentionConfidenceCategoryMapperTestData; @@ -57,6 +55,7 @@ import static uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus.IGNORED; import static uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus.PENDING; import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum.CASE_CLOSED; +import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED; @SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true") // To override Clock bean class StopAndCloseHandlerTest extends HandlerTestData { @@ -111,9 +110,9 @@ private void createAndSaveRetentionConfidenceCategoryMappings() { RetentionConfidenceCategoryMapperTestData testData = PersistableFactory.getRetentionConfidenceCategoryMapperTestData(); TestRetentionConfidenceCategoryMapperEntity closedMappingEntity = testData.someMinimalBuilder() - .confidenceCategory(RetentionConfidenceCategoryEnum.CASE_CLOSED) + .confidenceCategory(CASE_CLOSED) .confidenceReason(RetentionConfidenceReasonEnum.CASE_CLOSED) - .confidenceScore(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED) + .confidenceScore(CASE_PERFECTLY_CLOSED) .build(); dartsPersistence.save(closedMappingEntity.getEntity()); } @@ -316,7 +315,7 @@ void shouldCreateNewCaseRetentionWhenNoneExist() { assertEquals(testTime, persistedCase.getCaseClosedTimestamp()); assertTrue(persistedCase.getClosed()); assertEquals(RetentionConfidenceReasonEnum.CASE_CLOSED, persistedCase.getRetConfReason()); - assertEquals(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED, persistedCase.getRetConfScore()); + assertEquals(CASE_PERFECTLY_CLOSED, persistedCase.getRetConfScore()); assertEquals(CURRENT_DATE_TIME, persistedCase.getRetConfUpdatedTs()); var hearingsForCase = dartsDatabase.findByCourthouseCourtroomAndDate( SOME_COURTHOUSE, SOME_ROOM, testTime.toLocalDate()); @@ -412,7 +411,7 @@ void shouldUpdateExistingCaseRetentionWhenPendingExist() { assertTrue(persistedCase.getClosed()); assertEquals(testTime.plusSeconds(10), persistedCase.getCaseClosedTimestamp()); assertEquals(RetentionConfidenceReasonEnum.CASE_CLOSED, persistedCase.getRetConfReason()); - assertEquals(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED, persistedCase.getRetConfScore()); + assertEquals(CASE_PERFECTLY_CLOSED, persistedCase.getRetConfScore()); assertEquals(CURRENT_DATE_TIME, persistedCase.getRetConfUpdatedTs()); // apply retention and check it was applied correctly @@ -510,7 +509,7 @@ void createsAdditionalPendingCaseRetentionWhenCaseManagementRetentionDoesNotExis assertTrue(persistedCase.getClosed()); assertEquals(testTime.plusSeconds(10), persistedCase.getCaseClosedTimestamp()); assertEquals(RetentionConfidenceReasonEnum.CASE_CLOSED, persistedCase.getRetConfReason()); - assertEquals(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED, persistedCase.getRetConfScore()); + assertEquals(CASE_PERFECTLY_CLOSED, persistedCase.getRetConfScore()); assertEquals(CURRENT_DATE_TIME, persistedCase.getRetConfUpdatedTs()); // apply retention and check it was applied correctly diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/retention/controller/RetentionControllerPostRetentionIntTest.java b/src/integrationTest/java/uk/gov/hmcts/darts/retention/controller/RetentionControllerPostRetentionIntTest.java index de9dbdfb5b..5568fd3249 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/retention/controller/RetentionControllerPostRetentionIntTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/retention/controller/RetentionControllerPostRetentionIntTest.java @@ -20,9 +20,7 @@ import uk.gov.hmcts.darts.common.entity.UserAccountEntity; import uk.gov.hmcts.darts.common.helper.CurrentTimeHelper; import uk.gov.hmcts.darts.retention.enums.CaseRetentionStatus; -import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; -import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; import uk.gov.hmcts.darts.test.common.data.PersistableFactory; import uk.gov.hmcts.darts.test.common.data.RetentionConfidenceCategoryMapperTestData; import uk.gov.hmcts.darts.test.common.data.builder.TestRetentionConfidenceCategoryMapperEntity; @@ -78,9 +76,9 @@ private void createAndSaveRetentionConfidenceCategoryMappings() { RetentionConfidenceCategoryMapperTestData testData = PersistableFactory.getRetentionConfidenceCategoryMapperTestData(); TestRetentionConfidenceCategoryMapperEntity manualOverrideMappingEntity = testData.someMinimalBuilder() - .confidenceCategory(RetentionConfidenceCategoryEnum.MANUAL_OVERRIDE) + .confidenceCategory(MANUAL_OVERRIDE) .confidenceReason(RetentionConfidenceReasonEnum.MANUAL_OVERRIDE) - .confidenceScore(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED) + .confidenceScore(CASE_PERFECTLY_CLOSED) .build(); dartsPersistence.save(manualOverrideMappingEntity.getEntity()); } diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsPersistence.java b/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsPersistence.java index 6b6b3fe224..fb72e4b731 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsPersistence.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/testutils/stubs/DartsPersistence.java @@ -159,6 +159,7 @@ public class DartsPersistence { private final TransactionalUtil transactionalUtil; @Transactional + @SuppressWarnings("PMD.AvoidReassigningParameters") public RetentionConfidenceCategoryMapperEntity save(RetentionConfidenceCategoryMapperEntity retentionConfidenceCategoryMapperEntity) { retentionConfidenceCategoryMapperEntity = (RetentionConfidenceCategoryMapperEntity) preCheckPersist(retentionConfidenceCategoryMapperEntity); diff --git a/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImplTest.java b/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImplTest.java index 9edf560679..28ca0eeec3 100644 --- a/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/darts/retention/service/impl/RetentionServiceImplTest.java @@ -19,6 +19,8 @@ import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; import uk.gov.hmcts.darts.retention.mapper.RetentionMapper; import uk.gov.hmcts.darts.retention.service.RetentionService; +import uk.gov.hmcts.darts.test.common.data.PersistableFactory; +import uk.gov.hmcts.darts.test.common.data.RetentionConfidenceCategoryMapperTestData; import java.time.Clock; import java.time.Instant; @@ -148,13 +150,13 @@ void shouldSetNullScoreAndReason_whenConfidenceMappingDoesNotExistInDB() { } private RetentionConfidenceCategoryMapperEntity createConfidenceMapping() { - var mappingEntity = new RetentionConfidenceCategoryMapperEntity(); - mappingEntity.setConfidenceCategory(RetentionConfidenceCategoryEnum.CASE_CLOSED); + RetentionConfidenceCategoryMapperTestData testData = PersistableFactory.getRetentionConfidenceCategoryMapperTestData(); - mappingEntity.setConfidenceScore(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED); - mappingEntity.setConfidenceReason(RetentionConfidenceReasonEnum.CASE_CLOSED); - - return mappingEntity; + return testData.someMinimalBuilder() + .confidenceCategory(RetentionConfidenceCategoryEnum.CASE_CLOSED) + .confidenceScore(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED) + .confidenceReason(RetentionConfidenceReasonEnum.CASE_CLOSED) + .build(); } } diff --git a/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/builder/TestRetentionConfidenceCategoryMapperEntity.java b/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/builder/TestRetentionConfidenceCategoryMapperEntity.java index baf06d29dd..160dabb868 100644 --- a/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/builder/TestRetentionConfidenceCategoryMapperEntity.java +++ b/src/testCommon/java/uk/gov/hmcts/darts/test/common/data/builder/TestRetentionConfidenceCategoryMapperEntity.java @@ -13,6 +13,7 @@ import java.lang.reflect.InvocationTargetException; import java.time.OffsetDateTime; +@SuppressWarnings({"PMD.TestClassWithoutTestCases", "PMD.ConstructorCallsOverridableMethod"}) @RequiredArgsConstructor public class TestRetentionConfidenceCategoryMapperEntity extends RetentionConfidenceCategoryMapperEntity implements DbInsertable { @@ -24,6 +25,7 @@ public TestRetentionConfidenceCategoryMapperEntity(Integer id, RetentionConfiden String description, OffsetDateTime createdAt, OffsetDateTime lastModifiedAt, UserAccountEntity createdBy, UserAccountEntity lastModifiedBy) { + super(); setId(id); setConfidenceScore(confidenceScore); setConfidenceReason(confidenceReason); From 54c719f333a026861c94674a8926fc817f3fc0c8 Mon Sep 17 00:00:00 2001 From: chris-bellingham Date: Wed, 18 Dec 2024 15:12:26 +0000 Subject: [PATCH 05/10] DMP-3534: Update RetentionConfidenceCategoryMapperEntity to use string value for confidenceReason; add associated test --- ...onfidenceCategoryMapperRepositoryTest.java | 130 ++++++++++++++++++ ...tentionConfidenceCategoryMapperEntity.java | 3 + 2 files changed, 133 insertions(+) create mode 100644 src/integrationTest/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepositoryTest.java diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepositoryTest.java b/src/integrationTest/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepositoryTest.java new file mode 100644 index 0000000000..0ca1d930e4 --- /dev/null +++ b/src/integrationTest/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepositoryTest.java @@ -0,0 +1,130 @@ +package uk.gov.hmcts.darts.common.repository; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.IncorrectResultSizeDataAccessException; +import uk.gov.hmcts.darts.common.entity.RetentionConfidenceCategoryMapperEntity; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceCategoryEnum; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceReasonEnum; +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; +import uk.gov.hmcts.darts.test.common.data.PersistableFactory; +import uk.gov.hmcts.darts.test.common.data.RetentionConfidenceCategoryMapperTestData; +import uk.gov.hmcts.darts.test.common.data.builder.TestRetentionConfidenceCategoryMapperEntity; +import uk.gov.hmcts.darts.testutils.PostgresIntegrationBase; + +import java.sql.SQLException; +import java.util.Optional; +import javax.sql.DataSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class RetentionConfidenceCategoryMapperRepositoryTest extends PostgresIntegrationBase { + + @Autowired + private RetentionConfidenceCategoryMapperRepository repository; + + @Autowired + private DataSource dataSource; + + private static final RetentionConfidenceCategoryMapperTestData CATEGORY_MAPPER_TEST_DATA = + PersistableFactory.getRetentionConfidenceCategoryMapperTestData(); + + + @Nested + class SaveTest { + + @Test + @SuppressWarnings("PMD.CheckResultSet") + void shouldSaveEntity_withStringValueForReason_andNumericsForCategoryAndScore() throws SQLException { + // Given + TestRetentionConfidenceCategoryMapperEntity mapperTestEntity = CATEGORY_MAPPER_TEST_DATA.someMinimalBuilder() + .confidenceCategory(RetentionConfidenceCategoryEnum.MANUAL_OVERRIDE) + .confidenceReason(RetentionConfidenceReasonEnum.MANUAL_OVERRIDE) + .confidenceScore(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED) + .build(); + + // When + var preparedEntity = dartsPersistence.save(mapperTestEntity.getEntity()); + + // Then + String query = "SELECT confidence_category, ret_conf_reason, ret_conf_score FROM retention_confidence_category_mapper WHERE rcc_id = ?"; + try (var connection = dataSource.getConnection(); + var preparedStatement = connection.prepareStatement(query)) { + preparedStatement.setInt(1, preparedEntity.getId()); + try (var resultSet = preparedStatement.executeQuery()) { + assertTrue(resultSet.next()); + + assertEquals(RetentionConfidenceReasonEnum.MANUAL_OVERRIDE.name(), resultSet.getString("ret_conf_reason")); + assertEquals(RetentionConfidenceCategoryEnum.MANUAL_OVERRIDE.getId(), resultSet.getInt("confidence_category")); + assertEquals(RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED.getId(), resultSet.getInt("ret_conf_score")); + } + } + } + + } + + @Nested + class FindByConfidenceCategoryTest { + + @Test + void shouldReturnSingleResult_whenSingleMatchExists() { + // Given + RetentionConfidenceCategoryEnum category = RetentionConfidenceCategoryEnum.CASE_CLOSED; + + TestRetentionConfidenceCategoryMapperEntity mapperTestEntity = CATEGORY_MAPPER_TEST_DATA.someMinimalBuilder() + .confidenceCategory(category) + .build(); + + var preparedEntity = dartsPersistence.save(mapperTestEntity.getEntity()); + + // When + Optional returnedEntity = repository.findByConfidenceCategory(category); + + // Then + assertTrue(returnedEntity.isPresent()); + var result = returnedEntity.get(); + assertEquals(preparedEntity.getId(), result.getId()); + } + + @Test + void shouldThrowException_whenMultipleMatchesExist() { + // Given + RetentionConfidenceCategoryEnum category = RetentionConfidenceCategoryEnum.CASE_CLOSED; + + TestRetentionConfidenceCategoryMapperEntity mapperTestEntity1 = CATEGORY_MAPPER_TEST_DATA.someMinimalBuilder() + .confidenceCategory(category) + .build(); + dartsPersistence.save(mapperTestEntity1.getEntity()); + + TestRetentionConfidenceCategoryMapperEntity mapperTestEntity2 = CATEGORY_MAPPER_TEST_DATA.someMinimalBuilder() + .confidenceCategory(category) + .build(); + dartsPersistence.save(mapperTestEntity2.getEntity()); + + // When + IncorrectResultSizeDataAccessException exception = assertThrows(IncorrectResultSizeDataAccessException.class, () -> + repository.findByConfidenceCategory(category)); + assertEquals("Incorrect result size: expected 1, actual 2", exception.getMessage()); + } + + @Test + void shouldReturnEmptyOptional_whenNoMatchExists() { + Optional result = repository.findByConfidenceCategory(RetentionConfidenceCategoryEnum.CASE_CLOSED); + + assertFalse(result.isPresent()); + } + + @Test + void shouldReturnEmptyOptional_whenProvidedWithNullParam() { + Optional result = repository.findByConfidenceCategory(null); + + assertFalse(result.isPresent()); + } + + } + +} diff --git a/src/main/java/uk/gov/hmcts/darts/common/entity/RetentionConfidenceCategoryMapperEntity.java b/src/main/java/uk/gov/hmcts/darts/common/entity/RetentionConfidenceCategoryMapperEntity.java index 4697a8faa9..d7a9a3ea3e 100644 --- a/src/main/java/uk/gov/hmcts/darts/common/entity/RetentionConfidenceCategoryMapperEntity.java +++ b/src/main/java/uk/gov/hmcts/darts/common/entity/RetentionConfidenceCategoryMapperEntity.java @@ -2,6 +2,8 @@ import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @@ -32,6 +34,7 @@ public class RetentionConfidenceCategoryMapperEntity extends MandatoryCreatedMod private RetentionConfidenceScoreEnum confidenceScore; @Column(name = "ret_conf_reason") + @Enumerated(EnumType.STRING) private RetentionConfidenceReasonEnum confidenceReason; @Column(name = "confidence_category") From db04e359c1662024f0d828e8f5a8f79c1edf5c45 Mon Sep 17 00:00:00 2001 From: chris-bellingham Date: Thu, 19 Dec 2024 09:19:26 +0000 Subject: [PATCH 06/10] DMP-3534: Rework test to remove incorrect setup --- .../service/CloseOldCasesProcessorTest.java | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/cases/service/CloseOldCasesProcessorTest.java b/src/integrationTest/java/uk/gov/hmcts/darts/cases/service/CloseOldCasesProcessorTest.java index 43db6b6d06..715d436fa3 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/cases/service/CloseOldCasesProcessorTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/cases/service/CloseOldCasesProcessorTest.java @@ -32,6 +32,7 @@ import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; import java.util.List; +import java.util.Optional; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -127,18 +128,15 @@ void closeCases_shouldCloseCases_andUseDateAsClosedDate_andSetNullConfidenceReas // And HearingEntity hearing = dartsDatabase.createHearing("a_courthouse", "1", "1078", LocalDateTime.now().minusYears(7).plusMonths(3)); - OffsetDateTime closeDate = OffsetDateTime.now().minusYears(7); + OffsetDateTime expectedCloseDate = OffsetDateTime.now().minusYears(7); - EventEntity eventEntity1 = dartsDatabase.getEventStub().createEvent(hearing, 8);//Re-examination - eventEntity1.setCreatedDateTime(OffsetDateTime.now().minusYears(7).plusDays(10)); - EventEntity eventEntity2 = dartsDatabase.getEventStub().createEvent(hearing, 214);//case closed - eventEntity2.setCreatedDateTime(closeDate); - EventEntity eventEntity3 = dartsDatabase.getEventStub().createEvent(hearing, 23);//Application: No case to answer - eventEntity3.setCreatedDateTime(OffsetDateTime.now().minusYears(7).plusDays(5)); - dartsDatabase.saveAll(eventEntity1, eventEntity2, eventEntity3); + Integer someEventHandlerId = dartsPersistence.getEventHandlerRepository().findAll().getFirst().getId(); + EventEntity eventEntity = dartsDatabase.getEventStub().createEvent(hearing, someEventHandlerId); + eventEntity.setCreatedDateTime(expectedCloseDate); + dartsDatabase.save(eventEntity); CourtCaseEntity courtCaseEntity = hearing.getCourtCase(); - courtCaseEntity.setCreatedDateTime(OffsetDateTime.now().minusYears(10)); + courtCaseEntity.setCreatedDateTime(expectedCloseDate.minusYears(3)); dartsDatabase.getCaseRepository().save(courtCaseEntity); assertFalse(courtCaseEntity.getClosed()); @@ -146,17 +144,21 @@ void closeCases_shouldCloseCases_andUseDateAsClosedDate_andSetNullConfidenceReas closeOldCasesProcessor.closeCases(BATCH_SIZE); // Then - CourtCaseEntity updatedCourtCaseEntity = dartsDatabase.getCaseRepository().findById(courtCaseEntity.getId()).orElse(null); - assert updatedCourtCaseEntity != null; + Optional updatedCourtCaseEntityOptional = dartsDatabase.getCaseRepository() + .findById(courtCaseEntity.getId()); + + assertTrue(updatedCourtCaseEntityOptional.isPresent()); + CourtCaseEntity updatedCourtCaseEntity = updatedCourtCaseEntityOptional.get(); + assertTrue(updatedCourtCaseEntity.getClosed()); - assertEquals(closeDate.truncatedTo(ChronoUnit.MINUTES), + assertEquals(expectedCloseDate.truncatedTo(ChronoUnit.MINUTES), updatedCourtCaseEntity.getCaseClosedTimestamp().truncatedTo(ChronoUnit.MINUTES)); assertNull(updatedCourtCaseEntity.getRetConfScore()); assertNull(updatedCourtCaseEntity.getRetConfReason()); assertEquals(CURRENT_DATE_TIME, updatedCourtCaseEntity.getRetConfUpdatedTs()); - CaseRetentionEntity caseRetentionEntity = dartsDatabase.getCaseRetentionRepository().findAll().get(0); - assertEquals(courtCaseEntity.getId(), caseRetentionEntity.getCourtCase().getId()); - assertEquals(closeDate.plusYears(7).truncatedTo(ChronoUnit.DAYS), caseRetentionEntity.getRetainUntil()); + + CaseRetentionEntity caseRetentionEntity = dartsDatabase.getCaseRetentionRepository() + .findAll().getFirst(); assertEquals(RetentionConfidenceCategoryEnum.AGED_CASE, caseRetentionEntity.getConfidenceCategory()); } From 61c35540e31efe293a629939099e4ef3e2dbc7c6 Mon Sep 17 00:00:00 2001 From: chris-bellingham Date: Thu, 19 Dec 2024 10:32:21 +0000 Subject: [PATCH 07/10] DMP-3534: Fix integration test assertion --- .../RetentionConfidenceCategoryMapperRepositoryTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepositoryTest.java b/src/integrationTest/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepositoryTest.java index 0ca1d930e4..b545ff11f9 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepositoryTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/common/repository/RetentionConfidenceCategoryMapperRepositoryTest.java @@ -108,7 +108,7 @@ void shouldThrowException_whenMultipleMatchesExist() { // When IncorrectResultSizeDataAccessException exception = assertThrows(IncorrectResultSizeDataAccessException.class, () -> repository.findByConfidenceCategory(category)); - assertEquals("Incorrect result size: expected 1, actual 2", exception.getMessage()); + assertEquals("Query did not return a unique result: 2 results were returned", exception.getMessage()); } @Test From 99b2a71fa5f4a45ea11c1faba1b913c837a3b3fa Mon Sep 17 00:00:00 2001 From: chris-bellingham Date: Thu, 19 Dec 2024 10:46:25 +0000 Subject: [PATCH 08/10] DMP-3534: Add flyway data migration for ret_conf_score across all impacted tables --- .../V1_423__update_ret_conf_score_values.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/main/resources/db/migration/common/V1_423__update_ret_conf_score_values.sql diff --git a/src/main/resources/db/migration/common/V1_423__update_ret_conf_score_values.sql b/src/main/resources/db/migration/common/V1_423__update_ret_conf_score_values.sql new file mode 100644 index 0000000000..b51bd3f362 --- /dev/null +++ b/src/main/resources/db/migration/common/V1_423__update_ret_conf_score_values.sql @@ -0,0 +1,17 @@ +UPDATE annotation_document SET ret_conf_score=0 WHERE ret_conf_score=1; +UPDATE annotation_document SET ret_conf_score=1 WHERE ret_conf_score=2; + +UPDATE case_document SET ret_conf_score=0 WHERE ret_conf_score=1; +UPDATE case_document SET ret_conf_score=1 WHERE ret_conf_score=2; + +UPDATE court_case SET ret_conf_score=0 WHERE ret_conf_score=1; +UPDATE court_case SET ret_conf_score=1 WHERE ret_conf_score=2; + +UPDATE media SET ret_conf_score=0 WHERE ret_conf_score=1; +UPDATE media SET ret_conf_score=1 WHERE ret_conf_score=2; + +UPDATE retention_confidence_category_mapper SET ret_conf_score=0 WHERE ret_conf_score=1; +UPDATE retention_confidence_category_mapper SET ret_conf_score=1 WHERE ret_conf_score=2; + +UPDATE transcription_document SET ret_conf_score=0 WHERE ret_conf_score=1; +UPDATE transcription_document SET ret_conf_score=1 WHERE ret_conf_score=2; From b5f127b57a3790156dc86305e2f3e348f785d757 Mon Sep 17 00:00:00 2001 From: chris-bellingham Date: Fri, 20 Dec 2024 08:28:40 +0000 Subject: [PATCH 09/10] DMP-3534: Adjust flyway numbering --- ..._score_values.sql => V1_425__update_ret_conf_score_values.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/resources/db/migration/common/{V1_423__update_ret_conf_score_values.sql => V1_425__update_ret_conf_score_values.sql} (100%) diff --git a/src/main/resources/db/migration/common/V1_423__update_ret_conf_score_values.sql b/src/main/resources/db/migration/common/V1_425__update_ret_conf_score_values.sql similarity index 100% rename from src/main/resources/db/migration/common/V1_423__update_ret_conf_score_values.sql rename to src/main/resources/db/migration/common/V1_425__update_ret_conf_score_values.sql From a70eb3ae67de112c4bb24a0d9a58809ffca7d0b5 Mon Sep 17 00:00:00 2001 From: chris-bellingham Date: Fri, 20 Dec 2024 09:58:41 +0000 Subject: [PATCH 10/10] DMP-3534: Remove flyway data migration --- .../V1_425__update_ret_conf_score_values.sql | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 src/main/resources/db/migration/common/V1_425__update_ret_conf_score_values.sql diff --git a/src/main/resources/db/migration/common/V1_425__update_ret_conf_score_values.sql b/src/main/resources/db/migration/common/V1_425__update_ret_conf_score_values.sql deleted file mode 100644 index b51bd3f362..0000000000 --- a/src/main/resources/db/migration/common/V1_425__update_ret_conf_score_values.sql +++ /dev/null @@ -1,17 +0,0 @@ -UPDATE annotation_document SET ret_conf_score=0 WHERE ret_conf_score=1; -UPDATE annotation_document SET ret_conf_score=1 WHERE ret_conf_score=2; - -UPDATE case_document SET ret_conf_score=0 WHERE ret_conf_score=1; -UPDATE case_document SET ret_conf_score=1 WHERE ret_conf_score=2; - -UPDATE court_case SET ret_conf_score=0 WHERE ret_conf_score=1; -UPDATE court_case SET ret_conf_score=1 WHERE ret_conf_score=2; - -UPDATE media SET ret_conf_score=0 WHERE ret_conf_score=1; -UPDATE media SET ret_conf_score=1 WHERE ret_conf_score=2; - -UPDATE retention_confidence_category_mapper SET ret_conf_score=0 WHERE ret_conf_score=1; -UPDATE retention_confidence_category_mapper SET ret_conf_score=1 WHERE ret_conf_score=2; - -UPDATE transcription_document SET ret_conf_score=0 WHERE ret_conf_score=1; -UPDATE transcription_document SET ret_conf_score=1 WHERE ret_conf_score=2;