use of com.qcadoo.model.beans.sample.SampleTreeDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceCopyTest method shouldCopyEntityWithoutTreeField.
@Test
public void shouldCopyEntityWithoutTreeField() throws Exception {
// given
SampleTreeDatabaseObject treeDatabaseObject = new SampleTreeDatabaseObject();
treeDatabaseObject.setId(12L);
treeDatabaseObject.setName("Mr T");
SampleParentDatabaseObject parentDatabaseObject = new SampleParentDatabaseObject();
parentDatabaseObject.setId(13L);
parentDatabaseObject.setName("Mr T");
given(criteria.setProjection(Projections.rowCount()).uniqueResult()).willReturn(1, 0);
given(session.get(Mockito.eq(SampleSimpleDatabaseObject.class), Mockito.eq(12L))).willReturn(treeDatabaseObject);
given(session.get(Mockito.eq(SampleParentDatabaseObject.class), Mockito.eq(13L))).willReturn(parentDatabaseObject);
given(criteria.list()).willReturn(Lists.newArrayList(treeDatabaseObject));
// when
List<Entity> entities = parentDataDefinition.copy(new Long[] { 13L });
// then
assertEquals(1, entities.size());
assertTrue(entities.get(0).isValid());
Assert.assertEquals("Mr T", entities.get(0).getField(NAME));
verify(session, times(1)).save(Mockito.any());
verify(session, never()).get(Mockito.eq(SampleSimpleDatabaseObject.class), anyInt());
}
use of com.qcadoo.model.beans.sample.SampleTreeDatabaseObject 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.SampleTreeDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceCopyTest method shouldCopyEntityWithTreeField.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldCopyEntityWithTreeField() throws Exception {
// given
parentFieldDefinitionTree.withType(new TreeEntitiesType("tree", "entity", "owner", TreeType.Cascade.DELETE, true, dataDefinitionService));
SampleTreeDatabaseObject treeDatabaseObject = new SampleTreeDatabaseObject();
treeDatabaseObject.setId(12L);
treeDatabaseObject.setName("Mr T");
SampleParentDatabaseObject parentDatabaseObject = new SampleParentDatabaseObject();
parentDatabaseObject.setId(13L);
parentDatabaseObject.setName("Mr T");
given(hibernateService.getTotalNumberOfEntities(Mockito.any(Criteria.class))).willReturn(1, 0);
given(session.get(Mockito.eq(SampleSimpleDatabaseObject.class), Mockito.eq(12L))).willReturn(treeDatabaseObject);
given(session.get(Mockito.eq(SampleParentDatabaseObject.class), Mockito.eq(13L))).willReturn(parentDatabaseObject);
given(hibernateService.list(Mockito.any(Criteria.class))).willReturn((List) Lists.newArrayList(treeDatabaseObject));
// when
List<Entity> entities = parentDataDefinition.copy(new Long[] { 13L });
// then
assertEquals(1, entities.size());
assertTrue(entities.get(0).isValid());
Assert.assertEquals("Mr T", entities.get(0).getField(NAME));
verify(session, times(2)).save(Mockito.any());
verify(session, never()).get(Mockito.eq(SampleSimpleDatabaseObject.class), anyInt());
}
Aggregations