Search in sources :

Example 31 with Entity

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

Example 32 with Entity

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));
}
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 33 with Entity

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);
}
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)

Example 34 with Entity

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

Example 35 with Entity

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

Aggregations

Entity (com.qcadoo.model.api.Entity)2833 FormComponent (com.qcadoo.view.api.components.FormComponent)514 Test (org.junit.Test)491 BigDecimal (java.math.BigDecimal)376 DataDefinition (com.qcadoo.model.api.DataDefinition)337 FieldComponent (com.qcadoo.view.api.components.FieldComponent)210 LookupComponent (com.qcadoo.view.api.components.LookupComponent)188 Date (java.util.Date)185 List (java.util.List)149 GridComponent (com.qcadoo.view.api.components.GridComponent)141 Map (java.util.Map)124 Autowired (org.springframework.beans.factory.annotation.Autowired)114 Service (org.springframework.stereotype.Service)112 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)105 Transactional (org.springframework.transaction.annotation.Transactional)101 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)100 DataDefinitionService (com.qcadoo.model.api.DataDefinitionService)95 Collectors (java.util.stream.Collectors)87 Lists (com.google.common.collect.Lists)75 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)75