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