Search in sources :

Example 86 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.

the class StateChangeViewClientValidationUtilTest method mockDataDefinition.

private DataDefinition mockDataDefinition(final Iterable<FieldDefinition> fieldDefinitions) {
    final DataDefinition dataDefinition = mock(DataDefinition.class);
    final Map<String, FieldDefinition> fields = Maps.newHashMap();
    for (final FieldDefinition fieldDefinition : fieldDefinitions) {
        fields.put(fieldDefinition.getName(), fieldDefinition);
    }
    given(dataDefinition.getFields()).willReturn(Collections.unmodifiableMap(fields));
    given(dataDefinition.getField(Mockito.anyString())).willAnswer(new Answer<FieldDefinition>() {

        @Override
        public FieldDefinition answer(final InvocationOnMock invocation) throws Throwable {
            return fields.get(invocation.getArguments()[0]);
        }
    });
    return dataDefinition;
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) FieldDefinition(com.qcadoo.model.api.FieldDefinition) DataDefinition(com.qcadoo.model.api.DataDefinition)

Example 87 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.

the class ProductValidatorsTest method shouldCheckEanUniquenessJustReturnTrueIfValueWasNotChanged.

public final void shouldCheckEanUniquenessJustReturnTrueIfValueWasNotChanged() {
    // given
    // for example when updating product without modifying ean
    String oldVal = "123456";
    String newVal = oldVal;
    // when
    boolean isValid = productValidators.checkEanUniqueness(dataDefinition, fieldDefinition, entity, oldVal, newVal);
    // then
    assertTrue(isValid);
    verify(entity, never()).addError(any(FieldDefinition.class), anyString());
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition) Matchers.anyString(org.mockito.Matchers.anyString)

Example 88 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.

the class ProductValidatorsTest method shouldCheckEanUniquenessJustReturnTrueIfNewEanIsEmpty.

@Test
public final void shouldCheckEanUniquenessJustReturnTrueIfNewEanIsEmpty() {
    // given
    String oldVal = "123456";
    String newVal = "";
    // when
    boolean isValid = productValidators.checkEanUniqueness(dataDefinition, fieldDefinition, entity, oldVal, newVal);
    // then
    assertTrue(isValid);
    verify(entity, never()).addError(any(FieldDefinition.class), anyString());
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 89 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class DefaultEntityTest method init.

@Before
public final void init() {
    belongsToFieldDefinition = mock(FieldDefinition.class);
    final BelongsToType belongsToType = mock(BelongsToType.class);
    when(belongsToFieldDefinition.getType()).thenReturn(belongsToType);
    belongsToFieldDataDefinition = mock(DataDefinition.class);
    when(belongsToFieldDefinition.getDataDefinition()).thenReturn(belongsToFieldDataDefinition);
    decimalFieldDefinition = mock(FieldDefinition.class);
    final DecimalType decimalType = new DecimalType();
    when(decimalFieldDefinition.getType()).thenReturn(decimalType);
    integerFieldDefinition = mock(FieldDefinition.class);
    final IntegerType integerType = new IntegerType();
    when(integerFieldDefinition.getType()).thenReturn(integerType);
    manyToManyFieldDefinition = mock(FieldDefinition.class);
    final ManyToManyType manyToManyType = mock(ManyToManyType.class);
    when(manyToManyFieldDefinition.getType()).thenReturn(manyToManyType);
    manyToManyDataDefinition = mock(DataDefinition.class);
    when(manyToManyFieldDefinition.getDataDefinition()).thenReturn(manyToManyDataDefinition);
    secondManyToManyFieldDefinition = mock(FieldDefinition.class);
    when(secondManyToManyFieldDefinition.getType()).thenReturn(manyToManyType);
    secondManyToManyDataDefinition = mock(DataDefinition.class);
    when(secondManyToManyFieldDefinition.getDataDefinition()).thenReturn(secondManyToManyDataDefinition);
    hasManyFieldDefinition = mock(FieldDefinition.class);
    final HasManyType hasManyType = mock(HasManyType.class);
    when(hasManyType.getJoinFieldName()).thenReturn(BELONGS_TO_FIELD_NAME);
    when(hasManyFieldDefinition.getType()).thenReturn(hasManyType);
    hasManyDataDefinition = mock(DataDefinition.class);
    when(hasManyFieldDefinition.getDataDefinition()).thenReturn(hasManyDataDefinition);
    stringFieldDefinition = mock(FieldDefinition.class);
    when(stringFieldDefinition.isPersistent()).thenReturn(false);
    dataDefinition = mock(DataDefinition.class);
    booleanFieldDefinition = mock(FieldDefinition.class);
    defaultEntity = new DefaultEntity(dataDefinition);
    final Map<String, FieldDefinition> fieldsMap = Maps.newHashMap();
    fieldsMap.put(BELONGS_TO_FIELD_NAME, belongsToFieldDefinition);
    fieldsMap.put(STRING_FIELD_NAME, stringFieldDefinition);
    fieldsMap.put(BOOLEAN_FIELD_NAME, booleanFieldDefinition);
    fieldsMap.put(DECIMAL_FIELD_NAME, decimalFieldDefinition);
    fieldsMap.put(INTEGER_FIELD_NAME, integerFieldDefinition);
    fieldsMap.put(MANY_TO_MANY_FIELD_NAME, manyToManyFieldDefinition);
    fieldsMap.put(SECOND_MANY_TO_MANY_FIELD_NAME, secondManyToManyFieldDefinition);
    fieldsMap.put(HAS_MANY_FIELD_NAME, hasManyFieldDefinition);
    for (Map.Entry<String, FieldDefinition> fieldEntry : fieldsMap.entrySet()) {
        when(dataDefinition.getField(fieldEntry.getKey())).thenReturn(fieldEntry.getValue());
    }
    when(dataDefinition.getFields()).thenReturn(fieldsMap);
}
Also used : IntegerType(com.qcadoo.model.internal.types.IntegerType) HasManyType(com.qcadoo.model.api.types.HasManyType) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) ManyToManyType(com.qcadoo.model.api.types.ManyToManyType) DecimalType(com.qcadoo.model.internal.types.DecimalType) DataDefinition(com.qcadoo.model.api.DataDefinition) Map(java.util.Map) Before(org.junit.Before)

Example 90 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class DataAccessServiceImpl method deleteEntity.

private EntityOpResult deleteEntity(final InternalDataDefinition dataDefinition, final Long entityId, final boolean testOnly, final Set<EntitySignature> traversedEntities) {
    Object databaseEntity = getDatabaseEntity(dataDefinition, entityId);
    if (databaseEntity == null) {
        logEntityDebug(dataDefinition, entityId, "has been deleted earlier, for example onDelete hook");
        return new EntityOpResult(true, new EntityMessagesHolderImpl());
    }
    Entity entity = get(dataDefinition, entityId);
    if (!dataDefinition.callDeleteHook(entity)) {
        logDeletionErrors(entity);
        entity.addGlobalError("qcadooView.message.deleteFailedMessage");
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return EntityOpResult.failure(entity);
    }
    priorityService.deprioritizeEntity(dataDefinition, databaseEntity);
    Map<String, FieldDefinition> fields = dataDefinition.getFields();
    for (FieldDefinition fieldDefinition : fields.values()) {
        if (fieldDefinition.getType() instanceof CollectionFieldType) {
            CollectionFieldType collectionFieldType = (CollectionFieldType) fieldDefinition.getType();
            @SuppressWarnings("unchecked") Collection<Entity> children = (Collection<Entity>) entity.getField(fieldDefinition.getName());
            EntityOpResult cascadeDeletionRes = performCascadeStrategy(entity, collectionFieldType, children, traversedEntities);
            if (!cascadeDeletionRes.isSuccessfull()) {
                return cascadeDeletionRes;
            }
        }
    }
    if (testOnly) {
        logEntityInfo(dataDefinition, entityId, "may be cascade deleted");
    } else {
        try {
            databaseEntity = getDatabaseEntity(dataDefinition, entityId);
            if (databaseEntity != null) {
                hibernateService.getCurrentSession().delete(databaseEntity);
                hibernateService.getCurrentSession().flush();
            }
        } catch (ConstraintViolationException e) {
            throw new IllegalStateException(getConstraintViolationMessage(entity), e);
        }
        logEntityDebug(dataDefinition, entityId, "has been deleted");
    }
    return new EntityOpResult(true, entity);
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) CollectionFieldType(com.qcadoo.model.api.types.CollectionFieldType) EntityOpResult(com.qcadoo.model.api.EntityOpResult) Collection(java.util.Collection) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException)

Aggregations

FieldDefinition (com.qcadoo.model.api.FieldDefinition)142 Test (org.junit.Test)92 DataDefinition (com.qcadoo.model.api.DataDefinition)49 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)48 Entity (com.qcadoo.model.api.Entity)32 BelongsToType (com.qcadoo.model.api.types.BelongsToType)19 InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)15 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)13 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)12 TextInputComponentPattern (com.qcadoo.view.internal.components.TextInputComponentPattern)11 HasManyType (com.qcadoo.model.api.types.HasManyType)9 StringType (com.qcadoo.model.internal.types.StringType)9 JSONObject (org.json.JSONObject)8 Map (java.util.Map)7 Matchers.anyString (org.mockito.Matchers.anyString)7 SearchCriterion (com.qcadoo.model.api.search.SearchCriterion)6 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)6 WindowComponentPattern (com.qcadoo.view.internal.components.window.WindowComponentPattern)6 Before (org.junit.Before)6 EntityList (com.qcadoo.model.api.EntityList)5