diff --git a/src/main/java/org/folio/linked/data/service/resource/impl/ResourceEdgeServiceImpl.java b/src/main/java/org/folio/linked/data/service/resource/impl/ResourceEdgeServiceImpl.java index 3b8b3a99..8d385759 100644 --- a/src/main/java/org/folio/linked/data/service/resource/impl/ResourceEdgeServiceImpl.java +++ b/src/main/java/org/folio/linked/data/service/resource/impl/ResourceEdgeServiceImpl.java @@ -1,52 +1,41 @@ package org.folio.linked.data.service.resource.impl; -import static org.folio.ld.dictionary.ResourceTypeDictionary.INSTANCE; -import static org.folio.ld.dictionary.ResourceTypeDictionary.WORK; - -import lombok.RequiredArgsConstructor; -import org.folio.linked.data.mapper.dto.common.SingleResourceMapper; +import java.util.Map; +import java.util.function.Predicate; +import org.folio.ld.dictionary.PredicateDictionary; +import org.folio.ld.dictionary.ResourceTypeDictionary; import org.folio.linked.data.model.entity.Resource; import org.folio.linked.data.model.entity.ResourceEdge; import org.folio.linked.data.service.resource.ResourceEdgeService; import org.springframework.stereotype.Service; @Service -@RequiredArgsConstructor public class ResourceEdgeServiceImpl implements ResourceEdgeService { - private final SingleResourceMapper singleResourceMapper; + private static final Predicate IS_ADMIN_METADATA = edge -> edge.getPredicate().getUri() + .equals(PredicateDictionary.ADMIN_METADATA.getUri()); - @Override - public void copyOutgoingEdges(Resource from, Resource to) { - if (shouldNotCopyOutgoingEdges(from, to)) { - return; - } + private Map> edgesTobeCopied; - from.getOutgoingEdges() - .stream() - .filter(edge -> !isExcludedEdge(edge)) - .filter(this::hasNoMapper) - .map(edge -> new ResourceEdge(to, edge.getTarget(), edge.getPredicate())) - .forEach(to::addOutgoingEdge); + public ResourceEdgeServiceImpl() { + initializeEdgesTobeCopied(); } - private boolean hasNoMapper(ResourceEdge edge) { - return edge.getTarget() - .getTypes() + public void copyOutgoingEdges(Resource from, Resource to) { + this.edgesTobeCopied.entrySet() .stream() - .allMatch(type -> - singleResourceMapper - .getMapperUnit(type.getUri(), edge.getPredicate(), null, null) - .isEmpty() - ); - } - - private boolean shouldNotCopyOutgoingEdges(Resource from, Resource to) { - return !(from.isOfType(INSTANCE) && to.isOfType(INSTANCE) - || from.isOfType(WORK) && to.isOfType(WORK)); + .filter(entry -> from.isOfType(entry.getKey())) + .filter(entry -> to.isOfType(entry.getKey())) + .map(Map.Entry::getValue) + .flatMap(condition -> from.getOutgoingEdges().stream().filter(condition)) + .map(edge -> new ResourceEdge(to, edge.getTarget(), edge.getPredicate())) + .forEach(to::addOutgoingEdge); } - private boolean isExcludedEdge(ResourceEdge edge) { - return edge.getPredicate().getUri().startsWith("http://bibfra.me/vocab/relation/"); + private void initializeEdgesTobeCopied() { + this.edgesTobeCopied = Map.of( + ResourceTypeDictionary.WORK, IS_ADMIN_METADATA, + ResourceTypeDictionary.INSTANCE, IS_ADMIN_METADATA + ); } } diff --git a/src/test/java/org/folio/linked/data/e2e/resource/ResourceControllerRetainOutgoingEdgesIT.java b/src/test/java/org/folio/linked/data/e2e/resource/ResourceControllerRetainOutgoingEdgesIT.java index 57be8dfb..3d43a2c0 100644 --- a/src/test/java/org/folio/linked/data/e2e/resource/ResourceControllerRetainOutgoingEdgesIT.java +++ b/src/test/java/org/folio/linked/data/e2e/resource/ResourceControllerRetainOutgoingEdgesIT.java @@ -1,8 +1,6 @@ package org.folio.linked.data.e2e.resource; import static org.folio.ld.dictionary.ResourceTypeDictionary.ANNOTATION; -import static org.folio.ld.dictionary.ResourceTypeDictionary.IDENTIFIER; -import static org.folio.ld.dictionary.ResourceTypeDictionary.ID_LCCN; import static org.folio.ld.dictionary.ResourceTypeDictionary.INSTANCE; import static org.folio.ld.dictionary.ResourceTypeDictionary.PERSON; import static org.folio.ld.dictionary.ResourceTypeDictionary.WORK; @@ -41,8 +39,9 @@ @IntegrationTest class ResourceControllerRetainOutgoingEdgesIT { - public static final String RESOURCE_URL = "/resource"; + public static final String RESOURCE_URL = "/linked-data/resource"; private static final String WORK_ID_PLACEHOLDER = "%WORK_ID%"; + @Autowired private ResourceTestService resourceTestService; @Autowired @@ -53,13 +52,13 @@ class ResourceControllerRetainOutgoingEdgesIT { private HashService hashService; @Test - void should_retain_admin_metadata_outgoing_edge_of_work() throws Exception { + void shouldRetainAdminMetadataOfWorkAfterUpdate() throws Exception { // given var creatorPerson = createResource(Map.of(), Set.of(PERSON), Map.of()); var annotation = createResource(Map.of(), Set.of(ANNOTATION), Map.of()); var work = createResource(Map.of(), Set.of(WORK), Map.of( - PredicateDictionary.TITLE, List.of(createPrimaryTitle(3L)), + PredicateDictionary.TITLE, List.of(createPrimaryTitle(1L)), PredicateDictionary.ADMIN_METADATA, List.of(annotation), PredicateDictionary.CREATOR, List.of(creatorPerson), PredicateDictionary.DESIGNER_OF_BOOK, List.of(creatorPerson) @@ -77,24 +76,24 @@ void should_retain_admin_metadata_outgoing_edge_of_work() throws Exception { } @Test - void should_retain_admin_metadata_outgoing_edge_of_instance() throws Exception { + void shouldRetainAdminMetadataOfInstanceAfterUpdate() throws Exception { // given var work = getSampleWork(null); setResourceIds(work); resourceTestService.saveGraph(work); - var lccn = createResource(Map.of(), Set.of(IDENTIFIER, ID_LCCN), Map.of()); var annotation = createResource(Map.of(), Set.of(ANNOTATION), Map.of()); var instance = createResource(Map.of(), Set.of(INSTANCE), Map.of( - PredicateDictionary.TITLE, List.of(createPrimaryTitle(3L)), + PredicateDictionary.TITLE, List.of(createPrimaryTitle(1L)), PredicateDictionary.ADMIN_METADATA, List.of(annotation), - PredicateDictionary.INSTANTIATES, List.of(work), - PredicateDictionary.MAP, List.of(lccn) + PredicateDictionary.INSTANTIATES, List.of(work) ) ); - var metadata = new FolioMetadata(instance).setSource(LINKED_DATA).setInventoryId(UUID.randomUUID().toString()); - instance.setFolioMetadata(metadata); + var folioMetadata = new FolioMetadata(instance) + .setSource(LINKED_DATA) + .setInventoryId(UUID.randomUUID().toString()); + instance.setFolioMetadata(folioMetadata); setResourceIds(instance); resourceTestService.saveGraph(instance); @@ -108,7 +107,8 @@ void should_retain_admin_metadata_outgoing_edge_of_instance() throws Exception { } private void assertAdminMetadataEdgeRetained(String id) throws Exception { - var requestBuilder = get("/graph/" + RESOURCE_URL + "/" + id) + var resourceGraphUrl = String.format(RESOURCE_URL + "/%s/graph", id); + var requestBuilder = get(resourceGraphUrl) .contentType(APPLICATION_JSON) .headers(defaultHeaders(env));