ACS-3525: API for manual triggering rules on a folder (#1458)

* ACS-3525: API for manual triggering rules on a folder
This commit is contained in:
krdabrowski
2022-10-05 12:19:49 +02:00
committed by GitHub
parent 8afd06a57f
commit 67f7fff0b7
12 changed files with 419 additions and 36 deletions

View File

@@ -352,7 +352,7 @@ public class CreateRulesTests extends RestTest
STEP(String.format("Add a user with '%s' role in the private site's folder", userRole.toString())); STEP(String.format("Add a user with '%s' role in the private site's folder", userRole.toString()));
UserModel userWithRole = dataUser.createRandomTestUser(); UserModel userWithRole = dataUser.createRandomTestUser();
dataUser.addUserToSite(userWithRole, privateSite, userRole); dataUser.addUserToSite(userWithRole, privateSite, userRole);
RestRuleModel ruleModel = createRuleModel("testRule", List.of(createDefaultActionModel())); RestRuleModel ruleModel = createRuleModel("testRule", List.of(createAddAudioAspectAction()));
return restClient.authenticateUser(userWithRole).withCoreAPI().usingNode(privateFolder).usingDefaultRuleSet().createSingleRule(ruleModel); return restClient.authenticateUser(userWithRole).withCoreAPI().usingNode(privateFolder).usingDefaultRuleSet().createSingleRule(ruleModel);
} }

View File

@@ -0,0 +1,96 @@
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.rules;
import static org.alfresco.rest.rules.RulesTestsUtils.AUDIO_ASPECT;
import static org.alfresco.rest.rules.RulesTestsUtils.createRuleExecutionRequest;
import static org.alfresco.rest.rules.RulesTestsUtils.createRuleModelWithDefaultValues;
import static org.alfresco.utility.report.log.Step.STEP;
import org.alfresco.dataprep.CMISUtil;
import org.alfresco.rest.RestTest;
import org.alfresco.rest.model.RestNodeModel;
import org.alfresco.rest.model.RestRuleModel;
import org.alfresco.utility.Utility;
import org.alfresco.utility.model.FileModel;
import org.alfresco.utility.model.FolderModel;
import org.alfresco.utility.model.SiteModel;
import org.alfresco.utility.model.TestGroup;
import org.alfresco.utility.model.UserModel;
import org.springframework.http.HttpStatus;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Tests for POST /nodes/{nodeId}/rule-executions.
*/
@Test(groups = { TestGroup.RULES})
public class ExecuteRulesTests extends RestTest
{
private UserModel user;
private SiteModel site;
private FolderModel ruleFolder;
private FileModel file;
@BeforeClass(alwaysRun = true)
public void dataPreparation()
{
STEP("Create a user, site, folder and file in it");
user = dataUser.createRandomTestUser();
site = dataSite.usingUser(user).createPublicRandomSite();
ruleFolder = dataContent.usingUser(user).usingSite(site).createFolder();
file = dataContent.usingUser(user).usingResource(ruleFolder).createContent(CMISUtil.DocumentType.TEXT_PLAIN);
STEP("Create one rule with add audio aspect action in the folder");
RestRuleModel ruleModel = createRuleModelWithDefaultValues();
restClient.authenticateUser(user).withCoreAPI().usingNode(ruleFolder).usingDefaultRuleSet().createSingleRule(ruleModel);
}
/**
* Execute one rule with one action trying to add audio aspect to a file
*/
@Test (groups = { TestGroup.REST_API, TestGroup.RULES, TestGroup.SANITY })
public void executeOneRuleWithOneAction() throws InterruptedException
{
STEP("Check if file aspects don't contain Audio one");
RestNodeModel node = restClient.authenticateUser(user).withCoreAPI().usingNode(file).getNode();
restClient.assertStatusCodeIs(HttpStatus.OK);
node.assertThat().field("aspectNames").notContains(AUDIO_ASPECT);
STEP("Execute rule");
restClient.authenticateUser(user).withCoreAPI().usingNode(ruleFolder).executeRules(createRuleExecutionRequest());
restClient.assertStatusCodeIs(HttpStatus.CREATED);
STEP("Check if file contains Audio aspect");
Utility.sleep(500, 10000, () -> {
RestNodeModel fileNode = restClient.authenticateUser(user).withCoreAPI().usingNode(file).getNode();
restClient.assertStatusCodeIs(HttpStatus.OK);
fileNode.assertThat().field("aspectNames").contains(AUDIO_ASPECT);
});
}
// TODO add more E2Es. For more see: ACS-3620
}

View File

@@ -2,7 +2,7 @@
* #%L * #%L
* Alfresco Repository * Alfresco Repository
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2022 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
@@ -152,7 +152,7 @@ public class GetRulesTests extends RestTest
rules.getEntries().get(i).onModel() rules.getEntries().get(i).onModel()
.assertThat().field("isShared").isNotNull() .assertThat().field("isShared").isNotNull()
.assertThat().field("description").isNull() .assertThat().field("description").isNull()
.assertThat().field("isEnabled").is(false) .assertThat().field("isEnabled").is(true)
.assertThat().field("isInheritable").is(false) .assertThat().field("isInheritable").is(false)
.assertThat().field("isAsynchronous").is(false) .assertThat().field("isAsynchronous").is(false)
.assertThat().field("errorScript").isNull() .assertThat().field("errorScript").isNull()

View File

@@ -32,6 +32,7 @@ import java.util.Map;
import org.alfresco.rest.model.RestActionBodyExecTemplateModel; import org.alfresco.rest.model.RestActionBodyExecTemplateModel;
import org.alfresco.rest.model.RestCompositeConditionDefinitionModel; import org.alfresco.rest.model.RestCompositeConditionDefinitionModel;
import org.alfresco.rest.model.RestRuleExecutionBodyModel;
import org.alfresco.rest.model.RestRuleModel; import org.alfresco.rest.model.RestRuleModel;
import org.alfresco.rest.model.RestSimpleConditionDefinitionModel; import org.alfresco.rest.model.RestSimpleConditionDefinitionModel;
@@ -52,15 +53,22 @@ public class RulesTestsUtils
static final String AND = "and"; static final String AND = "and";
static final String ID = "id"; static final String ID = "id";
static final String IS_SHARED = "isShared"; static final String IS_SHARED = "isShared";
static final String AUDIO_ASPECT = "audio:audio";
/**
* Create a rule model filled with default values.
*
* @return The created rule model.
*/
public static RestRuleModel createRuleModelWithModifiedValues() public static RestRuleModel createRuleModelWithModifiedValues()
{ {
RestRuleModel ruleModel = createRuleModelWithDefaultValues(); return createRuleModelWithModifiedValues(List.of(createAddAudioAspectAction()));
}
/**
* Create a rule model filled with custom constant values.
*
* @param actions - rule's actions.
* @return The created rule model.
*/
public static RestRuleModel createRuleModelWithModifiedValues(List<RestActionBodyExecTemplateModel> actions)
{
RestRuleModel ruleModel = createRuleModel(RULE_NAME_DEFAULT, actions);
ruleModel.setDescription(RULE_DESCRIPTION_DEFAULT); ruleModel.setDescription(RULE_DESCRIPTION_DEFAULT);
ruleModel.setIsEnabled(RULE_ENABLED_DEFAULT); ruleModel.setIsEnabled(RULE_ENABLED_DEFAULT);
ruleModel.setIsInheritable(RULE_CASCADE_DEFAULT); ruleModel.setIsInheritable(RULE_CASCADE_DEFAULT);
@@ -74,26 +82,27 @@ public class RulesTestsUtils
public static RestRuleModel createRuleModelWithDefaultValues() public static RestRuleModel createRuleModelWithDefaultValues()
{ {
return createRuleModel(RULE_NAME_DEFAULT, List.of(createDefaultActionModel())); return createRuleModel(RULE_NAME_DEFAULT);
} }
public static RestRuleModel createRuleModel(String name) public static RestRuleModel createRuleModel(String name)
{ {
return createRuleModel(name, List.of(createDefaultActionModel())); return createRuleModel(name, List.of(createAddAudioAspectAction()));
} }
/** /**
* Create a rule model. * Create a rule model.
* *
* @param name The name for the rule. * @param name The name for the rule.
* @param restActionModels Rule's actions. * @param actions Rule's actions.
* @return The created rule model. * @return The created rule model.
*/ */
public static RestRuleModel createRuleModel(String name, List<RestActionBodyExecTemplateModel> restActionModels) public static RestRuleModel createRuleModel(String name, List<RestActionBodyExecTemplateModel> actions)
{ {
RestRuleModel ruleModel = new RestRuleModel(); RestRuleModel ruleModel = new RestRuleModel();
ruleModel.setIsEnabled(true);
ruleModel.setName(name); ruleModel.setName(name);
ruleModel.setActions(restActionModels); ruleModel.setActions(actions);
return ruleModel; return ruleModel;
} }
@@ -102,12 +111,9 @@ public class RulesTestsUtils
* *
* @return The created action model. * @return The created action model.
*/ */
public static RestActionBodyExecTemplateModel createDefaultActionModel() public static RestActionBodyExecTemplateModel createAddAudioAspectAction()
{ {
RestActionBodyExecTemplateModel restActionModel = new RestActionBodyExecTemplateModel(); return createCustomActionModel("add-features", Map.of("aspect-name", AUDIO_ASPECT));
restActionModel.setActionDefinitionId("set-property-value");
restActionModel.setParams(Map.of("aspect-name", "cm:audio"));
return restActionModel;
} }
public static RestActionBodyExecTemplateModel createCustomActionModel(String actionDefinitionId, Map<String, Serializable> params) public static RestActionBodyExecTemplateModel createCustomActionModel(String actionDefinitionId, Map<String, Serializable> params)
@@ -139,7 +145,7 @@ public class RulesTestsUtils
createSimpleCondition("tag", "equals", "uat") createSimpleCondition("tag", "equals", "uat")
)), )),
createCompositeCondition(INVERTED, List.of( createCompositeCondition(INVERTED, List.of(
createSimpleCondition("aspect", "equals", "audio:audio"), createSimpleCondition("aspect", "equals", AUDIO_ASPECT),
createSimpleCondition("cm:modelVersion", "begins", "1.") createSimpleCondition("cm:modelVersion", "begins", "1.")
)) ))
)); ));
@@ -182,6 +188,20 @@ public class RulesTestsUtils
return createCompositeCondition(AND, inverted, null, simpleConditions); return createCompositeCondition(AND, inverted, null, simpleConditions);
} }
public static RestRuleExecutionBodyModel createRuleExecutionRequest()
{
return createRuleExecutionRequest(false, false);
}
public static RestRuleExecutionBodyModel createRuleExecutionRequest(boolean eachSubFolderIncluded, boolean eachInheritedRuleExecuted)
{
RestRuleExecutionBodyModel ruleExecutionBody = new RestRuleExecutionBodyModel();
ruleExecutionBody.setIsEachSubFolderIncluded(eachSubFolderIncluded);
ruleExecutionBody.setIsEachInheritedRuleExecuted(eachInheritedRuleExecuted);
return ruleExecutionBody;
}
private static RestCompositeConditionDefinitionModel createCompositeCondition(String booleanMode, boolean inverted, private static RestCompositeConditionDefinitionModel createCompositeCondition(String booleanMode, boolean inverted,
List<RestCompositeConditionDefinitionModel> compositeConditions, List<RestSimpleConditionDefinitionModel> simpleConditions) List<RestCompositeConditionDefinitionModel> compositeConditions, List<RestSimpleConditionDefinitionModel> simpleConditions)
{ {

View File

@@ -34,9 +34,8 @@ import static org.alfresco.rest.rules.RulesTestsUtils.RULE_CASCADE_DEFAULT;
import static org.alfresco.rest.rules.RulesTestsUtils.RULE_ENABLED_DEFAULT; import static org.alfresco.rest.rules.RulesTestsUtils.RULE_ENABLED_DEFAULT;
import static org.alfresco.rest.rules.RulesTestsUtils.createCompositeCondition; import static org.alfresco.rest.rules.RulesTestsUtils.createCompositeCondition;
import static org.alfresco.rest.rules.RulesTestsUtils.createCustomActionModel; import static org.alfresco.rest.rules.RulesTestsUtils.createCustomActionModel;
import static org.alfresco.rest.rules.RulesTestsUtils.createDefaultActionModel; import static org.alfresco.rest.rules.RulesTestsUtils.createAddAudioAspectAction;
import static org.alfresco.rest.rules.RulesTestsUtils.createRuleModel; import static org.alfresco.rest.rules.RulesTestsUtils.createRuleModel;
import static org.alfresco.rest.rules.RulesTestsUtils.createRuleModelWithDefaultValues;
import static org.alfresco.rest.rules.RulesTestsUtils.createRuleModelWithModifiedValues; import static org.alfresco.rest.rules.RulesTestsUtils.createRuleModelWithModifiedValues;
import static org.alfresco.rest.rules.RulesTestsUtils.createSimpleCondition; import static org.alfresco.rest.rules.RulesTestsUtils.createSimpleCondition;
import static org.alfresco.rest.rules.RulesTestsUtils.createVariousConditions; import static org.alfresco.rest.rules.RulesTestsUtils.createVariousConditions;
@@ -530,7 +529,7 @@ public class UpdateRulesTests extends RestTest
private RestRuleModel createAndSaveRule(String name) private RestRuleModel createAndSaveRule(String name)
{ {
return createAndSaveRule(name, List.of(createDefaultActionModel())); return createAndSaveRule(name, List.of(createAddAudioAspectAction()));
} }
/** /**

View File

@@ -123,7 +123,7 @@
<dependency.mariadb.version>2.7.4</dependency.mariadb.version> <dependency.mariadb.version>2.7.4</dependency.mariadb.version>
<dependency.tas-utility.version>3.0.56</dependency.tas-utility.version> <dependency.tas-utility.version>3.0.56</dependency.tas-utility.version>
<dependency.rest-assured.version>5.2.0</dependency.rest-assured.version> <dependency.rest-assured.version>5.2.0</dependency.rest-assured.version>
<dependency.tas-restapi.version>1.127</dependency.tas-restapi.version> <dependency.tas-restapi.version>1.128</dependency.tas-restapi.version>
<dependency.tas-email.version>1.9</dependency.tas-email.version> <dependency.tas-email.version>1.9</dependency.tas-email.version>
<dependency.tas-webdav.version>1.7</dependency.tas-webdav.version> <dependency.tas-webdav.version>1.7</dependency.tas-webdav.version>
<dependency.tas-ftp.version>1.7</dependency.tas-ftp.version> <dependency.tas-ftp.version>1.7</dependency.tas-ftp.version>

View File

@@ -29,6 +29,7 @@ package org.alfresco.rest.api;
import java.util.List; import java.util.List;
import org.alfresco.rest.api.model.rules.Rule; import org.alfresco.rest.api.model.rules.Rule;
import org.alfresco.rest.api.model.rules.RuleExecution;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging; import org.alfresco.rest.framework.resource.parameters.Paging;
@@ -65,10 +66,10 @@ public interface Rules
Rule getRuleById(String folderNodeId, String ruleSetId, String ruleId, List<String> includes); Rule getRuleById(String folderNodeId, String ruleSetId, String ruleId, List<String> includes);
/** /**
* Create new rules (and potentially a rule set if "_default_" is supplied). * Create new rules (and potentially a rule set if "-default-" is supplied).
* *
* @param folderNodeId The node id of a folder. * @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 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. * @param rule The definition of the rule.
* @param includes The list of optional fields to include in the response. * @param includes The list of optional fields to include in the response.
* @return The newly created rules. * @return The newly created rules.
@@ -81,7 +82,7 @@ public interface Rules
* Update a rule. * Update a rule.
* *
* @param folderNodeId The id of a folder. * @param folderNodeId The id of a folder.
* @param ruleSetId The id of a rule set within the folder (or "_default_" to use the default rule set for the folder). * @param ruleSetId The id of a rule set within the folder (or "-default-" to use the default rule set for the folder).
* @param ruleId The rule id. * @param ruleId The rule id.
* @param rule The new version of the rule. * @param rule The new version of the rule.
* @param includes The list of optional fields to include in the response. * @param includes The list of optional fields to include in the response.
@@ -94,7 +95,16 @@ public interface Rules
* *
* @param folderNodeId - folder node ID * @param folderNodeId - folder node ID
* @param ruleSetId - rule set ID * @param ruleSetId - rule set ID
* @param ruleId - rule ID * * @param ruleId - rule ID
*/ */
void deleteRuleById(String folderNodeId, String ruleSetId, String ruleId); void deleteRuleById(String folderNodeId, String ruleSetId, String ruleId);
/**
* Execute rules for given folder node.
*
* @param folderNodeId - the ID of a folder
* @param eachSubFolderIncluded - indicates if rules should be executed also on sub-folders
* @param eachInheritedRuleExecuted - indicates if the inherited rules should be also executed
*/
RuleExecution executeRules(final String folderNodeId, final boolean eachSubFolderIncluded, final boolean eachInheritedRuleExecuted);
} }

View File

@@ -26,29 +26,30 @@
package org.alfresco.rest.api.impl.rules; package org.alfresco.rest.api.impl.rules;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.alfresco.rest.api.Nodes; import org.alfresco.repo.action.ActionImpl;
import org.alfresco.repo.action.access.ActionAccessRestriction;
import org.alfresco.repo.action.executer.ExecuteAllRulesActionExecuter;
import org.alfresco.rest.api.Rules; import org.alfresco.rest.api.Rules;
import org.alfresco.rest.api.model.mapper.RestModelMapper; import org.alfresco.rest.api.model.mapper.RestModelMapper;
import org.alfresco.rest.api.model.rules.CompositeCondition;
import org.alfresco.rest.api.model.rules.Rule; import org.alfresco.rest.api.model.rules.Rule;
import org.alfresco.rest.api.model.rules.RuleExecution;
import org.alfresco.rest.api.model.rules.RuleSet; import org.alfresco.rest.api.model.rules.RuleSet;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.api.model.rules.SimpleCondition;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.ListPage; import org.alfresco.rest.framework.resource.parameters.ListPage;
import org.alfresco.rest.framework.resource.parameters.Paging; import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.service.Experimental; import org.alfresco.service.Experimental;
import org.alfresco.service.cmr.action.ActionCondition; import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.action.CompositeAction;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.rule.RuleService; import org.alfresco.service.cmr.rule.RuleService;
import org.alfresco.util.collections.CollectionUtils; import org.alfresco.util.GUID;
import org.alfresco.service.namespace.NamespaceService; import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -58,6 +59,7 @@ public class RulesImpl implements Rules
private static final Logger LOGGER = LoggerFactory.getLogger(RulesImpl.class); private static final Logger LOGGER = LoggerFactory.getLogger(RulesImpl.class);
private static final String MUST_HAVE_AT_LEAST_ONE_ACTION = "A rule must have at least one action"; private static final String MUST_HAVE_AT_LEAST_ONE_ACTION = "A rule must have at least one action";
private ActionService actionService;
private RuleService ruleService; private RuleService ruleService;
private NodeValidator validator; private NodeValidator validator;
private RuleLoader ruleLoader; private RuleLoader ruleLoader;
@@ -130,6 +132,26 @@ public class RulesImpl implements Rules
ruleService.removeRule(folderNodeRef, rule); ruleService.removeRule(folderNodeRef, rule);
} }
@Override
public RuleExecution executeRules(final String folderNodeId, final boolean eachSubFolderIncluded, final boolean eachInheritedRuleExecuted)
{
final NodeRef folderNodeRef = validator.validateFolderNode(folderNodeId, false);
final Map<String, Serializable> parameterValues = new HashMap<>();
parameterValues.put(ExecuteAllRulesActionExecuter.PARAM_RUN_ALL_RULES_ON_CHILDREN, eachSubFolderIncluded);
parameterValues.put(ExecuteAllRulesActionExecuter.PARAM_EXECUTE_INHERITED_RULES, eachInheritedRuleExecuted);
final ActionImpl action = new ActionImpl(null, GUID.generate(), ExecuteAllRulesActionExecuter.NAME);
action.setNodeRef(folderNodeRef);
action.setParameterValues(parameterValues);
ActionAccessRestriction.setActionContext(action, ActionAccessRestriction.V1_ACTION_CONTEXT);
actionService.executeAction(action, folderNodeRef, true, false);
return RuleExecution.builder()
.eachSubFolderIncluded(eachSubFolderIncluded)
.eachInheritedRuleExecuted(eachInheritedRuleExecuted)
.create();
}
private org.alfresco.service.cmr.rule.Rule mapToServiceModelAndValidateActions(Rule rule) private org.alfresco.service.cmr.rule.Rule mapToServiceModelAndValidateActions(Rule rule)
{ {
if (CollectionUtils.isEmpty(rule.getActions())) if (CollectionUtils.isEmpty(rule.getActions()))
@@ -141,6 +163,11 @@ public class RulesImpl implements Rules
return actionPermissionValidator.validateRulePermissions(serviceModelRule); return actionPermissionValidator.validateRulePermissions(serviceModelRule);
} }
public void setActionService(ActionService actionService)
{
this.actionService = actionService;
}
public void setRuleService(RuleService ruleService) public void setRuleService(RuleService ruleService)
{ {
this.ruleService = ruleService; this.ruleService = ruleService;

View File

@@ -0,0 +1,111 @@
/*
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.model.rules;
import java.util.Objects;
import org.alfresco.service.Experimental;
@Experimental
public class RuleExecution
{
private boolean eachSubFolderIncluded;
private boolean eachInheritedRuleExecuted;
public boolean getIsEachSubFolderIncluded()
{
return eachSubFolderIncluded;
}
public void setIsEachSubFolderIncluded(boolean eachSubFolderIncluded)
{
this.eachSubFolderIncluded = eachSubFolderIncluded;
}
public boolean getIsEachInheritedRuleExecuted()
{
return eachInheritedRuleExecuted;
}
public void setIsEachInheritedRuleExecuted(boolean eachInheritedRuleExecuted)
{
this.eachInheritedRuleExecuted = eachInheritedRuleExecuted;
}
@Override
public String toString()
{
return "RuleExecution{" + "eachSubFolderIncluded=" + eachSubFolderIncluded + ", eachInheritedRuleExecuted=" + eachInheritedRuleExecuted + '}';
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
RuleExecution that = (RuleExecution) o;
return eachSubFolderIncluded == that.eachSubFolderIncluded && eachInheritedRuleExecuted == that.eachInheritedRuleExecuted;
}
@Override
public int hashCode()
{
return Objects.hash(eachSubFolderIncluded, eachInheritedRuleExecuted);
}
public static Builder builder()
{
return new Builder();
}
public static class Builder
{
private boolean eachSubFolderIncluded;
private boolean eachInheritedRuleExecuted;
public Builder eachSubFolderIncluded(boolean eachSubFolderIncluded)
{
this.eachSubFolderIncluded = eachSubFolderIncluded;
return this;
}
public Builder eachInheritedRuleExecuted(boolean eachInheritedRuleExecuted)
{
this.eachInheritedRuleExecuted = eachInheritedRuleExecuted;
return this;
}
public RuleExecution create()
{
final RuleExecution ruleExecution = new RuleExecution();
ruleExecution.setIsEachSubFolderIncluded(eachSubFolderIncluded);
ruleExecution.setIsEachInheritedRuleExecuted(eachInheritedRuleExecuted);
return ruleExecution;
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.nodes;
import java.util.List;
import org.alfresco.rest.api.Rules;
import org.alfresco.rest.api.model.rules.RuleExecution;
import org.alfresco.rest.framework.resource.RelationshipResource;
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.service.Experimental;
import org.alfresco.util.PropertyCheck;
import org.springframework.beans.factory.InitializingBean;
@Experimental
@RelationshipResource(name = "rule-executions", entityResource = NodesEntityResource.class, title = "Executing rules")
public class NodeRuleExecutionsRelation implements RelationshipResourceAction.Create<RuleExecution>, InitializingBean
{
private final Rules rules;
public NodeRuleExecutionsRelation(Rules rules)
{
this.rules = rules;
}
@Override
public void afterPropertiesSet() throws Exception
{
PropertyCheck.mandatory(this, "rules", this.rules);
}
/**
* Execute rules for given folder node.
*
* @param folderNodeId - the ID of a folder
* @param ruleExecutionParameters - rule execution parameters
* @param parameters - additional request parameters
* @return execution details
*/
@Override
public List<RuleExecution> create(String folderNodeId, List<RuleExecution> ruleExecutionParameters, Parameters parameters)
{
final RuleExecution ruleExecution = ruleExecutionParameters.stream().findFirst().orElse(new RuleExecution());
return List.of(rules.executeRules(folderNodeId, ruleExecution.getIsEachSubFolderIncluded(), ruleExecution.getIsEachInheritedRuleExecuted()));
}
}

View File

@@ -906,6 +906,7 @@
</bean> </bean>
<bean id="rules" class="org.alfresco.rest.api.impl.rules.RulesImpl"> <bean id="rules" class="org.alfresco.rest.api.impl.rules.RulesImpl">
<property name="actionService" ref="ActionService"/>
<property name="validator" ref="nodeValidator"/> <property name="validator" ref="nodeValidator"/>
<property name="ruleService" ref="RuleService" /> <property name="ruleService" ref="RuleService" />
<property name="ruleLoader" ref="ruleLoader"/> <property name="ruleLoader" ref="ruleLoader"/>
@@ -927,6 +928,10 @@
<constructor-arg name="ruleSets" ref="RuleSets" /> <constructor-arg name="ruleSets" ref="RuleSets" />
</bean> </bean>
<bean class="org.alfresco.rest.api.nodes.NodeRuleExecutionsRelation">
<constructor-arg name="rules" ref="Rules" />
</bean>
<bean id="ruleSettings" class="org.alfresco.rest.api.impl.rules.RuleSettingsImpl"> <bean id="ruleSettings" class="org.alfresco.rest.api.impl.rules.RuleSettingsImpl">
<property name="validator" ref="nodeValidator" /> <property name="validator" ref="nodeValidator" />
<property name="nodeService" ref="NodeService" /> <property name="nodeService" ref="NodeService" />

View File

@@ -33,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then; import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@@ -40,13 +41,18 @@ import static org.mockito.Mockito.mock;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.alfresco.repo.action.ActionImpl;
import org.alfresco.repo.action.access.ActionAccessRestriction;
import org.alfresco.repo.action.executer.ExecuteAllRulesActionExecuter;
import org.alfresco.rest.api.Nodes; import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.mapper.RestModelMapper; import org.alfresco.rest.api.model.mapper.RestModelMapper;
import org.alfresco.rest.api.model.rules.Action; import org.alfresco.rest.api.model.rules.Action;
import org.alfresco.rest.api.model.rules.Rule; import org.alfresco.rest.api.model.rules.Rule;
import org.alfresco.rest.api.model.rules.RuleExecution;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException; import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException; import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException;
@@ -54,12 +60,14 @@ import org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundE
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging; import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.service.Experimental; import org.alfresco.service.Experimental;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.rule.RuleService; import org.alfresco.service.cmr.rule.RuleService;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
@@ -77,10 +85,14 @@ public class RulesImplTest extends TestCase
private static final NodeRef RULE_NODE_REF = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, RULE_ID); private static final NodeRef RULE_NODE_REF = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, RULE_ID);
private static final Paging PAGING = Paging.DEFAULT; private static final Paging PAGING = Paging.DEFAULT;
private static final List<String> INCLUDE = emptyList(); private static final List<String> INCLUDE = emptyList();
private static final boolean INCLUDE_SUB_FOLDERS = true;
private static final boolean EXECUTE_INHERITED_RULES = true;
@Mock @Mock
private Nodes nodesMock; private Nodes nodesMock;
@Mock @Mock
private ActionService actionServiceMock;
@Mock
private RestModelMapper<Rule, org.alfresco.service.cmr.rule.Rule> ruleMapper; private RestModelMapper<Rule, org.alfresco.service.cmr.rule.Rule> ruleMapper;
@Mock @Mock
private NodeValidator nodeValidatorMock; private NodeValidator nodeValidatorMock;
@@ -618,6 +630,39 @@ public class RulesImplTest extends TestCase
} }
} }
@Test
public void testExecuteRule()
{
// when
final RuleExecution actualRuleExecution = rules.executeRules(FOLDER_NODE_ID, INCLUDE_SUB_FOLDERS, EXECUTE_INHERITED_RULES);
final RuleExecution expectedRuleExecution = RuleExecution.builder()
.eachSubFolderIncluded(INCLUDE_SUB_FOLDERS)
.eachInheritedRuleExecuted(EXECUTE_INHERITED_RULES)
.create();
final ActionImpl expectedAction = new ActionImpl(null, null, ExecuteAllRulesActionExecuter.NAME);
expectedAction.setNodeRef(FOLDER_NODE_REF);
expectedAction.setParameterValues(Map.of(
ExecuteAllRulesActionExecuter.PARAM_RUN_ALL_RULES_ON_CHILDREN, INCLUDE_SUB_FOLDERS,
ExecuteAllRulesActionExecuter.PARAM_EXECUTE_INHERITED_RULES, EXECUTE_INHERITED_RULES,
ActionAccessRestriction.ACTION_CONTEXT_PARAM_NAME, ActionAccessRestriction.V1_ACTION_CONTEXT)
);
final ArgumentCaptor<ActionImpl> actionCaptor = ArgumentCaptor.forClass(ActionImpl.class);
then(nodeValidatorMock).should().validateFolderNode(FOLDER_NODE_ID, false);
then(nodeValidatorMock).shouldHaveNoMoreInteractions();
then(actionServiceMock).should().executeAction(actionCaptor.capture(), eq(FOLDER_NODE_REF), eq(true), eq(false));
then(actionServiceMock).shouldHaveNoMoreInteractions();
final ActionImpl actualAction = actionCaptor.getValue();
assertThat(actualAction)
.isNotNull()
.usingRecursiveComparison().ignoringFields("id")
.isEqualTo(expectedAction);
assertThat(actualRuleExecution)
.isNotNull()
.usingRecursiveComparison()
.isEqualTo(expectedRuleExecution);
}
private static org.alfresco.service.cmr.rule.Rule createRule(final String id) private static org.alfresco.service.cmr.rule.Rule createRule(final String id)
{ {
final NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, id); final NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, id);