Skip to content

Commit

Permalink
[MODINV-1123] Update suppress from discovery flag at SRS record durin…
Browse files Browse the repository at this point in the history
…g instance sharing (#791)
  • Loading branch information
RomanChernetskyi authored Dec 4, 2024
1 parent 7a7de01 commit 64bda1c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Future<String> publishInstance(Instance instance, SharingInstance sharin
return restDataImportHelper.importMarcRecord(marcRecord, sharingInstanceMetadata, kafkaHeaders)
.compose(result -> {
if ("COMMITTED".equals(result)) {
return updateTargetInstanceWithNonMarcControlledFields(instance, target)
return updateTargetInstanceWithNonMarcControlledFields(instance, target, kafkaHeaders)
// Delete source record by record ID if the result is "COMMITTED"
.compose(targetInstance -> deleteSourceRecordByRecordId(marcRecord.getId(), instanceId, sourceTenant, sourceStorageClient)
.map(targetInstance))
Expand All @@ -98,11 +98,22 @@ public Future<String> publishInstance(Instance instance, SharingInstance sharin
});
}

private Future<Instance> updateTargetInstanceWithNonMarcControlledFields(Instance sourceInstance, Target targetTenantProvider) {
private Future<Instance> updateTargetInstanceWithNonMarcControlledFields(Instance sourceInstance, Target targetTenantProvider,
Map<String, String> kafkaHeaders) {
return instanceOperations.getInstanceById(sourceInstance.getId(), targetTenantProvider)
.map(targetInstance -> populateTargetInstanceWithNonMarcControlledFields(targetInstance, sourceInstance))
.compose(targetInstance -> instanceOperations.updateInstance(targetInstance, targetTenantProvider)
.map(targetInstance));
.compose(targetInstance -> instanceOperations.updateInstance(targetInstance, targetTenantProvider).map(targetInstance))
.compose(targetInstance -> updateSuppressFromDiscoveryFlagIfNeeded(targetInstance, targetTenantProvider, kafkaHeaders));
}

private Future<Instance> updateSuppressFromDiscoveryFlagIfNeeded(Instance targetInstance, Target targetTenantProvider,
Map<String, String> kafkaHeaders) {
if (Boolean.TRUE.equals(targetInstance.getDiscoverySuppress())) {
SourceStorageRecordsClient sourceStorageClient = getSourceStorageRecordsClient(targetTenantProvider.getTenantId(), kafkaHeaders);
return updateSourceRecordSuppressFromDiscoveryByInstanceId(targetInstance.getId(), targetInstance.getDiscoverySuppress(), sourceStorageClient)
.map(targetInstance);
}
return Future.succeededFuture(targetInstance);
}

private Instance populateTargetInstanceWithNonMarcControlledFields(Instance targetInstance, Instance sourceInstance) {
Expand Down Expand Up @@ -246,6 +257,27 @@ Future<String> deleteSourceRecordByRecordId(String recordId, String instanceId,
});
}

Future<String> updateSourceRecordSuppressFromDiscoveryByInstanceId(String instanceId, boolean suppress,
SourceStorageRecordsClient sourceStorageClient) {
LOGGER.info("updateSourceRecordSuppressFromDiscoveryByInstanceId:: Updating suppress from discovery flag for record in SRS, instanceId: {}, suppressFromDiscovery: {}",
instanceId, suppress);

return sourceStorageClient.putSourceStorageRecordsSuppressFromDiscoveryById(instanceId,
INSTANCE_ID_TYPE, suppress)
.compose(response -> {
if (response.statusCode() == org.folio.HttpStatus.HTTP_OK.toInt()) {
LOGGER.info("updateSourceRecordSuppressFromDiscoveryByInstanceId:: Suppress from discovery flag was successfully updated for record in SRS, instanceId: {}, suppressFromDiscovery: {}",
instanceId, suppress);
return Future.succeededFuture(instanceId);
} else {
String errorMessage = format("Cannot update suppress from discovery flag for SRS record, instanceId: %s, statusCode: %s, suppressFromDiscovery: %s",
instanceId, response.statusCode(), suppress);
LOGGER.warn(format("updateSourceRecordSuppressFromDiscoveryByInstanceId:: %s", errorMessage));
return Future.failedFuture(errorMessage);
}
});
}

public SourceStorageRecordsClient getSourceStorageRecordsClient(String tenant, Map<String, String> kafkaHeaders) {
LOGGER.info("getSourceStorageRecordsClient :: Creating SourceStorageRecordsClient for tenant={}", tenant);
return new SourceStorageRecordsClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@

import static org.folio.HttpStatus.HTTP_INTERNAL_SERVER_ERROR;
import static org.folio.HttpStatus.HTTP_NO_CONTENT;
import static org.folio.HttpStatus.HTTP_OK;
import static org.folio.inventory.TestUtil.buildHttpResponseWithBuffer;
import static org.folio.inventory.consortium.handlers.MarcInstanceSharingHandlerImpl.SRS_RECORD_ID_TYPE;
import static org.folio.inventory.dataimport.handlers.actions.ReplaceInstanceEventHandler.INSTANCE_ID_TYPE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.argThat;
Expand Down Expand Up @@ -176,6 +179,7 @@ public void publishInstanceTest(TestContext testContext) {

ArgumentCaptor<Instance> updatedInstanceCaptor = ArgumentCaptor.forClass(Instance.class);
verify(instanceOperationsHelper).updateInstance(updatedInstanceCaptor.capture(), argThat(p -> MEMBER_TENANT.equals(p.getTenantId())));
verify(marcHandler, times(0)).updateSourceRecordSuppressFromDiscoveryByInstanceId(any(), anyBoolean(), any());
Instance updatedInstance = updatedInstanceCaptor.getValue();
testContext.assertEquals("CONSORTIUM-MARC", updatedInstance.getSource());
testContext.assertEquals(targetInstanceHrid, updatedInstance.getHrid());
Expand Down Expand Up @@ -545,6 +549,57 @@ public void deleteSourceRecordByInstanceIdFailedTestWhenResponseStatusIsNotNoCon
verify(sourceStorageClient, times(1)).deleteSourceStorageRecordsById(recordId, SRS_RECORD_ID_TYPE);
}

@Test
public void updateSourceRecordSuppressFromDiscoveryByInstanceIdSuccessTest() {

String instanceId = "fea6477b-d8f5-4d22-9e86-6218407c780b";

HttpResponse<Buffer> mockedResponse = mock(HttpResponse.class);

when(mockedResponse.statusCode()).thenReturn(HTTP_OK.toInt());
when(sourceStorageClient.putSourceStorageRecordsSuppressFromDiscoveryById(any(), any(), anyBoolean()))
.thenReturn(Future.succeededFuture(mockedResponse));

MarcInstanceSharingHandlerImpl handler = new MarcInstanceSharingHandlerImpl(instanceOperationsHelper, null, vertx, httpClient);
handler.updateSourceRecordSuppressFromDiscoveryByInstanceId(instanceId, true, sourceStorageClient)
.onComplete(result -> assertEquals(instanceId, result.result()));

verify(sourceStorageClient, times(1)).putSourceStorageRecordsSuppressFromDiscoveryById(instanceId, INSTANCE_ID_TYPE, true);
}

@Test
public void updateSourceRecordSuppressFromDiscoveryByInstanceIdFailedTest() {

String instanceId = "991f37c8-cd22-4db7-9543-a4ec68735e95";

when(sourceStorageClient.putSourceStorageRecordsSuppressFromDiscoveryById(any(), any(), anyBoolean()))
.thenReturn(Future.failedFuture(new NotFoundException("Not found")));

MarcInstanceSharingHandlerImpl handler = new MarcInstanceSharingHandlerImpl(instanceOperationsHelper, null, vertx, httpClient);
handler.updateSourceRecordSuppressFromDiscoveryByInstanceId(instanceId, true, sourceStorageClient)
.onComplete(result -> assertTrue(result.failed()));

verify(sourceStorageClient, times(1)).putSourceStorageRecordsSuppressFromDiscoveryById(instanceId, INSTANCE_ID_TYPE, true);
}

@Test
public void updateSourceRecordSuppressFromDiscoveryByInstanceIdFailedTestWhenResponseStatusIsNotOk() {

String instanceId = "991f37c8-cd22-4db7-9543-a4ec68735e95";

HttpResponse<Buffer> mockedResponse = mock(HttpResponse.class);

when(mockedResponse.statusCode()).thenReturn(HTTP_INTERNAL_SERVER_ERROR.toInt());
when(sourceStorageClient.putSourceStorageRecordsSuppressFromDiscoveryById(any(), any(), anyBoolean()))
.thenReturn(Future.succeededFuture(mockedResponse));

MarcInstanceSharingHandlerImpl handler = new MarcInstanceSharingHandlerImpl(instanceOperationsHelper, null, vertx, httpClient);
handler.updateSourceRecordSuppressFromDiscoveryByInstanceId(instanceId, true, sourceStorageClient)
.onComplete(result -> assertTrue(result.failed()));

verify(sourceStorageClient, times(1)).putSourceStorageRecordsSuppressFromDiscoveryById(instanceId, INSTANCE_ID_TYPE, true);
}

@Test
public void shouldPopulateTargetInstanceWithNonMarcControlledFields(TestContext testContext) {
//given
Expand Down Expand Up @@ -576,6 +631,7 @@ public void shouldPopulateTargetInstanceWithNonMarcControlledFields(TestContext
doReturn(sourceStorageClient).when(marcHandler).getSourceStorageRecordsClient(anyString(), eq(kafkaHeaders));
doReturn(Future.succeededFuture(record)).when(marcHandler).getSourceMARCByInstanceId(any(), any(), any());
doReturn(Future.succeededFuture(instanceId)).when(marcHandler).deleteSourceRecordByRecordId(any(), any(), any(), any());
doReturn(Future.succeededFuture(instanceId)).when(marcHandler).updateSourceRecordSuppressFromDiscoveryByInstanceId(any(), anyBoolean(), any());

when(restDataImportHelper.importMarcRecord(any(), any(), any()))
.thenReturn(Future.succeededFuture("COMMITTED"));
Expand All @@ -592,6 +648,7 @@ public void shouldPopulateTargetInstanceWithNonMarcControlledFields(TestContext

ArgumentCaptor<Instance> updatedInstanceCaptor = ArgumentCaptor.forClass(Instance.class);
verify(instanceOperationsHelper).updateInstance(updatedInstanceCaptor.capture(), argThat(p -> CONSORTIUM_TENANT.equals(p.getTenantId())));
verify(marcHandler, times(1)).updateSourceRecordSuppressFromDiscoveryByInstanceId(instanceId, true, sourceStorageClient);
Instance targetInstanceWithNonMarcData = updatedInstanceCaptor.getValue();
testContext.assertEquals("MARC", targetInstanceWithNonMarcData.getSource());
testContext.assertEquals(targetInstanceHrid, targetInstanceWithNonMarcData.getHrid());
Expand Down

0 comments on commit 64bda1c

Please sign in to comment.