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