Fixed tests and place calculation

This commit is contained in:
Raf Ponsaerts
2019-01-22 09:53:52 +01:00
parent f2c2b768f7
commit 86f3f9e9c3
7 changed files with 841 additions and 15 deletions

View File

@@ -252,6 +252,9 @@ public abstract class DSpaceObjectServiceImpl<T extends DSpaceObject> implements
} }
} }
MetadataValue metadataValue = metadataValueService.create(context, dso, metadataField); MetadataValue metadataValue = metadataValueService.create(context, dso, metadataField);
// TODO Set place to list length
metadataValue.setPlace(this.getMetadata(dso, Item.ANY, Item.ANY, Item.ANY, Item.ANY).size());
metadataValue.setLanguage(lang == null ? null : lang.trim()); metadataValue.setLanguage(lang == null ? null : lang.trim());
// Logic to set Authority and Confidence: // Logic to set Authority and Confidence:

View File

@@ -69,11 +69,13 @@ public class RelationshipServiceImpl implements RelationshipService {
public void updatePlaceInRelationship(Context context, Relationship relationship, boolean isCreation) public void updatePlaceInRelationship(Context context, Relationship relationship, boolean isCreation)
throws SQLException, AuthorizeException { throws SQLException, AuthorizeException {
Item leftItem = relationship.getLeftItem();
List<Relationship> leftRelationships = findByItemAndRelationshipType(context, List<Relationship> leftRelationships = findByItemAndRelationshipType(context,
relationship.getLeftItem(), leftItem,
relationship.getRelationshipType(), true); relationship.getRelationshipType(), true);
Item rightItem = relationship.getRightItem();
List<Relationship> rightRelationships = findByItemAndRelationshipType(context, List<Relationship> rightRelationships = findByItemAndRelationshipType(context,
relationship.getRightItem(), rightItem,
relationship.getRelationshipType(), relationship.getRelationshipType(),
false); false);
@@ -81,27 +83,32 @@ public class RelationshipServiceImpl implements RelationshipService {
if (!leftRelationships.isEmpty()) { if (!leftRelationships.isEmpty()) {
leftRelationships.sort((o1, o2) -> o2.getLeftPlace() - o1.getLeftPlace()); leftRelationships.sort((o1, o2) -> o2.getLeftPlace() - o1.getLeftPlace());
for (int i = 0; i < leftRelationships.size(); i++) { for (int i = 0; i < leftRelationships.size(); i++) {
leftRelationships.get(leftRelationships.size() - 1 - i).setLeftPlace(i + 1); leftRelationships.get(leftRelationships.size() - 1 - i).setLeftPlace(i);
} }
relationship.setLeftPlace(leftRelationships.get(0).getLeftPlace() + 1); relationship.setLeftPlace(leftRelationships.get(0).getLeftPlace() + 1);
} else { } else {
relationship.setLeftPlace(0); relationship.setLeftPlace(0);
} }
} else {
updateItem(context, leftItem);
} }
if (!virtualMetadataPopulator.isUseForPlaceTrueForRelationshipType(relationship.getRelationshipType(), false)) { if (!virtualMetadataPopulator.isUseForPlaceTrueForRelationshipType(relationship.getRelationshipType(), false)) {
if (!rightRelationships.isEmpty()) { if (!rightRelationships.isEmpty()) {
rightRelationships.sort((o1, o2) -> o2.getRightPlace() - o1.getRightPlace()); rightRelationships.sort((o1, o2) -> o2.getRightPlace() - o1.getRightPlace());
for (int i = 0; i < rightRelationships.size(); i++) { for (int i = 0; i < rightRelationships.size(); i++) {
rightRelationships.get(rightRelationships.size() - 1 - i).setRightPlace(i + 1); rightRelationships.get(rightRelationships.size() - 1 - i).setRightPlace(i);
} }
relationship.setRightPlace(rightRelationships.get(0).getRightPlace() + 1); relationship.setRightPlace(rightRelationships.get(0).getRightPlace() + 1);
} else { } else {
relationship.setRightPlace(0); relationship.setRightPlace(0);
} }
} else {
updateItem(context, rightItem);
} }
updateItems(context, relationship);
if (isCreation) { if (isCreation) {
handleCreationPlaces(context, relationship); handleCreationPlaces(context, relationship);
@@ -109,14 +116,13 @@ public class RelationshipServiceImpl implements RelationshipService {
} }
public void updateItems(Context context, Relationship relationship) public void updateItem(Context context, Item leftItem)
throws SQLException, AuthorizeException { throws SQLException, AuthorizeException {
relationship.getLeftItem().setMetadataModified(); leftItem.setMetadataModified();
relationship.getRightItem().setMetadataModified(); itemService.update(context, leftItem);
itemService.update(context, relationship.getLeftItem());
itemService.update(context, relationship.getRightItem());
} }
private void handleCreationPlaces(Context context, Relationship relationship) throws SQLException { private void handleCreationPlaces(Context context, Relationship relationship) throws SQLException {
List<Relationship> leftRelationships; List<Relationship> leftRelationships;
List<Relationship> rightRelationships; List<Relationship> rightRelationships;

View File

@@ -91,7 +91,6 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
public void updatePlaceInRelationship(Context context, Relationship relationship, boolean isCreation) public void updatePlaceInRelationship(Context context, Relationship relationship, boolean isCreation)
throws SQLException, AuthorizeException; throws SQLException, AuthorizeException;
public void updateItems(Context context, Relationship relationship) throws SQLException, AuthorizeException; public void updateItem(Context context, Item leftItem) throws SQLException, AuthorizeException;
} }

View File

@@ -120,7 +120,8 @@ public class RelationshipRestRepository extends DSpaceRestRepository<Relationshi
relationship.setRightItem(rightItem); relationship.setRightItem(rightItem);
relationship.setRelationshipType(relationshipType); relationship.setRelationshipType(relationshipType);
relationship = relationshipService.create(context, relationship); relationship = relationshipService.create(context, relationship);
relationshipService.updateItems(context, relationship); relationshipService.updateItem(context, relationship.getLeftItem());
relationshipService.updateItem(context, relationship.getRightItem());
return relationshipConverter.fromModel(relationship); return relationshipConverter.fromModel(relationship);
} else { } else {
throw new AccessDeniedException("You do not have write rights on this relationship's items"); throw new AccessDeniedException("You do not have write rights on this relationship's items");

View File

@@ -9,14 +9,21 @@ package org.dspace.app.rest;
import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.File; import java.io.File;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.dspace.app.rest.builder.CollectionBuilder; import org.dspace.app.rest.builder.CollectionBuilder;
import org.dspace.app.rest.builder.CommunityBuilder; import org.dspace.app.rest.builder.CommunityBuilder;
import org.dspace.app.rest.builder.ItemBuilder; import org.dspace.app.rest.builder.ItemBuilder;
@@ -28,9 +35,11 @@ import org.dspace.content.Collection;
import org.dspace.content.Community; import org.dspace.content.Community;
import org.dspace.content.EntityType; import org.dspace.content.EntityType;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.Relationship; import org.dspace.content.Relationship;
import org.dspace.content.RelationshipType; import org.dspace.content.RelationshipType;
import org.dspace.content.service.EntityTypeService; import org.dspace.content.service.EntityTypeService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.RelationshipService; import org.dspace.content.service.RelationshipService;
import org.dspace.content.service.RelationshipTypeService; import org.dspace.content.service.RelationshipTypeService;
import org.dspace.services.ConfigurationService; import org.dspace.services.ConfigurationService;
@@ -38,6 +47,8 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
public class RelationshipRestRepositoryIT extends AbstractControllerIntegrationTest { public class RelationshipRestRepositoryIT extends AbstractControllerIntegrationTest {
@@ -53,6 +64,9 @@ public class RelationshipRestRepositoryIT extends AbstractControllerIntegrationT
@Autowired @Autowired
private ConfigurationService configurationService; private ConfigurationService configurationService;
@Autowired
private ItemService itemService;
@Before @Before
public void setup() throws Exception { public void setup() throws Exception {
@@ -188,4 +202,799 @@ public class RelationshipRestRepositoryIT extends AbstractControllerIntegrationT
))) )))
; ;
} }
@Test
public void addRelationshipsAndMetadataToValidatePlaceTest() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
Item author1 = ItemBuilder.createItem(context, col1)
.withTitle("Author1")
.withIssueDate("2017-10-17")
.withPersonIdentifierFirstName("Donald")
.withPersonIdentifierLastName("Smith")
.withRelationshipType("Person")
.build();
Item author2 = ItemBuilder.createItem(context, col2)
.withTitle("Author2")
.withIssueDate("2016-02-13")
.withPersonIdentifierFirstName("Maria")
.withPersonIdentifierLastName("Smith")
.withRelationshipType("Person")
.build();
Item author3 = ItemBuilder.createItem(context, col2)
.withTitle("Author3")
.withIssueDate("2016-02-13")
.withPersonIdentifierFirstName("Maybe")
.withPersonIdentifierLastName("Maybe")
.withRelationshipType("Person")
.build();
Item publication = ItemBuilder.createItem(context, col3)
.withTitle("Publication1")
.withIssueDate("2015-01-01")
.withRelationshipType("Publication")
.build();
RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService
.findbyTypesAndLabels(context, entityTypeService.findByEntityType(context, "Publication"),
entityTypeService.findByEntityType(context, "Person"),
"isAuthorOfPublication", "isPublicationOfAuthor");
String adminToken = getAuthToken(admin.getEmail(), password);
MvcResult mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", publication.getID().toString())
.param("rightItem", author1.getID().toString())
.param("relationshipType",
isAuthorOfPublicationRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
ObjectMapper mapper = new ObjectMapper();
String content = mvcResult.getResponse().getContentAsString();
Map<String, Object> map = mapper.readValue(content, Map.class);
String firstRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(0)));
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text");
itemService.update(context, publication);
List<MetadataValue> list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text")) {
assertEquals(1, mdv.getPlace());
}
}
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(0)));
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", publication.getID().toString())
.param("rightItem", author2.getID().toString())
.param("relationshipType",
isAuthorOfPublicationRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
content = mvcResult.getResponse().getContentAsString();
map = mapper.readValue(content, Map.class);
String secondRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(2)));
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text two");
// itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text three");
itemService.update(context, publication);
// itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text four");
// itemService.update(context, publication);
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text two")) {
assertEquals(3, mdv.getPlace());
}
}
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", publication.getID().toString())
.param("rightItem", author3.getID().toString())
.param("relationshipType",
isAuthorOfPublicationRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
content = mvcResult.getResponse().getContentAsString();
map = mapper.readValue(content, Map.class);
String thirdRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(4)));
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text three");
itemService.update(context, publication);
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text three")) {
assertEquals(5, mdv.getPlace());
}
}
}
@Test
public void deleteMetadataValueAndValidatePlace() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
Item author1 = ItemBuilder.createItem(context, col1)
.withTitle("Author1")
.withIssueDate("2017-10-17")
.withPersonIdentifierFirstName("Donald")
.withPersonIdentifierLastName("Smith")
.withRelationshipType("Person")
.build();
Item author2 = ItemBuilder.createItem(context, col2)
.withTitle("Author2")
.withIssueDate("2016-02-13")
.withPersonIdentifierFirstName("Maria")
.withPersonIdentifierLastName("Smith")
.withRelationshipType("Person")
.build();
Item author3 = ItemBuilder.createItem(context, col2)
.withTitle("Author3")
.withIssueDate("2016-02-13")
.withPersonIdentifierFirstName("Maybe")
.withPersonIdentifierLastName("Maybe")
.withRelationshipType("Person")
.build();
Item publication = ItemBuilder.createItem(context, col3)
.withTitle("Publication1")
.withIssueDate("2015-01-01")
.withRelationshipType("Publication")
.build();
RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService
.findbyTypesAndLabels(context, entityTypeService.findByEntityType(context, "Publication"),
entityTypeService.findByEntityType(context, "Person"),
"isAuthorOfPublication", "isPublicationOfAuthor");
String adminToken = getAuthToken(admin.getEmail(), password);
MvcResult mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", publication.getID().toString())
.param("rightItem", author1.getID().toString())
.param("relationshipType",
isAuthorOfPublicationRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
ObjectMapper mapper = new ObjectMapper();
String content = mvcResult.getResponse().getContentAsString();
Map<String, Object> map = mapper.readValue(content, Map.class);
String firstRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(0)));
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text");
itemService.update(context, publication);
List<MetadataValue> list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text")) {
assertEquals(1, mdv.getPlace());
}
}
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(0)));
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", publication.getID().toString())
.param("rightItem", author2.getID().toString())
.param("relationshipType",
isAuthorOfPublicationRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
content = mvcResult.getResponse().getContentAsString();
map = mapper.readValue(content, Map.class);
String secondRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(2)));
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text two");
// itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text three");
itemService.update(context, publication);
// itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text four");
// itemService.update(context, publication);
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text two")) {
assertEquals(3, mdv.getPlace());
}
}
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", publication.getID().toString())
.param("rightItem", author3.getID().toString())
.param("relationshipType",
isAuthorOfPublicationRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
content = mvcResult.getResponse().getContentAsString();
map = mapper.readValue(content, Map.class);
String thirdRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(4)));
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text three");
itemService.update(context, publication);
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text three")) {
assertEquals(5, mdv.getPlace());
}
}
List<MetadataValue> authors = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
List<MetadataValue> listToRemove = new LinkedList<>();
for (MetadataValue metadataValue : authors) {
if (StringUtils.equals(metadataValue.getValue(), "plain text two")) {
listToRemove.add(metadataValue);
}
}
itemService.removeMetadataValues(context, publication, listToRemove);
itemService.update(context, publication);
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text")) {
assertEquals(1, mdv.getPlace());
}
}
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text three")) {
assertEquals(4, mdv.getPlace());
}
}
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(0)));
getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(2)));
getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(3)));
}
@Test
public void deleteRelationshipsAndValidatePlace() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
Item author1 = ItemBuilder.createItem(context, col1)
.withTitle("Author1")
.withIssueDate("2017-10-17")
.withPersonIdentifierFirstName("Donald")
.withPersonIdentifierLastName("Smith")
.withRelationshipType("Person")
.build();
Item author2 = ItemBuilder.createItem(context, col2)
.withTitle("Author2")
.withIssueDate("2016-02-13")
.withPersonIdentifierFirstName("Maria")
.withPersonIdentifierLastName("Smith")
.withRelationshipType("Person")
.build();
Item author3 = ItemBuilder.createItem(context, col2)
.withTitle("Author3")
.withIssueDate("2016-02-13")
.withPersonIdentifierFirstName("Maybe")
.withPersonIdentifierLastName("Maybe")
.withRelationshipType("Person")
.build();
Item publication = ItemBuilder.createItem(context, col3)
.withTitle("Publication1")
.withIssueDate("2015-01-01")
.withRelationshipType("Publication")
.build();
RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService
.findbyTypesAndLabels(context, entityTypeService.findByEntityType(context, "Publication"),
entityTypeService.findByEntityType(context, "Person"),
"isAuthorOfPublication", "isPublicationOfAuthor");
String adminToken = getAuthToken(admin.getEmail(), password);
MvcResult mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", publication.getID().toString())
.param("rightItem", author1.getID().toString())
.param("relationshipType",
isAuthorOfPublicationRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
ObjectMapper mapper = new ObjectMapper();
String content = mvcResult.getResponse().getContentAsString();
Map<String, Object> map = mapper.readValue(content, Map.class);
String firstRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(0)));
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text");
itemService.update(context, publication);
List<MetadataValue> list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text")) {
assertEquals(1, mdv.getPlace());
}
}
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(0)));
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", publication.getID().toString())
.param("rightItem", author2.getID().toString())
.param("relationshipType",
isAuthorOfPublicationRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
content = mvcResult.getResponse().getContentAsString();
map = mapper.readValue(content, Map.class);
String secondRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(2)));
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text two");
itemService.update(context, publication);
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text two")) {
assertEquals(3, mdv.getPlace());
}
}
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", publication.getID().toString())
.param("rightItem", author3.getID().toString())
.param("relationshipType",
isAuthorOfPublicationRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
content = mvcResult.getResponse().getContentAsString();
map = mapper.readValue(content, Map.class);
String thirdRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(4)));
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text three");
itemService.update(context, publication);
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text three")) {
assertEquals(5, mdv.getPlace());
}
}
getClient(adminToken).perform(delete("/api/core/relationships/" + secondRelationshipIdString));
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(0)));
publication = itemService.find(context, publication.getID());
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text")) {
assertEquals(1, mdv.getPlace());
}
}
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text two")) {
assertEquals(2, mdv.getPlace());
}
}
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text three")) {
assertEquals(4, mdv.getPlace());
}
}
getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(3)));
}
@Test
public void addRelationshipsNotUseForPlace() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
Item author1 = ItemBuilder.createItem(context, col1)
.withTitle("Author1")
.withIssueDate("2017-10-17")
.withAuthor("Smith, Donald")
.withRelationshipType("Person")
.build();
Item author2 = ItemBuilder.createItem(context, col2)
.withTitle("Author2")
.withIssueDate("2016-02-13")
.withAuthor("Smith, Maria")
.withRelationshipType("Person")
.build();
Item author3 = ItemBuilder.createItem(context, col2)
.withTitle("Author3")
.withIssueDate("2016-02-13")
.withAuthor("Maybe, Maybe")
.withRelationshipType("Person")
.build();
Item orgUnit1 = ItemBuilder.createItem(context, col3)
.withTitle("OrgUnit1")
.withIssueDate("2015-01-01")
.withRelationshipType("OrgUnit")
.build();
RelationshipType isOrgUnitOfPersonRelationshipType = relationshipTypeService
.findbyTypesAndLabels(context, entityTypeService.findByEntityType(context, "Person"),
entityTypeService.findByEntityType(context, "OrgUnit"),
"isOrgUnitOfPerson", "isPersonOfOrgUnit");
String adminToken = getAuthToken(admin.getEmail(), password);
MvcResult mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", author1.getID().toString())
.param("rightItem", orgUnit1.getID().toString())
.param("relationshipType",
isOrgUnitOfPersonRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
ObjectMapper mapper = new ObjectMapper();
String content = mvcResult.getResponse().getContentAsString();
Map<String, Object> map = mapper.readValue(content, Map.class);
String firstRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("rightPlace", is(0)));
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", author2.getID().toString())
.param("rightItem", orgUnit1.getID().toString())
.param("relationshipType",
isOrgUnitOfPersonRelationshipType.getID().toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
mapper = new ObjectMapper();
content = mvcResult.getResponse().getContentAsString();
map = mapper.readValue(content, Map.class);
String secondRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("rightPlace", is(1)));
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", author3.getID().toString())
.param("rightItem", orgUnit1.getID().toString())
.param("relationshipType",
isOrgUnitOfPersonRelationshipType.getID().toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
mapper = new ObjectMapper();
content = mvcResult.getResponse().getContentAsString();
map = mapper.readValue(content, Map.class);
String thirdRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("rightPlace", is(2)));
}
@Test
public void addAndDeleteRelationshipsNotUseForPlace() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
Item author1 = ItemBuilder.createItem(context, col1)
.withTitle("Author1")
.withIssueDate("2017-10-17")
.withAuthor("Smith, Donald")
.withRelationshipType("Person")
.build();
Item author2 = ItemBuilder.createItem(context, col2)
.withTitle("Author2")
.withIssueDate("2016-02-13")
.withAuthor("Smith, Maria")
.withRelationshipType("Person")
.build();
Item author3 = ItemBuilder.createItem(context, col2)
.withTitle("Author3")
.withIssueDate("2016-02-13")
.withAuthor("Maybe, Maybe")
.withRelationshipType("Person")
.build();
Item orgUnit1 = ItemBuilder.createItem(context, col3)
.withTitle("OrgUnit1")
.withIssueDate("2015-01-01")
.withRelationshipType("OrgUnit")
.build();
RelationshipType isOrgUnitOfPersonRelationshipType = relationshipTypeService
.findbyTypesAndLabels(context, entityTypeService.findByEntityType(context, "Person"),
entityTypeService.findByEntityType(context, "OrgUnit"),
"isOrgUnitOfPerson", "isPersonOfOrgUnit");
String adminToken = getAuthToken(admin.getEmail(), password);
MvcResult mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", author1.getID().toString())
.param("rightItem", orgUnit1.getID().toString())
.param("relationshipType",
isOrgUnitOfPersonRelationshipType.getID()
.toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
ObjectMapper mapper = new ObjectMapper();
String content = mvcResult.getResponse().getContentAsString();
Map<String, Object> map = mapper.readValue(content, Map.class);
String firstRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("rightPlace", is(0)));
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", author2.getID().toString())
.param("rightItem", orgUnit1.getID().toString())
.param("relationshipType",
isOrgUnitOfPersonRelationshipType.getID().toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
mapper = new ObjectMapper();
content = mvcResult.getResponse().getContentAsString();
map = mapper.readValue(content, Map.class);
String secondRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("rightPlace", is(1)));
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", author3.getID().toString())
.param("rightItem", orgUnit1.getID().toString())
.param("relationshipType",
isOrgUnitOfPersonRelationshipType.getID().toString())
.contentType(MediaType.APPLICATION_JSON).content(
"{\"id\":530,\"leftId\":\"77877343-3f75-4c33-9492" +
"-6ed7c98ed84e\",\"relationshipTypeId\":0,\"rightId\":\"423d0eda-b808-4b87-97ae-b85fe9d59418\"," +
"\"leftPlace\":1,\"rightPlace\":1}"))
.andExpect(status().isCreated())
.andReturn();
mapper = new ObjectMapper();
content = mvcResult.getResponse().getContentAsString();
map = mapper.readValue(content, Map.class);
String thirdRelationshipIdString = String.valueOf(map.get("id"));
getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("rightPlace", is(2)));
getClient(adminToken).perform(delete("/api/core/relationships/" + secondRelationshipIdString));
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("rightPlace", is(0)));
getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("rightPlace", is(1)));
}
} }

View File

@@ -64,6 +64,14 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
return addMetadataValue(item, MetadataSchemaEnum.DC.getName(), "contributor", "author", authorName); return addMetadataValue(item, MetadataSchemaEnum.DC.getName(), "contributor", "author", authorName);
} }
public ItemBuilder withPersonIdentifierFirstName(final String personIdentifierFirstName) {
return addMetadataValue(item, "person", "identifier", "firstname", personIdentifierFirstName);
}
public ItemBuilder withPersonIdentifierLastName(final String personIdentifierLastName) {
return addMetadataValue(item, "person", "identifier", "lastname", personIdentifierLastName);
}
public ItemBuilder withSubject(final String subject) { public ItemBuilder withSubject(final String subject) {
return addMetadataValue(item, MetadataSchemaEnum.DC.getName(), "subject", null, subject); return addMetadataValue(item, MetadataSchemaEnum.DC.getName(), "subject", null, subject);
} }

View File

@@ -149,13 +149,13 @@ public class CsvImportIT extends AbstractControllerIntegrationTest {
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.leftPlace", is(0))) .andExpect(jsonPath("$.leftPlace", is(0)))
.andExpect(jsonPath("$.rightId", is(itemC.getID().toString()))) .andExpect(jsonPath("$.rightId", is(itemC.getID().toString())))
.andExpect(jsonPath("$.rightPlace", is(2))) .andExpect(jsonPath("$.rightPlace", is(1)))
.andExpect(jsonPath("$", Matchers.is(RelationshipMatcher.matchRelationship(relationships.get(0))))); .andExpect(jsonPath("$", Matchers.is(RelationshipMatcher.matchRelationship(relationships.get(0)))));
getClient().perform(get("/api/core/relationships/" + relationships.get(1).getID().toString())) getClient().perform(get("/api/core/relationships/" + relationships.get(1).getID().toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.leftPlace", is(1))) .andExpect(jsonPath("$.leftPlace", is(1)))
.andExpect(jsonPath("$.rightId", is(itemB.getID().toString()))) .andExpect(jsonPath("$.rightId", is(itemB.getID().toString())))
.andExpect(jsonPath("$.rightPlace", is(2))) .andExpect(jsonPath("$.rightPlace", is(1)))
.andExpect(jsonPath("$", Matchers.is(RelationshipMatcher.matchRelationship(relationships.get(1))))); .andExpect(jsonPath("$", Matchers.is(RelationshipMatcher.matchRelationship(relationships.get(1)))));
Item itemF = validateSpecificItemRelationCreationCsvImport(col1, itemE, "TestItemF", "Person", Item itemF = validateSpecificItemRelationCreationCsvImport(col1, itemE, "TestItemF", "Person",