From a070a2e9d1329539a3ddd7ae882470a4dcc4a46a Mon Sep 17 00:00:00 2001 From: karen-hedges <133129444+karen-hedges@users.noreply.github.com> Date: Tue, 3 Dec 2024 21:38:09 +0000 Subject: [PATCH] DMP-4376 Add ARM RPO logs for Dynatrace Added dynatrace logging for ARM RPO Search and Polling --- .../service/impl/ArmRpoPollServiceImpl.java | 64 +++++++++++-------- .../impl/ArmRpoPollServiceImplTest.java | 28 ++++---- 2 files changed, 53 insertions(+), 39 deletions(-) diff --git a/src/main/java/uk/gov/hmcts/darts/arm/service/impl/ArmRpoPollServiceImpl.java b/src/main/java/uk/gov/hmcts/darts/arm/service/impl/ArmRpoPollServiceImpl.java index c3b29f5b474..8f16d6c22b9 100644 --- a/src/main/java/uk/gov/hmcts/darts/arm/service/impl/ArmRpoPollServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/darts/arm/service/impl/ArmRpoPollServiceImpl.java @@ -13,13 +13,14 @@ import uk.gov.hmcts.darts.arm.service.ArmRpoService; import uk.gov.hmcts.darts.authorisation.component.UserIdentity; import uk.gov.hmcts.darts.common.entity.ArmRpoExecutionDetailEntity; +import uk.gov.hmcts.darts.common.entity.UserAccountEntity; import uk.gov.hmcts.darts.common.service.FileOperationService; import uk.gov.hmcts.darts.log.api.LogApi; import java.io.File; +import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import static java.util.Objects.isNull; @@ -38,13 +39,14 @@ public class ArmRpoPollServiceImpl implements ArmRpoPollService { private final ArmDataManagementConfiguration armDataManagementConfiguration; private final LogApi logApi; - private List tempProductionFiles; + private List tempProductionFiles = new ArrayList<>(); - private List allowableFailedStates = null; + private List allowableFailedStates = new ArrayList<>(); @Override public void pollArmRpo(boolean isManualRun) { + log.info("Polling ARM RPO service - isManualRun: {}", isManualRun); setupFailedStatuses(); Integer executionId = null; tempProductionFiles = new ArrayList<>(); @@ -55,6 +57,8 @@ public void pollArmRpo(boolean isManualRun) { logApi.armRpoPollingFailed(executionId); return; } + executionId = armRpoExecutionDetailEntity.getId(); + var bearerToken = armApiService.getArmBearerToken(); if (isNull(bearerToken)) { log.warn("Unable to get bearer token to poll ARM RPO"); @@ -62,7 +66,6 @@ public void pollArmRpo(boolean isManualRun) { return; } - executionId = armRpoExecutionDetailEntity.getId(); var userAccount = userIdentity.getUserAccount(); // step to call ARM RPO API to get the extended searches by matter @@ -83,17 +86,7 @@ public void pollArmRpo(boolean isManualRun) { var productionOutputFiles = armRpoApi.getProductionOutputFiles(bearerToken, executionId, userAccount); for (var productionExportFileId : productionOutputFiles) { - String productionExportFilename = generateTempProductionExportFilename(productionExportFileId); - // step to call ARM RPO API to download the production export file - var inputStream = armRpoApi.downloadProduction(bearerToken, executionId, productionExportFileId, userAccount); - log.info("About to save production export file to temp workspace {}", productionExportFilename); - Path tempProductionFile = fileOperationService.saveFileToTempWorkspace( - inputStream, - productionExportFilename, - armDataManagementConfiguration, - true - ); - tempProductionFiles.add(tempProductionFile.toFile()); + processProductionFiles(productionExportFileId, bearerToken, executionId, userAccount); } if (CollectionUtils.isNotEmpty(tempProductionFiles)) { // step to call ARM RPO API to remove the production @@ -111,17 +104,28 @@ public void pollArmRpo(boolean isManualRun) { log.error("Error while polling ARM RPO", e); logApi.armRpoPollingFailed(executionId); } finally { - try { - cleanUpTempFiles(); - } catch (Exception e) { - log.error("Error while cleaning up ARM RPO polling service temp files", e); - } + cleanUpTempFiles(); } } + private void processProductionFiles(String productionExportFileId, String bearerToken, Integer executionId, + UserAccountEntity userAccount) throws IOException { + String productionExportFilename = generateTempProductionExportFilename(productionExportFileId); + // step to call ARM RPO API to download the production export file + var inputStream = armRpoApi.downloadProduction(bearerToken, executionId, productionExportFileId, userAccount); + log.info("About to save production export file to temp workspace {}", productionExportFilename); + Path tempProductionFile = fileOperationService.saveFileToTempWorkspace( + inputStream, + productionExportFilename, + armDataManagementConfiguration, + true + ); + tempProductionFiles.add(tempProductionFile.toFile()); + } + private void setupFailedStatuses() { if (CollectionUtils.isEmpty(allowableFailedStates)) { - allowableFailedStates = Collections.unmodifiableList(List.of( + allowableFailedStates = List.of( ArmRpoHelper.getExtendedSearchesByMatterRpoState().getId(), ArmRpoHelper.getMasterIndexFieldByRecordClassSchemaSecondaryRpoState().getId(), ArmRpoHelper.createExportBasedOnSearchResultsTableRpoState().getId(), @@ -129,19 +133,23 @@ private void setupFailedStatuses() { ArmRpoHelper.getProductionOutputFilesRpoState().getId(), ArmRpoHelper.downloadProductionRpoState().getId(), ArmRpoHelper.removeProductionRpoState().getId() - )); + ); } } private void cleanUpTempFiles() { - for (var tempProductionFile : tempProductionFiles) { - if (tempProductionFile.exists()) { - if (tempProductionFile.delete()) { - log.info("Deleted temp production file {}", tempProductionFile.getName()); - } else { - log.warn("Failed to delete temp production file {}", tempProductionFile.getName()); + try { + for (var tempProductionFile : tempProductionFiles) { + if (tempProductionFile.exists()) { + if (tempProductionFile.delete()) { + log.info("Deleted temp production file {}", tempProductionFile.getName()); + } else { + log.warn("Failed to delete temp production file {}", tempProductionFile.getName()); + } } } + } catch (Exception e) { + log.error("Error while cleaning up ARM RPO polling service temp files", e); } } diff --git a/src/test/java/uk/gov/hmcts/darts/arm/service/impl/ArmRpoPollServiceImplTest.java b/src/test/java/uk/gov/hmcts/darts/arm/service/impl/ArmRpoPollServiceImplTest.java index f8e6b63cb2d..9fb4cae5548 100644 --- a/src/test/java/uk/gov/hmcts/darts/arm/service/impl/ArmRpoPollServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/darts/arm/service/impl/ArmRpoPollServiceImplTest.java @@ -6,7 +6,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import uk.gov.hmcts.darts.arm.config.ArmDataManagementConfiguration; @@ -26,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; import static org.mockito.ArgumentMatchers.any; @@ -63,16 +63,21 @@ class ArmRpoPollServiceImplTest { @TempDir private File tempDirectory; + private final List tempProductionFiles = new ArrayList<>(); + private final List allowableFailedStates = new ArrayList<>(); + private static final Integer EXECUTION_ID = 1; private ArmRpoExecutionDetailEntity armRpoExecutionDetailEntity; private static final ArmRpoHelperMocks ARM_RPO_HELPER_MOCKS = new ArmRpoHelperMocks(); - @InjectMocks private ArmRpoPollServiceImpl armRpoPollService; @BeforeEach void setUp() { + armRpoPollService = new ArmRpoPollServiceImpl(armRpoApi, armApiService, armRpoService, userIdentity, fileOperationService, + armDataManagementConfiguration, logApi, tempProductionFiles, allowableFailedStates); + lenient().when(userIdentity.getUserAccount()).thenReturn(userAccountEntity); armRpoExecutionDetailEntity = new ArmRpoExecutionDetailEntity(); @@ -81,7 +86,7 @@ void setUp() { } @Test - void pollArmRpo_shouldPollSuccessfullyWithSaveBackgroundCompleted() throws IOException { + void pollArmRpo_shouldPollSuccessfully_whenSaveBackgroundCompleted() throws IOException { // given armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getCompletedRpoStatus()); armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getSaveBackgroundSearchRpoState()); @@ -122,7 +127,7 @@ void pollArmRpo_shouldPollSuccessfullyWithSaveBackgroundCompleted() throws IOExc } @Test - void pollArmRpo_shouldPollSuccessfullyWithSaveBackgroundCompletedForManualRun() throws IOException { + void pollArmRpo_shouldPollSuccessfully_whenSaveBackgroundCompletedForManualRun() throws IOException { // given armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getCompletedRpoStatus()); armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getSaveBackgroundSearchRpoState()); @@ -163,7 +168,7 @@ void pollArmRpo_shouldPollSuccessfullyWithSaveBackgroundCompletedForManualRun() } @Test - void pollArmRpo_shouldPollSuccessfullyWithCreateExportBasedOnSearchResultsTableInProgress() throws IOException { + void pollArmRpo_shouldPollSuccessfully_whenCreateExportBasedOnSearchResultsTableInProgress() throws IOException { // given armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getInProgressRpoStatus()); armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getCreateExportBasedOnSearchResultsTableRpoState()); @@ -204,7 +209,7 @@ void pollArmRpo_shouldPollSuccessfullyWithCreateExportBasedOnSearchResultsTableI } @Test - void pollArmRpo_shouldPollSuccessfullyWithCreateExportBasedOnSearchResultsTableForManualRun() throws IOException { + void pollArmRpo_shouldPollSuccessfully_whenCreateExportBasedOnSearchResultsTableForManualRun() throws IOException { // given armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getInProgressRpoStatus()); armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getCreateExportBasedOnSearchResultsTableRpoState()); @@ -245,7 +250,7 @@ void pollArmRpo_shouldPollSuccessfullyWithCreateExportBasedOnSearchResultsTableF } @Test - void pollArmRpo_shouldPollSuccessfullyWithDownloadProductionFailedOnPreviousAttemptForManualRun() throws IOException { + void pollArmRpo_shouldPollSuccessfully_whenDownloadProductionFailedOnPreviousAttemptForManualRun() throws IOException { // given armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getFailedRpoStatus()); armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getDownloadProductionRpoState()); @@ -286,7 +291,7 @@ void pollArmRpo_shouldPollSuccessfullyWithDownloadProductionFailedOnPreviousAtte } @Test - void pollArmRpo_shouldPollNotFindLatestExecutionDetailOnStepDownloadProductionFailed() throws IOException { + void pollArmRpo_shouldPollNotFindLatestExecutionDetail_OnStepDownloadProductionFailed() { // given armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getFailedRpoStatus()); armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getDownloadProductionRpoState()); @@ -302,7 +307,7 @@ void pollArmRpo_shouldPollNotFindLatestExecutionDetailOnStepDownloadProductionFa } @Test - void pollArmRpo_shouldPollNotFindLatestExecutionDetailForManualRunOnStepDownloadProductionInProgress() throws IOException { + void pollArmRpo_shouldPollNotFindLatestExecutionDetailForManualRun_OnStepDownloadProductionInProgress() { // given armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getInProgressRpoStatus()); armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getDownloadProductionRpoState()); @@ -399,7 +404,7 @@ void pollArmRpo_shouldHandleNoProductionFiles() { } @Test - void pollArmRpo_shouldHandleExceptionDuringPollingOnCreateExportBasedOnSearchResultsTableStep() { + void pollArmRpo_shouldHandleExceptionDuringPolling_whenCreateExportBasedOnSearchResultsTableStep() { // given armRpoExecutionDetailEntity.setArmRpoStatus(ARM_RPO_HELPER_MOCKS.getCompletedRpoStatus()); armRpoExecutionDetailEntity.setArmRpoState(ARM_RPO_HELPER_MOCKS.getSaveBackgroundSearchRpoState()); @@ -429,7 +434,8 @@ private List createHeaderColumns() { false), createMasterIndexFieldByRecordClassSchema("109b6bf1-57a0-48ec-b22e-c7248dc74f91", "Contributor", "contributor", "string", false), createMasterIndexFieldByRecordClassSchema("893048bf-1e7c-4811-9abf-00cd77a715cf", "Record Date", "recordDate", "date", false), - createMasterIndexFieldByRecordClassSchema("fdd0fcbb-da46-4af1-a627-ac255c12bb23", "ObjectId", "bf_012", "number", false) + createMasterIndexFieldByRecordClassSchema("fdd0fcbb-da46-4af1-a627-ac255c12bb23", "ObjectId", "bf_012", "number", false), + createMasterIndexFieldByRecordClassSchema("1fa3bf91-5234-432d-bebb-b339dc3aaccf", "Region", "bf_012", "number", true) ); }