Search in sources :

Example 1 with SampleParentDatabaseObject

use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.

the class DataAccessServiceGetTest method shouldGetHasManyField.

@Test
public void shouldGetHasManyField() 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("entities"), CoreMatchers.instanceOf(EntityListImpl.class));
    EntityList entities = entity.getHasManyField("entities");
    assertEquals(1L, getField(entities, "parentId"));
    assertEquals("belongsTo", ((FieldDefinition) getField(entities, "joinFieldDefinition")).getName());
    assertEquals("simple.entity", ((DataDefinition) getField(entities, "dataDefinition")).getName());
}
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)

Example 2 with SampleParentDatabaseObject

use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.

the class DataAccessServiceSaveTest method shouldSaveHasManyField.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldSaveHasManyField() throws Exception {
    // given
    Entity child1 = new DefaultEntity(dataDefinition, 2L);
    child1.setField("name", "Mr T");
    child1.setField("age", "66");
    Entity child2 = new DefaultEntity(dataDefinition);
    child2.setField("name", "Mr X");
    child2.setField("age", "67");
    List<Entity> children = Arrays.asList(new Entity[] { child1, child2 });
    Entity parent = new DefaultEntity(parentDataDefinition, 1L);
    parent.setField("entities", children);
    SampleParentDatabaseObject existingParent = new SampleParentDatabaseObject(1L);
    SampleSimpleDatabaseObject existingChild = new SampleSimpleDatabaseObject(2L);
    SampleSimpleDatabaseObject existingChildToDelete = new SampleSimpleDatabaseObject(13L);
    given(session.get(any(Class.class), Matchers.eq(1L))).willReturn(existingParent);
    given(session.get(any(Class.class), Matchers.eq(2L))).willReturn(existingChild);
    given(session.get(any(Class.class), Matchers.eq(13L))).willReturn(existingChildToDelete);
    given(session.load(any(Class.class), Matchers.eq(1L))).willReturn(existingParent);
    given(hibernateService.getTotalNumberOfEntities(Mockito.any(Criteria.class))).willReturn(1);
    given(hibernateService.list(Mockito.any(Criteria.class))).willReturn((List) Arrays.asList(new SampleSimpleDatabaseObject[] { existingChild, existingChildToDelete }));
    // when
    Entity entity = parentDataDefinition.save(parent);
    // then
    assertTrue(entity.isValid());
    List<Entity> entities = (List<Entity>) entity.getField("entities");
    assertEquals(2, entities.size());
    assertEquals(Long.valueOf(1L), entities.get(0).getBelongsToField("belongsTo").getId());
    assertEquals(Long.valueOf(1L), entities.get(1).getBelongsToField("belongsTo").getId());
    verify(session, times(3)).save(Mockito.any());
    verify(session, times(1)).delete(existingChildToDelete);
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleParentDatabaseObject(com.qcadoo.model.beans.sample.SampleParentDatabaseObject) EntityList(com.qcadoo.model.api.EntityList) List(java.util.List) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 3 with SampleParentDatabaseObject

use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject 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 4 with SampleParentDatabaseObject

use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.

the class DataAccessServiceCopyTest method shouldCopyEntityWithHasManyField.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void shouldCopyEntityWithHasManyField() throws Exception {
    // given
    parentFieldDefinitionHasMany.withType(new HasManyEntitiesType("simple", "entity", "belongsTo", HasManyType.Cascade.DELETE, true, dataDefinitionService));
    SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
    simpleDatabaseObject.setId(12L);
    simpleDatabaseObject.setName("Mr T");
    simpleDatabaseObject.setAge(66);
    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(simpleDatabaseObject);
    given(session.get(Mockito.eq(SampleParentDatabaseObject.class), Mockito.eq(13L))).willReturn(parentDatabaseObject);
    given(hibernateService.list(Mockito.any(Criteria.class))).willReturn((List) Lists.newArrayList(simpleDatabaseObject));
    // 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) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) HasManyEntitiesType(com.qcadoo.model.internal.types.HasManyEntitiesType) Test(org.junit.Test)

Example 5 with SampleParentDatabaseObject

use of com.qcadoo.model.beans.sample.SampleParentDatabaseObject in project qcadoo by qcadoo.

the class EntityServiceImplTest method shouldConvertDatabaseEntityIntoGenericOneWithHasMany.

@Test
public void shouldConvertDatabaseEntityIntoGenericOneWithHasMany() throws Exception {
    // given
    SampleParentDatabaseObject parentDatabaseEntity = new SampleParentDatabaseObject(1L);
    parentDatabaseEntity.setName("Mr X");
    // when
    Entity genericEntity = entityService.convertToGenericEntity(parentDataDefinition, parentDatabaseEntity);
    // then
    assertNotNull(genericEntity);
    assertEquals(Long.valueOf(1), genericEntity.getId());
    List<Entity> hasManyField = genericEntity.getHasManyField("entities");
    Field field = ReflectionUtils.findField(EntityListImpl.class, "entities");
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, hasManyField, Collections.emptyList());
    assertThat(hasManyField, instanceOf(EntityListImpl.class));
    assertEquals(dataDefinition, ReflectionTestUtils.getField(hasManyField, "dataDefinition"));
    assertEquals(Long.valueOf(1), ReflectionTestUtils.getField(hasManyField, "parentId"));
    assertEquals(fieldDefinitionBelongsTo, ReflectionTestUtils.getField(hasManyField, "joinFieldDefinition"));
}
Also used : Entity(com.qcadoo.model.api.Entity) Field(java.lang.reflect.Field) SampleParentDatabaseObject(com.qcadoo.model.beans.sample.SampleParentDatabaseObject) 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