Commit 4f127fea authored by Aurelien Pupier's avatar Aurelien Pupier
Browse files

Merge pull request #264 from bonitasoft/BS-14255_generatedScriptDependenciesCorrection

Bs 14255 generated script dependencies correction
parents 161bb5fa 8d1212b3
......@@ -29,6 +29,7 @@ import org.bonitasoft.engine.bdm.model.field.SimpleField;
import org.bonitasoft.studio.common.ExpressionConstants;
import org.bonitasoft.studio.common.repository.RepositoryAccessor;
import org.bonitasoft.studio.contract.core.mapping.FieldToContractInputMapping;
import org.bonitasoft.studio.contract.core.mapping.operation.BusinessObjectInstantiationException;
import org.bonitasoft.studio.expression.editor.ExpressionEditorService;
import org.bonitasoft.studio.model.expression.Expression;
import org.bonitasoft.studio.model.expression.assertions.ExpressionAssert;
......@@ -36,6 +37,7 @@ import org.bonitasoft.studio.model.process.BusinessObjectData;
import org.bonitasoft.studio.model.process.ContractInput;
import org.bonitasoft.studio.model.process.ContractInputType;
import org.bonitasoft.studio.model.process.assertions.ContractInputAssert;
import org.eclipse.jdt.core.JavaModelException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
......@@ -109,4 +111,24 @@ public class FieldToContractInputMappingExpressionBuilderTest {
ContractInputAssert.assertThat((ContractInput) expression.getReferencedElements().get(0)).hasName("employee");
}
@Test
public void should_not_add_businessVariable_dependency_forInitializationScript() throws JavaModelException, BusinessObjectInstantiationException {
final FieldToContractInputMappingExpressionBuilder expressionBuilder = new FieldToContractInputMappingExpressionBuilder(repositoryAccessor,
expressionEditorService);
final RelationField address = aCompositionField("address", aBO("Address").build());
final FieldToContractInputMapping mapping = aRelationMapping(address).build();
mapping.toContractInput(aContractInput().withName("employee").withType(ContractInputType.COMPLEX).build());
final BusinessObjectData businessObjectData = aBusinessData().withName("myEmployee").build();
final Expression expression = expressionBuilder.toExpression(businessObjectData,
mapping, true);
ExpressionAssert.assertThat(expression)
.hasName("employee.address")
.hasContent("def addressVar = myEmployee.address == null ? new Address() : myEmployee.address" + System.lineSeparator() + "return addressVar")
.hasReturnType("Address")
.hasType(ExpressionConstants.SCRIPT_TYPE);
assertThat(expression.getReferencedElements()).hasSize(1);
}
}
......@@ -62,7 +62,7 @@ public class RootContractInputGenerator {
public void build(final BusinessObjectData data) throws OperationCreationException {
build(data, false);
}
private void build(final BusinessObjectData data, final boolean isOnPool) throws OperationCreationException {
contractInput = ProcessFactory.eINSTANCE.createContractInput();
contractInput.setName(rootContractInputName);
......
......@@ -62,7 +62,7 @@ public class FieldToContractInputMappingExpressionBuilder {
final MappingOperationScriptBuilder mappingOperationScriptBuilder = mapping.getScriptBuilder(data);
final String script = getScriptText(isOnPool, mappingOperationScriptBuilder);
final Expression scriptExpression = ExpressionHelper.createGroovyScriptExpression(script, mapping.getFieldType());
addScriptDependencies(mappingOperationScriptBuilder, mapping.getContractInput(), data, scriptExpression);
addScriptDependencies(mappingOperationScriptBuilder, mapping.getContractInput(), data, scriptExpression, isOnPool);
scriptExpression.setName(Joiner.on(".").join(toAncestorNameList().apply(contractInput)));
return scriptExpression;
}
......@@ -77,10 +77,10 @@ public class FieldToContractInputMappingExpressionBuilder {
}
private void addScriptDependencies(final MappingOperationScriptBuilder scriptBuilder, final ContractInput contractInput, final BusinessObjectData data,
final Expression groovyScriptExpression) throws JavaModelException {
final Expression groovyScriptExpression, final boolean isOnPool) throws JavaModelException {
groovyScriptExpression.getReferencedElements().add(
ExpressionHelper.createDependencyFromEObject(rootContractInput(contractInput)));
if (scriptBuilder.needsDataDependency()) {
if (scriptBuilder.needsDataDependency() && !isOnPool) {
groovyScriptExpression.getReferencedElements().add(
ExpressionHelper.createDependencyFromEObject(data));
}
......
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