Search in sources :

Example 36 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class ValidatorTest method shouldHasNoErrorsIfFieldIsNotDuplicated.

@Test
public void shouldHasNoErrorsIfFieldIsNotDuplicated() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition);
    entity.setField("name", "not existed");
    given(criteria.uniqueResult()).willReturn(0);
    fieldDefinitionName.withValidator(new UniqueValidator());
    // when
    entity = dataDefinition.save(entity);
    // then
    verify(session).save(any(SampleSimpleDatabaseObject.class));
    assertTrue(entity.isValid());
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) UniqueValidator(com.qcadoo.model.internal.validators.UniqueValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 37 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class ValidatorTest method shouldHasErrorsIfRequiredFieldsAreNotSet.

@Test
public void shouldHasErrorsIfRequiredFieldsAreNotSet() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition);
    entity.setField("name", "");
    entity.setField("age", null);
    fieldDefinitionName.withValidator(new RequiredValidator());
    fieldDefinitionAge.withValidator(new RequiredValidator());
    // when
    entity = dataDefinition.save(entity);
    // then
    verify(session, never()).save(any(SampleSimpleDatabaseObject.class));
    assertFalse(entity.isValid());
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) RequiredValidator(com.qcadoo.model.internal.validators.RequiredValidator) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 38 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class ValidatorTest method shouldHasErrorsIfFieldIsDuplicated.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void shouldHasErrorsIfFieldIsDuplicated() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition);
    entity.setField("name", "existed");
    given(hibernateService.getTotalNumberOfEntities(any(Criteria.class))).willReturn(1);
    given(hibernateService.list(any(Criteria.class))).willReturn((List) Collections.singletonList(entity));
    fieldDefinitionName.withValidator(new UniqueValidator());
    // when
    entity = dataDefinition.save(entity);
    // then
    verify(session, never()).save(any(SampleSimpleDatabaseObject.class));
    assertFalse(entity.isValid());
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) Criteria(org.hibernate.Criteria) UniqueValidator(com.qcadoo.model.internal.validators.UniqueValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 39 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class ValidatorTest method shouldHasErrorsIfDateValueIsOutsideTheRange.

@Test
public void shouldHasErrorsIfDateValueIsOutsideTheRange() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition);
    entity.setField("birthDate", "2010-01-01");
    fieldDefinitionBirthDate.withValidator(new RangeValidator(new Date(), new Date(), true));
    // when
    entity = dataDefinition.save(entity);
    // then
    verify(session, never()).save(any(SampleSimpleDatabaseObject.class));
    assertFalse(entity.isValid());
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) RangeValidator(com.qcadoo.model.internal.validators.RangeValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Date(java.util.Date) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 40 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class ValidatorTest method shouldHasErrorsIfBigDecimalScaleAreTooShort.

@Test
public void shouldHasErrorsIfBigDecimalScaleAreTooShort() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition);
    entity.setField("money", new BigDecimal("123.45"));
    fieldDefinitionMoney.withValidator(new ScaleValidator(3, null, null));
    // when
    entity = dataDefinition.save(entity);
    // then
    verify(session, never()).save(any(SampleSimpleDatabaseObject.class));
    assertFalse(entity.isValid());
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) ScaleValidator(com.qcadoo.model.internal.validators.ScaleValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) BigDecimal(java.math.BigDecimal) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Aggregations

DefaultEntity (com.qcadoo.model.internal.DefaultEntity)92 Test (org.junit.Test)91 Entity (com.qcadoo.model.api.Entity)90 DataAccessTest (com.qcadoo.model.internal.DataAccessTest)66 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)53 EntityHookDefinitionImpl (com.qcadoo.model.internal.hooks.EntityHookDefinitionImpl)16 AbstractStateTest (com.qcadoo.view.internal.states.AbstractStateTest)12 DataDefinition (com.qcadoo.model.api.DataDefinition)10 RangeValidator (com.qcadoo.model.internal.validators.RangeValidator)9 CustomEntityService (com.qcadoo.model.beans.sample.CustomEntityService)8 LengthValidator (com.qcadoo.model.internal.validators.LengthValidator)8 UnscaledValueValidator (com.qcadoo.model.internal.validators.UnscaledValueValidator)8 JSONObject (org.json.JSONObject)8 BigDecimal (java.math.BigDecimal)7 FieldDefinition (com.qcadoo.model.api.FieldDefinition)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 ScaleValidator (com.qcadoo.model.internal.validators.ScaleValidator)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 FieldHookDefinitionImpl (com.qcadoo.model.internal.hooks.FieldHookDefinitionImpl)4 CustomEntityValidator (com.qcadoo.model.internal.validators.CustomEntityValidator)4