FilterTranslator implements FilterOperation<String> { @Override public String apply(FilterPredicate filterPredicate) { return apply(filterPredicate, false); }  static void registerJPQLGenerator(Operator op,
                                             JPQLPredicateGenerator generator); static void registerJPQLGenerator(Operator op,
                                             Class<?> entityClass,
                                             String fieldName,
                                             JPQLPredicateGenerator generator); static JPQLPredicateGenerator lookupJPQLGenerator(Operator op,
                                           Class<?> entityClass,
                                           String fieldName); static JPQLPredicateGenerator lookupJPQLGenerator(Operator op); @Override String apply(FilterPredicate filterPredicate); String apply(FilterExpression filterExpression, boolean prefixWithAlias);  }
@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(); String query = filterOp.apply(not, false); 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 = "WHERE NOT (((name IN (" + p2Params + ") OR genre IN (" + p3Params + ")) " + "AND (authors IS NOT EMPTY AND authors.name IN (" + p1Params + "))))"; assertEquals(expected, query); }
@Test public void testMemberOfOperator() throws Exception { List<Path.PathElement> path = Arrays.asList( new Path.PathElement(Book.class, String.class, "awards") ); FilterPredicate p1 = new FilterPredicate(new Path(path), Operator.HASMEMBER, Arrays.asList("awards1")); FilterPredicate p2 = new FilterPredicate(new Path(path), Operator.HASNOMEMBER, Arrays.asList("awards2")); AndFilterExpression and = new AndFilterExpression(p1, p2); FilterTranslator filterOp = new FilterTranslator(); String query = filterOp.apply(and, false); 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 expected = "WHERE (" + p1Params + " MEMBER OF awards " + "AND " + p2Params + " NOT MEMBER OF awards)"; assertEquals(expected, query); }
@Test public void testEmptyFieldOnPrefix() throws Exception { FilterPredicate pred = new FilterPredicate(new Path.PathElement(Book.class, String.class, ""), Operator.PREFIX_CASE_INSENSITIVE, Arrays.asList("value")); FilterTranslator filterOp = new FilterTranslator(); assertThrows(InvalidValueException.class, () -> filterOp.apply(pred)); }
@Test public void testEmptyFieldOnInfix() throws Exception { FilterPredicate pred = new FilterPredicate(new Path.PathElement(Book.class, String.class, ""), Operator.INFIX_CASE_INSENSITIVE, Arrays.asList("value")); FilterTranslator filterOp = new FilterTranslator(); assertThrows(InvalidValueException.class, () -> filterOp.apply(pred)); }
@Test public void testEmptyFieldOnPostfix() throws Exception { FilterPredicate pred = new FilterPredicate(new Path.PathElement(Book.class, String.class, ""), Operator.POSTFIX_CASE_INSENSITIVE, Arrays.asList("value")); FilterTranslator filterOp = new FilterTranslator(); assertThrows(InvalidValueException.class, () -> filterOp.apply(pred)); }