Commit a3387ee5 authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub

fix(operation) accept primitive types (#532)

accept primitive types from query expression in process variables

Closes [BS-16723](https://bonitasoft.atlassian.net/browse/BS-16723)
parent 9b423a99
......@@ -17,7 +17,9 @@ package org.bonitasoft.studio.expression.editor.operation;
import static org.bonitasoft.studio.assertions.StatusAssert.assertThat;
import static org.bonitasoft.studio.model.expression.builders.ExpressionBuilder.anExpression;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
import static org.bonitasoft.studio.model.process.builders.DataBuilder.aData;
import static org.bonitasoft.studio.model.process.builders.JavaObjectDataBuilder.aJavaObjectData;
import static org.bonitasoft.studio.model.process.builders.LongDataTypeBuilder.aLongDataType;
import java.util.ArrayList;
import java.util.List;
......@@ -111,14 +113,16 @@ public class OperationReturnTypesValidatorTest {
final IStatus status = validator.validate(expr);
Assertions.assertThat(status.isOK()).isFalse();
Assertions.assertThat(status.getMessage()).isEqualTo(Messages.incompatibleType + " " + Messages.messageOperationWithDocumentInForm);
Assertions.assertThat(status.getMessage())
.isEqualTo(Messages.incompatibleType + " " + Messages.messageOperationWithDocumentInForm);
final Task task = ProcessFactory.eINSTANCE.createTask();
task.getOperations().add(operation);
final IStatus taskStatus = validator.validate(expr);
Assertions.assertThat(taskStatus.isOK()).isFalse();
Assertions.assertThat(taskStatus.getMessage()).isEqualTo(Messages.incompatibleType + " " + Messages.messageOperationWithDocumentInTask);
Assertions.assertThat(taskStatus.getMessage())
.isEqualTo(Messages.incompatibleType + " " + Messages.messageOperationWithDocumentInTask);
}
......@@ -126,7 +130,8 @@ public class OperationReturnTypesValidatorTest {
public void should_not_fail_for_SetDocument_Operation_with_DocumentValue() {
final Expression leftOperand = ExpressionHelper.createDocumentExpressionWithDependency("doc");
final Expression rightOperand = ExpressionHelper.createGroovyScriptExpression("myDocumentValue", DocumentValue.class.getName());
final Expression rightOperand = ExpressionHelper.createGroovyScriptExpression("myDocumentValue",
DocumentValue.class.getName());
rightOperand.setContent("new DocumentValue()");
createOperation(leftOperand, rightOperand, ExpressionConstants.SET_DOCUMENT_OPERATOR);
......@@ -139,8 +144,9 @@ public class OperationReturnTypesValidatorTest {
@Test
public void should_not_fail_for_SetDocument_Operation_with_FileInputValue() {
final Expression leftOperand = ExpressionHelper.createDocumentExpressionWithDependency("doc");
final Expression rightOperand = ExpressionHelper.createContractInputExpression(aContractInput().withType(ContractInputType.FILE).withName("myFile")
.build());
final Expression rightOperand = ExpressionHelper
.createContractInputExpression(aContractInput().withType(ContractInputType.FILE).withName("myFile")
.build());
createOperation(leftOperand, rightOperand, ExpressionConstants.SET_DOCUMENT_OPERATOR);
final IStatus status = new OperationReturnTypesValidator().validate(rightOperand);
......@@ -179,10 +185,12 @@ public class OperationReturnTypesValidatorTest {
}
@Test
public void shouldValidateSetDocumentListOperation_add_error_message_when_right_operand_is_String_in_Operation_of_Task_Or_Form() throws Exception {
public void shouldValidateSetDocumentListOperation_add_error_message_when_right_operand_is_String_in_Operation_of_Task_Or_Form()
throws Exception {
final Expression leftOperand = ExpressionHelper.createListDocumentExpressionWithDependency("doc");
final Expression rightOperand = ExpressionHelper.createConstantExpression("toto", "toto", String.class.getName());
final Operation operation = createOperation(leftOperand, rightOperand, ExpressionConstants.SET_LIST_DOCUMENT_OPERATOR);
final Operation operation = createOperation(leftOperand, rightOperand,
ExpressionConstants.SET_LIST_DOCUMENT_OPERATOR);
final OperationReturnTypesValidator validator = new OperationReturnTypesValidator();
// test in a task operation
......@@ -191,14 +199,17 @@ public class OperationReturnTypesValidatorTest {
final IStatus status = validator.validate(rightOperand);
Assertions.assertThat(status.isOK()).isFalse();
Assertions.assertThat(status.getMessage()).isEqualTo(Messages.incompatibleType + " " + Messages.messageOperationWithListDocumentInTask);
Assertions.assertThat(status.getMessage())
.isEqualTo(Messages.incompatibleType + " " + Messages.messageOperationWithListDocumentInTask);
}
@Test
public void shouldValidateSetListDocumentOperation_add_info_message_when_expression_is_empty_in_Operation_of_Task_Or_Form() throws Exception {
public void shouldValidateSetListDocumentOperation_add_info_message_when_expression_is_empty_in_Operation_of_Task_Or_Form()
throws Exception {
final Expression leftOperand = ExpressionHelper.createListDocumentExpressionWithDependency("doc");
final Expression rightOperand = ExpressionHelper.createConstantExpression("", "", String.class.getName());
final Operation operation = createOperation(leftOperand, rightOperand, ExpressionConstants.SET_LIST_DOCUMENT_OPERATOR);
final Operation operation = createOperation(leftOperand, rightOperand,
ExpressionConstants.SET_LIST_DOCUMENT_OPERATOR);
final OperationReturnTypesValidator validator = new OperationReturnTypesValidator();
// Test task Action
......@@ -220,7 +231,8 @@ public class OperationReturnTypesValidatorTest {
}
@Test
public void shouldValidateSetDocumentOperation_add_info_message_when_rightOperandExpression_is_empty_in_Operation_of_Task_Or_Form() throws Exception {
public void shouldValidateSetDocumentOperation_add_info_message_when_rightOperandExpression_is_empty_in_Operation_of_Task_Or_Form()
throws Exception {
// set left operand
final Expression leftOperand = ExpressionHelper.createDocumentExpressionWithDependency("doc");
// set right operand
......@@ -249,9 +261,11 @@ public class OperationReturnTypesValidatorTest {
@Test
public void should_fail_when_assigning_a_business_object_in_a_process_data() throws Exception {
final Expression leftOperand = ExpressionHelper.createVariableExpression(aJavaObjectData().withName("employee").withClassname("org.test.Employee")
.build());
final Expression rightOperand = anExpression().withExpressionType(ExpressionConstants.QUERY_TYPE).withName("Employee.findById")
final Expression leftOperand = ExpressionHelper
.createVariableExpression(aJavaObjectData().withName("employee").withClassname("org.test.Employee")
.build());
final Expression rightOperand = anExpression().withExpressionType(ExpressionConstants.QUERY_TYPE)
.withName("Employee.findById")
.withContent("SELECT AN EMPLOYEE").withReturnType("org.test.Employee").build();
final Operation operation = createOperation(leftOperand, rightOperand, ExpressionConstants.ASSIGNMENT_OPERATOR);
......@@ -262,10 +276,13 @@ public class OperationReturnTypesValidatorTest {
}
@Test
public void should_fail_when_assigning_a_business_object_in_a_process_data_using_java_method_operator() throws Exception {
final Expression leftOperand = ExpressionHelper.createVariableExpression(aJavaObjectData().withName("employee").withClassname("org.test.Employee")
.build());
final Expression rightOperand = anExpression().withExpressionType(ExpressionConstants.QUERY_TYPE).withName("Employee.findById")
public void should_fail_when_assigning_a_business_object_in_a_process_data_using_java_method_operator()
throws Exception {
final Expression leftOperand = ExpressionHelper
.createVariableExpression(aJavaObjectData().withName("employee").withClassname("org.test.Employee")
.build());
final Expression rightOperand = anExpression().withExpressionType(ExpressionConstants.QUERY_TYPE)
.withName("Employee.findById")
.withContent("SELECT AN EMPLOYEE").withReturnType("org.test.Employee").build();
final Operation operation = createOperation(leftOperand, rightOperand, ExpressionConstants.JAVA_METHOD_OPERATOR);
......@@ -274,4 +291,19 @@ public class OperationReturnTypesValidatorTest {
StatusAssert.assertThat(status).isNotOK().hasMessage(Messages.cannotStoreBusinessObjectInProcessVariable);
}
@Test
public void should_not_fail_when_assigning_a_primitive_query_result_in_a_process_data() throws Exception {
final Expression leftOperand = ExpressionHelper
.createVariableExpression(aData().havingDataType(aLongDataType()).withName("nbEmployee").build());
final Expression rightOperand = anExpression().withExpressionType(ExpressionConstants.QUERY_TYPE)
.withName("Employee.countForfind")
.withContent("SELECT AN EMPLOYEE").withReturnType(Long.class.getName()).build();
final Operation operation = createOperation(leftOperand, rightOperand, ExpressionConstants.ASSIGNMENT_OPERATOR);
final OperationReturnTypesValidator validator = new OperationReturnTypesValidator();
final IStatus status = validator.validate(operation.getRightOperand());
StatusAssert.assertThat(status).isOK();
}
}
......@@ -16,6 +16,9 @@ package org.bonitasoft.studio.expression.editor.operation;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.bonitasoft.engine.bpm.contract.FileInputValue;
import org.bonitasoft.engine.bpm.document.DocumentValue;
......@@ -49,6 +52,12 @@ public class OperationReturnTypesValidator implements IExpressionValidator {
private Expression dataExpression;
private static Set<String> primitiveTypes = Stream.of(Long.class.getName(),
Integer.class.getName(),
Float.class.getName(),
Double.class.getName())
.collect(Collectors.toSet());
/*
* (non-Javadoc)
* @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
......@@ -104,7 +113,8 @@ public class OperationReturnTypesValidator implements IExpressionValidator {
}
if (ExpressionConstants.DOCUMENT_REF_TYPE.equals(dataExpression.getType())) {
return ValidationStatus.error(Messages.bind(Messages.incompatibleExpressionTypeForOperator,
typeLabelProvider.getText(dataExpression.getType()), operatorLabelProvider.getText(operation.getOperator())));
typeLabelProvider.getText(dataExpression.getType()),
operatorLabelProvider.getText(operation.getOperator())));
}
}
if (ExpressionConstants.MESSAGE_ID_TYPE.equals(operation.getRightOperand().getType())) {
......@@ -139,29 +149,33 @@ public class OperationReturnTypesValidator implements IExpressionValidator {
try {
Integer.valueOf(expressionContent);
} catch (final NumberFormatException e) {
return ValidationStatus.warning(Messages.bind(Messages.expressionValueNotCompatibleWithReturnType, expressionContent,
returnType));
return ValidationStatus.warning(
Messages.bind(Messages.expressionValueNotCompatibleWithReturnType, expressionContent,
returnType));
}
} else if (Double.class.getName().equals(returnType)) {
try {
Double.valueOf(expressionContent);
} catch (final NumberFormatException e) {
return ValidationStatus.warning(Messages.bind(Messages.expressionValueNotCompatibleWithReturnType, expressionContent,
returnType));
return ValidationStatus.warning(
Messages.bind(Messages.expressionValueNotCompatibleWithReturnType, expressionContent,
returnType));
}
} else if (Float.class.getName().equals(returnType)) {
try {
Float.valueOf(expressionContent);
} catch (final NumberFormatException e) {
return ValidationStatus.warning(Messages.bind(Messages.expressionValueNotCompatibleWithReturnType, expressionContent,
returnType));
return ValidationStatus.warning(
Messages.bind(Messages.expressionValueNotCompatibleWithReturnType, expressionContent,
returnType));
}
} else if (Long.class.getName().equals(returnType)) {
try {
Long.valueOf(expressionContent);
} catch (final NumberFormatException e) {
return ValidationStatus.warning(Messages.bind(Messages.expressionValueNotCompatibleWithReturnType, expressionContent,
returnType));
return ValidationStatus.warning(
Messages.bind(Messages.expressionValueNotCompatibleWithReturnType, expressionContent,
returnType));
}
}
}
......@@ -176,18 +190,27 @@ public class OperationReturnTypesValidator implements IExpressionValidator {
private boolean isInvalidQueryExpression(final Operation operation) {
if (leftOperandHasReferencedElement(operation)) {
final EObject data = operation.getLeftOperand().getReferencedElements().get(0);
return !(data instanceof BusinessObjectData) && Objects.equals(operation.getRightOperand().getType(), ExpressionConstants.QUERY_TYPE);
return !(data instanceof BusinessObjectData)
&& Objects.equals(operation.getRightOperand().getType(), ExpressionConstants.QUERY_TYPE)
&& !isPrimitive(operation.getRightOperand().getReturnType());
}
return false;
}
private boolean isPrimitive(String returnType) {
return primitiveTypes.contains(returnType);
}
private boolean leftOperandHasReferencedElement(final Operation operation) {
return !operation.getLeftOperand().getReferencedElements().isEmpty();
}
protected IStatus validateDeletionOperation(final Expression expression, final String expressionName, final Operation operation) {
if (!dataExpression.getReferencedElements().isEmpty() && !(dataExpression.getReferencedElements().get(0) instanceof BusinessObjectData)) {
return ValidationStatus.error(Messages.bind(Messages.incompatibleExpressionTypeForOperator, typeLabelProvider.getText(dataExpression.getType()),
protected IStatus validateDeletionOperation(final Expression expression, final String expressionName,
final Operation operation) {
if (!dataExpression.getReferencedElements().isEmpty()
&& !(dataExpression.getReferencedElements().get(0) instanceof BusinessObjectData)) {
return ValidationStatus.error(Messages.bind(Messages.incompatibleExpressionTypeForOperator,
typeLabelProvider.getText(dataExpression.getType()),
operatorLabelProvider.getText(operation.getOperator())));
}
return null;
......@@ -206,16 +229,20 @@ public class OperationReturnTypesValidator implements IExpressionValidator {
final String dataReturnType = storageExpression.getReturnType();
final String returnType = expression.getReturnType();
try {
final boolean isListType = listClass.equals(returnType) || List.class.isAssignableFrom(Class.forName(returnType));
final boolean isListType = listClass.equals(returnType)
|| List.class.isAssignableFrom(Class.forName(returnType));
if (!isListType && listClass.equals(dataReturnType)) {
if (isTask) {
return ValidationStatus.warning(Messages.incompatibleType + " " + Messages.messageOperationWithListDocumentInTask);
return ValidationStatus
.warning(Messages.incompatibleType + " " + Messages.messageOperationWithListDocumentInTask);
} else {
if (PlatformUtil.isACommunityBonitaProduct()) {
return ValidationStatus.warning(Messages.incompatibleType + " " + Messages.messageOperationWithListDocumentInFormInCommunity);
return ValidationStatus.warning(Messages.incompatibleType + " "
+ Messages.messageOperationWithListDocumentInFormInCommunity);
} else {
return ValidationStatus.warning(Messages.incompatibleType + " " + Messages.messageOperationWithListDocumentInForm);
return ValidationStatus.warning(
Messages.incompatibleType + " " + Messages.messageOperationWithListDocumentInForm);
}
}
} else {
......@@ -240,7 +267,8 @@ public class OperationReturnTypesValidator implements IExpressionValidator {
final String expressionName, final Operation operation) {
if (!ExpressionConstants.VARIABLE_TYPE.equals(dataExpression.getType())
&& !(dataExpression.getReferencedElements().get(0) instanceof BusinessObjectData)) {
return ValidationStatus.error(Messages.bind(Messages.incompatibleExpressionTypeForOperator, typeLabelProvider.getText(dataExpression.getType()),
return ValidationStatus.error(Messages.bind(Messages.incompatibleExpressionTypeForOperator,
typeLabelProvider.getText(dataExpression.getType()),
operatorLabelProvider.getText(operation.getOperator())));
}
return null;
......@@ -294,10 +322,14 @@ public class OperationReturnTypesValidator implements IExpressionValidator {
if (expression != null && expression.getContent() != null && !expression.getContent().isEmpty()) {
final String typeName = storageExpression.getReturnType();
final String actionExpressionReturnType = expression.getReturnType();
if (!((DocumentValue.class.getName().equals(actionExpressionReturnType) || FileInputValue.class.getName().equals(actionExpressionReturnType))
&& typeName.equals(String.class.getName()))) {
return isTask ? ValidationStatus.warning(Messages.incompatibleType + " " + Messages.messageOperationWithDocumentInTask) : ValidationStatus
.warning(Messages.incompatibleType + " " + Messages.messageOperationWithDocumentInForm);
if (!((DocumentValue.class.getName().equals(actionExpressionReturnType)
|| FileInputValue.class.getName().equals(actionExpressionReturnType))
&& typeName.equals(String.class.getName()))) {
return isTask
? ValidationStatus
.warning(Messages.incompatibleType + " " + Messages.messageOperationWithDocumentInTask)
: ValidationStatus
.warning(Messages.incompatibleType + " " + Messages.messageOperationWithDocumentInForm);
} else {
return ValidationStatus.ok();
}
......@@ -310,7 +342,8 @@ public class OperationReturnTypesValidator implements IExpressionValidator {
protected IStatus validateXPathOperation(final Expression expression,
final String expressionName, final Operation operation) {
if (!ExpressionConstants.VARIABLE_TYPE.equals(dataExpression.getType())) {
return ValidationStatus.error(Messages.bind(Messages.incompatibleExpressionTypeForOperator, typeLabelProvider.getText(dataExpression.getType()),
return ValidationStatus.error(Messages.bind(Messages.incompatibleExpressionTypeForOperator,
typeLabelProvider.getText(dataExpression.getType()),
operatorLabelProvider.getText(operation.getOperator())));
}
if (dataExpression != null && dataExpression.getContent() != null && !dataExpression.getContent().isEmpty()) {
......@@ -339,8 +372,10 @@ public class OperationReturnTypesValidator implements IExpressionValidator {
protected IStatus validateJavaMethodOperation(final Expression expression,
final String expressionName, final Operation operation) {
if (!ExpressionConstants.VARIABLE_TYPE.equals(dataExpression.getType())
|| !(!dataExpression.getReferencedElements().isEmpty() && dataExpression.getReferencedElements().get(0) instanceof JavaObjectData)) {
return ValidationStatus.error(Messages.bind(Messages.incompatibleExpressionTypeForOperator, typeLabelProvider.getText(dataExpression.getType()),
|| !(!dataExpression.getReferencedElements().isEmpty()
&& dataExpression.getReferencedElements().get(0) instanceof JavaObjectData)) {
return ValidationStatus.error(Messages.bind(Messages.incompatibleExpressionTypeForOperator,
typeLabelProvider.getText(dataExpression.getType()),
operatorLabelProvider.getText(operation.getOperator())));
}
if (!operation.getOperator().getInputTypes().isEmpty()) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment