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 72c1572cd6..033400d39c 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 @@ -83,6 +83,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; @@ -199,6 +200,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; @@ -304,6 +306,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; + } + } + +}