Search in sources :

Example 11 with PathElement

use of com.yahoo.elide.core.Path.PathElement in project elide by yahoo.

the class VerifyFieldAccessFilterExpressionVisitorTest method testAccept.

@Test
public void testAccept() throws Exception {
    Path p1Path = new Path(Arrays.asList(new PathElement(Book.class, Author.class, AUTHORS), new PathElement(Author.class, String.class, NAME)));
    FilterPredicate p1 = new InPredicate(p1Path, "foo", "bar");
    Path p2Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, NAME)));
    FilterPredicate p2 = new InPredicate(p2Path, "blah");
    Path p3Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, GENRE)));
    FilterPredicate p3 = new InPredicate(p3Path, SCIFI);
    // P4 is a duplicate of P3
    Path p4Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, GENRE)));
    FilterPredicate p4 = new InPredicate(p4Path, SCIFI);
    OrFilterExpression or = new OrFilterExpression(p2, p3);
    AndFilterExpression and1 = new AndFilterExpression(or, p1);
    AndFilterExpression and2 = new AndFilterExpression(and1, p4);
    NotFilterExpression not = new NotFilterExpression(and2);
    Book book = new Book();
    Author author = new Author();
    book.setAuthors(Collections.singleton(author));
    author.setBooks(Collections.singleton(book));
    PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
    VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
    // unrestricted fields
    assertTrue(not.accept(visitor));
    assertTrue(and1.accept(visitor));
    assertTrue(and2.accept(visitor));
    assertTrue(or.accept(visitor));
    assertTrue(p1.accept(visitor));
    assertTrue(p2.accept(visitor));
    assertTrue(p3.accept(visitor));
    assertTrue(p4.accept(visitor));
    PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
    verify(permissionExecutor, times(17)).evaluateFilterJoinUserChecks(any(), any());
    verify(permissionExecutor, times(5)).checkSpecificFieldPermissions(resource, null, ReadPermission.class, NAME);
    verify(permissionExecutor, times(21)).checkUserPermissions(any(), any(), isA(String.class));
    verify(permissionExecutor, never()).handleFilterJoinReject(any(), any(), any());
}
Also used : Path(com.yahoo.elide.core.Path) PersistentResource(com.yahoo.elide.core.PersistentResource) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) PathElement(com.yahoo.elide.core.Path.PathElement) Book(example.Book) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test)

Example 12 with PathElement

use of com.yahoo.elide.core.Path.PathElement in project elide by yahoo.

the class ExpressionScopingVisitorTest method testExpressionCopy.

@Test
public void testExpressionCopy() throws Exception {
    Path p1Path = new Path(Arrays.asList(new PathElement(Book.class, Author.class, "authors"), new PathElement(Author.class, String.class, NAME)));
    FilterPredicate p1 = new InPredicate(p1Path, "foo", "bar");
    FilterPredicate p2 = new InPredicate(new PathElement(Book.class, String.class, NAME), "blah%");
    FilterPredicate p3 = new InPredicate(new PathElement(Book.class, String.class, GENRE), SCIFI);
    // P4 is a duplicate of P3
    FilterPredicate p4 = new InPredicate(new PathElement(Book.class, String.class, GENRE), SCIFI);
    OrFilterExpression or = new OrFilterExpression(p2, p3);
    AndFilterExpression and1 = new AndFilterExpression(or, p1);
    AndFilterExpression and2 = new AndFilterExpression(and1, p4);
    NotFilterExpression not = new NotFilterExpression(and2);
    PathElement scope = new PathElement(Author.class, String.class, NAME);
    ExpressionScopingVisitor scopingVisitor = new ExpressionScopingVisitor(scope);
    FilterExpression copy = not.accept(scopingVisitor);
    assertNotEquals(not, copy);
    List<FilterPredicate> predicates = (List) copy.accept(new PredicateExtractionVisitor(new ArrayList<>()));
    List<FilterPredicate> toCompare = Arrays.asList(p2.scopedBy(scope), p3.scopedBy(scope), p1.scopedBy(scope), p4.scopedBy(scope));
    for (int i = 0; i < predicates.size(); i++) {
        FilterPredicate predicateOriginal = toCompare.get(i);
        FilterPredicate predicateCopy = predicates.get(i);
        assertEquals(predicateOriginal, predicateCopy);
        assertTrue(predicateCopy != predicateOriginal);
    }
    assertEquals("example_Author_name", predicates.get(0).getPath().getAlias());
    assertTrue(predicates.get(0).getParameters().get(0).getPlaceholder().startsWith(":name_name_"));
    assertEquals("blah\\%", predicates.get(0).getParameters().get(0).escapeMatching());
}
Also used : Path(com.yahoo.elide.core.Path) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) PathElement(com.yahoo.elide.core.Path.PathElement) Book(example.Book) ArrayList(java.util.ArrayList) List(java.util.List) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Test(org.junit.jupiter.api.Test)

Example 13 with PathElement

use of com.yahoo.elide.core.Path.PathElement in project elide by yahoo.

the class FilterPredicatePushdownExtractorTest method testPath.

@Test
public void testPath() {
    Path path = new Path(Book.class, dictionary, "genre");
    ImmutableList<PathElement> pathElements = ImmutableList.of(new PathElement(Book.class, String.class, "genre"));
    assertEquals("example_Book", path.getAlias());
    assertEquals("genre", path.getFieldPath());
    assertEquals(pathElements, path.getPathElements());
    assertEquals(Optional.of(pathElements.get(0)), path.lastElement());
    assertEquals("[Book].genre", path.toString());
    path = new Path(Book.class, dictionary, "this.editor.firstName");
    pathElements = ImmutableList.of(new PathElement(Book.class, null, "this"), new PathElement(Book.class, Editor.class, "editor"), new PathElement(Editor.class, String.class, "firstName"));
    assertEquals("example_Book_editor", path.getAlias());
    assertEquals("this.editor.firstName", path.getFieldPath());
    assertEquals(pathElements, path.getPathElements());
    assertEquals(Optional.of(pathElements.get(2)), path.lastElement());
    assertEquals("[Book].this/[Book].editor/[Editor].firstName", path.toString());
}
Also used : Path(com.yahoo.elide.core.Path) PathElement(com.yahoo.elide.core.Path.PathElement) Book(example.Book) Test(org.junit.jupiter.api.Test)

Example 14 with PathElement

use of com.yahoo.elide.core.Path.PathElement in project elide by yahoo.

the class AsyncAPICleanerRunnable method deleteAsyncAPI.

/**
 * This method deletes the historical queries based on threshold.
 * @param type AsyncAPI Type Implementation.
 */
protected <T extends AsyncAPI> void deleteAsyncAPI(Class<T> type) {
    try {
        Date cleanupDate = dateUtil.calculateFilterDate(Calendar.DATE, queryCleanupDays);
        PathElement createdOnPathElement = new PathElement(type, Long.class, "createdOn");
        FilterExpression fltDeleteExp = new LEPredicate(createdOnPathElement, cleanupDate);
        asyncAPIDao.deleteAsyncAPIAndResultByFilter(fltDeleteExp, type);
    } catch (Exception e) {
        log.error("Exception in scheduled cleanup: {}", e.toString());
    }
}
Also used : PathElement(com.yahoo.elide.core.Path.PathElement) LEPredicate(com.yahoo.elide.core.filter.predicates.LEPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Date(java.util.Date)

Example 15 with PathElement

use of com.yahoo.elide.core.Path.PathElement in project elide by yahoo.

the class AsyncAPICleanerRunnable method timeoutAsyncAPI.

/**
 * This method updates the status of long running async query which
 * were interrupted due to host crash/app shutdown to TIMEDOUT.
 * @param type AsyncAPI Type Implementation.
 */
protected <T extends AsyncAPI> void timeoutAsyncAPI(Class<T> type) {
    try {
        Date filterDate = dateUtil.calculateFilterDate(Calendar.MINUTE, maxRunTimeMinutes);
        PathElement createdOnPathElement = new PathElement(type, Long.class, "createdOn");
        PathElement statusPathElement = new PathElement(type, String.class, "status");
        FilterPredicate inPredicate = new InPredicate(statusPathElement, QueryStatus.PROCESSING, QueryStatus.QUEUED);
        FilterPredicate lePredicate = new LEPredicate(createdOnPathElement, filterDate);
        AndFilterExpression fltTimeoutExp = new AndFilterExpression(inPredicate, lePredicate);
        asyncAPIDao.updateStatusAsyncAPIByFilter(fltTimeoutExp, QueryStatus.TIMEDOUT, type);
    } catch (Exception e) {
        log.error("Exception in scheduled cleanup: {}", e.toString());
    }
}
Also used : PathElement(com.yahoo.elide.core.Path.PathElement) LEPredicate(com.yahoo.elide.core.filter.predicates.LEPredicate) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Date(java.util.Date) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Aggregations

PathElement (com.yahoo.elide.core.Path.PathElement)15 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)7 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)7 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)6 Test (org.junit.jupiter.api.Test)6 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)5 PermissionExecutor (com.yahoo.elide.core.security.PermissionExecutor)5 Book (example.Book)5 Path (com.yahoo.elide.core.Path)4 PersistentResource (com.yahoo.elide.core.PersistentResource)4 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)4 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)4 ReadPermission (com.yahoo.elide.annotation.ReadPermission)3 ForbiddenAccessException (com.yahoo.elide.core.exceptions.ForbiddenAccessException)3 LEPredicate (com.yahoo.elide.core.filter.predicates.LEPredicate)3 Author (example.Author)3 RequestScope (com.yahoo.elide.core.RequestScope)2 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)2 ExpressionResult (com.yahoo.elide.core.security.permissions.ExpressionResult)2 ArrayList (java.util.ArrayList)2