Search in sources :

Example 46 with SampleSimpleDatabaseObject

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());
}
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) Test(org.junit.Test)

Example 47 with SampleSimpleDatabaseObject

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

Example 48 with SampleSimpleDatabaseObject

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

Example 49 with SampleSimpleDatabaseObject

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);
}
Also used : SampleParentDatabaseObject(com.qcadoo.model.beans.sample.SampleParentDatabaseObject) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) HasManyEntitiesType(com.qcadoo.model.internal.types.HasManyEntitiesType) Test(org.junit.Test)

Example 50 with SampleSimpleDatabaseObject

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());
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) ArrayList(java.util.ArrayList) SearchResult(com.qcadoo.model.api.search.SearchResult) 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