use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.
the class EntityServiceImplTest method shouldSetNullIfBelongsToFieldIsEmpty.
@Test
public void shouldSetNullIfBelongsToFieldIsEmpty() throws Exception {
// given
SampleParentDatabaseObject parentDatabaseEntity = new SampleParentDatabaseObject(1L);
parentDatabaseEntity.setName("Mr X");
SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(2L);
databaseEntity.setBelongsTo(parentDatabaseEntity);
// when
entityService.setField(databaseEntity, fieldDefinitionBelongsTo, null);
// then
assertNull(databaseEntity.getBelongsTo());
}
use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.
the class EntityServiceImplTest method shouldConvertGenericEntityIntoDatabaseOneUsingExistingEntity.
@Test
public void shouldConvertGenericEntityIntoDatabaseOneUsingExistingEntity() throws Exception {
// given
Entity genericEntity = new DefaultEntity(dataDefinition, 2L);
genericEntity.setField("name", "Mr T");
genericEntity.setField("age", 12);
genericEntity.setField("belongsTo", 1L);
SampleSimpleDatabaseObject existingDatabaseEntity = new SampleSimpleDatabaseObject(11L);
SampleParentDatabaseObject parentDatabaseEntity = new SampleParentDatabaseObject(1L);
parentDatabaseEntity.setName("Mr X");
given(session.get(SampleParentDatabaseObject.class, 1L)).willReturn(parentDatabaseEntity);
given(session.load(SampleParentDatabaseObject.class, 1L)).willReturn(parentDatabaseEntity);
validationService.validateGenericEntity(dataDefinition, genericEntity, new DefaultEntity(dataDefinition, 2L));
// when
Object databaseEntity = entityService.convertToDatabaseEntity(dataDefinition, genericEntity, existingDatabaseEntity);
// then
assertNotNull(databaseEntity);
isInstanceOf(SampleSimpleDatabaseObject.class, databaseEntity);
assertEquals(Long.valueOf(11), ((SampleSimpleDatabaseObject) databaseEntity).getId());
assertEquals(Integer.valueOf(12), ((SampleSimpleDatabaseObject) databaseEntity).getAge());
assertEquals("Mr T", ((SampleSimpleDatabaseObject) databaseEntity).getName());
assertNotNull(((SampleSimpleDatabaseObject) databaseEntity).getBelongsTo());
assertEquals("Mr X", ((SampleSimpleDatabaseObject) databaseEntity).getBelongsTo().getName());
}
use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.
the class EntityServiceImplTest method shouldConvertDatabaseEntityIntoGenericOne.
@Test
public void shouldConvertDatabaseEntityIntoGenericOne() throws Exception {
// given
SampleParentDatabaseObject parentDatabaseEntity = new SampleParentDatabaseObject(1L);
parentDatabaseEntity.setName("Mr X");
SampleParentDatabaseObject lazyParentDatabaseEntity = mock(SampleParentDatabaseObject.class, RETURNS_DEEP_STUBS);
given(lazyParentDatabaseEntity.getHibernateLazyInitializer().getIdentifier()).willReturn(77L);
SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(2L);
databaseEntity.setAge(12);
databaseEntity.setName("Mr T");
databaseEntity.setBelongsTo(parentDatabaseEntity);
databaseEntity.setLazyBelongsTo(lazyParentDatabaseEntity);
// when
Entity genericEntity = entityService.convertToGenericEntity(dataDefinition, databaseEntity);
// then
assertNotNull(genericEntity);
assertEquals(Long.valueOf(2), genericEntity.getId());
assertEquals(12, genericEntity.getField("age"));
assertEquals("Mr T", genericEntity.getField("name"));
isInstanceOf(DefaultEntity.class, genericEntity.getField("belongsTo"));
assertEquals("Mr X", ((Entity) genericEntity.getField("belongsTo")).getField("name"));
isInstanceOf(ProxyEntity.class, genericEntity.getField("lazyBelongsTo"));
assertEquals(Long.valueOf(77), ((Entity) genericEntity.getField("lazyBelongsTo")).getId());
}
use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceGetTest method shouldGetTreeField.
@Test
public void shouldGetTreeField() throws Exception {
// given
SampleParentDatabaseObject parentDatabaseObject = new SampleParentDatabaseObject();
parentDatabaseObject.setId(1L);
parentDatabaseObject.setName("Mr T");
given(session.get(any(Class.class), Matchers.anyInt())).willReturn(parentDatabaseObject);
// when
Entity entity = parentDataDefinition.get(1L);
// then
assertEquals(1L, entity.getId().longValue());
assertEquals("Mr T", entity.getField("name"));
assertThat(entity.getField("tree"), CoreMatchers.instanceOf(EntityTreeImpl.class));
EntityTree tree = entity.getTreeField("tree");
assertEquals(1L, getField(tree, "belongsToId"));
assertEquals("owner", ((FieldDefinition) getField(tree, "joinFieldDefinition")).getName());
assertEquals("tree.entity", ((DataDefinition) getField(tree, "dataDefinition")).getName());
}
use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceSaveTest method shouldNotSaveEntityListField.
@Test
public void shouldNotSaveEntityListField() throws Exception {
// given
EntityList entities = new EntityListImpl(dataDefinition, "", 1L);
Entity parent = new DefaultEntity(parentDataDefinition, 1L);
parent.setField("entities", entities);
SampleParentDatabaseObject existingParent = new SampleParentDatabaseObject(1L);
given(session.get(any(Class.class), Matchers.anyInt())).willReturn(existingParent);
given(session.load(any(Class.class), Matchers.eq(1L))).willReturn(existingParent);
given(criteria.uniqueResult()).willReturn(1L);
// when
Entity entity = parentDataDefinition.save(parent);
// then
assertTrue(entity.isValid());
assertEquals(entities, entity.getField("entities"));
verify(session, times(1)).save(Mockito.any());
}
Aggregations