Search in sources :

Example 1 with UniqueValidator

use of com.qcadoo.model.internal.validators.UniqueValidator in project qcadoo by qcadoo.

the class DataAccessServiceCopyTest method shouldCopyEntityWithUniqueField.

@Test
public void shouldCopyEntityWithUniqueField() throws Exception {
    // given
    FieldHookDefinition fieldHook = new UniqueValidator();
    // fieldHook.initialize(dataDefinition, fieldDefinitionName);
    fieldDefinitionName.withValidator(fieldHook);
    SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
    simpleDatabaseObject.setId(13L);
    simpleDatabaseObject.setName("Mr T");
    simpleDatabaseObject.setAge(66);
    given(criteria.setProjection(Projections.rowCount()).uniqueResult()).willReturn(0);
    given(session.get(any(Class.class), Matchers.anyInt())).willReturn(simpleDatabaseObject);
    // when
    List<Entity> entities = dataDefinition.copy(new Long[] { 13L });
    // then
    verify(session, times(1)).save(Mockito.any(SampleSimpleDatabaseObject.class));
    assertEquals(1, entities.size());
    assertTrue(entities.get(0).isValid());
    Assert.assertEquals(66, entities.get(0).getField("age"));
    Assert.assertEquals("Mr T(1)", entities.get(0).getField(NAME));
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldHookDefinition(com.qcadoo.model.internal.api.FieldHookDefinition) UniqueValidator(com.qcadoo.model.internal.validators.UniqueValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 2 with UniqueValidator

use of com.qcadoo.model.internal.validators.UniqueValidator in project qcadoo by qcadoo.

the class ValidatorTest method shouldHasNoErrorsIfUpdatedFieldIsNotDuplicated.

@Test
public void shouldHasNoErrorsIfUpdatedFieldIsNotDuplicated() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition, 1L);
    entity.setField("name", "not existed");
    SampleSimpleDatabaseObject databaseObject = new SampleSimpleDatabaseObject(1L);
    given(session.get(SampleSimpleDatabaseObject.class, 1L)).willReturn(databaseObject);
    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 3 with UniqueValidator

use of com.qcadoo.model.internal.validators.UniqueValidator 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 4 with UniqueValidator

use of com.qcadoo.model.internal.validators.UniqueValidator 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 5 with UniqueValidator

use of com.qcadoo.model.internal.validators.UniqueValidator in project qcadoo by qcadoo.

the class ValidatorTest method shouldBeUniqueIfHasUniqueValidator.

@Test
public void shouldBeUniqueIfHasUniqueValidator() throws Exception {
    // given
    fieldDefinitionName.withValidator(new UniqueValidator());
    // then
    assertTrue(fieldDefinitionName.isUnique());
}
Also used : UniqueValidator(com.qcadoo.model.internal.validators.UniqueValidator) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Aggregations

UniqueValidator (com.qcadoo.model.internal.validators.UniqueValidator)7 Test (org.junit.Test)7 Entity (com.qcadoo.model.api.Entity)6 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)6 DataAccessTest (com.qcadoo.model.internal.DataAccessTest)4 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)3 FieldHookDefinition (com.qcadoo.model.internal.api.FieldHookDefinition)3 Criteria (org.hibernate.Criteria)3