use of org.molgenis.api.model.Sort in project molgenis by molgenis.
the class DataServiceV3ImplTest method testFindAll.
@SuppressWarnings("unchecked")
@Test
void testFindAll() {
String entityTypeId = "MyEntityType";
Selection filter = Selection.FULL_SELECTION;
Selection expand = Selection.EMPTY_SELECTION;
EntityType entityType = mock(EntityType.class);
Repository<Entity> repository = mock(Repository.class);
when(repository.getEntityType()).thenReturn(entityType);
Entity entity1 = mock(Entity.class);
Entity entity2 = mock(Entity.class);
Sort sort = Sort.create("field", Direction.ASC);
Fetch fetch = new Fetch().field("id", new Fetch().field("refAttr"));
Query q = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
org.molgenis.data.Query<Entity> findAllQuery = mock(org.molgenis.data.Query.class);
org.molgenis.data.Sort dataSort = mock(org.molgenis.data.Sort.class);
org.molgenis.data.Query<Entity> findQuery = new QueryImpl<>(findAllQuery);
findQuery.fetch(fetch);
findQuery.offset(10);
findQuery.pageSize(10);
findQuery.sort(dataSort);
org.molgenis.data.Query<Entity> countQuery = new QueryImpl<>(findAllQuery);
countQuery.offset(0);
countQuery.pageSize(Integer.MAX_VALUE);
when(repository.findAll(findQuery)).thenReturn(Stream.of(entity1, entity2));
when(repository.count(countQuery)).thenReturn(100L);
when(queryMapper.map(q, repository)).thenReturn(findAllQuery).thenReturn(countQuery);
when(sortMapper.map(sort, entityType)).thenReturn(dataSort);
when(metaDataService.getRepository(entityTypeId)).thenReturn(Optional.of(repository));
Entities actual = dataServiceV3Impl.findAll(entityTypeId, q, filter, expand, sort, 10, 1);
assertEquals(actual, Entities.builder().setEntities(asList(entity1, entity2)).setTotal(100).build());
}
use of org.molgenis.api.model.Sort in project molgenis by molgenis.
the class DataServiceV3ImplTest method testFindFieldUnsupportedAttributeType.
@SuppressWarnings("unchecked")
@Test
void testFindFieldUnsupportedAttributeType() {
String entityTypeId = "MyEntityType";
String entityId = "MyEntity";
String fieldId = "MyField";
Selection filter = Selection.FULL_SELECTION;
Selection expand = Selection.EMPTY_SELECTION;
Attribute idAttribute = mock(Attribute.class, "id");
when(idAttribute.getDataType()).thenReturn(STRING);
Attribute mrefAttribute = mock(Attribute.class, "mref");
when(mrefAttribute.getDataType()).thenReturn(STRING);
EntityType entityType = mock(EntityType.class, "entityType");
when(entityType.getIdAttribute()).thenReturn(idAttribute);
when(entityType.getAttribute(fieldId)).thenReturn(mrefAttribute);
Repository<Entity> repository = mock(Repository.class);
when(repository.getEntityType()).thenReturn(entityType);
Fetch fetch = new Fetch().field("MyField", new Fetch().field("id"));
Sort sort = Sort.create("field", Direction.ASC);
Query q = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
org.molgenis.data.Query<Entity> findAllQuery = mock(org.molgenis.data.Query.class);
org.molgenis.data.Sort dataSort = mock(org.molgenis.data.Sort.class);
org.molgenis.data.Query<Entity> findQuery = new QueryImpl<>(findAllQuery);
findQuery.fetch(fetch);
findQuery.offset(10);
findQuery.pageSize(10);
findQuery.sort(dataSort);
org.molgenis.data.Query<Entity> countQuery = new QueryImpl<>(findAllQuery);
countQuery.offset(0);
countQuery.pageSize(Integer.MAX_VALUE);
doReturn(Optional.of(repository)).when(metaDataService).getRepository(entityTypeId);
assertThrows(UnsupportedAttributeTypeException.class, () -> dataServiceV3Impl.findSubresources(entityTypeId, entityId, fieldId, q, filter, expand, sort, 10, 1));
}
use of org.molgenis.api.model.Sort in project molgenis by molgenis.
the class EntityControllerTest method testGetEntities.
@Test
void testGetEntities() {
String entityTypeId = "MyEntityTypeId";
Selection filter = Selection.FULL_SELECTION;
Selection expand = Selection.FULL_SELECTION;
Query query = Query.builder().setOperator(Operator.MATCHES).setValue("value").build();
Sort sort = Sort.create("field", Direction.ASC);
ReadEntitiesRequest entityRequest = new ReadEntitiesRequest();
entityRequest.setEntityTypeId(entityTypeId);
entityRequest.setQ(query);
entityRequest.setSort(sort);
entityRequest.setFilter(filter);
entityRequest.setExpand(expand);
entityRequest.setSize(10);
entityRequest.setPage(2);
Entities entities = Entities.create(emptyList(), 30);
when(dataServiceV3.findAll(entityTypeId, query, filter, expand, sort, 10, 2)).thenReturn(entities);
EntityCollection entityCollection = EntityCollection.builder().setEntityTypeId(entityTypeId).setEntities(emptyList()).setPage(Page.builder().setOffset(20).setPageSize(10).setTotal(30).build()).build();
EntitiesResponse entitiesResponse = mock(EntitiesResponse.class);
when(entityMapper.map(entityCollection, filter, expand, 10, 2, 30)).thenReturn(entitiesResponse);
assertEquals(entitiesResponse, entityController.getEntities(entityRequest));
}
use of org.molgenis.api.model.Sort in project molgenis by molgenis.
the class SortMapperTest method testMapUnknownAttribute.
@Test
void testMapUnknownAttribute() {
SortMapper sortMapper = new SortMapper();
Sort sort = Sort.create(singletonList(Order.create("attr1")));
EntityType entityType = mock(EntityType.class);
assertThrows(UnknownSortAttributeException.class, () -> sortMapper.map(sort, entityType));
}
use of org.molgenis.api.model.Sort in project molgenis by molgenis.
the class SortMapperTest method testMapNoOrder.
@Test
void testMapNoOrder() {
Attribute attr1 = mock(Attribute.class);
EntityType entityType = mock(EntityType.class);
doReturn(attr1).when(entityType).getAttribute("attr1");
SortMapper sortMapper = new SortMapper();
Sort sort = Sort.create(singletonList(Order.create("attr1")));
org.molgenis.data.Sort expected = new org.molgenis.data.Sort().on("attr1", org.molgenis.data.Sort.Direction.ASC);
assertEquals(expected, sortMapper.map(sort, entityType));
}
Aggregations