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

Merge pull request #296 from bonitasoft/fix/BS-14131_script_generation

BS-14131 Script generation with deep bdm.
parents b4aa2191 866cd922
......@@ -42,7 +42,7 @@ public class MappingOperationScriptBuilderTest {
public void should_not_need_data_dependency_for_a_simple_mapping() throws Exception {
final SimpleField firstNameField = aSimpleField().withName("firstName").ofType(FieldType.TEXT).build();
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
new SimpleFieldToContractInputMapping(firstNameField), firstNameField);
new SimpleFieldToContractInputMapping(firstNameField));
final String script = scriptBuilder.toScript();
......@@ -57,7 +57,7 @@ public class MappingOperationScriptBuilderTest {
final RelationFieldToContractInputMapping relationFieldToContractInputMapping = new RelationFieldToContractInputMapping(addressField);
relationFieldToContractInputMapping.addChild(new SimpleFieldToContractInputMapping(streetField));
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
relationFieldToContractInputMapping, addressField);
relationFieldToContractInputMapping);
final String script = scriptBuilder.toScript();
......@@ -76,7 +76,7 @@ public class MappingOperationScriptBuilderTest {
child.setGenerated(false);
relationFieldToContractInputMapping.addChild(child);
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
relationFieldToContractInputMapping, addressField);
relationFieldToContractInputMapping);
final String script = scriptBuilder.toScript();
......@@ -93,14 +93,14 @@ public class MappingOperationScriptBuilderTest {
relationFieldToContractInputMapping.addChild(new SimpleFieldToContractInputMapping(streetField));
relationFieldToContractInputMapping.addChild(new RelationFieldToContractInputMapping(countryField));
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
relationFieldToContractInputMapping, addressField);
relationFieldToContractInputMapping);
final String script = scriptBuilder.toScript();
assertThat(script).isEqualTo("def addressVar = new Address()" + System.lineSeparator()
+ "addressVar.street = address.street" + System.lineSeparator()
+ "addressVar.country = {" + System.lineSeparator()
+ SWT.TAB + "def countryVar = address.country == null ? new Country() : address.country" + System.lineSeparator()
+ SWT.TAB + "def countryVar = addressVar.country == null ? new Country() : addressVar.country" + System.lineSeparator()
+ SWT.TAB + "return countryVar}()" + System.lineSeparator()
+ "return addressVar");
}
......@@ -123,7 +123,7 @@ public class MappingOperationScriptBuilderTest {
};
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
mapping, field);
mapping);
thrown.expect(UnsupportedOperationException.class);
scriptBuilder.toScript();
......
......@@ -19,18 +19,21 @@ import static org.bonitasoft.studio.model.businessObject.BusinessObjectBuilder.a
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.anAggregationField;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
import org.bonitasoft.studio.contract.core.mapping.RelationFieldToContractInputMapping;
import org.junit.Test;
public class AggregationReferencePropertyInitializerTest {
@Test
public void should_call_query_in_a_closure() throws Exception {
final AggregationReferencePropertyInitializer propertyInitializer = new AggregationReferencePropertyInitializer(null, anAggregationField("country",
aBO("country").build()), aContractInput().withName("persistenceId").in(aContractInput().withName("country")).build(),
"", new VariableNameResolver());
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(anAggregationField("country",
aBO("country").build()));
context.setMapping(mapping);
context.setContractInput(aContractInput().withName("persistenceId").in(aContractInput().withName("country")).build());
context.setLocalVariableName("countryVar");
final String initialValue = propertyInitializer.getInitialValue();
final String initialValue = new AggregationReferencePropertyInitializer(null, context).getInitialValue();
assertThat(initialValue).isEqualTo("{" + System.lineSeparator()
+ "//Retrieve aggregated country using its DAO and persistenceId" + System.lineSeparator()
......
......@@ -18,9 +18,10 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.bonitasoft.studio.model.businessObject.BusinessObjectBuilder.aBO;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aStringField;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.anAggregationField;
import static org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder.aBusinessData;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
import org.bonitasoft.studio.contract.core.mapping.RelationFieldToContractInputMapping;
import org.bonitasoft.studio.model.process.ContractInputType;
import org.junit.Rule;
import org.junit.Test;
......@@ -33,10 +34,16 @@ public class BusinessObjectQueryInitializerTest {
@Test
public void should_generate_a_script_using_query_to_retrieve_a_business_object_in_initial_value() throws Exception {
final BusinessObjectQueryInitializer initializer = new BusinessObjectQueryInitializer(null, anAggregationField("country", aBO("org.test.Country")
.build()),
aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX).havingInput(aContractInput().withName("persistenceId")).build(),
"myCountry", new VariableNameResolver());
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(anAggregationField("country",
aBO("org.test.Country").build()));
context.setMapping(mapping);
context.setData(aBusinessData().withName("myCountry").build());
context.setContractInput(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)
.havingInput(aContractInput().withName("persistenceId")).build());
context.setLocalVariableName("countryVar");
final BusinessObjectQueryInitializer initializer = new BusinessObjectQueryInitializer(null, context);
initializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(null, aStringField("persistenceId").build(),
aContractInput().withName("persistenceId").in(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)).build()));
......@@ -54,12 +61,17 @@ public class BusinessObjectQueryInitializerTest {
@Test
public void should_throw_an_IllegalStateException_if_persistenceId_is_not_found() throws Exception {
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(anAggregationField("country",
aBO("org.test.Country").build()));
context.setMapping(mapping);
context.setData(aBusinessData().withName("myCountry").build());
context.setContractInput(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)
.havingInput(aContractInput().withName("notPersistenceId")).build());
context.setLocalVariableName("countryVar");
expectedException.expect(IllegalStateException.class);
new BusinessObjectQueryInitializer(null, null,
aContractInput()
.withName("notPersistenceId")
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()).build(),
"myData.employees", new VariableNameResolver());
new BusinessObjectQueryInitializer(null, context);
}
}
......@@ -18,12 +18,13 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.bonitasoft.studio.model.businessObject.BusinessObjectBuilder.aBO;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aCompositionField;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aSimpleField;
import static org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder.aBusinessData;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
import org.bonitasoft.engine.bdm.model.field.FieldType;
import org.bonitasoft.engine.bdm.model.field.SimpleField;
import org.bonitasoft.studio.contract.core.mapping.RelationFieldToContractInputMapping;
import org.bonitasoft.studio.contract.core.mapping.operation.BusinessObjectInstantiationException;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
import org.bonitasoft.studio.model.process.ContractInputType;
import org.junit.Rule;
import org.junit.Test;
......@@ -37,8 +38,16 @@ public class CompositionReferencePropertyInitializerTest {
@Test
public void should_initialize_object_poperty_in_a_closure() throws Exception {
final SimpleField streetField = aSimpleField().withName("street").ofType(FieldType.TEXT).notNullable().build();
final CompositionReferencePropertyInitializer propertyInitializer = new CompositionReferencePropertyInitializer(aCompositionField("address",
aBO("org.test.Address").withField(streetField).build()), aContractInput().build(), new VariableNameResolver(), "employee.address");
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(aCompositionField("address",
aBO("org.test.Address").withField(streetField).build()));
context.setMapping(mapping);
context.setData(aBusinessData().withName("employee").build());
context.setContractInput(aContractInput().build());
context.setLocalVariableName("addressVar");
final CompositionReferencePropertyInitializer propertyInitializer = new CompositionReferencePropertyInitializer(context);
propertyInitializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(null,
streetField, aContractInput().withName("street")
.in(aContractInput().withName("address").withType(ContractInputType.COMPLEX)
......@@ -56,10 +65,15 @@ public class CompositionReferencePropertyInitializerTest {
@Test
public void should_throw_an_BusinessObjectInstantiationException_when_creating_an_inconsistent_business_object() throws Exception {
final CompositionReferencePropertyInitializer propertyInitializer = new CompositionReferencePropertyInitializer(
aCompositionField("address", aBO("org.test.Address").withField(aSimpleField().withName("street").notNullable().build()).build()),
aContractInput().build(),
new VariableNameResolver(), "employee.address");
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(aCompositionField("address", aBO("org.test.Address")
.withField(aSimpleField().withName("street").notNullable().build()).build()));
context.setMapping(mapping);
context.setData(aBusinessData().withName("employee").build());
context.setContractInput(aContractInput().build());
context.setLocalVariableName("addressVar");
final CompositionReferencePropertyInitializer propertyInitializer = new CompositionReferencePropertyInitializer(context);
thrown.expect(BusinessObjectInstantiationException.class);
propertyInitializer.getInitialValue();
......
/**
* Copyright (C) 2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.contract.core.mapping.operation.initializer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.bonitasoft.studio.model.businessObject.BusinessObjectBuilder.aBO;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aCompositionField;
import static org.bonitasoft.studio.model.process.builders.DataBuilder.aData;
import org.bonitasoft.studio.contract.core.mapping.RelationFieldToContractInputMapping;
import org.junit.Test;
public class InitializerContextTest {
@Test
public void should_return_data_field_refrence() throws Exception {
final InitializerContext initializerContext = new InitializerContext();
initializerContext.setData(aData().withName("employee").build());
initializerContext
.setMapping(new RelationFieldToContractInputMapping(aCompositionField("address", aBO("Address").build())));
final String ref = initializerContext.getRef(null);
assertThat(ref).isEqualTo("employee.address");
}
@Test
public void should_return_field_refrence_from_parent() throws Exception {
final InitializerContext initializerContext = new InitializerContext();
initializerContext.setData(aData().withName("employee").build());
initializerContext
.setMapping(new RelationFieldToContractInputMapping(aCompositionField("address", aBO("Address").build())));
final InitializerContext parentContext = new InitializerContext();
parentContext.setLocalVariableName("employeeVar");
final String ref = initializerContext.getRef(parentContext);
assertThat(ref).isEqualTo("employeeVar.address");
}
}
......@@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.bonitasoft.studio.model.businessObject.BusinessObjectBuilder.aBO;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aStringField;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.anAggregationField;
import static org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder.aBusinessData;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
import org.bonitasoft.engine.bdm.model.BusinessObject;
......@@ -25,7 +26,7 @@ import org.bonitasoft.engine.bdm.model.field.Field;
import org.bonitasoft.engine.bdm.model.field.FieldType;
import org.bonitasoft.engine.bdm.model.field.RelationField;
import org.bonitasoft.engine.bdm.model.field.SimpleField;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
import org.bonitasoft.studio.contract.core.mapping.RelationFieldToContractInputMapping;
import org.bonitasoft.studio.model.process.ContractInputType;
import org.junit.Test;
......@@ -43,17 +44,23 @@ public class MultipleAggregationReferencePropertyInitializerTest {
final RelationField employeesField = anAggregationField("employees", employeeBo);
employeesField.setCollection(true);
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(null, employeeBo,
employeesField, aContractInput().withName("persistenceId")
.in(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()).build(),
"emp", new VariableNameResolver(), false);
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(employeesField);
context.setMapping(mapping);
context.setData(aBusinessData().withName("emp").build());
context.setContractInput(aContractInput().withName("persistenceId")
.in(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()).build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(null, employeeBo, context);
final String initialValue = initializer.getInitialValue();
assertThat(initialValue).isEqualTo("{" + System.lineSeparator()
+ "def employeeList = []" + System.lineSeparator()
+ "//Uncomment line below to append existing emp" + System.lineSeparator()
+ "//employeeList.addAll(emp)" + System.lineSeparator()
+ "//Uncomment line below to append existing employees" + System.lineSeparator()
+ "//employeeList.addAll(emp.employees)" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employees.each{" + System.lineSeparator()
+ "//Add aggregated Employee instance" + System.lineSeparator()
......@@ -77,11 +84,17 @@ public class MultipleAggregationReferencePropertyInitializerTest {
final RelationField employeesField = anAggregationField("employees", employeeBo);
employeesField.setCollection(true);
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(null, employeeBo,
employeesField, aContractInput().withName("persistenceId")
.in(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()).build(),
"emp", new VariableNameResolver(), true);
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(employeesField);
context.setMapping(mapping);
context.setData(aBusinessData().withName("emp").build());
context.setContractInput(aContractInput().withName("persistenceId")
.in(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()).build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
context.setOnPool(true);
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(null, employeeBo, context);
final String initialValue = initializer.getInitialValue();
assertThat(initialValue).isEqualTo("{" + System.lineSeparator()
......@@ -111,13 +124,18 @@ public class MultipleAggregationReferencePropertyInitializerTest {
employeesField.setCollection(true);
directoryBo.addField(employeesField);
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(directoryBo, employeeBo,
employeesField,
aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()
.havingInput(aContractInput().withName("persistenceId"))
.in(aContractInput().withName("direcotries").withType(ContractInputType.COMPLEX).multiple()).build(),
"emp", new VariableNameResolver(), false);
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(employeesField);
context.setMapping(mapping);
context.setData(aBusinessData().withName("emp").build());
context.setContractInput(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()
.havingInput(aContractInput().withName("persistenceId"))
.in(aContractInput().withName("direcotries").withType(ContractInputType.COMPLEX).multiple()).build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(directoryBo, employeeBo,
context);
final String initialValue = initializer.getInitialValue();
assertThat(initialValue).isEqualTo("{" + System.lineSeparator()
......
......@@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.bonitasoft.studio.model.businessObject.BusinessObjectBuilder.aBO;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aStringField;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.anAggregationField;
import static org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder.aBusinessData;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
import org.bonitasoft.engine.bdm.model.BusinessObject;
......@@ -25,7 +26,7 @@ import org.bonitasoft.engine.bdm.model.field.Field;
import org.bonitasoft.engine.bdm.model.field.FieldType;
import org.bonitasoft.engine.bdm.model.field.RelationField;
import org.bonitasoft.engine.bdm.model.field.SimpleField;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
import org.bonitasoft.studio.contract.core.mapping.RelationFieldToContractInputMapping;
import org.bonitasoft.studio.model.process.ContractInputType;
import org.junit.Rule;
import org.junit.Test;
......@@ -47,11 +48,18 @@ public class MultipleBusinessObjectQueryInitializerTest {
employeeBo.addField(nameField);
final RelationField employeesField = anAggregationField("employees", employeeBo);
employeesField.setCollection(true);
final MultipleBusinessObjectQueryInitializer initializer = new MultipleBusinessObjectQueryInitializer(employeeBo, employeesField,
aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()
.havingInput(aContractInput()
.withName("persistenceId")).build(),
"myData.employees", new VariableNameResolver(), false);
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(employeesField);
context.setMapping(mapping);
context.setData(aBusinessData().withName("myData").build());
context.setContractInput(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()
.havingInput(aContractInput()
.withName("persistenceId")).build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
final MultipleBusinessObjectQueryInitializer initializer = new MultipleBusinessObjectQueryInitializer(employeeBo, context);
initializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(employeeBo, nameField, aContractInput().withName("name")
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()).build()));
......@@ -74,10 +82,14 @@ public class MultipleBusinessObjectQueryInitializerTest {
public void should_throw_an_IllegalStateException_if_persistenceId_is_not_found() throws Exception {
expectedException.expect(IllegalStateException.class);
new MultipleBusinessObjectQueryInitializer(null, null,
aContractInput()
.withName("notPersistenceId")
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()).build(),
"myData.employees", new VariableNameResolver(), false);
final InitializerContext context = new InitializerContext();
context.setData(aBusinessData().withName("myData").build());
context.setContractInput(aContractInput()
.withName("notPersistenceId")
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()).build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
new MultipleBusinessObjectQueryInitializer(null, context);
}
}
......@@ -17,11 +17,12 @@ package org.bonitasoft.studio.contract.core.mapping.operation.initializer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.bonitasoft.studio.model.businessObject.BusinessObjectBuilder.aBO;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aCompositionField;
import static org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder.aBusinessData;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
import org.bonitasoft.engine.bdm.model.BusinessObject;
import org.bonitasoft.engine.bdm.model.field.RelationField;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
import org.bonitasoft.studio.contract.core.mapping.RelationFieldToContractInputMapping;
import org.bonitasoft.studio.model.process.ContractInputType;
import org.junit.Test;
......@@ -31,16 +32,23 @@ public class MultipleCompositionReferencePropertyInitializerTest {
public void should_build_a_closure_for_multiple_field_in_a_single_businessObject() throws Exception {
final RelationField field = aCompositionField("addresses", aBO("Address").build());
field.setCollection(true);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(null, field,
aContractInput().withName("addresses").multiple()
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX))
.build(),
"employee", new VariableNameResolver(), false);
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(field);
context.setMapping(mapping);
context.setData(aBusinessData().withName("employee").build());
context.setContractInput(aContractInput().withName("addresses").multiple()
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX))
.build());
context.setLocalVariableName("addressVar");
context.setLocalListVariableName("addressList");
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(null, context);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + System.lineSeparator()
+ "//Uncomment line below to append existing employee" + System.lineSeparator()
+ "//addressList.addAll(employee)" + System.lineSeparator()
+ "//Uncomment line below to append existing addresses" + System.lineSeparator()
+ "//addressList.addAll(employee.addresses)" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employeeInput.addresses.each{" + System.lineSeparator()
+ "//Add a new composed Address instance" + System.lineSeparator()
......@@ -56,11 +64,19 @@ public class MultipleCompositionReferencePropertyInitializerTest {
public void should_build_a_closure_for_multiple_field_in_a_single_businessObject_without_existingValueOnPool() throws Exception {
final RelationField field = aCompositionField("addresses", aBO("Address").build());
field.setCollection(true);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(null, field,
aContractInput().withName("addresses").multiple()
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX))
.build(),
"employee", new VariableNameResolver(), true);
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(field);
context.setMapping(mapping);
context.setData(aBusinessData().withName("employee").build());
context.setContractInput(aContractInput().withName("addresses").multiple()
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX))
.build());
context.setLocalVariableName("addressVar");
context.setLocalListVariableName("addressList");
context.setOnPool(true);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(null, context);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + System.lineSeparator()
......@@ -79,12 +95,20 @@ public class MultipleCompositionReferencePropertyInitializerTest {
public void should_build_a_closure_using_parent_iterator_for_multiple_field_in_a_multiple_businessObject() throws Exception {
final RelationField field = aCompositionField("addresses", aBO("Address").build());
field.setCollection(true);
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(field);
context.setMapping(mapping);
context.setData(aBusinessData().withName("employee").build());
context.setContractInput(aContractInput().withName("addresses").multiple()
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple())
.build());
context.setLocalVariableName("addressVar");
context.setLocalListVariableName("addressList");
context.setOnPool(true);
final BusinessObject employee = aBO("Employee").withField(field).build();
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(employee, field,
aContractInput().withName("addresses").multiple()
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple())
.build(),
"employee", new VariableNameResolver(), false);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(employee, context);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + System.lineSeparator()
......@@ -98,5 +122,4 @@ public class MultipleCompositionReferencePropertyInitializerTest {
+ "}" + System.lineSeparator()
+ "return addressList}()");
}