use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceGetTest method shouldNotFailIfFieldTypeIsNotValid.
@Test
public void shouldNotFailIfFieldTypeIsNotValid() throws Exception {
// given
fieldDefinitionName.withType(new IntegerType());
SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
simpleDatabaseObject.setId(1L);
simpleDatabaseObject.setName("Mr T");
simpleDatabaseObject.setAge(66);
given(criteria.uniqueResult()).willReturn(simpleDatabaseObject);
// when
dataDefinition.get(1L);
}
use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceSaveTest method shouldSaveNewEntity.
@Test
public void shouldSaveNewEntity() throws Exception {
// given
Entity entity = new DefaultEntity(dataDefinition);
entity.setField("name", "Mr T");
entity.setField("age", 66);
SampleSimpleDatabaseObject databaseObject = new SampleSimpleDatabaseObject();
databaseObject.setName("Mr T");
databaseObject.setAge(66);
// when
entity = dataDefinition.save(entity);
// then
verify(session).save(databaseObject);
assertTrue(entity.isValid());
}
use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceSaveTest method shouldSaveExistingEntity.
@Test
public void shouldSaveExistingEntity() throws Exception {
// given
Entity entity = new DefaultEntity(dataDefinition, 1L);
entity.setField("name", "Mr T");
entity.setField("age", 66);
SampleSimpleDatabaseObject existingDatabaseObject = new SampleSimpleDatabaseObject();
existingDatabaseObject.setId(1L);
existingDatabaseObject.setName("Mr X");
existingDatabaseObject.setAge(33);
given(session.get(any(Class.class), Matchers.anyInt())).willReturn(existingDatabaseObject);
SampleSimpleDatabaseObject databaseObject = new SampleSimpleDatabaseObject();
databaseObject.setId(1L);
databaseObject.setName("Mr T");
databaseObject.setAge(66);
// when
entity = dataDefinition.save(entity);
// then
verify(session).save(databaseObject);
assertTrue(entity.isValid());
}
use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.
the class DataAccessServiceSaveTest method shouldNotOmitCreateHooksExceptOnCopy.
@Test
public void shouldNotOmitCreateHooksExceptOnCopy() throws Exception {
EntityHookDefinition onCreateHook = mock(EntityHookDefinition.class);
given(onCreateHook.isEnabled()).willReturn(true);
dataDefinition.addCreateHook(onCreateHook);
Entity entity = new DefaultEntity(dataDefinition);
entity.setField("name", "Mr T");
entity.setField("age", 66);
SampleSimpleDatabaseObject databaseObject = new SampleSimpleDatabaseObject();
databaseObject.setName("Mr T");
databaseObject.setAge(66);
// when
entity = dataDefinition.save(entity);
// then
verify(session).save(databaseObject);
assertTrue(entity.isValid());
verify(onCreateHook, times(1)).isEnabled();
verify(onCreateHook, times(1)).call(Mockito.any(Entity.class));
}
use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject 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);
}
Aggregations