ACS-3225 REST API to create one or more rules. (#1216) [tas]

* ACS-3225: Folder Rules v1 REST API - Create Rule

* ACS-3225 Create one or more rules within a folder/rule set.

* ACS-3225 Update ExtendedRuleServiceImpl in AGS to match new interface.

* ACS-3225 Code review fixes.

Make RuleBuilder an inner class, add documentation to API and rename method to createRules.

Co-authored-by: Krystian Dabrowski <krystian.dabrowski@hyland.com>
This commit is contained in:
Tom Page
2022-07-20 10:23:30 +01:00
committed by GitHub
parent 06f0f181df
commit 84f7726cbd
9 changed files with 317 additions and 87 deletions

View File

@@ -156,25 +156,24 @@ public class ExtendedRuleServiceImpl extends RuleServiceImpl
* @see org.alfresco.repo.rule.RuleServiceImpl#saveRule(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.rule.Rule)
*/
@Override
public void saveRule(final NodeRef nodeRef, final Rule rule)
public Rule saveRule(final NodeRef nodeRef, final Rule rule)
{
validateWormLockRuleAction(rule);
if (filePlanService.isFilePlanComponent(nodeRef))
{
AuthenticationUtil.runAsSystem(new RunAsWork<Void>()
return AuthenticationUtil.runAsSystem(new RunAsWork<Rule>()
{
@Override
public Void doWork()
public Rule doWork()
{
ExtendedRuleServiceImpl.super.saveRule(nodeRef, rule);
return null;
return ExtendedRuleServiceImpl.super.saveRule(nodeRef, rule);
}
});
}
else
{
super.saveRule(nodeRef, rule);
return super.saveRule(nodeRef, rule);
}
}

View File

@@ -27,9 +27,13 @@
package org.alfresco.rest.api;
import org.alfresco.rest.api.model.rules.Rule;
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.rule.RuleServiceException;
import java.util.List;
/**
* Folder node rules API.
@@ -57,4 +61,16 @@ public interface Rules
* @return {@link Rule} definition
*/
Rule getRuleById(String folderNodeId, String ruleSetId, String ruleId);
/**
* Create new rules (and potentially a rule set if "_default_" is supplied).
*
* @param folderNodeId The node id of a folder.
* @param ruleSetId The id of a rule set (or "_default_" to use/create the default rule set for the folder).
* @param rule The definition of the rule.
* @return The newly created rules.
* @throws InvalidArgumentException If the nodes are not the expected types, or the rule set does not correspond to the folder.
* @throws RuleServiceException If the folder is already linked to another rule set.
*/
List<Rule> createRules(String folderNodeId, String ruleSetId, List<Rule> rule);
}

View File

@@ -26,6 +26,10 @@
package org.alfresco.rest.api.impl;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.rule.RuleModel;
import org.alfresco.rest.api.Nodes;
@@ -44,10 +48,6 @@ import org.alfresco.service.cmr.security.AccessStatus;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.namespace.QName;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Experimental
public class RulesImpl implements Rules
{
@@ -82,6 +82,19 @@ public class RulesImpl implements Rules
return Rule.from(ruleService.getRule(ruleNodeRef));
}
@Override
public List<Rule> createRules(final String folderNodeId, final String ruleSetId, final List<Rule> rules)
{
final NodeRef folderNodeRef = validateFolderNode(folderNodeId);
validateRuleSetNode(ruleSetId, folderNodeRef);
return rules.stream()
.map(rule -> rule.toServiceModel(nodes))
.map(rule -> ruleService.saveRule(folderNodeRef, rule))
.map(Rule::from)
.collect(Collectors.toList());
}
public void setNodes(Nodes nodes)
{
this.nodes = nodes;

View File

@@ -26,17 +26,16 @@
package org.alfresco.rest.api.model.rules;
import java.util.Objects;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.framework.resource.UniqueId;
import org.alfresco.service.Experimental;
import org.alfresco.service.cmr.action.CompositeAction;
import java.util.List;
import java.util.stream.Collectors;
import org.alfresco.service.cmr.repository.NodeRef;
@Experimental
public class Rule
{
private String id;
private String name;
@@ -45,11 +44,25 @@ public class Rule
return null;
}
final Rule rule = new Rule();
rule.id = ruleModel.getNodeRef().getId();
rule.name = ruleModel.getTitle();
return builder()
.setId(ruleModel.getNodeRef().getId())
.setName(ruleModel.getTitle())
.createRule();
}
return rule;
/**
* Convert the REST model object to the equivalent service POJO.
*
* @param nodes The nodes API.
* @return The rule service POJO.
*/
public org.alfresco.service.cmr.rule.Rule toServiceModel(Nodes nodes)
{
org.alfresco.service.cmr.rule.Rule ruleModel = new org.alfresco.service.cmr.rule.Rule();
NodeRef nodeRef = nodes.validateOrLookupNode(id, null);
ruleModel.setNodeRef(nodeRef);
ruleModel.setTitle(name);
return ruleModel;
}
@UniqueId
@@ -73,9 +86,63 @@ public class Rule
this.name = name;
}
@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (!(o instanceof Rule))
{
return false;
}
Rule rule = (Rule) o;
return Objects.equals(id, rule.id) &&
Objects.equals(name, rule.name);
}
@Override
public int hashCode()
{
return Objects.hash(id, name);
}
@Override
public String toString()
{
return "Rule{" + "id='" + id + '\'' + ", name='" + name + '\'' + '}';
}
public static RuleBuilder builder()
{
return new RuleBuilder();
}
/** Builder class. */
public static class RuleBuilder
{
private String id;
private String name;
public RuleBuilder setId(String id)
{
this.id = id;
return this;
}
public RuleBuilder setName(String name)
{
this.name = name;
return this;
}
public Rule createRule()
{
Rule rule = new Rule();
rule.setId(id);
rule.setName(name);
return rule;
}
}
}

View File

@@ -31,7 +31,7 @@ import org.alfresco.service.Experimental;
@Experimental
public class RuleSet
{
private static final String DEFAULT_ID = "-default-";
public static final String DEFAULT_ID = "-default-";
private String id;

View File

@@ -39,6 +39,7 @@ import org.alfresco.util.PropertyCheck;
import org.springframework.beans.factory.InitializingBean;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* Folder node's rules.
@@ -46,7 +47,8 @@ import javax.servlet.http.HttpServletResponse;
*/
@Experimental
@RelationshipResource(name = "rules", entityResource = NodeRuleSetsRelation.class, title = "Folder node rules")
public class NodeRulesRelation implements RelationshipResourceAction.Read<Rule>, RelationshipResourceAction.ReadById<Rule>, InitializingBean
public class NodeRulesRelation implements RelationshipResourceAction.Read<Rule>, RelationshipResourceAction.ReadById<Rule>, RelationshipResourceAction.Create<Rule>,
InitializingBean
{
private Rules rules;
@@ -103,6 +105,27 @@ public class NodeRulesRelation implements RelationshipResourceAction.Read<Rule>,
return rules.getRuleById(folderNodeId, ruleSetId, ruleId);
}
/**
* Create one or more rules inside a given folder and rule set.
*
* @param folderNodeId The folder in which to create the rule.
* @param ruleList The list of rules to create.
* @param parameters List of parameters including the rule set id as the relationship.
* @return The newly created rules.
*/
@WebApiDescription(
title = "Create folder rule",
description = "Creates one or more folder rules for the given folder and rule set",
successStatus = HttpServletResponse.SC_CREATED
)
@Override
public List<Rule> create(String folderNodeId, List<Rule> ruleList, Parameters parameters)
{
final String ruleSetId = parameters.getRelationshipId();
return rules.createRules(folderNodeId, ruleSetId, ruleList);
}
public void setRules(Rules rules)
{
this.rules = rules;

View File

@@ -26,6 +26,22 @@
package org.alfresco.rest.api.impl;
import static java.util.Collections.emptyList;
import static org.alfresco.rest.api.model.rules.RuleSet.DEFAULT_ID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import junit.framework.TestCase;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.rules.Rule;
@@ -47,22 +63,10 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collection;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
@Experimental
@RunWith(MockitoJUnitRunner.class)
public class RulesImplTest extends TestCase
{
private static final String FOLDER_NODE_ID = "dummy-folder-node-id";
private static final String RULE_SET_ID = "dummy-rule-set-id";
private static final String RULE_ID = "dummy-rule-id";
@@ -70,6 +74,7 @@ public class RulesImplTest extends TestCase
private static final NodeRef ruleSetNodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, RULE_SET_ID);
private static final NodeRef ruleNodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, RULE_ID);
private static final Paging paging = Paging.DEFAULT;
private static final String RULE_NAME = "Rule name";
@Mock
private Nodes nodesMock;
@@ -129,11 +134,10 @@ public class RulesImplTest extends TestCase
@Test
public void testGetRulesForDefaultRuleSet()
{
final String defaultRuleSetId = "-default-";
given(ruleServiceMock.getRules(any())).willReturn(List.of(createRule(RULE_ID)));
// when
final CollectionWithPagingInfo<Rule> rulesPage = rules.getRules(FOLDER_NODE_ID, defaultRuleSetId, paging);
final CollectionWithPagingInfo<Rule> rulesPage = rules.getRules(FOLDER_NODE_ID, DEFAULT_ID, paging);
then(nodesMock).should().validateOrLookupNode(eq(FOLDER_NODE_ID), isNull());
then(nodesMock).should().nodeMatches(eq(folderNodeRef), any(), isNull());
@@ -281,6 +285,115 @@ public class RulesImplTest extends TestCase
then(ruleServiceMock).shouldHaveNoMoreInteractions();
}
/** Create a single rule. */
@Test
public void testSaveRules()
{
Rule ruleBody = mock(Rule.class);
List<Rule> ruleList = List.of(ruleBody);
given(ruleServiceMock.isRuleSetAssociatedWithFolder(any(), any())).willReturn(true);
org.alfresco.service.cmr.rule.Rule serviceRuleBody = mock(org.alfresco.service.cmr.rule.Rule.class);
given(ruleBody.toServiceModel(nodesMock)).willReturn(serviceRuleBody);
org.alfresco.service.cmr.rule.Rule serviceRule = mock(org.alfresco.service.cmr.rule.Rule.class);
given(ruleServiceMock.saveRule(folderNodeRef, serviceRuleBody)).willReturn(serviceRule);
given(serviceRule.getNodeRef()).willReturn(ruleNodeRef);
// when
List<Rule> actual = rules.createRules(folderNodeRef.getId(), ruleSetNodeRef.getId(), ruleList);
then(ruleServiceMock).should().isRuleSetAssociatedWithFolder(ruleSetNodeRef, folderNodeRef);
then(ruleServiceMock).should().saveRule(folderNodeRef, ruleBody.toServiceModel(nodesMock));
then(ruleServiceMock).shouldHaveNoMoreInteractions();
List<Rule> expected = List.of(Rule.from(serviceRule));
assertThat(actual).isEqualTo(expected);
}
/** Check that when passing the default rule set then we don't perform any validation around the rule set node. */
@Test
public void testSaveRules_defaultRuleSet()
{
NodeRef defaultRuleSetNodeRef = new NodeRef("default://rule/set");
given(ruleServiceMock.getRuleSetNode(folderNodeRef)).willReturn(defaultRuleSetNodeRef);
Rule ruleBody = mock(Rule.class);
List<Rule> ruleList = List.of(ruleBody);
org.alfresco.service.cmr.rule.Rule serviceRuleBody = mock(org.alfresco.service.cmr.rule.Rule.class);
given(ruleBody.toServiceModel(nodesMock)).willReturn(serviceRuleBody);
org.alfresco.service.cmr.rule.Rule serviceRule = mock(org.alfresco.service.cmr.rule.Rule.class);
given(ruleServiceMock.saveRule(folderNodeRef, serviceRuleBody)).willReturn(serviceRule);
given(serviceRule.getNodeRef()).willReturn(ruleNodeRef);
// when
List<Rule> actual = rules.createRules(folderNodeRef.getId(), DEFAULT_ID, ruleList);
then(ruleServiceMock).should().getRuleSetNode(folderNodeRef);
then(ruleServiceMock).should().saveRule(folderNodeRef, ruleBody.toServiceModel(nodesMock));
then(ruleServiceMock).shouldHaveNoMoreInteractions();
List<Rule> expected = List.of(Rule.from(serviceRule));
assertThat(actual).isEqualTo(expected);
}
@Test
public void testSaveRules_ruleSetNotAssociatedWithFolder()
{
Rule rule = Rule.builder().setName(RULE_NAME)
.createRule();
List<Rule> ruleList = List.of(rule);
given(ruleServiceMock.isRuleSetAssociatedWithFolder(ruleSetNodeRef, folderNodeRef)).willReturn(false);
// when
assertThatExceptionOfType(InvalidArgumentException.class).isThrownBy(
() -> rules.createRules(folderNodeRef.getId(), ruleSetNodeRef.getId(), ruleList));
then(ruleServiceMock).should().isRuleSetAssociatedWithFolder(ruleSetNodeRef, folderNodeRef);
then(ruleServiceMock).shouldHaveNoMoreInteractions();
}
@Test
public void testSaveRules_emptyRuleList()
{
given(ruleServiceMock.isRuleSetAssociatedWithFolder(any(), any())).willReturn(true);
List<Rule> ruleList = emptyList();
// when
List<Rule> actual = this.rules.createRules(folderNodeRef.getId(), ruleSetNodeRef.getId(), ruleList);
then(ruleServiceMock).should().isRuleSetAssociatedWithFolder(ruleSetNodeRef, folderNodeRef);
then(ruleServiceMock).shouldHaveNoMoreInteractions();
assertThat(actual).isEqualTo(emptyList());
}
/** Create three rules in a single call and check they are all passed to the RuleService. */
@Test
public void testSaveRules_createMultipleRules()
{
given(ruleServiceMock.isRuleSetAssociatedWithFolder(any(), any())).willReturn(true);
List<Rule> ruleBodyList = new ArrayList<>();
List<Rule> expected = new ArrayList<>();
for (String ruleId : List.of("A", "B", "C"))
{
Rule ruleBody = mock(Rule.class);
ruleBodyList.add(ruleBody);
org.alfresco.service.cmr.rule.Rule serviceRuleBody = mock(org.alfresco.service.cmr.rule.Rule.class);
given(ruleBody.toServiceModel(nodesMock)).willReturn(serviceRuleBody);
org.alfresco.service.cmr.rule.Rule serviceRule = mock(org.alfresco.service.cmr.rule.Rule.class);
given(ruleServiceMock.saveRule(folderNodeRef, serviceRuleBody)).willReturn(serviceRule);
NodeRef ruleNodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, ruleId);
given(serviceRule.getNodeRef()).willReturn(ruleNodeRef);
expected.add(Rule.from(serviceRule));
}
// when
List<Rule> actual = rules.createRules(folderNodeRef.getId(), ruleSetNodeRef.getId(), ruleBodyList);
then(ruleServiceMock).should().isRuleSetAssociatedWithFolder(ruleSetNodeRef, folderNodeRef);
for (Rule ruleBody : ruleBodyList)
{
then(ruleServiceMock).should().saveRule(folderNodeRef, ruleBody.toServiceModel(nodesMock));
}
then(ruleServiceMock).shouldHaveNoMoreInteractions();
assertThat(actual).isEqualTo(expected);
}
private static org.alfresco.service.cmr.rule.Rule createRule(final String id) {
final org.alfresco.service.cmr.rule.Rule rule = new org.alfresco.service.cmr.rule.Rule();
rule.setNodeRef(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, id));

View File

@@ -745,62 +745,61 @@ public class RuleServiceImpl
}
@Override
public void saveRule(NodeRef nodeRef, Rule rule)
public Rule saveRule(NodeRef nodeRef, Rule rule)
{
checkForLinkedRules(nodeRef);
if (this.permissionService.hasPermission(nodeRef, PermissionService.CHANGE_PERMISSIONS) == AccessStatus.ALLOWED)
{
disableRules();
try
{
if (this.nodeService.exists(nodeRef) == false)
{
throw new RuleServiceException("The node does not exist.");
}
NodeRef ruleNodeRef = rule.getNodeRef();
if (ruleNodeRef == null)
{
if (this.nodeService.hasAspect(nodeRef, RuleModel.ASPECT_RULES) == false)
{
// Add the actionable aspect
this.nodeService.addAspect(nodeRef, RuleModel.ASPECT_RULES, null);
}
// Create the action node
ruleNodeRef = this.nodeService.createNode(
getSavedRuleFolderRef(nodeRef),
ContentModel.ASSOC_CONTAINS,
QName.createQName(RuleModel.RULE_MODEL_URI, ASSOC_NAME_RULES_PREFIX + GUID.generate()),
RuleModel.TYPE_RULE).getChildRef();
// Set the rule node reference and the owning node reference
rule.setNodeRef(ruleNodeRef);
}
// Update the properties of the rule
this.nodeService.setProperty(ruleNodeRef, ContentModel.PROP_TITLE, rule.getTitle());
this.nodeService.setProperty(ruleNodeRef, ContentModel.PROP_DESCRIPTION, rule.getDescription());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_RULE_TYPE, (Serializable)rule.getRuleTypes());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_APPLY_TO_CHILDREN, rule.isAppliedToChildren());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_EXECUTE_ASYNC, rule.getExecuteAsynchronously());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_DISABLED, rule.getRuleDisabled());
// Save the rule's action
saveAction(ruleNodeRef, rule);
}
finally
{
enableRules();
// Drop the rules from the cache
nodeRulesCache.remove(nodeRef);
}
}
else
if (this.permissionService.hasPermission(nodeRef, PermissionService.CHANGE_PERMISSIONS) != AccessStatus.ALLOWED)
{
throw new RuleServiceException("Insufficient permissions to save a rule.");
}
disableRules();
try
{
if (this.nodeService.exists(nodeRef) == false)
{
throw new RuleServiceException("The node does not exist.");
}
NodeRef ruleNodeRef = rule.getNodeRef();
if (ruleNodeRef == null)
{
if (this.nodeService.hasAspect(nodeRef, RuleModel.ASPECT_RULES) == false)
{
// Add the actionable aspect
this.nodeService.addAspect(nodeRef, RuleModel.ASPECT_RULES, null);
}
// Create the action node
ruleNodeRef = this.nodeService.createNode(
getSavedRuleFolderRef(nodeRef),
ContentModel.ASSOC_CONTAINS,
QName.createQName(RuleModel.RULE_MODEL_URI, ASSOC_NAME_RULES_PREFIX + GUID.generate()),
RuleModel.TYPE_RULE).getChildRef();
// Set the rule node reference and the owning node reference
rule.setNodeRef(ruleNodeRef);
}
// Update the properties of the rule
this.nodeService.setProperty(ruleNodeRef, ContentModel.PROP_TITLE, rule.getTitle());
this.nodeService.setProperty(ruleNodeRef, ContentModel.PROP_DESCRIPTION, rule.getDescription());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_RULE_TYPE, (Serializable)rule.getRuleTypes());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_APPLY_TO_CHILDREN, rule.isAppliedToChildren());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_EXECUTE_ASYNC, rule.getExecuteAsynchronously());
this.nodeService.setProperty(ruleNodeRef, RuleModel.PROP_DISABLED, rule.getRuleDisabled());
// Save the rule's action
saveAction(ruleNodeRef, rule);
}
finally
{
enableRules();
// Drop the rules from the cache
nodeRulesCache.remove(nodeRef);
}
return rule;
}
@Override

View File

@@ -235,8 +235,8 @@ public interface RuleService
* @param rule Rule
*/
@Auditable(parameters = {"nodeRef", "rule"})
public void saveRule(NodeRef nodeRef, Rule rule);
public Rule saveRule(NodeRef nodeRef, Rule rule);
/**
*
* @param nodeRef NodeRef