mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
ACS-3855: Add validation for rule condition comparator. (#1538)
This commit is contained in:
@@ -64,6 +64,7 @@ public class RestRuleSimpleConditionModelMapper implements RestModelMapper<Simpl
|
||||
static final String FIELD_NOT_NULL = "Field in condition must not be blank";
|
||||
static final String PARAMETER_NOT_NULL = "Parameter in condition must not be blank";
|
||||
static final String COMPARATOR_NOT_NULL = "Comparator in condition must not be blank";
|
||||
static final String INVALID_COMPARATOR_VALUE = "Comparator value for condition is invalid: %s";
|
||||
private final NamespaceService namespaceService;
|
||||
private final Nodes nodes;
|
||||
|
||||
@@ -157,13 +158,23 @@ public class RestRuleSimpleConditionModelMapper implements RestModelMapper<Simpl
|
||||
parameterValues.put(ComparePropertyValueEvaluator.PARAM_PROPERTY, QName.createQName(field, namespaceService));
|
||||
}
|
||||
checkStringNotBlank(restModel.getComparator(), COMPARATOR_NOT_NULL);
|
||||
parameterValues.put(ComparePropertyValueEvaluator.PARAM_OPERATION, restModel.getComparator().toUpperCase());
|
||||
parameterValues.put(ComparePropertyValueEvaluator.PARAM_OPERATION, getComparatorValue(restModel.getComparator()));
|
||||
parameterValues.put(ComparePropertyValueEvaluator.PARAM_VALUE, parameter);
|
||||
break;
|
||||
}
|
||||
return new ActionConditionImpl(UUID.randomUUID().toString(), conditionDefinitionId, parameterValues);
|
||||
}
|
||||
|
||||
private String getComparatorValue(String comparator)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ComparePropertyValueOperation.valueOf(comparator.toUpperCase()).toString();
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new InvalidArgumentException(String.format(INVALID_COMPARATOR_VALUE, comparator));
|
||||
}
|
||||
}
|
||||
|
||||
private void checkStringNotBlank(final String string, final String message) {
|
||||
if (Strings.isBlank(string))
|
||||
{
|
||||
|
@@ -28,6 +28,7 @@ package org.alfresco.rest.api.impl.mapper.rules;
|
||||
|
||||
import static org.alfresco.rest.api.impl.mapper.rules.RestRuleSimpleConditionModelMapper.COMPARATOR_NOT_NULL;
|
||||
import static org.alfresco.rest.api.impl.mapper.rules.RestRuleSimpleConditionModelMapper.FIELD_NOT_NULL;
|
||||
import static org.alfresco.rest.api.impl.mapper.rules.RestRuleSimpleConditionModelMapper.INVALID_COMPARATOR_VALUE;
|
||||
import static org.alfresco.rest.api.impl.mapper.rules.RestRuleSimpleConditionModelMapper.PARAMETER_NOT_NULL;
|
||||
import static org.alfresco.rest.api.impl.mapper.rules.RestRuleSimpleConditionModelMapper.PARAM_CATEGORY;
|
||||
import static org.alfresco.rest.api.impl.mapper.rules.RestRuleSimpleConditionModelMapper.PARAM_MIMETYPE;
|
||||
@@ -366,6 +367,22 @@ public class RestRuleSimpleConditionModelMapperTest
|
||||
.hasMessageContaining(COMPARATOR_NOT_NULL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToServiceModel_invalidComparator()
|
||||
{
|
||||
final String comparator = "greaterthan";
|
||||
final SimpleCondition simpleConditionNullComparator = SimpleCondition.builder()
|
||||
.field("size")
|
||||
.comparator(comparator)
|
||||
.parameter("65000")
|
||||
.create();
|
||||
|
||||
// when
|
||||
assertThatThrownBy(() -> objectUnderTest.toServiceModel(simpleConditionNullComparator))
|
||||
.isInstanceOf(InvalidArgumentException.class)
|
||||
.hasMessageContaining(String.format(INVALID_COMPARATOR_VALUE, comparator));
|
||||
}
|
||||
|
||||
private static ActionCondition createActionCondition(final String actionDefinitionName)
|
||||
{
|
||||
return new ActionConditionImpl("fake-id", actionDefinitionName, createParameterValues());
|
||||
|
Reference in New Issue
Block a user