Search in sources :

Example 16 with SampleParentDatabaseObject

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

the class DataAccessServiceCopyTest method shouldCopyEntityWithoutHasManyField.

@Test
public void shouldCopyEntityWithoutHasManyField() throws Exception {
    // given
    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(criteria.setProjection(Projections.rowCount()).uniqueResult()).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(criteria.list()).willReturn(Lists.newArrayList(simpleDatabaseObject));
    // when
    List<Entity> entities = parentDataDefinition.copy(new Long[] { 13L });
    // then
    verify(session, times(1)).save(Mockito.any(SampleSimpleDatabaseObject.class));
    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) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 17 with SampleParentDatabaseObject

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

the class DataAccessServiceCopyTest method shouldCopyEntityWithManyToManyField.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldCopyEntityWithManyToManyField() throws Exception {
    // given
    SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
    simpleDatabaseObject.setId(12L);
    simpleDatabaseObject.setName("Mr T");
    simpleDatabaseObject.setAge(30);
    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).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) Test(org.junit.Test)

Example 18 with SampleParentDatabaseObject

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

Example 19 with SampleParentDatabaseObject

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

the class DataAccessServiceDeleteTest method shouldProperlyDeleteAndNullifyChildren.

@Test
public void shouldProperlyDeleteAndNullifyChildren() throws Exception {
    // given
    SampleParentDatabaseObject parentDatabaseEntity = new SampleParentDatabaseObject(1L);
    parentDatabaseEntity.setName("Mr X");
    final SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject(1L);
    simpleDatabaseObject.setName("Mr T");
    simpleDatabaseObject.setAge(66);
    simpleDatabaseObject.setBelongsTo(parentDatabaseEntity);
    parentFieldDefinitionHasMany.withType(new HasManyEntitiesType("simple", "entity", "belongsTo", HasManyType.Cascade.NULLIFY, false, dataDefinitionService));
    parentDataDefinition.withField(parentFieldDefinitionHasMany);
    given(session.get(SampleParentDatabaseObject.class, 1L)).willReturn(parentDatabaseEntity);
    given(hibernateService.getTotalNumberOfEntities(Mockito.any(Criteria.class))).willReturn(1);
    given(hibernateService.list(Mockito.any(Criteria.class))).willAnswer(new Answer<List<Object>>() {

        @Override
        public List<Object> answer(final InvocationOnMock invocation) throws Throwable {
            Criteria criteria = (Criteria) invocation.getArguments()[0];
            if (criteria.toString().contains("belongsTo.id=")) {
                return Lists.<Object>newArrayList(simpleDatabaseObject);
            }
            return Lists.newArrayList();
        }
    });
    given(session.get(SampleSimpleDatabaseObject.class, 1L)).willReturn(simpleDatabaseObject);
    // when
    parentDataDefinition.delete(1L);
    // then
    verify(session).save(simpleDatabaseObject);
    verify(session).delete(parentDatabaseEntity);
}
Also used : SampleParentDatabaseObject(com.qcadoo.model.beans.sample.SampleParentDatabaseObject) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) HasManyEntitiesType(com.qcadoo.model.internal.types.HasManyEntitiesType) 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