Search in sources :

Example 1 with Sorting

use of com.yahoo.elide.core.request.Sorting in project elide by yahoo.

the class EntityProjectionMakerTest method testRelationshipsAndIncludeWithFilterAndSort.

@Test
public void testRelationshipsAndIncludeWithFilterAndSort() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.add("include", "authors");
    queryParams.add("filter[author]", "name=='Foo'");
    queryParams.add("filter[publisher]", "name=='Foo'");
    queryParams.add("sort", "name");
    String path = "/book/1/relationships/publisher";
    Sorting sorting = SortingImpl.parseSortRule("name", ClassType.of(Publisher.class), dictionary);
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Book.class).relationship("publisher", EntityProjection.builder().type(Publisher.class).filterExpression(new InPredicate(new Path(Publisher.class, dictionary, "name"), "Foo")).sorting(sorting).pagination(PaginationImpl.getDefaultPagination(ClassType.of(Publisher.class))).build()).relationship("authors", EntityProjection.builder().attribute(Attribute.builder().name("name").type(String.class).build()).attribute(Attribute.builder().name("type").type(Author.AuthorType.class).build()).attribute(Attribute.builder().name("homeAddress").type(Address.class).build()).attribute(Attribute.builder().name("vacationHomes").type(Set.class).build()).attribute(Attribute.builder().name("stuff").type(Map.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).filterExpression(new InPredicate(new Path(Author.class, dictionary, "name"), "Foo")).relationship("books", EntityProjection.builder().type(Book.class).build()).type(Author.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).build();
    EntityProjection actual = maker.parsePath(path);
    projectionEquals(expected, actual);
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Set(java.util.Set) Publisher(example.Publisher) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Sorting(com.yahoo.elide.core.request.Sorting) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Book(example.Book) Collection(java.util.Collection) Editor(example.Editor) Test(org.junit.jupiter.api.Test)

Example 2 with Sorting

use of com.yahoo.elide.core.request.Sorting in project elide by yahoo.

the class DefaultQueryValidator method validateSorting.

@Override
public void validateSorting(Query query) {
    Sorting sorting = query.getSorting();
    if (sorting == null) {
        return;
    }
    Map<Path, Sorting.SortOrder> sortClauses = sorting.getSortingPaths();
    Set<String> allFields = query.getColumnProjections().stream().map(ColumnProjection::getAlias).collect(Collectors.toCollection(LinkedHashSet::new));
    sortClauses.keySet().forEach((path) -> validateSortingPath(path, allFields));
}
Also used : Path(com.yahoo.elide.core.Path) Sorting(com.yahoo.elide.core.request.Sorting)

Example 3 with Sorting

use of com.yahoo.elide.core.request.Sorting in project elide by yahoo.

the class InMemoryStoreTransactionTest method testSortOnComplexAttribute.

@Test
public void testSortOnComplexAttribute() {
    Map<String, Sorting.SortOrder> sortOrder = new HashMap<>();
    sortOrder.put("homeAddress.street1", Sorting.SortOrder.asc);
    Sorting sorting = new SortingImpl(sortOrder, Author.class, dictionary);
    EntityProjection projection = EntityProjection.builder().type(Author.class).sorting(sorting).build();
    DataStoreIterable sortInMemory = new DataStoreIterableBuilder(Arrays.asList(author1, author2)).sortInMemory(true).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(sortInMemory);
    Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
    assertEquals(2, loaded.size());
    Object[] sorted = loaded.toArray();
    assertEquals(author2, sorted[0]);
    assertEquals(author1, sorted[1]);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HashMap(java.util.HashMap) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) Test(org.junit.jupiter.api.Test)

Example 4 with Sorting

use of com.yahoo.elide.core.request.Sorting in project elide by yahoo.

the class InMemoryStoreTransactionTest method testSortingRequiresInMemoryPagination.

@Test
public void testSortingRequiresInMemoryPagination() {
    PaginationImpl pagination = new PaginationImpl(ClassType.of(Book.class), 0, 3, 10, 10, true, false);
    Map<String, Sorting.SortOrder> sortOrder = new HashMap<>();
    sortOrder.put("title", Sorting.SortOrder.desc);
    Sorting sorting = new SortingImpl(sortOrder, Book.class, dictionary);
    EntityProjection projection = EntityProjection.builder().type(Book.class).sorting(sorting).pagination(pagination).build();
    DataStoreIterable sortInMemory = new DataStoreIterableBuilder(books).sortInMemory(true).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(sortInMemory);
    Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
    verify(wrappedTransaction, times(1)).loadObjects(any(EntityProjection.class), eq(scope));
    assertEquals(3, loaded.size());
    List<String> bookTitles = loaded.stream().map((o) -> ((Book) o).getTitle()).collect(Collectors.toList());
    assertEquals(Lists.newArrayList("Book 3", "Book 2", "Book 1"), bookTitles);
    assertEquals(3, pagination.getPageTotals());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Path(com.yahoo.elide.core.Path) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Publisher(example.Publisher) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) ClassType(com.yahoo.elide.core.type.ClassType) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Relationship(com.yahoo.elide.core.request.Relationship) RequestScope(com.yahoo.elide.core.RequestScope) ImmutableSet(com.google.common.collect.ImmutableSet) DefaultClassScanner(com.yahoo.elide.core.utils.DefaultClassScanner) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) HashMap(java.util.HashMap) Author(example.Author) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Editor(example.Editor) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) ElideSettings(com.yahoo.elide.ElideSettings) Price(example.Price) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Sorting(com.yahoo.elide.core.request.Sorting) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Mockito.times(org.mockito.Mockito.times) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Address(example.Address) Mockito.reset(org.mockito.Mockito.reset) EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HashMap(java.util.HashMap) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Book(example.Book) Test(org.junit.jupiter.api.Test)

Example 5 with Sorting

use of com.yahoo.elide.core.request.Sorting in project elide by yahoo.

the class RootCollectionPageTotalsQueryBuilderTest method testRootFetchWithSorting.

@Test
public void testRootFetchWithSorting() {
    Sorting sorting = mock(Sorting.class);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).sorting(sorting).build();
    TestQueryWrapper query = (TestQueryWrapper) new RootCollectionPageTotalsQueryBuilder(entityProjection, dictionary, new TestSessionWrapper()).build();
    String expected = "SELECT COUNT(DISTINCT example_Book) FROM example.Book AS example_Book";
    String actual = query.getQueryText();
    actual = actual.trim().replaceAll(" +", " ");
    assertEquals(expected, actual);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) RootCollectionPageTotalsQueryBuilder(com.yahoo.elide.datastores.jpql.query.RootCollectionPageTotalsQueryBuilder) Sorting(com.yahoo.elide.core.request.Sorting) Test(org.junit.jupiter.api.Test)

Aggregations

Sorting (com.yahoo.elide.core.request.Sorting)22 Test (org.junit.jupiter.api.Test)13 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)12 EntityProjection (com.yahoo.elide.core.request.EntityProjection)11 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)10 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)10 HashMap (java.util.HashMap)10 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)9 Path (com.yahoo.elide.core.Path)8 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)8 Collection (java.util.Collection)7 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)6 ClassType (com.yahoo.elide.core.type.ClassType)6 Map (java.util.Map)6 RequestScope (com.yahoo.elide.core.RequestScope)5 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)5 PaginationImpl (com.yahoo.elide.core.pagination.PaginationImpl)5 Pagination (com.yahoo.elide.core.request.Pagination)5 Book (example.Book)5 Editor (example.Editor)5