Search in sources :

Example 11 with EntityProjection

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

the class EntityProjectionTranslator method resolveNonTimeDimensions.

/**
 * Gets dimensions except time dimensions based on relationships and attributes from {@link EntityProjection}.
 */
private Set<DimensionProjection> resolveNonTimeDimensions() {
    Set<DimensionProjection> attributes = entityProjection.getAttributes().stream().filter(attribute -> queriedTable.getTimeDimension(attribute.getName()) == null).map(dimAttr -> {
        Dimension dimension = queriedTable.getDimension(dimAttr.getName());
        return dimension == null ? null : engine.constructDimensionProjection(dimension, dimAttr.getAlias(), getArgumentMapFromArgumentSet(dimAttr.getArguments()));
    }).filter(Objects::nonNull).collect(Collectors.toSet());
    Set<DimensionProjection> relationships = entityProjection.getRelationships().stream().map(dimAttr -> {
        Dimension dimension = queriedTable.getDimension(dimAttr.getName());
        return dimension == null ? null : engine.constructDimensionProjection(dimension, dimAttr.getAlias(), Collections.emptyMap());
    }).filter(Objects::nonNull).collect(Collectors.toSet());
    return Sets.union(attributes, relationships);
}
Also used : PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) DimensionProjection(com.yahoo.elide.datastores.aggregation.query.DimensionProjection) Set(java.util.Set) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Dimension(com.yahoo.elide.datastores.aggregation.metadata.models.Dimension) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) FilterConstraints(com.yahoo.elide.datastores.aggregation.filter.visitor.FilterConstraints) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) ImmutablePagination(com.yahoo.elide.datastores.aggregation.query.ImmutablePagination) Argument.getArgumentMapFromArgumentSet(com.yahoo.elide.core.request.Argument.getArgumentMapFromArgumentSet) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) TimeDimensionProjection(com.yahoo.elide.datastores.aggregation.query.TimeDimensionProjection) MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) RequestScope(com.yahoo.elide.core.RequestScope) SplitFilterExpressionVisitor(com.yahoo.elide.datastores.aggregation.filter.visitor.SplitFilterExpressionVisitor) DimensionProjection(com.yahoo.elide.datastores.aggregation.query.DimensionProjection) TimeDimensionProjection(com.yahoo.elide.datastores.aggregation.query.TimeDimensionProjection) Dimension(com.yahoo.elide.datastores.aggregation.metadata.models.Dimension) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension)

Example 12 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection 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 13 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection 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 14 with EntityProjection

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

the class InMemoryStoreTransactionTest method testFilteringRequiresInMemoryPagination.

@Test
public void testFilteringRequiresInMemoryPagination() {
    FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction");
    PaginationImpl pagination = new PaginationImpl(ClassType.of(Book.class), 0, 2, 10, 10, true, false);
    EntityProjection projection = EntityProjection.builder().type(Book.class).filterExpression(expression).pagination(pagination).build();
    DataStoreIterable filterInMemory = new DataStoreIterableBuilder(books).filterInMemory(true).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(filterInMemory);
    Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
    verify(wrappedTransaction, times(1)).loadObjects(any(EntityProjection.class), eq(scope));
    assertEquals(2, loaded.size());
    assertTrue(loaded.contains(book1));
    assertTrue(loaded.contains(book3));
    assertEquals(2, pagination.getPageTotals());
}
Also used : Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Test(org.junit.jupiter.api.Test)

Example 15 with EntityProjection

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

the class PersistentResourceTest method testLoadRecordForbidden.

@Test
public void testLoadRecordForbidden() {
    NoReadEntity noRead = new NoReadEntity();
    noRead.setId(1);
    EntityProjection collection = EntityProjection.builder().type(NoReadEntity.class).build();
    when(tx.loadObject(eq(collection), eq(1L), any())).thenReturn(noRead);
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    goodScope.setEntityProjection(collection);
    assertThrows(ForbiddenAccessException.class, () -> PersistentResource.loadRecord(EntityProjection.builder().type(NoReadEntity.class).build(), "1", goodScope));
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) NoReadEntity(example.NoReadEntity) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Aggregations

EntityProjection (com.yahoo.elide.core.request.EntityProjection)108 Test (org.junit.jupiter.api.Test)90 Book (example.Book)41 RequestScope (com.yahoo.elide.core.RequestScope)27 Author (example.Author)27 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)22 Publisher (example.Publisher)22 Relationship (com.yahoo.elide.core.request.Relationship)19 Path (com.yahoo.elide.core.Path)18 TestRequestScope (com.yahoo.elide.core.TestRequestScope)18 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)18 HashMap (java.util.HashMap)18 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)16 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)15 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)15 Collection (java.util.Collection)15 LinkedHashSet (java.util.LinkedHashSet)14 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)13 Editor (example.Editor)13