diff --git a/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/SetInheritanceTests.java b/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/SetInheritanceTests.java new file mode 100644 index 0000000000..c25b3dbaec --- /dev/null +++ b/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/SetInheritanceTests.java @@ -0,0 +1,232 @@ +/* + * #%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 . + * #L% + */ +package org.alfresco.rest.rules; + +import static org.alfresco.rest.requests.RuleSettings.IS_INHERITANCE_ENABLED; +import static org.alfresco.utility.constants.UserRole.SiteCollaborator; +import static org.alfresco.utility.report.log.Step.STEP; +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.FORBIDDEN; +import static org.springframework.http.HttpStatus.NOT_FOUND; +import static org.springframework.http.HttpStatus.OK; + +import org.alfresco.rest.RestTest; +import org.alfresco.rest.model.RestRuleSettingsModel; +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.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * Tests for GET and PUT /nodes/{nodeId}/rule-settings/{ruleSettingKey}. + */ +@Test (groups = { TestGroup.RULES }) +public class SetInheritanceTests extends RestTest +{ + private UserModel siteOwner; + private SiteModel site; + + @BeforeClass (alwaysRun = true) + public void dataPreparation() + { + STEP("Create a user, site and folder."); + siteOwner = dataUser.createRandomTestUser(); + site = dataSite.usingUser(siteOwner).createPrivateRandomSite(); + } + + /** Check we can get the -isInheritanceEnabled- rule setting for the folder. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void getIsInherited() + { + STEP("Create a folder for the test."); + FolderModel folder = dataContent.usingUser(siteOwner).usingSite(site).createFolder(); + + STEP("Get the -isInheritanceEnabled- rule settings for the folder."); + RestRuleSettingsModel ruleSettingsModel = restClient.authenticateUser(siteOwner) + .withCoreAPI() + .usingResource(folder) + .usingIsInheritanceEnabledRuleSetting() + .retrieveSetting(); + + restClient.assertStatusCodeIs(OK); + RestRuleSettingsModel expected = new RestRuleSettingsModel(); + expected.setKey(IS_INHERITANCE_ENABLED); + expected.setValue(true); + ruleSettingsModel.assertThat().isEqualTo(expected); + } + + /** Check we get an error when trying to get settings from a non-existent folder. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void getIsInheritedFromNonExistentFolder() + { + STEP("Try to get the -isInheritanceEnabled- rule settings for a fake folder."); + FolderModel nonExistentFolder = FolderModel.getRandomFolderModel(); + nonExistentFolder.setNodeRef("fake-id"); + restClient.authenticateUser(siteOwner) + .withCoreAPI() + .usingResource(nonExistentFolder) + .usingIsInheritanceEnabledRuleSetting() + .retrieveSetting(); + + restClient.assertLastError().statusCodeIs(NOT_FOUND) + .containsSummary("The entity with id: fake-id was not found"); + } + + /** Check we get an error when trying to retrieve a non-existent setting. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void getNonExistentSetting() + { + STEP("Create a folder for the test."); + FolderModel folder = dataContent.usingUser(siteOwner).usingSite(site).createFolder(); + + STEP("Try to get a fake setting from the folder."); + restClient.authenticateUser(siteOwner).withCoreAPI().usingResource(folder).usingRuleSetting("-fakeRuleSetting-") + .retrieveSetting(); + + restClient.assertLastError().statusCodeIs(NOT_FOUND) + .containsSummary("Unrecognised rule setting key -fakeRuleSetting-"); + } + + /** Check a user without permission for the folder cannot get the -isInheritanceEnabled- rule setting. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void getIsInheritedWithoutPermission() + { + STEP("Create a folder and a user without permission to access it."); + FolderModel folder = dataContent.usingUser(siteOwner).usingSite(site).createFolder(); + UserModel noPermissionUser = dataUser.createRandomTestUser(); + + STEP("Try to get the -isInheritanceEnabled- setting without permission."); + restClient.authenticateUser(noPermissionUser) + .withCoreAPI() + .usingResource(folder) + .usingIsInheritanceEnabledRuleSetting() + .retrieveSetting(); + + restClient.assertLastError().statusCodeIs(FORBIDDEN) + .containsSummary("Cannot read from this node"); + } + + /** Check we can change the -isInheritanceEnabled- rule setting for the folder. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void updateIsInherited() + { + STEP("Create a folder for the test."); + FolderModel folder = dataContent.usingUser(siteOwner).usingSite(site).createFolder(); + + STEP("Set -isInheritanceEnabled- to false."); + RestRuleSettingsModel updateBody = new RestRuleSettingsModel(); + updateBody.setValue(false); + + RestRuleSettingsModel ruleSettingsModel = restClient.authenticateUser(siteOwner) + .withCoreAPI() + .usingResource(folder) + .usingIsInheritanceEnabledRuleSetting() + .updateSetting(updateBody); + + restClient.assertStatusCodeIs(OK); + RestRuleSettingsModel expected = new RestRuleSettingsModel(); + expected.setKey(IS_INHERITANCE_ENABLED); + expected.setValue(false); + ruleSettingsModel.assertThat().isEqualTo(expected); + } + + /** Check we get an error when trying to set -isInheritanceEnabled- to something other than a boolean. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void updateInheritedWithBadValue() + { + STEP("Create a folder for the test."); + FolderModel folder = dataContent.usingUser(siteOwner).usingSite(site).createFolder(); + + STEP("Try to set -isInheritanceEnabled- to \"banana\"."); + RestRuleSettingsModel updateBody = new RestRuleSettingsModel(); + updateBody.setValue("banana"); + + restClient.authenticateUser(siteOwner).withCoreAPI().usingResource(folder).usingIsInheritanceEnabledRuleSetting() + .updateSetting(updateBody); + + restClient.assertLastError().statusCodeIs(BAD_REQUEST) + .containsSummary("Rule setting " + IS_INHERITANCE_ENABLED + " requires a boolean value."); + } + + /** Check we get an error when the folder is not found. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void updateInheritedWithNonExistentFolder() + { + STEP("Try to set -isInheritanceEnabled- against a fake folder."); + FolderModel nonExistentFolder = FolderModel.getRandomFolderModel(); + nonExistentFolder.setNodeRef("fake-id"); + + RestRuleSettingsModel updateBody = new RestRuleSettingsModel(); + updateBody.setValue(true); + + restClient.authenticateUser(siteOwner).withCoreAPI().usingResource(nonExistentFolder).usingIsInheritanceEnabledRuleSetting() + .updateSetting(updateBody); + + restClient.assertLastError().statusCodeIs(NOT_FOUND) + .containsSummary("The entity with id: fake-id was not found"); + } + + /** Check we get an error when trying to set a non-existent setting. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void updateNonExistentSetting() + { + STEP("Create a folder for the test."); + FolderModel folder = dataContent.usingUser(siteOwner).usingSite(site).createFolder(); + + STEP("Try to set a fake setting on the folder."); + RestRuleSettingsModel updateBody = new RestRuleSettingsModel(); + updateBody.setValue(true); + + restClient.authenticateUser(siteOwner).withCoreAPI().usingResource(folder).usingRuleSetting("-fakeRuleSetting-") + .updateSetting(updateBody); + + restClient.assertLastError().statusCodeIs(NOT_FOUND) + .containsSummary("Unrecognised rule setting key -fakeRuleSetting-"); + } + + /** Check a user without manage permission cannot update the -isInheritanceEnabled- rule setting. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void updateIsInheritedWithoutPermission() + { + STEP("Create a folder and a collaborator."); + FolderModel folder = dataContent.usingUser(siteOwner).usingSite(site).createFolder(); + UserModel collaborator = dataUser.createRandomTestUser(); + collaborator.setUserRole(SiteCollaborator); + restClient.authenticateUser(siteOwner).withCoreAPI().usingSite(site).addPerson(collaborator); + + STEP("Try to update the -isInheritanceEnabled- setting without permission."); + RestRuleSettingsModel updateBody = new RestRuleSettingsModel(); + updateBody.setValue(true); + + restClient.authenticateUser(collaborator).withCoreAPI().usingResource(folder).usingIsInheritanceEnabledRuleSetting() + .updateSetting(updateBody); + + restClient.assertLastError().statusCodeIs(FORBIDDEN) + .containsSummary("Insufficient permissions to manage rules"); + } +} diff --git a/pom.xml b/pom.xml index 38b3ecffe1..d40f66ff05 100644 --- a/pom.xml +++ b/pom.xml @@ -120,7 +120,7 @@ 2.7.4 3.0.49 5.1.1 - 1.110 + 1.114 1.32 1.9 1.7 diff --git a/remote-api/src/main/java/org/alfresco/rest/api/RuleSettings.java b/remote-api/src/main/java/org/alfresco/rest/api/RuleSettings.java new file mode 100644 index 0000000000..13e8b5a07e --- /dev/null +++ b/remote-api/src/main/java/org/alfresco/rest/api/RuleSettings.java @@ -0,0 +1,54 @@ +/* + * #%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 . + * #L% + */ +package org.alfresco.rest.api; + +import org.alfresco.rest.api.model.rules.RuleSetting; +import org.alfresco.service.Experimental; + +/** + * Rule settings API. + */ +@Experimental +public interface RuleSettings +{ + /** + * Get the rule setting with the given key. + * + * @param folderId Folder node ID + * @param ruleSettingKey Rule setting key + * @return {@link RuleSetting} The retrieved rule setting object. + */ + RuleSetting getRuleSetting(String folderId, String ruleSettingKey); + + /** + * Set the rule setting against the specified folder. + * + * @param folderId The folder to update. + * @param ruleSetting The new rule setting. + * @return The updated rule setting object. + */ + RuleSetting setRuleSetting(String folderId, RuleSetting ruleSetting); +} diff --git a/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSettingsImpl.java b/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSettingsImpl.java new file mode 100644 index 0000000000..081bd8732b --- /dev/null +++ b/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSettingsImpl.java @@ -0,0 +1,107 @@ +/* + * #%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 . + * #L% + */ +package org.alfresco.rest.api.impl.rules; + +import static org.alfresco.repo.rule.RuleModel.ASPECT_IGNORE_INHERITED_RULES; +import static org.alfresco.rest.api.model.rules.RuleSetting.IS_INHERITANCE_ENABLED_KEY; + +import java.util.Collections; + +import org.alfresco.rest.api.RuleSettings; +import org.alfresco.rest.api.model.rules.RuleSetting; +import org.alfresco.rest.framework.core.exceptions.NotFoundException; +import org.alfresco.service.Experimental; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; + +@Experimental +public class RuleSettingsImpl implements RuleSettings +{ + private NodeValidator validator; + private NodeService nodeService; + + @Override + public RuleSetting getRuleSetting(String folderId, String ruleSettingKey) + { + NodeRef folderNode = validator.validateFolderNode(folderId, false); + switch (ruleSettingKey) + { + case IS_INHERITANCE_ENABLED_KEY: + return getIsInheritanceEnabled(folderNode); + default: + throw new NotFoundException("Unrecognised rule setting key " + ruleSettingKey); + } + } + + private RuleSetting getIsInheritanceEnabled(NodeRef folderNode) + { + boolean inheritanceDisabled = nodeService.hasAspect(folderNode, ASPECT_IGNORE_INHERITED_RULES); + return RuleSetting.builder().key(IS_INHERITANCE_ENABLED_KEY).value(!inheritanceDisabled).create(); + } + + @Override + public RuleSetting setRuleSetting(String folderId, RuleSetting ruleSetting) + { + NodeRef folderNode = validator.validateFolderNode(folderId, true); + + switch (ruleSetting.getKey()) + { + case IS_INHERITANCE_ENABLED_KEY: + return updateIsInheritanceEnabled(folderNode, ruleSetting.getValue()); + default: + throw new NotFoundException("Unrecognised rule setting key " + ruleSetting.getKey()); + } + } + + private RuleSetting updateIsInheritanceEnabled(NodeRef folderNode, Object value) + { + if (!(value instanceof Boolean)) + { + throw new IllegalArgumentException("Rule setting " + IS_INHERITANCE_ENABLED_KEY + " requires a boolean value."); + } + + if ((boolean) value) + { + nodeService.removeAspect(folderNode, ASPECT_IGNORE_INHERITED_RULES); + } + else + { + nodeService.addAspect(folderNode, ASPECT_IGNORE_INHERITED_RULES, Collections.emptyMap()); + } + + return RuleSetting.builder().key(IS_INHERITANCE_ENABLED_KEY).value(value).create(); + } + + public void setValidator(NodeValidator validator) + { + this.validator = validator; + } + + public void setNodeService(NodeService nodeService) + { + this.nodeService = nodeService; + } +} diff --git a/remote-api/src/main/java/org/alfresco/rest/api/model/rules/RuleSet.java b/remote-api/src/main/java/org/alfresco/rest/api/model/rules/RuleSet.java index df1984673f..7cddaa45cc 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/model/rules/RuleSet.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/model/rules/RuleSet.java @@ -29,7 +29,6 @@ package org.alfresco.rest.api.model.rules; import java.util.Objects; import java.util.StringJoiner; -import org.alfresco.rest.api.People; import org.alfresco.service.Experimental; import org.alfresco.service.cmr.repository.NodeRef; diff --git a/remote-api/src/main/java/org/alfresco/rest/api/model/rules/RuleSetting.java b/remote-api/src/main/java/org/alfresco/rest/api/model/rules/RuleSetting.java new file mode 100644 index 0000000000..892a1f75d5 --- /dev/null +++ b/remote-api/src/main/java/org/alfresco/rest/api/model/rules/RuleSetting.java @@ -0,0 +1,126 @@ +/* + * #%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 . + * #L% + */ +package org.alfresco.rest.api.model.rules; + +import java.util.Objects; +import java.util.StringJoiner; + +import org.alfresco.rest.framework.resource.UniqueId; +import org.alfresco.service.Experimental; + +@Experimental +public class RuleSetting +{ + public static final String IS_INHERITANCE_ENABLED_KEY = "-isInheritanceEnabled-"; + + private String key; + private Object value; + + @UniqueId + public String getKey() + { + return key; + } + + public void setKey(String key) + { + this.key = key; + } + + public Object getValue() + { + return value; + } + + public void setValue(Object value) + { + this.value = value; + } + + @Override + public String toString() + { + return "RuleSetting{" + + new StringJoiner(", ") + .add("key=" + key) + .add("value=" + value.toString()) + .toString() + + "}"; + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (!(o instanceof RuleSetting)) + { + return false; + } + RuleSetting that = (RuleSetting) o; + return Objects.equals(key, that.key) + && Objects.equals(value, that.value); + } + + @Override + public int hashCode() + { + return Objects.hash(key, value); + } + + public static RuleSetting.Builder builder() + { + return new RuleSetting.Builder(); + } + + public static class Builder + { + private String key; + private Object value; + + public RuleSetting.Builder key(String key) + { + this.key = key; + return this; + } + + public RuleSetting.Builder value(Object value) + { + this.value = value; + return this; + } + + public RuleSetting create() + { + final RuleSetting ruleSetting = new RuleSetting(); + ruleSetting.setKey(key); + ruleSetting.setValue(value); + return ruleSetting; + } + } +} diff --git a/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeRuleSettingsRelation.java b/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeRuleSettingsRelation.java new file mode 100644 index 0000000000..9f77ac7945 --- /dev/null +++ b/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeRuleSettingsRelation.java @@ -0,0 +1,104 @@ +/* + * #%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 . + * #L% + */ +package org.alfresco.rest.api.nodes; + +import javax.servlet.http.HttpServletResponse; + +import org.alfresco.rest.api.RuleSettings; +import org.alfresco.rest.api.model.rules.RuleSetting; +import org.alfresco.rest.framework.WebApiDescription; +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; +import org.alfresco.service.Experimental; +import org.alfresco.util.PropertyCheck; +import org.springframework.beans.factory.InitializingBean; + +/** + * Folder node rule settings (rule inheritance). + */ +@Experimental +@RelationshipResource (name = "rule-settings", entityResource = NodesEntityResource.class, title = "Folder rule settings") +public class NodeRuleSettingsRelation implements RelationshipResourceAction.ReadById, + RelationshipResourceAction.Update, + InitializingBean +{ + private RuleSettings ruleSettings; + + @Override + public void afterPropertiesSet() throws Exception + { + PropertyCheck.mandatory(this, "ruleSettings", ruleSettings); + } + + /** + * Get the given configuration value for the specified folder. + *

+ * - GET /nodes/{folderId}/rule-settings/{ruleSettingKey} + * + * @param folderId The id of the folder. + * @param ruleSettingKey The setting to retrieve. + * @param parameters Unused. + * @return {@link RuleSetting} The current value of the setting. + */ + @WebApiDescription ( + title = "Get a folder node rule setting", + description = "Returns the specified rule setting for the given folder", + successStatus = HttpServletResponse.SC_OK + ) + @Override + public RuleSetting readById(String folderId, String ruleSettingKey, Parameters parameters) throws RelationshipResourceNotFoundException + { + return ruleSettings.getRuleSetting(folderId, ruleSettingKey); + } + + /** + * Set the value of a rule setting for the specified folder. + *

+ * PUT /nodes/{folderId}/rule-settings/{ruleSettingKey} + * + * @param folderId The id of the folder. + * @param ruleSetting The new value of the rule setting. + * @param parameters Unused. + * @return The updated rule setting. + */ + @WebApiDescription ( + title = "Update folder node rule setting", + description = "Update a rule setting for given node", + successStatus = HttpServletResponse.SC_OK + ) + @Override + public RuleSetting update(String folderId, RuleSetting ruleSetting, Parameters parameters) + { + return ruleSettings.setRuleSetting(folderId, ruleSetting); + } + + public void setRuleSettings(RuleSettings ruleSettings) + { + this.ruleSettings = ruleSettings; + } +} diff --git a/remote-api/src/main/resources/alfresco/public-rest-context.xml b/remote-api/src/main/resources/alfresco/public-rest-context.xml index 33990e8451..074af5be3b 100644 --- a/remote-api/src/main/resources/alfresco/public-rest-context.xml +++ b/remote-api/src/main/resources/alfresco/public-rest-context.xml @@ -912,6 +912,25 @@ + + + + + + + + + + + + + + + + + + + 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 06c6b39b53..16094b1aa1 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 @@ -63,12 +63,12 @@ import org.mockito.junit.MockitoJUnitRunner; @RunWith (MockitoJUnitRunner.class) public class RuleSetsImplTest extends TestCase { - private static final String FOLDER_NODE_ID = "dummy-folder-node-id"; + private static final String FOLDER_ID = "dummy-folder-id"; private static final String LINK_TO_NODE_ID = "dummy-link-to-node-id"; private static final String RULE_SET_ID = "dummy-rule-set-id"; - private static final NodeRef FOLDER_NODE_REF = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, FOLDER_NODE_ID); - private static final NodeRef LINK_TO_NODE_REF = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, LINK_TO_NODE_ID); - private static final NodeRef RULE_SET_NODE_REF = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, RULE_SET_ID); + private static final NodeRef FOLDER_NODE = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, FOLDER_ID); + private static final NodeRef LINK_TO_NODE = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, LINK_TO_NODE_ID); + private static final NodeRef RULE_SET_NODE = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, RULE_SET_ID); private static final Paging PAGING = Paging.DEFAULT; private static final List INCLUDES = List.of("dummy-includes"); @@ -94,24 +94,25 @@ public class RuleSetsImplTest extends TestCase public void setUp() { MockitoAnnotations.openMocks(this); - given(nodeValidatorMock.validateFolderNode(eq(LINK_TO_NODE_ID), anyBoolean())).willReturn(LINK_TO_NODE_REF); - given(nodeValidatorMock.validateFolderNode(eq(FOLDER_NODE_ID), anyBoolean())).willReturn(FOLDER_NODE_REF); - given(nodeValidatorMock.validateRuleSetNode(RULE_SET_ID, FOLDER_NODE_REF)).willReturn(RULE_SET_NODE_REF); - given(ruleServiceMock.getRuleSetNode(FOLDER_NODE_REF)).willReturn(RULE_SET_NODE_REF); - given(ruleSetLoaderMock.loadRuleSet(RULE_SET_NODE_REF, FOLDER_NODE_REF, INCLUDES)).willReturn(ruleSetMock); + given(nodeValidatorMock.validateFolderNode(eq(LINK_TO_NODE_ID), anyBoolean())).willReturn(LINK_TO_NODE); + given(nodeValidatorMock.validateFolderNode(eq(FOLDER_ID), anyBoolean())).willReturn(FOLDER_NODE); + given(nodeValidatorMock.validateRuleSetNode(RULE_SET_ID, FOLDER_NODE)).willReturn(RULE_SET_NODE); + + given(ruleServiceMock.getRuleSetNode(FOLDER_NODE)).willReturn(RULE_SET_NODE); + given(ruleSetLoaderMock.loadRuleSet(RULE_SET_NODE, FOLDER_NODE, INCLUDES)).willReturn(ruleSetMock); } @Test public void testGetRuleSets() { // Call the method under test. - CollectionWithPagingInfo actual = ruleSets.getRuleSets(FOLDER_NODE_ID, INCLUDES, PAGING); + CollectionWithPagingInfo actual = ruleSets.getRuleSets(FOLDER_ID, INCLUDES, PAGING); - then(nodeValidatorMock).should().validateFolderNode(FOLDER_NODE_ID, false); + then(nodeValidatorMock).should().validateFolderNode(FOLDER_ID, false); then(nodeValidatorMock).shouldHaveNoMoreInteractions(); - then(ruleServiceMock).should().getRuleSetNode(FOLDER_NODE_REF); + then(ruleServiceMock).should().getRuleSetNode(FOLDER_NODE); then(ruleServiceMock).shouldHaveNoMoreInteractions(); Collection expected = List.of(ruleSetMock); @@ -123,15 +124,15 @@ public class RuleSetsImplTest extends TestCase public void testGetZeroRuleSets() { // Simulate no rule sets for the folder. - given(ruleServiceMock.getRuleSetNode(FOLDER_NODE_REF)).willReturn(null); + given(ruleServiceMock.getRuleSetNode(FOLDER_NODE)).willReturn(null); // Call the method under test. - CollectionWithPagingInfo actual = ruleSets.getRuleSets(FOLDER_NODE_ID, INCLUDES, PAGING); + CollectionWithPagingInfo actual = ruleSets.getRuleSets(FOLDER_ID, INCLUDES, PAGING); - then(nodeValidatorMock).should().validateFolderNode(FOLDER_NODE_ID, false); + then(nodeValidatorMock).should().validateFolderNode(FOLDER_ID, false); then(nodeValidatorMock).shouldHaveNoMoreInteractions(); - then(ruleServiceMock).should().getRuleSetNode(FOLDER_NODE_REF); + then(ruleServiceMock).should().getRuleSetNode(FOLDER_NODE); then(ruleServiceMock).shouldHaveNoMoreInteractions(); assertEquals(emptyList(), actual.getCollection()); @@ -142,10 +143,10 @@ public class RuleSetsImplTest extends TestCase public void testGetRuleSetById() { // Call the method under test. - RuleSet actual = ruleSets.getRuleSetById(FOLDER_NODE_ID, RULE_SET_ID, INCLUDES); + RuleSet actual = ruleSets.getRuleSetById(FOLDER_ID, RULE_SET_ID, INCLUDES); - then(nodeValidatorMock).should().validateFolderNode(FOLDER_NODE_ID, false); - then(nodeValidatorMock).should().validateRuleSetNode(RULE_SET_ID, FOLDER_NODE_REF); + then(nodeValidatorMock).should().validateFolderNode(FOLDER_ID, false); + then(nodeValidatorMock).should().validateRuleSetNode(RULE_SET_ID, FOLDER_NODE); then(nodeValidatorMock).shouldHaveNoMoreInteractions(); assertEquals(ruleSetMock, actual); @@ -161,28 +162,28 @@ public class RuleSetsImplTest extends TestCase given(assocRef.getChildRef()).willReturn(childNodeRef); //when - assertEquals(ruleSets.linkToRuleSet(FOLDER_NODE_ID,LINK_TO_NODE_ID).getId(), childNodeRef.getId()); + assertEquals(ruleSets.linkToRuleSet(FOLDER_ID,LINK_TO_NODE_ID).getId(), childNodeRef.getId()); - then(ruleServiceMock).should().hasRules(LINK_TO_NODE_REF); - then(ruleServiceMock).should().hasRules(FOLDER_NODE_REF); - then(runtimeRuleServiceMock).should().getSavedRuleFolderAssoc(LINK_TO_NODE_REF); + then(ruleServiceMock).should().hasRules(LINK_TO_NODE); + then(ruleServiceMock).should().hasRules(FOLDER_NODE); + then(runtimeRuleServiceMock).should().getSavedRuleFolderAssoc(LINK_TO_NODE); then(runtimeRuleServiceMock).shouldHaveNoMoreInteractions(); - then(nodeServiceMock).should().addChild(FOLDER_NODE_REF, childNodeRef, RuleModel.ASSOC_RULE_FOLDER, RuleModel.ASSOC_RULE_FOLDER); + then(nodeServiceMock).should().addChild(FOLDER_NODE, childNodeRef, RuleModel.ASSOC_RULE_FOLDER, RuleModel.ASSOC_RULE_FOLDER); then(nodeServiceMock).shouldHaveNoMoreInteractions(); } @Test public void testLinkToRuleSet_targetFolderHasNoRules() { - given(ruleServiceMock.hasRules(LINK_TO_NODE_REF)).willReturn(false); + given(ruleServiceMock.hasRules(LINK_TO_NODE)).willReturn(false); //when assertThatExceptionOfType(InvalidArgumentException.class).isThrownBy( - () -> ruleSets.linkToRuleSet(FOLDER_NODE_ID, LINK_TO_NODE_ID) + () -> ruleSets.linkToRuleSet(FOLDER_ID, LINK_TO_NODE_ID) ); then(nodeServiceMock).shouldHaveNoMoreInteractions(); - then(ruleServiceMock).should().hasRules(LINK_TO_NODE_REF); + then(ruleServiceMock).should().hasRules(LINK_TO_NODE); then(ruleServiceMock).shouldHaveNoMoreInteractions(); then(runtimeRuleServiceMock).shouldHaveNoInteractions(); } @@ -194,11 +195,11 @@ public class RuleSetsImplTest extends TestCase //when assertThatExceptionOfType(InvalidArgumentException.class).isThrownBy( - () -> ruleSets.linkToRuleSet(FOLDER_NODE_ID, LINK_TO_NODE_ID)); + () -> ruleSets.linkToRuleSet(FOLDER_ID, LINK_TO_NODE_ID)); then(nodeServiceMock).shouldHaveNoMoreInteractions(); - then(ruleServiceMock).should().hasRules(LINK_TO_NODE_REF); - then(ruleServiceMock).should().hasRules(FOLDER_NODE_REF); + then(ruleServiceMock).should().hasRules(LINK_TO_NODE); + then(ruleServiceMock).should().hasRules(FOLDER_NODE); then(ruleServiceMock).shouldHaveNoMoreInteractions(); then(runtimeRuleServiceMock).shouldHaveNoInteractions(); } diff --git a/remote-api/src/test/java/org/alfresco/rest/api/impl/rules/RuleSettingsImplTest.java b/remote-api/src/test/java/org/alfresco/rest/api/impl/rules/RuleSettingsImplTest.java new file mode 100644 index 0000000000..0f635a73c1 --- /dev/null +++ b/remote-api/src/test/java/org/alfresco/rest/api/impl/rules/RuleSettingsImplTest.java @@ -0,0 +1,145 @@ +/* + * #%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 . + * #L% + */ +package org.alfresco.rest.api.impl.rules; + +import static java.util.Collections.emptyMap; + +import static org.alfresco.repo.rule.RuleModel.ASPECT_IGNORE_INHERITED_RULES; +import static org.alfresco.rest.api.model.rules.RuleSetting.IS_INHERITANCE_ENABLED_KEY; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; + +import junit.framework.TestCase; +import org.alfresco.rest.api.model.rules.RuleSetting; +import org.alfresco.rest.framework.core.exceptions.NotFoundException; +import org.alfresco.service.Experimental; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.repository.StoreRef; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +/** Unit tests for {@link RuleSettingsImpl}. */ +@Experimental +@RunWith (MockitoJUnitRunner.class) +public class RuleSettingsImplTest extends TestCase +{ + private static final String FOLDER_ID = "dummy-folder-id"; + private static final NodeRef FOLDER_NODE = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, FOLDER_ID); + + @InjectMocks + private RuleSettingsImpl ruleSettings; + @Mock + private NodeValidator nodeValidatorMock; + @Mock + private NodeService nodeServiceMock; + + @Before + @Override + public void setUp() + { + given(nodeValidatorMock.validateFolderNode(eq(FOLDER_ID), anyBoolean())).willReturn(FOLDER_NODE); + } + + @Test + public void testGetRuleSetting_disabled() + { + given(nodeServiceMock.hasAspect(FOLDER_NODE, ASPECT_IGNORE_INHERITED_RULES)).willReturn(true); + + // Call the method under test. + RuleSetting ruleSetting = ruleSettings.getRuleSetting(FOLDER_ID, IS_INHERITANCE_ENABLED_KEY); + + RuleSetting expected = RuleSetting.builder().key(IS_INHERITANCE_ENABLED_KEY).value(false).create(); + assertEquals(expected, ruleSetting); + } + + @Test + public void testGetRuleSetting_enabled() + { + given(nodeServiceMock.hasAspect(FOLDER_NODE, ASPECT_IGNORE_INHERITED_RULES)).willReturn(false); + + // Call the method under test. + RuleSetting ruleSetting = ruleSettings.getRuleSetting(FOLDER_ID, IS_INHERITANCE_ENABLED_KEY); + + RuleSetting expected = RuleSetting.builder().key(IS_INHERITANCE_ENABLED_KEY).value(true).create(); + assertEquals(expected, ruleSetting); + } + + @Test + public void testGetRuleSetting_unrecognisedKey() + { + assertThatExceptionOfType(NotFoundException.class) + .isThrownBy(() -> ruleSettings.getRuleSetting(FOLDER_ID, "-fakeSetting-")); + } + + @Test + public void testSetRuleSetting_enable() + { + RuleSetting ruleSetting = RuleSetting.builder().key(IS_INHERITANCE_ENABLED_KEY).value(true).create(); + + // Call the method under test. + RuleSetting actual = ruleSettings.setRuleSetting(FOLDER_ID, ruleSetting); + + assertEquals(ruleSetting, actual); + then(nodeServiceMock).should().removeAspect(FOLDER_NODE, ASPECT_IGNORE_INHERITED_RULES); + } + + @Test + public void testSetRuleSetting_disable() + { + RuleSetting ruleSetting = RuleSetting.builder().key(IS_INHERITANCE_ENABLED_KEY).value(false).create(); + + // Call the method under test. + RuleSetting actual = ruleSettings.setRuleSetting(FOLDER_ID, ruleSetting); + + assertEquals(ruleSetting, actual); + then(nodeServiceMock).should().addAspect(FOLDER_NODE, ASPECT_IGNORE_INHERITED_RULES, emptyMap()); + } + + @Test + public void testSetRuleSetting_unrecognisedKey() + { + RuleSetting ruleSetting = RuleSetting.builder().key("-fakeSetting-").value(true).create(); + assertThatExceptionOfType(NotFoundException.class) + .isThrownBy(() -> ruleSettings.setRuleSetting(FOLDER_ID, ruleSetting)); + } + + @Test + public void testSetRuleSetting_nonBooleanValue() + { + RuleSetting ruleSetting = RuleSetting.builder().key(IS_INHERITANCE_ENABLED_KEY).value(123456).create(); + + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> ruleSettings.setRuleSetting(FOLDER_ID, ruleSetting)); + } +}