Search in sources :

Example 41 with SampleSimpleDatabaseObject

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

the class PriorityTest method shouldChangeEntitiesBetweenCurrentAndTargetPriorityWhileMoving.

@Test
public void shouldChangeEntitiesBetweenCurrentAndTargetPriorityWhileMoving() throws Exception {
    // given
    SampleSimpleDatabaseObject existingDatabaseObject = new SampleSimpleDatabaseObject(1L);
    existingDatabaseObject.setPriority(5);
    SampleSimpleDatabaseObject otherDatabaseObject = new SampleSimpleDatabaseObject(2L);
    otherDatabaseObject.setPriority(6);
    given(session.get(any(Class.class), Matchers.anyInt())).willReturn(existingDatabaseObject);
    given(criteria.uniqueResult()).willReturn(6);
    given(criteria.list()).willReturn(Lists.newArrayList(otherDatabaseObject));
    // when
    dataDefinition.move(1L, 1);
    // then
    SampleSimpleDatabaseObject movedDatabaseObject = new SampleSimpleDatabaseObject(1L);
    movedDatabaseObject.setPriority(6);
    verify(session).update(movedDatabaseObject);
    SampleSimpleDatabaseObject updatedDatabaseObject = new SampleSimpleDatabaseObject(2L);
    updatedDatabaseObject.setPriority(5);
    verify(session).update(updatedDatabaseObject);
}
Also used : SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 42 with SampleSimpleDatabaseObject

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

the class DataAccessServiceCopyTest method shouldCopyEntityWithoutInfinityCycleWith3Entities.

@Test
public void shouldCopyEntityWithoutInfinityCycleWith3Entities() {
    // given
    SampleSimpleDatabaseObject simpleDatabaseObject1 = new SampleSimpleDatabaseObject();
    simpleDatabaseObject1.setId(1L);
    simpleDatabaseObject1.setName(SIMPLE_1);
    SampleSimpleDatabaseObject simpleDatabaseObject2 = new SampleSimpleDatabaseObject();
    simpleDatabaseObject2.setId(2L);
    simpleDatabaseObject2.setName(SIMPLE_2);
    SampleSimpleDatabaseObject simpleDatabaseObject3 = new SampleSimpleDatabaseObject();
    simpleDatabaseObject3.setId(3L);
    simpleDatabaseObject3.setName(SIMPLE_3);
    simpleDatabaseObject1.setBelongsToSimple(simpleDatabaseObject2);
    simpleDatabaseObject2.setBelongsToSimple(simpleDatabaseObject3);
    simpleDatabaseObject3.setBelongsToSimple(simpleDatabaseObject1);
    stubSessionGet(simpleDatabaseObject1);
    stubSessionGet(simpleDatabaseObject2);
    stubSessionGet(simpleDatabaseObject3);
    // when
    List<Entity> entities = null;
    try {
        entities = dataDefinition.copy(new Long[] { simpleDatabaseObject1.getId() });
    } catch (StackOverflowError e) {
        Assert.fail();
    }
    // then
    final ArgumentCaptor<SampleSimpleDatabaseObject> argCaptor = ArgumentCaptor.forClass(SampleSimpleDatabaseObject.class);
    verify(session, times(1)).save(Mockito.any(SampleSimpleDatabaseObject.class));
    verify(session, times(1)).save(argCaptor.capture());
    final SampleSimpleDatabaseObject savedObject = argCaptor.getValue();
    assertEquals(SIMPLE_1, savedObject.getName());
    assertFalse(simpleDatabaseObject1.getId().equals(savedObject.getId()));
    assertEquals(SIMPLE_2, savedObject.getBelongsToSimple().getName());
    assertEquals(simpleDatabaseObject2.getId(), savedObject.getBelongsToSimple().getId());
    assertEquals(SIMPLE_3, savedObject.getBelongsToSimple().getBelongsToSimple().getName());
    assertEquals(simpleDatabaseObject3.getId(), savedObject.getBelongsToSimple().getBelongsToSimple().getId());
    assertEquals(1, entities.size());
    assertTrue(entities.get(0).isValid());
    final Entity entityCopy = entities.get(0);
    Assert.assertEquals(SIMPLE_1, entityCopy.getField(NAME));
    Assert.assertEquals(SIMPLE_2, entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
    Assert.assertEquals(SIMPLE_3, entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
    Assert.assertEquals(Long.valueOf(2L), entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getId());
    assertFalse(simpleDatabaseObject1.getId().equals(entityCopy.getId()));
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 43 with SampleSimpleDatabaseObject

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

the class DataAccessServiceCopyTest method shouldCopyEntityWithoutInfinityCycleWith4Entities.

@Test
public void shouldCopyEntityWithoutInfinityCycleWith4Entities() {
    // given
    SampleSimpleDatabaseObject simpleDatabaseObject1 = new SampleSimpleDatabaseObject();
    simpleDatabaseObject1.setId(1L);
    simpleDatabaseObject1.setName(SIMPLE_1);
    SampleSimpleDatabaseObject simpleDatabaseObject2 = new SampleSimpleDatabaseObject();
    simpleDatabaseObject2.setId(2L);
    simpleDatabaseObject2.setName(SIMPLE_2);
    SampleSimpleDatabaseObject simpleDatabaseObject3 = new SampleSimpleDatabaseObject();
    simpleDatabaseObject3.setId(3L);
    simpleDatabaseObject3.setName(SIMPLE_3);
    SampleSimpleDatabaseObject simpleDatabaseObject4 = new SampleSimpleDatabaseObject();
    simpleDatabaseObject4.setId(4L);
    simpleDatabaseObject4.setName(SIMPLE_4);
    simpleDatabaseObject1.setBelongsToSimple(simpleDatabaseObject2);
    simpleDatabaseObject2.setBelongsToSimple(simpleDatabaseObject3);
    simpleDatabaseObject3.setBelongsToSimple(simpleDatabaseObject4);
    simpleDatabaseObject4.setBelongsToSimple(simpleDatabaseObject1);
    stubSessionGet(simpleDatabaseObject1);
    stubSessionGet(simpleDatabaseObject2);
    stubSessionGet(simpleDatabaseObject3);
    stubSessionGet(simpleDatabaseObject4);
    // when
    List<Entity> entities = null;
    try {
        entities = dataDefinition.copy(new Long[] { simpleDatabaseObject1.getId() });
    } catch (StackOverflowError e) {
        Assert.fail();
    }
    // then
    final ArgumentCaptor<SampleSimpleDatabaseObject> argCaptor = ArgumentCaptor.forClass(SampleSimpleDatabaseObject.class);
    verify(session, times(1)).save(Mockito.any(SampleSimpleDatabaseObject.class));
    verify(session, times(1)).save(argCaptor.capture());
    final SampleSimpleDatabaseObject savedObject = argCaptor.getValue();
    assertEquals(SIMPLE_1, savedObject.getName());
    assertFalse(simpleDatabaseObject1.getId().equals(savedObject.getId()));
    assertEquals(SIMPLE_2, savedObject.getBelongsToSimple().getName());
    assertEquals(simpleDatabaseObject2.getId(), savedObject.getBelongsToSimple().getId());
    assertEquals(SIMPLE_3, savedObject.getBelongsToSimple().getBelongsToSimple().getName());
    assertEquals(simpleDatabaseObject3.getId(), savedObject.getBelongsToSimple().getBelongsToSimple().getId());
    assertEquals(SIMPLE_4, savedObject.getBelongsToSimple().getBelongsToSimple().getBelongsToSimple().getName());
    assertEquals(simpleDatabaseObject4.getId(), savedObject.getBelongsToSimple().getBelongsToSimple().getBelongsToSimple().getId());
    assertEquals(1, entities.size());
    assertTrue(entities.get(0).isValid());
    final Entity entityCopy = entities.get(0);
    Assert.assertEquals(SIMPLE_1, entityCopy.getField(NAME));
    Assert.assertEquals(SIMPLE_2, entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
    Assert.assertEquals(SIMPLE_3, entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
    Assert.assertEquals(SIMPLE_4, entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getBelongsToField(BELONGS_TO_SIMPLE).getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
    Assert.assertEquals(Long.valueOf(2L), entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getId());
    assertFalse(simpleDatabaseObject1.getId().equals(entityCopy.getId()));
}
Also used : Entity(com.qcadoo.model.api.Entity) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 44 with SampleSimpleDatabaseObject

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

the class DataAccessServiceCopyTest method shouldCopyEntityWithoutHasManyField.

@Test
public void shouldCopyEntityWithoutHasManyField() throws Exception {
    // given
    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(criteria.setProjection(Projections.rowCount()).uniqueResult()).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(criteria.list()).willReturn(Lists.newArrayList(simpleDatabaseObject));
    // when
    List<Entity> entities = parentDataDefinition.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("Mr T", entities.get(0).getField(NAME));
    verify(session, times(1)).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) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 45 with SampleSimpleDatabaseObject

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

the class DataAccessServiceCopyTest method shouldCopyEntityWithoutInfinityCycleWith2Entities.

@Test
public void shouldCopyEntityWithoutInfinityCycleWith2Entities() {
    // given
    SampleSimpleDatabaseObject simpleDatabaseObject1 = new SampleSimpleDatabaseObject();
    simpleDatabaseObject1.setId(1L);
    simpleDatabaseObject1.setName(SIMPLE_1);
    SampleSimpleDatabaseObject simpleDatabaseObject2 = new SampleSimpleDatabaseObject();
    simpleDatabaseObject2.setId(2L);
    simpleDatabaseObject2.setName(SIMPLE_2);
    simpleDatabaseObject1.setBelongsToSimple(simpleDatabaseObject2);
    simpleDatabaseObject2.setBelongsToSimple(simpleDatabaseObject1);
    stubSessionGet(simpleDatabaseObject1);
    stubSessionGet(simpleDatabaseObject2);
    // when
    List<Entity> entities = null;
    try {
        entities = dataDefinition.copy(new Long[] { simpleDatabaseObject1.getId() });
    } catch (StackOverflowError e) {
        Assert.fail();
    }
    // then
    final ArgumentCaptor<SampleSimpleDatabaseObject> argCaptor = ArgumentCaptor.forClass(SampleSimpleDatabaseObject.class);
    verify(session, times(1)).save(Mockito.any(SampleSimpleDatabaseObject.class));
    verify(session, times(1)).save(argCaptor.capture());
    final SampleSimpleDatabaseObject savedObject = argCaptor.getValue();
    assertEquals(SIMPLE_1, savedObject.getName());
    assertFalse(simpleDatabaseObject1.getId().equals(savedObject.getId()));
    assertEquals(SIMPLE_2, savedObject.getBelongsToSimple().getName());
    assertEquals(simpleDatabaseObject2.getId(), savedObject.getBelongsToSimple().getId());
    assertEquals(1, entities.size());
    assertTrue(entities.get(0).isValid());
    final Entity entityCopy = entities.get(0);
    Assert.assertEquals(SIMPLE_1, entityCopy.getField(NAME));
    Assert.assertEquals(SIMPLE_2, entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
    Assert.assertEquals(Long.valueOf(2L), entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getId());
    assertFalse(simpleDatabaseObject1.getId().equals(entityCopy.getId()));
}
Also used : Entity(com.qcadoo.model.api.Entity) 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