Skip to content

Commit

Permalink
DMP-4143: Implement ProcessE2EArmRpoPendingAutomatedTask
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbellingham-hmcts committed Nov 15, 2024
1 parent af6caae commit e304b77
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package uk.gov.hmcts.darts.task.runner.impl;

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.test.context.TestPropertySource;
import uk.gov.hmcts.darts.arm.client.ArmRpoClient;
import uk.gov.hmcts.darts.arm.client.model.rpo.ArmAsyncSearchResponse;
import uk.gov.hmcts.darts.arm.client.model.rpo.IndexesByMatterIdResponse;
import uk.gov.hmcts.darts.arm.client.model.rpo.MasterIndexFieldByRecordClassSchemaResponse;
import uk.gov.hmcts.darts.arm.client.model.rpo.ProfileEntitlementResponse;
import uk.gov.hmcts.darts.arm.client.model.rpo.RecordManagementMatterResponse;
import uk.gov.hmcts.darts.arm.client.model.rpo.SaveBackgroundSearchResponse;
import uk.gov.hmcts.darts.arm.client.model.rpo.StorageAccountResponse;
import uk.gov.hmcts.darts.arm.exception.ArmRpoException;
import uk.gov.hmcts.darts.arm.service.ArmApiService;
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.enums.ArmRpoStateEnum;
import uk.gov.hmcts.darts.common.enums.ArmRpoStatusEnum;
import uk.gov.hmcts.darts.testutils.PostgresIntegrationBase;

import java.util.Collections;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

@TestPropertySource(properties = {
"darts.storage.arm-api.arm-storage-account-name=SOME ARM STORAGE ACCOUNT NAME",
"darts.storage.arm-api.arm-service-entitlement=SOME ENTITLEMENT NAME"
})
class ProcessE2EArmRpoPendingAutomatedTaskIntTest extends PostgresIntegrationBase {

@Autowired
private ProcessE2EArmRpoPendingAutomatedTask task;

@MockBean
private UserIdentity userIdentity;

@MockBean
private ArmApiService armApiService;

@MockBean
private ArmRpoClient armRpoClient;

private static final String BEARER_TOKEN = "SOME BEARER TOKEN";
private static final String MATTER_ID = "SOME MATTER ID";
private static final String INDEX_ID = "SOME INDEX ID";
private static final String STORAGE_ACCOUNT_INDEX_NAME = "SOME ARM STORAGE ACCOUNT NAME";
private static final String STORAGE_ACCOUNT_INDEX_ID = "SOME ARM STORAGE ACCOUNT INDEX ID";
private static final String ENTITLEMENT_NAME = "SOME ENTITLEMENT NAME";
private static final String ENTITLEMENT_ID = "SOME ENTITLEMENT ID";
private static final String SOME_MASTER_INDEX_FIELD_ID = "SOME MASTER INDEX FIELD ID";
private static final String SEARCH_ID = "SOME SEARCH ID";

@BeforeEach
void setUp() {
UserAccountEntity user = dartsDatabase.getUserAccountStub().getSystemUserAccountEntity();
when(userIdentity.getUserAccount())
.thenReturn(user);

when(armApiService.getArmBearerToken())
.thenReturn(BEARER_TOKEN);
}

@Test
void runTask_shouldTriggerArmRpoSearch_andSetExpectedFinalExecutionDetailStateAndStatus_whenAllDownstreamArmCallsAreSuccessful() {
// Given
createAndSetRecordManagementMatterMock();
createAndSetIndexesByMatterIdMock();
createAndSetGetStorageAccountsMock();
createAndSetGetProfileEntitlementsMock();
createAndSetGetMasterIndexFieldByRecordClassSchemaMock();
createAndSetAddAsyncSearchMock();
createAndSetSaveBackgroundSearchMock();

// When
task.runTask();

// Then
List<ArmRpoExecutionDetailEntity> allExecutionDetails = dartsDatabase.getArmRpoExecutionDetailRepository()
.findAll();
assertEquals(1, allExecutionDetails.size());

var executionDetail = allExecutionDetails.getFirst();
assertEquals(ArmRpoStateEnum.SAVE_BACKGROUND_SEARCH.getId(), executionDetail.getArmRpoState().getId());
assertEquals(ArmRpoStatusEnum.COMPLETED.getId(), executionDetail.getArmRpoStatus().getId());

assertEquals(MATTER_ID, executionDetail.getMatterId());
assertEquals(INDEX_ID, executionDetail.getIndexId());
assertEquals(ENTITLEMENT_ID, executionDetail.getEntitlementId());
assertEquals(STORAGE_ACCOUNT_INDEX_ID, executionDetail.getStorageAccountId());
assertNull(executionDetail.getProductionId());
assertEquals(SOME_MASTER_INDEX_FIELD_ID, executionDetail.getSortingField());
assertNull(executionDetail.getSearchItemCount());
}

@Test
void runTask_shouldThrowException_andSetExpectedExecutionDetailStateAndStatus_whenAnyDownstreamArmCallFails() {
// Given the first API call fails
var response = new RecordManagementMatterResponse();
response.setIsError(true);
response.setStatus(400);

when(armRpoClient.getRecordManagementMatter(eq(BEARER_TOKEN)))
.thenReturn(response);

// When
ArmRpoException exception = assertThrows(ArmRpoException.class, () -> task.runTask());
assertThat(exception.getMessage(),
containsString("Failure during ARM RPO getRecordManagementMatter: ARM RPO API failed with status - 400 BAD_REQUEST"));

// Then
List<ArmRpoExecutionDetailEntity> allExecutionDetails = dartsDatabase.getArmRpoExecutionDetailRepository()
.findAll();
assertEquals(1, allExecutionDetails.size());

var executionDetail = allExecutionDetails.getFirst();
assertEquals(ArmRpoStateEnum.GET_RECORD_MANAGEMENT_MATTER.getId(), executionDetail.getArmRpoState().getId());
assertEquals(ArmRpoStatusEnum.FAILED.getId(), executionDetail.getArmRpoStatus().getId());
}

private void createAndSetSaveBackgroundSearchMock() {
SaveBackgroundSearchResponse saveBackgroundSearchResponse = new SaveBackgroundSearchResponse();
saveBackgroundSearchResponse.setStatus(200);
saveBackgroundSearchResponse.setIsError(false);

when(armRpoClient.saveBackgroundSearch(eq(BEARER_TOKEN), any()))
.thenReturn(saveBackgroundSearchResponse);
}

private void createAndSetAddAsyncSearchMock() {
var response = new ArmAsyncSearchResponse();
response.setStatus(200);
response.setIsError(false);
response.setSearchId(SEARCH_ID);

when(armRpoClient.addAsyncSearch(eq(BEARER_TOKEN), anyString()))
.thenReturn(response);
}

private void createAndSetGetMasterIndexFieldByRecordClassSchemaMock() {
var masterIndexField = new MasterIndexFieldByRecordClassSchemaResponse.MasterIndexField();
masterIndexField.setPropertyName("ingestionDate");
masterIndexField.setMasterIndexFieldId(SOME_MASTER_INDEX_FIELD_ID);

var response = new MasterIndexFieldByRecordClassSchemaResponse();
response.setMasterIndexFields(Collections.singletonList(masterIndexField));
response.setStatus(200);
response.setIsError(false);

when(armRpoClient.getMasterIndexFieldByRecordClassSchema(eq(BEARER_TOKEN), any()))
.thenReturn(response);
}

private void createAndSetGetProfileEntitlementsMock() {
var profileEntitlement = new ProfileEntitlementResponse.ProfileEntitlement();
profileEntitlement.setName(ENTITLEMENT_NAME);
profileEntitlement.setEntitlementId(ENTITLEMENT_ID);

var response = new ProfileEntitlementResponse();
response.setEntitlements(Collections.singletonList(profileEntitlement));
response.setStatus(200);
response.setIsError(false);

when(armRpoClient.getProfileEntitlementResponse(BEARER_TOKEN))
.thenReturn(response);
}

private void createAndSetGetStorageAccountsMock() {
var indexDetails = new StorageAccountResponse.IndexDetails();
indexDetails.setName(STORAGE_ACCOUNT_INDEX_NAME);
indexDetails.setIndexId(STORAGE_ACCOUNT_INDEX_ID);

var index = new StorageAccountResponse.Index();
index.setIndex(indexDetails);

var response = new StorageAccountResponse();
response.setIndexes(Collections.singletonList(index));
response.setIsError(false);
response.setStatus(200);

when(armRpoClient.getStorageAccounts(eq(BEARER_TOKEN), any()))
.thenReturn(response);
}

private void createAndSetIndexesByMatterIdMock() {
var indexDetails = new IndexesByMatterIdResponse.IndexDetails();
indexDetails.setIndexId(INDEX_ID);

var index = new IndexesByMatterIdResponse.Index();
index.setIndex(indexDetails);

var response = new IndexesByMatterIdResponse();
response.setIndexes(Collections.singletonList(index));
response.setIsError(false);
response.setStatus(200);

when(armRpoClient.getIndexesByMatterId(eq(BEARER_TOKEN), any()))
.thenReturn(response);
}

private void createAndSetRecordManagementMatterMock() {
var recordManagementMatter = new RecordManagementMatterResponse.RecordManagementMatter();
recordManagementMatter.setMatterId(MATTER_ID);

var response = new RecordManagementMatterResponse();
response.setRecordManagementMatter(recordManagementMatter);
response.setIsError(false);
response.setStatus(200);

when(armRpoClient.getRecordManagementMatter(BEARER_TOKEN))
.thenReturn(response);
}

}
2 changes: 1 addition & 1 deletion src/main/java/uk/gov/hmcts/darts/arm/rpo/ArmRpoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface ArmRpoApi {
List<MasterIndexFieldByRecordClassSchema> getMasterIndexFieldByRecordClassSchema(String bearerToken, Integer executionId, ArmRpoStateEntity rpoStateEntity,
UserAccountEntity userAccount);

void addAsyncSearch(String bearerToken, Integer executionId, UserAccountEntity userAccount);
String addAsyncSearch(String bearerToken, Integer executionId, UserAccountEntity userAccount);

void saveBackgroundSearch(String bearerToken, Integer executionId, String searchName, UserAccountEntity userAccount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public void getRecordManagementMatter(String bearerToken, Integer executionId, U
// this ensures the full error body containing the ARM error detail is logged rather than a truncated version
log.error("Error during ARM get record management matter: {}", e.contentUTF8());
throw handleFailureAndCreateException(ARM_GET_RECORD_MANAGEMENT_MATTER_ERROR, armRpoExecutionDetailEntity, userAccount);
} catch (Exception e) {
log.error("Error during ARM get record management matter: {}", e.getMessage());
throw handleFailureAndCreateException(ARM_GET_RECORD_MANAGEMENT_MATTER_ERROR, armRpoExecutionDetailEntity, userAccount);
}

handleResponseStatus(userAccount, recordManagementMatterResponse, errorMessage, armRpoExecutionDetailEntity);
Expand Down Expand Up @@ -284,7 +287,7 @@ public List<MasterIndexFieldByRecordClassSchema> getMasterIndexFieldByRecordClas
}

@Override
public void addAsyncSearch(String bearerToken, Integer executionId, UserAccountEntity userAccount) {
public String addAsyncSearch(String bearerToken, Integer executionId, UserAccountEntity userAccount) {

final ArmRpoExecutionDetailEntity executionDetail = armRpoService.getArmRpoExecutionDetailEntity(executionId);
armRpoService.updateArmRpoStateAndStatus(executionDetail,
Expand Down Expand Up @@ -334,6 +337,8 @@ public void addAsyncSearch(String bearerToken, Integer executionId, UserAccountE

executionDetail.setSearchId(searchId);
armRpoService.updateArmRpoStatus(executionDetail, ArmRpoHelper.completedRpoStatus(), userAccount);

return searchName;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface ArmApiService {
UpdateMetadataResponse updateMetadata(String externalRecordId, OffsetDateTime eventTimestamp, int retConfScore, String retConfReason);

DownloadResponseMetaData downloadArmData(String externalRecordId, String externalFileId) throws FileNotDownloadedException;

String getArmBearerToken();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

public interface ArmRpoService {

ArmRpoExecutionDetailEntity createArmRpoExecutionDetailEntity(UserAccountEntity userAccount);

ArmRpoExecutionDetailEntity getArmRpoExecutionDetailEntity(Integer executionId);

void updateArmRpoStateAndStatus(ArmRpoExecutionDetailEntity armRpoExecutionDetailEntity, ArmRpoStateEntity armRpoStateEntity,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package uk.gov.hmcts.darts.arm.service;

public interface TriggerArmRpoSearchService {
void triggerArmRpoSearch();
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public DownloadResponseMetaData downloadArmData(String externalRecordId, String
return responseMetaData;
}

private String getArmBearerToken() {
@Override
public String getArmBearerToken() {
log.debug("Get ARM Bearer Token with Username: {}, Password: {}", armApiConfigurationProperties.getArmUsername(),
armApiConfigurationProperties.getArmPassword());
String accessToken = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package uk.gov.hmcts.darts.arm.service.impl;

import jakarta.persistence.EntityManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import uk.gov.hmcts.darts.arm.service.ArmRpoService;
import uk.gov.hmcts.darts.common.entity.ArmRpoExecutionDetailEntity;
import uk.gov.hmcts.darts.common.entity.ArmRpoStateEntity;
Expand All @@ -20,6 +22,19 @@ public class ArmRpoServiceImpl implements ArmRpoService {

public static final String ARM_RPO_EXECUTION_DETAIL_NOT_FOUND = "ArmRpoExecutionDetail not found";
private final ArmRpoExecutionDetailRepository armRpoExecutionDetailRepository;
private final EntityManager entityManager;

@Override
@Transactional
public ArmRpoExecutionDetailEntity createArmRpoExecutionDetailEntity(UserAccountEntity userAccount) {
ArmRpoExecutionDetailEntity armRpoExecutionDetailEntity = new ArmRpoExecutionDetailEntity();

UserAccountEntity mergedUserAccountEntity = entityManager.merge(userAccount);
armRpoExecutionDetailEntity.setCreatedBy(mergedUserAccountEntity);
armRpoExecutionDetailEntity.setLastModifiedBy(mergedUserAccountEntity);

return saveArmRpoExecutionDetailEntity(armRpoExecutionDetailEntity);
}

@Override
public ArmRpoExecutionDetailEntity getArmRpoExecutionDetailEntity(Integer executionId) {
Expand Down Expand Up @@ -53,4 +68,5 @@ public void updateArmRpoStatus(ArmRpoExecutionDetailEntity armRpoExecutionDetail
public ArmRpoExecutionDetailEntity saveArmRpoExecutionDetailEntity(ArmRpoExecutionDetailEntity armRpoExecutionDetailEntity) {
return armRpoExecutionDetailRepository.save(armRpoExecutionDetailEntity);
}

}
Loading

0 comments on commit e304b77

Please sign in to comment.