use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.
the class EntityListImplTest method shouldLoadEntities.
@Test
public void shouldLoadEntities() throws Exception {
// given
Entity entity = mock(Entity.class);
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.getName()).willReturn("field");
given(fieldDefinition.getType()).willReturn(fieldType);
given(dataDefinition.getField("hasMany")).willReturn(fieldDefinition);
given(dataDefinition.find().createAlias(fieldDefinition.getName(), fieldDefinition.getName()).add(SearchRestrictions.eq(fieldDefinition.getName() + ".id", 1L)).list().getEntities()).willReturn(entities);
given(dataAccessService.get(dataDefinition, 1L)).willReturn(entity);
EntityListImpl list = new EntityListImpl(dataDefinition, "hasMany", 1L);
// then
assertEquals(1, list.size());
assertEquals(entity, list.get(0));
}
use of com.qcadoo.model.api.types.BelongsToType 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.api.types.BelongsToType 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.api.types.BelongsToType 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.api.types.BelongsToType 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();
}
Aggregations