MultipleFilterDialect implements JoinFilterDialect, SubqueryFilterDialect { @Override public FilterExpression parseGlobalExpression(String path, MultivaluedMap<String, String> queryParams) throws ParseException { if (joinDialects.isEmpty()) { throw new ParseException("Heterogeneous type filtering not supported"); } return parseExpression(joinDialects, (dialect) -> dialect.parseGlobalExpression(path, queryParams)); } MultipleFilterDialect(EntityDictionary dictionary); @Override FilterExpression parseGlobalExpression(String path,
                                                  MultivaluedMap<String, String> queryParams); @Override Map<String, FilterExpression> parseTypedExpression(String path,
                                                              MultivaluedMap<String, String> queryParams);  }
@Test public void testGlobalExpressionParsing() throws Exception { JoinFilterDialect dialect1 = mock(JoinFilterDialect.class); JoinFilterDialect dialect2 = mock(JoinFilterDialect.class); FilterExpression filterExpression = mock(FilterExpression.class); MultipleFilterDialect dialect = new MultipleFilterDialect( Arrays.asList(dialect1, dialect2), Collections.EMPTY_LIST ); MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(); queryParams.add( "filter[author.books.title][in]", "foo,bar,baz" ); queryParams.add( "filter[author.name][infix]", "Hemingway" ); when(dialect1.parseGlobalExpression("/author", queryParams)).thenThrow(new ParseException("")); when(dialect2.parseGlobalExpression("/author", queryParams)).thenReturn(filterExpression); FilterExpression returnExpression = dialect.parseGlobalExpression("/author", queryParams); verify(dialect1, times(1)).parseGlobalExpression("/author", queryParams); verify(dialect2, times(1)).parseGlobalExpression("/author", queryParams); assertEquals(returnExpression, filterExpression); }
@Test public void testMissingGlobalDialect() throws Exception { MultipleFilterDialect dialect = new MultipleFilterDialect( Collections.EMPTY_LIST, Collections.EMPTY_LIST ); MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(); queryParams.add( "filter[author.books.title][in]", "foo,bar,baz" ); queryParams.add( "filter[author.name][infix]", "Hemingway" ); assertThrows(ParseException.class, () -> dialect.parseGlobalExpression("/author", queryParams)); }
@Test public void testGlobalExpressionParseFailure() throws Exception { JoinFilterDialect dialect1 = mock(JoinFilterDialect.class); JoinFilterDialect dialect2 = mock(JoinFilterDialect.class); MultipleFilterDialect dialect = new MultipleFilterDialect( Arrays.asList(dialect1, dialect2), Collections.EMPTY_LIST ); MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(); queryParams.add( "filter[author.books.title][in]", "foo,bar,baz" ); queryParams.add( "filter[author.name][infix]", "Hemingway" ); when(dialect1.parseGlobalExpression("/author", queryParams)).thenThrow(new ParseException("one")); when(dialect2.parseGlobalExpression("/author", queryParams)).thenThrow(new ParseException("two")); try { dialect.parseGlobalExpression("/author", queryParams); } catch (ParseException e) { assertEquals("two\none", e.getMessage()); } }