Search in sources :

Example 31 with Query

use of org.molgenis.api.model.Query 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 32 with Query

use of org.molgenis.api.model.Query in project molgenis by molgenis.

the class QueryRsqlVisitorTest method testVisitComparisonNodeContains.

@Test
void testVisitComparisonNodeContains() {
    String selector = "name";
    String argument = "piet";
    ComparisonOperator operator = new ComparisonOperator("=like=");
    ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));
    Query query = Query.builder().setItem(selector).setOperator(CONTAINS).setValue(argument).build();
    assertEquals(query, queryRsqlVisitor.visit(node));
}
Also used : ComparisonOperator(cz.jirutka.rsql.parser.ast.ComparisonOperator) ComparisonNode(cz.jirutka.rsql.parser.ast.ComparisonNode) Query(org.molgenis.api.model.Query) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 33 with Query

use of org.molgenis.api.model.Query in project molgenis by molgenis.

the class QueryRsqlVisitorTest method testVisitComparisonNodeNotEquals.

@Test
void testVisitComparisonNodeNotEquals() {
    String selector = "name";
    String argument = "piet";
    ComparisonOperator operator = new ComparisonOperator("!=");
    ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));
    Query query = Query.builder().setItem(selector).setOperator(NOT_EQUALS).setValue(argument).build();
    assertEquals(query, queryRsqlVisitor.visit(node));
}
Also used : ComparisonOperator(cz.jirutka.rsql.parser.ast.ComparisonOperator) ComparisonNode(cz.jirutka.rsql.parser.ast.ComparisonNode) Query(org.molgenis.api.model.Query) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 34 with Query

use of org.molgenis.api.model.Query in project molgenis by molgenis.

the class QueryRsqlVisitorTest method testVisitComparisonNodeNotIn.

@Test
void testVisitComparisonNodeNotIn() {
    String selector = "name";
    List<String> arguments = asList("jan", "piet");
    ComparisonOperator operator = new ComparisonOperator("=out=", true);
    ComparisonNode node = new ComparisonNode(operator, selector, arguments);
    Query query = Query.builder().setItem(selector).setOperator(NOT_IN).setValue(arguments).build();
    assertEquals(query, queryRsqlVisitor.visit(node));
}
Also used : ComparisonOperator(cz.jirutka.rsql.parser.ast.ComparisonOperator) ComparisonNode(cz.jirutka.rsql.parser.ast.ComparisonNode) Query(org.molgenis.api.model.Query) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 35 with Query

use of org.molgenis.api.model.Query in project molgenis by molgenis.

the class QueryRsqlVisitorTest method testVisitComparisonNodeMatches.

@Test
void testVisitComparisonNodeMatches() {
    String selector = "name";
    String argument = "piet";
    ComparisonOperator operator = new ComparisonOperator("=q=");
    ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));
    Query query = Query.builder().setItem(selector).setOperator(MATCHES).setValue(argument).build();
    assertEquals(query, queryRsqlVisitor.visit(node));
}
Also used : ComparisonOperator(cz.jirutka.rsql.parser.ast.ComparisonOperator) ComparisonNode(cz.jirutka.rsql.parser.ast.ComparisonNode) Query(org.molgenis.api.model.Query) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Query (org.molgenis.api.model.Query)58 Test (org.junit.jupiter.api.Test)52 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)52 Entity (org.molgenis.data.Entity)27 Attribute (org.molgenis.data.meta.model.Attribute)22 QueryImpl (org.molgenis.data.support.QueryImpl)22 ComparisonNode (cz.jirutka.rsql.parser.ast.ComparisonNode)17 ComparisonOperator (cz.jirutka.rsql.parser.ast.ComparisonOperator)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)16 EntityType (org.molgenis.data.meta.model.EntityType)15 Sort (org.molgenis.api.model.Sort)14 Selection (org.molgenis.api.model.Selection)9 Fetch (org.molgenis.data.Fetch)7 AttributeMetadata (org.molgenis.data.meta.model.AttributeMetadata)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 EntitiesResponse (org.molgenis.api.data.v3.model.EntitiesResponse)2 QueryRule (org.molgenis.data.QueryRule)2 AndNode (cz.jirutka.rsql.parser.ast.AndNode)1 OrNode (cz.jirutka.rsql.parser.ast.OrNode)1 Stream (java.util.stream.Stream)1