Search in sources :

Example 6 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class CanPaginateVisitorTest method testFalseUserOROperationExpression.

@Test
public void testFalseUserOROperationExpression() throws Exception {
    @Entity
    @Include(rootLevel = false)
    class Book {

        @Id
        private long id;

        @ReadPermission(expression = "False User Check OR In Memory Check")
        private String title;
    }
    EntityDictionary dictionary = TestDictionary.getTestDictionary(checkMappings);
    dictionary.bindEntity(Book.class);
    RequestScope scope = mock(RequestScope.class);
    assertFalse(CanPaginateVisitor.canPaginate(ClassType.of(Book.class), dictionary, scope, new HashSet<>()));
}
Also used : Entity(javax.persistence.Entity) Include(com.yahoo.elide.annotation.Include) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RequestScope(com.yahoo.elide.core.RequestScope) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class PermissionToFilterExpressionVisitorTest method filterExpressionForPermissions.

private FilterExpression filterExpressionForPermissions(String permission) {
    Function<Check, Expression> checkFn = (check) -> new CheckExpression(check, null, requestScope, null, cache);
    ParseTree expression = EntityPermissions.parseExpression(permission);
    PermissionToFilterExpressionVisitor fev = new PermissionToFilterExpressionVisitor(dictionary, requestScope, null);
    return expression.accept(new PermissionExpressionVisitor(dictionary, checkFn)).accept(NORMALIZATION_VISITOR).accept(fev);
}
Also used : CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) BeforeEach(org.junit.jupiter.api.BeforeEach) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Arrays(java.util.Arrays) Path(com.yahoo.elide.core.Path) Role(com.yahoo.elide.core.security.checks.prefab.Role) User(com.yahoo.elide.core.security.User) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) Map(java.util.Map) PermissionExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionExpressionVisitor) NO_EVALUATION_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.NO_EVALUATION_EXPRESSION) RequestScope(com.yahoo.elide.core.RequestScope) OperationCheck(com.yahoo.elide.core.security.checks.OperationCheck) MethodSource(org.junit.jupiter.params.provider.MethodSource) PermissionToFilterExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor) UUID(java.util.UUID) Arguments(org.junit.jupiter.params.provider.Arguments) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) ExpressionResultCache(com.yahoo.elide.core.security.permissions.ExpressionResultCache) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Optional(java.util.Optional) TRUE_USER_CHECK_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.TRUE_USER_CHECK_EXPRESSION) PermissionExpressionNormalizationVisitor(com.yahoo.elide.core.security.visitors.PermissionExpressionNormalizationVisitor) TestDictionary(com.yahoo.elide.core.dictionary.TestDictionary) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) HashMap(java.util.HashMap) Author(example.Author) Function(java.util.function.Function) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) FilterExpressionCheck(com.yahoo.elide.core.security.checks.FilterExpressionCheck) ElideSettings(com.yahoo.elide.ElideSettings) TestUser(com.yahoo.elide.core.security.TestUser) ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) Check(com.yahoo.elide.core.security.checks.Check) Book(example.Book) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EntityPermissions(com.yahoo.elide.core.dictionary.EntityPermissions) Type(com.yahoo.elide.core.type.Type) Operator(com.yahoo.elide.core.filter.Operator) Collections(java.util.Collections) FALSE_USER_CHECK_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.FALSE_USER_CHECK_EXPRESSION) CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) OperationCheck(com.yahoo.elide.core.security.checks.OperationCheck) FilterExpressionCheck(com.yahoo.elide.core.security.checks.FilterExpressionCheck) Check(com.yahoo.elide.core.security.checks.Check) PermissionToFilterExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor) PermissionExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionExpressionVisitor) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression)

Example 8 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class AggregationDataStoreTransaction method loadObjects.

@Override
public <T> DataStoreIterable<T> loadObjects(EntityProjection entityProjection, RequestScope scope) {
    QueryResult result = null;
    QueryResponse response = null;
    String cacheKey = null;
    try {
        // Convert multivalued map to map.
        Map<String, String> headers = scope.getRequestHeaders().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, (entry) -> entry.getValue().stream().collect(Collectors.joining(" "))));
        queryLogger.acceptQuery(scope.getRequestId(), scope.getUser(), headers, scope.getApiVersion(), scope.getQueryParams(), scope.getPath());
        Query query = buildQuery(entityProjection, scope);
        Table table = (Table) query.getSource();
        if (cache != null && !query.isBypassingCache()) {
            String tableVersion = queryEngine.getTableVersion(table, queryEngineTransaction);
            tableVersion = tableVersion == null ? "" : tableVersion;
            cacheKey = tableVersion + ';' + QueryKeyExtractor.extractKey(query);
            result = cache.get(cacheKey);
        }
        boolean isCached = result != null;
        List<String> queryText = queryEngine.explain(query);
        queryLogger.processQuery(scope.getRequestId(), query, queryText, isCached);
        if (result == null) {
            result = queryEngine.executeQuery(query, queryEngineTransaction);
            if (cacheKey != null) {
                // The query result needs to be streamed into an in memory list before caching.
                // TODO - add a cap to how many records can be streamed back.  If this is exceeded, abort caching
                // and return the results.
                QueryResult cacheableResult = QueryResult.builder().data(Lists.newArrayList(result.getData().iterator())).pageTotals(result.getPageTotals()).build();
                cache.put(cacheKey, cacheableResult);
                result = cacheableResult;
            }
        }
        if (entityProjection.getPagination() != null && entityProjection.getPagination().returnPageTotals()) {
            entityProjection.getPagination().setPageTotals(result.getPageTotals());
        }
        response = new QueryResponse(HttpStatus.SC_OK, result.getData(), null);
        return new DataStoreIterableBuilder(result.getData()).build();
    } catch (HttpStatusException e) {
        response = new QueryResponse(e.getStatus(), null, e.getMessage());
        throw e;
    } catch (Exception e) {
        response = new QueryResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, null, e.getMessage());
        throw e;
    } finally {
        queryLogger.completeQuery(scope.getRequestId(), response);
    }
}
Also used : HttpStatus(com.yahoo.elide.core.exceptions.HttpStatus) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) QueryResponse(com.yahoo.elide.datastores.aggregation.core.QueryResponse) HashMap(java.util.HashMap) Argument(com.yahoo.elide.core.request.Argument) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) Map(java.util.Map) MatchesTemplateVisitor(com.yahoo.elide.datastores.aggregation.filter.visitor.MatchesTemplateVisitor) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ToString(lombok.ToString) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) RequiresFilter(com.yahoo.elide.datastores.aggregation.metadata.models.RequiresFilter) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) Cache(com.yahoo.elide.datastores.aggregation.cache.Cache) Lists(org.apache.commons.compress.utils.Lists) EntityProjection(com.yahoo.elide.core.request.EntityProjection) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) QueryKeyExtractor(com.yahoo.elide.datastores.aggregation.cache.QueryKeyExtractor) Type(com.yahoo.elide.core.type.Type) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) QueryLogger(com.yahoo.elide.datastores.aggregation.core.QueryLogger) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Query(com.yahoo.elide.datastores.aggregation.query.Query) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) ToString(lombok.ToString) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) IOException(java.io.IOException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) QueryResponse(com.yahoo.elide.datastores.aggregation.core.QueryResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class EntityProjectionMakerTest method testRootEntityWithSingleInclude.

@Test
public void testRootEntityWithSingleInclude() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.add("include", "authors");
    String path = "/book/1";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Book.class).attribute(Attribute.builder().name("title").type(String.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).attribute(Attribute.builder().name("genre").type(String.class).build()).attribute(Attribute.builder().name("language").type(String.class).build()).attribute(Attribute.builder().name("publishDate").type(long.class).build()).attribute(Attribute.builder().name("authorTypes").type(Collection.class).build()).attribute(Attribute.builder().name("price").type(Price.class).build()).relationship("authors", EntityProjection.builder().type(Author.class).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()).relationship("books", EntityProjection.builder().type(Book.class).build()).build()).relationship("publisher", EntityProjection.builder().type(Publisher.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) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Address(example.Address) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Book(example.Book) Collection(java.util.Collection) Author(example.Author) Editor(example.Editor) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.jupiter.api.Test)

Example 10 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class EntityProjectionMakerTest method testNestedCollectionNoQueryParams.

@Test
public void testNestedCollectionNoQueryParams() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    String path = "/author/1/books/3/publisher";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Author.class).relationship("books", EntityProjection.builder().type(Book.class).relationship("publisher", EntityProjection.builder().type(Publisher.class).attribute(Attribute.builder().name("name").type(String.class).build()).attribute(Attribute.builder().name("updateHookInvoked").type(boolean.class).build()).relationship("books", EntityProjection.builder().type(Book.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).pagination(PaginationImpl.getDefaultPagination(ClassType.of(Publisher.class))).build()).build()).build();
    EntityProjection actual = maker.parsePath(path);
    projectionEquals(expected, actual);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) TestRequestScope(com.yahoo.elide.core.TestRequestScope) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Publisher(example.Publisher) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Aggregations

RequestScope (com.yahoo.elide.core.RequestScope)132 Test (org.junit.jupiter.api.Test)99 PersistentResource (com.yahoo.elide.core.PersistentResource)63 TestRequestScope (com.yahoo.elide.core.TestRequestScope)28 Include (com.yahoo.elide.annotation.Include)27 Entity (javax.persistence.Entity)27 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)26 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)23 ReadPermission (com.yahoo.elide.annotation.ReadPermission)22 EntityProjection (com.yahoo.elide.core.request.EntityProjection)22 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)22 Book (example.Book)19 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)17 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)15 HashSet (java.util.HashSet)15 Publisher (example.Publisher)14 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)12 Author (example.Author)10 Editor (example.Editor)10 Collection (java.util.Collection)10