Search in sources :

Example 1 with HasManyEntitiesType

use of com.qcadoo.model.internal.types.HasManyEntitiesType 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 2 with HasManyEntitiesType

use of com.qcadoo.model.internal.types.HasManyEntitiesType 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)2 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)2 HasManyEntitiesType (com.qcadoo.model.internal.types.HasManyEntitiesType)2 Criteria (org.hibernate.Criteria)2 Test (org.junit.Test)2 Entity (com.qcadoo.model.api.Entity)1 List (java.util.List)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1