diff --git a/src/main/java/org/folio/linked/data/service/rdf/RdfImportServiceImpl.java b/src/main/java/org/folio/linked/data/service/rdf/RdfImportServiceImpl.java index 6b0085f1..04710822 100644 --- a/src/main/java/org/folio/linked/data/service/rdf/RdfImportServiceImpl.java +++ b/src/main/java/org/folio/linked/data/service/rdf/RdfImportServiceImpl.java @@ -68,7 +68,7 @@ private ImportUtils.ImportReport doImport(Set resources) { var mockResourcesSearchResult = lccnResourceService.findMockResources(resources); resources.forEach(resourceModel -> { try { - resourceModel = lccnResourceService.unMockLccnResource(resourceModel, mockResourcesSearchResult); + resourceModel = lccnResourceService.unMockLccnEdges(resourceModel, mockResourcesSearchResult); var resource = resourceModelMapper.toEntity(resourceModel); metadataService.ensure(resource); var saveGraphResult = resourceGraphService.saveMergingGraphInNewTransaction(resource); diff --git a/src/main/java/org/folio/linked/data/service/search/lccn/LccnResourceService.java b/src/main/java/org/folio/linked/data/service/search/lccn/LccnResourceService.java index cc28994d..78715757 100644 --- a/src/main/java/org/folio/linked/data/service/search/lccn/LccnResourceService.java +++ b/src/main/java/org/folio/linked/data/service/search/lccn/LccnResourceService.java @@ -11,7 +11,7 @@ public interface LccnResourceService { Map findMockResources(Set resources); - Resource unMockLccnResource(Resource resourceModel, Map searchResults); + Resource unMockLccnEdges(Resource resourceModel, Map searchResults); record LccnResourceSearchResult(@Nullable ResourceSubgraphView subgraphView, @Nonnull String inventoryId) { } diff --git a/src/main/java/org/folio/linked/data/service/search/lccn/LccnResourceServiceImpl.java b/src/main/java/org/folio/linked/data/service/search/lccn/LccnResourceServiceImpl.java index d29e9e95..fb8e2998 100644 --- a/src/main/java/org/folio/linked/data/service/search/lccn/LccnResourceServiceImpl.java +++ b/src/main/java/org/folio/linked/data/service/search/lccn/LccnResourceServiceImpl.java @@ -10,6 +10,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.Map; +import java.util.Optional; import java.util.Set; import lombok.RequiredArgsConstructor; import org.folio.ld.dictionary.model.FolioMetadata; @@ -26,7 +27,6 @@ @Service @RequiredArgsConstructor public class LccnResourceServiceImpl implements LccnResourceService { - private static final String NOT_FOUND_MESSAGE = "Resource presented only with LCCN [%s] is not found in %s"; private final SearchClient searchClient; private final MockLccnResourceService mockLccnResourceService; private final ResourceSubgraphViewMapper resourceSubgraphViewMapper; @@ -63,24 +63,20 @@ public Map findMockResources(Set res } @Override - public Resource unMockLccnResource(Resource resourceModel, Map searchResults) { - return mockLccnResourceService.unMockLccnResource(resourceModel, lccn -> getLccnResource(lccn, searchResults)); + public Resource unMockLccnEdges(Resource resource, Map searchResults) { + return mockLccnResourceService.unMockLccnEdges(resource, lccn -> getLccnResource(lccn, searchResults)); } - private Resource getLccnResource(String lccn, Map searchResults) { + private Optional getLccnResource(String lccn, Map searchResults) { return ofNullable(searchResults.get(lccn)) - .map(searchResult -> { + .flatMap(searchResult -> { var inventoryId = searchResult.inventoryId(); return ofNullable(searchResult.subgraphView()) .flatMap(rsw -> resourceSubgraphViewMapper.fromJson(rsw.getResourceSubgraph()) .map(r -> r.setFolioMetadata(new FolioMetadata().setInventoryId(rsw.getInventoryId()))) ) - .or(() -> resourceMarcAuthorityService.fetchResourceFromSrsByInventoryId(inventoryId)) - .orElseThrow( - () -> new RuntimeException(NOT_FOUND_MESSAGE.formatted(lccn, "SRS by inventoryId " + inventoryId)) - ); - }) - .orElseThrow(() -> new RuntimeException(NOT_FOUND_MESSAGE.formatted(lccn, "Search"))); + .or(() -> resourceMarcAuthorityService.fetchResourceFromSrsByInventoryId(inventoryId)); + }); } } diff --git a/src/test/java/org/folio/linked/data/service/rdf/RdfImportServiceTest.java b/src/test/java/org/folio/linked/data/service/rdf/RdfImportServiceTest.java index de2df8ab..cf366057 100644 --- a/src/test/java/org/folio/linked/data/service/rdf/RdfImportServiceTest.java +++ b/src/test/java/org/folio/linked/data/service/rdf/RdfImportServiceTest.java @@ -73,7 +73,7 @@ void importFile_createsResources_whenValidFileProvided() throws IOException { var searchResults = new HashMap(); when(lccnResourceService.findMockResources(resources)).thenReturn(searchResults); var unMockedResource = new Resource().setId(1L); - when(lccnResourceService.unMockLccnResource(any(), any())).thenReturn(unMockedResource); + when(lccnResourceService.unMockLccnEdges(any(), any())).thenReturn(unMockedResource); when(resourceModelMapper.toEntity(unMockedResource)).thenReturn(entity); var saveGraphResult = new SaveGraphResult(entity, Set.of(entity), Set.of()); when(resourceGraphService.saveMergingGraphInNewTransaction(entity)).thenReturn(saveGraphResult); @@ -98,7 +98,7 @@ void importFile_updatesResources_whenValidFileProvidedAndResourceExists() throws var searchResults = new HashMap(); when(lccnResourceService.findMockResources(resources)).thenReturn(searchResults); var unMockedResource = new Resource().setId(1L); - when(lccnResourceService.unMockLccnResource(any(), any())).thenReturn(unMockedResource); + when(lccnResourceService.unMockLccnEdges(any(), any())).thenReturn(unMockedResource); when(resourceModelMapper.toEntity(unMockedResource)).thenReturn(entity); var saveGraphResult = new SaveGraphResult(entity, Set.of(), Set.of(entity)); when(resourceGraphService.saveMergingGraphInNewTransaction(entity)).thenReturn(saveGraphResult); @@ -183,7 +183,7 @@ void saveImportEventResources_shouldSaveResources() { var expectedImportEventResult = new ImportEventResult().setEventTs(Long.parseLong(ts)); when(importEventResultMapper.fromImportReport(eq(event), any(), any())) .thenReturn(expectedImportEventResult); - when(lccnResourceService.unMockLccnResource(any(), any())) + when(lccnResourceService.unMockLccnEdges(any(), any())) .thenAnswer(inv -> inv.getArgument(0)); diff --git a/src/test/java/org/folio/linked/data/service/search/lccn/LccnResourceServiceTest.java b/src/test/java/org/folio/linked/data/service/search/lccn/LccnResourceServiceTest.java index e391b963..f588818f 100644 --- a/src/test/java/org/folio/linked/data/service/search/lccn/LccnResourceServiceTest.java +++ b/src/test/java/org/folio/linked/data/service/search/lccn/LccnResourceServiceTest.java @@ -2,7 +2,6 @@ import static java.util.Optional.empty; import static java.util.Optional.of; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import static org.folio.linked.data.service.search.lccn.LccnResourceService.LccnResourceSearchResult; import static org.folio.linked.data.test.TestUtil.getLccnResourceSearchResult; @@ -15,6 +14,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.function.Function; @@ -53,7 +53,7 @@ class LccnResourceServiceTest { private ResourceMarcAuthorityService resourceMarcAuthorityService; @Test - void shouldReturnResultsWithFoundResourceAndInventoryId() { + void findMockResources_shouldReturnResultsWithFoundResourceAndInventoryId() { // given var lccn1 = "lccnOfExistedResource"; var lccn2 = "lccnOfNotExistedResourceButFoundInventoryId"; @@ -85,7 +85,7 @@ void shouldReturnResultsWithFoundResourceAndInventoryId() { } @Test - void shouldReturnNothing_ifNoGivenResources() { + void findMockResources_shouldReturnNothing_ifNoGivenResources() { // when var result = lccnResourceSearchService.findMockResources(new HashSet<>()); @@ -95,7 +95,7 @@ void shouldReturnNothing_ifNoGivenResources() { } @Test - void shouldReturnNothing_ifNoMockLccnResources() { + void findMockResources_shouldReturnNothing_ifNoMockLccnResources() { // given var resources = Set.of(new Resource().setId(1L)); var lccns = Set.of(); @@ -109,7 +109,7 @@ void shouldReturnNothing_ifNoMockLccnResources() { } @Test - void unMockLccnResource_shouldReturnMappedResourceFromSearchResults_ifPresentedInGraph() { + void unMockLccnEdges_shouldMapEdgesFromSearchResults_ifPresentedInGraph() { // given var lccn = UUID.randomUUID().toString(); var searchResults = new HashMap(); @@ -119,20 +119,18 @@ void unMockLccnResource_shouldReturnMappedResourceFromSearchResults_ifPresentedI var resource = new org.folio.ld.dictionary.model.Resource().setId(1L); doReturn(of(resource)).when(resourceSubgraphViewMapper).fromJson("1"); var mockResource = new Resource().setId(2L); - when(mockLccnResourceService.unMockLccnResource(any(), any())).thenAnswer(inv -> { - var lccnProvider = (Function) inv.getArgument(1); - return lccnProvider.apply(lccn); - }); + when(mockLccnResourceService.unMockLccnEdges(any(), any())) + .thenAnswer(inv -> ((Function>) inv.getArgument(1)).apply(lccn).get()); // when - var result = lccnResourceSearchService.unMockLccnResource(mockResource, searchResults); + var result = lccnResourceSearchService.unMockLccnEdges(mockResource, searchResults); // then assertThat(result).isEqualTo(resource); } @Test - void unMockLccnResource_shouldReturnResourceFromResourceMarcAuthorityService_ifNotPresentedInGraph() { + void unMockLccnResource_shouldMapEdgesTakenFromMarcAuthorityService_ifNotPresentedInGraph() { // given var lccn = UUID.randomUUID().toString(); var searchResults = new HashMap(); @@ -143,20 +141,18 @@ void unMockLccnResource_shouldReturnResourceFromResourceMarcAuthorityService_ifN var resource = new org.folio.ld.dictionary.model.Resource().setId(1L); doReturn(of(resource)).when(resourceMarcAuthorityService).fetchResourceFromSrsByInventoryId(inventoryId); var mockResource = new Resource().setId(2L); - when(mockLccnResourceService.unMockLccnResource(any(), any())).thenAnswer(inv -> { - var lccnProvider = (Function) inv.getArgument(1); - return lccnProvider.apply(lccn); - }); + when(mockLccnResourceService.unMockLccnEdges(any(), any())) + .thenAnswer(inv -> ((Function>) inv.getArgument(1)).apply(lccn).get()); // when - var result = lccnResourceSearchService.unMockLccnResource(mockResource, searchResults); + var result = lccnResourceSearchService.unMockLccnEdges(mockResource, searchResults); // then assertThat(result).isEqualTo(resource); } @Test - void unMockLccnResource_shouldThrowException_ifResourceNotFoundByResourceMarcAuthorityService() { + void unMockLccnResource_shouldNotMapAnything_ifNotFoundInMarcAuthorityService() { // given var lccn = UUID.randomUUID().toString(); var searchResults = new HashMap(); @@ -166,36 +162,31 @@ void unMockLccnResource_shouldThrowException_ifResourceNotFoundByResourceMarcAut searchResults.put(lccn + "3", getLccnResourceSearchResult("3", null)); doReturn(empty()).when(resourceMarcAuthorityService).fetchResourceFromSrsByInventoryId(inventoryId); var mockResource = new Resource().setId(2L); - when(mockLccnResourceService.unMockLccnResource(any(), any())).thenAnswer(inv -> { - var lccnProvider = (Function) inv.getArgument(1); - return lccnProvider.apply(lccn); - }); + when(mockLccnResourceService.unMockLccnEdges(any(), any())) + .thenAnswer(inv -> ((Function>) inv.getArgument(1)).apply(lccn).orElse(null)); // when - var result = assertThatThrownBy(() -> lccnResourceSearchService.unMockLccnResource(mockResource, searchResults)); + var result = lccnResourceSearchService.unMockLccnEdges(mockResource, searchResults); // then - result.hasMessage("Resource presented only with LCCN [" + lccn - + "] is not found in SRS by inventoryId " + inventoryId); + assertThat(result).isNull(); } @Test - void unMockLccnResource_shouldThrowException_ifResourceNotFoundBySearch() { + void unMockLccnResource_shouldNotMapAnything_ifEdgesNotFoundBySearch() { // given var lccn = UUID.randomUUID().toString(); var searchResults = new HashMap(); searchResults.put(lccn + "2", getLccnResourceSearchResult("2", null)); searchResults.put(lccn + "3", getLccnResourceSearchResult("3", null)); var mockResource = new Resource().setId(2L); - when(mockLccnResourceService.unMockLccnResource(any(), any())).thenAnswer(inv -> { - var lccnProvider = (Function) inv.getArgument(1); - return lccnProvider.apply(lccn); - }); + when(mockLccnResourceService.unMockLccnEdges(any(), any())) + .thenAnswer(inv -> ((Function>) inv.getArgument(1)).apply(lccn).orElse(null)); // when - var result = assertThatThrownBy(() -> lccnResourceSearchService.unMockLccnResource(mockResource, searchResults)); + var result = lccnResourceSearchService.unMockLccnEdges(mockResource, searchResults); // then - result.hasMessage("Resource presented only with LCCN [" + lccn + "] is not found in Search"); + assertThat(result).isNull(); } }