Skip to content

Commit

Permalink
DMP-4571 Reading of IU File failure (#2433)
Browse files Browse the repository at this point in the history
  • Loading branch information
karen-hedges authored Jan 13, 2025
1 parent 1050849 commit d5432b9
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 53 deletions.
4 changes: 2 additions & 2 deletions bin/generateBatchArmResponses.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ addNewResponses () {
exit 1
fi

read -p "enter IU timestamp or return to use default - 2024-01-11T12:46:21.215310: " iuTimestamp
read -p "enter IU timestamp or return to use default - 2024-06-10T14:08:28.316382+00:00 (yyyy-MM-dd'T'HH:mm:ss.SSSSSS[XXXX][XXXXX]): " iuTimestamp
if [ -z "$iuTimestamp" ]
then
iuTimestamp="2024-01-11T12:46:21.215310"
iuTimestamp="2024-06-10T14:08:28.316382+00:00"
fi
#${parameter//pattern/string}
uiFileContentsParam="{\"operation\": \"input_upload\", \"timestamp\": \"IU_TIMESTAMP\", \"status\": 1, \"exception_description\": null, \"error_status\": null, \"filename\": \"CGITestFilesMalformedManifest_1\", \"submission_folder\": \"/dropzone/A360/submission\", \"file_hash\": \"fbfec54925d62146aeced724ff9f3c8e\"}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.azure.core.exception.AzureException;
import com.azure.core.util.BinaryData;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -38,6 +39,7 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -64,15 +66,28 @@
import static uk.gov.hmcts.darts.common.enums.ObjectRecordStatusEnum.ARM_RPO_PENDING;
import static uk.gov.hmcts.darts.test.common.TestUtils.getContentsFromFile;

@Slf4j
@SuppressWarnings({"VariableDeclarationUsageDistance", "PMD.NcssCount", "PMD.ExcessiveImports"})
abstract class AbstractArmBatchProcessResponseFilesIntTest extends IntegrationBase {
private static final LocalDateTime HEARING_DATETIME = LocalDateTime.of(2023, 6, 10, 10, 0, 0);
private static final String INBOUND_UPLOAD_FILE_TIMESTAMP_STR = "2031-08-10T10:00:00.000Z";
private static final OffsetDateTime INBOUND_UPLOAD_FILE_TIMESTAMP = OffsetDateTime.parse(INBOUND_UPLOAD_FILE_TIMESTAMP_STR);
public static final String HASHCODE_2 = "7a374f19a9ce7dc9cc480ea8d4eca0fc";
public static final String T_13_00_00_Z = "2023-06-10T13:00:00Z";
public static final String T_13_45_00_Z = "2023-06-10T13:45:00Z";

private static final String DATETIMEKEY = "<datetimekey>";
private static final String INPUT_UPLOAD_RESPONSE_DATETIME = "2023-06-10T14:08:28.316382+00:00";
private static final String HASHCODE_2 = "7a374f19a9ce7dc9cc480ea8d4eca0fc";
private static final String INPUT_UPLOAD_RESPONSE = """
{
"operation": "input_upload",
"timestamp": "<datetimekey>",
"status": 1,
"exception_description": null,
"error_status": null,
"filename": "DARTS_fa292f18-55e7-4d58-b610-0435a37900a2",
"submission_folder": "/dropzone/DARTS/submission",
"file_hash": "a11f992a43ea6d0b192d57fe44403942"
}
""";

protected static final String T_13_00_00_Z = "2023-06-10T13:00:00Z";
protected static final String T_13_45_00_Z = "2023-06-10T13:45:00Z";

@Autowired
protected ExternalObjectDirectoryRepository externalObjectDirectoryRepository;
Expand Down Expand Up @@ -103,7 +118,7 @@ abstract class AbstractArmBatchProcessResponseFilesIntTest extends IntegrationBa
protected String continuationToken;

protected static final Integer BATCH_SIZE = 10;

protected OffsetDateTime inputUploadProcessedTimestamp;

@BeforeEach
void commonSetup() {
Expand All @@ -112,8 +127,13 @@ void commonSetup() {
when(userIdentity.getUserAccount()).thenReturn(testUser);
lenient().when(armDataManagementConfiguration.getMaxContinuationBatchSize()).thenReturn(10);
lenient().when(armDataManagementConfiguration.getArmMissingResponseDuration()).thenReturn(Duration.ofHours(24));
String dateTimeFormatStr = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS[XXXX][XXXXX]";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormatStr);
inputUploadProcessedTimestamp = OffsetDateTime.parse(INPUT_UPLOAD_RESPONSE_DATETIME, formatter);
lenient().when(armDataManagementConfiguration.getInputUploadResponseTimestampFormat()).thenReturn(dateTimeFormatStr);

BinaryData inputUploadFileRecord = convertStringToBinaryData("{\"timestamp\": \"" + INBOUND_UPLOAD_FILE_TIMESTAMP + "\"}");
String inputUploadResponse = INPUT_UPLOAD_RESPONSE.replace(DATETIMEKEY, INPUT_UPLOAD_RESPONSE_DATETIME);
BinaryData inputUploadFileRecord = convertStringToBinaryData(inputUploadResponse);

when(armDataManagementApi.getBlobData(Mockito.startsWith("dropzone/DARTS/response/" + prefix() + "_"))).thenReturn(inputUploadFileRecord);
}
Expand Down Expand Up @@ -305,7 +325,8 @@ void batchProcessResponseFiles_WithMediaReturnsSuccess() throws IOException {

assertEquals(1, foundMediaList.size());
ExternalObjectDirectoryEntity foundMedia = foundMediaList.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia.getInputUploadProcessedTs());

assertEquals(inputUploadProcessedTimestamp, foundMedia.getInputUploadProcessedTs());

assertEquals(ARM_RPO_PENDING.getId(), foundMedia.getStatus().getId());
assertEquals(1, foundMedia.getVerificationAttempts());
Expand All @@ -329,7 +350,7 @@ void batchProcessResponseFiles_WithMediaReturnsSuccess() throws IOException {

assertEquals(1, foundMediaList3.size());
ExternalObjectDirectoryEntity foundMedia3 = foundMediaList3.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia3.getInputUploadProcessedTs());
assertEquals("2023-06-10T14:08:28.316382Z", foundMedia3.getInputUploadProcessedTs().toString());
assertEquals(ARM_RESPONSE_MANIFEST_FAILED.getId(), foundMedia3.getStatus().getId());
assertEquals(2, foundMedia3.getVerificationAttempts());
assertEquals(1, foundMedia3.getTransferAttempts());
Expand All @@ -340,7 +361,7 @@ void batchProcessResponseFiles_WithMediaReturnsSuccess() throws IOException {

assertEquals(1, foundMediaList4.size());
ExternalObjectDirectoryEntity foundMedia4 = foundMediaList4.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia4.getInputUploadProcessedTs());
assertEquals("2023-06-10T14:08:28.316382Z", foundMedia4.getInputUploadProcessedTs().toString());
assertEquals(ARM_RESPONSE_MANIFEST_FAILED.getId(), foundMedia4.getStatus().getId());
assertEquals(2, foundMedia4.getVerificationAttempts());
assertEquals(1, foundMedia4.getTransferAttempts());
Expand All @@ -355,7 +376,7 @@ void batchProcessResponseFiles_WithMediaReturnsSuccess() throws IOException {

assertEquals(1, foundMediaList5.size());
ExternalObjectDirectoryEntity foundMedia5 = foundMediaList5.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia5.getInputUploadProcessedTs());
assertEquals("2023-06-10T14:08:28.316382Z", foundMedia5.getInputUploadProcessedTs().toString());
assertEquals(ARM_DROP_ZONE.getId(), foundMedia5.getStatus().getId());
assertEquals(1, foundMedia5.getVerificationAttempts());
assertEquals(1, foundMedia5.getTransferAttempts());
Expand Down Expand Up @@ -460,7 +481,7 @@ void batchProcessResponseFiles_WithInvalidLineFileAndCreateRecordFileSuccess() t

assertEquals(1, externalObjectDirectoryEntities.size());
ExternalObjectDirectoryEntity foundEod = externalObjectDirectoryEntities.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundEod.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundEod.getInputUploadProcessedTs());
assertEquals(ARM_RESPONSE_MANIFEST_FAILED.getId(), foundEod.getStatus().getId());
assertEquals(2, foundEod.getVerificationAttempts());
assertEquals("Operation: create_record - PS.20023:INVALID_PARAMETERS:Invalid line: invalid json; ", foundEod.getErrorCode());
Expand Down Expand Up @@ -648,7 +669,7 @@ void batchProcessResponseFiles_With3InvalidLineFilesCausingFailure() throws IOEx

assertEquals(1, foundMediaList.size());
ExternalObjectDirectoryEntity foundMedia = foundMediaList.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundMedia.getInputUploadProcessedTs());
assertEquals(ARM_RESPONSE_PROCESSING_FAILED.getId(), foundMedia.getStatus().getId());
assertEquals(1, foundMedia.getVerificationAttempts());

Expand Down Expand Up @@ -804,7 +825,7 @@ void batchProcessResponseFiles_WithMediaUsingSmallContinuationTokenReturnsSucces

assertEquals(1, foundMediaList.size());
ExternalObjectDirectoryEntity foundMedia = foundMediaList.get(0);
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundMedia.getInputUploadProcessedTs());
assertEquals(ARM_RPO_PENDING.getId(), foundMedia.getStatus().getId());
assertEquals(1, foundMedia.getVerificationAttempts());
assertNotNull(foundMedia.getDataIngestionTs());
Expand All @@ -815,7 +836,7 @@ void batchProcessResponseFiles_WithMediaUsingSmallContinuationTokenReturnsSucces

assertEquals(1, foundMediaList2.size());
ExternalObjectDirectoryEntity foundMedia2 = foundMediaList2.get(0);
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia2.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundMedia2.getInputUploadProcessedTs());
assertEquals(ARM_RESPONSE_MANIFEST_FAILED.getId(), foundMedia2.getStatus().getId());
assertEquals(2, foundMedia2.getVerificationAttempts());
assertEquals(1, foundMedia2.getTransferAttempts());
Expand All @@ -826,7 +847,7 @@ void batchProcessResponseFiles_WithMediaUsingSmallContinuationTokenReturnsSucces

assertEquals(1, foundMediaList3.size());
ExternalObjectDirectoryEntity foundMedia3 = foundMediaList3.get(0);
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia3.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundMedia3.getInputUploadProcessedTs());
assertEquals(ARM_RESPONSE_MANIFEST_FAILED.getId(), foundMedia3.getStatus().getId());
assertEquals(2, foundMedia3.getVerificationAttempts());
assertEquals(1, foundMedia3.getTransferAttempts());
Expand All @@ -837,7 +858,7 @@ void batchProcessResponseFiles_WithMediaUsingSmallContinuationTokenReturnsSucces

assertEquals(1, foundMediaList5.size());
ExternalObjectDirectoryEntity foundMedia5 = foundMediaList5.get(0);
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia5.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundMedia5.getInputUploadProcessedTs());
assertEquals(ARM_DROP_ZONE.getId(), foundMedia5.getStatus().getId());
assertEquals(1, foundMedia5.getVerificationAttempts());
assertFalse(foundMedia5.isResponseCleaned());
Expand Down Expand Up @@ -1005,7 +1026,7 @@ void batchProcessResponseFiles_GetBlobsThrowsException() throws IOException {

assertEquals(1, foundMediaList.size());
ExternalObjectDirectoryEntity foundMedia = foundMediaList.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundMedia.getInputUploadProcessedTs());
assertEquals(ARM_DROP_ZONE.getId(), foundMedia.getStatus().getId());
assertEquals(1, foundMedia.getVerificationAttempts());
assertFalse(foundMedia.isResponseCleaned());
Expand All @@ -1015,7 +1036,7 @@ void batchProcessResponseFiles_GetBlobsThrowsException() throws IOException {

assertEquals(1, foundMediaList2.size());
ExternalObjectDirectoryEntity foundMedia2 = foundMediaList2.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia2.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundMedia2.getInputUploadProcessedTs());
assertEquals(ARM_DROP_ZONE.getId(), foundMedia2.getStatus().getId());
assertEquals(1, foundMedia2.getVerificationAttempts());
assertFalse(foundMedia2.isResponseCleaned());
Expand All @@ -1026,7 +1047,7 @@ void batchProcessResponseFiles_GetBlobsThrowsException() throws IOException {

assertEquals(1, foundMediaList3.size());
ExternalObjectDirectoryEntity foundMedia3 = foundMediaList3.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia3.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundMedia3.getInputUploadProcessedTs());
assertEquals(ARM_DROP_ZONE.getId(), foundMedia3.getStatus().getId());
assertEquals(1, foundMedia3.getVerificationAttempts());
assertFalse(foundMedia3.isResponseCleaned());
Expand All @@ -1036,7 +1057,7 @@ void batchProcessResponseFiles_GetBlobsThrowsException() throws IOException {

assertEquals(1, foundMediaList4.size());
ExternalObjectDirectoryEntity foundMedia4 = foundMediaList4.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia4.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundMedia4.getInputUploadProcessedTs());
assertEquals(ARM_DROP_ZONE.getId(), foundMedia4.getStatus().getId());
assertEquals(1, foundMedia4.getVerificationAttempts());
assertFalse(foundMedia4.isResponseCleaned());
Expand All @@ -1046,7 +1067,7 @@ void batchProcessResponseFiles_GetBlobsThrowsException() throws IOException {

assertEquals(1, foundMediaList5.size());
ExternalObjectDirectoryEntity foundMedia5 = foundMediaList5.getFirst();
assertEquals(INBOUND_UPLOAD_FILE_TIMESTAMP, foundMedia5.getInputUploadProcessedTs());
assertEquals(inputUploadProcessedTimestamp, foundMedia5.getInputUploadProcessedTs());
assertEquals(ARM_DROP_ZONE.getId(), foundMedia5.getStatus().getId());
assertEquals(1, foundMedia5.getVerificationAttempts());
assertFalse(foundMedia5.isResponseCleaned());
Expand Down Expand Up @@ -2395,10 +2416,7 @@ void batchProcessResponseFiles_updateEodWithArmMissingResponse_WhenNoResponseFil
OffsetDateTime endTime = OffsetDateTime.parse(T_13_45_00_Z);
MediaEntity media1 = createMediaEntity(hearing, startTime, endTime, 1);
when(currentTimeHelper.currentOffsetDateTime()).thenReturn(OffsetDateTime.now());
OffsetDateTime inputUploadFileTimestamp = OffsetDateTime.parse("2024-12-19T10:00:00.000Z");
BinaryData inputUploadFileRecord = convertStringToBinaryData("{\"timestamp\": \"" + inputUploadFileTimestamp + "\"}");
when(armDataManagementApi.getBlobData(Mockito.startsWith("dropzone/DARTS/response/" + prefix() + "_"))).thenReturn(inputUploadFileRecord);


String manifest1Uuid = UUID.randomUUID().toString();
String manifestFile1 = prefix() + "_" + manifest1Uuid + ".a360";

Expand Down Expand Up @@ -2439,7 +2457,7 @@ void batchProcessResponseFiles_updateEodWithArmMissingResponse_WhenNoResponseFil

assertEquals(1, externalObjectDirectoryEntities.size());
ExternalObjectDirectoryEntity foundEod = externalObjectDirectoryEntities.getFirst();
assertEquals(inputUploadFileTimestamp, foundEod.getInputUploadProcessedTs());
assertEquals("2023-06-10T14:08:28.316382Z", foundEod.getInputUploadProcessedTs().toString());
assertEquals(ARM_MISSING_RESPONSE.getId(), foundEod.getStatus().getId());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"operation": "input_upload",
"timestamp": "2025-01-10T07:08:28.316382+00:00",
"status": 1,
"exception_description": null,
"error_status": null,
"filename": "DARTS_fa292f18-55e7-4d58-b610-0435a37900a2",
"submission_folder": "/dropzone/DARTS/submission",
"file_hash": "a11f992a43ea6d0b192d57fe44403942"
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class ArmDataManagementConfiguration extends StorageConfiguration {
private Integer eventDateAdjustmentYears;
private Integer maxContinuationBatchSize;
private Duration armMissingResponseDuration;
private String inputUploadResponseTimestampFormat;


@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@NoArgsConstructor
Expand All @@ -16,7 +14,7 @@ public class ArmResponseInputUploadFileRecord {
private String operation;

@JsonProperty("timestamp")
private LocalDateTime timestamp;
private String timestamp;

@JsonProperty("status")
private Integer status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -75,6 +76,8 @@ public abstract class AbstractArmBatchProcessResponseFiles implements ArmRespons
protected final ExternalObjectDirectoryService externalObjectDirectoryService;
protected final LogApi logApi;

protected DateTimeFormatter dateTimeFormatter;

@Override
public void processResponseFiles(int batchSize) {
UserAccountEntity userAccount = userIdentity.getUserAccount();
Expand Down Expand Up @@ -128,12 +131,12 @@ private void processInputUploadBlob(String inputUploadBlob, UserAccountEntity us
log.info("Contents of ARM Input Upload file: '{}' '{}", inputUploadBlob, inputUploadFileRecordStr);
ArmResponseInputUploadFileRecord inputUploadFileRecord = objectMapper.readValue(inputUploadFileRecordStr, ArmResponseInputUploadFileRecord.class);


List<ExternalObjectDirectoryEntity> externalObjectDirectoryEntities = externalObjectDirectoryRepository
.findAllByStatusAndManifestFile(EodHelper.armDropZoneStatus(), manifestName);

if (CollectionUtils.isNotEmpty(externalObjectDirectoryEntities)) {
OffsetDateTime timestamp = inputUploadFileRecord.getTimestamp().atOffset(OffsetDateTime.now().getOffset());
OffsetDateTime timestamp = getInputUploadFileTimestamp(inputUploadFileRecord);

List<ExternalObjectDirectoryEntity> editedExternalObjectDirectoryEntities = externalObjectDirectoryEntities.stream()
.filter(eod -> eod.getInputUploadProcessedTs() == null)
.peek(eod -> eod.setInputUploadProcessedTs(timestamp))
Expand Down Expand Up @@ -162,6 +165,17 @@ private void processInputUploadBlob(String inputUploadBlob, UserAccountEntity us
}
}

OffsetDateTime getInputUploadFileTimestamp(ArmResponseInputUploadFileRecord inputUploadFileRecord) {
try {
dateTimeFormatter = DateTimeFormatter.ofPattern(armDataManagementConfiguration.getInputUploadResponseTimestampFormat());
return OffsetDateTime.parse(inputUploadFileRecord.getTimestamp(), dateTimeFormatter);
} catch (Exception e) {
log.error("Unable to parse timestamp {} from ARM input upload file {}", inputUploadFileRecord.getTimestamp(),
inputUploadFileRecord.getFilename(), e);
throw new IllegalArgumentException(e);
}
}

private void deleteResponseBlobsByManifestName(BatchInputUploadFileFilenameProcessor batchUploadFileFilenameProcessor,
String manifestName) {
List<ExternalObjectDirectoryEntity> externalObjectDirectoryEntities = externalObjectDirectoryRepository.findByManifestFile(manifestName);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ darts:
transcription-record-properties-file: properties/arm/transcription-record.properties
annotation-record-properties-file: properties/arm/annotation-record.properties
case-record-properties-file: properties/arm/case-record.properties
input-upload-response-timestamp-format: yyyy-MM-dd'T'HH:mm:ss.SSSSSS[XXXX][XXXXX]

response-cleanup-buffer-days: 0
batch-response-cleanup:
Expand Down
Loading

0 comments on commit d5432b9

Please sign in to comment.