Search in sources :

Example 11 with SampleParentDatabaseObject

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());
}
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 12 with SampleParentDatabaseObject

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

Example 13 with SampleParentDatabaseObject

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

Example 14 with SampleParentDatabaseObject

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

Example 15 with SampleParentDatabaseObject

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

Aggregations

SampleParentDatabaseObject (com.qcadoo.model.beans.sample.SampleParentDatabaseObject)19 Test (org.junit.Test)19 Entity (com.qcadoo.model.api.Entity)16 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)13 Criteria (org.hibernate.Criteria)5 EntityList (com.qcadoo.model.api.EntityList)4 SampleTreeDatabaseObject (com.qcadoo.model.beans.sample.SampleTreeDatabaseObject)3 List (java.util.List)3 EntityTree (com.qcadoo.model.api.EntityTree)2 HasManyEntitiesType (com.qcadoo.model.internal.types.HasManyEntitiesType)2 TreeEntitiesType (com.qcadoo.model.internal.types.TreeEntitiesType)1 Field (java.lang.reflect.Field)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1