Commit 5bf32554 authored by Adrien's avatar Adrien Committed by Romain Bioteau

Feat(Data management) improve generated scripts and add some tests (#1604)

*  avoid to check existence on pool -> useless
*  Remove useless checkExistence constructor parameter
parent 82ccb02f
......@@ -45,6 +45,7 @@ public class CompositionReferencePropertyInitializerTest {
context.setData(aBusinessData().withName("employee").build());
context.setContractInput(aContractInput().build());
context.setLocalVariableName("addressVar");
context.setCheckExistence(true);
final CompositionReferencePropertyInitializer propertyInitializer = new CompositionReferencePropertyInitializer(
context);
......
......@@ -31,12 +31,10 @@ import java.util.Optional;
import org.bonitasoft.engine.bdm.model.BusinessObject;
import org.bonitasoft.engine.bdm.model.field.RelationField;
import org.bonitasoft.engine.bdm.model.field.RelationField.Type;
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.RelationFieldToContractInputMapping;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.AggregationReferencePropertyInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.CompositionReferencePropertyInitializer;
......@@ -126,34 +124,6 @@ public class RelationPropertyInitializerFactoryTest {
assertThat(propertyInitializer).isInstanceOf(MultipleAggregationReferencePropertyInitializer.class);
}
@Test
public void should_check_existence_for_composition_field_with_multiple_parent_on_a_task() {
RelationPropertyInitializerFactory factory = newFactory();
BusinessObject businessObject = aBO("Employee").build();
RelationField aCompositionField = aCompositionField("address", businessObject);
businessObject.addField(aCompositionField);
BusinessObjectData businessObjectData = new BusinessObjectDataBuilder()
.withClassname(businessObject.getQualifiedName())
.multiple()
.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);
FieldToContractInputMapping addressMapping = mappings.get(0);
RelationField parentRelationField = new RelationField();
parentRelationField.setCollection(true);
parentRelationField.setType(Type.COMPOSITION);
new RelationFieldToContractInputMapping(parentRelationField).addChild(addressMapping);
assertThat(factory.checkExistence(addressMapping, false)).isTrue();
assertThat(factory.checkExistence(addressMapping, true)).isFalse();
}
private RelationPropertyInitializerFactory newFactory() {
return new RelationPropertyInitializerFactory(new VariableNameResolver());
}
......
......@@ -14,9 +14,7 @@
*/
package org.bonitasoft.studio.contract.core.mapping.operation.initializer;
import org.bonitasoft.studio.common.emf.tools.ModelHelper;
import org.bonitasoft.studio.contract.core.mapping.operation.BusinessObjectInstantiationException;
import org.bonitasoft.studio.model.process.ContractInput;
public class CompositionReferencePropertyInitializer extends NewBusinessObjectInitializer {
......@@ -35,17 +33,4 @@ public class CompositionReferencePropertyInitializer extends NewBusinessObjectIn
return scriptBuilder.toString();
}
@Override
protected boolean checkExistence() {
return hasAMultipleParent()
? super.checkExistence()
: true;
}
private boolean hasAMultipleParent() {
final ContractInput parentInput = ModelHelper.getFirstContainerOfType(context.getContractInput().eContainer(),
ContractInput.class);
return parentInput != null && parentInput.isMultiple();
}
}
......@@ -26,7 +26,8 @@ public abstract class AbsractInitializerFactory implements InitializerFactory {
protected BusinessObject firstMultipleParentBusinessObject(final FieldToContractInputMapping mapping) {
FieldToContractInputMapping parentMapping = mapping.getParent();
while (parentMapping != null && !(parentMapping.getField().isCollection() && parentMapping instanceof RelationFieldToContractInputMapping)) {
while (parentMapping != null && !(parentMapping.getField().isCollection()
&& parentMapping instanceof RelationFieldToContractInputMapping)) {
parentMapping = parentMapping.getParent();
}
......@@ -35,7 +36,8 @@ public abstract class AbsractInitializerFactory implements InitializerFactory {
protected BusinessObject businessObject(final FieldToContractInputMapping mapping) {
FieldToContractInputMapping parentMapping = mapping;
while (parentMapping != null && !(parentMapping.getField().isCollection() && parentMapping instanceof RelationFieldToContractInputMapping)) {
while (parentMapping != null && !(parentMapping.getField().isCollection()
&& parentMapping instanceof RelationFieldToContractInputMapping)) {
parentMapping = parentMapping.getParent();
}
......@@ -45,12 +47,11 @@ public abstract class AbsractInitializerFactory implements InitializerFactory {
protected InitializerContext createContext(final Data data,
final VariableNameResolver resolver,
final FieldToContractInputMapping mapping,
final boolean checkExistence,
final boolean isOnPool) {
final InitializerContext context = new InitializerContext();
final BusinessObject businessObject = ((RelationField) mapping.getField()).getReference();
context.setMapping(mapping);
context.setCheckExistence(checkExistence);
context.setCheckExistence(!isOnPool);
context.setOnPool(isOnPool);
context.setData(data);
context.setContractInput(mapping.getContractInput());
......
......@@ -40,7 +40,7 @@ public class BusinessObjectInitializerFactory extends AbsractInitializerFactory
public IPropertyInitializer newPropertyInitializer(FieldToContractInputMapping mapping, BusinessObjectData data,
boolean isOnPool) {
RelationField relationField = (RelationField) mapping.getField();
InitializerContext context = createContext(data, variableNameResolver, mapping, false, isOnPool);
InitializerContext context = createContext(data, variableNameResolver, mapping, isOnPool);
return relationField.getType() == Type.AGGREGATION
? newAggregatedObjectInitializer(context)
: newComposedObjectInitializer(context);
......
......@@ -14,8 +14,6 @@
*/
package org.bonitasoft.studio.contract.core.mapping.operation.initializer.factory;
import org.bonitasoft.engine.bdm.model.field.Field;
import org.bonitasoft.engine.bdm.model.field.RelationField;
import org.bonitasoft.engine.bdm.model.field.RelationField.Type;
import org.bonitasoft.studio.contract.core.mapping.FieldToContractInputMapping;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
......@@ -38,22 +36,12 @@ public class RelationPropertyInitializerFactory extends AbsractInitializerFactor
@Override
public IPropertyInitializer newPropertyInitializer(FieldToContractInputMapping mapping,
BusinessObjectData data, boolean isOnPool) {
InitializerContext context = createContext(data, variableNameResolver, mapping, checkExistence(mapping, isOnPool),
isOnPool);
InitializerContext context = createContext(data, variableNameResolver, mapping, isOnPool);
return context.getField().getType() == Type.AGGREGATION
? newAggregatedReferenceInitializer(context)
: newComposedReferenceInitializer(context);
}
protected boolean checkExistence(FieldToContractInputMapping mapping, boolean isOnPool) {
if (!isOnPool && mapping.getParent() != null) {
Field parent = mapping.getParent().getField();
boolean isComposition = ((RelationField) mapping.getField()).getType().equals(RelationField.Type.COMPOSITION);
return parent.isCollection() && isComposition;
}
return false;
}
private IPropertyInitializer newAggregatedReferenceInitializer(InitializerContext context) {
return context.getField().isCollection()
? new MultipleAggregationReferencePropertyInitializer(
......
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