use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceSaveTest method shouldSaveTreeField.
@SuppressWarnings("unchecked")
@Test
public void shouldSaveTreeField() throws Exception {
// given
Entity child1 = new DefaultEntity(treeDataDefinition);
child1.setField("name", "Mr T");
Entity root = new DefaultEntity(treeDataDefinition, 2L);
root.setField("name", "Mr X");
root.setField("children", Collections.singletonList(child1));
Entity parent = new DefaultEntity(parentDataDefinition, 1L);
parent.setField("tree", Collections.singletonList(root));
SampleParentDatabaseObject existingParent = new SampleParentDatabaseObject(1L);
SampleTreeDatabaseObject existingChild = new SampleTreeDatabaseObject(2L);
given(session.get(any(Class.class), Matchers.eq(1L))).willReturn(existingParent);
given(session.get(any(Class.class), Matchers.eq(2L))).willReturn(existingChild);
given(session.load(any(Class.class), Matchers.eq(1L))).willReturn(existingParent);
given(session.load(any(Class.class), Matchers.eq(2L))).willReturn(existingChild);
given(criteria.uniqueResult()).willReturn(1L);
// when
Entity entity = parentDataDefinition.save(parent);
// then
assertTrue(entity.isValid());
List<Entity> rootEntities = (List<Entity>) entity.getField("tree");
assertEquals(1, rootEntities.size());
assertEquals(Long.valueOf(1L), rootEntities.get(0).getBelongsToField("owner").getId());
assertNull(rootEntities.get(0).getBelongsToField("parent"));
List<Entity> childEntities = (List<Entity>) rootEntities.get(0).getField("children");
assertEquals(1, childEntities.size());
assertEquals(Long.valueOf(1L), childEntities.get(0).getBelongsToField("owner").getId());
assertEquals(Long.valueOf(2L), childEntities.get(0).getBelongsToField("parent").getId());
verify(session, times(3)).save(Mockito.any());
}
use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceSaveTest method shouldNotSaveEntityTreeField.
@Test
public void shouldNotSaveEntityTreeField() throws Exception {
// given
EntityTree tree = new EntityTreeImpl(dataDefinition, "", 1L);
Entity parent = new DefaultEntity(parentDataDefinition, 1L);
parent.setField("tree", tree);
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(tree, entity.getField("tree"));
verify(session, times(1)).save(Mockito.any());
}
use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.
the class EntityServiceImplTest method shouldSetBelongsToField.
@Test
public void shouldSetBelongsToField() throws Exception {
// given
Entity parentEntity = new DefaultEntity(dataDefinition, 1L);
parentEntity.setField("name", "Mr X");
SampleParentDatabaseObject parentDatabaseEntity = new SampleParentDatabaseObject(1L);
parentDatabaseEntity.setName("Mr X");
SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(2L);
given(session.load(SampleParentDatabaseObject.class, 1L)).willReturn(parentDatabaseEntity);
// when
entityService.setField(databaseEntity, fieldDefinitionBelongsTo, parentEntity);
// then
assertNotNull(databaseEntity.getBelongsTo());
assertEquals(parentDatabaseEntity, databaseEntity.getBelongsTo());
}
use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.
the class EntityServiceImplTest method shouldReturnProperValueOfTheBelongsToField.
@Test
public void shouldReturnProperValueOfTheBelongsToField() throws Exception {
// given
SampleParentDatabaseObject parentDatabaseEntity = new SampleParentDatabaseObject(1L);
parentDatabaseEntity.setName("Mr X");
SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(2L);
databaseEntity.setName("Mr T");
databaseEntity.setBelongsTo(parentDatabaseEntity);
// when
Object value = entityService.getField(databaseEntity, fieldDefinitionBelongsTo);
// then
isInstanceOf(Entity.class, value);
assertEquals(Long.valueOf(1), ((Entity) value).getId());
assertEquals("Mr X", ((Entity) value).getField("name"));
}
use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.
the class EntityServiceImplTest method shouldConvertGenericEntityIntoDatabaseOne.
@Test
public void shouldConvertGenericEntityIntoDatabaseOne() throws Exception {
// given
Entity genericEntity = new DefaultEntity(dataDefinition, 2L);
genericEntity.setField("name", "Mr T");
genericEntity.setField("age", 12);
genericEntity.setField("belongsTo", 1L);
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, null);
// when
Object databaseEntity = entityService.convertToDatabaseEntity(dataDefinition, genericEntity, null);
// then
assertNotNull(databaseEntity);
isInstanceOf(SampleSimpleDatabaseObject.class, databaseEntity);
assertEquals(Long.valueOf(2), ((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());
}
Aggregations