Search in sources :

Example 1 with SampleSimpleDatabaseObject

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);
}
Also used : IntegerType(com.qcadoo.model.internal.types.IntegerType) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 2 with SampleSimpleDatabaseObject

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());
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 3 with SampleSimpleDatabaseObject

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());
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 4 with SampleSimpleDatabaseObject

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));
}
Also used : Entity(com.qcadoo.model.api.Entity) EntityHookDefinition(com.qcadoo.model.internal.api.EntityHookDefinition) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 5 with SampleSimpleDatabaseObject

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);
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleParentDatabaseObject(com.qcadoo.model.beans.sample.SampleParentDatabaseObject) EntityList(com.qcadoo.model.api.EntityList) List(java.util.List) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Aggregations

SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)50 Test (org.junit.Test)49 Entity (com.qcadoo.model.api.Entity)27 SampleParentDatabaseObject (com.qcadoo.model.beans.sample.SampleParentDatabaseObject)13 DataAccessTest (com.qcadoo.model.internal.DataAccessTest)8 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)8 EntityHookDefinitionImpl (com.qcadoo.model.internal.hooks.EntityHookDefinitionImpl)7 Criteria (org.hibernate.Criteria)7 UniqueValidator (com.qcadoo.model.internal.validators.UniqueValidator)4 FieldDefinition (com.qcadoo.model.api.FieldDefinition)3 FieldHookDefinition (com.qcadoo.model.internal.api.FieldHookDefinition)3 HasManyEntitiesType (com.qcadoo.model.internal.types.HasManyEntitiesType)2 IntegerType (com.qcadoo.model.internal.types.IntegerType)2 List (java.util.List)2 EntityList (com.qcadoo.model.api.EntityList)1 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)1 SearchResult (com.qcadoo.model.api.search.SearchResult)1 EntityHookDefinition (com.qcadoo.model.internal.api.EntityHookDefinition)1 StringType (com.qcadoo.model.internal.types.StringType)1 ArrayList (java.util.ArrayList)1