Commit 5216eb0a authored by Adrien's avatar Adrien Committed by Romain Bioteau

feat(data management) add persistence_id contract input by default (#1589)

* multiple business data have a persistenceId_string contract input (on
task) in order to provide an edition behavior by default. Associated
operation script has been updated. Operator is now "takes value of"
* Idem for multiple reference in aggregation/composition
* For aggregation, the contract input is now persistenceId_string
instead of persistenceId, in order to avoid potential issues related to
js behavior (Long max range isn't the same ...)

closes [BST-101](https://bonitasoft.atlassian.net/browse/BST-101)
parent 436097e0
......@@ -29,7 +29,6 @@ import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.bonitasoft.engine.bdm.BusinessObjectModelConverter;
......@@ -61,9 +60,6 @@ import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.swt.graphics.Image;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.wiring.BundleWiring;
import org.xml.sax.SAXException;
import com.google.common.base.Predicate;
......@@ -91,12 +87,10 @@ public class BusinessObjectModelRepositoryStore<F extends AbstractBDMFileStore>
extensions.add(BDM_TYPE_EXTENSION);
}
public BusinessObjectModelConverter getConverter() {
return converter;
}
@Override
public AbstractBDMFileStore createRepositoryFileStore(final String fileName) {
return new BusinessObjectModelFileStore(fileName, this);
......@@ -107,13 +101,11 @@ public class BusinessObjectModelRepositoryStore<F extends AbstractBDMFileStore>
return STORE_NAME;
}
@Override
public String getDisplayName() {
return Messages.businessObjectRepositoryStoreName;
}
@Override
public Image getIcon() {
return Pics.getImage("bdm.png", BusinessObjectPlugin.getDefault());
......@@ -131,7 +123,7 @@ public class BusinessObjectModelRepositoryStore<F extends AbstractBDMFileStore>
.map(fileStore -> fileStore.getBusinessObject(qualifiedName) != null ? fileStore : null);
}
public Optional<BusinessObject> getBusinessObjectByQualifiedName(final String qualifiedName) {
public Optional<BusinessObject> getBusinessObjectByQualifiedName(String qualifiedName) {
final Optional<BusinessObjectModelFileStore> businessObjectFileStore = Optional
.ofNullable((BusinessObjectModelFileStore) getChild(BusinessObjectModelFileStore.BOM_FILENAME));
return businessObjectFileStore.map(fileStore -> fileStore.getBusinessObject(qualifiedName));
......
......@@ -198,9 +198,8 @@ public class FieldToContractInputMappingOperationBuilderTest {
mapping, new NullProgressMonitor());
OperatorAssert.assertThat(operation.getOperator())
.hasType(ExpressionConstants.JAVA_METHOD_OPERATOR)
.hasInputTypes(Collection.class.getName())
.hasExpression("addAll");
.hasType(ExpressionConstants.ASSIGNMENT_OPERATOR)
.hasInputTypes(Collection.class.getName());
}
private FieldToContractInputMappingOperationBuilder createFixture() {
......
......@@ -27,17 +27,26 @@ public class AggregationReferencePropertyInitializerTest {
@Test
public void should_call_query_in_a_closure() throws Exception {
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(anAggregationField("country",
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(
anAggregationField("country",
aBO("country").build()));
context.setMapping(mapping);
context.setContractInput(aContractInput().withName("persistenceId").in(aContractInput().withName("country")).build());
context.setContractInput(
aContractInput().withName("persistenceId_string").in(aContractInput().withName("country")).build());
context.setLocalVariableName("countryVar");
final String initialValue = new AggregationReferencePropertyInitializer(null, context).getInitialValue();
assertThat(initialValue).isEqualTo("{" + System.lineSeparator()
+ "//Retrieve aggregated country using its DAO and persistenceId" + System.lineSeparator()
+ "def countryVar = countryDAO.findByPersistenceId(country.persistenceId.toLong())" + System.lineSeparator()
+ "def countryVar = countryDAO.findByPersistenceId(country.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!countryVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `country` with the persistence id \" + country.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "return countryVar}()");
}
}
......@@ -35,34 +35,46 @@ public class BusinessObjectQueryInitializerTest {
@Test
public void should_generate_a_script_using_query_to_retrieve_a_business_object_in_initial_value() throws Exception {
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(anAggregationField("country",
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());
.havingInput(aContractInput().withName("persistenceId_string")).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()));
aContractInput().withName("persistenceId_string")
.in(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)).build()));
initializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(null, aStringField("name").build(),
aContractInput().withName("name").in(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)).build()));
aContractInput().withName("name")
.in(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)).build()));
final String initialValue = initializer.getInitialValue();
assertThat(initialValue).isEqualTo(
"//Retrieve aggregated Country using its DAO and persistenceId" + System.lineSeparator()
+ "def countryVar = countryDAO.findByPersistenceId(countryInput.persistenceId.toLong())" + System.lineSeparator()
+ "countryVar.name = countryInput.name" + System.lineSeparator()
+ "def countryVar = countryDAO.findByPersistenceId(countryInput.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!countryVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `Country` with the persistence id \" + countryInput.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "countryVar.name = countryInput.name"
+ System.lineSeparator()
+ "return countryVar");
}
@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",
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(
anAggregationField("country",
aBO("org.test.Country").build()));
context.setMapping(mapping);
context.setData(aBusinessData().withName("myCountry").build());
......
......@@ -32,7 +32,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class MultipleBusinessObjectQueryInitializerTest {
public class MultipleAggregationBusinessObjectQueryInitializerTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
......@@ -55,22 +55,34 @@ public class MultipleBusinessObjectQueryInitializerTest {
context.setData(aBusinessData().withName("myData").build());
context.setContractInput(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()
.havingInput(aContractInput()
.withName("persistenceId")).build());
.withName("persistenceId_string"))
.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()));
final MultipleAggregationBusinessObjectQueryInitializer initializer = new MultipleAggregationBusinessObjectQueryInitializer(
employeeBo,
context);
initializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(employeeBo, nameField,
aContractInput().withName("name")
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple())
.build()));
final String initialValue = initializer.getInitialValue();
assertThat(initialValue).isEqualTo("def employeeList = []" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employeeInput.each{" + System.lineSeparator()
+ "//Add aggregated Employee instance" + System.lineSeparator()
+ "//Add Employee instance" + System.lineSeparator()
+ "employeeList.add({ currentEmployeeInput ->" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId.toLong())" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!employeeVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `Employee` with the persistence id \" + currentEmployeeInput.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "employeeVar.name = currentEmployeeInput.name" + System.lineSeparator()
+ "return employeeVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
......@@ -90,6 +102,6 @@ public class MultipleBusinessObjectQueryInitializerTest {
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
new MultipleBusinessObjectQueryInitializer(null, context);
new MultipleAggregationBusinessObjectQueryInitializer(null, context);
}
}
......@@ -48,24 +48,30 @@ public class MultipleAggregationReferencePropertyInitializerTest {
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(employeesField);
context.setMapping(mapping);
context.setData(aBusinessData().withName("emp").build());
context.setContractInput(aContractInput().withName("persistenceId")
context.setContractInput(aContractInput().withName("persistenceId_string")
.in(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()).build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(null, employeeBo, context);
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 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()
+ "//Add Employee instance" + System.lineSeparator()
+ "employeeList.add({ currentEmployeeInput ->" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId.toLong())" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!employeeVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `Employee` with the persistence id \" + currentEmployeeInput.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "return employeeVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
+ "}" + System.lineSeparator()
......@@ -88,22 +94,30 @@ public class MultipleAggregationReferencePropertyInitializerTest {
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(employeesField);
context.setMapping(mapping);
context.setData(aBusinessData().withName("emp").build());
context.setContractInput(aContractInput().withName("persistenceId")
context.setContractInput(aContractInput().withName("persistenceId_string")
.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 MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(
null, employeeBo, context);
final String initialValue = initializer.getInitialValue();
assertThat(initialValue).isEqualTo("{" + System.lineSeparator()
+ "def employeeList = []" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employees.each{" + System.lineSeparator()
+ "//Add aggregated Employee instance" + System.lineSeparator()
+ "//Add Employee instance" + System.lineSeparator()
+ "employeeList.add({ currentEmployeeInput ->" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId.toLong())" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!employeeVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `Employee` with the persistence id \" + currentEmployeeInput.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "return employeeVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
+ "}" + System.lineSeparator()
......@@ -129,12 +143,13 @@ public class MultipleAggregationReferencePropertyInitializerTest {
context.setMapping(mapping);
context.setData(aBusinessData().withName("emp").build());
context.setContractInput(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()
.havingInput(aContractInput().withName("persistenceId"))
.havingInput(aContractInput().withName("persistenceId_string"))
.in(aContractInput().withName("direcotries").withType(ContractInputType.COMPLEX).multiple()).build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(directoryBo, employeeBo,
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(
directoryBo, employeeBo,
context);
final String initialValue = initializer.getInitialValue();
......@@ -142,9 +157,16 @@ public class MultipleAggregationReferencePropertyInitializerTest {
+ "def employeeList = []" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "currentDirectoryInput.employees.each{" + System.lineSeparator()
+ "//Add aggregated Employee instance" + System.lineSeparator()
+ "//Add Employee instance" + System.lineSeparator()
+ "employeeList.add({ currentEmployeeInput ->" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId.toLong())" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!employeeVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `Employee` with the persistence id \" + currentEmployeeInput.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "return employeeVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
+ "}" + System.lineSeparator()
......
......@@ -43,12 +43,11 @@ public class MultipleCompositionReferencePropertyInitializerTest {
context.setLocalVariableName("addressVar");
context.setLocalListVariableName("addressList");
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(null, context);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(
null, context);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + 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()
......@@ -61,7 +60,8 @@ public class MultipleCompositionReferencePropertyInitializerTest {
}
@Test
public void should_build_a_closure_for_multiple_field_in_a_single_businessObject_without_existingValueOnPool() throws Exception {
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);
......@@ -76,7 +76,8 @@ public class MultipleCompositionReferencePropertyInitializerTest {
context.setLocalListVariableName("addressList");
context.setOnPool(true);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(null, context);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(
null, context);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + System.lineSeparator()
......@@ -92,7 +93,8 @@ public class MultipleCompositionReferencePropertyInitializerTest {
}
@Test
public void should_build_a_closure_using_parent_iterator_for_multiple_field_in_a_multiple_businessObject() throws Exception {
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);
......@@ -108,7 +110,8 @@ public class MultipleCompositionReferencePropertyInitializerTest {
context.setOnPool(true);
final BusinessObject employee = aBO("Employee").withField(field).build();
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(employee, context);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(
employee, context);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + 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.aCompositionField;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aSimpleField;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aStringField;
import static org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder.aBusinessData;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
......@@ -26,41 +27,48 @@ 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.RelationFieldToContractInputMapping;
import org.bonitasoft.studio.contract.core.mapping.SimpleFieldToContractInputMapping;
import org.bonitasoft.studio.model.process.ContractInputType;
import org.junit.Test;
public class NewBusinessObjectListInitializerTest {
@SuppressWarnings("unchecked")
@Test
public void should_initialize_new_object_property_for_multiple_composed_reference() throws Exception {
final SimpleField streetField = aSimpleField().withName("street").ofType(FieldType.STRING).notNullable().build();
final BusinessObject businessObject = aBO("org.test.Address").withField(streetField).build();
final RelationField addressField = aCompositionField("address",
businessObject);
SimpleField streetField = aSimpleField().withName("street").ofType(FieldType.STRING).notNullable().build();
BusinessObject businessObject = aBO("org.test.Address").withField(streetField).build();
RelationField addressField = aCompositionField("address", businessObject);
addressField.setCollection(true);
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(addressField);
InitializerContext context = new InitializerContext();
RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(addressField);
mapping.addChild(new SimpleFieldToContractInputMapping(aStringField("persistenceId_string").build()));
context.setMapping(mapping);
context.setData(aBusinessData().withName("employee").build());
context.setContractInput(aContractInput().withName("address")
.multiple().in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX)).build());
context.setContractInput(aContractInput()
.withName("address")
.multiple()
.in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX))
.havingInput(aContractInput().withName("persistenceId_string").withType(ContractInputType.TEXT))
.build());
context.setLocalVariableName("addressVar");
context.setLocalListVariableName("addressList");
final AbstractBusinessObjectInitializer propertyInitializer = new NewBusinessObjectListInitializer(context);
final AbstractBusinessObjectInitializer propertyInitializer = new MultipleCompositionBusinessObjectQueryInitializer(
businessObject, context);
propertyInitializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(businessObject,
streetField, aContractInput().withName("street")
.in(aContractInput().withName("address").withType(ContractInputType.COMPLEX).multiple()
.in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX))).build()));
.in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX)))
.build()));
assertThat(propertyInitializer.getInitialValue()).isEqualTo("def addressList = []" + System.lineSeparator()
+ "//Uncomment line below to append existing address" + System.lineSeparator()
+ "//addressList.addAll(employee.address)" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employee.address.each{" + System.lineSeparator()
+ "//Add a new composed Address instance" + System.lineSeparator()
+ "//Add Address instance" + System.lineSeparator()
+ "addressList.add({ currentAddressInput ->" + System.lineSeparator()
+ "def addressVar = new org.test.Address()" + System.lineSeparator()
+ "def addressVar = employee.address.find { it.persistenceId.toString() == employee.address.persistenceId_string} ?: new org.test.Address()"
+ System.lineSeparator()
+ "addressVar.street = currentAddressInput.street" + System.lineSeparator()
+ "return addressVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
......@@ -69,7 +77,8 @@ public class NewBusinessObjectListInitializerTest {
}
@Test
public void should_initialize_new_object_property_for_multiple_composed_reference_withoutExistingOnPool() throws Exception {
public void should_initialize_new_object_property_for_multiple_composed_reference_withoutExistingOnPool()
throws Exception {
final SimpleField streetField = aSimpleField().withName("street").ofType(FieldType.STRING).notNullable().build();
final BusinessObject businessObject = aBO("org.test.Address").withField(streetField).build();
final RelationField addressField = aCompositionField("address",
......@@ -90,7 +99,8 @@ public class NewBusinessObjectListInitializerTest {
propertyInitializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(businessObject,
streetField, aContractInput().withName("street")
.in(aContractInput().withName("address").withType(ContractInputType.COMPLEX).multiple()
.in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX))).build()));
.in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX)))
.build()));
assertThat(propertyInitializer.getInitialValue()).isEqualTo("def addressList = []" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employee.address.each{" + System.lineSeparator()
......@@ -125,7 +135,8 @@ public class NewBusinessObjectListInitializerTest {
propertyInitializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(businessObject,
streetField, aContractInput().withName("street")
.in(aContractInput().withName("address").withType(ContractInputType.COMPLEX).multiple()
.in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX))).build()));
.in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX)))
.build()));
assertThat(propertyInitializer.getInitialValue()).isEqualTo("def addressList = []" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "addresses.each{" + System.lineSeparator()
......@@ -138,4 +149,5 @@ public class NewBusinessObjectListInitializerTest {
+ "}" + System.lineSeparator()
+ "return addressList");
}
}
......@@ -22,20 +22,28 @@ import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aCompositi
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.PoolBuilder.aPool;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Optional;
import org.bonitasoft.engine.bdm.model.BusinessObject;
import org.bonitasoft.engine.bdm.model.field.RelationField;
import org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelFileStore;
import org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelRepositoryStore;
import org.bonitasoft.studio.contract.core.mapping.FieldToContractInputMapping;
import org.bonitasoft.studio.contract.core.mapping.FieldToContractInputMappingFactory;
import org.bonitasoft.studio.contract.core.mapping.operation.FieldToContractInputMappingBuilder;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.BusinessObjectQueryInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.IPropertyInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.MultipleBusinessObjectQueryInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.MultipleAggregationBusinessObjectQueryInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.NewBusinessObjectInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.NewBusinessObjectListInitializer;
import org.bonitasoft.studio.model.process.BusinessObjectData;
import org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder;
import org.junit.Test;
public class BusinessObjectInitializerFactoryTest {
......@@ -47,7 +55,8 @@ public class BusinessObjectInitializerFactoryTest {
final RelationField aCompositionField = aCompositionField("employee", aBO("Employee").build());
final FieldToContractInputMapping mapping = aRelationMapping(aCompositionField)
.addChild(aSimpleMapping(aStringField("name").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping, aBusinessData().withName("employee").build(), false);
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping,
aBusinessData().withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(NewBusinessObjectInitializer.class);
}
......@@ -60,34 +69,53 @@ public class BusinessObjectInitializerFactoryTest {
aCompositionField.setCollection(true);
final FieldToContractInputMapping mapping = FieldToContractInputMappingBuilder
.aRelationMapping(aCompositionField)
.addChild(aSimpleMapping(aStringField("name").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping, aBusinessData().withName("employee").build(), false);
.addChild(aSimpleMapping(aStringField("name").build()))
.addChild(aSimpleMapping(aStringField("persistenceId_string").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping,
aBusinessData().withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(NewBusinessObjectListInitializer.class);
}
@Test
public void should_create_a_BusinessObjectQueryInitializer() throws Exception {
final BusinessObjectInitializerFactory factory = newFactory();
final BusinessObject businessObject = aBO("Employee").withField(anAggregationField("country", aBO("Country").build())).build();
final List<FieldToContractInputMapping> mappings = new FieldToContractInputMappingFactory().createMappingForBusinessObjectType(businessObject);
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mappings.get(0), aBusinessData().withName("employee").build(), false);
BusinessObjectInitializerFactory factory = newFactory();
BusinessObject businessObject = aBO("Employee").withField(anAggregationField("country", aBO("Country").build()))
.build();
BusinessObjectData businessObjectData = new BusinessObjectDataBuilder()
.withClassname(businessObject.getQualifiedName()).build();
BusinessObjectModelRepositoryStore<BusinessObjectModelFileStore> repositoryStore = mock(
BusinessObjectModelRepositoryStore.class);
when(repositoryStore.getBusinessObjectByQualifiedName(businessObjectData.getClassName()))
.thenReturn(Optional.of(businessObject));
List<FieldToContractInputMapping> mappings = new FieldToContractInputMappingFactory(repositoryStore)
.createMappingForBusinessObjectType(aPool().build(), businessObjectData);
IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mappings.get(0),
aBusinessData().withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(BusinessObjectQueryInitializer.class);
}
@Test
public void should_create_a_MultipleBusinessObjectQueryInitializer() throws Exception {
final BusinessObjectInitializerFactory factory = newFactory();
final RelationField anAggregationField = anAggregationField("countries", aBO("Country").build());
BusinessObjectInitializerFactory factory = newFactory();
RelationField anAggregationField = anAggregationField("countries", aBO("Country").build());
anAggregationField.setCollection(true);
final BusinessObject businessObject = aBO("Employee").withField(anAggregationField).build();
final List<FieldToContractInputMapping> mappings = new FieldToContractInputMappingFactory().createMappingForBusinessObjectType(businessObject);
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mappings.get(0), aBusinessData()
BusinessObject businessObject = aBO("Employee").withField(anAggregationField).build();
BusinessObjectData businessObjectData = new BusinessObjectDataBuilder()