You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@olingo.apache.org by Joe Bidon <jo...@mrbidon.fr> on 2019/05/24 14:05:06 UTC

Writing Integration Test of ExpressionVisitor with Olingo / ODATA 4

Hi,

I'm implementing a web service respecting version 4 of the OData standard
in Java language with the framework Olingo. I need to customize the
response of $filter. I've implemented a Visitor as documented in the quick
start
<https://olingo.apache.org/doc/odata4/tutorials/sqo_f/tutorial_sqo_f.html>.
I need to implement an integration Test to try a different kind of grammar
and to prevent regression bug in case of future maintenance.

For the V2, I found the following tutorial
<https://olingo.apache.org/doc/odata2/tutorials/Olingo_Tutorial_AdvancedRead_FilterVisitor.html>
and the following code :

@Testpublic void printExpressionWithProperty() throws Exception {
  //Use a mocked edmProvider for this tutorial
  TestEdmProvider provider = new TestEdmProvider();
  Edm edm = RuntimeDelegate.createEdm(provider);
  EdmEntityType entityType =
edm.getEntityType(TestEdmProvider.NAMESPACE_1,
TestEdmProvider.ENTITY_TYPE_1_1.getName());

  String rawExpression = "EmployeeId eq '1'";
  FilterExpression expression = UriParser.parseFilter (null,
entityType, rawExpression);
  String whereClause = (String) expression.accept(new
JdbcSimpleStringVisitor());
  System.out.println("Raw: " + rawExpression + " ------> Whereclause:
" + whereClause);
  System.out.println();}

Unfortunately, UriParser.parseFilter doesn't exist in the v4.

I tried this :

public class MyVisitorTest {

    private final FullQualifiedName NAME1 = new
FullQualifiedName("testNamespace1", "myfield");
    private final OData odata = OData.newInstance();

    public EdmEntityType createEntityType(final FullQualifiedName fqn) {
        if (NAME1.equals(fqn)) {
            EdmEntityType entityType = mock(EdmEntityType.class);
            when(entityType.getNamespace()).thenReturn(fqn.getNamespace());
            when(entityType.getName()).thenReturn(fqn.getName());
            return entityType;
        }
        return null;
    }


    private Expression parseExpression(final String expressionString)
            throws UriParserException, UriValidationException {

        UriTokenizer tokenizer = new UriTokenizer(expressionString);

        EdmEntityType entityType = createEntityType(NAME1);

        Edm edm = mock(Edm.class);
        when(edm.getEntityType(NAME1)).thenReturn(entityType);

        final Expression expression = new ExpressionParser(edm,
odata).parse(tokenizer, null, null, null);
        assertNotNull(expression);
        assertTrue(tokenizer.next(UriTokenizer.TokenKind.EOF));
        return expression;
    }

    @Test
    public void simpleTest() throws UriParserException,
UriValidationException, ODataApplicationException,
ExpressionVisitException {

        String exp = "myfield gt 2019-01-01T00:00:00Z";
        Expression e = parseExpression(exp);

        MyVisitor myVisitor = new MyVisitor();
        String result = (String) e.accept(startEndMeasureVisitor);
        assertEquals(result.toString(), "MyResult");
    }}

And it doesn't work, it sends me the following message :

Property paths must follow a structured type.

So I'm looking for any ideas to make my unit test to work or if you've got
working example to share...

ps : question already asked on stackoverflow :.
https://stackoverflow.com/questions/54214601/writing-integration-test-of-expressionvisitor-with-olingo-odata-4