generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a770d07
commit 7eaab33
Showing
16 changed files
with
533 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = "[email protected]"; | ||
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.