use of org.molgenis.api.model.Sort in project molgenis by molgenis.
the class MetadataApiControllerTest method testGetEntityTypes.
@Test
void testGetEntityTypes() {
int page = 2;
int size = 1;
Sort sort = mock(Sort.class);
Query query = mock(Query.class);
ReadEntityTypesRequest readEntityTypesRequest = new ReadEntityTypesRequest();
readEntityTypesRequest.setPage(page);
readEntityTypesRequest.setSize(size);
readEntityTypesRequest.setSort(sort);
readEntityTypesRequest.setQ(query);
EntityTypes entityTypes = mock(EntityTypes.class);
when(metadataApiService.findEntityTypes(query, sort, size, page)).thenReturn(entityTypes);
EntityTypesResponse entityTypesResponse = mock(EntityTypesResponse.class);
when(entityTypeResponseMapper.toEntityTypesResponse(entityTypes, size, page)).thenReturn(entityTypesResponse);
assertEquals(entityTypesResponse, metadataApiController.getEntityTypes(readEntityTypesRequest));
}
use of org.molgenis.api.model.Sort in project molgenis by molgenis.
the class MetadataApiServiceImplTest method testFindAttributesNestedQuery.
@SuppressWarnings("unchecked")
@Test
void testFindAttributesNestedQuery() {
Repository<Attribute> attributeRepository = mock(Repository.class);
EntityType entityType = mock(EntityType.class);
when(attributeRepository.getEntityType()).thenReturn(entityType);
when(metadataService.getRepository("sys_md_Attribute", Attribute.class)).thenReturn(Optional.of(attributeRepository));
Query query = mock(Query.class);
org.molgenis.data.Query<Attribute> repositoryQuery = new QueryImpl<>(asList(new QueryRule("id", QueryRule.Operator.EQUALS, "id0"), new QueryRule(QueryRule.Operator.OR), new QueryRule("id", QueryRule.Operator.EQUALS, "id1")));
when(queryMapper.map(query, attributeRepository)).thenReturn(repositoryQuery);
Attribute attribute = mock(Attribute.class);
ArgumentCaptor<org.molgenis.data.Query<Attribute>> findQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class);
when(attributeRepository.findAll(findQueryCaptor.capture())).thenReturn(Stream.of(attribute));
long total = 100L;
ArgumentCaptor<org.molgenis.data.Query<Attribute>> countQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class);
when(attributeRepository.count(countQueryCaptor.capture())).thenReturn(total);
Sort sort = Sort.EMPTY_SORT;
org.molgenis.data.Sort repositorySort = mock(org.molgenis.data.Sort.class);
when(sortMapper.map(sort, entityType)).thenReturn(repositorySort);
int size = 5;
int number = 2;
Attributes expectedAttributes = Attributes.builder().setAttributes(ImmutableList.of(attribute)).setTotal((int) total).build();
String entityTypeId = "MyEntityTypeId";
when(metadataService.hasEntityType(entityTypeId)).thenReturn(true);
Attributes attributes = metadataApiService.findAttributes(entityTypeId, query, sort, size, number);
org.molgenis.data.Query<Attribute> findQuery = findQueryCaptor.getValue();
org.molgenis.data.Query<Attribute> countQuery = countQueryCaptor.getValue();
assertAll(() -> assertEquals(expectedAttributes, attributes), () -> assertEquals(asList(new QueryRule(asList(new QueryRule("id", QueryRule.Operator.EQUALS, "id0"), new QueryRule(QueryRule.Operator.OR), new QueryRule("id", QueryRule.Operator.EQUALS, "id1"))), new QueryRule(QueryRule.Operator.AND), new QueryRule("entity", QueryRule.Operator.EQUALS, entityTypeId)), findQuery.getRules()), () -> assertEquals(size * number, findQuery.getOffset()), () -> assertEquals(size, findQuery.getPageSize()), () -> assertEquals(repositorySort, findQuery.getSort()), () -> assertEquals(asList(new QueryRule(asList(new QueryRule("id", QueryRule.Operator.EQUALS, "id0"), new QueryRule(QueryRule.Operator.OR), new QueryRule("id", QueryRule.Operator.EQUALS, "id1"))), new QueryRule(QueryRule.Operator.AND), new QueryRule("entity", QueryRule.Operator.EQUALS, entityTypeId)), countQuery.getRules()), () -> assertEquals(0, countQuery.getOffset()), () -> assertEquals(Integer.MAX_VALUE, countQuery.getPageSize()));
}
use of org.molgenis.api.model.Sort in project molgenis by molgenis.
the class MetadataApiServiceImplTest method testFindEntityTypes.
@Test
void testFindEntityTypes() {
EntityType entityType = mock(EntityType.class);
@SuppressWarnings("unchecked") Repository<EntityType> entityTypeRepository = mock(Repository.class);
when(entityTypeRepository.getEntityType()).thenReturn(entityType);
when(metadataService.getRepository("sys_md_EntityType", EntityType.class)).thenReturn(Optional.of(entityTypeRepository));
Query query = Query.builder().setItem("id").setOperator(Operator.EQUALS).setValue("id0").build();
when(queryMapper.map(query, entityTypeRepository)).thenReturn(new QueryImpl<>());
@SuppressWarnings("unchecked") ArgumentCaptor<org.molgenis.data.Query<EntityType>> findQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class);
when(entityTypeRepository.findAll(findQueryCaptor.capture())).thenReturn(Stream.of(entityType));
long total = 100L;
@SuppressWarnings("unchecked") ArgumentCaptor<org.molgenis.data.Query<EntityType>> countQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class);
when(entityTypeRepository.count(countQueryCaptor.capture())).thenReturn(total);
Sort sort = Sort.EMPTY_SORT;
org.molgenis.data.Sort repositorySort = mock(org.molgenis.data.Sort.class);
when(sortMapper.map(sort, entityType)).thenReturn(repositorySort);
int size = 5;
int number = 2;
EntityTypes expectedEntityTypes = EntityTypes.builder().setEntityTypes(ImmutableList.of(entityType)).setTotal((int) total).build();
EntityTypes entityTypes = metadataApiService.findEntityTypes(query, sort, size, number);
org.molgenis.data.Query<EntityType> findQuery = findQueryCaptor.getValue();
org.molgenis.data.Query<EntityType> countQuery = countQueryCaptor.getValue();
assertAll(() -> assertEquals(expectedEntityTypes, entityTypes), () -> assertEquals(size * number, findQuery.getOffset()), () -> assertEquals(size, findQuery.getPageSize()), () -> assertEquals(repositorySort, findQuery.getSort()), () -> assertEquals(0, countQuery.getOffset()), () -> assertEquals(Integer.MAX_VALUE, countQuery.getPageSize()));
}
use of org.molgenis.api.model.Sort in project molgenis by molgenis.
the class MetadataApiServiceImplTest method testFindAttributes.
@SuppressWarnings("unchecked")
@Test
void testFindAttributes() {
Repository<Attribute> attributeRepository = mock(Repository.class);
EntityType entityType = mock(EntityType.class);
when(attributeRepository.getEntityType()).thenReturn(entityType);
when(metadataService.getRepository("sys_md_Attribute", Attribute.class)).thenReturn(Optional.of(attributeRepository));
Query query = mock(Query.class);
when(queryMapper.map(query, attributeRepository)).thenReturn(new QueryImpl<>());
Attribute attribute = mock(Attribute.class);
ArgumentCaptor<org.molgenis.data.Query<Attribute>> findQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class);
when(attributeRepository.findAll(findQueryCaptor.capture())).thenReturn(Stream.of(attribute));
long total = 100L;
ArgumentCaptor<org.molgenis.data.Query<Attribute>> countQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class);
when(attributeRepository.count(countQueryCaptor.capture())).thenReturn(total);
Sort sort = Sort.EMPTY_SORT;
org.molgenis.data.Sort repositorySort = mock(org.molgenis.data.Sort.class);
when(sortMapper.map(sort, entityType)).thenReturn(repositorySort);
int size = 5;
int number = 2;
Attributes expectedAttributes = Attributes.builder().setAttributes(ImmutableList.of(attribute)).setTotal((int) total).build();
String entityTypeId = "MyEntityTypeId";
when(metadataService.hasEntityType(entityTypeId)).thenReturn(true);
Attributes attributes = metadataApiService.findAttributes(entityTypeId, query, sort, size, number);
org.molgenis.data.Query<Attribute> findQuery = findQueryCaptor.getValue();
org.molgenis.data.Query<Attribute> countQuery = countQueryCaptor.getValue();
assertAll(() -> assertEquals(expectedAttributes, attributes), () -> assertEquals(singletonList(new QueryRule("entity", QueryRule.Operator.EQUALS, entityTypeId)), findQuery.getRules()), () -> assertEquals(size * number, findQuery.getOffset()), () -> assertEquals(size, findQuery.getPageSize()), () -> assertEquals(repositorySort, findQuery.getSort()), () -> assertEquals(singletonList(new QueryRule("entity", QueryRule.Operator.EQUALS, entityTypeId)), countQuery.getRules()), () -> assertEquals(0, countQuery.getOffset()), () -> assertEquals(Integer.MAX_VALUE, countQuery.getPageSize()));
}
use of org.molgenis.api.model.Sort in project molgenis by molgenis.
the class MetadataApiController method getAttributes.
@Transactional(readOnly = true)
@GetMapping("/{entityTypeId}/attributes")
public AttributesResponse getAttributes(@PathVariable("entityTypeId") String entityTypeId, @Valid ReadAttributesRequest readAttributesRequest) {
int size = readAttributesRequest.getSize();
int page = readAttributesRequest.getPage();
Sort sort = readAttributesRequest.getSort();
Attributes attributes = metadataApiService.findAttributes(entityTypeId, readAttributesRequest.getQ().orElse(null), sort, size, page);
return attributeResponseMapper.toAttributesResponse(attributes, size, page);
}
Aggregations