use of com.yahoo.elide.core.filter.expression.NotFilterExpression in project elide by yahoo.
the class FilterExpressionNormalizationVisitor method visitNotExpression.
@Override
public FilterExpression visitNotExpression(NotFilterExpression fe) {
FilterExpression inner = fe.getNegated();
if (inner instanceof AndFilterExpression) {
AndFilterExpression and = (AndFilterExpression) inner;
FilterExpression left = new NotFilterExpression(and.getLeft()).accept(this);
FilterExpression right = new NotFilterExpression(and.getRight()).accept(this);
return new OrFilterExpression(left, right);
}
if (inner instanceof OrFilterExpression) {
OrFilterExpression or = (OrFilterExpression) inner;
FilterExpression left = new NotFilterExpression(or.getLeft()).accept(this);
FilterExpression right = new NotFilterExpression(or.getRight()).accept(this);
return new AndFilterExpression(left, right);
}
if (inner instanceof NotFilterExpression) {
NotFilterExpression not = (NotFilterExpression) inner;
return (not.getNegated()).accept(this);
}
if (inner instanceof FilterPredicate) {
FilterPredicate filter = (FilterPredicate) inner;
return filter.negate();
}
return inner;
}
use of com.yahoo.elide.core.filter.expression.NotFilterExpression in project elide by yahoo.
the class MatchesTemplateVisitor method matches.
private boolean matches(FilterExpression a, FilterExpression b) {
if (!a.getClass().equals(b.getClass())) {
return false;
}
if (a instanceof AndFilterExpression) {
AndFilterExpression andA = (AndFilterExpression) a;
AndFilterExpression andB = (AndFilterExpression) b;
return matches(andA.getLeft(), andB.getLeft()) && matches(andA.getRight(), andB.getRight());
}
if (a instanceof OrFilterExpression) {
OrFilterExpression orA = (OrFilterExpression) a;
OrFilterExpression orB = (OrFilterExpression) b;
return matches(orA.getLeft(), orB.getLeft()) && matches(orA.getRight(), orB.getRight());
}
if (a instanceof NotFilterExpression) {
NotFilterExpression notA = (NotFilterExpression) a;
NotFilterExpression notB = (NotFilterExpression) b;
return matches(notA.getNegated(), notB.getNegated());
}
FilterPredicate predicateA = (FilterPredicate) a;
FilterPredicate predicateB = (FilterPredicate) b;
boolean valueMatches = predicateA.getValues().equals(predicateB.getValues());
boolean operatorMatches = predicateA.getOperator().equals(predicateB.getOperator());
boolean pathMatches = pathMatches(predicateA.getPath(), predicateB.getPath());
boolean usingTemplate = false;
if (predicateA.getValues().size() == 1) {
String value = predicateA.getValues().get(0).toString();
Matcher matcher = TEMPLATE_PATTERN.matcher(value);
usingTemplate = matcher.matches();
if (usingTemplate && pathMatches & operatorMatches) {
String argumentName = matcher.group(1);
arguments.put(argumentName, Argument.builder().name(argumentName).value(predicateB.getValues().size() == 1 ? predicateB.getValues().get(0) : predicateB.getValues()).build());
}
}
return (operatorMatches && pathMatches && (valueMatches || usingTemplate));
}
use of com.yahoo.elide.core.filter.expression.NotFilterExpression in project elide by yahoo.
the class VerifyFieldAccessFilterExpressionVisitorTest method testReject.
@Test
public void testReject() {
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, HOME)));
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);
PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
when(permissionExecutor.checkSpecificFieldPermissions(resource, null, ReadPermission.class, HOME)).thenThrow(ForbiddenAccessException.class);
VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
// restricted HOME field
assertFalse(not.accept(visitor));
assertFalse(and1.accept(visitor));
assertFalse(and2.accept(visitor));
assertFalse(or.accept(visitor));
assertFalse(p2.accept(visitor));
// unrestricted fields
assertTrue(p1.accept(visitor));
assertTrue(p3.accept(visitor));
assertTrue(p4.accept(visitor));
verify(permissionExecutor, times(8)).evaluateFilterJoinUserChecks(any(), any());
verify(permissionExecutor, times(5)).checkSpecificFieldPermissions(resource, null, ReadPermission.class, HOME);
verify(permissionExecutor, times(9)).checkUserPermissions(any(), any(), isA(String.class));
verify(permissionExecutor, times(5)).handleFilterJoinReject(any(), any(), any());
}
use of com.yahoo.elide.core.filter.expression.NotFilterExpression 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);
}
use of com.yahoo.elide.core.filter.expression.NotFilterExpression in project elide by yahoo.
the class SplitFilterExpressionVisitorTest method testVisitNotExpression.
@Test
public void testVisitNotExpression() {
NotFilterExpression notExpression = new NotFilterExpression(new AndFilterExpression(WHERE_PREDICATE, HAVING_PREDICATE));
assertNull(splitFilterExpressionVisitor.visitNotExpression(notExpression).getWhereExpression());
assertEquals("(playerStats.overallRating NOT [foo] OR playerStats.highScore LE [99])", splitFilterExpressionVisitor.visitNotExpression(notExpression).getHavingExpression().toString());
}
Aggregations