Search in sources :

Example 16 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.

the class EntityListImplTest method shouldReturnCriteriaBuilder.

@Test
public void shouldReturnCriteriaBuilder() throws Exception {
    // given
    BelongsToType fieldType = mock(BelongsToType.class);
    InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
    given(fieldType.getDataDefinition()).willReturn(dataDefinition);
    FieldDefinition fieldDefinition = mock(FieldDefinition.class);
    given(fieldDefinition.getType()).willReturn(fieldType);
    given(fieldDefinition.getName()).willReturn("field");
    given(dataDefinition.getField("hasMany")).willReturn(fieldDefinition);
    SearchCriteriaBuilder searchCriteriaBuilder = mock(SearchCriteriaBuilder.class);
    given(dataDefinition.find().createAlias(fieldDefinition.getName(), fieldDefinition.getName()).add(SearchRestrictions.eq(fieldDefinition.getName() + ".id", 1L))).willReturn(searchCriteriaBuilder);
    EntityList list = new EntityListImpl(dataDefinition, "hasMany", 1L);
    // then
    assertEquals(searchCriteriaBuilder, list.find());
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) EntityList(com.qcadoo.model.api.EntityList) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Test(org.junit.Test)

Example 17 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.

the class EntityServiceImplTest method shouldLazyLoadEntitiesUsingProxy.

@Test
public void shouldLazyLoadEntitiesUsingProxy() throws Exception {
    // given
    InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
    FieldDefinition fieldDefinition = mock(FieldDefinition.class);
    BelongsToType fieldType = mock(BelongsToType.class);
    Entity entity1 = new DefaultEntity(dataDefinition, 1L);
    Entity entity2 = new DefaultEntity(dataDefinition, 2L);
    given(fieldDefinition.getName()).willReturn("joinField");
    given(fieldDefinition.getType()).willReturn(fieldType);
    given(fieldType.getDataDefinition()).willReturn(dataDefinition);
    given(dataDefinition.isEnabled()).willReturn(true);
    given(dataDefinition.getField("joinField")).willReturn(fieldDefinition);
    given(dataDefinition.find().createAlias(fieldDefinition.getName(), fieldDefinition.getName()).add(SearchRestrictions.eq(fieldDefinition.getName() + ".id", 5L)).list().getEntities()).willReturn(Lists.newArrayList(entity1, entity2));
    List<Entity> entityList = new EntityListImpl(dataDefinition, "joinField", 5L);
    // then
    assertNotNull(entityList);
    assertEquals(2, entityList.size());
    assertThat(entityList, JUnitMatchers.hasItems(entity1, entity2));
}
Also used : Entity(com.qcadoo.model.api.Entity) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Test(org.junit.Test)

Example 18 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.

the class EntityTreeImplTest method shouldBuildTree.

@Test
public void shouldBuildTree() throws Exception {
    // given
    Entity entity1 = mock(Entity.class);
    given(entity1.getId()).willReturn(1L);
    Entity entity2 = mock(Entity.class);
    given(entity2.getId()).willReturn(2L);
    given(entity2.getBelongsToField("parent")).willReturn(entity1);
    Entity entity3 = mock(Entity.class);
    given(entity3.getId()).willReturn(3L);
    given(entity3.getBelongsToField("parent")).willReturn(entity1);
    Entity entity4 = mock(Entity.class);
    given(entity4.getId()).willReturn(4L);
    given(entity4.getBelongsToField("parent")).willReturn(entity2);
    List<Entity> entities = Arrays.asList(new Entity[] { entity1, entity2, entity3, entity4 });
    BelongsToType fieldType = mock(BelongsToType.class);
    InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
    given(fieldType.getDataDefinition()).willReturn(dataDefinition);
    FieldDefinition fieldDefinition = mock(FieldDefinition.class);
    given(fieldDefinition.getType()).willReturn(fieldType);
    given(fieldDefinition.getName()).willReturn("field");
    given(dataDefinition.getField("tree")).willReturn(fieldDefinition);
    given(dataDefinition.find().add(SearchRestrictions.belongsTo("field", dataDefinition, 1L)).addOrder(SearchOrders.asc("priority")).list().getEntities()).willReturn(entities);
    EntityTreeImpl tree = new EntityTreeImpl(dataDefinition, "tree", 1L);
    // when
    EntityTreeNodeImpl root = tree.getRoot();
    // then
    assertEquals(4, tree.size());
    assertEquals(Long.valueOf(1L), root.getId());
    assertEquals(Long.valueOf(2L), root.getChildren().get(0).getId());
    assertEquals(Long.valueOf(3L), root.getChildren().get(1).getId());
    assertEquals(Long.valueOf(4L), root.getChildren().get(0).getChildren().get(0).getId());
}
Also used : Entity(com.qcadoo.model.api.Entity) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Test(org.junit.Test)

Example 19 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.

the class EntityTreeImplTest method shouldFailIfThereAreMultipleRoots.

@Test(expected = IllegalStateException.class)
public void shouldFailIfThereAreMultipleRoots() throws Exception {
    // given
    Entity entity1 = mock(Entity.class);
    given(entity1.getId()).willReturn(1L);
    Entity entity2 = mock(Entity.class);
    given(entity2.getId()).willReturn(2L);
    List<Entity> entities = Arrays.asList(new Entity[] { entity1, entity2 });
    BelongsToType fieldType = mock(BelongsToType.class);
    InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
    given(fieldType.getDataDefinition()).willReturn(dataDefinition);
    FieldDefinition fieldDefinition = mock(FieldDefinition.class);
    given(fieldDefinition.getType()).willReturn(fieldType);
    given(fieldDefinition.getName()).willReturn("field");
    given(dataDefinition.getField("tree")).willReturn(fieldDefinition);
    given(dataDefinition.find().add(SearchRestrictions.belongsTo("field", dataDefinition, 1L)).addOrder(SearchOrders.asc("priority")).list().getEntities()).willReturn(entities);
    EntityTreeImpl tree = new EntityTreeImpl(dataDefinition, "tree", 1L);
    // when
    tree.size();
}
Also used : Entity(com.qcadoo.model.api.Entity) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Test(org.junit.Test)

Example 20 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.

the class EntityTreeImplTest method shouldFailIfThereIsNoRoot.

@Test(expected = IllegalStateException.class)
public void shouldFailIfThereIsNoRoot() throws Exception {
    // given
    Entity entity = mock(Entity.class);
    given(entity.getBelongsToField("parent")).willReturn(entity);
    List<Entity> entities = Collections.singletonList(entity);
    BelongsToType fieldType = mock(BelongsToType.class);
    InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
    given(fieldType.getDataDefinition()).willReturn(dataDefinition);
    FieldDefinition fieldDefinition = mock(FieldDefinition.class);
    given(fieldDefinition.getType()).willReturn(fieldType);
    given(fieldDefinition.getName()).willReturn("field");
    given(dataDefinition.getField("tree")).willReturn(fieldDefinition);
    given(dataDefinition.find().add(SearchRestrictions.belongsTo("field", dataDefinition, 1L)).addOrder(SearchOrders.asc("priority")).list().getEntities()).willReturn(entities);
    EntityTreeImpl tree = new EntityTreeImpl(dataDefinition, "tree", 1L);
    // when
    tree.size();
}
Also used : Entity(com.qcadoo.model.api.Entity) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Test(org.junit.Test)

Aggregations

InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)28 Entity (com.qcadoo.model.api.Entity)16 FieldDefinition (com.qcadoo.model.api.FieldDefinition)12 BelongsToType (com.qcadoo.model.api.types.BelongsToType)10 Test (org.junit.Test)8 Monitorable (com.qcadoo.model.api.aop.Monitorable)7 Transactional (org.springframework.transaction.annotation.Transactional)7 ArrayList (java.util.ArrayList)6 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)5 EntityList (com.qcadoo.model.api.EntityList)3 ManyToManyType (com.qcadoo.model.api.types.ManyToManyType)3 TreeType (com.qcadoo.model.api.types.TreeType)3 EntityOpResult (com.qcadoo.model.api.EntityOpResult)2 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)2 HasManyType (com.qcadoo.model.api.types.HasManyType)2 TranslationService (com.qcadoo.localization.api.TranslationService)1 CustomHook (com.qcadoo.model.CustomHook)1 CopyException (com.qcadoo.model.api.CopyException)1 DataDefinition (com.qcadoo.model.api.DataDefinition)1 Cascadeable (com.qcadoo.model.api.types.Cascadeable)1