Skip to content

Commit

Permalink
MODLD-593: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pkjacob committed Dec 3, 2024
1 parent 37622e9 commit e290d65
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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<ResourceEdge> 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<ResourceTypeDictionary, Predicate<ResourceEdge>> 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
);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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);

Expand All @@ -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));

Expand Down

0 comments on commit e290d65

Please sign in to comment.