diff --git a/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/GetRuleSetsTests.java b/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/GetRuleSetsTests.java index 4f116f6cd2..79257be1f5 100644 --- a/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/GetRuleSetsTests.java +++ b/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/GetRuleSetsTests.java @@ -53,6 +53,7 @@ public class GetRuleSetsTests extends RestTest private SiteModel site; private FolderModel ruleFolder; private RestRuleModel rule; + private String ruleSetId; @BeforeClass (alwaysRun = true) public void dataPreparation() @@ -66,6 +67,12 @@ public class GetRuleSetsTests extends RestTest RestRuleModel ruleModel = createRuleModel("ruleName"); rule = restClient.authenticateUser(user).withCoreAPI().usingNode(ruleFolder).usingDefaultRuleSet() .createSingleRule(ruleModel); + + STEP("Get the rule sets for the folder and find the rule set id"); + RestRuleSetModelsCollection ruleSets = restClient.authenticateUser(user).withCoreAPI().usingNode(ruleFolder) + .getListOfRuleSets(); + ruleSets.assertThat().entriesListCountIs(1); + ruleSetId = ruleSets.getEntries().get(0).onModel().getId(); } /** Check we can get an empty list of rule sets. */ @@ -108,16 +115,27 @@ public class GetRuleSetsTests extends RestTest restClient.assertStatusCodeIs(NOT_FOUND); } + /** Check we can get the id of the folder that owns a list of rule sets. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void getRuleSetsAndOwningFolders() + { + STEP("Get the rule sets and owning folders"); + RestRuleSetModelsCollection ruleSets = restClient.authenticateUser(user).withCoreAPI() + .usingNode(ruleFolder) + .usingParams("include=owningFolder") + .getListOfRuleSets(); + + restClient.assertStatusCodeIs(OK); + ruleSets.getEntries().get(0).onModel() + .assertThat().field("owningFolder").is(ruleFolder.getNodeRef()) + .assertThat().field("id").is(ruleSetId); + ruleSets.assertThat().entriesListCountIs(1); + } + /** Check we can get a rule set by its id. */ @Test (groups = { TestGroup.REST_API, TestGroup.RULES, TestGroup.SANITY }) public void getRuleSetById() { - STEP("Get the rule sets for the folder and find the rule set id"); - RestRuleSetModelsCollection ruleSets = restClient.authenticateUser(user).withCoreAPI().usingNode(ruleFolder) - .getListOfRuleSets(); - ruleSets.assertThat().entriesListCountIs(1); - String ruleSetId = ruleSets.getEntries().get(0).onModel().getId(); - STEP("Get the rule set using its rule set id"); RestRuleSetModel ruleSet = restClient.authenticateUser(user).withCoreAPI().usingNode(ruleFolder) .getRuleSet(ruleSetId); @@ -150,12 +168,27 @@ public class GetRuleSetsTests extends RestTest } /** Check we get 404 for a non-existing rule set id. */ - @Test (groups = { TestGroup.REST_API, TestGroup.RULES, TestGroup.SANITY }) + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) public void getRuleSetByNonExistingId() { STEP("Get the rule set using fake rule set id"); - String ruleSetId = "fake-rule-set-id"; - restClient.authenticateUser(user).withCoreAPI().usingNode(ruleFolder).getRuleSet(ruleSetId); + String fakeRuleSetId = "fake-rule-set-id"; + restClient.authenticateUser(user).withCoreAPI().usingNode(ruleFolder).getRuleSet(fakeRuleSetId); restClient.assertStatusCodeIs(NOT_FOUND); } + + /** Check we can get the id of the folder that owns a rule set. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void getRuleSetAndOwningFolder() + { + STEP("Get the rule set and owning folder"); + RestRuleSetModel ruleSet = restClient.authenticateUser(user).withCoreAPI() + .usingNode(ruleFolder) + .usingParams("include=owningFolder") + .getRuleSet(ruleSetId); + + restClient.assertStatusCodeIs(OK); + ruleSet.assertThat().field("owningFolder").is(ruleFolder.getNodeRef()) + .assertThat().field("id").is(ruleSetId); + } } diff --git a/pom.xml b/pom.xml index 5a97df2dcf..d02e536f9d 100644 --- a/pom.xml +++ b/pom.xml @@ -120,7 +120,7 @@ 2.7.4 3.0.49 3.3.0 - 1.101 + 1.102 1.31 1.8 1.6 diff --git a/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/NodeValidator.java b/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/NodeValidator.java index eaa3568f9c..13a57370bc 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/NodeValidator.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/NodeValidator.java @@ -37,12 +37,14 @@ import org.alfresco.rest.api.model.rules.RuleSet; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException; import org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException; +import org.alfresco.service.Experimental; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.rule.RuleService; import org.alfresco.service.cmr.security.PermissionService; import org.alfresco.service.namespace.QName; /** Responsible for validating nodes when working with rules. */ +@Experimental public class NodeValidator { private static final String RULE_SET_EXPECTED_TYPE_NAME = "rule set"; diff --git a/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetLoader.java b/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetLoader.java new file mode 100644 index 0000000000..32296098ff --- /dev/null +++ b/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetLoader.java @@ -0,0 +1,65 @@ +/* + * #%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 java.util.List; + +import org.alfresco.rest.api.model.rules.RuleSet; +import org.alfresco.service.Experimental; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; + +/** Responsible for converting a NodeRef into a {@link RuleSet} object. */ +@Experimental +public class RuleSetLoader +{ + private NodeService nodeService; + + /** + * Load a rule set for the given node ref. + * + * @param ruleSetNodeRef The rule set node. + * @param includes A list of fields to include. + * @return The rule set object. + */ + protected RuleSet loadRuleSet(NodeRef ruleSetNodeRef, List includes) + { + String ruleSetId = ruleSetNodeRef.getId(); + RuleSet ruleSet = RuleSet.of(ruleSetId); + + if (includes != null && includes.contains("owningFolder")) + { + NodeRef parentRef = nodeService.getPrimaryParent(ruleSetNodeRef).getParentRef(); + ruleSet.setOwningFolder(parentRef); + } + return ruleSet; + } + + public void setNodeService(NodeService nodeService) + { + this.nodeService = nodeService; + } +} diff --git a/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetsImpl.java b/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetsImpl.java index 6f05b225ef..89dfcb57e6 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetsImpl.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/impl/rules/RuleSetsImpl.java @@ -43,6 +43,7 @@ import org.alfresco.service.cmr.rule.RuleService; @Experimental public class RuleSetsImpl implements RuleSets { + private RuleSetLoader ruleSetLoader; private RuleService ruleService; private NodeValidator validator; @@ -53,8 +54,7 @@ public class RuleSetsImpl implements RuleSets NodeRef ruleSetNode = ruleService.getRuleSetNode(folderNode); List ruleSets = Optional.ofNullable(ruleSetNode) - .map(NodeRef::getId) - .map(RuleSet::of).stream().collect(toList()); + .map(nodeRef -> ruleSetLoader.loadRuleSet(nodeRef, includes)).stream().collect(toList()); return ListPage.of(ruleSets, paging); } @@ -65,7 +65,12 @@ public class RuleSetsImpl implements RuleSets NodeRef folderNode = validator.validateFolderNode(folderNodeId, false); NodeRef ruleSetNode = validator.validateRuleSetNode(ruleSetId, folderNode); - return RuleSet.of(ruleSetNode.getId()); + return ruleSetLoader.loadRuleSet(ruleSetNode, includes); + } + + public void setRuleSetLoader(RuleSetLoader ruleSetLoader) + { + this.ruleSetLoader = ruleSetLoader; } public void setValidator(NodeValidator validator) 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 de1fbab8cd..343c2bc13e 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,6 +29,7 @@ package org.alfresco.rest.api.model.rules; import java.util.Objects; import org.alfresco.service.Experimental; +import org.alfresco.service.cmr.repository.NodeRef; @Experimental public class RuleSet @@ -36,6 +37,7 @@ public class RuleSet public static final String DEFAULT_ID = "-default-"; private String id; + private NodeRef owningFolder; public static RuleSet of(String id) { @@ -62,6 +64,16 @@ public class RuleSet this.id = id; } + public NodeRef getOwningFolder() + { + return owningFolder; + } + + public void setOwningFolder(NodeRef owningFolder) + { + this.owningFolder = owningFolder; + } + @Override public String toString() { @@ -76,13 +88,14 @@ public class RuleSet if (o == null || getClass() != o.getClass()) return false; RuleSet ruleSet = (RuleSet) o; - return Objects.equals(id, ruleSet.id); + return Objects.equals(id, ruleSet.id) + && Objects.equals(owningFolder, ruleSet.owningFolder); } @Override public int hashCode() { - return Objects.hash(id); + return Objects.hash(id, owningFolder); } public static Builder builder() @@ -93,6 +106,7 @@ public class RuleSet public static class Builder { private String id; + private NodeRef owningFolder; public Builder id(String id) { @@ -100,10 +114,17 @@ public class RuleSet return this; } + public Builder owningFolder(NodeRef owningFolder) + { + this.owningFolder = owningFolder; + return this; + } + public RuleSet create() { final RuleSet ruleSet = new RuleSet(); ruleSet.setId(id); + ruleSet.setOwningFolder(owningFolder); return ruleSet; } } 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 ec123dbef4..8eabf12edc 100644 --- a/remote-api/src/main/resources/alfresco/public-rest-context.xml +++ b/remote-api/src/main/resources/alfresco/public-rest-context.xml @@ -860,7 +860,12 @@ + + + + + diff --git a/remote-api/src/test/java/org/alfresco/rest/api/impl/rules/RuleSetLoaderTest.java b/remote-api/src/test/java/org/alfresco/rest/api/impl/rules/RuleSetLoaderTest.java new file mode 100644 index 0000000000..54299b261e --- /dev/null +++ b/remote-api/src/test/java/org/alfresco/rest/api/impl/rules/RuleSetLoaderTest.java @@ -0,0 +1,91 @@ +/* + * #%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.mockito.BDDMockito.given; + +import java.util.List; + +import junit.framework.TestCase; +import org.alfresco.rest.api.model.rules.RuleSet; +import org.alfresco.rest.framework.resource.parameters.Paging; +import org.alfresco.service.Experimental; +import org.alfresco.service.cmr.repository.ChildAssociationRef; +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 RuleSetLoader}. */ +@Experimental +@RunWith (MockitoJUnitRunner.class) +public class RuleSetLoaderTest extends TestCase +{ + private static final String FOLDER_ID = "dummy-folder-id"; + private static final String RULE_SET_ID = "dummy-rule-set-id"; + private static final NodeRef FOLDER_NODE = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, FOLDER_ID); + private static final NodeRef RULE_SET_NODE = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, RULE_SET_ID); + + @InjectMocks + private RuleSetLoader ruleSetLoader; + @Mock + private NodeService nodeServiceMock; + @Mock + private ChildAssociationRef ruleSetAssociationMock; + + @Before + @Override + public void setUp() + { + given(ruleSetAssociationMock.getParentRef()).willReturn(FOLDER_NODE); + given(nodeServiceMock.getPrimaryParent(RULE_SET_NODE)).willReturn(ruleSetAssociationMock); + } + + @Test + public void testLoadRuleSet_noIncludes() + { + // Call the method under test. + RuleSet actual = ruleSetLoader.loadRuleSet(RULE_SET_NODE, null); + + RuleSet expected = RuleSet.builder().id(RULE_SET_ID).create(); + assertEquals(expected, actual); + } + + @Test + public void testLoadRuleSet_includeOwningFolder() + { + // Call the method under test. + RuleSet actual = ruleSetLoader.loadRuleSet(RULE_SET_NODE, List.of("owningFolder")); + + RuleSet expected = RuleSet.builder().id(RULE_SET_ID).owningFolder(FOLDER_NODE).create(); + assertEquals(expected, actual); + } +} 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 d504af258a..bb2fba99a4 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 @@ -40,7 +40,9 @@ import org.alfresco.rest.api.model.rules.RuleSet; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.resource.parameters.Paging; import org.alfresco.service.Experimental; +import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.rule.RuleService; import org.junit.Before; @@ -61,13 +63,18 @@ public class RuleSetsImplTest extends TestCase private static final NodeRef FOLDER_NODE = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, FOLDER_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"); @InjectMocks private RuleSetsImpl ruleSets; @Mock + private RuleSetLoader ruleSetLoaderMock; + @Mock private NodeValidator nodeValidatorMock; @Mock private RuleService ruleServiceMock; + @Mock + private RuleSet ruleSetMock; @Before @Override @@ -80,13 +87,14 @@ public class RuleSetsImplTest extends TestCase 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, INCLUDES)).willReturn(ruleSetMock); } @Test public void testGetRuleSets() { // Call the method under test. - CollectionWithPagingInfo actual = ruleSets.getRuleSets(FOLDER_ID, null, PAGING); + CollectionWithPagingInfo actual = ruleSets.getRuleSets(FOLDER_ID, INCLUDES, PAGING); then(nodeValidatorMock).should().validateFolderNode(FOLDER_ID, false); then(nodeValidatorMock).shouldHaveNoMoreInteractions(); @@ -94,7 +102,7 @@ public class RuleSetsImplTest extends TestCase then(ruleServiceMock).should().getRuleSetNode(FOLDER_NODE); then(ruleServiceMock).shouldHaveNoMoreInteractions(); - Collection expected = List.of(RuleSet.of(RULE_SET_ID)); + Collection expected = List.of(ruleSetMock); assertEquals(expected, actual.getCollection()); assertEquals(PAGING, actual.getPaging()); } @@ -106,7 +114,7 @@ public class RuleSetsImplTest extends TestCase given(ruleServiceMock.getRuleSetNode(FOLDER_NODE)).willReturn(null); // Call the method under test. - CollectionWithPagingInfo actual = ruleSets.getRuleSets(FOLDER_ID, null, PAGING); + CollectionWithPagingInfo actual = ruleSets.getRuleSets(FOLDER_ID, INCLUDES, PAGING); then(nodeValidatorMock).should().validateFolderNode(FOLDER_ID, false); then(nodeValidatorMock).shouldHaveNoMoreInteractions(); @@ -122,12 +130,12 @@ public class RuleSetsImplTest extends TestCase public void testGetRuleSetById() { // Call the method under test. - RuleSet actual = ruleSets.getRuleSetById(FOLDER_ID, RULE_SET_ID, null); + RuleSet actual = ruleSets.getRuleSetById(FOLDER_ID, RULE_SET_ID, INCLUDES); then(nodeValidatorMock).should().validateFolderNode(FOLDER_ID, false); then(nodeValidatorMock).should().validateRuleSetNode(RULE_SET_ID, FOLDER_NODE); then(nodeValidatorMock).shouldHaveNoMoreInteractions(); - assertEquals(RuleSet.of(RULE_SET_ID), actual); + assertEquals(ruleSetMock, actual); } }