From 91e2421d7c237b45f2723f2d7ebaf13c966f79fa Mon Sep 17 00:00:00 2001 From: George Evangelopoulos Date: Tue, 27 Sep 2022 16:45:55 +0100 Subject: [PATCH] ACS-3291: Support for unlinking rule sets (#1421) * ACS-3291: Support for unlinking rule sets * ACS-3291: add unit tests and edge case exceptions * ACS-3291: add support for -default- ruleSetId Co-authored-by: Tom Page --- .../java/org/alfresco/rest/api/RuleSets.java | 5 +++ .../rest/api/impl/rules/RuleSetsImpl.java | 17 +++++++++ .../api/nodes/NodeRuleSetLinksRelation.java | 24 +++++++++++-- .../rest/api/impl/rules/RuleSetsImplTest.java | 35 +++++++++++++++++++ .../api/nodes/NodeRuleSetsRelationTest.java | 13 +++++++ 5 files changed, 92 insertions(+), 2 deletions(-) diff --git a/remote-api/src/main/java/org/alfresco/rest/api/RuleSets.java b/remote-api/src/main/java/org/alfresco/rest/api/RuleSets.java index 4fc5a28ec0..e77766b403 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/RuleSets.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/RuleSets.java @@ -63,4 +63,9 @@ public interface RuleSets * Link a rule set to a folder */ RuleSetLink linkToRuleSet(String folderNodeId, String linkToNodeId); + + /** + * Removes the link between a rule set and a folder + */ + void unlinkRuleSet(String folderNodeId, String ruleSetId); } diff --git a/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetsImpl.java b/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetsImpl.java index a2ebeb8061..0a55148fc6 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetsImpl.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetsImpl.java @@ -41,6 +41,7 @@ import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.resource.parameters.ListPage; import org.alfresco.rest.framework.resource.parameters.Paging; import org.alfresco.service.Experimental; +import org.alfresco.service.cmr.repository.AspectMissingException; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.rule.RuleService; @@ -110,6 +111,22 @@ public class RuleSetsImpl implements RuleSets return ruleSetLink; } + @Override + public void unlinkRuleSet(String folderNodeId, String ruleSetId) + { + final NodeRef folderNodeRef = validator.validateFolderNode(folderNodeId,true); + final NodeRef ruleSetNodeRef = validator.validateRuleSetNode(ruleSetId, folderNodeRef); + + //The folder should be linked to a rule set + if (!ruleService.isLinkedToRuleNode(folderNodeRef)) + { + throw new InvalidArgumentException("The folder is not linked to a rule set."); + } + + //The following line also handles the deletion of the parent-child association that gets created during linking + nodeService.removeAspect(folderNodeRef,RuleModel.ASPECT_RULES); + } + public void setRuleSetLoader(RuleSetLoader ruleSetLoader) { this.ruleSetLoader = ruleSetLoader; diff --git a/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeRuleSetLinksRelation.java b/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeRuleSetLinksRelation.java index 96d7466c49..be1cc86bbb 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeRuleSetLinksRelation.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeRuleSetLinksRelation.java @@ -35,6 +35,7 @@ import org.alfresco.rest.api.model.rules.RuleSetLink; import org.alfresco.rest.framework.WebApiDescription; import org.alfresco.rest.framework.WebApiParam; import org.alfresco.rest.framework.core.ResourceParameter; +import org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException; import org.alfresco.rest.framework.resource.RelationshipResource; import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction; import org.alfresco.rest.framework.resource.parameters.Parameters; @@ -42,8 +43,9 @@ import org.alfresco.util.PropertyCheck; import org.springframework.beans.factory.InitializingBean; -@RelationshipResource(name = "rule-set-links", entityResource = NodesEntityResource.class, title = "Linking to a rule set") -public class NodeRuleSetLinksRelation implements InitializingBean, RelationshipResourceAction.Create +@RelationshipResource(name = "rule-set-links", entityResource = NodesEntityResource.class, title = "Rule set links") +public class NodeRuleSetLinksRelation implements InitializingBean, RelationshipResourceAction.Create, + RelationshipResourceAction.Delete { private final RuleSets ruleSets; @@ -67,6 +69,24 @@ public class NodeRuleSetLinksRelation implements InitializingBean, RelationshipR .collect(Collectors.toList()); } + /** + * Remove link between a rule set and a folder for given rule set's and folder's node IDs. + *

+ * - DELETE /nodes/{folderNodeId}/rule-set-links/{ruleSetId} + * + * @param folderNodeId - folder node ID + * @param ruleSetNodeId - rule set node ID (associated with folder node) + * @throws RelationshipResourceNotFoundException in case resource was not found + */ + @WebApiDescription(title = "Remove link between a rule set and a folder node", + description = "Submits a request to unlink a rule set from a folder", + successStatus = HttpServletResponse.SC_NO_CONTENT) + @Override + public void delete(String folderNodeId, String ruleSetNodeId, Parameters parameters) + { + ruleSets.unlinkRuleSet(folderNodeId, ruleSetNodeId); + } + public NodeRuleSetLinksRelation(RuleSets ruleSets) { this.ruleSets = ruleSets; diff --git a/remote-api/src/test/java/org/alfresco/rest/api/impl/rules/RuleSetsImplTest.java b/remote-api/src/test/java/org/alfresco/rest/api/impl/rules/RuleSetsImplTest.java index 026c1d2ef5..86951183d5 100644 --- a/remote-api/src/test/java/org/alfresco/rest/api/impl/rules/RuleSetsImplTest.java +++ b/remote-api/src/test/java/org/alfresco/rest/api/impl/rules/RuleSetsImplTest.java @@ -46,6 +46,7 @@ import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.resource.parameters.Paging; import org.alfresco.service.Experimental; +import org.alfresco.service.cmr.repository.AspectMissingException; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; @@ -294,4 +295,38 @@ public class RuleSetsImplTest extends TestCase then(ruleServiceMock).shouldHaveNoMoreInteractions(); then(runtimeRuleServiceMock).shouldHaveNoInteractions(); } + + @Test + public void testUnlinkRuleSet() + { + given(ruleServiceMock.isLinkedToRuleNode(FOLDER_NODE)).willReturn(true); + + //when + ruleSets.unlinkRuleSet(FOLDER_ID,RULE_SET_ID); + + then(nodeValidatorMock).should().validateFolderNode(FOLDER_ID,true); + then(nodeValidatorMock).should().validateRuleSetNode(RULE_SET_ID,FOLDER_NODE); + then(nodeValidatorMock).shouldHaveNoMoreInteractions(); + then(ruleServiceMock).should().isLinkedToRuleNode(FOLDER_NODE); + then(ruleServiceMock).shouldHaveNoMoreInteractions(); + then(nodeServiceMock).should().removeAspect(FOLDER_NODE,RuleModel.ASPECT_RULES); + then(nodeServiceMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void testUnlinkRuleSet_folderIsNotLinkedToRuleSet() + { + given(ruleServiceMock.isLinkedToRuleNode(FOLDER_NODE)).willReturn(false); + + //when + assertThatExceptionOfType(InvalidArgumentException.class).isThrownBy( + () -> ruleSets.unlinkRuleSet(FOLDER_ID,RULE_SET_ID)); + + then(nodeValidatorMock).should().validateFolderNode(FOLDER_ID,true); + then(nodeValidatorMock).should().validateRuleSetNode(RULE_SET_ID,FOLDER_NODE); + then(nodeValidatorMock).shouldHaveNoMoreInteractions(); + then(ruleServiceMock).should().isLinkedToRuleNode(FOLDER_NODE); + then(ruleServiceMock).shouldHaveNoMoreInteractions(); + then(nodeServiceMock).shouldHaveNoInteractions(); + } } diff --git a/remote-api/src/test/java/org/alfresco/rest/api/nodes/NodeRuleSetsRelationTest.java b/remote-api/src/test/java/org/alfresco/rest/api/nodes/NodeRuleSetsRelationTest.java index 2125d215bd..d9dc0af8f3 100644 --- a/remote-api/src/test/java/org/alfresco/rest/api/nodes/NodeRuleSetsRelationTest.java +++ b/remote-api/src/test/java/org/alfresco/rest/api/nodes/NodeRuleSetsRelationTest.java @@ -26,6 +26,8 @@ package org.alfresco.rest.api.nodes; +import static org.mockito.BDDMockito.then; + import junit.framework.TestCase; import org.alfresco.rest.api.RuleSets; import org.alfresco.rest.api.model.rules.RuleSetLink; @@ -46,6 +48,7 @@ public class NodeRuleSetsRelationTest extends TestCase { private static final String FOLDER_NODE_ID = "dummy-folder-node-id"; private static final String LINK_TO_NODE_ID = "dummy-link-to-node-id"; + private static final String RULE_SET_NODE_ID = "dummy-rule-set-node-id"; @Mock private RuleSets ruleSets; @@ -71,5 +74,15 @@ public class NodeRuleSetsRelationTest extends TestCase Assert.assertEquals(ruleResult, actual); } + @Test + public void testUnlinkRuleSet() + { + //when + ruleSets.unlinkRuleSet(FOLDER_NODE_ID,RULE_SET_NODE_ID); + + then(ruleSets).should().unlinkRuleSet(FOLDER_NODE_ID,RULE_SET_NODE_ID); + then(ruleSets).shouldHaveNoMoreInteractions(); + } + }