From f20df500fcb55a46043d056269758dc2475f25e9 Mon Sep 17 00:00:00 2001 From: RomanChernetskyi <111740564+RomanChernetskyi@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:30:21 +0200 Subject: [PATCH] [MODINV-1123] Update suppress from discovery flag at SRS record during instance sharing (#791) (cherry picked from commit 64bda1c42c988d48dde1c92679293ef7101a1de0) --- .../MarcInstanceSharingHandlerImpl.java | 40 +++++++++++-- .../MarcInstanceSharingHandlerImplTest.java | 57 +++++++++++++++++++ 2 files changed, 93 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/folio/inventory/consortium/handlers/MarcInstanceSharingHandlerImpl.java b/src/main/java/org/folio/inventory/consortium/handlers/MarcInstanceSharingHandlerImpl.java index d47133b6e..761da0844 100644 --- a/src/main/java/org/folio/inventory/consortium/handlers/MarcInstanceSharingHandlerImpl.java +++ b/src/main/java/org/folio/inventory/consortium/handlers/MarcInstanceSharingHandlerImpl.java @@ -78,7 +78,7 @@ public Future 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)) @@ -98,11 +98,22 @@ public Future publishInstance(Instance instance, SharingInstance sharin }); } - private Future updateTargetInstanceWithNonMarcControlledFields(Instance sourceInstance, Target targetTenantProvider) { + private Future updateTargetInstanceWithNonMarcControlledFields(Instance sourceInstance, Target targetTenantProvider, + Map 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 updateSuppressFromDiscoveryFlagIfNeeded(Instance targetInstance, Target targetTenantProvider, + Map 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) { @@ -246,6 +257,27 @@ Future deleteSourceRecordByRecordId(String recordId, String instanceId, }); } + Future 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 kafkaHeaders) { LOGGER.info("getSourceStorageRecordsClient :: Creating SourceStorageRecordsClient for tenant={}", tenant); return new SourceStorageRecordsClient( diff --git a/src/test/java/org/folio/inventory/consortium/handlers/MarcInstanceSharingHandlerImplTest.java b/src/test/java/org/folio/inventory/consortium/handlers/MarcInstanceSharingHandlerImplTest.java index 0db8c75f1..3c527e0c5 100644 --- a/src/test/java/org/folio/inventory/consortium/handlers/MarcInstanceSharingHandlerImplTest.java +++ b/src/test/java/org/folio/inventory/consortium/handlers/MarcInstanceSharingHandlerImplTest.java @@ -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; @@ -176,6 +179,7 @@ public void publishInstanceTest(TestContext testContext) { ArgumentCaptor 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()); @@ -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 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 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 @@ -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")); @@ -592,6 +648,7 @@ public void shouldPopulateTargetInstanceWithNonMarcControlledFields(TestContext ArgumentCaptor 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());