Search in sources :

Example 1 with NotEmptyPredicate

use of com.yahoo.elide.core.filter.predicates.NotEmptyPredicate in project elide by yahoo.

the class InMemoryFilterExecutorTest method isemptyAndNotemptyPredicateTest.

@Test
public void isemptyAndNotemptyPredicateTest() throws Exception {
    author = new Author();
    author.setId(1L);
    // When name is empty and books are empty
    author.setBooks(new HashSet<>());
    author.setAwards(new HashSet<>());
    expression = new IsEmptyPredicate(authorAwardsElement);
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
    expression = new IsEmptyPredicate(authorBooksElement);
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
    expression = new NotEmptyPredicate(authorAwardsElement);
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    expression = new NotEmptyPredicate(authorBooksElement);
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    // When name and books are not empty
    author.setAwards(Arrays.asList("Bookery prize"));
    author.getBooks().add(new Book());
    expression = new IsEmptyPredicate(authorAwardsElement);
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    expression = new IsEmptyPredicate(authorBooksElement);
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    expression = new NotEmptyPredicate(authorAwardsElement);
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
    expression = new NotEmptyPredicate(authorBooksElement);
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
}
Also used : IsEmptyPredicate(com.yahoo.elide.core.filter.predicates.IsEmptyPredicate) Book(example.Book) Author(example.Author) NotEmptyPredicate(com.yahoo.elide.core.filter.predicates.NotEmptyPredicate) Test(org.junit.jupiter.api.Test)

Example 2 with NotEmptyPredicate

use of com.yahoo.elide.core.filter.predicates.NotEmptyPredicate in project elide by yahoo.

the class FilterTranslatorTest method testHQLQueryVisitor.

@Test
public void testHQLQueryVisitor() throws Exception {
    List<Path.PathElement> p0Path = Arrays.asList(new Path.PathElement(Book.class, Author.class, "authors"));
    FilterPredicate p0 = new NotEmptyPredicate(new Path(p0Path));
    List<Path.PathElement> p1Path = Arrays.asList(new Path.PathElement(Book.class, Author.class, "authors"), new Path.PathElement(Author.class, String.class, "name"));
    FilterPredicate p1 = new InPredicate(new Path(p1Path), "foo", "bar");
    List<Path.PathElement> p2Path = Arrays.asList(new Path.PathElement(Book.class, String.class, "name"));
    FilterPredicate p2 = new InPredicate(new Path(p2Path), "blah");
    List<Path.PathElement> p3Path = Arrays.asList(new Path.PathElement(Book.class, String.class, "genre"));
    FilterPredicate p3 = new InPredicate(new Path(p3Path), "scifi");
    OrFilterExpression or = new OrFilterExpression(p2, p3);
    AndFilterExpression and1 = new AndFilterExpression(p0, p1);
    AndFilterExpression and2 = new AndFilterExpression(or, and1);
    NotFilterExpression not = new NotFilterExpression(and2);
    FilterTranslator filterOp = new FilterTranslator(dictionary);
    String query = filterOp.apply(not, false);
    query = query.trim().replaceAll(" +", " ");
    String p1Params = p1.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
    String p2Params = p2.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
    String p3Params = p3.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
    String expected = "NOT (((name IN (" + p2Params + ") OR genre IN (" + p3Params + ")) " + "AND (authors IS NOT EMPTY AND authors.name IN (" + p1Params + "))))";
    assertEquals(expected, query);
}
Also used : Path(com.yahoo.elide.core.Path) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) Book(example.Book) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) NotEmptyPredicate(com.yahoo.elide.core.filter.predicates.NotEmptyPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test)

Aggregations

NotEmptyPredicate (com.yahoo.elide.core.filter.predicates.NotEmptyPredicate)2 Author (example.Author)2 Book (example.Book)2 Test (org.junit.jupiter.api.Test)2 Path (com.yahoo.elide.core.Path)1 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)1 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)1 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)1 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)1 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)1 IsEmptyPredicate (com.yahoo.elide.core.filter.predicates.IsEmptyPredicate)1