use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject 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());
}
use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceCopyTest method shouldCopyEntityWithUniqueFieldWhenNull.
@Test
public void shouldCopyEntityWithUniqueFieldWhenNull() throws Exception {
// given
FieldHookDefinition fieldHook = new UniqueValidator();
// fieldHook.initialize(dataDefinition, fieldDefinitionName);
fieldDefinitionName.withValidator(fieldHook);
SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
simpleDatabaseObject.setId(13L);
simpleDatabaseObject.setName(null);
simpleDatabaseObject.setAge(66);
given(hibernateService.getTotalNumberOfEntities(Mockito.any(Criteria.class))).willReturn(0);
given(session.get(any(Class.class), Matchers.anyInt())).willReturn(simpleDatabaseObject);
// when
List<Entity> entities = dataDefinition.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(66, entities.get(0).getField("age"));
Assert.assertEquals(null, entities.get(0).getField(NAME));
}
use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceCopyTest method shouldCopyEntityWithUniqueField2.
@Test
public void shouldCopyEntityWithUniqueField2() throws Exception {
// given
FieldHookDefinition fieldHook = new UniqueValidator();
// fieldHook.initialize(dataDefinition, fieldDefinitionName);
fieldDefinitionName.withValidator(fieldHook);
SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
simpleDatabaseObject.setId(13L);
simpleDatabaseObject.setName("Mr T(1)");
simpleDatabaseObject.setAge(66);
given(hibernateService.getTotalNumberOfEntities(Mockito.any(Criteria.class))).willReturn(1, 0);
given(session.get(any(Class.class), Matchers.anyInt())).willReturn(simpleDatabaseObject);
// when
List<Entity> entities = dataDefinition.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(66, entities.get(0).getField("age"));
Assert.assertEquals("Mr T(3)", entities.get(0).getField(NAME));
}
use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject 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);
}
use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceFindTest method shouldReturnValidEntities.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void shouldReturnValidEntities() throws Exception {
// given
List<SampleSimpleDatabaseObject> databaseObjects = new ArrayList<SampleSimpleDatabaseObject>();
databaseObjects.add(createDatabaseObject(1L, "name1", 1));
databaseObjects.add(createDatabaseObject(2L, "name2", 2));
databaseObjects.add(createDatabaseObject(3L, "name3", 3));
databaseObjects.add(createDatabaseObject(4L, "name4", 4));
SearchCriteriaBuilder searchCriteriaBuilder = dataDefinition.find().setFirstResult(0).setMaxResults(4);
given(hibernateService.getTotalNumberOfEntities(Mockito.any(Criteria.class))).willReturn(4);
given(hibernateService.list(Mockito.any(Criteria.class))).willReturn((List) databaseObjects);
// when
SearchResult resultSet = searchCriteriaBuilder.list();
// then
assertEquals(4, resultSet.getTotalNumberOfEntities());
assertEquals(4, resultSet.getEntities().size());
assertEquals(Long.valueOf(1L), resultSet.getEntities().get(0).getId());
assertEquals(Long.valueOf(2L), resultSet.getEntities().get(1).getId());
assertEquals(Long.valueOf(3L), resultSet.getEntities().get(2).getId());
assertEquals(Long.valueOf(4L), resultSet.getEntities().get(3).getId());
}
Aggregations