|
| 1 | +package uk.gov.hmcts.darts.task.runner.impl; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 6 | +import uk.gov.hmcts.darts.arm.client.ArmApiClient; |
| 7 | +import uk.gov.hmcts.darts.arm.client.ArmTokenClient; |
| 8 | +import uk.gov.hmcts.darts.arm.client.model.ArmTokenRequest; |
| 9 | +import uk.gov.hmcts.darts.arm.client.model.ArmTokenResponse; |
| 10 | +import uk.gov.hmcts.darts.arm.client.model.AvailableEntitlementProfile; |
| 11 | +import uk.gov.hmcts.darts.arm.client.model.UpdateMetadataRequest; |
| 12 | +import uk.gov.hmcts.darts.arm.client.model.UpdateMetadataResponse; |
| 13 | +import uk.gov.hmcts.darts.arm.config.ArmApiConfigurationProperties; |
| 14 | +import uk.gov.hmcts.darts.common.entity.ExternalObjectDirectoryEntity; |
| 15 | +import uk.gov.hmcts.darts.common.entity.HearingEntity; |
| 16 | +import uk.gov.hmcts.darts.common.entity.MediaEntity; |
| 17 | +import uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum; |
| 18 | +import uk.gov.hmcts.darts.test.common.data.PersistableFactory; |
| 19 | +import uk.gov.hmcts.darts.testutils.PostgresIntegrationBase; |
| 20 | +import uk.gov.hmcts.darts.testutils.stubs.AuthorisationStub; |
| 21 | + |
| 22 | +import java.time.OffsetDateTime; |
| 23 | +import java.time.ZoneOffset; |
| 24 | +import java.time.format.DateTimeFormatter; |
| 25 | +import java.util.List; |
| 26 | +import java.util.UUID; |
| 27 | + |
| 28 | +import static java.time.temporal.ChronoUnit.MILLIS; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 32 | +import static org.mockito.ArgumentMatchers.any; |
| 33 | +import static org.mockito.Mockito.doAnswer; |
| 34 | +import static org.mockito.Mockito.verify; |
| 35 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 36 | +import static org.mockito.Mockito.when; |
| 37 | +import static uk.gov.hmcts.darts.common.enums.ExternalLocationTypeEnum.ARM; |
| 38 | +import static uk.gov.hmcts.darts.common.enums.ObjectRecordStatusEnum.STORED; |
| 39 | +import static uk.gov.hmcts.darts.retention.enums.RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED; |
| 40 | +import static uk.gov.hmcts.darts.test.common.data.PersistableFactory.getMediaTestData; |
| 41 | + |
| 42 | +class ArmRetentionEventDateCalculatorAutomatedTaskIntTest extends PostgresIntegrationBase { |
| 43 | + private static final String BEARER_TOKEN = "bearer"; |
| 44 | + |
| 45 | + private static final OffsetDateTime DOCUMENT_RETENTION_DATE_TIME = |
| 46 | + OffsetDateTime.of(2023, 6, 10, 10, 50, 0, 0, ZoneOffset.UTC); |
| 47 | + private static final OffsetDateTime RETENTION_DATE_TIME = |
| 48 | + OffsetDateTime.of(1923, 6, 10, 10, 50, 0, 0, ZoneOffset.UTC); |
| 49 | + private static final OffsetDateTime START_TIME = |
| 50 | + OffsetDateTime.of(2023, 6, 10, 10, 0, 0, 0, ZoneOffset.UTC); |
| 51 | + private static final OffsetDateTime END_TIME = |
| 52 | + OffsetDateTime.of(2023, 6, 10, 10, 45, 0, 0, ZoneOffset.UTC); |
| 53 | + private static final int EVENT_DATE_ADJUSTMENT_YEARS = 100; |
| 54 | + |
| 55 | + @Autowired |
| 56 | + private AuthorisationStub authorisationStub; |
| 57 | + @Autowired |
| 58 | + private ArmApiConfigurationProperties armApiConfigurationProperties; |
| 59 | + |
| 60 | + @MockitoBean |
| 61 | + private ArmApiClient armApiClient; |
| 62 | + @MockitoBean |
| 63 | + private ArmTokenClient armTokenClient; |
| 64 | + |
| 65 | + //@Autowired |
| 66 | + //private ArmRetentionEventDateCalculator armRetentionEventDateCalculator; |
| 67 | + |
| 68 | + @Autowired |
| 69 | + private ArmRetentionEventDateCalculatorAutomatedTask armRetentionEventDateCalculatorAutomatedTask; |
| 70 | + |
| 71 | + @Test |
| 72 | + void runTask_ShouldUpdateRetentionEventDate() { |
| 73 | + // given |
| 74 | + final String confidenceReason = "reason"; |
| 75 | + final RetentionConfidenceScoreEnum confidenceScore = CASE_PERFECTLY_CLOSED; |
| 76 | + final String externalRecordId = "recordId"; |
| 77 | + |
| 78 | + HearingEntity hearing = PersistableFactory.getHearingTestData().someMinimalHearing(); |
| 79 | + |
| 80 | + MediaEntity savedMedia = dartsPersistence.save( |
| 81 | + getMediaTestData().createMediaWith( |
| 82 | + hearing.getCourtroom(), |
| 83 | + START_TIME, |
| 84 | + END_TIME, |
| 85 | + 1, |
| 86 | + "mp2", |
| 87 | + confidenceScore, |
| 88 | + confidenceReason |
| 89 | + )); |
| 90 | + savedMedia.setRetainUntilTs(DOCUMENT_RETENTION_DATE_TIME); |
| 91 | + dartsPersistence.save(savedMedia); |
| 92 | + |
| 93 | + ExternalObjectDirectoryEntity armEod = PersistableFactory.getExternalObjectDirectoryTestData() |
| 94 | + .someMinimalBuilder().media(savedMedia) |
| 95 | + .status(dartsDatabase.getExternalObjectDirectoryStub().getStatus(STORED)) |
| 96 | + .externalLocationType(dartsDatabase.getExternalObjectDirectoryStub().getLocation(ARM)) |
| 97 | + .externalLocation(UUID.randomUUID().toString()).build().getEntity(); |
| 98 | + |
| 99 | + armEod.setExternalRecordId(externalRecordId); |
| 100 | + armEod.setEventDateTs(END_TIME); |
| 101 | + armEod.setUpdateRetention(true); |
| 102 | + dartsPersistence.save(armEod); |
| 103 | + |
| 104 | + UpdateMetadataResponse response = UpdateMetadataResponse.builder().responseStatus(200).isError(false).build(); |
| 105 | + ArmTokenRequest armTokenRequest = createTokenRequest(); |
| 106 | + when(armApiClient.updateMetadata(any(), any())).thenReturn(response); |
| 107 | + |
| 108 | + ArmTokenResponse armTokenResponse = createArmTokenResponse(); |
| 109 | + when(armTokenClient.getToken(armTokenRequest)).thenReturn(armTokenResponse); |
| 110 | + |
| 111 | + AvailableEntitlementProfile availableEntitlementProfile = createAvailableEntitlementProfile(); |
| 112 | + when(armTokenClient.availableEntitlementProfiles(any(), any())).thenReturn(availableEntitlementProfile); |
| 113 | + |
| 114 | + when(armTokenClient.selectEntitlementProfile(any(), any(), any())).thenReturn(armTokenResponse); |
| 115 | + |
| 116 | + armRetentionEventDateCalculatorAutomatedTask.preRunTask(); |
| 117 | + |
| 118 | + // when |
| 119 | + armRetentionEventDateCalculatorAutomatedTask.runTask(); |
| 120 | + |
| 121 | + // then |
| 122 | + var persistedEod = dartsDatabase.getExternalObjectDirectoryRepository().findById(armEod.getId()).orElseThrow(); |
| 123 | + assertFalse(persistedEod.isUpdateRetention()); |
| 124 | + assertEquals(0, persistedEod.getEventDateTs().truncatedTo(MILLIS).compareTo(RETENTION_DATE_TIME.truncatedTo(MILLIS))); |
| 125 | + |
| 126 | + UpdateMetadataRequest expectedMetadataRequest = UpdateMetadataRequest.builder() |
| 127 | + .itemId(externalRecordId) |
| 128 | + .manifest(UpdateMetadataRequest.Manifest.builder() |
| 129 | + .eventDate(formatDateTime(savedMedia.getRetainUntilTs().minusYears(EVENT_DATE_ADJUSTMENT_YEARS))) |
| 130 | + .retConfReason(confidenceReason) |
| 131 | + .retConfScore(confidenceScore.getId()) |
| 132 | + .build()) |
| 133 | + .useGuidsForFields(false) |
| 134 | + .build(); |
| 135 | + verify(armApiClient).updateMetadata("Bearer " + BEARER_TOKEN, expectedMetadataRequest); |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void runTask_ShouldNotUpdateRetentionEventDate_WhenInterruptedExceptionIsThrown() { |
| 141 | + // given |
| 142 | + final String confidenceReason = "reason"; |
| 143 | + final RetentionConfidenceScoreEnum confidenceScore = CASE_PERFECTLY_CLOSED; |
| 144 | + final String externalRecordId = "recordId"; |
| 145 | + |
| 146 | + HearingEntity hearing = PersistableFactory.getHearingTestData().someMinimalHearing(); |
| 147 | + |
| 148 | + MediaEntity savedMedia = dartsPersistence.save( |
| 149 | + getMediaTestData().createMediaWith( |
| 150 | + hearing.getCourtroom(), |
| 151 | + START_TIME, |
| 152 | + END_TIME, |
| 153 | + 1, |
| 154 | + "mp2", |
| 155 | + confidenceScore, |
| 156 | + confidenceReason |
| 157 | + )); |
| 158 | + savedMedia.setRetainUntilTs(DOCUMENT_RETENTION_DATE_TIME); |
| 159 | + dartsPersistence.save(savedMedia); |
| 160 | + |
| 161 | + ExternalObjectDirectoryEntity armEod = PersistableFactory.getExternalObjectDirectoryTestData() |
| 162 | + .someMinimalBuilder().media(savedMedia) |
| 163 | + .status(dartsDatabase.getExternalObjectDirectoryStub().getStatus(STORED)) |
| 164 | + .externalLocationType(dartsDatabase.getExternalObjectDirectoryStub().getLocation(ARM)) |
| 165 | + .externalLocation(UUID.randomUUID().toString()).build().getEntity(); |
| 166 | + |
| 167 | + armEod.setExternalRecordId(externalRecordId); |
| 168 | + armEod.setEventDateTs(END_TIME); |
| 169 | + armEod.setUpdateRetention(true); |
| 170 | + dartsPersistence.save(armEod); |
| 171 | + |
| 172 | + ArmTokenResponse armTokenResponse = createArmTokenResponse(); |
| 173 | + when(armTokenClient.getToken(any())).thenReturn(armTokenResponse); |
| 174 | + // Simulate InterruptedException |
| 175 | + doAnswer(invocation -> { |
| 176 | + throw new InterruptedException("Simulated interruption"); |
| 177 | + }).when(armTokenClient).getToken(any()); |
| 178 | + |
| 179 | + armRetentionEventDateCalculatorAutomatedTask.preRunTask(); |
| 180 | + |
| 181 | + // when |
| 182 | + armRetentionEventDateCalculatorAutomatedTask.runTask(); |
| 183 | + |
| 184 | + // then |
| 185 | + var persistedEod = dartsDatabase.getExternalObjectDirectoryRepository().findById(armEod.getId()).orElseThrow(); |
| 186 | + assertTrue(persistedEod.isUpdateRetention()); |
| 187 | + // Verify that the updateMetadata method was not called |
| 188 | + verifyNoInteractions(armApiClient); |
| 189 | + } |
| 190 | + |
| 191 | + private AvailableEntitlementProfile createAvailableEntitlementProfile() { |
| 192 | + List<AvailableEntitlementProfile.Profiles> profiles = List.of(AvailableEntitlementProfile.Profiles.builder() |
| 193 | + .profileName("some-profile-name") |
| 194 | + .profileId("some-profile-id") |
| 195 | + .build()); |
| 196 | + |
| 197 | + return AvailableEntitlementProfile.builder() |
| 198 | + .profiles(profiles) |
| 199 | + .isError(false) |
| 200 | + .build(); |
| 201 | + } |
| 202 | + |
| 203 | + private String formatDateTime(OffsetDateTime offsetDateTime) { |
| 204 | + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"); |
| 205 | + return offsetDateTime.format(dateTimeFormatter); |
| 206 | + } |
| 207 | + |
| 208 | + private static ArmTokenRequest createTokenRequest() { |
| 209 | + return ArmTokenRequest.builder() |
| 210 | + .username("some-username") |
| 211 | + .password("some-password") |
| 212 | + .build(); |
| 213 | + } |
| 214 | + |
| 215 | + private static ArmTokenResponse createArmTokenResponse() { |
| 216 | + return ArmTokenResponse.builder() |
| 217 | + .accessToken(BEARER_TOKEN) |
| 218 | + .tokenType("Bearer") |
| 219 | + .expiresIn("3600") |
| 220 | + .build(); |
| 221 | + } |
| 222 | +} |
0 commit comments