Commit fb16f192 authored by Aurelien Pupier's avatar Aurelien Pupier
Browse files

BS-14249: support generation of script for initial value of Business

Variable of types containing composition multiple (clean code, use two
methods with better naming instead of providing a parameter)
parent f278a5e4
...@@ -92,7 +92,7 @@ public class RelationFieldToContractInputMappingTest { ...@@ -92,7 +92,7 @@ public class RelationFieldToContractInputMappingTest {
final MappingOperationScriptBuilder scriptBuilder = fieldToContractInputMapping.getScriptBuilder(aBusinessData().withName("myEmployee").build()); final MappingOperationScriptBuilder scriptBuilder = fieldToContractInputMapping.getScriptBuilder(aBusinessData().withName("myEmployee").build());
assertThat(scriptBuilder.toScript(false)).isEqualTo( assertThat(scriptBuilder.toScript()).isEqualTo(
"def addressVar = myEmployee.address == null ? new Address() : myEmployee.address" + System.lineSeparator() "def addressVar = myEmployee.address == null ? new Address() : myEmployee.address" + System.lineSeparator()
+ "addressVar.street = employee.address.street" + System.lineSeparator() + "addressVar.street = employee.address.street" + System.lineSeparator()
+ "return addressVar"); + "return addressVar");
......
...@@ -170,7 +170,7 @@ public class FieldToContractInputMappingOperationBuilderTest { ...@@ -170,7 +170,7 @@ public class FieldToContractInputMappingOperationBuilderTest {
final FieldToContractInputMapping mapping = spy(aRelationMapping(address).build()); final FieldToContractInputMapping mapping = spy(aRelationMapping(address).build());
mapping.toContractInput(aContractInput().withName("employee").withType(ContractInputType.COMPLEX).build()); mapping.toContractInput(aContractInput().withName("employee").withType(ContractInputType.COMPLEX).build());
final MappingOperationScriptBuilder fakeScriptBuilder = mock(MappingOperationScriptBuilder.class); final MappingOperationScriptBuilder fakeScriptBuilder = mock(MappingOperationScriptBuilder.class);
when(fakeScriptBuilder.toScript(false)).thenThrow(BusinessObjectInstantiationException.class); when(fakeScriptBuilder.toScript()).thenThrow(BusinessObjectInstantiationException.class);
doReturn(fakeScriptBuilder).when(mapping).getScriptBuilder(any(BusinessObjectData.class)); doReturn(fakeScriptBuilder).when(mapping).getScriptBuilder(any(BusinessObjectData.class));
when(expressionBuilder.toExpression(any(BusinessObjectData.class), any(FieldToContractInputMapping.class), anyBoolean())).thenReturn( when(expressionBuilder.toExpression(any(BusinessObjectData.class), any(FieldToContractInputMapping.class), anyBoolean())).thenReturn(
anExpression().build()); anExpression().build());
......
...@@ -44,7 +44,7 @@ public class MappingOperationScriptBuilderTest { ...@@ -44,7 +44,7 @@ public class MappingOperationScriptBuilderTest {
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(), final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
new SimpleFieldToContractInputMapping(firstNameField), firstNameField); new SimpleFieldToContractInputMapping(firstNameField), firstNameField);
final String script = scriptBuilder.toScript(false); final String script = scriptBuilder.toScript();
assertThat(scriptBuilder.needsDataDependency()).isFalse(); assertThat(scriptBuilder.needsDataDependency()).isFalse();
assertThat(script).isEqualTo("firstName"); assertThat(script).isEqualTo("firstName");
...@@ -59,7 +59,7 @@ public class MappingOperationScriptBuilderTest { ...@@ -59,7 +59,7 @@ public class MappingOperationScriptBuilderTest {
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(), final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
relationFieldToContractInputMapping, addressField); relationFieldToContractInputMapping, addressField);
final String script = scriptBuilder.toScript(false); final String script = scriptBuilder.toScript();
assertThat(scriptBuilder.needsDataDependency()).isTrue(); assertThat(scriptBuilder.needsDataDependency()).isTrue();
assertThat(script).isEqualTo("def addressVar = new Address()" + System.lineSeparator() assertThat(script).isEqualTo("def addressVar = new Address()" + System.lineSeparator()
...@@ -78,7 +78,7 @@ public class MappingOperationScriptBuilderTest { ...@@ -78,7 +78,7 @@ public class MappingOperationScriptBuilderTest {
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(), final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
relationFieldToContractInputMapping, addressField); relationFieldToContractInputMapping, addressField);
final String script = scriptBuilder.toScript(false); final String script = scriptBuilder.toScript();
assertThat(script).isEqualTo("def addressVar = new Address()" + System.lineSeparator() assertThat(script).isEqualTo("def addressVar = new Address()" + System.lineSeparator()
+ "return addressVar"); + "return addressVar");
...@@ -95,7 +95,7 @@ public class MappingOperationScriptBuilderTest { ...@@ -95,7 +95,7 @@ public class MappingOperationScriptBuilderTest {
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(), final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
relationFieldToContractInputMapping, addressField); relationFieldToContractInputMapping, addressField);
final String script = scriptBuilder.toScript(false); final String script = scriptBuilder.toScript();
assertThat(script).isEqualTo("def addressVar = new Address()" + System.lineSeparator() assertThat(script).isEqualTo("def addressVar = new Address()" + System.lineSeparator()
+ "addressVar.street = address.street" + System.lineSeparator() + "addressVar.street = address.street" + System.lineSeparator()
...@@ -126,6 +126,6 @@ public class MappingOperationScriptBuilderTest { ...@@ -126,6 +126,6 @@ public class MappingOperationScriptBuilderTest {
mapping, field); mapping, field);
thrown.expect(UnsupportedOperationException.class); thrown.expect(UnsupportedOperationException.class);
scriptBuilder.toScript(false); scriptBuilder.toScript();
} }
} }
...@@ -60,13 +60,22 @@ public class FieldToContractInputMappingExpressionBuilder { ...@@ -60,13 +60,22 @@ public class FieldToContractInputMappingExpressionBuilder {
throws BusinessObjectInstantiationException, JavaModelException { throws BusinessObjectInstantiationException, JavaModelException {
final ContractInput contractInput = mapping.getContractInput(); final ContractInput contractInput = mapping.getContractInput();
final MappingOperationScriptBuilder mappingOperationScriptBuilder = mapping.getScriptBuilder(data); final MappingOperationScriptBuilder mappingOperationScriptBuilder = mapping.getScriptBuilder(data);
final Expression scriptExpression = ExpressionHelper.createGroovyScriptExpression(mappingOperationScriptBuilder.toScript(isOnPool), final String script = getScriptText(isOnPool, mappingOperationScriptBuilder);
mapping.getFieldType()); final Expression scriptExpression = ExpressionHelper.createGroovyScriptExpression(script, mapping.getFieldType());
addScriptDependencies(mappingOperationScriptBuilder, mapping.getContractInput(), data, scriptExpression); addScriptDependencies(mappingOperationScriptBuilder, mapping.getContractInput(), data, scriptExpression);
scriptExpression.setName(Joiner.on(".").join(toAncestorNameList().apply(contractInput))); scriptExpression.setName(Joiner.on(".").join(toAncestorNameList().apply(contractInput)));
return scriptExpression; return scriptExpression;
} }
protected String getScriptText(final boolean isOnPool, final MappingOperationScriptBuilder mappingOperationScriptBuilder)
throws BusinessObjectInstantiationException {
if (isOnPool) {
return mappingOperationScriptBuilder.toInstanciationScript();
} else {
return mappingOperationScriptBuilder.toScript();
}
}
private void addScriptDependencies(final MappingOperationScriptBuilder scriptBuilder, final ContractInput contractInput, final BusinessObjectData data, private void addScriptDependencies(final MappingOperationScriptBuilder scriptBuilder, final ContractInput contractInput, final BusinessObjectData data,
final Expression groovyScriptExpression) throws JavaModelException { final Expression groovyScriptExpression) throws JavaModelException {
groovyScriptExpression.getReferencedElements().add( groovyScriptExpression.getReferencedElements().add(
......
...@@ -54,7 +54,15 @@ public class MappingOperationScriptBuilder { ...@@ -54,7 +54,15 @@ public class MappingOperationScriptBuilder {
this.field = field; this.field = field;
} }
public String toScript(final boolean isOnPool) throws BusinessObjectInstantiationException { public String toInstanciationScript() throws BusinessObjectInstantiationException {
return toScript(true);
}
public String toScript() throws BusinessObjectInstantiationException {
return toScript(false);
}
private String toScript(final boolean isOnPool) throws BusinessObjectInstantiationException {
mapping.getContractInput(); mapping.getContractInput();
return format(buildPropertyInitializerTree(mapping, field, data, isOnPool).getInitialValue()); return format(buildPropertyInitializerTree(mapping, field, data, isOnPool).getInitialValue());
} }
......
Supports Markdown
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