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);
}
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));
}
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]);
}
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());
}
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);
}
Aggregations