use of com.qcadoo.model.api.Entity 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.api.Entity 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.api.Entity 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);
}
use of com.qcadoo.model.api.Entity in project qcadoo by qcadoo.
the class DefaultEntityTest method shouldCopyReturnCopyOfEntity.
@Test
public final void shouldCopyReturnCopyOfEntity() throws Exception {
// given
Entity belongsToEntity = mock(Entity.class);
when(belongsToEntity.getDataDefinition()).thenReturn(belongsToFieldDataDefinition);
when(belongsToEntity.copy()).thenReturn(belongsToEntity);
defaultEntity.setId(1L);
defaultEntity.setField(BELONGS_TO_FIELD_NAME, belongsToEntity);
defaultEntity.setField(STRING_FIELD_NAME, "some string value");
// when
Entity returnedCopy = defaultEntity.copy();
// then
assertEquals(defaultEntity, returnedCopy);
assertEquals(defaultEntity.hashCode(), returnedCopy.hashCode());
assertNotSame(defaultEntity, returnedCopy);
}
use of com.qcadoo.model.api.Entity in project qcadoo by qcadoo.
the class DefaultEntityTest method shouldEqualsReturnTruIfBelongsToFieldIsBothNull.
@Test
public final void shouldEqualsReturnTruIfBelongsToFieldIsBothNull() {
// given
final Entity firstEntity = new DefaultEntity(dataDefinition);
final Entity secondEntity = null;
final Entity firstOtherEntity = new DefaultEntity(dataDefinition);
final Entity secondOtherEntity = null;
firstEntity.setField(BELONGS_TO_FIELD_NAME, secondEntity);
firstEntity.setField(STRING_FIELD_NAME, L_FIRST);
firstOtherEntity.setField(BELONGS_TO_FIELD_NAME, secondOtherEntity);
firstOtherEntity.setField(STRING_FIELD_NAME, L_FIRST);
// when
final boolean result = firstEntity.equals(firstOtherEntity);
// then
assertTrue(result);
}
Aggregations