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));
}
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());
}
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());
}
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());
}
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());
}
Aggregations