[Task 70419] fixed a bug where the RelationshipService creation did not check the max cardinality properly

This commit is contained in:
Raf Ponsaerts
2020-04-20 17:18:27 +02:00
parent 6c91e98f48
commit c7f6a46296
3 changed files with 61 additions and 5 deletions

View File

@@ -202,14 +202,14 @@ public class RelationshipServiceImpl implements RelationshipService {
return false;
}
if (!verifyMaxCardinality(context, relationship.getLeftItem(),
relationshipType.getLeftMaxCardinality(), relationshipType)) {
relationshipType.getLeftMaxCardinality(), relationshipType, true)) {
log.warn("The relationship has been deemed invalid since the left item has more" +
" relationships than the left max cardinality allows after we'd store this relationship");
logRelationshipTypeDetailsForError(relationshipType);
return false;
}
if (!verifyMaxCardinality(context, relationship.getRightItem(),
relationshipType.getRightMaxCardinality(), relationshipType)) {
relationshipType.getRightMaxCardinality(), relationshipType, false)) {
log.warn("The relationship has been deemed invalid since the right item has more" +
" relationships than the right max cardinality allows after we'd store this relationship");
logRelationshipTypeDetailsForError(relationshipType);
@@ -232,9 +232,10 @@ public class RelationshipServiceImpl implements RelationshipService {
private boolean verifyMaxCardinality(Context context, Item itemToProcess,
Integer maxCardinality,
RelationshipType relationshipType) throws SQLException {
RelationshipType relationshipType,
boolean isLeft) throws SQLException {
List<Relationship> rightRelationships = findByItemAndRelationshipType(context, itemToProcess, relationshipType,
false);
isLeft);
if (maxCardinality != null && rightRelationships.size() >= maxCardinality) {
return false;
}

View File

@@ -2629,5 +2629,60 @@ public class RelationshipRestRepositoryIT extends AbstractEntityIntegrationTest
.andExpect(jsonPath("$.page", PageMatcher.pageEntryWithTotalPagesAndElements(0, 1, 1, 1)));
}
@Test
public void orgUnitLeftMaxCardinalityTest() throws Exception {
context.turnOffAuthorisationSystem();
EntityType orgUnit = entityTypeService.findByEntityType(context, "OrgUnit");
RelationshipType isParentOrgUnitOf = relationshipTypeService
.findbyTypesAndTypeName(context, orgUnit, orgUnit, "isParentOrgUnitOf", "isChildOrgUnitOf");
MetadataSchema metadataSchema = metadataSchemaService.find(context, "relation");
MetadataFieldBuilder.createMetadataField(context, metadataSchema, "isParentOrgUnitOf", null, null).build();
MetadataFieldBuilder.createMetadataField(context, metadataSchema, "isChildOrgUnitOf", null, null).build();
String adminToken = getAuthToken(admin.getEmail(), password);
context.restoreAuthSystemState();
// Here we create our first Relationship to the Publication to give it a dc.contributor.author virtual
// metadata field.
getClient(adminToken).perform(post("/api/core/relationships")
.param("relationshipType",
isParentOrgUnitOf.getID().toString())
.contentType(MediaType.parseMediaType
(org.springframework.data.rest.webmvc.RestMediaTypes
.TEXT_URI_LIST_VALUE))
.content(
"https://localhost:8080/server/api/core/items/" + orgUnit1
.getID() + "\n" +
"https://localhost:8080/server/api/core/items" +
"/" + orgUnit2
.getID()))
.andExpect(status().isCreated());
getClient(adminToken).perform(post("/api/core/relationships")
.param("relationshipType",
isParentOrgUnitOf.getID().toString())
.contentType(MediaType.parseMediaType
(org.springframework.data.rest.webmvc.RestMediaTypes
.TEXT_URI_LIST_VALUE))
.content(
"https://localhost:8080/server/api/core/items/" + orgUnit1
.getID() + "\n" +
"https://localhost:8080/server/api/core/items" +
"/" + orgUnit3
.getID()))
.andExpect(status().isBadRequest());
getClient().perform(get("/api/core/relationships/search/byLabel")
.param("label", "isParentOrgUnitOf")
.param("dso", String.valueOf(orgUnit1.getID()))
.param("page", "0"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.page", PageMatcher.pageEntryWithTotalPagesAndElements(0, 20, 1, 1)));
}
}

View File

@@ -105,7 +105,7 @@ public class AbstractEntityIntegrationTest extends AbstractControllerIntegration
RelationshipTypeBuilder.createRelationshipTypeBuilder(context, orgUnit, orgUnit, "isParentOrgUnitOf",
"isChildOrgUnitOf", null,
null, null, null)
1, null, null)
.build();
context.restoreAuthSystemState();