-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
...test/java/org/folio/linked/data/e2e/resource/ResourceControllerRetainOutgoingEdgesIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} |