Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pkjacob committed Dec 2, 2024
1 parent d6ecd2b commit 37622e9
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
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;
import static org.folio.linked.data.model.entity.ResourceSource.LINKED_DATA;
import static org.folio.linked.data.test.MonographTestUtil.createPrimaryTitle;
import static org.folio.linked.data.test.MonographTestUtil.createResource;
import static org.folio.linked.data.test.MonographTestUtil.getSampleWork;
import static org.folio.linked.data.test.TestUtil.INSTANCE_WITH_WORK_REF_SAMPLE;
import static org.folio.linked.data.test.TestUtil.OBJECT_MAPPER;
import static org.folio.linked.data.test.TestUtil.SIMPLE_WORK_WITH_TITLE_SAMPLE;
import static org.folio.linked.data.test.TestUtil.defaultHeaders;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.folio.ld.dictionary.PredicateDictionary;
import org.folio.linked.data.domain.dto.InstanceResponseField;
import org.folio.linked.data.domain.dto.ResourceResponseDto;
import org.folio.linked.data.domain.dto.WorkResponseField;
import org.folio.linked.data.e2e.base.IntegrationTest;
import org.folio.linked.data.model.entity.FolioMetadata;
import org.folio.linked.data.model.entity.Resource;
import org.folio.linked.data.model.entity.ResourceEdge;
import org.folio.linked.data.service.resource.hash.HashService;
import org.folio.linked.data.test.ResourceTestService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.test.web.servlet.MockMvc;

@IntegrationTest
class ResourceControllerRetainOutgoingEdgesIT {
public static final String RESOURCE_URL = "/resource";
private static final String WORK_ID_PLACEHOLDER = "%WORK_ID%";
@Autowired
private ResourceTestService resourceTestService;
@Autowired
private Environment env;
@Autowired
private MockMvc mockMvc;
@Autowired
private HashService hashService;

@Test
void should_retain_admin_metadata_outgoing_edge_of_work() 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.ADMIN_METADATA, List.of(annotation),
PredicateDictionary.CREATOR, List.of(creatorPerson),
PredicateDictionary.DESIGNER_OF_BOOK, List.of(creatorPerson)
)
);
setResourceIds(work);
var saved = resourceTestService.saveGraph(work);

// when
var updatedResource = updateResource(work.getId(), SIMPLE_WORK_WITH_TITLE_SAMPLE);
var newWorkId = ((WorkResponseField) updatedResource.getResource()).getWork().getId();

// then
assertAdminMetadataEdgeRetained(newWorkId);
}

@Test
void should_retain_admin_metadata_outgoing_edge_of_instance() 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.ADMIN_METADATA, List.of(annotation),
PredicateDictionary.INSTANTIATES, List.of(work),
PredicateDictionary.MAP, List.of(lccn)
)
);
var metadata = new FolioMetadata(instance).setSource(LINKED_DATA).setInventoryId(UUID.randomUUID().toString());
instance.setFolioMetadata(metadata);
setResourceIds(instance);
resourceTestService.saveGraph(instance);

// when
var updatedResource = updateResource(instance.getId(),
INSTANCE_WITH_WORK_REF_SAMPLE.replaceAll(WORK_ID_PLACEHOLDER, work.getId().toString()));
var newInstanceId = ((InstanceResponseField) updatedResource.getResource()).getInstance().getId();

// then
assertAdminMetadataEdgeRetained(newInstanceId);
}

private void assertAdminMetadataEdgeRetained(String id) throws Exception {
var requestBuilder = get("/graph/" + RESOURCE_URL + "/" + id)
.contentType(APPLICATION_JSON)
.headers(defaultHeaders(env));

mockMvc.perform(requestBuilder)
.andExpect(status().isOk())
.andExpect(jsonPath("$.outgoingEdges.edges['http://bibfra.me/vocab/marc/adminMetadata']").isArray())
.andExpect(jsonPath("$.outgoingEdges.edges['http://bibfra.me/vocab/marc/adminMetadata'].length()").value(1));
}


private ResourceResponseDto updateResource(Long id, String payload) throws Exception {
var updateRequest = put(RESOURCE_URL + "/" + id)
.contentType(APPLICATION_JSON)
.headers(defaultHeaders(env))
.content(payload);
var response = mockMvc.perform(updateRequest)
.andExpect(status().isOk())
.andReturn()
.getResponse().getContentAsString();
return OBJECT_MAPPER.readValue(response, ResourceResponseDto.class);
}

private void setResourceIds(Resource resource) {
resource.setId(hashService.hash(resource));
resource.getOutgoingEdges()
.stream()
.map(ResourceEdge::getTarget)
.forEach(this::setResourceIds);
}
}
3 changes: 3 additions & 0 deletions src/test/java/org/folio/linked/data/test/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public class TestUtil {
public static final String INSTANCE_WITH_WORK_REF_SAMPLE = loadResourceAsString("samples/instance_and_work_ref.json");
public static final String WORK_WITH_INSTANCE_REF_SAMPLE = loadResourceAsString("samples/work_and_instance_ref.json");
public static final String SIMPLE_WORK_WITH_INSTANCE_REF_SAMPLE = loadResourceAsString("samples/simple_work.json");
public static final String SIMPLE_WORK_WITH_TITLE_SAMPLE =
loadResourceAsString("samples/simple_work_just_title.json");

private static final EasyRandomParameters PARAMETERS = new EasyRandomParameters();
private static final EasyRandom GENERATOR = new EasyRandom(PARAMETERS);
private static final String FOLIO_OKAPI_URL = "folio.okapi-url";
Expand Down
24 changes: 24 additions & 0 deletions src/test/resources/samples/simple_work_just_title.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"resource": {
"http://bibfra.me/vocab/lite/Work": {
"http://bibfra.me/vocab/marc/title": [
{
"http://bibfra.me/vocab/marc/Title": {
"http://bibfra.me/vocab/bflc/nonSortNum": [
"4"
],
"http://bibfra.me/vocab/marc/mainTitle": [
"simple_work"
],
"http://bibfra.me/vocab/marc/partNumber": [
"1"
],
"http://bibfra.me/vocab/marc/partName": [
"part 1"
]
}
}
]
}
}
}

0 comments on commit 37622e9

Please sign in to comment.