Search in sources :

Example 6 with SampleParentDatabaseObject

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

Example 7 with SampleParentDatabaseObject

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

Example 8 with SampleParentDatabaseObject

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

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

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