Skip to content

Commit

Permalink
DMP-3534: Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbellingham-hmcts committed Dec 18, 2024
1 parent e070ab4 commit 0b674db
Show file tree
Hide file tree
Showing 16 changed files with 533 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -245,6 +320,8 @@ void givenNoDataUseCreatedDateAsClosedDate() {

@Test
void givenNoDataUseCreatedDateAsClosedDateUseBatchSizeTwo() {
createAndSaveRetentionConfidenceCategoryMappings();

// given
OffsetDateTime closeDate1 = OffsetDateTime.now().minusYears(9);
CourtCaseEntity courtCaseEntity1 = dartsDatabase.createCase("a_courthouse", "019278");
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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() {
Expand All @@ -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
Expand Down
Loading

0 comments on commit 0b674db

Please sign in to comment.