diff --git a/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/actions/ActionsTests.java b/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/actions/ActionsTests.java index df822111de..fdfe88c263 100644 --- a/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/actions/ActionsTests.java +++ b/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/actions/ActionsTests.java @@ -2,10 +2,13 @@ package org.alfresco.rest.actions; import static org.testng.Assert.assertFalse; -import com.google.common.collect.ImmutableMap; +import java.util.List; +import com.google.common.collect.ImmutableMap; import org.alfresco.dataprep.CMISUtil; import org.alfresco.rest.RestTest; +import org.alfresco.rest.model.RestActionConstraintDataModel; +import org.alfresco.rest.model.RestActionConstraintModel; import org.alfresco.rest.model.RestActionDefinitionModel; import org.alfresco.rest.model.RestActionDefinitionModelsCollection; import org.alfresco.rest.model.RestNodeModel; @@ -148,4 +151,66 @@ public class ActionsTests extends RestTest restActionDefinition.getDescription().equals("This will add an aspect to the matched item."); restActionDefinition.getTitle().equals("Add aspect"); } + + @TestRail(section = {TestGroup.REST_API, TestGroup.ACTIONS}, executionType = ExecutionType.SANITY, + description = "Sanity test for ACTIONS endpoint GET action-conditions/{actionConstraintName}") + @Test(groups = {TestGroup.REST_API, TestGroup.ACTIONS, TestGroup.SANITY}) + public void testGetSingleActionConstraint() + { + final UserModel testUser = dataUser.createRandomTestUser(); + restClient.authenticateUser(testUser); + + final String compareOperationsName = "ac-compare-operations"; + final RestActionConstraintModel actionConstraintCompareOperations = + restClient.withCoreAPI().usingActions().getActionConstraintByName(compareOperationsName); + + restClient.assertStatusCodeIs(HttpStatus.OK); + + final RestActionConstraintModel expectedComparatorConstraints = new RestActionConstraintModel(); + expectedComparatorConstraints.setConstraintName(compareOperationsName); + expectedComparatorConstraints.setConstraintValues(getComparatorConstraints()); + actionConstraintCompareOperations.assertThat().isEqualTo(expectedComparatorConstraints); + } + + @TestRail(section = {TestGroup.REST_API, TestGroup.ACTIONS}, executionType = ExecutionType.SANITY, + description = "Sanity test for ACTIONS endpoint GET action-conditions/{actionConstraintName} - non existing constraint name") + @Test(groups = {TestGroup.REST_API, TestGroup.ACTIONS, TestGroup.SANITY}) + public void testGetSingleNonExistingActionConstraint() + { + final UserModel testUser = dataUser.createRandomTestUser(); + restClient.authenticateUser(testUser); + restClient.withCoreAPI().usingActions().getActionConstraintByName("dummy-name"); + + restClient.assertStatusCodeIs(HttpStatus.NOT_FOUND); + } + + private List getComparatorConstraints() + { + final RestActionConstraintDataModel equalsConstraint = new RestActionConstraintDataModel(); + equalsConstraint.setValue("EQUALS"); + equalsConstraint.setLabel("Equals"); + final RestActionConstraintDataModel containsConstraint = new RestActionConstraintDataModel(); + containsConstraint.setValue("CONTAINS"); + containsConstraint.setLabel("Contains"); + final RestActionConstraintDataModel beginsConstraint = new RestActionConstraintDataModel(); + beginsConstraint.setValue("BEGINS"); + beginsConstraint.setLabel("Begins With"); + final RestActionConstraintDataModel endsConstraint = new RestActionConstraintDataModel(); + endsConstraint.setValue("ENDS"); + endsConstraint.setLabel("Ends With"); + final RestActionConstraintDataModel greaterThanConstraint = new RestActionConstraintDataModel(); + greaterThanConstraint.setValue("GREATER_THAN"); + greaterThanConstraint.setLabel("Greater Than"); + final RestActionConstraintDataModel greaterThanEqualConstraint = new RestActionConstraintDataModel(); + greaterThanEqualConstraint.setValue("GREATER_THAN_EQUAL"); + greaterThanEqualConstraint.setLabel("Greater Than Or Equal To"); + final RestActionConstraintDataModel lessThanConstraint = new RestActionConstraintDataModel(); + lessThanConstraint.setValue("LESS_THAN"); + lessThanConstraint.setLabel("Less Than"); + final RestActionConstraintDataModel lessThanEqualConstraint = new RestActionConstraintDataModel(); + lessThanEqualConstraint.setValue("LESS_THAN_EQUAL"); + lessThanEqualConstraint.setLabel("Less Than Or Equal To"); + return List.of(equalsConstraint, containsConstraint, beginsConstraint, endsConstraint, greaterThanConstraint, + greaterThanEqualConstraint, lessThanConstraint, lessThanEqualConstraint); + } } diff --git a/pom.xml b/pom.xml index 4f6f334e08..167a273a28 100644 --- a/pom.xml +++ b/pom.xml @@ -123,7 +123,7 @@ 2.7.4 3.0.56 5.2.0 - 1.132 + 1.133 1.9 1.7 1.7 diff --git a/remote-api/src/main/java/org/alfresco/rest/api/Actions.java b/remote-api/src/main/java/org/alfresco/rest/api/Actions.java index e45fa3ce60..d829a7d4dd 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/Actions.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/Actions.java @@ -2,7 +2,7 @@ * #%L * Alfresco Remote API * %% - * Copyright (C) 2005 - 2018 Alfresco Software Limited + * 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 @@ -29,8 +29,10 @@ package org.alfresco.rest.api; import org.alfresco.rest.api.model.Action; import org.alfresco.rest.api.model.ActionDefinition; +import org.alfresco.rest.api.model.ActionParameterConstraint; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.resource.parameters.Parameters; +import org.alfresco.service.Experimental; import org.alfresco.service.cmr.repository.NodeRef; public interface Actions @@ -45,7 +47,10 @@ public interface Actions { NAME, TITLE - }; + } Action executeAction(Action action, Parameters parameters); + + @Experimental + ActionParameterConstraint getActionConstraint(String constraintName); } diff --git a/remote-api/src/main/java/org/alfresco/rest/api/actions/ActionParameterConstraintsEntityResource.java b/remote-api/src/main/java/org/alfresco/rest/api/actions/ActionParameterConstraintsEntityResource.java new file mode 100644 index 0000000000..117d214e3c --- /dev/null +++ b/remote-api/src/main/java/org/alfresco/rest/api/actions/ActionParameterConstraintsEntityResource.java @@ -0,0 +1,59 @@ +/* + * #%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.actions; + +import javax.servlet.http.HttpServletResponse; + +import org.alfresco.rest.api.Actions; +import org.alfresco.rest.api.model.ActionParameterConstraint; +import org.alfresco.rest.framework.WebApiDescription; +import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException; +import org.alfresco.rest.framework.resource.EntityResource; +import org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction; +import org.alfresco.rest.framework.resource.parameters.Parameters; +import org.alfresco.service.Experimental; + +@EntityResource(name="action-parameter-constraints", title = "Action parameter constraints") +@Experimental +public class ActionParameterConstraintsEntityResource implements EntityResourceAction.ReadById +{ + private final Actions actions; + + public ActionParameterConstraintsEntityResource(Actions actions) + { + this.actions = actions; + } + + @WebApiDescription(title = "Get single action parameter constraint", + description = "Retrieves a single action parameter constraint by constraint name", + successStatus = HttpServletResponse.SC_OK) + @Experimental + @Override + public ActionParameterConstraint readById(String constraintName, Parameters parameters) throws EntityNotFoundException + { + return actions.getActionConstraint(constraintName); + } +} diff --git a/remote-api/src/main/java/org/alfresco/rest/api/impl/ActionsImpl.java b/remote-api/src/main/java/org/alfresco/rest/api/impl/ActionsImpl.java index 2bc272ba0b..02298d6eb3 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/impl/ActionsImpl.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/impl/ActionsImpl.java @@ -27,15 +27,22 @@ package org.alfresco.rest.api.impl; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.repo.action.access.ActionAccessRestriction; +import org.alfresco.repo.action.constraint.FolderContentsParameterConstraint; import org.alfresco.rest.api.Actions; +import org.alfresco.rest.api.impl.rules.ActionParameterConverter; import org.alfresco.rest.api.model.Action; import org.alfresco.rest.api.model.ActionDefinition; +import org.alfresco.rest.api.model.ActionParameterConstraint; import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; +import org.alfresco.rest.framework.core.exceptions.NotFoundException; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; +import org.alfresco.rest.framework.resource.parameters.ListPage; import org.alfresco.rest.framework.resource.parameters.Parameters; import org.alfresco.rest.framework.resource.parameters.SortColumn; +import org.alfresco.service.Experimental; import org.alfresco.service.cmr.action.ActionService; +import org.alfresco.service.cmr.action.ParameterConstraint; import org.alfresco.service.cmr.action.ParameterDefinition; import org.alfresco.service.cmr.dictionary.DataTypeDefinition; import org.alfresco.service.cmr.dictionary.DictionaryException; @@ -59,6 +66,7 @@ import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -68,11 +76,14 @@ import static java.util.Comparator.nullsFirst; public class ActionsImpl implements Actions { + static final String CONSTRAINT_NOT_EXISTS = "Action parameter constraints for name %s do not exist."; + private ActionService actionService; private DictionaryService dictionaryService; private NamespaceService namespaceService; private NodeService nodeService; private NamespacePrefixResolver prefixResolver; + private ActionParameterConverter actionParameterConverter; public void setActionService(ActionService actionService) { @@ -99,6 +110,11 @@ public class ActionsImpl implements Actions this.prefixResolver = prefixResolver; } + public void setActionParameterConverter(ActionParameterConverter actionParameterConverter) + { + this.actionParameterConverter = actionParameterConverter; + } + @Override public ActionDefinition getActionDefinitionById(String actionDefinitionId) { @@ -125,10 +141,10 @@ public class ActionsImpl implements Actions return result; } - - private ActionDefinition getActionDefinition( + + private ActionDefinition getActionDefinition( org.alfresco.service.cmr.action.ActionDefinition actionDefinitionId) - { + { List paramDefs = actionDefinitionId. getParameterDefinitions(). @@ -145,7 +161,7 @@ public class ActionsImpl implements Actions actionDefinitionId.getTrackStatus(), paramDefs); } - + @Override public CollectionWithPagingInfo getActionDefinitions(NodeRef nodeRef, Parameters params) { @@ -174,7 +190,7 @@ public class ActionsImpl implements Actions sortKey = Actions.SortKey.valueOf(sorting.get(0).column.toUpperCase()); sortAsc = sorting.get(0).asc; } - + Comparator comparator; switch (sortKey) { @@ -187,7 +203,7 @@ public class ActionsImpl implements Actions default: throw new IllegalArgumentException("Invalid sort key, must be either 'title' or 'name'."); } - + if (!sortAsc) { comparator = comparator.reversed(); @@ -220,7 +236,7 @@ public class ActionsImpl implements Actions skip(skip). limit(maxItems). collect(Collectors.toList()); - + boolean hasMoreItems = actionDefinitions.size() > (skip + maxItems); return CollectionWithPagingInfo.asPaged( @@ -297,6 +313,52 @@ public class ActionsImpl implements Actions return result; } + @Override + @Experimental + public ActionParameterConstraint getActionConstraint(String constraintName) + { + final ParameterConstraint parameterConstraint = actionService.getParameterConstraint(constraintName); + if (Objects.isNull(parameterConstraint)) + { + throw new NotFoundException(String.format(CONSTRAINT_NOT_EXISTS, constraintName)); + } + return mapToActionConstraint(parameterConstraint); + } + + @Experimental + private ActionParameterConstraint mapToActionConstraint(ParameterConstraint parameterConstraint) + { + final ActionParameterConstraint constraint = new ActionParameterConstraint(); + constraint.setConstraintName(parameterConstraint.getName()); + constraint.setConstraintValues(getConstraintDataList(parameterConstraint)); + return constraint; + } + + @Experimental + private List getConstraintDataList(final ParameterConstraint parameterConstraint) + { + final Map constraintValues = parameterConstraint.getValues(); + if (parameterConstraint instanceof FolderContentsParameterConstraint) + { + return convertNodeRefConstraintValues(constraintValues).entrySet().stream() + .map(e -> new ActionParameterConstraint.ConstraintData(e.getKey(), e.getValue())) + .collect(Collectors.toList()); + } else + { + return constraintValues.entrySet().stream() + .map(e -> new ActionParameterConstraint.ConstraintData(e.getKey(), e.getValue())) + .collect(Collectors.toList()); + } + } + + @Experimental + private Map convertNodeRefConstraintValues(final Map inputValues) + { + return inputValues.entrySet().stream() + .collect(Collectors.toMap(e -> actionParameterConverter.convertParamFromServiceModel(new NodeRef(e.getKey())).toString(), + Map.Entry::getValue)); + } + private Map extractActionParams(org.alfresco.service.cmr.action.ActionDefinition actionDefinition, Map params) { Map parameterValues = new HashMap<>(); @@ -391,7 +453,7 @@ public class ActionsImpl implements Actions map(this::toShortQName). collect(Collectors.toList()); } - + private String toShortQName(QName type) { return type.toPrefixString(prefixResolver); 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 index f4830370a2..9ded55ecc4 100644 --- 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 @@ -57,7 +57,6 @@ public class RuleSetLoader private static final int MAX_LINKED_TO_BY_SIZE = 100; private NodeService nodeService; private RuleService ruleService; - private RestRuleModelMapper restRuleModelMapper; /** * Load a rule set for the given node ref. @@ -167,8 +166,4 @@ public class RuleSetLoader this.ruleService = ruleService; } - public void setRestRuleModelMapper(RestRuleModelMapper restRuleModelMapper) - { - this.restRuleModelMapper = restRuleModelMapper; - } } diff --git a/remote-api/src/main/java/org/alfresco/rest/api/model/ActionParameterConstraint.java b/remote-api/src/main/java/org/alfresco/rest/api/model/ActionParameterConstraint.java new file mode 100644 index 0000000000..c20815e422 --- /dev/null +++ b/remote-api/src/main/java/org/alfresco/rest/api/model/ActionParameterConstraint.java @@ -0,0 +1,99 @@ +/* + * #%L + * Alfresco Remote API + * %% + * Copyright (C) 2005 - 2021 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; + +import java.util.List; + +import org.alfresco.service.Experimental; + +/** + * Representation of action parameter constraint. + * Helps to constraint the list of allowable values for an action parameter. + * When action parameter has constraints defined (@see ActionDefinition.ParameterDefinition#getParameterConstraintName()) + * they will be listed here. + * + * @author mpichura + */ +@Experimental +public class ActionParameterConstraint +{ + /** + * Constraint name. + */ + private String constraintName; + /** + * List of objects representing constraint values along with additional data + */ + private List constraintValues; + + public List getConstraintValues() + { + return constraintValues; + } + + public void setConstraintValues(List constraintValues) + { + this.constraintValues = constraintValues; + } + + public String getConstraintName() + { + return constraintName; + } + + public void setConstraintName(String constraintName) + { + this.constraintName = constraintName; + } + + public static class ConstraintData + { + public ConstraintData(final String value, final String label) + { + this.value = value; + this.label = label; + } + /** + * Actual constraint value + */ + private String value; + /** + * A label associated to constraint's value + */ + private String label; + + public String getValue() + { + return value; + } + + public String getLabel() + { + return label; + } + } +} 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 f1de526fa1..500d7f317e 100644 --- a/remote-api/src/main/resources/alfresco/public-rest-context.xml +++ b/remote-api/src/main/resources/alfresco/public-rest-context.xml @@ -567,6 +567,7 @@ + @@ -584,6 +585,9 @@ + + + @@ -865,7 +869,6 @@ - diff --git a/remote-api/src/test/java/org/alfresco/rest/api/actions/ActionParameterConstraintsEntityResourceTest.java b/remote-api/src/test/java/org/alfresco/rest/api/actions/ActionParameterConstraintsEntityResourceTest.java new file mode 100644 index 0000000000..7a582c17c6 --- /dev/null +++ b/remote-api/src/test/java/org/alfresco/rest/api/actions/ActionParameterConstraintsEntityResourceTest.java @@ -0,0 +1,66 @@ +/* + * #%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.actions; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; + +import org.alfresco.rest.api.Actions; +import org.alfresco.rest.api.model.ActionParameterConstraint; +import org.alfresco.rest.framework.resource.parameters.Parameters; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class ActionParameterConstraintsEntityResourceTest +{ + @Mock + private Actions actionsMock; + @Mock + private Parameters parametersMock; + + @InjectMocks + private ActionParameterConstraintsEntityResource objectUnderTest; + + @Test + public void testReadById() { + final String name = "name"; + final ActionParameterConstraint dummyConstraint = new ActionParameterConstraint(); + given(actionsMock.getActionConstraint(name)).willReturn(dummyConstraint); + + //when + ActionParameterConstraint result = objectUnderTest.readById(name, parametersMock); + + then(actionsMock).should().getActionConstraint(name); + then(actionsMock).shouldHaveNoMoreInteractions(); + assertThat(result).isNotNull().usingRecursiveComparison().isEqualTo(dummyConstraint); + } +} diff --git a/remote-api/src/test/java/org/alfresco/rest/api/impl/ActionsImplTest.java b/remote-api/src/test/java/org/alfresco/rest/api/impl/ActionsImplTest.java new file mode 100644 index 0000000000..ef07315b43 --- /dev/null +++ b/remote-api/src/test/java/org/alfresco/rest/api/impl/ActionsImplTest.java @@ -0,0 +1,165 @@ +/* + * #%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; + +import static org.alfresco.rest.api.impl.ActionsImpl.CONSTRAINT_NOT_EXISTS; +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.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; + +import java.util.Map; + +import org.alfresco.repo.action.constraint.FolderContentsParameterConstraint; +import org.alfresco.rest.api.impl.rules.ActionParameterConverter; +import org.alfresco.rest.api.model.ActionParameterConstraint; +import org.alfresco.rest.framework.core.exceptions.NotFoundException; +import org.alfresco.rest.framework.resource.parameters.Parameters; +import org.alfresco.service.cmr.action.ActionService; +import org.alfresco.service.cmr.action.ParameterConstraint; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class ActionsImplTest +{ + private static final String NAME = "name"; + private static final String CONSTRAINT = "constraint"; + private static final String LABEL = "label"; + private static final String DISPLAY = "display "; + + @Mock + private ActionService actionServiceMock; + @Mock + private Parameters parametersMock; + @Mock + private ActionParameterConverter parameterConverterMock; + + @InjectMocks + private ActionsImpl objectUnderTest; + + @Test + public void testGetSingleActionConstraint() + { + final String name = "name"; + final String value = CONSTRAINT; + final String label = LABEL; + final Map values = Map.of(value, label); + final ParameterConstraint testConstraint = createTestConstraint(name, values); + given(actionServiceMock.getParameterConstraint(name)).willReturn(testConstraint); + + //when + final ActionParameterConstraint actualConstraint = objectUnderTest.getActionConstraint(name); + + then(parametersMock).shouldHaveNoInteractions(); + then(actionServiceMock).should().getParameterConstraint(name); + then(actionServiceMock).shouldHaveNoMoreInteractions(); + assertThat(actualConstraint).isNotNull(); + assertThat(actualConstraint.getConstraintName()).isEqualTo(testConstraint.getName()); + ActionParameterConstraint.ConstraintData expectedConstraintData = new ActionParameterConstraint.ConstraintData(value, label); + assertThat(actualConstraint.getConstraintValues()).isNotNull().hasSize(1); + ActionParameterConstraint.ConstraintData actualConstraintData = actualConstraint.getConstraintValues().get(0); + assertThat(actualConstraintData).usingRecursiveComparison().isEqualTo(expectedConstraintData); + } + + @Test + public void testGetSingleActionNodeConstraint() + { + final String name = "name1"; + final String dummyNodeId = "dummy-node-id"; + final String value = "workspace://DummyStore/" + dummyNodeId; + final Map values = Map.of(value, LABEL); + final FolderContentsParameterConstraint testConstraint = mock(FolderContentsParameterConstraint.class); + given(testConstraint.getName()).willReturn(name); + given(testConstraint.getValues()).willReturn(values); + given(actionServiceMock.getParameterConstraint(name)).willReturn(testConstraint); + given(parameterConverterMock.convertParamFromServiceModel(any())).willReturn(dummyNodeId); + + //when + final ActionParameterConstraint actualConstraint = objectUnderTest.getActionConstraint(name); + + then(parametersMock).shouldHaveNoInteractions(); + then(actionServiceMock).should().getParameterConstraint(name); + then(actionServiceMock).shouldHaveNoMoreInteractions(); + assertThat(actualConstraint).isNotNull(); + assertThat(actualConstraint.getConstraintName()).isEqualTo(testConstraint.getName()); + ActionParameterConstraint.ConstraintData expectedConstraintData = new ActionParameterConstraint.ConstraintData(dummyNodeId, LABEL); + assertThat(actualConstraint.getConstraintValues()).isNotNull().hasSize(1); + ActionParameterConstraint.ConstraintData actualConstraintData = actualConstraint.getConstraintValues().get(0); + assertThat(actualConstraintData).usingRecursiveComparison().isEqualTo(expectedConstraintData); + } + + @Test + public void testGetActionConstraintsWithNameFilterNonExistingConstraint() + { + final String name = "name"; + given(actionServiceMock.getParameterConstraint(name)).willReturn(null); + + //when + assertThatExceptionOfType(NotFoundException.class).isThrownBy(() -> objectUnderTest.getActionConstraint(name)) + .withMessageContaining(String.format(CONSTRAINT_NOT_EXISTS, name)); + + then(parametersMock).shouldHaveNoInteractions(); + then(actionServiceMock).should().getParameterConstraint(name); + then(actionServiceMock).shouldHaveNoMoreInteractions(); + } + + private ParameterConstraint createTestConstraint(final String name, final Map values) + { + return new ParameterConstraint() + { + @Override + public String getName() + { + return name; + } + + @Override + public boolean isValidValue(String value) + { + return true; + } + + @Override + public String getValueDisplayLabel(String value) + { + return DISPLAY + name; + } + + @Override + public Map getAllowableValues() + { + return values; + } + }; + } +} diff --git a/repository/src/main/java/org/alfresco/repo/action/constraint/AspectParameterConstraint.java b/repository/src/main/java/org/alfresco/repo/action/constraint/AspectParameterConstraint.java index 2d020c4c80..4e83a711ca 100644 --- a/repository/src/main/java/org/alfresco/repo/action/constraint/AspectParameterConstraint.java +++ b/repository/src/main/java/org/alfresco/repo/action/constraint/AspectParameterConstraint.java @@ -1,34 +1,34 @@ -/* - * #%L - * Alfresco Repository - * %% - * Copyright (C) 2005 - 2016 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% - */ +/* + * #%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.repo.action.constraint; -import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Objects; import org.alfresco.service.cmr.dictionary.AspectDefinition; import org.alfresco.service.cmr.dictionary.DictionaryService; @@ -55,19 +55,22 @@ public class AspectParameterConstraint extends BaseParameterConstraint * @see org.alfresco.service.cmr.action.ParameterConstraint#getAllowableValues() */ protected Map getAllowableValuesImpl() - { - Collection aspects = dictionaryService.getAllAspects(); - Map result = new LinkedHashMap(aspects.size()); - for (QName aspect : aspects) - { - AspectDefinition aspectDef = dictionaryService.getAspect(aspect); - if (aspectDef != null && aspectDef.getTitle(dictionaryService) != null) - { - result.put(aspect.toPrefixString(), aspectDef.getTitle(dictionaryService)); - } - } - return result; - } - - + { + final Map values = getValues(); + values.values().removeIf(Objects::isNull); + return values; + } + + @Override + public Map getValues() + { + return dictionaryService.getAllAspects().stream() + .collect(LinkedHashMap::new, (m, v) -> m.put(v.toPrefixString(), getTitle(v)), LinkedHashMap::putAll); + } + + private String getTitle(QName aspect) + { + final AspectDefinition aspectDef = dictionaryService.getAspect(aspect); + return aspectDef != null ? aspectDef.getTitle(dictionaryService) : null; + } } diff --git a/repository/src/main/java/org/alfresco/repo/action/constraint/PropertyParameterConstraint.java b/repository/src/main/java/org/alfresco/repo/action/constraint/PropertyParameterConstraint.java index c9da558482..03e747d9ba 100644 --- a/repository/src/main/java/org/alfresco/repo/action/constraint/PropertyParameterConstraint.java +++ b/repository/src/main/java/org/alfresco/repo/action/constraint/PropertyParameterConstraint.java @@ -1,34 +1,34 @@ -/* - * #%L - * Alfresco Repository - * %% - * Copyright (C) 2005 - 2016 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% - */ +/* + * #%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.repo.action.constraint; -import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Objects; import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.dictionary.PropertyDefinition; @@ -55,17 +55,22 @@ public class PropertyParameterConstraint extends BaseParameterConstraint * @see org.alfresco.service.cmr.action.ParameterConstraint#getAllowableValues() */ protected Map getAllowableValuesImpl() - { - Collection properties = dictionaryService.getAllProperties(null); - Map result = new LinkedHashMap(properties.size()); - for (QName property : properties) - { - PropertyDefinition propertyDef = dictionaryService.getProperty(property); - if (propertyDef != null && propertyDef.getTitle(dictionaryService) != null) - { - result.put(property.toPrefixString(), propertyDef.getTitle(dictionaryService)); - } - } - return result; - } + { + final Map values = getValues(); + values.values().removeIf(Objects::isNull); + return values; + } + + @Override + public Map getValues() + { + return dictionaryService.getAllProperties(null).stream() + .collect(LinkedHashMap::new, (m, v) -> m.put(v.toPrefixString(), getTitle(v)), LinkedHashMap::putAll); + } + + private String getTitle(QName property) + { + final PropertyDefinition propertyDef = dictionaryService.getProperty(property); + return propertyDef != null ? propertyDef.getTitle(dictionaryService) : null; + } } diff --git a/repository/src/main/java/org/alfresco/repo/action/constraint/TypeParameterConstraint.java b/repository/src/main/java/org/alfresco/repo/action/constraint/TypeParameterConstraint.java index 639c98e14c..72e49b5878 100644 --- a/repository/src/main/java/org/alfresco/repo/action/constraint/TypeParameterConstraint.java +++ b/repository/src/main/java/org/alfresco/repo/action/constraint/TypeParameterConstraint.java @@ -1,34 +1,35 @@ -/* - * #%L - * Alfresco Repository - * %% - * Copyright (C) 2005 - 2016 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% - */ +/* + * #%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.repo.action.constraint; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Objects; import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.dictionary.TypeDefinition; @@ -55,17 +56,22 @@ public class TypeParameterConstraint extends BaseParameterConstraint * @see org.alfresco.service.cmr.action.ParameterConstraint#getAllowableValues() */ protected Map getAllowableValuesImpl() - { - Collection types = dictionaryService.getAllTypes(); - Map result = new LinkedHashMap(types.size()); - for (QName type : types) - { - TypeDefinition typeDef = dictionaryService.getType(type); - if (typeDef != null && typeDef.getTitle(dictionaryService) != null) - { - result.put(type.toPrefixString(), typeDef.getTitle(dictionaryService)); - } - } - return result; - } + { + final Map values = getValues(); + values.values().removeIf(Objects::isNull); + return values; + } + + @Override + public Map getValues() + { + return dictionaryService.getAllTypes().stream() + .collect(LinkedHashMap::new, (m, v) -> m.put(v.toPrefixString(), getTitle(v)), LinkedHashMap::putAll); + } + + private String getTitle(QName type) + { + final TypeDefinition typeDef = dictionaryService.getType(type); + return typeDef != null ? typeDef.getTitle(dictionaryService) : null; + } } diff --git a/repository/src/main/java/org/alfresco/service/cmr/action/ParameterConstraint.java b/repository/src/main/java/org/alfresco/service/cmr/action/ParameterConstraint.java index 33baac175d..d6fcd69e5c 100644 --- a/repository/src/main/java/org/alfresco/service/cmr/action/ParameterConstraint.java +++ b/repository/src/main/java/org/alfresco/service/cmr/action/ParameterConstraint.java @@ -1,28 +1,28 @@ -/* - * #%L - * Alfresco Repository - * %% - * Copyright (C) 2005 - 2016 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% - */ +/* + * #%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.service.cmr.action; @@ -63,4 +63,12 @@ public interface ParameterConstraint * The implementers are expected to return allowed values in the insertion order. */ Map getAllowableValues(); + + /** + * Returns possible constraint values. + * By default returns getAllowableValues() to be backwards compatible. + */ + default Map getValues() { + return getAllowableValues(); + } }