Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private ImportUtils.ImportReport doImport(Set<Resource> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface LccnResourceService {

Map<String, LccnResourceSearchResult> findMockResources(Set<Resource> resources);

Resource unMockLccnResource(Resource resourceModel, Map<String, LccnResourceSearchResult> searchResults);
Resource unMockLccnEdges(Resource resourceModel, Map<String, LccnResourceSearchResult> searchResults);

record LccnResourceSearchResult(@Nullable ResourceSubgraphView subgraphView, @Nonnull String inventoryId) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -63,24 +63,20 @@ public Map<String, LccnResourceSearchResult> findMockResources(Set<Resource> res
}

@Override
public Resource unMockLccnResource(Resource resourceModel, Map<String, LccnResourceSearchResult> searchResults) {
return mockLccnResourceService.unMockLccnResource(resourceModel, lccn -> getLccnResource(lccn, searchResults));
public Resource unMockLccnEdges(Resource resource, Map<String, LccnResourceSearchResult> searchResults) {
return mockLccnResourceService.unMockLccnEdges(resource, lccn -> getLccnResource(lccn, searchResults));
}

private Resource getLccnResource(String lccn, Map<String, LccnResourceSearchResult> searchResults) {
private Optional<Resource> getLccnResource(String lccn, Map<String, LccnResourceSearchResult> 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));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void importFile_createsResources_whenValidFileProvided() throws IOException {
var searchResults = new HashMap<String, LccnResourceService.LccnResourceSearchResult>();
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);
Expand All @@ -98,7 +98,7 @@ void importFile_updatesResources_whenValidFileProvidedAndResourceExists() throws
var searchResults = new HashMap<String, LccnResourceService.LccnResourceSearchResult>();
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);
Expand Down Expand Up @@ -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));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -53,7 +53,7 @@ class LccnResourceServiceTest {
private ResourceMarcAuthorityService resourceMarcAuthorityService;

@Test
void shouldReturnResultsWithFoundResourceAndInventoryId() {
void findMockResources_shouldReturnResultsWithFoundResourceAndInventoryId() {
// given
var lccn1 = "lccnOfExistedResource";
var lccn2 = "lccnOfNotExistedResourceButFoundInventoryId";
Expand Down Expand Up @@ -85,7 +85,7 @@ void shouldReturnResultsWithFoundResourceAndInventoryId() {
}

@Test
void shouldReturnNothing_ifNoGivenResources() {
void findMockResources_shouldReturnNothing_ifNoGivenResources() {

// when
var result = lccnResourceSearchService.findMockResources(new HashSet<>());
Expand All @@ -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();
Expand All @@ -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<String, LccnResourceSearchResult>();
Expand All @@ -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<String, Resource>) inv.getArgument(1);
return lccnProvider.apply(lccn);
});
when(mockLccnResourceService.unMockLccnEdges(any(), any()))
.thenAnswer(inv -> ((Function<String, Optional<Resource>>) 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<String, LccnResourceSearchResult>();
Expand All @@ -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<String, Resource>) inv.getArgument(1);
return lccnProvider.apply(lccn);
});
when(mockLccnResourceService.unMockLccnEdges(any(), any()))
.thenAnswer(inv -> ((Function<String, Optional<Resource>>) 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<String, LccnResourceSearchResult>();
Expand All @@ -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<String, Resource>) inv.getArgument(1);
return lccnProvider.apply(lccn);
});
when(mockLccnResourceService.unMockLccnEdges(any(), any()))
.thenAnswer(inv -> ((Function<String, Optional<Resource>>) 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<String, LccnResourceSearchResult>();
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<String, Resource>) inv.getArgument(1);
return lccnProvider.apply(lccn);
});
when(mockLccnResourceService.unMockLccnEdges(any(), any()))
.thenAnswer(inv -> ((Function<String, Optional<Resource>>) 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();
}
}