ExpressionParser { public Expression parse(UriTokenizer tokenizer, final EdmType referringType, final Collection<String> crossjoinEntitySetNames, final Map<String, AliasQueryOption> aliases) throws UriParserException, UriValidationException { this.tokenizer = tokenizer; this.referringType = referringType; this.crossjoinEntitySetNames = crossjoinEntitySetNames; this.aliases = aliases; final Expression expression = parseExpression(); checkNoCollection(expression); return expression; } ExpressionParser(final Edm edm, final OData odata); Expression parse(UriTokenizer tokenizer, final EdmType referringType,
      final Collection<String> crossjoinEntitySetNames, final Map<String, AliasQueryOption> aliases);  }
@Test public void testPropertyPathExp() throws Exception { final String entitySetName = "ESName"; final String keyPropertyName = "a"; EdmProperty keyProperty = mockProperty(keyPropertyName, OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)); EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty); EdmEntityType entityType = mockEntityType(keyPropertyName, keyPropertyRef); Mockito.when(entityType.getPropertyNames()).thenReturn(Collections.singletonList(keyPropertyName)); Mockito.when(entityType.getProperty(keyPropertyName)).thenReturn(keyProperty); EdmEntitySet entitySet = mockEntitySet(entitySetName, entityType); EdmEntityContainer container = mockContainer(entitySetName, entitySet); Edm mockedEdm = Mockito.mock(Edm.class); Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container); UriTokenizer tokenizer = new UriTokenizer("a eq \'abc\'"); Expression expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, entityType, null, null); assertNotNull(expression); assertEquals("{[a] EQ \'abc\'}", expression.toString()); tokenizer = new UriTokenizer("a in (\'abc\', \'xyz\')"); expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, entityType, null, null); assertNotNull(expression); assertEquals("{[a] IN [\'abc\', \'xyz\']}", expression.toString()); try { tokenizer = new UriTokenizer("a in (\'abc\', 10)"); expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, entityType, null, null); } catch (UriParserSemanticException e) { assertEquals("Incompatible types.", e.getMessage()); } }
@Test(expected = UriParserSemanticException.class) public void testPropertyPathExpWithoutType() throws Exception { final String entitySetName = "ESName"; final String keyPropertyName = "a"; EdmProperty keyProperty = mockProperty(keyPropertyName, OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)); EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty); EdmEntityType entityType = mockEntityType(keyPropertyName, keyPropertyRef); Mockito.when(entityType.getPropertyNames()).thenReturn(Collections.singletonList(keyPropertyName)); Mockito.when(entityType.getProperty(keyPropertyName)).thenReturn(keyProperty); EdmEntitySet entitySet = mockEntitySet(entitySetName, entityType); EdmEntityContainer container = mockContainer(entitySetName, entitySet); Edm mockedEdm = Mockito.mock(Edm.class); Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container); UriTokenizer tokenizer = new UriTokenizer("a eq \'abc\'"); new ExpressionParser(mockedEdm, odata).parse(tokenizer, null, null, null); }
@Test(expected = UriParserSemanticException.class) public void testPropertyPathExpWithoutProperty() throws Exception { final String entitySetName = "ESName"; final String keyPropertyName = "a"; EdmProperty keyProperty = mockProperty(keyPropertyName, OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)); EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty); EdmEntityType entityType = mockEntityType(keyPropertyName, keyPropertyRef); Mockito.when(entityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("test.ETName")); EdmEntitySet entitySet = mockEntitySet(entitySetName, entityType); EdmEntityContainer container = mockContainer(entitySetName, entitySet); Edm mockedEdm = Mockito.mock(Edm.class); Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container); UriTokenizer tokenizer = new UriTokenizer("a eq \'abc\'"); new ExpressionParser(mockedEdm, odata).parse(tokenizer, entityType, null, null); }
@Test public void testComplexPropertyPathExp() throws Exception { final String entitySetName = "ESName"; final String keyPropertyName = "a"; final String complexPropertyName = "comp"; final String propertyName = "prop"; EdmProperty keyProperty = mockProperty(keyPropertyName, OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)); EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty); EdmProperty property = mockProperty(propertyName, OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)); EdmComplexType complexType = mockComplexType(propertyName, property); EdmProperty complexProperty = mockProperty(complexPropertyName, complexType); EdmEntityType entityType = mockEntityType(keyPropertyName, keyPropertyRef); Mockito.when(entityType.getPropertyNames()).thenReturn(Arrays.asList(keyPropertyName, complexPropertyName)); Mockito.when(entityType.getProperty(keyPropertyName)).thenReturn(keyProperty); Mockito.when(entityType.getProperty(complexPropertyName)).thenReturn(complexProperty); EdmEntitySet entitySet = mockEntitySet(entitySetName, entityType); EdmEntityContainer container = mockContainer(entitySetName, entitySet); Edm mockedEdm = Mockito.mock(Edm.class); Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container); UriTokenizer tokenizer = new UriTokenizer("comp/prop eq \'abc\'"); Expression expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, entityType, null, null); assertNotNull(expression); assertEquals("{[comp, prop] EQ \'abc\'}", expression.toString()); tokenizer = new UriTokenizer("comp/prop in (\'abc\','xyz')"); expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, entityType, null, null); assertNotNull(expression); assertEquals("{[comp, prop] IN [\'abc\', \'xyz\']}", expression.toString()); }
@Test public void testLambdaPropertyPathExp() throws Exception { final String entitySetName = "ESName"; final String keyPropertyName = "a"; final String complexPropertyName = "comp"; final String propertyName = "prop"; EdmProperty keyProperty = mockProperty(keyPropertyName, OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)); EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty); EdmProperty property = mockProperty(propertyName, OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)); EdmComplexType complexType = mockComplexType(propertyName, property); EdmProperty complexProperty = mockProperty(complexPropertyName, complexType); Mockito.when(complexProperty.isCollection()).thenReturn(true); EdmEntityType entityType = mockEntityType(keyPropertyName, keyPropertyRef); Mockito.when(entityType.getPropertyNames()).thenReturn(Arrays.asList(keyPropertyName, complexPropertyName)); Mockito.when(entityType.getProperty(keyPropertyName)).thenReturn(keyProperty); Mockito.when(entityType.getProperty(complexPropertyName)).thenReturn(complexProperty); Mockito.when(entityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("test.ET")); EdmEntitySet entitySet = mockEntitySet(entitySetName, entityType); EdmEntityContainer container = mockContainer(entitySetName, entitySet); Edm mockedEdm = Mockito.mock(Edm.class); Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container); UriTokenizer tokenizer = new UriTokenizer("comp/any(d:d/prop eq \'abc\')"); Expression expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, entityType, null, null); assertNotNull(expression); assertEquals("[comp, any]", expression.toString()); tokenizer = new UriTokenizer("comp/all(d:d/prop eq \'abc\')"); expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, entityType, null, null); assertNotNull(expression); assertEquals("[comp, all]", expression.toString()); }
@Test public void testNavigationPropertyPathExp() throws Exception { final String entitySetName = "ESName"; final String keyPropertyName = "a"; final String complexPropertyName = "comp"; final String propertyName = "navProp"; EdmProperty keyProperty = mockProperty(keyPropertyName, OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)); EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty); EdmEntityType targetEntityType = mockEntityType(keyPropertyName, keyPropertyRef); Mockito.when(targetEntityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("test.TargetET")); EdmNavigationProperty navProperty = mockNavigationProperty(propertyName, targetEntityType); EdmComplexType complexType = mockComplexType(propertyName, navProperty); EdmProperty complexProperty = mockProperty(complexPropertyName, complexType); EdmEntityType startEntityType = mockEntityType(keyPropertyName, keyPropertyRef); Mockito.when(startEntityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("test.StartET")); Mockito.when(startEntityType.getPropertyNames()).thenReturn( Arrays.asList(keyPropertyName, complexPropertyName)); Mockito.when(startEntityType.getProperty(keyPropertyName)).thenReturn(keyProperty); Mockito.when(startEntityType.getProperty(complexPropertyName)).thenReturn(complexProperty); EdmEntitySet entitySet = mockEntitySet(entitySetName, startEntityType); EdmEntityContainer container = mockContainer(entitySetName, entitySet); Edm mockedEdm = Mockito.mock(Edm.class); Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container); UriTokenizer tokenizer = new UriTokenizer("comp/navProp"); final Expression expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, startEntityType, null, null); assertNotNull(expression); assertEquals("[comp, navProp]", expression.toString()); }
@Test public void testDerivedPathExp() throws Exception { final String derivedEntitySetName = "ESName"; final String keyPropertyName = "a"; final String propertyName = "navProp"; EdmProperty keyProperty = mockProperty(keyPropertyName, OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)); EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty); EdmEntityType navEntityType = mockEntityType(keyPropertyName, keyPropertyRef); Mockito.when(navEntityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("test.navET")); Mockito.when(navEntityType.getNamespace()).thenReturn("test"); Mockito.when(navEntityType.getPropertyNames()).thenReturn( Arrays.asList(keyPropertyName)); Mockito.when(navEntityType.getProperty(keyPropertyName)).thenReturn(keyProperty); EdmEntityType baseEntityType = mockEntityType(keyPropertyName, keyPropertyRef); Mockito.when(baseEntityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("test.baseET")); Mockito.when(baseEntityType.getNamespace()).thenReturn("test"); Mockito.when(baseEntityType.getPropertyNames()).thenReturn( Arrays.asList(keyPropertyName)); Mockito.when(baseEntityType.getProperty(keyPropertyName)).thenReturn(keyProperty); Mockito.when(navEntityType.getBaseType()).thenReturn(baseEntityType); Mockito.when(baseEntityType.compatibleTo(navEntityType)).thenReturn(true); EdmEntityType entityType = mockEntityType(keyPropertyName, keyPropertyRef); Mockito.when(entityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("test.derivedET")); Mockito.when(entityType.getNamespace()).thenReturn("test"); Mockito.when(entityType.getPropertyNames()).thenReturn(Arrays.asList(keyPropertyName, propertyName)); EdmNavigationProperty navProperty = mockNavigationProperty(propertyName, navEntityType); Mockito.when(entityType.getProperty(propertyName)).thenReturn(navProperty); EdmEntitySet entitySet = mockEntitySet(derivedEntitySetName, entityType); EdmEntityContainer container = mockContainer(derivedEntitySetName, entitySet); Edm mockedEdm = Mockito.mock(Edm.class); Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container); Mockito.when(mockedEdm.getEntityType(new FullQualifiedName("test.baseET"))).thenReturn(baseEntityType); UriTokenizer tokenizer = new UriTokenizer("navProp/test.baseET"); Expression expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, entityType, null, null); assertNotNull(expression); assertEquals("[navProp]", expression.toString()); }