Search in sources :

Example 1 with Sort

use of org.jnosql.diana.api.Sort in project jnosql-artemis by eclipse.

the class ColumnQueryParserTest method shouldFindByNameWithPageSortArgument.

@Test
public void shouldFindByNameWithPageSortArgument() {
    Pagination pagination = Pagination.of(2L, 10);
    Sort sort = Sort.of("age", Sort.SortType.ASC);
    ColumnQuery query = parser.parse("findByName", new Object[] { "name", pagination, sort }, classRepresentation, converters);
    assertEquals("Person", query.getColumnFamily());
    assertEquals(Condition.EQUALS, query.getCondition().get().getCondition());
    assertEquals(Column.of("name", "name"), query.getCondition().get().getColumn());
    assertEquals(pagination.getMaxResults(), query.getMaxResults());
    assertEquals(pagination.getFirstResult(), query.getFirstResult());
    assertEquals(sort, query.getSorts().get(0));
}
Also used : Pagination(org.jnosql.artemis.Pagination) ColumnQuery(org.jnosql.diana.api.column.ColumnQuery) Sort(org.jnosql.diana.api.Sort) Test(org.junit.jupiter.api.Test)

Example 2 with Sort

use of org.jnosql.diana.api.Sort in project jnosql-artemis by eclipse.

the class ColumnRepositoryAsyncProxyTest method shoudFindByNameSortPagination.

@Test
public void shoudFindByNameSortPagination() {
    Consumer<List<Person>> callback = v -> {
    };
    Sort sort = Sort.of("age", Sort.SortType.ASC);
    Pagination pagination = Pagination.of(10, 20);
    ArgumentCaptor<ColumnQuery> captor = ArgumentCaptor.forClass(ColumnQuery.class);
    ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class);
    personRepository.findByName("name", sort, pagination, callback);
    verify(template).select(captor.capture(), consumerCaptor.capture());
    ColumnQuery query = captor.getValue();
    ColumnCondition condition = query.getCondition().get();
    assertEquals("Person", query.getColumnFamily());
    assertEquals(Condition.EQUALS, condition.getCondition());
    assertEquals(Column.of("name", "name"), condition.getColumn());
    assertEquals(callback, consumerCaptor.getValue());
    assertEquals(sort, query.getSorts().get(0));
    assertEquals(pagination.getFirstResult(), query.getFirstResult());
    assertEquals(pagination.getMaxResults(), query.getMaxResults());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ColumnCondition(org.jnosql.diana.api.column.ColumnCondition) Converters(org.jnosql.artemis.Converters) Proxy(java.lang.reflect.Proxy) RepositoryAsync(org.jnosql.artemis.RepositoryAsync) Pagination(org.jnosql.artemis.Pagination) ColumnDeleteQuery(org.jnosql.diana.api.column.ColumnDeleteQuery) DynamicQueryException(org.jnosql.artemis.DynamicQueryException) ColumnQueryBuilder.delete(org.jnosql.diana.api.column.query.ColumnQueryBuilder.delete) Collections.singletonList(java.util.Collections.singletonList) Person(org.jnosql.artemis.model.Person) Reflections(org.jnosql.artemis.reflection.Reflections) Condition(org.jnosql.diana.api.Condition) Inject(javax.inject.Inject) CDIExtension(org.jnosql.artemis.CDIExtension) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Matchers.eq(org.mockito.Matchers.eq) Duration(java.time.Duration) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ColumnQuery(org.jnosql.diana.api.column.ColumnQuery) Sort(org.jnosql.diana.api.Sort) ColumnTemplateAsync(org.jnosql.artemis.column.ColumnTemplateAsync) ClassRepresentations(org.jnosql.artemis.reflection.ClassRepresentations) Column(org.jnosql.diana.api.column.Column) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) List(java.util.List) ColumnQueryBuilder.select(org.jnosql.diana.api.column.query.ColumnQueryBuilder.select) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) Pagination(org.jnosql.artemis.Pagination) ColumnQuery(org.jnosql.diana.api.column.ColumnQuery) Consumer(java.util.function.Consumer) Sort(org.jnosql.diana.api.Sort) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ColumnCondition(org.jnosql.diana.api.column.ColumnCondition) Test(org.junit.jupiter.api.Test)

Example 3 with Sort

use of org.jnosql.diana.api.Sort in project jnosql-artemis by eclipse.

the class DocumentQueryParser method parse.

public DocumentQuery parse(String methodName, Object[] args, ClassRepresentation representation, Converters converters) {
    String[] tokens = methodName.replace(PREFIX, EMPTY).split(TOKENIZER);
    String className = representation.getClassInstance().getName();
    DocumentCondition condition = null;
    List<Sort> sorts = new ArrayList<>();
    long limit = 0;
    long start = 0;
    int index = 0;
    for (String token : tokens) {
        if (token.startsWith(AND)) {
            DocumentQueryParserUtil.ConditionResult result = and(args, index, token, methodName, representation, condition, converters);
            condition = result.getCondition();
            index = result.getIndex();
        } else {
            if (token.startsWith(ORDER_BY)) {
                sort(sorts, token, representation);
            } else if (token.startsWith(DocumentQueryParserUtil.OR)) {
                DocumentQueryParserUtil.ConditionResult result = or(args, index, token, methodName, representation, condition, converters);
                condition = result.getCondition();
                index = result.getIndex();
            } else {
                condition = toCondition(token, index, args, methodName, representation, converters);
                index++;
            }
        }
    }
    while (index < args.length) {
        Object value = args[index];
        if (Sort.class.isInstance(value)) {
            sorts.add(Sort.class.cast(value));
        } else if (Pagination.class.isInstance(value)) {
            Pagination pagination = Pagination.class.cast(value);
            limit = pagination.getMaxResults();
            start = pagination.getFirstResult();
        } else {
            LOGGER.info(String.format("Ignoring parameter %s on  methodName %s class name %s arg-number: %d", String.valueOf(value), methodName, className, index));
        }
        index++;
    }
    return new ArtemisDocumentQuery(sorts, limit, start, condition, representation.getName());
}
Also used : ArrayList(java.util.ArrayList) Pagination(org.jnosql.artemis.Pagination) Sort(org.jnosql.diana.api.Sort) DocumentCondition(org.jnosql.diana.api.document.DocumentCondition)

Example 4 with Sort

use of org.jnosql.diana.api.Sort in project jnosql-artemis by eclipse.

the class DocumentQueryParserTest method shouldFindByNameWithPageSortArgument.

@Test
public void shouldFindByNameWithPageSortArgument() {
    Pagination pagination = Pagination.of(2L, 10);
    Sort sort = Sort.of("age", Sort.SortType.ASC);
    DocumentQuery query = parser.parse("findByName", new Object[] { "name", pagination, sort }, classRepresentation, converters);
    assertEquals("Person", query.getDocumentCollection());
    assertEquals(Condition.EQUALS, query.getCondition().get().getCondition());
    assertEquals(Document.of("name", "name"), query.getCondition().get().getDocument());
    assertEquals(pagination.getMaxResults(), query.getMaxResults());
    assertEquals(pagination.getFirstResult(), query.getFirstResult());
    assertEquals(sort, query.getSorts().get(0));
}
Also used : Pagination(org.jnosql.artemis.Pagination) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Sort(org.jnosql.diana.api.Sort) Test(org.junit.jupiter.api.Test)

Example 5 with Sort

use of org.jnosql.diana.api.Sort in project jnosql-artemis by eclipse.

the class DocumentQueryParserTest method shouldFindByNameWithPageSortArgumentAndIgnore.

@Test
public void shouldFindByNameWithPageSortArgumentAndIgnore() {
    Pagination pagination = Pagination.of(2L, 10);
    Sort sort = Sort.of("age", Sort.SortType.ASC);
    DocumentQuery query = parser.parse("findByName", new Object[] { "name", pagination, sort, "ignore" }, classRepresentation, converters);
    assertEquals("Person", query.getDocumentCollection());
    assertEquals(Condition.EQUALS, query.getCondition().get().getCondition());
    assertEquals(Document.of("name", "name"), query.getCondition().get().getDocument());
    assertEquals(pagination.getMaxResults(), query.getMaxResults());
    assertEquals(pagination.getFirstResult(), query.getFirstResult());
    assertEquals(sort, query.getSorts().get(0));
}
Also used : Pagination(org.jnosql.artemis.Pagination) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Sort(org.jnosql.diana.api.Sort) Test(org.junit.jupiter.api.Test)

Aggregations

Sort (org.jnosql.diana.api.Sort)14 Pagination (org.jnosql.artemis.Pagination)10 Test (org.junit.jupiter.api.Test)10 ColumnQuery (org.jnosql.diana.api.column.ColumnQuery)5 DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)5 Proxy (java.lang.reflect.Proxy)4 Duration (java.time.Duration)4 Collections.singletonList (java.util.Collections.singletonList)4 List (java.util.List)4 Optional (java.util.Optional)4 Consumer (java.util.function.Consumer)4 Inject (javax.inject.Inject)4 CDIExtension (org.jnosql.artemis.CDIExtension)4 Converters (org.jnosql.artemis.Converters)4 DynamicQueryException (org.jnosql.artemis.DynamicQueryException)4 RepositoryAsync (org.jnosql.artemis.RepositoryAsync)4 Person (org.jnosql.artemis.model.Person)4 ClassRepresentations (org.jnosql.artemis.reflection.ClassRepresentations)4 Reflections (org.jnosql.artemis.reflection.Reflections)4 Condition (org.jnosql.diana.api.Condition)4