Search in sources :

Example 6 with SampleSimpleDatabaseObject

use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.

the class DataAccessServiceCopyTest method shouldCopyEntityWithUniqueField.

@Test
public void shouldCopyEntityWithUniqueField() 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");
    simpleDatabaseObject.setAge(66);
    given(criteria.setProjection(Projections.rowCount()).uniqueResult()).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("Mr T(1)", entities.get(0).getField(NAME));
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldHookDefinition(com.qcadoo.model.internal.api.FieldHookDefinition) UniqueValidator(com.qcadoo.model.internal.validators.UniqueValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 7 with SampleSimpleDatabaseObject

use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.

the class DataAccessServiceCopyTest method shouldCopyEntity.

@Test
public void shouldCopyEntity() throws Exception {
    // given
    SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
    simpleDatabaseObject.setId(13L);
    simpleDatabaseObject.setName("Mr T");
    simpleDatabaseObject.setAge(66);
    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", entities.get(0).getField(NAME));
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 8 with SampleSimpleDatabaseObject

use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject 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());
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleParentDatabaseObject(com.qcadoo.model.beans.sample.SampleParentDatabaseObject) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) HasManyEntitiesType(com.qcadoo.model.internal.types.HasManyEntitiesType) Test(org.junit.Test)

Example 9 with SampleSimpleDatabaseObject

use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.

the class DataAccessServiceDeleteTest method shouldProperlyDelete.

@Test
public void shouldProperlyDelete() throws Exception {
    // given
    SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
    simpleDatabaseObject.setId(1L);
    simpleDatabaseObject.setName("Mr T");
    simpleDatabaseObject.setAge(66);
    given(session.get(SampleSimpleDatabaseObject.class, 1L)).willReturn(simpleDatabaseObject);
    // when
    dataDefinition.delete(1L);
    // then
    verify(session).delete(simpleDatabaseObject);
}
Also used : SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 10 with SampleSimpleDatabaseObject

use of com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject in project qcadoo by qcadoo.

the class EntityServiceImplTest method shouldReturnNullWhileGettingEmptyField.

@Test
public void shouldReturnNullWhileGettingEmptyField() throws Exception {
    // given
    SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(1L);
    // when
    Object value = entityService.getField(databaseEntity, fieldDefinitionName);
    // then
    assertNull(value);
}
Also used : SampleParentDatabaseObject(com.qcadoo.model.beans.sample.SampleParentDatabaseObject) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) 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