Search in sources :

Example 1 with SampleTreeDatabaseObject

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());
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleParentDatabaseObject(com.qcadoo.model.beans.sample.SampleParentDatabaseObject) SampleTreeDatabaseObject(com.qcadoo.model.beans.sample.SampleTreeDatabaseObject) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 2 with SampleTreeDatabaseObject

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());
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleParentDatabaseObject(com.qcadoo.model.beans.sample.SampleParentDatabaseObject) SampleTreeDatabaseObject(com.qcadoo.model.beans.sample.SampleTreeDatabaseObject) EntityList(com.qcadoo.model.api.EntityList) List(java.util.List) Test(org.junit.Test)

Example 3 with SampleTreeDatabaseObject

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());
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleParentDatabaseObject(com.qcadoo.model.beans.sample.SampleParentDatabaseObject) SampleTreeDatabaseObject(com.qcadoo.model.beans.sample.SampleTreeDatabaseObject) TreeEntitiesType(com.qcadoo.model.internal.types.TreeEntitiesType) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Aggregations

Entity (com.qcadoo.model.api.Entity)3 SampleParentDatabaseObject (com.qcadoo.model.beans.sample.SampleParentDatabaseObject)3 SampleTreeDatabaseObject (com.qcadoo.model.beans.sample.SampleTreeDatabaseObject)3 Test (org.junit.Test)3 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)2 EntityList (com.qcadoo.model.api.EntityList)1 TreeEntitiesType (com.qcadoo.model.internal.types.TreeEntitiesType)1 List (java.util.List)1 Criteria (org.hibernate.Criteria)1