FilterParserImpl implements FilterParser { @Override public Expression parse(String filterString) throws ExpressionVisitException { if (_log.isDebugEnabled()) { _log.debug("Parsing filter: " + filterString); } if (Validator.isNull(filterString)) { throw new ExpressionVisitException("Filter is null"); } UriInfo uriInfo = _getUriInfo(filterString); FilterOption filterOption = uriInfo.getFilterOption(); org.apache.olingo.server.api.uri.queryoption.expression.Expression expression = filterOption.getExpression(); try { return expression.accept(new ExpressionVisitorImpl()); } catch (Exception exception) { throw new ExpressionVisitException( exception.getMessage(), exception); } } FilterParserImpl(EntityModel entityModel); @Override Expression parse(String filterString);  }
@Test public void testParseNonexistingField() { String filterString = "(nonExistingField eq 'value')"; AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse(filterString) ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage( "A property used in the filter criteria is not supported: %s", filterString); }
@Test public void testParseWithContainsMethod() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "contains(fieldExternal, 'value')"); Assert.assertNotNull(expression); MethodExpression methodExpression = (MethodExpression)expression; Assert.assertEquals( MethodExpression.Type.CONTAINS, methodExpression.getType()); List<Expression> expressions = methodExpression.getExpressions(); MemberExpression memberExpression = (MemberExpression)expressions.get( 0); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "fieldExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)expressions.get(1); Assert.assertEquals("'value'", literalExpression.getText()); }
@Test public void testParseWithContainsMethodAndBooleanType() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse("contains(booleanExternal, 7)") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage("Incompatible types."); }
@Test public void testParseWithContainsMethodAndDateType() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse( "contains(dateExternal, 2012-05-29T09:13:28Z)") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage("Incompatible types."); }
@Test public void testParseWithContainsMethodAndDoubleType() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse("contains(doubleExternal, 7)") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage("Incompatible types."); }
@Test public void testParseWithEmptyFilter() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse("") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage("Filter is null"); }
@Test public void testParseWithEqBinaryExpressionOnCollectionField() { try { _filterParserImpl.parse("collectionFieldExternal eq 'value'"); Assert.fail("Expected ExpressionVisitException was not thrown"); } catch (ExpressionVisitException expressionVisitException) { Assert.assertEquals( "Collection not allowed.", expressionVisitException.getMessage()); } }
@Test public void testParseWithEqBinaryExpressionWithBooleanFalse() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "booleanExternal eq false"); Assert.assertNotNull(expression); BinaryExpression binaryExpression = (BinaryExpression)expression; MemberExpression memberExpression = (MemberExpression)binaryExpression.getLeftOperationExpression(); Assert.assertEquals( BinaryExpression.Operation.EQ, binaryExpression.getOperation()); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "booleanExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)binaryExpression.getRightOperationExpression(); Assert.assertEquals("false", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.BOOLEAN, literalExpression.getType()); }
@Test public void testParseWithEqBinaryExpressionWithBooleanInvalid() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse("booleanExternal eq 'invalid'") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage("Incompatible types."); }
@Test public void testParseWithEqBinaryExpressionWithBooleanTrue() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "booleanExternal eq true"); Assert.assertNotNull(expression); BinaryExpression binaryExpression = (BinaryExpression)expression; MemberExpression memberExpression = (MemberExpression)binaryExpression.getLeftOperationExpression(); Assert.assertEquals( BinaryExpression.Operation.EQ, binaryExpression.getOperation()); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "booleanExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)binaryExpression.getRightOperationExpression(); Assert.assertEquals("true", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.BOOLEAN, literalExpression.getType()); }
@Test public void testParseWithEqBinaryExpressionWithDate() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "dateExternal ge 2012-05-29"); Assert.assertNotNull(expression); BinaryExpression binaryExpression = (BinaryExpression)expression; Assert.assertEquals( BinaryExpression.Operation.GE, binaryExpression.getOperation()); MemberExpression memberExpression = (MemberExpression)binaryExpression.getLeftOperationExpression(); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "dateExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)binaryExpression.getRightOperationExpression(); Assert.assertEquals("2012-05-29", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.DATE, literalExpression.getType()); }
@Test public void testParseWithEqBinaryExpressionWithDateTimeOffset() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "dateTimeExternal ge 2012-05-29T09:13:28Z"); Assert.assertNotNull(expression); BinaryExpression binaryExpression = (BinaryExpression)expression; Assert.assertEquals( BinaryExpression.Operation.GE, binaryExpression.getOperation()); MemberExpression memberExpression = (MemberExpression)binaryExpression.getLeftOperationExpression(); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "dateTimeExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)binaryExpression.getRightOperationExpression(); Assert.assertEquals( "2012-05-29T09:13:28Z", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.DATE_TIME, literalExpression.getType()); }
@Test public void testParseWithEqBinaryExpressionWithDateTimeWithInvalidType() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse("dateTimeExternal ge 2012-05-29") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessageContaining("Incompatible types"); }
@Test public void testParseWithEqBinaryExpressionWithDateWithInvalidType() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse( "dateExternal ge 2012-05-29T09:13:28Z") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessageContaining("Incompatible types"); }
@Test public void testParseWithEqBinaryExpressionWithMemberWithComplexProperty() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "complexField/primitiveField eq 'value'"); Assert.assertNotNull(expression); BinaryExpression binaryExpression = (BinaryExpression)expression; MemberExpression memberExpression = (MemberExpression)binaryExpression.getLeftOperationExpression(); ComplexPropertyExpression complexPropertyExpression = (ComplexPropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "complexField", complexPropertyExpression.getName()); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression) complexPropertyExpression.getPropertyExpression(); Assert.assertEquals( "primitiveField", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)binaryExpression.getRightOperationExpression(); Assert.assertEquals("'value'", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.STRING, literalExpression.getType()); }
@Test public void testParseWithEqBinaryExpressionWithNoSingleQuotes() { String filterString = "(fieldExternal eq value)"; AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse(filterString) ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage( "A property used in the filter criteria is not supported: %s", filterString); }
@Test public void testParseWithEqBinaryExpressionWithSingleQuotes() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "fieldExternal eq 'value'"); Assert.assertNotNull(expression); BinaryExpression binaryExpression = (BinaryExpression)expression; Assert.assertEquals( BinaryExpression.Operation.EQ, binaryExpression.getOperation()); MemberExpression memberExpression = (MemberExpression)binaryExpression.getLeftOperationExpression(); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "fieldExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)binaryExpression.getRightOperationExpression(); Assert.assertEquals("'value'", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.STRING, literalExpression.getType()); }
@Test public void testParseWithEqBinaryExpressionWithSingleQuotesAndParentheses() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "(fieldExternal eq 'value')"); Assert.assertNotNull(expression); BinaryExpression binaryExpression = (BinaryExpression)expression; Assert.assertEquals( BinaryExpression.Operation.EQ, binaryExpression.getOperation()); MemberExpression memberExpression = (MemberExpression)binaryExpression.getLeftOperationExpression(); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "fieldExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)binaryExpression.getRightOperationExpression(); Assert.assertEquals("'value'", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.STRING, literalExpression.getType()); }
@Test public void testParseWithGeBinaryExpression() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "fieldExternal ge 'value'"); Assert.assertNotNull(expression); BinaryExpression binaryExpression = (BinaryExpression)expression; Assert.assertEquals( BinaryExpression.Operation.GE, binaryExpression.getOperation()); MemberExpression memberExpression = (MemberExpression)binaryExpression.getLeftOperationExpression(); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "fieldExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)binaryExpression.getRightOperationExpression(); Assert.assertEquals("'value'", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.STRING, literalExpression.getType()); }
@Test public void testParseWithINMethod() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "fieldExternal in ('value1', 'value2', 'value3')"); Assert.assertNotNull(expression); ListExpression listExpression = (ListExpression)expression; Assert.assertEquals( ListExpression.Operation.IN, listExpression.getOperation()); MemberExpression memberExpression = (MemberExpression)listExpression.getLeftOperationExpression(); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "fieldExternal", primitivePropertyExpression.getName()); List<Expression> rightOperationExpressions = listExpression.getRightOperationExpressions(); LiteralExpression literalExpression1 = (LiteralExpression)rightOperationExpressions.get(0); Assert.assertEquals("'value1'", literalExpression1.getText()); }
@Test public void testParseWithLambdaAllOnCollectionField() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse( "collectionFieldExternal/all(f:contains(f,'alu'))") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage( "An expression cannot be obtained from URI resources " + "[collectionFieldExternal, all]"); }
@Test public void testParseWithLambdaAnyContainsOnCollectionField() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "collectionFieldExternal/any(f:contains(f,'alu'))"); Assert.assertNotNull(expression); MemberExpression memberExpression = (MemberExpression)expression; CollectionPropertyExpression collectionPropertyExpression = (CollectionPropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "collectionFieldExternal", collectionPropertyExpression.getName()); LambdaFunctionExpression lambdaFunctionExpression = collectionPropertyExpression.getLambdaFunctionExpression(); Assert.assertEquals( LambdaFunctionExpression.Type.ANY, lambdaFunctionExpression.getType()); Assert.assertEquals("f", lambdaFunctionExpression.getVariableName()); MethodExpression methodExpression = (MethodExpression)lambdaFunctionExpression.getExpression(); Assert.assertEquals( MethodExpression.Type.CONTAINS, methodExpression.getType()); List<Expression> methodExpressionExpressions = methodExpression.getExpressions(); Assert.assertNotNull(methodExpressionExpressions); Assert.assertEquals( methodExpressionExpressions.toString(), 2, methodExpressionExpressions.size()); MemberExpression methodExpressionMemberExpression = (MemberExpression)methodExpressionExpressions.get(0); LambdaVariableExpression lambdaVariableExpression = (LambdaVariableExpression) methodExpressionMemberExpression.getExpression(); Assert.assertNotNull(lambdaVariableExpression); Assert.assertEquals("f", lambdaVariableExpression.getVariableName()); LiteralExpression literalExpression = (LiteralExpression)methodExpressionExpressions.get(1); Assert.assertNotNull(literalExpression); Assert.assertEquals("'alu'", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.STRING, literalExpression.getType()); }
@Test public void testParseWithLambdaAnyContainsOnNoncollectionField() { try { _filterParserImpl.parse("fieldExternal/any(f:contains(f,'alu'))"); Assert.fail("Expected ExpressionVisitException was not thrown"); } catch (ExpressionVisitException expressionVisitException) { Assert.assertEquals( "Expected token 'QualifiedName' not found.", expressionVisitException.getMessage()); } }
@Test public void testParseWithLambdaAnyEqOnCollectionFieldInComplexField() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "complexField/collectionField/any(f:contains(f,'alu'))"); Assert.assertNotNull(expression); }
@Test public void testParseWithLeBinaryExpression() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "fieldExternal le 'value'"); Assert.assertNotNull(expression); BinaryExpression binaryExpression = (BinaryExpression)expression; Assert.assertEquals( BinaryExpression.Operation.LE, binaryExpression.getOperation()); MemberExpression memberExpression = (MemberExpression)binaryExpression.getLeftOperationExpression(); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "fieldExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)binaryExpression.getRightOperationExpression(); Assert.assertEquals("'value'", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.STRING, literalExpression.getType()); }
@Test public void testParseWithNotUnaryExpressionWithEqBinaryExpression() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "not (booleanExternal eq true)"); Assert.assertNotNull(expression); UnaryExpression unaryExpression = (UnaryExpression)expression; Assert.assertEquals( UnaryExpression.Operation.NOT, unaryExpression.getOperation()); BinaryExpression binaryExpression = (BinaryExpression)unaryExpression.getExpression(); MemberExpression memberExpression = (MemberExpression)binaryExpression.getLeftOperationExpression(); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "booleanExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)binaryExpression.getRightOperationExpression(); Assert.assertEquals("true", literalExpression.getText()); Assert.assertEquals( LiteralExpression.Type.BOOLEAN, literalExpression.getType()); }
@Test public void testParseWithNullExpression() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse(null) ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage("Filter is null"); }
@Test public void testParseWithStarsWithMethod() throws ExpressionVisitException { Expression expression = _filterParserImpl.parse( "startswith(fieldExternal, 'value')"); Assert.assertNotNull(expression); MethodExpression methodExpression = (MethodExpression)expression; Assert.assertEquals( MethodExpression.Type.STARTS_WITH, methodExpression.getType()); List<Expression> expressions = methodExpression.getExpressions(); MemberExpression memberExpression = (MemberExpression)expressions.get( 0); PrimitivePropertyExpression primitivePropertyExpression = (PrimitivePropertyExpression)memberExpression.getExpression(); Assert.assertEquals( "fieldExternal", primitivePropertyExpression.getName()); LiteralExpression literalExpression = (LiteralExpression)expressions.get(1); Assert.assertEquals("'value'", literalExpression.getText()); }
@Test public void testParseWithStarsWithMethodAndBooleanType() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse("startswith(booleanExternal, 7)") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage("Incompatible types."); }
@Test public void testParseWithStarsWithMethodAndDateType() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse( "contains(dateExternal, 2012-05-29T09:13:28Z)") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage("Incompatible types."); }
@Test public void testParseWithStarsWithMethodAnDoubleType() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse("contains(doubleExternal, 7)") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage("Incompatible types."); }
@Test public void testParseWithStartsWithMethodAndDateType() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _filterParserImpl.parse( "startswith(dateExternal, 2012-05-29T09:13:28Z)") ).isInstanceOf( ExpressionVisitException.class ); exception.hasMessage("Incompatible types."); }