Search in sources :

Example 11 with Sort

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));
}
Also used : EntityTypesResponse(org.molgenis.api.metadata.v3.model.EntityTypesResponse) Query(org.molgenis.api.model.Query) Sort(org.molgenis.api.model.Sort) ReadEntityTypesRequest(org.molgenis.api.metadata.v3.model.ReadEntityTypesRequest) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 12 with Sort

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()));
}
Also used : Query(org.molgenis.api.model.Query) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) Sort(org.molgenis.api.model.Sort) QueryRule(org.molgenis.data.QueryRule) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 13 with Sort

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()));
}
Also used : Query(org.molgenis.api.model.Query) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) EntityType(org.molgenis.data.meta.model.EntityType) Sort(org.molgenis.api.model.Sort) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 14 with Sort

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()));
}
Also used : Query(org.molgenis.api.model.Query) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) Sort(org.molgenis.api.model.Sort) QueryRule(org.molgenis.data.QueryRule) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 15 with Sort

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);
}
Also used : Sort(org.molgenis.api.model.Sort) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Sort (org.molgenis.api.model.Sort)22 Test (org.junit.jupiter.api.Test)17 Query (org.molgenis.api.model.Query)14 EntityType (org.molgenis.data.meta.model.EntityType)14 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)14 Selection (org.molgenis.api.model.Selection)11 Attribute (org.molgenis.data.meta.model.Attribute)11 QueryImpl (org.molgenis.data.support.QueryImpl)8 Entity (org.molgenis.data.Entity)7 Fetch (org.molgenis.data.Fetch)7 Transactional (org.springframework.transaction.annotation.Transactional)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 AttributeMetadata (org.molgenis.data.meta.model.AttributeMetadata)3 EntitiesResponse (org.molgenis.api.data.v3.model.EntitiesResponse)2 QueryRule (org.molgenis.data.QueryRule)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Maps (com.google.common.collect.Maps)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1