Search in sources :

Example 6 with UniqueValidator

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

the class DataAccessServiceCopyTest method shouldCopyEntityWithUniqueFieldWhenNull.

@Test
public void shouldCopyEntityWithUniqueFieldWhenNull() throws Exception {
    // given
    FieldHookDefinition fieldHook = new UniqueValidator();
    // fieldHook.initialize(dataDefinition, fieldDefinitionName);
    fieldDefinitionName.withValidator(fieldHook);
    SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
    simpleDatabaseObject.setId(13L);
    simpleDatabaseObject.setName(null);
    simpleDatabaseObject.setAge(66);
    given(hibernateService.getTotalNumberOfEntities(Mockito.any(Criteria.class))).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(null, 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) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 7 with UniqueValidator

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

the class DataAccessServiceCopyTest method shouldCopyEntityWithUniqueField2.

@Test
public void shouldCopyEntityWithUniqueField2() 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(1)");
    simpleDatabaseObject.setAge(66);
    given(hibernateService.getTotalNumberOfEntities(Mockito.any(Criteria.class))).willReturn(1, 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(3)", 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) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) 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