[OLINGO-266] server-test module introduced

This commit is contained in:
Stephan Klevenz 2014-05-13 14:49:26 +02:00
parent 0a36331e0e
commit ee3a0d8fff
28 changed files with 11399 additions and 2 deletions

View File

@ -43,5 +43,6 @@
<module>server-api</module>
<module>server-core</module>
<module>server-tecsvc</module>
<module>server-test</module>
</modules>
</project>

View File

@ -34,6 +34,45 @@
<relativePath>..</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>
${project.build.directory}/${project.artifactId}-${project.version}.jar
</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
@ -53,7 +92,7 @@
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
@ -64,7 +103,7 @@
<artifactId>slf4j-simple</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

86
lib/server-test/pom.xml Normal file
View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>olingo-server-test</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<parent>
<groupId>org.apache.olingo</groupId>
<artifactId>olingo-lib</artifactId>
<version>0.1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>olingo-server-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>olingo-server-tecsvc</artifactId>
<version>${project.version}</version>
<!-- <type>jar</type> -->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,133 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.serializer.json;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmEntityContainer;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.commons.api.edm.EdmFunctionImport;
import org.apache.olingo.commons.api.edm.EdmSingleton;
import org.apache.olingo.server.api.ODataServer;
import org.apache.olingo.server.api.serializer.ODataFormat;
import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.junit.Before;
import org.junit.Test;
public class ServiceDocumentTest {
private Edm edm;
@Before
public void before() {
EdmEntitySet edmEntitySet1 = mock(EdmEntitySet.class);
when(edmEntitySet1.getName()).thenReturn("entitySetName1");
when(edmEntitySet1.isIncludeInServiceDocument()).thenReturn(true);
EdmEntitySet edmEntitySet2 = mock(EdmEntitySet.class);
when(edmEntitySet2.getName()).thenReturn("entitySetName2");
when(edmEntitySet2.isIncludeInServiceDocument()).thenReturn(true);
EdmEntitySet edmEntitySet3 = mock(EdmEntitySet.class);
when(edmEntitySet3.getName()).thenReturn("entitySetName3");
when(edmEntitySet3.isIncludeInServiceDocument()).thenReturn(false);
List<EdmEntitySet> entitySets = new ArrayList<EdmEntitySet>();
entitySets.add(edmEntitySet1);
entitySets.add(edmEntitySet2);
entitySets.add(edmEntitySet3);
EdmFunctionImport functionImport1 = mock(EdmFunctionImport.class);
when(functionImport1.getName()).thenReturn("functionImport1");
when(functionImport1.isIncludeInServiceDocument()).thenReturn(true);
EdmFunctionImport functionImport2 = mock(EdmFunctionImport.class);
when(functionImport2.getName()).thenReturn("functionImport2");
when(functionImport2.isIncludeInServiceDocument()).thenReturn(true);
EdmFunctionImport functionImport3 = mock(EdmFunctionImport.class);
when(functionImport3.getName()).thenReturn("functionImport3");
when(functionImport3.isIncludeInServiceDocument()).thenReturn(false);
List<EdmFunctionImport> functionImports = new ArrayList<EdmFunctionImport>();
functionImports.add(functionImport1);
functionImports.add(functionImport2);
functionImports.add(functionImport3);
EdmSingleton singleton1 = mock(EdmSingleton.class);
when(singleton1.getName()).thenReturn("singleton1");
EdmSingleton singleton2 = mock(EdmSingleton.class);
when(singleton2.getName()).thenReturn("singleton2");
EdmSingleton singleton3 = mock(EdmSingleton.class);
when(singleton3.getName()).thenReturn("singleton3");
List<EdmSingleton> singletons = new ArrayList<EdmSingleton>();
singletons.add(singleton1);
singletons.add(singleton2);
singletons.add(singleton3);
EdmEntityContainer edmEntityContainer = mock(EdmEntityContainer.class);
when(edmEntityContainer.getEntitySets()).thenReturn(entitySets);
when(edmEntityContainer.getFunctionImports()).thenReturn(functionImports);
when(edmEntityContainer.getSingletons()).thenReturn(singletons);
edm = mock(Edm.class);
when(edm.getEntityContainer(null)).thenReturn(edmEntityContainer);
}
@Test
public void writeServiceDocumentJson() throws Exception {
String serviceRoot = "http://localhost:8080/odata.svc";
ODataServer server = ODataServer.newInstance();
assertNotNull(server);
ODataSerializer serializer = server.getSerializer(ODataFormat.JSON);
assertNotNull(serializer);
InputStream result = serializer.serviceDocument(edm, serviceRoot);
assertNotNull(result);
String jsonString = IOUtils.toString(result);
assertTrue(jsonString.contains("entitySetName1"));
assertTrue(jsonString.contains("entitySetName2"));
assertFalse(jsonString.contains("entitySetName3"));
assertTrue(jsonString.contains("functionImport1"));
assertTrue(jsonString.contains("functionImport2"));
assertFalse(jsonString.contains("functionImport3"));
assertTrue(jsonString.contains("singleton1"));
assertTrue(jsonString.contains("singleton2"));
assertTrue(jsonString.contains("singleton3"));
}
}

View File

@ -0,0 +1,254 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.serializer.xml;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.olingo.commons.api.ODataException;
import org.apache.olingo.commons.api.ODataRuntimeException;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.Target;
import org.apache.olingo.server.api.ODataServer;
import org.apache.olingo.server.api.edm.provider.Action;
import org.apache.olingo.server.api.edm.provider.ActionImport;
import org.apache.olingo.server.api.edm.provider.ComplexType;
import org.apache.olingo.server.api.edm.provider.EdmProvider;
import org.apache.olingo.server.api.edm.provider.EntityContainer;
import org.apache.olingo.server.api.edm.provider.EntitySet;
import org.apache.olingo.server.api.edm.provider.EntityType;
import org.apache.olingo.server.api.edm.provider.EnumMember;
import org.apache.olingo.server.api.edm.provider.EnumType;
import org.apache.olingo.server.api.edm.provider.Function;
import org.apache.olingo.server.api.edm.provider.FunctionImport;
import org.apache.olingo.server.api.edm.provider.NavigationProperty;
import org.apache.olingo.server.api.edm.provider.NavigationPropertyBinding;
import org.apache.olingo.server.api.edm.provider.Parameter;
import org.apache.olingo.server.api.edm.provider.Property;
import org.apache.olingo.server.api.edm.provider.ReturnType;
import org.apache.olingo.server.api.edm.provider.Schema;
import org.apache.olingo.server.api.edm.provider.Singleton;
import org.apache.olingo.server.api.edm.provider.TypeDefinition;
import org.apache.olingo.server.api.serializer.ODataFormat;
import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.apache.olingo.server.core.edm.provider.EdmProviderImpl;
import org.apache.olingo.server.tecsvc.provider.EdmTechProvider;
import org.junit.Test;
public class MetadataDocumentTest {
@Test(expected = ODataRuntimeException.class)
public void metadataOnJsonResultsInException() {
ODataSerializer serializer = ODataServer.newInstance().getSerializer(ODataFormat.JSON);
serializer.metadataDocument(mock(Edm.class));
}
@Test
public void writeMetadataWithEmptyMockedEdm() {
ODataSerializer serializer = ODataServer.newInstance().getSerializer(ODataFormat.XML);
Edm edm = mock(Edm.class);
serializer.metadataDocument(edm);
}
@Test
public void writeMetadataWithLocalTestEdm() throws Exception {
ODataSerializer serializer = ODataServer.newInstance().getSerializer(ODataFormat.XML);
Edm edm = new EdmProviderImpl(new TestMetadataProvider());
InputStream metadata = serializer.metadataDocument(edm);
assertNotNull(metadata);
String metadataString = IOUtils.toString(metadata);
assertTrue(metadataString
.contains("<edmx:Edmx Version=\"4.0\" xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\">"));
assertTrue(metadataString
.contains("<Schema xmlns=\"http://docs.oasis-open.org/odata/ns/edm\" " +
"Namespace=\"namespace\" Alias=\"alias\">"));
assertTrue(metadataString
.contains("<EntityType Name=\"ETBaseName\"><Property Name=\"P1\" Type=\"Edm.Int16\"/><NavigationProperty " +
"Name=\"N1\" Type=\"namespace.ETBaseName\" Nullable=\"true\" Partner=\"N1\"/></EntityType>"));
assertTrue(metadataString
.contains("<EntityType Name=\"ETDerivedName\" BaseType=\"namespace.ETBaseName\"><Property Name=\"P2\" " +
"Type=\"Edm.Int16\"/><NavigationProperty Name=\"N2\" Type=\"namespace.ETDerivedName\" Nullable=\"true\" " +
"Partner=\"N2\"/></EntityType>"));
assertTrue(metadataString
.contains("<ComplexType Name=\"CTBaseName\"><Property Name=\"P1\" Type=\"Edm.Int16\"/><NavigationProperty " +
"Name=\"N1\" Type=\"namespace.ETBaseName\" Nullable=\"true\" Partner=\"N1\"/></ComplexType>"));
assertTrue(metadataString
.contains("<ComplexType Name=\"CTDerivedName\" BaseType=\"namespace.CTBaseName\"><Property Name=\"P2\" " +
"Type=\"Edm.Int16\"/><NavigationProperty Name=\"N2\" Type=\"namespace.ETDerivedName\" Nullable=\"true\" " +
"Partner=\"N2\"/></ComplexType>"));
assertTrue(metadataString.contains("<TypeDefinition Name=\"typeDef\" Type=\"Edm.Int16\"/>"));
assertTrue(metadataString.contains("<Action Name=\"ActionWOParameter\" IsBound=\"false\"/>"));
assertTrue(metadataString
.contains("<Action Name=\"ActionName\" IsBound=\"true\"><Parameter Name=\"param\" Type=\"Edm.Int16\"/>" +
"<Parameter Name=\"param2\" Type=\"Collection(Edm.Int16)\"/><ReturnType Type=\"namespace.CTBaseName\"/>" +
"</Action>"));
assertTrue(metadataString
.contains("<Function Name=\"FunctionWOParameter\" IsBound=\"false\" IsComposable=\"false\"><ReturnType " +
"Type=\"namespace.CTBaseName\"/></Function>"));
assertTrue(metadataString
.contains("<Function Name=\"FunctionName\" IsBound=\"true\" IsComposable=\"false\"><Parameter Name=\"param\" " +
"Type=\"Edm.Int16\"/><Parameter Name=\"param2\" Type=\"Collection(Edm.Int16)\"/><ReturnType " +
"Type=\"namespace.CTBaseName\"/></Function>"));
assertTrue(metadataString.contains("<EntityContainer Name=\"container\">"));
assertTrue(metadataString
.contains("<EntitySet Name=\"EntitySetName\" EntityType=\"namespace.ETBaseName\"><NavigationPropertyBinding " +
"Path=\"N1\" Target=\"namespace.container/EntitySetName\"/></EntitySet>"));
assertTrue(metadataString
.contains("<Singleton Name=\"SingletonName\" EntityType=\"namespace.ETBaseName\"><NavigationPropertyBinding " +
"Path=\"N1\" Target=\"namespace.container/EntitySetName\"/></Singleton>"));
assertTrue(metadataString.contains("<ActionImport Name=\"actionImport\" Action=\"namespace.ActionWOParameter\"/>"));
assertTrue(metadataString
.contains("<FunctionImport Name=\"actionImport\" Function=\"namespace.FunctionName\" " +
"EntitySet=\"namespace.EntitySetName\" IncludeInServiceDocument=\"false\"/>"));
assertTrue(metadataString.contains("</EntityContainer></Schema></edmx:DataServices></edmx:Edmx>"));
}
@Test
public void writeMetadataWithTechnicalScenario() {
ODataSerializer serializer = ODataServer.newInstance().getSerializer(ODataFormat.XML);
EdmProviderImpl edm = new EdmProviderImpl(new EdmTechProvider());
InputStream metadata = serializer.metadataDocument(edm);
assertNotNull(metadata);
// The technical scenario is too big to verify. We are content for now to make sure we can serialize it.
// System.out.println(StringUtils.inputStreamToString(metadata, false));
}
private class TestMetadataProvider extends EdmProvider {
@Override
public List<Schema> getSchemas() throws ODataException {
Property p1 = new Property().setName("P1").setType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName());
String ns = "namespace";
NavigationProperty n1 = new NavigationProperty().setName("N1")
.setType(new FullQualifiedName(ns, "ETBaseName")).setNullable(true).setPartner("N1");
Property p2 = new Property().setName("P2").setType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName());
NavigationProperty n2 = new NavigationProperty().setName("N2")
.setType(new FullQualifiedName(ns, "ETDerivedName")).setNullable(true).setPartner("N2");
Schema schema = new Schema().setNamespace(ns).setAlias("alias");
List<ComplexType> complexTypes = new ArrayList<ComplexType>();
schema.setComplexTypes(complexTypes);
ComplexType ctBase =
new ComplexType().setName("CTBaseName").setProperties(Arrays.asList(p1)).setNavigationProperties(
Arrays.asList(n1));
complexTypes.add(ctBase);
ComplexType ctDerived =
new ComplexType().setName("CTDerivedName").setBaseType(new FullQualifiedName(ns, "CTBaseName"))
.setProperties(Arrays.asList(p2)).setNavigationProperties(Arrays.asList(n2));
complexTypes.add(ctDerived);
List<EntityType> entityTypes = new ArrayList<EntityType>();
schema.setEntityTypes(entityTypes);
EntityType etBase =
new EntityType().setName("ETBaseName").setProperties(Arrays.asList(p1)).setNavigationProperties(
Arrays.asList(n1));
entityTypes.add(etBase);
EntityType etDerived =
new EntityType().setName("ETDerivedName").setBaseType(new FullQualifiedName(ns, "ETBaseName"))
.setProperties(Arrays.asList(p2)).setNavigationProperties(Arrays.asList(n2));
entityTypes.add(etDerived);
List<Action> actions = new ArrayList<Action>();
schema.setActions(actions);
// TODO:EntitySetPath
actions.add((new Action().setName("ActionWOParameter")));
List<Parameter> parameters = new ArrayList<Parameter>();
parameters.add(new Parameter().setName("param").setType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName()));
parameters.add(new Parameter().setName("param2").setType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName())
.setCollection(true));
actions.add(new Action().setName("ActionName").setBound(true).setParameters(parameters).setReturnType(
new ReturnType().setType(new FullQualifiedName(ns, "CTBaseName"))));
List<Function> functions = new ArrayList<Function>();
schema.setFunctions(functions);
functions.add((new Function().setName("FunctionWOParameter")
.setReturnType(new ReturnType().setType(new FullQualifiedName(ns, "CTBaseName")))));
functions.add(new Function().setName("FunctionName").setBound(true).setParameters(parameters).setReturnType(
new ReturnType().setType(new FullQualifiedName(ns, "CTBaseName"))));
List<EnumType> enumTypes = new ArrayList<EnumType>();
schema.setEnumTypes(enumTypes);
List<EnumMember> members = new ArrayList<EnumMember>();
members.add(new EnumMember().setName("member").setValue("1"));
enumTypes.add(new EnumType().setName("EnumName").setFlags(true).setMembers(members));
List<TypeDefinition> typeDefinitions = new ArrayList<TypeDefinition>();
schema.setTypeDefinitions(typeDefinitions);
typeDefinitions.add(new TypeDefinition().setName("typeDef")
.setUnderlyingType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName()));
EntityContainer container = new EntityContainer().setName("container");
schema.setEntityContainer(container);
List<ActionImport> actionImports = new ArrayList<ActionImport>();
container.setActionImports(actionImports);
actionImports.add(new ActionImport().setName("actionImport").setAction(
new FullQualifiedName(ns, "ActionWOParameter")).setEntitySet(
new Target().setEntityContainer(new FullQualifiedName(ns, "container")).setTargetName("EntitySetName")));
List<FunctionImport> functionImports = new ArrayList<FunctionImport>();
container.setFunctionImports(functionImports);
functionImports.add(new FunctionImport().setName("actionImport").setFunction(
new FullQualifiedName(ns, "FunctionName")).setEntitySet(
new Target().setEntityContainer(new FullQualifiedName(ns, "container")).setTargetName("EntitySetName")));
List<EntitySet> entitySets = new ArrayList<EntitySet>();
container.setEntitySets(entitySets);
List<NavigationPropertyBinding> nPB = new ArrayList<NavigationPropertyBinding>();
nPB.add(new NavigationPropertyBinding().setPath("N1").setTarget(
new Target().setEntityContainer(new FullQualifiedName(ns, "container")).setTargetName("EntitySetName")));
entitySets.add(new EntitySet().setName("EntitySetName").setType(new FullQualifiedName(ns, "ETBaseName"))
.setNavigationPropertyBindings(nPB));
List<Singleton> singletons = new ArrayList<Singleton>();
container.setSingletons(singletons);
singletons.add(new Singleton().setName("SingletonName").setType(new FullQualifiedName(ns, "ETBaseName"))
.setNavigationPropertyBindings(nPB));
List<Schema> schemas = new ArrayList<Schema>();
schemas.add(schema);
return schemas;
}
}
}

View File

@ -0,0 +1,151 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.olingo.server.core.uri.parser.RawUri;
import org.apache.olingo.server.core.uri.parser.UriDecoder;
import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException;
import org.junit.Test;
public class RawUriTest {
private RawUri runRawParser(final String uri, final int scipSegments) throws UriParserSyntaxException {
return UriDecoder.decodeUri(uri, scipSegments);
}
@Test
public void testOption() throws Exception {
RawUri rawUri;
rawUri = runRawParser("?", 0);
checkOptionCount(rawUri, 0);
rawUri = runRawParser("?a", 0);
checkOption(rawUri, 0, "a", "");
rawUri = runRawParser("?a=b", 0);
checkOption(rawUri, 0, "a", "b");
rawUri = runRawParser("?=", 0);
checkOption(rawUri, 0, "", "");
rawUri = runRawParser("?=b", 0);
checkOption(rawUri, 0, "", "b");
rawUri = runRawParser("?a&c", 0);
checkOption(rawUri, 0, "a", "");
checkOption(rawUri, 1, "c", "");
rawUri = runRawParser("?a=b&c", 0);
checkOption(rawUri, 0, "a", "b");
checkOption(rawUri, 1, "c", "");
rawUri = runRawParser("?a=b&c=d", 0);
checkOption(rawUri, 0, "a", "b");
checkOption(rawUri, 1, "c", "d");
rawUri = runRawParser("?=&=", 0);
checkOption(rawUri, 0, "", "");
checkOption(rawUri, 1, "", "");
rawUri = runRawParser("?=&c=d", 0);
checkOption(rawUri, 0, "", "");
checkOption(rawUri, 1, "c", "d");
}
private void checkOption(final RawUri rawUri, final int index, final String name, final String value) {
RawUri.QueryOption option = rawUri.queryOptionListDecoded.get(index);
assertEquals(name, option.name);
assertEquals(value, option.value);
}
private void checkOptionCount(final RawUri rawUri, final int count) {
assertEquals(count, rawUri.queryOptionListDecoded.size());
}
@Test
public void testPath() throws Exception {
RawUri rawUri;
rawUri = runRawParser("http://test.org", 0);
checkPath(rawUri, "", new ArrayList<String>());
rawUri = runRawParser("http://test.org/", 0);
checkPath(rawUri, "/", Arrays.asList(""));
rawUri = runRawParser("http://test.org/entitySet", 0);
checkPath(rawUri, "/entitySet", Arrays.asList("entitySet"));
rawUri = runRawParser("http://test.org/nonServiceSegment/entitySet", 0);
checkPath(rawUri, "/nonServiceSegment/entitySet", Arrays.asList("nonServiceSegment", "entitySet"));
rawUri = runRawParser("http://test.org/nonServiceSegment/entitySet", 1);
checkPath(rawUri, "/nonServiceSegment/entitySet", Arrays.asList("entitySet"));
rawUri = runRawParser("", 0);
checkPath(rawUri, "", new ArrayList<String>());
rawUri = runRawParser("/", 0);
checkPath(rawUri, "/", Arrays.asList(""));
rawUri = runRawParser("/entitySet", 0);
checkPath(rawUri, "/entitySet", Arrays.asList("entitySet"));
rawUri = runRawParser("entitySet", 0);
checkPath(rawUri, "entitySet", Arrays.asList("entitySet"));
rawUri = runRawParser("nonServiceSegment/entitySet", 0);
checkPath(rawUri, "nonServiceSegment/entitySet", Arrays.asList("nonServiceSegment", "entitySet"));
rawUri = runRawParser("nonServiceSegment/entitySet", 1);
checkPath(rawUri, "nonServiceSegment/entitySet", Arrays.asList("entitySet"));
rawUri = runRawParser("http://test.org/a?abc=xx+yz", 0);
}
@Test
public void testSplitt() {
UriDecoder.splitt("", '/');
UriDecoder.splitt("/", '/');
UriDecoder.splitt("a", '/');
UriDecoder.splitt("a/", '/');
UriDecoder.splitt("/a", '/');
UriDecoder.splitt("a/a", '/');
}
private void checkPath(final RawUri rawUri, final String path, final List<String> list) {
assertEquals(path, rawUri.path);
assertEquals(list.size(), rawUri.pathSegmentListDecoded.size());
int i = 0;
while (i < list.size()) {
assertEquals(list.get(i), rawUri.pathSegmentListDecoded.get(i));
i++;
}
}
}

View File

@ -0,0 +1,201 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.List;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmEntityType;
import org.apache.olingo.server.api.uri.UriInfoAll;
import org.apache.olingo.server.api.uri.UriInfoBatch;
import org.apache.olingo.server.api.uri.UriInfoCrossjoin;
import org.apache.olingo.server.api.uri.UriInfoEntityId;
import org.apache.olingo.server.api.uri.UriInfoKind;
import org.apache.olingo.server.api.uri.UriInfoMetadata;
import org.apache.olingo.server.api.uri.UriInfoResource;
import org.apache.olingo.server.api.uri.UriInfoService;
import org.apache.olingo.server.api.uri.queryoption.CustomQueryOption;
import org.apache.olingo.server.core.edm.provider.EdmProviderImpl;
import org.apache.olingo.server.core.uri.queryoption.CountOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.CustomQueryOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.ExpandOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.FilterOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.FormatOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.IdOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.LevelsOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.OrderByOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.QueryOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.SearchOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.SelectOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.SkipOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.SkipTokenOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.TopOptionImpl;
import org.apache.olingo.server.core.uri.testutil.EdmTechTestProvider;
import org.apache.olingo.server.tecsvc.provider.EntityTypeProvider;
import org.junit.Test;
public class UriInfoImplTest {
Edm edm = new EdmProviderImpl(new EdmTechTestProvider());
@Test
public void testKind() {
UriInfoImpl uriInfo = new UriInfoImpl().setKind(UriInfoKind.all);
assertEquals(UriInfoKind.all, uriInfo.getKind());
}
@Test
public void testCasts() {
UriInfoImpl uriInfo = new UriInfoImpl();
UriInfoAll all = uriInfo.asUriInfoAll();
assertEquals(uriInfo, all);
UriInfoBatch batch = uriInfo.asUriInfoBatch();
assertEquals(uriInfo, batch);
UriInfoCrossjoin crossjoin = uriInfo.asUriInfoCrossjoin();
assertEquals(uriInfo, crossjoin);
UriInfoEntityId entityID = uriInfo.asUriInfoEntityId();
assertEquals(uriInfo, entityID);
UriInfoMetadata metadata = uriInfo.asUriInfoMetadata();
assertEquals(uriInfo, metadata);
UriInfoResource resource = uriInfo.asUriInfoResource();
assertEquals(uriInfo, resource);
UriInfoService service = uriInfo.asUriInfoService();
assertEquals(uriInfo, service);
}
@Test
public void testEntityNames() {
UriInfoImpl uriInfo = new UriInfoImpl();
uriInfo.addEntitySetName("A");
uriInfo.addEntitySetName("B");
assertEquals("A", uriInfo.getEntitySetNames().get(0));
assertEquals("B", uriInfo.getEntitySetNames().get(1));
}
@Test
public void testResourceParts() {
UriInfoImpl uriInfo = new UriInfoImpl();
UriResourceActionImpl action = new UriResourceActionImpl();
UriResourceEntitySetImpl entitySet0 = new UriResourceEntitySetImpl();
UriResourceEntitySetImpl entitySet1 = new UriResourceEntitySetImpl();
uriInfo.addResourcePart(action);
uriInfo.addResourcePart(entitySet0);
assertEquals(action, uriInfo.getUriResourceParts().get(0));
assertEquals(entitySet0, uriInfo.getUriResourceParts().get(1));
assertEquals(entitySet0, uriInfo.getLastResourcePart());
uriInfo.addResourcePart(entitySet1);
assertEquals(entitySet1, uriInfo.getLastResourcePart());
}
@Test
public void testCustomQueryOption() {
UriInfoImpl uriInfo = new UriInfoImpl();
List<QueryOptionImpl> queryOptions = new ArrayList<QueryOptionImpl>();
ExpandOptionImpl expand = new ExpandOptionImpl();
FilterOptionImpl filter = new FilterOptionImpl();
FormatOptionImpl format = new FormatOptionImpl();
IdOptionImpl id = new IdOptionImpl();
CountOptionImpl inlinecount = new CountOptionImpl();
OrderByOptionImpl orderby = new OrderByOptionImpl();
SearchOptionImpl search = new SearchOptionImpl();
SelectOptionImpl select = new SelectOptionImpl();
SkipOptionImpl skip = new SkipOptionImpl();
SkipTokenOptionImpl skipToken = new SkipTokenOptionImpl();
TopOptionImpl top = new TopOptionImpl();
LevelsOptionImpl levels = new LevelsOptionImpl();
CustomQueryOptionImpl customOption0 = new CustomQueryOptionImpl();
customOption0.setText("A");
CustomQueryOptionImpl customOption1 = new CustomQueryOptionImpl();
customOption1.setText("B");
QueryOptionImpl queryOption = new QueryOptionImpl();
queryOptions.add(expand);
queryOptions.add(filter);
queryOptions.add(format);
queryOptions.add(id);
queryOptions.add(inlinecount);
queryOptions.add(orderby);
queryOptions.add(search);
queryOptions.add(select);
queryOptions.add(skip);
queryOptions.add(skipToken);
queryOptions.add(top);
queryOptions.add(customOption0);
queryOptions.add(customOption1);
queryOptions.add(levels);// not stored
queryOptions.add(queryOption);// not stored
uriInfo.setQueryOptions(queryOptions);
assertEquals(expand, uriInfo.getExpandOption());
assertEquals(filter, uriInfo.getFilterOption());
assertEquals(format, uriInfo.getFormatOption());
assertEquals(id, uriInfo.getIdOption());
assertEquals(inlinecount, uriInfo.getCountOption());
assertEquals(orderby, uriInfo.getOrderByOption());
assertEquals(search, uriInfo.getSearchOption());
assertEquals(select, uriInfo.getSelectOption());
assertEquals(skip, uriInfo.getSkipOption());
assertEquals(skipToken, uriInfo.getSkipTokenOption());
assertEquals(top, uriInfo.getTopOption());
List<CustomQueryOption> customQueryOptions = uriInfo.getCustomQueryOptions();
assertEquals(customOption0, customQueryOptions.get(0));
assertEquals(customOption1, customQueryOptions.get(1));
}
@Test
public void testFragment() {
UriInfoImpl uriInfo = new UriInfoImpl();
uriInfo.setFragment("F");
assertEquals("F", uriInfo.getFragment());
}
@Test
public void testEntityTypeCast() {
UriInfoImpl uriInfo = new UriInfoImpl();
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETKeyNav);
assertNotNull(entityType);
uriInfo.setEntityTypeCast(entityType);
assertEquals(entityType, uriInfo.getEntityTypeCast());
}
}

View File

@ -0,0 +1,508 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmAction;
import org.apache.olingo.commons.api.edm.EdmActionImport;
import org.apache.olingo.commons.api.edm.EdmEntityType;
import org.apache.olingo.commons.api.edm.EdmFunction;
import org.apache.olingo.commons.api.edm.EdmFunctionImport;
import org.apache.olingo.commons.api.edm.EdmNavigationProperty;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.EdmProperty;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory;
import org.apache.olingo.server.api.uri.UriResourceKind;
import org.apache.olingo.server.core.edm.provider.EdmComplexTypeImpl;
import org.apache.olingo.server.core.edm.provider.EdmEntitySetImpl;
import org.apache.olingo.server.core.edm.provider.EdmProviderImpl;
import org.apache.olingo.server.core.edm.provider.EdmSingletonImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.ExpressionImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.LiteralImpl;
import org.apache.olingo.server.core.uri.testutil.EdmTechTestProvider;
import org.apache.olingo.server.tecsvc.provider.ActionProvider;
import org.apache.olingo.server.tecsvc.provider.ComplexTypeProvider;
import org.apache.olingo.server.tecsvc.provider.EntityTypeProvider;
import org.junit.Test;
public class UriResourceImplTest {
Edm edm = new EdmProviderImpl(new EdmTechTestProvider());
@Test
public void testUriParameterImpl() {
UriParameterImpl impl = new UriParameterImpl();
ExpressionImpl expression = new LiteralImpl().setText("Expression");
impl.setText("Text");
impl.setName("A");
impl.setAlias("@A");
impl.setExpression(expression);
assertEquals("Text", impl.getText());
assertEquals("A", impl.getName());
assertEquals("@A", impl.getAlias());
assertEquals(expression, impl.getExression());
}
@Test
public void testUriResourceActionImpl() {
UriResourceActionImpl impl = new UriResourceActionImpl();
assertEquals(UriResourceKind.action, impl.getKind());
assertEquals("", impl.toString());
// action
EdmAction action = edm.getUnboundAction(ActionProvider.nameUARTETParam);
impl.setAction(action);
assertEquals(action, impl.getAction());
assertEquals(ActionProvider.nameUARTETParam.getName(), impl.toString());
// action import
impl = new UriResourceActionImpl();
EdmActionImport actionImport = edm.getEntityContainer(null).getActionImport("AIRTPrimParam");
impl.setActionImport(actionImport);
assertEquals(actionImport, impl.getActionImport());
assertEquals(actionImport.getUnboundAction(), impl.getAction());
assertEquals(false, impl.isCollection());
assertEquals("AIRTPrimParam", impl.toString());
assertEquals(actionImport.getUnboundAction().getReturnType().getType(), impl.getType());
}
@Test
public void testUriResourceLambdaAllImpl() {
UriResourceLambdaAllImpl impl = new UriResourceLambdaAllImpl();
assertEquals(UriResourceKind.lambdaAll, impl.getKind());
ExpressionImpl expression = new LiteralImpl().setText("Expression");
impl.setExpression(expression);
impl.setLamdaVariable("A");
assertEquals(false, impl.isCollection());
assertEquals(expression, impl.getExpression());
assertEquals("A", impl.getLambdaVariable());
assertEquals(EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Boolean), impl.getType());
assertEquals("all", impl.toString());
}
@Test
public void testUriResourceLambdaAnyImpl() {
UriResourceLambdaAnyImpl impl = new UriResourceLambdaAnyImpl();
assertEquals(UriResourceKind.lambdaAny, impl.getKind());
ExpressionImpl expression = new LiteralImpl().setText("Expression");
impl.setExpression(expression);
impl.setLamdaVariable("A");
assertEquals(false, impl.isCollection());
assertEquals(expression, impl.getExpression());
assertEquals("A", impl.getLamdaVariable());
assertEquals(EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Boolean), impl.getType());
assertEquals("any", impl.toString());
}
@Test
public void testUriResourceComplexPropertyImpl() {
UriResourceComplexPropertyImpl impl = new UriResourceComplexPropertyImpl();
assertEquals(UriResourceKind.complexProperty, impl.getKind());
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETKeyNav);
EdmProperty property = (EdmProperty) entityType.getProperty("PropertyComplex");
impl.setProperty(property);
assertEquals(property, impl.getProperty());
assertEquals(property.getName(), impl.toString());
assertEquals(false, impl.isCollection());
assertEquals(property.getType(), impl.getType());
assertEquals(property.getType(), impl.getComplexType());
impl.getComplexType();
EdmComplexTypeImpl complexTypeImplType =
(EdmComplexTypeImpl) edm.getComplexType(ComplexTypeProvider.nameCTBasePrimCompNav);
impl.setTypeFilter(complexTypeImplType);
assertEquals(complexTypeImplType, impl.getTypeFilter());
assertEquals(complexTypeImplType, impl.getComplexTypeFilter());
impl.getComplexTypeFilter();
}
@Test
public void testUriResourcePrimitivePropertyImpl() {
UriResourcePrimitivePropertyImpl impl = new UriResourcePrimitivePropertyImpl();
assertEquals(UriResourceKind.primitiveProperty, impl.getKind());
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETKeyNav);
EdmProperty property = (EdmProperty) entityType.getProperty("PropertyInt16");
impl.setProperty(property);
assertEquals(property, impl.getProperty());
assertEquals(property.getName(), impl.toString());
assertEquals(false, impl.isCollection());
assertEquals(property.getType(), impl.getType());
}
@Test
public void testUriResourceCountImpl() {
UriResourceCountImpl impl = new UriResourceCountImpl();
assertEquals(UriResourceKind.count, impl.getKind());
assertEquals("$count", impl.toString());
}
@Test
public void testUriResourceEntitySetImpl() {
UriResourceEntitySetImpl impl = new UriResourceEntitySetImpl();
assertEquals(UriResourceKind.entitySet, impl.getKind());
EdmEntitySetImpl entitySet = (EdmEntitySetImpl) edm.getEntityContainer(null).getEntitySet("ESAllPrim");
impl.setEntitSet(entitySet);
assertEquals("ESAllPrim", impl.toString());
assertEquals(entitySet, impl.getEntitySet());
assertEquals(entitySet.getEntityType(), impl.getType());
assertEquals(entitySet.getEntityType(), impl.getEntityType());
impl.getEntityType();
// is Collection
assertEquals(true, impl.isCollection());
impl.setKeyPredicates(new ArrayList<UriParameterImpl>());
assertEquals(false, impl.isCollection());
}
@Test
public void testUriResourceFunctionImpl() {
UriResourceFunctionImpl impl = new UriResourceFunctionImpl();
assertEquals(UriResourceKind.function, impl.getKind());
assertEquals("", impl.toString());
// function
EdmFunction function = (EdmFunction) edm.getEntityContainer(null).getFunctionImport("FINRTInt16")
.getUnboundFunction(new ArrayList<String>());
assertNotNull(function);
impl.setFunction(function);
assertEquals(function, impl.getFunction());
assertEquals("UFNRTInt16", impl.toString());
assertEquals(function.getReturnType().getType(), impl.getType());
assertEquals(false, impl.isParameterListFilled());
// function import
impl = new UriResourceFunctionImpl();
EdmFunctionImport functionImport = edm.getEntityContainer(null).getFunctionImport("FINRTInt16");
impl.setFunctionImport(functionImport, new ArrayList<UriParameterImpl>());
assertEquals(functionImport, impl.getFunctionImport());
assertEquals("FINRTInt16", impl.toString());
// function collection
impl = new UriResourceFunctionImpl();
functionImport = edm.getEntityContainer(null).getFunctionImport("FICRTESTwoKeyNavParam");
assertNotNull(function);
UriParameterImpl parameter = new UriParameterImpl().setName("ParameterInt16");
impl.setFunctionImport(functionImport, Arrays.asList(parameter));
assertEquals("FICRTESTwoKeyNavParam", impl.toString());
impl.setFunction(functionImport.getUnboundFunction(Arrays.asList("ParameterInt16")));
assertEquals(true, impl.isCollection());
impl.setKeyPredicates(new ArrayList<UriParameterImpl>());
assertEquals(false, impl.isCollection());
assertEquals(parameter, impl.getParameters().get(0));
assertEquals(true, impl.isParameterListFilled());
}
@Test
public void testUriResourceImplKeyPred() {
class Mock extends UriResourceWithKeysImpl {
EdmType type;
public Mock() {
super(UriResourceKind.action);
}
@Override
public EdmType getType() {
return type;
}
public Mock setType(final EdmType type) {
this.type = type;
return this;
}
@Override
public boolean isCollection() {
return false;
}
@Override
public String toString() {
return "mock";
}
}
Mock impl = new Mock();
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETTwoKeyNav);
EdmEntityType entityTypeBaseColl = edm.getEntityType(EntityTypeProvider.nameETBaseTwoKeyNav);
EdmEntityType entityTypeBaseEntry = edm.getEntityType(EntityTypeProvider.nameETTwoBaseTwoKeyNav);
impl.setType(entityType);
assertEquals(entityType, impl.getType());
assertEquals("mock", impl.toString(false));
assertEquals("mock", impl.toString(true));
// set both
impl.setCollectionTypeFilter(entityTypeBaseColl);
assertEquals(entityTypeBaseColl, impl.getTypeFilterOnCollection());
assertEquals("mock", impl.toString(false));
assertEquals("mock/com.sap.odata.test1.ETBaseTwoKeyNav", impl.toString(true));
impl.setEntryTypeFilter(entityTypeBaseEntry);
assertEquals(entityTypeBaseEntry, impl.getTypeFilterOnEntry());
assertEquals("mock", impl.toString(false));
assertEquals("mock/com.sap.odata.test1.ETBaseTwoKeyNav/()com.sap.odata.test1.ETTwoBaseTwoKeyNav",
impl.toString(true));
// set entry
impl = new Mock();
impl.setType(entityType);
impl.setEntryTypeFilter(entityTypeBaseEntry);
assertEquals(entityTypeBaseEntry, impl.getTypeFilterOnEntry());
assertEquals("mock", impl.toString(false));
assertEquals("mock/com.sap.odata.test1.ETTwoBaseTwoKeyNav", impl.toString(true));
// set collection
impl = new Mock();
impl.setType(entityType);
impl.setCollectionTypeFilter(entityTypeBaseColl);
assertEquals(entityTypeBaseColl, impl.getTypeFilterOnCollection());
assertEquals("mock", impl.toString(false));
assertEquals("mock/com.sap.odata.test1.ETBaseTwoKeyNav", impl.toString(true));
impl = new Mock();
UriParameterImpl parameter = new UriParameterImpl().setName("ParameterInt16");
List<UriParameterImpl> keyPredicates = new ArrayList<UriParameterImpl>();
keyPredicates.add(parameter);
impl.setKeyPredicates(keyPredicates);
assertNotNull(null, impl.getKeyPredicates());
}
@Test
public void testUriResourceImplTyped() {
class Mock extends UriResourceTypedImpl {
EdmType type;
public Mock() {
super(UriResourceKind.action);
}
@Override
public EdmType getType() {
return type;
}
@Override
public boolean isCollection() {
return false;
}
public Mock setType(final EdmType type) {
this.type = type;
return this;
}
@Override
public String toString() {
return "mock";
}
}
Mock impl = new Mock();
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETTwoKeyNav);
EdmEntityType entityTypeBaseColl = edm.getEntityType(EntityTypeProvider.nameETBaseTwoKeyNav);
edm.getEntityType(EntityTypeProvider.nameETTwoBaseTwoKeyNav);
impl.setType(entityType);
assertEquals("mock", impl.toString());
assertEquals("mock", impl.toString(true));
assertEquals("mock", impl.toString(false));
impl.setTypeFilter(entityTypeBaseColl);
assertEquals(entityTypeBaseColl, impl.getTypeFilter());
assertEquals("mock", impl.toString());
assertEquals("mock/com.sap.odata.test1.ETBaseTwoKeyNav", impl.toString(true));
assertEquals("mock", impl.toString(false));
//
}
@Test
public void testUriResourceItImpl() {
UriResourceItImpl impl = new UriResourceItImpl();
assertEquals(UriResourceKind.it, impl.getKind());
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETTwoKeyNav);
assertEquals("$it", impl.toString());
impl.setType(entityType);
assertEquals(entityType, impl.getType());
UriParameterImpl parameter = new UriParameterImpl().setName("ParameterInt16");
List<UriParameterImpl> keyPredicates = new ArrayList<UriParameterImpl>();
keyPredicates.add(parameter);
assertEquals(false, impl.isCollection());
impl.setCollection(true);
assertEquals(true, impl.isCollection());
impl.setKeyPredicates(keyPredicates);
assertEquals(false, impl.isCollection());
}
@Test
public void testUriResourceNavigationPropertyImpl() {
UriResourceNavigationPropertyImpl impl = new UriResourceNavigationPropertyImpl();
assertEquals(UriResourceKind.navigationProperty, impl.getKind());
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETTwoKeyNav);
EdmNavigationProperty property = (EdmNavigationProperty) entityType.getProperty("NavPropertyETKeyNavMany");
assertNotNull(property);
impl.setNavigationProperty(property);
assertEquals(property, impl.getProperty());
assertEquals("NavPropertyETKeyNavMany", impl.toString());
assertEquals(property.getType(), impl.getType());
UriParameterImpl parameter = new UriParameterImpl().setName("ParameterInt16");
List<UriParameterImpl> keyPredicates = new ArrayList<UriParameterImpl>();
keyPredicates.add(parameter);
assertEquals(true, impl.isCollection());
impl.setKeyPredicates(keyPredicates);
assertEquals(false, impl.isCollection());
}
@Test
public void testUriResourceRefImpl() {
UriResourceRefImpl impl = new UriResourceRefImpl();
assertEquals(UriResourceKind.ref, impl.getKind());
assertEquals("$ref", impl.toString());
}
@Test
public void testUriResourceRootImpl() {
UriResourceRootImpl impl = new UriResourceRootImpl();
assertEquals(UriResourceKind.root, impl.getKind());
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETTwoKeyNav);
assertEquals("$root", impl.toString());
impl.setType(entityType);
assertEquals(entityType, impl.getType());
UriParameterImpl parameter = new UriParameterImpl().setName("ParameterInt16");
List<UriParameterImpl> keyPredicates = new ArrayList<UriParameterImpl>();
keyPredicates.add(parameter);
assertEquals(false, impl.isCollection());
impl.setCollection(true);
assertEquals(true, impl.isCollection());
impl.setKeyPredicates(keyPredicates);
assertEquals(false, impl.isCollection());
}
@Test
public void testUriResourceSingletonImpl() {
UriResourceSingletonImpl impl = new UriResourceSingletonImpl();
assertEquals(UriResourceKind.singleton, impl.getKind());
EdmSingletonImpl singleton = (EdmSingletonImpl) edm.getEntityContainer(null).getSingleton("SINav");
EdmEntityType entityTypeBaseColl = edm.getEntityType(EntityTypeProvider.nameETBaseTwoKeyNav);
impl.setSingleton(singleton);
assertEquals("SINav", impl.toString());
assertEquals(singleton, impl.getSingleton());
assertEquals(singleton.getEntityType(), impl.getType());
assertEquals(singleton.getEntityType(), impl.getEntityType());
impl.getEntityType();
impl.setTypeFilter(entityTypeBaseColl);
assertEquals(entityTypeBaseColl, impl.getEntityTypeFilter());
// is Collection
assertEquals(false, impl.isCollection());
}
@Test
public void testUriResourceValueImpl() {
UriResourceValueImpl impl = new UriResourceValueImpl();
assertEquals(UriResourceKind.value, impl.getKind());
assertEquals("$value", impl.toString());
}
@Test
public void testUriResourceLambdaVarImpl() {
UriResourceLambdaVarImpl impl = new UriResourceLambdaVarImpl();
assertEquals(UriResourceKind.lambdaVariable, impl.getKind());
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETTwoKeyNav);
impl.setType(entityType);
impl.setVariableText("A");
assertEquals("A", impl.toString());
assertEquals(entityType, impl.getType());
assertEquals("A", impl.getVariableName());
assertEquals(false, impl.isCollection());
impl.setCollection(true);
assertEquals(true, impl.isCollection());
}
@Test
public void testUriResourceStartingTypeFilterImpl() {
UriResourceStartingTypeFilterImpl impl = new UriResourceStartingTypeFilterImpl();
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETTwoKeyNav);
impl.setType(entityType);
assertEquals("com.sap.odata.test1.ETTwoKeyNav", impl.toString());
assertEquals(entityType, impl.getType());
UriParameterImpl parameter = new UriParameterImpl().setName("ParameterInt16");
List<UriParameterImpl> keyPredicates = new ArrayList<UriParameterImpl>();
keyPredicates.add(parameter);
assertEquals(false, impl.isCollection());
impl.setCollection(true);
assertEquals(true, impl.isCollection());
impl.setKeyPredicates(keyPredicates);
assertEquals(false, impl.isCollection());
}
}

View File

@ -0,0 +1,248 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.antlr;
import org.antlr.v4.runtime.Lexer;
import org.apache.olingo.server.core.uri.testutil.TokenValidator;
import org.junit.Test;
public class TestLexer {
private TokenValidator test = null;
private static final String cPCT_ENCODED = "%45%46%47" + "%22" + "%5C";// last two chars are not in
// cPCT_ENCODED_UNESCAPED
private static final String cUNRESERVED = "ABCabc123-._~";
private static final String cOTHER_DELIMS = "!()*+,;";
private static final String cSUB_DELIMS = "$&'=" + cOTHER_DELIMS;
private static final String cPCHAR = cUNRESERVED + cPCT_ENCODED + cSUB_DELIMS + ":@";
public TestLexer() {
test = new TokenValidator();
}
@Test
public void test() {
// test.log(1).run("ESAllPrim?$orderby=PropertyDouble eq 3.5E+38");
}
// ;------------------------------------------------------------------------------
// ; 0. URI
// ;------------------------------------------------------------------------------
@Test
public void testUriTokens() {
test.globalMode(UriLexer.MODE_QUERY);
test.run("#").isText("#").isType(UriLexer.FRAGMENT);
test.run("$count").isText("$count").isType(UriLexer.COUNT);
test.run("$ref").isText("$ref").isType(UriLexer.REF);
test.run("$value").isText("$value").isType(UriLexer.VALUE);
}
// ;------------------------------------------------------------------------------
// ; 2. Query Options
// ;------------------------------------------------------------------------------
@Test
public void testQueryOptionsTokens() {
test.globalMode(UriLexer.MODE_QUERY);
test.run("$skip=1").isAllText("$skip=1").isType(UriLexer.SKIP);
test.run("$skip=2").isAllText("$skip=2").isType(UriLexer.SKIP);
test.run("$skip=123").isAllText("$skip=123").isType(UriLexer.SKIP);
test.run("$top=1").isAllText("$top=1").isType(UriLexer.TOP);
test.run("$top=2").isAllText("$top=2").isType(UriLexer.TOP);
test.run("$top=123").isAllText("$top=123").isType(UriLexer.TOP);
test.run("$levels=1").isAllText("$levels=1").isType(UriLexer.LEVELS);
test.run("$levels=2").isAllText("$levels=2").isType(UriLexer.LEVELS);
test.run("$levels=123").isAllText("$levels=123").isType(UriLexer.LEVELS);
test.run("$levels=max").isAllText("$levels=max").isType(UriLexer.LEVELS);
test.run("$format=atom").isAllText("$format=atom").isType(UriLexer.FORMAT);
test.run("$format=json").isAllText("$format=json").isType(UriLexer.FORMAT);
test.run("$format=xml").isAllText("$format=xml").isType(UriLexer.FORMAT);
test.run("$format=abc/def").isAllText("$format=abc/def").isType(UriLexer.FORMAT);
test.run("$id=123").isAllText("$id=123").isType(UriLexer.ID);
test.run("$id=ABC").isAllText("$id=ABC").isType(UriLexer.ID);
test.run("$skiptoken=ABC").isAllText("$skiptoken=ABC").isType(UriLexer.SKIPTOKEN);
test.run("$skiptoken=ABC").isAllText("$skiptoken=ABC").isType(UriLexer.SKIPTOKEN);
test.run("$search=\"ABC\"").isAllText("$search=\"ABC\"").isType(UriLexer.SEARCH);
test.run("$search=ABC").isAllText("$search=ABC").isType(UriLexer.SEARCH);
test.run("$search=\"A%20B%20C\"").isAllText("$search=\"A%20B%20C\"").isType(UriLexer.SEARCH);
}
// ;------------------------------------------------------------------------------
// ; 4. Expressions
// ;------------------------------------------------------------------------------
@Test
public void testQueryExpressions() {
test.globalMode(Lexer.DEFAULT_MODE);
test.run("$it").isText("$it").isType(UriLexer.IT);
test.run("$filter=contains(").at(2).isText("contains(").isType(UriLexer.CONTAINS_WORD);
test.run("$filter=containsabc").at(2).isText("containsabc")
.isType(UriLexer.ODATAIDENTIFIER); // test that this is a ODI
test.run("$filter=startswith(").at(2).isText("startswith(").isType(UriLexer.STARTSWITH_WORD);
test.run("$filter=endswith(").at(2).isText("endswith(").isType(UriLexer.ENDSWITH_WORD);
test.run("$filter=length(").at(2).isText("length(").isType(UriLexer.LENGTH_WORD);
test.run("$filter=indexof(").at(2).isText("indexof(").isType(UriLexer.INDEXOF_WORD);
test.run("$filter=substring(").at(2).isText("substring(").isType(UriLexer.SUBSTRING_WORD);
test.run("$filter=tolower(").at(2).isText("tolower(").isType(UriLexer.TOLOWER_WORD);
test.run("$filter=toupper(").at(2).isText("toupper(").isType(UriLexer.TOUPPER_WORD);
test.run("$filter=trim(").at(2).isText("trim(").isType(UriLexer.TRIM_WORD);
test.run("$filter=concat(").at(2).isText("concat(").isType(UriLexer.CONCAT_WORD);
}
// ;------------------------------------------------------------------------------
// ; 7. Literal Data Values
// ;------------------------------------------------------------------------------
@Test
public void testLiteralDataValues() {
test.globalMode(Lexer.DEFAULT_MODE);
// null
test.run("null").isInput().isType(UriLexer.NULLVALUE);
// binary
test.run("binary'ABCD'").isInput().isType(UriLexer.BINARY);
test.run("BiNaRy'ABCD'").isInput().isType(UriLexer.BINARY);
// boolean
test.run("true").isInput().isType(UriLexer.TRUE);
test.run("false").isInput().isType(UriLexer.FALSE);
test.run("TrUe").isInput().isType(UriLexer.BOOLEAN);
test.run("FaLsE").isInput().isType(UriLexer.BOOLEAN);
// Lexer rule INT
test.run("123").isInput().isType(UriLexer.INT);
test.run("123456789").isInput().isType(UriLexer.INT);
test.run("+123").isInput().isType(UriLexer.INT);
test.run("+123456789").isInput().isType(UriLexer.INT);
test.run("-123").isInput().isType(UriLexer.INT);
test.run("-123456789").isInput().isType(UriLexer.INT);
// Lexer rule DECIMAL
test.run("0.1").isInput().isType(UriLexer.DECIMAL);
test.run("1.1").isInput().isType(UriLexer.DECIMAL);
test.run("+0.1").isInput().isType(UriLexer.DECIMAL);
test.run("+1.1").isInput().isType(UriLexer.DECIMAL);
test.run("-0.1").isInput().isType(UriLexer.DECIMAL);
test.run("-1.1").isInput().isType(UriLexer.DECIMAL);
// Lexer rule EXP
test.run("1.1e+1").isInput().isType(UriLexer.DECIMAL);
test.run("1.1e-1").isInput().isType(UriLexer.DECIMAL);
test.run("NaN").isInput().isType(UriLexer.NANINFINITY);
test.run("-INF").isInput().isType(UriLexer.NANINFINITY);
test.run("INF").isInput().isType(UriLexer.NANINFINITY);
// Lexer rule GUID
test.run("1234ABCD-12AB-23CD-45EF-123456780ABC").isInput().isType(UriLexer.GUID);
test.run("1234ABCD-12AB-23CD-45EF-123456780ABC").isInput().isType(UriLexer.GUID);
// Lexer rule DATE
test.run("2013-11-15").isInput().isType(UriLexer.DATE);
// Lexer rule DATETIMEOFFSET
test.run("2013-11-15T13:35Z").isInput().isType(UriLexer.DATETIMEOFFSET);
test.run("2013-11-15T13:35:10Z").isInput().isType(UriLexer.DATETIMEOFFSET);
test.run("2013-11-15T13:35:10.1234Z").isInput().isType(UriLexer.DATETIMEOFFSET);
test.run("2013-11-15T13:35:10.1234+01:30").isInput().isType(UriLexer.DATETIMEOFFSET);
test.run("2013-11-15T13:35:10.1234-01:12").isInput().isType(UriLexer.DATETIMEOFFSET);
test.run("2013-11-15T13:35Z").isInput().isType(UriLexer.DATETIMEOFFSET);
// Lexer rule DURATION
test.run("duration'PT67S'").isInput().isType(UriLexer.DURATION);
test.run("duration'PT67.89S'").isInput().isType(UriLexer.DURATION);
test.run("duration'PT5M'").isInput().isType(UriLexer.DURATION);
test.run("duration'PT5M67S'").isInput().isType(UriLexer.DURATION);
test.run("duration'PT5M67.89S'").isInput().isType(UriLexer.DURATION);
test.run("duration'PT4H'").isInput().isType(UriLexer.DURATION);
test.run("duration'PT4H67S'").isInput().isType(UriLexer.DURATION);
test.run("duration'PT4H67.89S'").isInput().isType(UriLexer.DURATION);
test.run("duration'PT4H5M'").isInput().isType(UriLexer.DURATION);
test.run("duration'PT4H5M67S'").isInput().isType(UriLexer.DURATION);
test.run("duration'PT4H5M67.89S'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3D'");
test.run("duration'P3DT67S'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3DT67.89S'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3DT5M'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3DT5M67S'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3DT5M67.89S'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3DT4H'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3DT4H67S'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3DT4H67.89S'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3DT4H5M'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3DT4H5M67S'").isInput().isType(UriLexer.DURATION);
test.run("duration'P3DT4H5M67.89S'").isInput().isType(UriLexer.DURATION);
test.run("DuRaTiOn'P3DT4H5M67.89S'").isInput().isType(UriLexer.DURATION);
test.run("DuRaTiOn'-P3DT4H5M67.89S'").isInput().isType(UriLexer.DURATION);
test.run("20:00").isInput().isType(UriLexer.TIMEOFDAY);
test.run("20:15:01").isInput().isType(UriLexer.TIMEOFDAY);
test.run("20:15:01.02").isInput().isType(UriLexer.TIMEOFDAY);
test.run("20:15:01.02").isInput().isType(UriLexer.TIMEOFDAY);
// String
test.run("'ABC'").isText("'ABC'").isType(UriLexer.STRING);
test.run("'A%20C'").isInput().isType(UriLexer.STRING);
test.run("'%20%20%20ABC'").isInput().isType(UriLexer.STRING);
}
@Test
public void testDelims() {
String reserved = "/";
test.globalMode(UriLexer.MODE_QUERY);
// Test lexer rule UNRESERVED
test.run("$format=A/" + cUNRESERVED).isAllInput().isType(UriLexer.FORMAT);
test.run("$format=A/" + cUNRESERVED + reserved).isType(UriLexer.FORMAT).at(4).isText(cUNRESERVED);
// Test lexer rule PCT_ENCODED
test.run("$format=A/" + cPCT_ENCODED).isAllInput().isType(UriLexer.FORMAT);
test.run("$format=A/" + cPCT_ENCODED + reserved).isType(UriLexer.FORMAT).at(4).isText(cPCT_ENCODED);
// Test lexer rule SUB_DELIMS
test.run("$format=A/" + cSUB_DELIMS).isAllInput().isType(UriLexer.FORMAT);
test.run("$format=A/" + cSUB_DELIMS + reserved).isType(UriLexer.FORMAT).at(4).isText("$");
// Test lexer rule PCHAR rest
test.run("$format=A/:@").isAllText("$format=A/:@").isType(UriLexer.FORMAT);
test.run("$format=A/:@" + reserved).isType(UriLexer.FORMAT).at(4).isText(":@");
// Test lexer rule PCHAR all
test.run("$format=" + cPCHAR + "/" + cPCHAR).isAllInput().isType(UriLexer.FORMAT);
}
}

View File

@ -0,0 +1,303 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.queryoption;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.server.api.uri.UriInfoResource;
import org.apache.olingo.server.api.uri.queryoption.SystemQueryOptionKind;
import org.apache.olingo.server.core.edm.provider.EdmProviderImpl;
import org.apache.olingo.server.core.uri.UriInfoImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.AliasImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.ExpressionImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.LiteralImpl;
import org.apache.olingo.server.core.uri.testutil.EdmTechTestProvider;
import org.junit.Test;
//TOOD add getKind check to all
public class QueryOptionTest {
Edm edm = new EdmProviderImpl(new EdmTechTestProvider());
@Test
public void testAliasQueryOption() {
AliasQueryOptionImpl option = new AliasQueryOptionImpl();
ExpressionImpl expression = new LiteralImpl();
option.setAliasValue(expression);
assertEquals(expression, option.getValue());
}
@Test
public void testExandItemImpl() {
ExpandItemImpl option = new ExpandItemImpl();
// input options
ExpandOptionImpl expand = new ExpandOptionImpl();
FilterOptionImpl filter = new FilterOptionImpl();
CountOptionImpl inlinecount = new CountOptionImpl();
OrderByOptionImpl orderby = new OrderByOptionImpl();
SearchOptionImpl search = new SearchOptionImpl();
SelectOptionImpl select = new SelectOptionImpl();
SkipOptionImpl skip = new SkipOptionImpl();
TopOptionImpl top = new TopOptionImpl();
LevelsOptionImpl levels = new LevelsOptionImpl();
option.setSystemQueryOption(expand);
option.setSystemQueryOption(filter);
option.setSystemQueryOption(inlinecount);
option.setSystemQueryOption(orderby);
option.setSystemQueryOption(search);
option.setSystemQueryOption(select);
option.setSystemQueryOption(skip);
option.setSystemQueryOption(top);
option.setSystemQueryOption(levels);
assertEquals(expand, option.getExpandOption());
assertEquals(filter, option.getFilterOption());
assertEquals(inlinecount, option.getCountOption());
assertEquals(orderby, option.getOrderByOption());
assertEquals(search, option.getSearchOption());
assertEquals(select, option.getSelectOption());
assertEquals(skip, option.getSkipOption());
assertEquals(top, option.getTopOption());
assertEquals(levels, option.getLevelsOption());
// just for completeness
option = new ExpandItemImpl();
option.setSystemQueryOption(new IdOptionImpl());
option = new ExpandItemImpl();
List<SystemQueryOptionImpl> list = new ArrayList<SystemQueryOptionImpl>();
list.add(expand);
list.add(filter);
option.setSystemQueryOptions(list);
assertEquals(expand, option.getExpandOption());
assertEquals(filter, option.getFilterOption());
option = new ExpandItemImpl();
assertEquals(false, option.isRef());
option.setIsRef(true);
assertEquals(true, option.isRef());
option = new ExpandItemImpl();
assertEquals(false, option.isStar());
option.setIsStar(true);
assertEquals(true, option.isStar());
option = new ExpandItemImpl();
UriInfoResource resource = new UriInfoImpl().asUriInfoResource();
option.setResourcePath(resource);
assertEquals(resource, option.getResourcePath());
}
@Test
public void testExpandOptionImpl() {
ExpandOptionImpl option = new ExpandOptionImpl();
assertEquals(SystemQueryOptionKind.EXPAND, option.getKind());
ExpandItemImpl item1 = new ExpandItemImpl();
ExpandItemImpl item2 = new ExpandItemImpl();
option.addExpandItem(item1);
option.addExpandItem(item2);
assertEquals(item1, option.getExpandItems().get(0));
assertEquals(item2, option.getExpandItems().get(1));
}
@Test
public void testFilterOptionImpl() {
FilterOptionImpl option = new FilterOptionImpl();
assertEquals(SystemQueryOptionKind.FILTER, option.getKind());
AliasImpl expression = new AliasImpl();
option.setExpression(expression);
assertEquals(expression, option.getExpression());
}
@Test
public void testFormatOptionImpl() {
FormatOptionImpl option = new FormatOptionImpl();
assertEquals(SystemQueryOptionKind.FORMAT, option.getKind());
option.setFormat("A");
assertEquals("A", option.getFormat());
}
@Test
public void testIdOptionImpl() {
IdOptionImpl option = new IdOptionImpl();
assertEquals(SystemQueryOptionKind.ID, option.getKind());
option.setValue("A");
assertEquals("A", option.getValue());
}
@Test
public void testInlineCountImpl() {
CountOptionImpl option = new CountOptionImpl();
assertEquals(SystemQueryOptionKind.COUNT, option.getKind());
assertEquals(false, option.getValue());
option.setValue(true);
assertEquals(true, option.getValue());
}
@Test
public void testLevelsExpandOptionImpl() {
LevelsOptionImpl option = new LevelsOptionImpl();
assertEquals(SystemQueryOptionKind.LEVELS, option.getKind());
assertEquals(0, option.getValue());
option.setValue(1);
assertEquals(1, option.getValue());
option = new LevelsOptionImpl();
option.setMax();
assertEquals(true, option.isMax());
}
@Test
public void testOrderByItemImpl() {
OrderByItemImpl option = new OrderByItemImpl();
AliasImpl expression = new AliasImpl();
option.setExpression(expression);
assertEquals(expression, option.getExpression());
assertEquals(false, option.isDescending());
option.setDescending(true);
assertEquals(true, option.isDescending());
}
@Test
public void testOrderByOptionImpl() {
OrderByOptionImpl option = new OrderByOptionImpl();
OrderByItemImpl order0 = new OrderByItemImpl();
OrderByItemImpl order1 = new OrderByItemImpl();
option.addOrder(order0);
option.addOrder(order1);
assertEquals(order0, option.getOrders().get(0));
assertEquals(order1, option.getOrders().get(1));
}
@Test
public void testQueryOptionImpl() {
QueryOptionImpl option = new AliasQueryOptionImpl();
option.setName("A");
option.setText("B");
assertEquals("A", option.getName());
assertEquals("B", option.getText());
}
@Test
public void testSearchOptionImpl() {
SearchOptionImpl option = new SearchOptionImpl();
assertEquals(SystemQueryOptionKind.SEARCH, option.getKind());
// TODO $search is not supported yet
}
@Test
public void testSelectItemImpl() {
SelectItemImpl option = new SelectItemImpl();
// no typed collection else case ( e.g. if not path is added)
option = new SelectItemImpl();
option = new SelectItemImpl();
assertEquals(false, option.isStar());
option.setStar(true);
assertEquals(true, option.isStar());
option = new SelectItemImpl();
assertEquals(false, option.isAllOperationsInSchema());
FullQualifiedName fqName = new FullQualifiedName("Namespace", "Name");
option.addAllOperationsInSchema(fqName);
assertEquals(true, option.isAllOperationsInSchema());
assertEquals(fqName, option.getAllOperationsInSchemaNameSpace());
}
@Test
public void testSelectOptionImpl() {
SelectOptionImpl option = new SelectOptionImpl();
assertEquals(SystemQueryOptionKind.SELECT, option.getKind());
SelectItemImpl item0 = new SelectItemImpl();
SelectItemImpl item1 = new SelectItemImpl();
ArrayList<SelectItemImpl> list = new ArrayList<SelectItemImpl>();
list.add(item0);
list.add(item1);
option.setSelectItems(list);
assertEquals(item0, option.getSelectItems().get(0));
assertEquals(item1, option.getSelectItems().get(1));
}
@Test
public void testSkipOptionImpl() {
SkipOptionImpl option = new SkipOptionImpl();
assertEquals(SystemQueryOptionKind.SKIP, option.getKind());
option.setValue(10);
assertEquals(10, option.getValue());
}
@Test
public void testSkipTokenOptionImpl() {
SkipTokenOptionImpl option = new SkipTokenOptionImpl();
assertEquals(SystemQueryOptionKind.SKIPTOKEN, option.getKind());
option.setValue("A");
assertEquals("A", option.getValue());
}
@Test
public void testSystemQueryOptionImpl() {
SystemQueryOptionImpl option = new SystemQueryOptionImpl();
option.setKind(SystemQueryOptionKind.EXPAND);
assertEquals(SystemQueryOptionKind.EXPAND, option.getKind());
assertEquals("$expand", option.getName());
}
@Test
public void testTopOptionImpl() {
TopOptionImpl option = new TopOptionImpl();
assertEquals(SystemQueryOptionKind.TOP, option.getKind());
option.setValue(11);
assertEquals(11, option.getValue());
}
}

View File

@ -0,0 +1,239 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.queryoption.expression;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Arrays;
import org.apache.olingo.commons.api.ODataApplicationException;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmAction;
import org.apache.olingo.commons.api.edm.EdmEntityType;
import org.apache.olingo.commons.api.edm.EdmEnumType;
import org.apache.olingo.commons.api.edm.EdmFunction;
import org.apache.olingo.server.api.uri.UriInfoKind;
import org.apache.olingo.server.api.uri.UriInfoResource;
import org.apache.olingo.server.api.uri.queryoption.expression.BinaryOperatorKind;
import org.apache.olingo.server.api.uri.queryoption.expression.ExpressionVisitException;
import org.apache.olingo.server.api.uri.queryoption.expression.MethodKind;
import org.apache.olingo.server.api.uri.queryoption.expression.UnaryOperatorKind;
import org.apache.olingo.server.core.edm.provider.EdmProviderImpl;
import org.apache.olingo.server.core.uri.UriInfoImpl;
import org.apache.olingo.server.core.uri.UriResourceActionImpl;
import org.apache.olingo.server.core.uri.UriResourceFunctionImpl;
import org.apache.olingo.server.core.uri.testutil.EdmTechTestProvider;
import org.apache.olingo.server.core.uri.testutil.FilterTreeToText;
import org.apache.olingo.server.tecsvc.provider.ActionProvider;
import org.apache.olingo.server.tecsvc.provider.EntityTypeProvider;
import org.apache.olingo.server.tecsvc.provider.EnumTypeProvider;
import org.apache.olingo.server.tecsvc.provider.FunctionProvider;
import org.junit.Test;
public class ExpressionTest {
Edm edm = new EdmProviderImpl(new EdmTechTestProvider());
@Test
public void testSupportedOperators() {
assertEquals(UnaryOperatorKind.MINUS, UnaryOperatorKind.get("-"));
assertEquals(null, UnaryOperatorKind.get("XXX"));
assertEquals(BinaryOperatorKind.MOD, BinaryOperatorKind.get("mod"));
assertEquals(null, BinaryOperatorKind.get("XXX"));
assertEquals(MethodKind.CONCAT, MethodKind.get("concat"));
assertEquals(null, MethodKind.get("XXX"));
}
@Test
public void testAliasExpression() throws ExpressionVisitException, ODataApplicationException {
AliasImpl expression = new AliasImpl();
expression.setParameter("Test");
assertEquals("Test", expression.getParameterName());
String output = expression.accept(new FilterTreeToText());
assertEquals("<Test>", output);
}
@Test
public void testBinaryExpression() throws ExpressionVisitException, ODataApplicationException {
BinaryImpl expression = new BinaryImpl();
ExpressionImpl expressionLeft = new LiteralImpl().setText("A");
ExpressionImpl expressionRight = new LiteralImpl().setText("B");
expression.setLeftOperand(expressionLeft);
expression.setRightOperand(expressionRight);
expression.setOperator(BinaryOperatorKind.SUB);
assertEquals(expressionLeft, expression.getLeftOperand());
assertEquals(expressionRight, expression.getRightOperand());
assertEquals(BinaryOperatorKind.SUB, expression.getOperator());
String output = expression.accept(new FilterTreeToText());
assertEquals("<<A> sub <B>>", output);
}
@Test
public void testEnumerationExpression() throws ExpressionVisitException, ODataApplicationException {
EnumerationImpl expression = new EnumerationImpl();
EdmEnumType type = (EdmEnumType) edm.getEnumType(EnumTypeProvider.nameENString);
assertNotNull(type);
expression.setType(type);
assertEquals(type, expression.getType());
expression.addValue("A");
expression.addValue("B");
assertEquals("A", expression.getValues().get(0));
assertEquals("B", expression.getValues().get(1));
assertEquals("<com.sap.odata.test1.ENString<A,B>>", expression.accept(new FilterTreeToText()));
}
@Test
public void testLambdaRefExpression() throws ExpressionVisitException, ODataApplicationException {
LambdaRefImpl expression = new LambdaRefImpl();
expression.setVariableText("A");
assertEquals("A", expression.getVariableName());
assertEquals("<A>", expression.accept(new FilterTreeToText()));
}
@Test
public void testLiteralExpresion() throws ExpressionVisitException, ODataApplicationException {
LiteralImpl expression = new LiteralImpl();
expression.setText("A");
assertEquals("A", expression.getText());
assertEquals("<A>", expression.accept(new FilterTreeToText()));
}
@Test
public void testMemberExpression() throws ExpressionVisitException, ODataApplicationException {
MemberImpl expression = new MemberImpl();
EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETKeyNav);
// UriResourceImplTyped
EdmAction action = edm.getUnboundAction(ActionProvider.nameUARTPrimParam);
UriInfoResource uriInfo = new UriInfoImpl().setKind(UriInfoKind.resource).addResourcePart(
new UriResourceActionImpl().setAction(action)).asUriInfoResource();
expression.setResourcePath(uriInfo);
assertEquals(action.getReturnType().getType(), expression.getType());
// check accept and path
assertEquals(uriInfo, expression.getResourcePath());
assertEquals("<UARTPrimParam>", expression.accept(new FilterTreeToText()));
// UriResourceImplTyped check collection = false case
assertEquals(false, expression.isCollection());
// UriResourceImplTyped check collection = true case
action = edm.getUnboundAction(ActionProvider.nameUARTPrimCollParam);
expression.setResourcePath(new UriInfoImpl().setKind(UriInfoKind.resource).addResourcePart(
new UriResourceActionImpl().setAction(action))
.asUriInfoResource());
assertEquals(true, expression.isCollection());
// UriResourceImplTyped with filter
action = edm.getUnboundAction(ActionProvider.nameUARTPrimParam);
expression.setResourcePath(new UriInfoImpl().setKind(UriInfoKind.resource).addResourcePart(
new UriResourceActionImpl().setAction(action).setTypeFilter(entityType))
.asUriInfoResource());
assertEquals(entityType, expression.getType());
// UriResourceImplKeyPred
EdmFunction function = edm.getUnboundFunction(FunctionProvider.nameUFCRTETKeyNav, null);
expression.setResourcePath(new UriInfoImpl().setKind(UriInfoKind.resource).addResourcePart(
new UriResourceFunctionImpl().setFunction(function))
.asUriInfoResource());
assertEquals(function.getReturnType().getType(), expression.getType());
// UriResourceImplKeyPred typeFilter on entry
EdmEntityType entityBaseType = edm.getEntityType(EntityTypeProvider.nameETBaseTwoKeyNav);
function = edm.getUnboundFunction(FunctionProvider.nameUFCRTESTwoKeyNavParam, Arrays.asList("ParameterInt16"));
expression.setResourcePath(new UriInfoImpl().setKind(UriInfoKind.resource).addResourcePart(
new UriResourceFunctionImpl().setFunction(function).setEntryTypeFilter(entityBaseType))
.asUriInfoResource());
assertEquals(entityBaseType, expression.getType());
// UriResourceImplKeyPred typeFilter on entry
entityBaseType = edm.getEntityType(EntityTypeProvider.nameETBaseTwoKeyNav);
function = edm.getUnboundFunction(FunctionProvider.nameUFCRTESTwoKeyNavParam, Arrays.asList("ParameterInt16"));
expression.setResourcePath(new UriInfoImpl().setKind(UriInfoKind.resource).addResourcePart(
new UriResourceFunctionImpl().setFunction(function).setCollectionTypeFilter(entityBaseType))
.asUriInfoResource());
assertEquals(entityBaseType, expression.getType());
// no typed
entityBaseType = edm.getEntityType(EntityTypeProvider.nameETBaseTwoKeyNav);
function = edm.getUnboundFunction(FunctionProvider.nameUFCRTESTwoKeyNavParam, Arrays.asList("ParameterInt16"));
expression.setResourcePath(new UriInfoImpl().setKind(UriInfoKind.all));
assertEquals(null, expression.getType());
// no typed collection else case
assertEquals(false, expression.isCollection());
}
@Test
public void testMethodCallExpression() throws ExpressionVisitException, ODataApplicationException {
MethodImpl expression = new MethodImpl();
expression.setMethod(MethodKind.CONCAT);
ExpressionImpl p0 = new LiteralImpl().setText("A");
ExpressionImpl p1 = new LiteralImpl().setText("B");
expression.addParameter(p0);
expression.addParameter(p1);
assertEquals(MethodKind.CONCAT, expression.getMethod());
assertEquals("<concat(<A>,<B>)>", expression.accept(new FilterTreeToText()));
assertEquals(p0, expression.getParameters().get(0));
assertEquals(p1, expression.getParameters().get(1));
}
@Test
public void testTypeLiteralExpression() throws ExpressionVisitException, ODataApplicationException {
TypeLiteralImpl expression = new TypeLiteralImpl();
EdmEntityType entityBaseType = edm.getEntityType(EntityTypeProvider.nameETBaseTwoKeyNav);
expression.setType(entityBaseType);
assertEquals(entityBaseType, expression.getType());
assertEquals("<com.sap.odata.test1.ETBaseTwoKeyNav>", expression.accept(new FilterTreeToText()));
}
@Test
public void testUnaryExpression() throws ExpressionVisitException, ODataApplicationException {
UnaryImpl expression = new UnaryImpl();
expression.setOperator(UnaryOperatorKind.MINUS);
ExpressionImpl operand = new LiteralImpl().setText("A");
expression.setOperand(operand);
assertEquals(UnaryOperatorKind.MINUS, expression.getOperator());
assertEquals(operand, expression.getOperand());
assertEquals("<- <A>>", expression.accept(new FilterTreeToText()));
}
}

View File

@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import java.util.Arrays;
import java.util.List;
import org.apache.olingo.commons.api.ODataException;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.server.api.edm.provider.ComplexType;
import org.apache.olingo.server.api.edm.provider.EntitySet;
import org.apache.olingo.server.api.edm.provider.EntityType;
import org.apache.olingo.server.api.edm.provider.Property;
import org.apache.olingo.server.api.edm.provider.PropertyRef;
import org.apache.olingo.server.tecsvc.provider.EdmTechProvider;
/**
* Implement the EdmTechProvider and
* <li>adds a entity type <b>ETabc with</b> properties a,b,c,d,e,f</li>
* <li>adds a complex type <b>CTabc</b> with properties a,b,c,d,e,f</li>
* <li>adds a <b>abc</b> entity set of type <b>ETabc</b></li>
*/
public class EdmTechTestProvider extends EdmTechProvider {
private static final FullQualifiedName nameInt16 = EdmPrimitiveTypeKind.Int16.getFullQualifiedName();
public static final String nameSpace = "com.sap.odata.test1";
public static final FullQualifiedName nameContainer = new FullQualifiedName(nameSpace, "Container");
Property propertyAInt16 = new Property().setName("a").setType(nameInt16);
Property propertyBInt16 = new Property().setName("b").setType(nameInt16);
Property propertyCInt16 = new Property().setName("c").setType(nameInt16);
Property propertyDInt16 = new Property().setName("d").setType(nameInt16);
Property propertyEInt16 = new Property().setName("e").setType(nameInt16);
Property propertyFInt16 = new Property().setName("f").setType(nameInt16);
public static final FullQualifiedName nameCTabc = new FullQualifiedName(nameSpace, "CTabc");
public static final FullQualifiedName nameETabc = new FullQualifiedName(nameSpace, "ETabc");
@Override
public ComplexType getComplexType(final FullQualifiedName complexTypeName) throws ODataException {
if (complexTypeName.equals(nameCTabc)) {
return new ComplexType()
.setName("CTabc")
.setProperties(Arrays.asList(
propertyAInt16, propertyBInt16, propertyCInt16,
propertyDInt16, propertyEInt16, propertyFInt16
));
}
return super.getComplexType(complexTypeName);
}
@Override
public EntitySet getEntitySet(final FullQualifiedName entityContainer, final String name) throws ODataException {
if (nameContainer.equals(entityContainer)) {
if (name.equals("ESabc")) {
return new EntitySet()
.setName("ESabc")
.setType(nameETabc);
}
}
return super.getEntitySet(entityContainer, name);
}
@Override
public EntityType getEntityType(final FullQualifiedName entityTypeName) throws ODataException {
List<PropertyRef> oneKeyPropertyInt16 = Arrays.asList(new PropertyRef().setPropertyName("a"));
if (entityTypeName.equals(nameETabc)) {
return new EntityType()
.setName("ETabc")
.setProperties(Arrays.asList(
propertyAInt16, propertyBInt16, propertyCInt16,
propertyDInt16, propertyEInt16, propertyFInt16))
.setKey(oneKeyPropertyInt16);
}
return super.getEntityType(entityTypeName);
}
}

View File

@ -0,0 +1,230 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.apache.olingo.commons.api.ODataApplicationException;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.server.api.uri.UriInfoKind;
import org.apache.olingo.server.api.uri.queryoption.ExpandItem;
import org.apache.olingo.server.api.uri.queryoption.SelectItem;
import org.apache.olingo.server.api.uri.queryoption.expression.ExpressionVisitException;
import org.apache.olingo.server.core.uri.UriInfoImpl;
import org.apache.olingo.server.core.uri.queryoption.ExpandOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.FilterOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.OrderByOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.QueryOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.SelectOptionImpl;
public class ExpandValidator implements TestValidator {
private Edm edm;
private TestValidator invokedByValidator;
private int expandItemIndex;
private ExpandOptionImpl expandOption;
private ExpandItem expandItem;
// --- Setup ---
public ExpandValidator setUpValidator(final TestValidator validator) {
invokedByValidator = validator;
return this;
}
public ExpandValidator setExpand(final ExpandOptionImpl expand) {
expandOption = expand;
first();
return this;
}
public ExpandValidator setEdm(final Edm edm) {
this.edm = edm;
return this;
}
// --- Navigation ---
public ExpandValidator goUpToExpandValidator() {
return (ExpandValidator) invokedByValidator;
}
public ResourceValidator goUpToUriResourceValidator() {
return (ResourceValidator) invokedByValidator;
}
public ResourceValidator goPath() {
UriInfoImpl uriInfo = (UriInfoImpl) expandItem.getResourcePath();
if (uriInfo.getKind() != UriInfoKind.resource) {
fail("goPath() can only be used on UriInfoKind.resource");
}
return new ResourceValidator()
.setUpValidator(this)
.setEdm(edm)
.setUriInfoImplPath(uriInfo);
}
public FilterValidator goOrder(final int index) {
OrderByOptionImpl orderBy = (OrderByOptionImpl) expandItem.getOrderByOption();
return new FilterValidator()
.setValidator(this)
.setEdm(edm)
.setExpression(orderBy.getOrders().get(index).getExpression());
}
public ResourceValidator goSelectItem(final int index) {
SelectOptionImpl select = (SelectOptionImpl) expandItem.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
UriInfoImpl uriInfo = (UriInfoImpl) item.getResourcePath();
return new ResourceValidator()
.setUpValidator(this)
.setEdm(edm)
.setUriInfoImplPath(uriInfo);
}
public ExpandValidator goExpand() {
ExpandValidator val = new ExpandValidator()
.setExpand((ExpandOptionImpl) expandItem.getExpandOption())
.setUpValidator(this);
return val;
}
public ExpandValidator first() {
expandItemIndex = 0;
expandItem = expandOption.getExpandItems().get(expandItemIndex);
return this;
}
public ExpandValidator next() {
expandItemIndex++;
try {
expandItem = expandOption.getExpandItems().get(expandItemIndex);
} catch (IndexOutOfBoundsException ex) {
fail("not enought segments");
}
return this;
}
public ExpandValidator isSegmentStar(final int index) {
assertEquals(true, expandItem.isStar());
return this;
}
public ExpandValidator isSegmentRef(final int index) {
assertEquals(true, expandItem.isRef());
return this;
}
public ExpandValidator isLevelText(final String text) {
QueryOptionImpl option = (QueryOptionImpl) expandItem.getLevelsOption();
assertEquals(text, option.getText());
return this;
}
public ExpandValidator isSkipText(final String text) {
QueryOptionImpl option = (QueryOptionImpl) expandItem.getSkipOption();
assertEquals(text, option.getText());
return this;
}
public ExpandValidator isTopText(final String text) {
QueryOptionImpl option = (QueryOptionImpl) expandItem.getTopOption();
assertEquals(text, option.getText());
return this;
}
public ExpandValidator isInlineCountText(final String text) {
QueryOptionImpl option = (QueryOptionImpl) expandItem.getCountOption();
assertEquals(text, option.getText());
return this;
}
public ExpandValidator isSelectText(final String text) {
QueryOptionImpl option = (QueryOptionImpl) expandItem.getSelectOption();
assertEquals(text, option.getText());
return this;
}
public ExpandValidator isSelectItemStar(final int index) {
SelectOptionImpl select = (SelectOptionImpl) expandItem.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
assertEquals(true, item.isStar());
return this;
}
public ExpandValidator isSelectItemAllOperations(final int index, final FullQualifiedName fqn) {
SelectOptionImpl select = (SelectOptionImpl) expandItem.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
assertEquals(fqn.toString(), item.getAllOperationsInSchemaNameSpace().toString());
return this;
}
public ExpandValidator isFilterOptionText(final String text) {
QueryOptionImpl option = (QueryOptionImpl) expandItem.getFilterOption();
assertEquals(text, option.getText());
return this;
}
public ExpandValidator isFilterSerialized(final String serialized) {
FilterOptionImpl filter = (FilterOptionImpl) expandItem.getFilterOption();
try {
String tmp = FilterTreeToText.Serialize(filter);
assertEquals(serialized, tmp);
} catch (ExpressionVisitException e) {
fail("Exception occured while converting the filterTree into text" + "\n"
+ " Exception: " + e.getMessage());
} catch (ODataApplicationException e) {
fail("Exception occured while converting the filterTree into text" + "\n"
+ " Exception: " + e.getMessage());
}
return this;
}
public ExpandValidator isSortOrder(final int index, final boolean descending) {
OrderByOptionImpl orderBy = (OrderByOptionImpl) expandItem.getOrderByOption();
assertEquals(descending, orderBy.getOrders().get(index).isDescending());
return this;
}
public ExpandValidator isExpandStartType(final FullQualifiedName fullName) {
EdmType actualType = expandItem.getStartTypeFilter();
FullQualifiedName actualName = new FullQualifiedName(actualType.getNamespace(), actualType.getName());
assertEquals(fullName, actualName);
return this;
}
}

View File

@ -0,0 +1,154 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import java.util.List;
import org.apache.olingo.commons.api.ODataApplicationException;
import org.apache.olingo.commons.api.edm.EdmEnumType;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.server.api.uri.UriInfoResource;
import org.apache.olingo.server.api.uri.UriResource;
import org.apache.olingo.server.api.uri.UriResourceLambdaAll;
import org.apache.olingo.server.api.uri.UriResourceLambdaAny;
import org.apache.olingo.server.api.uri.UriResourcePartTyped;
import org.apache.olingo.server.api.uri.queryoption.FilterOption;
import org.apache.olingo.server.api.uri.queryoption.expression.BinaryOperatorKind;
import org.apache.olingo.server.api.uri.queryoption.expression.Expression;
import org.apache.olingo.server.api.uri.queryoption.expression.ExpressionVisitException;
import org.apache.olingo.server.api.uri.queryoption.expression.ExpressionVisitor;
import org.apache.olingo.server.api.uri.queryoption.expression.MethodKind;
import org.apache.olingo.server.api.uri.queryoption.expression.UnaryOperatorKind;
public class FilterTreeToText implements ExpressionVisitor<String> {
public static String Serialize(final FilterOption filter)
throws ExpressionVisitException, ODataApplicationException {
Expression expression = filter.getExpression();
return expression.accept(new FilterTreeToText());
}
public static String Serialize(final Expression expression)
throws ExpressionVisitException, ODataApplicationException {
return expression.accept(new FilterTreeToText());
}
@Override
public String visitBinaryOperator(final BinaryOperatorKind operator, final String left, final String right)
throws ExpressionVisitException {
return "<" + left + " " + operator.toString() + " " + right + ">";
}
@Override
public String visitUnaryOperator(final UnaryOperatorKind operator, final String operand)
throws ExpressionVisitException {
return "<" + operator + " " + operand.toString() + ">";
}
@Override
public String visitMethodCall(final MethodKind methodCall, final List<String> parameters)
throws ExpressionVisitException {
String text = "<" + methodCall + "(";
int i = 0;
while (i < parameters.size()) {
if (i > 0) {
text += ",";
}
text += parameters.get(i);
i++;
}
return text + ")>";
}
@Override
public String visitLiteral(final String literal) throws ExpressionVisitException {
return "<" + literal + ">";
}
@Override
public String visitMember(final UriInfoResource resource) throws ExpressionVisitException, ODataApplicationException {
String ret = "";
UriInfoResource path = resource;
for (UriResource item : path.getUriResourceParts()) {
String tmp = "";
if (item instanceof UriResourceLambdaAll) {
UriResourceLambdaAll all = (UriResourceLambdaAll) item;
tmp = visitLambdaExpression("ALL", all.getLambdaVariable(), all.getExpression());
} else if (item instanceof UriResourceLambdaAny) {
UriResourceLambdaAny any = (UriResourceLambdaAny) item;
tmp = visitLambdaExpression("ANY", any.getLamdaVariable(), any.getExpression());
} else if (item instanceof UriResourcePartTyped) {
UriResourcePartTyped typed = (UriResourcePartTyped) item;
tmp = typed.toString(true);
}
if (ret.length() != 0) {
ret += "/";
}
ret += tmp;
}
return "<" + ret + ">";
}
@Override
public String visitAlias(final String referenceName) throws ExpressionVisitException {
return "<" + referenceName + ">";
}
@Override
public String visitLambdaExpression(final String functionText, final String string, final Expression expression)
throws ExpressionVisitException, ODataApplicationException {
return "<" + functionText + ";" + ((expression == null) ? "" : expression.accept(this)) + ">";
}
@Override
public String visitTypeLiteral(final EdmType type) {
return "<" + type.getNamespace() + "." + type.getName() + ">";
}
@Override
public String visitLambdaReference(final String variableText) {
return "<" + variableText + ">";
}
@Override
public String visitEnum(final EdmEnumType type, final List<String> enumValues)
throws ExpressionVisitException, ODataApplicationException {
String tmp = "";
for (String item : enumValues) {
if (tmp.length() > 0) {
tmp += ",";
}
tmp += item;
}
return "<" + type.getNamespace() + "." + type.getName() + "<" + tmp + ">>";
}
}

View File

@ -0,0 +1,534 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.List;
import org.apache.olingo.commons.api.ODataApplicationException;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.UriInfoKind;
import org.apache.olingo.server.api.uri.queryoption.expression.BinaryOperatorKind;
import org.apache.olingo.server.api.uri.queryoption.expression.Expression;
import org.apache.olingo.server.api.uri.queryoption.expression.ExpressionVisitException;
import org.apache.olingo.server.api.uri.queryoption.expression.Member;
import org.apache.olingo.server.api.uri.queryoption.expression.MethodKind;
import org.apache.olingo.server.core.uri.UriInfoImpl;
import org.apache.olingo.server.core.uri.parser.Parser;
import org.apache.olingo.server.core.uri.parser.UriParserException;
import org.apache.olingo.server.core.uri.parser.UriParserSemanticException;
import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException;
import org.apache.olingo.server.core.uri.queryoption.FilterOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.OrderByOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.BinaryImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.EnumerationImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.LiteralImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.MemberImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.MethodImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.TypeLiteralImpl;
public class FilterValidator implements TestValidator {
private Edm edm;
private TestValidator invokedByValidator;
private FilterOptionImpl filter;
private Expression curExpression;
private Expression rootExpression;
private OrderByOptionImpl orderBy;
private UriParserException exception;
// --- Setup ---
public FilterValidator setUriResourcePathValidator(final ResourceValidator uriResourcePathValidator) {
invokedByValidator = uriResourcePathValidator;
return this;
}
public FilterValidator setUriValidator(final TestUriValidator uriValidator) {
invokedByValidator = uriValidator;
return this;
}
public FilterValidator setValidator(final TestValidator uriValidator) {
invokedByValidator = uriValidator;
return this;
}
public FilterValidator setEdm(final Edm edm) {
this.edm = edm;
return this;
}
public FilterValidator setFilter(final FilterOptionImpl filter) {
this.filter = filter;
if (filter.getExpression() == null) {
fail("FilterValidator: no filter found");
}
setExpression(filter.getExpression());
return this;
}
public FilterValidator setOrderBy(final OrderByOptionImpl orderBy) {
this.orderBy = orderBy;
return this;
}
public FilterValidator setExpression(final Expression expression) {
rootExpression = curExpression = expression;
return this;
}
// --- Execution ---
public FilterValidator runOrderByOnETAllPrim(final String orderBy) throws UriParserException {
String uri = "ESAllPrim?$orderby=" + orderBy.trim();
return runUriOrderBy(uri);
}
public FilterValidator runOrderByOnETTwoKeyNav(final String orderBy) throws UriParserException {
String uri = "ESTwoKeyNav?$orderby=" + orderBy.trim();
return runUriOrderBy(uri);
}
public FilterValidator runOrderByOnETTwoKeyNavEx(final String orderBy) throws UriParserException {
String uri = "ESTwoKeyNav?$orderby=" + orderBy.trim();
return runUriOrderByEx(uri);
}
public FilterValidator runOnETTwoKeyNav(final String filter) throws UriParserException {
String uri = "ESTwoKeyNav?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runOnETTwoKeyNavSingle(final String filter) throws UriParserException {
String uri = "SINav?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runOnETTwoKeyNavEx(final String filter) throws UriParserException {
String uri = "ESTwoKeyNav?$filter=" + filter.trim();
return runUriEx(uri);
}
public FilterValidator runOnETAllPrim(final String filter) throws UriParserException {
String uri = "ESAllPrim(1)?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runOnETKeyNav(final String filter) throws UriParserException {
String uri = "ESKeyNav(1)?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runOnETKeyNavEx(final String filter) throws UriParserException {
String uri = "ESKeyNav(1)?$filter=" + filter.trim();
return runUriEx(uri);
}
public FilterValidator runOnCTTwoPrim(final String filter) throws UriParserException {
String uri = "SINav/PropertyComplexTwoPrim?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runOnString(final String filter) throws UriParserException {
String uri = "SINav/PropertyString?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runOnInt32(final String filter) throws UriParserException {
String uri = "ESCollAllPrim(1)/CollPropertyInt32?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runOnDateTimeOffset(final String filter) throws UriParserException {
String uri = "ESCollAllPrim(1)/CollPropertyDateTimeOffset?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runOnDuration(final String filter) throws UriParserException {
String uri = "ESCollAllPrim(1)/CollPropertyDuration?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runOnTimeOfDay(final String filter) throws UriParserException {
String uri = "ESCollAllPrim(1)/CollPropertyTimeOfDay?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runESabc(final String filter) throws UriParserException {
String uri = "ESabc?$filter=" + filter.trim();
return runUri(uri);
}
public FilterValidator runUri(final String uri) throws UriParserException {
Parser parser = new Parser();
UriInfo uriInfo = null;
uriInfo = parser.parseUri(uri, edm);
if (uriInfo.getKind() != UriInfoKind.resource) {
fail("Filtervalidator can only be used on resourcePaths");
}
setFilter((FilterOptionImpl) uriInfo.getFilterOption());
curExpression = filter.getExpression();
return this;
}
public FilterValidator runUriEx(final String uri) {
Parser parser = new Parser();
UriInfo uriInfo = null;
try {
uriInfo = parser.parseUri(uri, edm);
} catch (UriParserException e) {
exception = e;
return this;
}
if (uriInfo.getKind() != UriInfoKind.resource) {
fail("Filtervalidator can only be used on resourcePaths");
}
setFilter((FilterOptionImpl) uriInfo.getFilterOption());
curExpression = filter.getExpression();
return this;
}
public FilterValidator runUriOrderBy(final String uri) throws UriParserException {
Parser parser = new Parser();
UriInfo uriInfo = null;
uriInfo = parser.parseUri(uri, edm);
if (uriInfo.getKind() != UriInfoKind.resource) {
fail("Filtervalidator can only be used on resourcePaths");
}
setOrderBy((OrderByOptionImpl) uriInfo.getOrderByOption());
return this;
}
public FilterValidator runUriOrderByEx(final String uri) {
Parser parser = new Parser();
UriInfo uriInfo = null;
try {
uriInfo = parser.parseUri(uri, edm);
} catch (UriParserException e) {
exception = e;
return this;
}
if (uriInfo.getKind() != UriInfoKind.resource) {
fail("Filtervalidator can only be used on resourcePaths");
}
setOrderBy((OrderByOptionImpl) uriInfo.getOrderByOption());
return this;
}
// --- Navigation ---
public ExpandValidator goUpToExpandValidator() {
return (ExpandValidator) invokedByValidator;
}
public ResourceValidator goUpToResourceValidator() {
return (ResourceValidator) invokedByValidator;
}
public ResourceValidator goPath() {
if (!(curExpression instanceof MemberImpl)) {
fail("Current expression not a member");
}
MemberImpl member = (MemberImpl) curExpression;
return new ResourceValidator()
.setEdm(edm)
.setUriInfoImplPath((UriInfoImpl) member.getResourcePath())
.setUpValidator(this);
}
public FilterValidator goParameter(final int parameterIndex) {
if (curExpression instanceof MethodImpl) {
MethodImpl methodCall = (MethodImpl) curExpression;
curExpression = methodCall.getParameters().get(parameterIndex);
} else {
fail("Current expression not a methodCall");
}
return this;
}
// --- Validation ---
/**
* Validates the serialized filterTree against a given filterString
* The given expected filterString is compressed before to allow better readable code in the unit tests
* @param toBeCompr
* @return
*/
public FilterValidator isCompr(final String toBeCompr) {
return is(compress(toBeCompr));
}
public FilterValidator is(final String expectedFilterAsString) {
try {
String actualFilterAsText = FilterTreeToText.Serialize((FilterOptionImpl) filter);
assertEquals(expectedFilterAsString, actualFilterAsText);
} catch (ExpressionVisitException e) {
fail("Exception occured while converting the filterTree into text" + "\n"
+ " Exception: " + e.getMessage());
} catch (ODataApplicationException e) {
fail("Exception occured while converting the filterTree into text" + "\n"
+ " Exception: " + e.getMessage());
}
return this;
}
// --- Helper ---
private String compress(final String expected) {
String ret = expected.replaceAll("\\s+", " ");
ret = ret.replaceAll("< ", "<");
ret = ret.replaceAll(" >", ">");
return ret;
}
public FilterValidator isType(final FullQualifiedName fullName) {
EdmType actualType = null;
if (curExpression instanceof MemberImpl) {
Member member = (Member) curExpression;
actualType = member.getType();
} else if (curExpression instanceof TypeLiteralImpl) {
TypeLiteralImpl typeLiteral = (TypeLiteralImpl) curExpression;
actualType = typeLiteral.getType();
} else if (curExpression instanceof LiteralImpl) {
LiteralImpl typeLiteral = (LiteralImpl) curExpression;
actualType = typeLiteral.getType();
}
if (actualType == null) {
fail("Current expression not typed");
}
FullQualifiedName actualName = new FullQualifiedName(actualType.getNamespace(), actualType.getName());
assertEquals(fullName, actualName);
return this;
}
public FilterValidator left() {
if (!(curExpression instanceof BinaryImpl)) {
fail("Current expression not a binary operator");
}
curExpression = ((BinaryImpl) curExpression).getLeftOperand();
return this;
}
public FilterValidator root() {
if (filter != null) {
curExpression = filter.getExpression();
} else {
curExpression = rootExpression;
}
return this;
}
public FilterValidator right() {
if (!(curExpression instanceof BinaryImpl)) {
fail("Current expression is not a binary operator");
}
curExpression = ((BinaryImpl) curExpression).getRightOperand();
return this;
}
public FilterValidator isLiteral(final String literalText) {
if (!(curExpression instanceof LiteralImpl)) {
fail("Current expression is not a literal");
}
String actualLiteralText = ((LiteralImpl) curExpression).getText();
assertEquals(literalText, actualLiteralText);
return this;
}
public FilterValidator isMethod(final MethodKind methodKind, final int parameterCount) {
if (!(curExpression instanceof MethodImpl)) {
fail("Current expression is not a methodCall");
}
MethodImpl methodCall = (MethodImpl) curExpression;
assertEquals(methodKind, methodCall.getMethod());
assertEquals(parameterCount, methodCall.getParameters().size());
return this;
}
public FilterValidator isParameterText(final int parameterIndex, final String parameterText)
throws ExpressionVisitException, ODataApplicationException {
if (!(curExpression instanceof MethodImpl)) {
fail("Current expression is not a method");
}
MethodImpl methodCall = (MethodImpl) curExpression;
Expression parameter = methodCall.getParameters().get(parameterIndex);
String actualParameterText = FilterTreeToText.Serialize(parameter);
assertEquals(parameterText, actualParameterText);
return this;
}
public FilterValidator isBinary(final BinaryOperatorKind binaryOperator) {
if (!(curExpression instanceof BinaryImpl)) {
fail("Current expression is not a binary operator");
}
BinaryImpl binary = (BinaryImpl) curExpression;
assertEquals(binaryOperator, binary.getOperator());
return this;
}
public FilterValidator isTypedLiteral(final FullQualifiedName fullName) {
if (!(curExpression instanceof TypeLiteralImpl)) {
fail("Current expression not a typeLiteral");
}
isType(fullName);
return this;
}
public FilterValidator isMember() {
if (!(curExpression instanceof MemberImpl)) {
fail("Current expression not a member");
}
return this;
}
public FilterValidator isMemberStartType(final FullQualifiedName fullName) {
if (!(curExpression instanceof MemberImpl)) {
fail("Current expression not a member");
}
MemberImpl member = (MemberImpl) curExpression;
EdmType actualType = member.getStartTypeFilter();
FullQualifiedName actualName = new FullQualifiedName(actualType.getNamespace(), actualType.getName());
assertEquals(fullName, actualName);
return this;
}
public FilterValidator isEnum(final FullQualifiedName nameenstring, final List<String> enumValues) {
if (!(curExpression instanceof EnumerationImpl)) {
fail("Current expression not a enumeration");
}
EnumerationImpl enumeration = (EnumerationImpl) curExpression;
FullQualifiedName actualName =
new FullQualifiedName(enumeration.getType().getNamespace(), enumeration.getType().getName());
// check name
assertEquals(nameenstring.toString(), actualName.toString());
// check values
int i = 0;
for (String item : enumValues) {
assertEquals(item, enumeration.getValues().get(i));
i++;
}
return this;
}
public FilterValidator isSortOrder(final int index, final boolean descending) {
assertEquals(descending, orderBy.getOrders().get(index).isDescending());
return this;
}
public FilterValidator goOrder(final int index) {
curExpression = orderBy.getOrders().get(index).getExpression();
return this;
}
public FilterValidator isExSyntax(final long errorID) {
assertEquals(UriParserSyntaxException.class, exception.getClass());
return this;
}
public FilterValidator isExSemantic(final long errorID) {
assertEquals(UriParserSemanticException.class, exception.getClass());
return this;
}
public FilterValidator isNull() {
if (!(curExpression instanceof LiteralImpl)) {
fail("Current expression is not a literal");
}
String actualLiteralText = ((LiteralImpl) curExpression).getText();
assertEquals("null", actualLiteralText);
return this;
}
public FilterValidator isTrue() {
if (!(curExpression instanceof LiteralImpl)) {
fail("Current expression is not a literal");
}
String actualLiteralText = ((LiteralImpl) curExpression).getText();
assertEquals("true", actualLiteralText);
return this;
}
public FilterValidator isFalse() {
if (!(curExpression instanceof LiteralImpl)) {
fail("Current expression is not a literal");
}
String actualLiteralText = ((LiteralImpl) curExpression).getText();
assertEquals("false", actualLiteralText);
return this;
}
}

View File

@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import java.util.Arrays;
import java.util.List;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import org.antlr.v4.runtime.misc.Utils;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.RuleNode;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.Tree;
public class ParseTreeToText {
public static String getTreeAsText(final Tree contextTree, final String[] ruleNames) {
return toStringTree(contextTree, Arrays.asList(ruleNames));
}
private static String toStringTree(final Tree t, @Nullable final List<String> ruleNames) {
if (t.getChildCount() == 0) {
return Utils.escapeWhitespace(getNodeText(t, ruleNames), false);
}
StringBuilder buf = new StringBuilder();
String s = Utils.escapeWhitespace(getNodeText(t, ruleNames), false);
buf.append(s);
buf.append("(");
for (int i = 0; i < t.getChildCount(); i++) {
if (i > 0) {
buf.append(' ');
}
buf.append(toStringTree(t.getChild(i), ruleNames));
}
buf.append(")");
return buf.toString();
}
private static String getNodeText(@NotNull final Tree t, @Nullable final List<String> ruleNames) {
if (ruleNames != null) {
if (t instanceof RuleNode) {
int ruleIndex = ((RuleNode) t).getRuleContext().getRuleIndex();
return ruleNames.get(ruleIndex);
} else if (t instanceof ErrorNode) {
return t.toString();
} else if (t instanceof TerminalNode) {
Token symbol = ((TerminalNode) t).getSymbol();
if (symbol != null) {
String s = symbol.getText();
return s;
}
}
}
// no recog for rule names
Object payload = t.getPayload();
if (payload instanceof Token) {
return ((Token) payload).getText();
}
return t.getPayload().toString();
}
}

View File

@ -0,0 +1,162 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.antlr.v4.runtime.ParserRuleContext;
import org.apache.olingo.server.core.uri.antlr.UriParserParser;
public class ParserValidator {
private String input = null;
private ParserRuleContext root;
int logLevel = 0;
private int lexerLogLevel = 0;
boolean allowFullContext;
boolean allowContextSensitifity;
boolean allowAmbiguity;
List<Exception> exceptions = new ArrayList<Exception>();
private Exception curException = null;
// --- Setup ---
public ParserValidator log(final int logLevel) {
this.logLevel = logLevel;
return this;
}
public ParserValidator lexerLog(final int logLevel) {
lexerLogLevel = logLevel;
return this;
}
/**
* Used in fast LL Parsing:
* Don't stop the parsing process when the slower full context parsing (with prediction mode SLL) is
* required
* @return
*/
public ParserValidator aFC() {
allowFullContext = true;
return this;
}
/**
* Used in fast LL Parsing:
* Allows ContextSensitifity Errors which occur often when using the slower full context parsing
* and indicate that there is a context sensitivity ( which may not be an error).
* @return
*/
public ParserValidator aCS() {
allowContextSensitifity = true;
return this;
}
/**
* Used in fast LL Parsing:
* Allows ambiguities
* @return
*/
public ParserValidator aAM() {
allowAmbiguity = true;
return this;
}
// --- Execution ---
public ParserValidator run(final String uri) {
input = uri;
// just run a short lexer step. E.g. to print the tokens
if (lexerLogLevel > 0) {
(new TokenValidator()).log(lexerLogLevel).run(input);
}
/**/// root = parseInput(uri);
// if LOG > 0 - Write serialized tree
if (logLevel > 0) {
if (root != null) {
System.out.println(ParseTreeToText.getTreeAsText(root, new UriParserParser(null).getRuleNames()));
} else {
System.out.println("root == null");
}
}
// reset for next test
allowFullContext = false;
allowContextSensitifity = false;
allowAmbiguity = false;
logLevel = 0;
return this;
}
// --- Navigation ---
public ParserValidator exFirst() {
try {
// curWeakException = exceptions.get(0);
} catch (IndexOutOfBoundsException ex) {
// curWeakException = null;
}
return this;
}
public ParserValidator exLast() {
// curWeakException = exceptions.get(exceptions.size() - 1);
return this;
}
public ParserValidator exAt(final int index) {
try {
// curWeakException = exceptions.get(index);
} catch (IndexOutOfBoundsException ex) {
// curWeakException = null;
}
return this;
}
// --- Validation ---
public ParserValidator isText(final String expected) {
assertEquals(null, curException);
assertEquals(0, exceptions.size());
String actualTreeAsText = ParseTreeToText.getTreeAsText(root, new UriParserParser(null).getRuleNames());
assertEquals(expected, actualTreeAsText);
return this;
}
public ParserValidator isExeptionType(final Class<?> exClass) {
assertEquals(exClass, curException.getClass());
return this;
}
}

View File

@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.apache.olingo.server.core.uri.antlr.UriParserParser;
import org.apache.olingo.server.core.uri.parser.Parser;
public class ParserWithLogging extends Parser {
TestErrorLogger errorCollector1;
TestErrorLogger errorCollector2;
public ParserWithLogging() {
errorCollector1 = new TestErrorLogger("Stage 1", 1);
errorCollector2 = new TestErrorLogger("Stage 2", 1);
}
@Override
protected void addStage2ErrorStategy(final UriParserParser parser) {
// Don't throw an at first syntax error, so the error listener will be called
parser.setErrorHandler(new DefaultErrorStrategy());
}
@Override
protected void addStage1ErrorListener(final UriParserParser parser) {
// Log error to console
parser.removeErrorListeners();
parser.addErrorListener(errorCollector1);
parser.addErrorListener(new DiagnosticErrorListener());
}
@Override
protected void addStage2ErrorListener(final UriParserParser parser) {
// Log error to console
parser.removeErrorListeners();
parser.addErrorListener(errorCollector2);
parser.addErrorListener(new DiagnosticErrorListener());
}
}

View File

@ -0,0 +1,599 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.List;
import org.apache.olingo.commons.api.ODataApplicationException;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmElement;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.UriInfoKind;
import org.apache.olingo.server.api.uri.UriParameter;
import org.apache.olingo.server.api.uri.UriResourceKind;
import org.apache.olingo.server.api.uri.UriResourcePartTyped;
import org.apache.olingo.server.api.uri.queryoption.CustomQueryOption;
import org.apache.olingo.server.api.uri.queryoption.SelectItem;
import org.apache.olingo.server.api.uri.queryoption.expression.ExpressionVisitException;
import org.apache.olingo.server.core.uri.UriInfoImpl;
import org.apache.olingo.server.core.uri.UriResourceActionImpl;
import org.apache.olingo.server.core.uri.UriResourceComplexPropertyImpl;
import org.apache.olingo.server.core.uri.UriResourceEntitySetImpl;
import org.apache.olingo.server.core.uri.UriResourceFunctionImpl;
import org.apache.olingo.server.core.uri.UriResourceImpl;
import org.apache.olingo.server.core.uri.UriResourceLambdaAllImpl;
import org.apache.olingo.server.core.uri.UriResourceLambdaAnyImpl;
import org.apache.olingo.server.core.uri.UriResourceNavigationPropertyImpl;
import org.apache.olingo.server.core.uri.UriResourcePrimitivePropertyImpl;
import org.apache.olingo.server.core.uri.UriResourceSingletonImpl;
import org.apache.olingo.server.core.uri.UriResourceWithKeysImpl;
import org.apache.olingo.server.core.uri.queryoption.CustomQueryOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.ExpandOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.SelectOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.ExpressionImpl;
import org.apache.olingo.server.core.uri.validator.UriValidator;
public class ResourceValidator implements TestValidator {
private Edm edm;
private TestValidator invokedBy;
private UriInfo uriInfo = null;
private UriResourceImpl uriPathInfo = null;
private int uriResourceIndex;
// --- Setup ---
public ResourceValidator setUpValidator(final TestValidator uriValidator) {
invokedBy = uriValidator;
return this;
}
public ResourceValidator setEdm(final Edm edm) {
this.edm = edm;
return this;
}
public ResourceValidator setUriInfoImplPath(final UriInfoImpl uriInfoPath) {
uriInfo = uriInfoPath;
last();
return this;
}
// --- Execution ---
public ResourceValidator run(final String uri) {
ParserWithLogging testParser = new ParserWithLogging();
UriInfoImpl uriInfoTmp = null;
uriPathInfo = null;
try {
uriInfoTmp = (UriInfoImpl) testParser.parseUri(uri, edm);
UriValidator uriValidator = new UriValidator();
uriValidator.validate(uriInfoTmp, "GET");
} catch (Exception e) {
fail("Exception occured while parsing the URI: " + uri + "\n"
+ " Message: " + e.getMessage());
}
if (uriInfoTmp.getKind() != UriInfoKind.resource) {
fail("Invalid UriInfoKind: " + uriInfoTmp.getKind().toString());
}
uriInfo = uriInfoTmp;
first();
return this;
}
// --- Navigation ---
public TestUriValidator goUpUriValidator() {
return (TestUriValidator) invokedBy;
}
public ExpandValidator goUpExpandValidator() {
return (ExpandValidator) invokedBy;
}
public FilterValidator goUpFilterValidator() {
return (FilterValidator) invokedBy;
}
public FilterValidator goParameter(final int index) {
assertEquals(UriResourceKind.function, uriPathInfo.getKind());
UriResourceFunctionImpl function = (UriResourceFunctionImpl) uriPathInfo;
return new FilterValidator()
.setEdm(edm)
.setExpression(function.getParameters().get(index).getExression())
.setValidator(this);
}
public FilterValidator goLambdaExpression() {
if (uriPathInfo.getKind() == UriResourceKind.lambdaAll) {
return new FilterValidator()
.setEdm(edm)
.setExpression(((UriResourceLambdaAllImpl) uriPathInfo).getExpression());
} else if (uriPathInfo.getKind() == UriResourceKind.lambdaAny) {
return new FilterValidator()
.setEdm(edm)
.setExpression(((UriResourceLambdaAnyImpl) uriPathInfo).getExpression());
} else {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
return null;
}
public ResourceValidator goSelectItem(final int index) {
SelectOptionImpl select = (SelectOptionImpl) uriInfo.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
UriInfoImpl uriInfo1 = (UriInfoImpl) item.getResourcePath();
return new ResourceValidator()
.setUpValidator(this)
.setEdm(edm)
.setUriInfoImplPath(uriInfo1);
}
public ExpandValidator goExpand() {
ExpandOptionImpl expand = (ExpandOptionImpl) uriInfo.getExpandOption();
if (expand == null) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
return new ExpandValidator().setUpValidator(this).setExpand(expand);
}
public ResourceValidator first() {
uriResourceIndex = 0;
uriPathInfo = (UriResourceImpl) uriInfo.getUriResourceParts().get(0);
return this;
}
public ResourceValidator last() {
uriResourceIndex = 0;
try {
uriPathInfo = (UriResourceImpl) uriInfo.getUriResourceParts().get(uriInfo.getUriResourceParts().size() - 1);
uriResourceIndex = uriInfo.getUriResourceParts().size() - 1;
} catch (IndexOutOfBoundsException ex) {
fail("not enough segments");
}
return this;
}
public ResourceValidator n() {
uriResourceIndex++;
try {
uriPathInfo = (UriResourceImpl) uriInfo.getUriResourceParts().get(uriResourceIndex);
} catch (IndexOutOfBoundsException ex) {
fail("not enough segments");
}
return this;
}
public ResourceValidator at(final int index) {
uriResourceIndex = index;
try {
uriPathInfo = (UriResourceImpl) uriInfo.getUriResourceParts().get(index);
} catch (IndexOutOfBoundsException ex) {
fail("not enough segments");
}
return this;
}
// --- Validation ---
public ResourceValidator isLambdaVar(final String var) {
String actualVar = null;
if (uriPathInfo.getKind() == UriResourceKind.lambdaAll) {
actualVar = ((UriResourceLambdaAllImpl) uriPathInfo).getLambdaVariable();
} else if (uriPathInfo.getKind() == UriResourceKind.lambdaAny) {
actualVar = ((UriResourceLambdaAnyImpl) uriPathInfo).getLamdaVariable();
} else {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
assertEquals(var, actualVar);
return this;
}
public ResourceValidator isTypeFilter(final FullQualifiedName expectedType) {
if (uriPathInfo.getKind() != UriResourceKind.complexProperty &&
uriPathInfo.getKind() != UriResourceKind.singleton) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
EdmType actualType = null;
if (uriPathInfo instanceof UriResourceComplexPropertyImpl) {
actualType = ((UriResourceComplexPropertyImpl) uriPathInfo).getComplexTypeFilter();
} else if (uriPathInfo instanceof UriResourceSingletonImpl) {
actualType = ((UriResourceSingletonImpl) uriPathInfo).getEntityTypeFilter();
}
if (actualType == null) {
fail("type information not set");
}
FullQualifiedName actualName = new FullQualifiedName(actualType.getNamespace(), actualType.getName());
assertEquals(expectedType.toString(), actualName.toString());
return this;
}
public ResourceValidator isType(final FullQualifiedName type) {
if (!(uriPathInfo instanceof UriResourcePartTyped)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourcePartTyped uriPathInfoTyped = (UriResourcePartTyped) uriPathInfo;
EdmType actualType = uriPathInfoTyped.getType();
if (actualType == null) {
fail("type information not set");
}
FullQualifiedName actualName = new FullQualifiedName(actualType.getNamespace(), actualType.getName());
assertEquals(type.toString(), actualName.toString());
return this;
}
public ResourceValidator isType(final FullQualifiedName type, final boolean isFinallyACollection) {
isType(type);
assertEquals(isFinallyACollection, ((UriResourcePartTyped) uriPathInfo).isCollection());
return this;
}
public ResourceValidator isTypeFilterOnEntry(final FullQualifiedName type) {
if (!(uriPathInfo instanceof UriResourceWithKeysImpl)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourceWithKeysImpl uriPathInfoKeyPred = (UriResourceWithKeysImpl) uriPathInfo;
// input parameter type may be null in order to assert that the singleTypeFilter is not set
EdmType actualType = uriPathInfoKeyPred.getTypeFilterOnEntry();
if (type == null) {
assertEquals(type, actualType);
} else {
assertEquals(type.toString(), new FullQualifiedName(actualType.getNamespace(), actualType.getName()).toString());
}
return this;
}
public ResourceValidator isTypeFilterOnCollection(final FullQualifiedName expectedType) {
if (!(uriPathInfo instanceof UriResourceWithKeysImpl)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourceWithKeysImpl uriPathInfoKeyPred = (UriResourceWithKeysImpl) uriPathInfo;
// input parameter type may be null in order to assert that the collectionTypeFilter is not set
EdmType actualType = uriPathInfoKeyPred.getTypeFilterOnCollection();
if (expectedType == null) {
assertEquals(expectedType, actualType);
} else {
FullQualifiedName actualName = new FullQualifiedName(actualType.getNamespace(), actualType.getName());
assertEquals(expectedType.toString(), actualName.toString());
}
return this;
}
// other functions
public ResourceValidator checkCustomParameter(final int index, final String name, final String value) {
if (uriInfo == null) {
fail("hasQueryParameter: uriInfo == null");
}
List<CustomQueryOption> list = uriInfo.getCustomQueryOptions();
if (list.size() <= index) {
fail("not enough queryParameters");
}
CustomQueryOptionImpl option = (CustomQueryOptionImpl) list.get(index);
assertEquals(name, option.getName());
assertEquals(value, option.getText());
return this;
}
// TODO remove
/*
* public ResourceValidator isCollection(final boolean isCollection) {
* if (!(uriPathInfo instanceof UriResourcePartTyped)) {
* fail("invalid resource kind: " + uriPathInfo.getKind().toString());
* }
* UriResourcePartTyped uriPathInfoTyped = (UriResourcePartTyped) uriPathInfo;
*
* EdmType type = uriPathInfoTyped.getType();
* if (type == null) {
* fail("isCollection: type == null");
* }
* assertEquals(isCollection, uriPathInfoTyped.isCollection());
* return this;
* }
*/
public ResourceValidator isFilterString(final String expectedFilterTreeAsString) {
ExpressionImpl filterTree = (ExpressionImpl) uriInfo.getFilterOption().getExpression();
try {
String filterTreeAsString = filterTree.accept(new FilterTreeToText());
assertEquals(expectedFilterTreeAsString, filterTreeAsString);
} catch (ExpressionVisitException e) {
fail("isFilterString: Exception " + e.getMessage() + " occured");
} catch (ODataApplicationException e) {
fail("isFilterString: Exception " + e.getMessage() + " occured");
}
return this;
}
public ResourceValidator isKeyPredicateRef(final int index, final String name, final String refencedProperty) {
if (!(uriPathInfo instanceof UriResourceWithKeysImpl)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourceWithKeysImpl info = (UriResourceWithKeysImpl) uriPathInfo;
List<UriParameter> keyPredicates = info.getKeyPredicates();
assertEquals(name, keyPredicates.get(index).getName());
assertEquals(refencedProperty, keyPredicates.get(index).getRefencedProperty());
return this;
}
public ResourceValidator isKeyPredicateAlias(final int index, final String name, final String alias) {
if (!(uriPathInfo instanceof UriResourceWithKeysImpl)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourceWithKeysImpl info = (UriResourceWithKeysImpl) uriPathInfo;
List<UriParameter> keyPredicates = info.getKeyPredicates();
assertEquals(name, keyPredicates.get(index).getName());
assertEquals(alias, keyPredicates.get(index).getAlias());
return this;
}
public ResourceValidator isKeyPredicate(final int index, final String name, final String text) {
if (!(uriPathInfo instanceof UriResourceWithKeysImpl)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourceWithKeysImpl info = (UriResourceWithKeysImpl) uriPathInfo;
List<UriParameter> keyPredicates = info.getKeyPredicates();
assertEquals(name, keyPredicates.get(index).getName());
assertEquals(text, keyPredicates.get(index).getText());
return this;
}
public ResourceValidator isParameter(final int index, final String name, final String text) {
if (!(uriPathInfo instanceof UriResourceFunctionImpl)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourceFunctionImpl info = (UriResourceFunctionImpl) uriPathInfo;
List<UriParameter> keyPredicates = info.getParameters();
assertEquals(name, keyPredicates.get(index).getName());
assertEquals(text, keyPredicates.get(index).getText());
return this;
}
public ResourceValidator isParameterAlias(final int index, final String name, final String alias) {
if (!(uriPathInfo instanceof UriResourceFunctionImpl)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourceFunctionImpl info = (UriResourceFunctionImpl) uriPathInfo;
List<UriParameter> keyPredicates = info.getParameters();
assertEquals(name, keyPredicates.get(index).getName());
assertEquals(alias, keyPredicates.get(index).getAlias());
return this;
}
public ResourceValidator isKind(final UriInfoKind kind) {
assertEquals(kind, uriInfo.getKind());
return this;
}
public ResourceValidator isPrimitiveProperty(final String name,
final FullQualifiedName type, final boolean isCollection) {
if (!(uriPathInfo instanceof UriResourcePrimitivePropertyImpl)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourcePrimitivePropertyImpl uriPathInfoProp = (UriResourcePrimitivePropertyImpl) uriPathInfo;
EdmElement property = uriPathInfoProp.getProperty();
assertEquals(name, property.getName());
assertEquals(type, new FullQualifiedName(property.getType().getNamespace(), property.getType().getName()));
assertEquals(isCollection, property.isCollection());
return this;
}
public ResourceValidator
isComplexProperty(final String name, final FullQualifiedName type, final boolean isCollection) {
if (!(uriPathInfo instanceof UriResourceComplexPropertyImpl)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourceComplexPropertyImpl uriPathInfoProp = (UriResourceComplexPropertyImpl) uriPathInfo;
EdmElement property = uriPathInfoProp.getProperty();
assertEquals(name, property.getName());
assertEquals(type, new FullQualifiedName(property.getType().getNamespace(), property.getType().getName()));
assertEquals(isCollection, property.isCollection());
return this;
}
public ResourceValidator isNavProperty(final String name, final FullQualifiedName type, final boolean isCollection) {
if (!(uriPathInfo instanceof UriResourceNavigationPropertyImpl)) {
fail("invalid resource kind: " + uriPathInfo.getKind().toString());
}
UriResourceNavigationPropertyImpl uriPathInfoProp = (UriResourceNavigationPropertyImpl) uriPathInfo;
EdmElement property = uriPathInfoProp.getProperty();
assertEquals(name, property.getName());
assertEquals(type, new FullQualifiedName(property.getType().getNamespace(), property.getType().getName()));
assertEquals(isCollection, uriPathInfoProp.isCollection());
return this;
}
public ResourceValidator isUriPathInfoKind(final UriResourceKind infoType) {
assertNotNull(uriPathInfo);
assertEquals(infoType, uriPathInfo.getKind());
return this;
}
public ResourceValidator isAction(final String name) {
assertEquals(UriResourceKind.action, uriPathInfo.getKind());
assertEquals(name, ((UriResourceActionImpl) uriPathInfo).getAction().getName());
return this;
}
public ResourceValidator isFunction(final String name) {
assertEquals(UriResourceKind.function, uriPathInfo.getKind());
assertEquals(name, ((UriResourceFunctionImpl) uriPathInfo).getFunction().getName());
return this;
}
public ResourceValidator isFunctionImport(final String name) {
assertEquals(UriResourceKind.function, uriPathInfo.getKind());
assertEquals(name, ((UriResourceFunctionImpl) uriPathInfo).getFunctionImport().getName());
return this;
}
public ResourceValidator isEntitySet(final String name) {
assertEquals(UriResourceKind.entitySet, uriPathInfo.getKind());
assertEquals(name, ((UriResourceEntitySetImpl) uriPathInfo).getEntitySet().getName());
return this;
}
public ResourceValidator isComplex(final String name) {
assertEquals(UriResourceKind.complexProperty, uriPathInfo.getKind());
assertEquals(name, ((UriResourceComplexPropertyImpl) uriPathInfo).getProperty().getName());
return this;
}
public ResourceValidator isSingleton(final String name) {
assertEquals(UriResourceKind.singleton, uriPathInfo.getKind());
assertEquals(name, ((UriResourceSingletonImpl) uriPathInfo).getSingleton().getName());
return this;
}
public ResourceValidator isValue() {
assertEquals(UriResourceKind.value, uriPathInfo.getKind());
return this;
}
public ResourceValidator isCount() {
assertEquals(UriResourceKind.count, uriPathInfo.getKind());
return this;
}
public ResourceValidator isRef() {
assertEquals(UriResourceKind.ref, uriPathInfo.getKind());
return this;
}
public ResourceValidator isActionImport(final String actionName) {
assertEquals(UriResourceKind.action, uriPathInfo.getKind());
assertEquals(actionName, ((UriResourceActionImpl) uriPathInfo).getActionImport().getName());
return this;
}
public ResourceValidator isIt() {
assertEquals(UriResourceKind.it, uriPathInfo.getKind());
return this;
}
public ResourceValidator isTopText(final String topText) {
assertEquals(topText, uriInfo.getTopOption().getText());
return this;
}
public ResourceValidator isFormatText(final String formatText) {
assertEquals(formatText, uriInfo.getFormatOption().getText());
return this;
}
public ResourceValidator isInlineCountText(final String inlineCountText) {
assertEquals(inlineCountText, uriInfo.getCountOption().getText());
return this;
}
public ResourceValidator isSkipText(final String skipText) {
assertEquals(skipText, uriInfo.getSkipOption().getText());
return this;
}
public ResourceValidator isSkipTokenText(final String skipTokenText) {
assertEquals(skipTokenText, uriInfo.getSkipTokenOption().getText());
return this;
}
public ResourceValidator isSelectItemStar(final int index) {
SelectOptionImpl select = (SelectOptionImpl) uriInfo.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
assertEquals(true, item.isStar());
return this;
}
public ResourceValidator isSelectItemAllOp(final int index, final FullQualifiedName fqn) {
SelectOptionImpl select = (SelectOptionImpl) uriInfo.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
assertEquals(fqn.toString(), item.getAllOperationsInSchemaNameSpace().toString());
return this;
}
public ResourceValidator isSelectStartType(final int index, final FullQualifiedName fullName) {
SelectOptionImpl select = (SelectOptionImpl) uriInfo.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
EdmType actualType = item.getStartTypeFilter();
FullQualifiedName actualName = new FullQualifiedName(actualType.getNamespace(), actualType.getName());
assertEquals(fullName, actualName);
return this;
}
}

View File

@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import java.util.BitSet;
import java.util.Collections;
import java.util.List;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;
import org.apache.olingo.server.core.uri.antlr.UriLexer;
class TestErrorLogger implements ANTLRErrorListener {
private String prefix;
private int logLevel = 0;
public TestErrorLogger(final String prefix, final int logLevel) {
this.prefix = prefix;
this.logLevel = logLevel;
}
@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line,
final int charPositionInLine,
final String msg, final RecognitionException e) {
if (logLevel > 0) {
System.out.println("\n" + prefix + " -- SyntaxError");
trace(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
}
}
@Override
public void reportAmbiguity(final Parser recognizer, final DFA dfa, final int startIndex, final int stopIndex,
final boolean exact,
final BitSet ambigAlts, final ATNConfigSet configs) {
}
@Override
public void reportAttemptingFullContext(final Parser recognizer, final DFA dfa, final int startIndex,
final int stopIndex,
final BitSet conflictingAlts, final ATNConfigSet configs) {
}
@Override
public void reportContextSensitivity(final Parser recognizer, final DFA dfa, final int startIndex,
final int stopIndex, final int prediction,
final ATNConfigSet configs) {
}
private void printStack(final Recognizer<?, ?> recognizer) {
List<String> stack = ((Parser) recognizer).getRuleInvocationStack();
Collections.reverse(stack);
System.out.println(" rule stack: " + stack);
}
public void trace(final Recognizer<?, ?> recognizer, final Object offendingSymbol,
final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
System.out.println("Error message: " + msg);
printStack(recognizer);
System.out.println(" line/char :" + line + " / " + charPositionInLine);
System.out.println(" sym :" + offendingSymbol);
if (e != null && e.getOffendingToken() != null) {
String lexerTokenName = "";
try {
lexerTokenName = UriLexer.tokenNames[e.getOffendingToken().getType()];
} catch (ArrayIndexOutOfBoundsException es) {
lexerTokenName = "token error";
}
System.out.println(" tokenname:" + lexerTokenName);
}
}
}

View File

@ -0,0 +1,258 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.List;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmEntityType;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.server.api.uri.UriInfoKind;
import org.apache.olingo.server.api.uri.queryoption.CustomQueryOption;
import org.apache.olingo.server.api.uri.queryoption.SelectItem;
import org.apache.olingo.server.core.uri.UriInfoImpl;
import org.apache.olingo.server.core.uri.parser.Parser;
import org.apache.olingo.server.core.uri.parser.UriParserException;
import org.apache.olingo.server.core.uri.parser.UriParserSemanticException;
import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException;
import org.apache.olingo.server.core.uri.queryoption.CustomQueryOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.ExpandOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.FilterOptionImpl;
import org.apache.olingo.server.core.uri.queryoption.SelectOptionImpl;
import org.apache.olingo.server.core.uri.validator.UriValidator;
public class TestUriValidator implements TestValidator {
private Edm edm;
private UriInfoImpl uriInfo;
private Exception exception;
// Setup
public TestUriValidator setEdm(final Edm edm) {
this.edm = edm;
return this;
}
// Execution
public TestUriValidator run(final String uri) {
Parser parser = new Parser();
UriValidator validator = new UriValidator();
uriInfo = null;
try {
uriInfo = (UriInfoImpl) parser.parseUri(uri, edm);
validator.validate(uriInfo, "GET");
} catch (Exception e) {
throw new RuntimeException(e);
}
return this;
}
public TestUriValidator runEx(final String uri) {
Parser parser = new Parser();
uriInfo = null;
try {
uriInfo = (UriInfoImpl) parser.parseUri(uri, edm);
fail("Exception expected");
} catch (UriParserException e) {
exception = e;
}
return this;
}
public TestUriValidator log(final String uri) {
ParserWithLogging parserTest = new ParserWithLogging();
parserTest.setLogLevel(1);
uriInfo = null;
try {
// uriInfoTmp = new UriParserImpl(edm).ParseUri(uri);
uriInfo = (UriInfoImpl) parserTest.parseUri(uri, edm);
} catch (UriParserException e) {
fail("Exception occured while parsing the URI: " + uri + "\n"
+ " Exception: " + e.getMessage());
}
return this;
}
// Navigation
public ResourceValidator goPath() {
if (uriInfo.getKind() != UriInfoKind.resource) {
fail("invalid resource kind: " + uriInfo.getKind().toString());
}
return new ResourceValidator()
.setUpValidator(this)
.setEdm(edm)
.setUriInfoImplPath(uriInfo);
}
public FilterValidator goFilter() {
FilterOptionImpl filter = (FilterOptionImpl) uriInfo.getFilterOption();
if (filter == null) {
fail("no filter found");
}
return new FilterValidator().setUriValidator(this).setFilter(filter);
}
public ExpandValidator goExpand() {
ExpandOptionImpl expand = (ExpandOptionImpl) uriInfo.getExpandOption();
if (expand == null) {
fail("invalid resource kind: " + uriInfo.getKind().toString());
}
return new ExpandValidator().setUpValidator(this).setExpand(expand);
}
public ResourceValidator goSelectItemPath(final int index) {
SelectOptionImpl select = (SelectOptionImpl) uriInfo.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
UriInfoImpl uriInfo1 = (UriInfoImpl) item.getResourcePath();
return new ResourceValidator()
.setUpValidator(this)
.setEdm(edm)
.setUriInfoImplPath(uriInfo1);
}
public TestUriValidator isSelectStartType(final int index, final FullQualifiedName fullName) {
SelectOptionImpl select = (SelectOptionImpl) uriInfo.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
EdmType actualType = item.getStartTypeFilter();
FullQualifiedName actualName = new FullQualifiedName(actualType.getNamespace(), actualType.getName());
assertEquals(fullName, actualName);
return this;
}
// Validation
public TestUriValidator isKind(final UriInfoKind kind) {
assertEquals(kind, uriInfo.getKind());
return this;
}
public TestUriValidator isCustomParameter(final int index, final String name, final String value) {
if (uriInfo == null) {
fail("hasQueryParameter: uriInfo == null");
}
List<CustomQueryOption> list = uriInfo.getCustomQueryOptions();
if (list.size() <= index) {
fail("not enought queryParameters");
}
CustomQueryOptionImpl option = (CustomQueryOptionImpl) list.get(index);
assertEquals(name, option.getName());
assertEquals(value, option.getText());
return this;
}
public void isCrossJoinEntityList(final List<String> entitySets) {
if (uriInfo.getKind() != UriInfoKind.crossjoin) {
fail("invalid resource kind: " + uriInfo.getKind().toString());
}
int i = 0;
for (String entitySet : entitySets) {
assertEquals(entitySet, uriInfo.getEntitySetNames().get(i));
i++;
}
}
public TestUriValidator isExSyntax(final long errorID) {
assertEquals(UriParserSyntaxException.class, exception.getClass());
return this;
}
public TestUriValidator isExSemantic(final long errorID) {
assertEquals(UriParserSemanticException.class, exception.getClass());
return this;
}
public TestUriValidator isIdText(final String text) {
assertEquals(text, uriInfo.getIdOption().getText());
return this;
}
public TestUriValidator isExpandText(final String text) {
assertEquals(text, uriInfo.getExpandOption().getText());
return this;
}
public TestUriValidator isSelectText(final String text) {
assertEquals(text, uriInfo.getSelectOption().getText());
return this;
}
public TestUriValidator isFormatText(final String text) {
assertEquals(text, uriInfo.getFormatOption().getText());
return this;
}
public TestUriValidator isFragmentText(final String text) {
if (uriInfo.getKind() != UriInfoKind.metadata) {
fail("invalid resource kind: " + uriInfo.getKind().toString());
}
assertEquals(text, uriInfo.getFragment());
return this;
}
public TestUriValidator isEntityType(final FullQualifiedName fullName) {
if (uriInfo.getKind() != UriInfoKind.entityId) {
fail("invalid resource kind: " + uriInfo.getKind().toString());
}
assertEquals(fullName.toString(), fullName(uriInfo.getEntityTypeCast()));
return this;
}
private String fullName(final EdmEntityType type) {
return type.getNamespace() + "." + type.getName();
}
public TestUriValidator isSelectItemStar(final int index) {
SelectOptionImpl select = (SelectOptionImpl) uriInfo.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
assertEquals(true, item.isStar());
return this;
}
public TestUriValidator isSelectItemAllOp(final int index, final FullQualifiedName fqn) {
SelectOptionImpl select = (SelectOptionImpl) uriInfo.getSelectOption();
SelectItem item = select.getSelectItems().get(index);
assertEquals(fqn.toString(), item.getAllOperationsInSchemaNameSpace().toString());
return this;
}
}

View File

@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
public interface TestValidator {
}

View File

@ -0,0 +1,194 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.Token;
import org.apache.olingo.server.core.uri.antlr.UriLexer;
//TODO extend to test also exception which can occure while paring
public class TokenValidator {
private String input = null;
private List<? extends Token> tokens = null;
private Token curToken = null;
private Exception curException = null;
private int startMode;
private int logLevel = 0;
// --- Setup ---
public TokenValidator log(final int logLevel) {
this.logLevel = logLevel;
return this;
}
// --- Execution ---
public TokenValidator run(final String uri) {
input = uri;
tokens = parseInput(uri);
if (logLevel > 0) {
showTokens();
}
first();
exFirst();
logLevel = 0;
return this;
}
// --- Navigation ---
// navigate within the tokenlist
public TokenValidator first() {
try {
curToken = tokens.get(0);
} catch (IndexOutOfBoundsException ex) {
curToken = null;
}
return this;
}
public TokenValidator last() {
curToken = tokens.get(tokens.size() - 1);
return this;
}
public TokenValidator at(final int index) {
try {
curToken = tokens.get(index);
} catch (IndexOutOfBoundsException ex) {
curToken = null;
}
return this;
}
public TokenValidator exLast() {
// curException = exceptions.get(exceptions.size() - 1);
return this;
}
// navigate within the exception list
public TokenValidator exFirst() {
try {
// curException = exceptions.get(0);
} catch (IndexOutOfBoundsException ex) {
curException = null;
}
return this;
}
public TokenValidator exAt(final int index) {
try {
// curException = exceptions.get(index);
} catch (IndexOutOfBoundsException ex) {
curException = null;
}
return this;
}
// --- Validation ---
public TokenValidator isText(final String expected) {
assertEquals(expected, curToken.getText());
return this;
}
public TokenValidator isAllText(final String expected) {
String actual = "";
for (Token curToken : tokens) {
actual += curToken.getText();
}
assertEquals(expected, actual);
return this;
}
public TokenValidator isAllInput() {
String actual = "";
for (Token curToken : tokens) {
actual += curToken.getText();
}
assertEquals(input, actual);
return this;
}
public TokenValidator isInput() {
assertEquals(input, curToken.getText());
return this;
}
public TokenValidator isType(final int expected) {
assertEquals(UriLexer.tokenNames[expected], UriLexer.tokenNames[curToken.getType()]);
return this;
}
public TokenValidator isExType(final Class<?> exClass) {
assertEquals(exClass, curException.getClass());
return this;
}
public void globalMode(final int mode) {
startMode = mode;
}
// --- Helper ---
private List<? extends Token> parseInput(final String input) {
ANTLRInputStream inputStream = new ANTLRInputStream(input);
UriLexer lexer = new UriLexerWithTrace(inputStream, logLevel, startMode);
// lexer.addErrorListener(new ErrorCollector(this));
return lexer.getAllTokens();
}
public TokenValidator showTokens() {
boolean first = true;
System.out.println("input: " + input);
String nL = "\n";
String out = "[" + nL;
for (Token token : tokens) {
if (!first) {
out += ",";
first = false;
}
int index = token.getType();
if (index != -1) {
out += "\"" + token.getText() + "\"" + " " + UriLexer.tokenNames[index] + nL;
} else {
out += "\"" + token.getText() + "\"" + " " + index + nL;
}
}
out += ']';
System.out.println("tokens: " + out);
return this;
}
}

View File

@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.testutil;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.Token;
import org.apache.olingo.server.core.uri.antlr.UriLexer;
public class UriLexerWithTrace extends UriLexer {
int logLevel = 0;
public UriLexerWithTrace(final ANTLRInputStream antlrInputStream, final int logLevel) {
super(antlrInputStream);
this.logLevel = logLevel;
}
public UriLexerWithTrace(final ANTLRInputStream antlrInputStream, final int logLevel, final int mode) {
super(antlrInputStream);
super.mode(mode);
this.logLevel = logLevel;
}
@Override
public void emit(final Token token) {
if (logLevel > 1) {
String out = String.format("%1$-" + 20 + "s", token.getText());
int tokenType = token.getType();
if (tokenType == -1) {
out += "-1/EOF";
} else {
out += UriLexer.tokenNames[tokenType];
}
System.out.println("Lexer.emit(...):" + out);
}
super.emit(token);
}
@Override
public void pushMode(final int m) {
String out = UriLexer.modeNames[_mode] + "-->";
super.pushMode(m);
out += UriLexer.modeNames[_mode];
if (logLevel > 1) {
System.out.println(out + " ");
}
}
@Override
public int popMode() {
String out = UriLexer.modeNames[_mode] + "-->";
int m = super.popMode();
out += UriLexer.modeNames[_mode];
if (logLevel > 1) {
System.out.println(out + " ");
}
return m;
}
}

View File

@ -0,0 +1,378 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.validator;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.core.edm.provider.EdmProviderImpl;
import org.apache.olingo.server.core.uri.parser.Parser;
import org.apache.olingo.server.core.uri.parser.UriParserException;
import org.apache.olingo.server.tecsvc.provider.EdmTechProvider;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class UriValidatorTest {
private static final String URI_ALL = "$all";
private static final String URI_BATCH = "$batch";
private static final String URI_CROSSJOIN = "$crossjoin(ESAllPrim)";
private static final String URI_ENTITY_ID = "/$entity";
private static final String URI_METADATA = "$metadata";
private static final String URI_SERVICE = "";
private static final String URI_ENTITY_SET = "/ESAllPrim";
private static final String URI_ENTITY_SET_COUNT = "/ESAllPrim/$count";
private static final String URI_ENTITY = "/ESAllPrim(1)";
private static final String URI_MEDIA_STREAM = "/ESMedia(1)/$value";
private static final String URI_REFERENCES = "/ESAllPrim/$ref";
private static final String URI_REFERENCE = "/ESAllPrim(1)/$ref";
private static final String URI_PROPERTY_COMPLEX = "/ESCompComp(1)/PropertyComplex";
private static final String URI_PROPERTY_COMPLEX_COLLECTION =
"/ESCompCollComp(1)/PropertyComplex/CollPropertyComplex";
private static final String URI_PROPERTY_COMPLEX_COLLECTION_COUNT =
"/ESCompCollComp(1)/PropertyComplex/CollPropertyComplex/$count";
private static final String URI_PROPERTY_PRIMITIVE = "/ESAllPrim(1)/PropertyString";
private static final String URI_PROPERTY_PRIMITIVE_COLLECTION = "/ESCollAllPrim/CollPropertyString";
private static final String URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT =
"/ESCollAllPrim/CollPropertyString/$count";
private static final String URI_PROPERTY_PRIMITIVE_VALUE = "/ESAllPrim(1)/PropertyString/$value";
private static final String URI_SINGLETON = "/SI";
private static final String URI_NAV_ENTITY = "/ESKeyNav/NavPropertyETKeyNavOne";
private static final String URI_NAV_ENTITY_SET = "/ESKeyNav/NavPropertyETKeyNavMany";
private static final String QO_FILTER = "$filter='1' eq '1'";
private static final String QO_FORMAT = "$format=bla";
private static final String QO_EXPAND = "$expand=*";
private static final String QO_ID = "$id=Products(0)";
private static final String QO_COUNT = "$count";
private static final String QO_ORDERBY = "$orderby=true";
// private static final String QO_SEARCH = "$search='bla'";
private static final String QO_SELECT = "$select=*";
private static final String QO_SKIP = "$skip=3";
private static final String QO_SKIPTOKEN = "$skiptoken=123";
private static final String QO_LEVELS = "$expand=*($levels=1)";
private static final String QO_TOP = "$top=1";
private String[][] urisWithValidSystemQueryOptions = {
{ URI_ALL, QO_FILTER, }, { URI_ALL, QO_FORMAT }, { URI_ALL, QO_EXPAND }, { URI_ALL, QO_COUNT },
{ URI_ALL, QO_ORDERBY }, /* { URI_ALL, QO_SEARCH }, */{ URI_ALL, QO_SELECT }, { URI_ALL, QO_SKIP },
{ URI_ALL, QO_SKIPTOKEN }, { URI_ALL, QO_LEVELS },
{ URI_CROSSJOIN, QO_FILTER, }, { URI_CROSSJOIN, QO_FORMAT },
{ URI_CROSSJOIN, QO_EXPAND }, { URI_CROSSJOIN, QO_COUNT }, { URI_CROSSJOIN, QO_ORDERBY },
/* { URI_CROSSJOIN, QO_SEARCH }, */{ URI_CROSSJOIN, QO_SELECT }, { URI_CROSSJOIN, QO_SKIP },
{ URI_CROSSJOIN, QO_SKIPTOKEN }, { URI_CROSSJOIN, QO_LEVELS }, { URI_CROSSJOIN, QO_TOP },
{ URI_ENTITY_ID, QO_ID, QO_FORMAT }, { URI_ENTITY_ID, QO_ID, }, { URI_ENTITY_ID, QO_ID, QO_EXPAND },
{ URI_ENTITY_ID, QO_ID, QO_SELECT }, { URI_ENTITY_ID, QO_ID, QO_LEVELS },
{ URI_METADATA, QO_FORMAT },
{ URI_SERVICE, QO_FORMAT },
{ URI_ENTITY_SET, QO_FILTER, }, { URI_ENTITY_SET, QO_FORMAT }, { URI_ENTITY_SET, QO_EXPAND },
{ URI_ENTITY_SET, QO_COUNT }, { URI_ENTITY_SET, QO_ORDERBY }, /* { URI_ENTITY_SET, QO_SEARCH }, */
{ URI_ENTITY_SET, QO_SELECT },
{ URI_ENTITY_SET, QO_SKIP }, { URI_ENTITY_SET, QO_SKIPTOKEN }, { URI_ENTITY_SET, QO_LEVELS },
{ URI_ENTITY_SET, QO_TOP },
{ URI_ENTITY_SET_COUNT, QO_FILTER }, /* { URI_ENTITY_SET_COUNT, QO_SEARCH }, */
{ URI_ENTITY, QO_FORMAT }, { URI_ENTITY, QO_EXPAND }, { URI_ENTITY, QO_SELECT }, { URI_ENTITY, QO_LEVELS },
{ URI_MEDIA_STREAM, QO_FORMAT },
{ URI_REFERENCES, QO_FILTER }, { URI_REFERENCES, QO_FORMAT }, { URI_REFERENCES, QO_ORDERBY },
/* { URI_REFERENCES, QO_SEARCH }, */{ URI_REFERENCES, QO_SKIP }, { URI_REFERENCES, QO_SKIPTOKEN },
{ URI_REFERENCES, QO_TOP },
{ URI_REFERENCE, QO_FORMAT },
{ URI_PROPERTY_COMPLEX, QO_FORMAT }, { URI_PROPERTY_COMPLEX, QO_SELECT }, { URI_PROPERTY_COMPLEX, QO_EXPAND },
{ URI_PROPERTY_COMPLEX, QO_LEVELS },
{ URI_PROPERTY_COMPLEX_COLLECTION, QO_FILTER }, { URI_PROPERTY_COMPLEX_COLLECTION, QO_FORMAT },
{ URI_PROPERTY_COMPLEX_COLLECTION, QO_EXPAND }, { URI_PROPERTY_COMPLEX_COLLECTION, QO_COUNT },
{ URI_PROPERTY_COMPLEX_COLLECTION, QO_SKIP }, { URI_PROPERTY_COMPLEX_COLLECTION, QO_SKIPTOKEN },
{ URI_PROPERTY_COMPLEX_COLLECTION, QO_LEVELS }, { URI_PROPERTY_COMPLEX_COLLECTION, QO_TOP },
{ URI_PROPERTY_COMPLEX_COLLECTION, QO_ORDERBY },
{ URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_FILTER }, /* { URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_SEARCH }, */
{ URI_PROPERTY_PRIMITIVE, QO_FORMAT },
{ URI_PROPERTY_PRIMITIVE_COLLECTION, QO_FILTER }, { URI_PROPERTY_PRIMITIVE_COLLECTION, QO_FORMAT },
{ URI_PROPERTY_PRIMITIVE_COLLECTION, QO_ORDERBY }, { URI_PROPERTY_PRIMITIVE_COLLECTION, QO_SKIP },
{ URI_PROPERTY_PRIMITIVE_COLLECTION, QO_SKIPTOKEN }, { URI_PROPERTY_PRIMITIVE_COLLECTION, QO_TOP },
{ URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_FILTER },
/* { URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_SEARCH }, */
{ URI_PROPERTY_PRIMITIVE_VALUE, QO_FORMAT },
{ URI_SINGLETON, QO_FORMAT }, { URI_SINGLETON, QO_EXPAND }, { URI_SINGLETON, QO_SELECT },
{ URI_SINGLETON, QO_LEVELS },
{ URI_NAV_ENTITY, QO_FORMAT }, { URI_NAV_ENTITY, QO_EXPAND }, { URI_NAV_ENTITY, QO_SELECT },
{ URI_NAV_ENTITY, QO_LEVELS },
{ URI_NAV_ENTITY_SET, QO_FILTER, }, { URI_NAV_ENTITY_SET, QO_FORMAT }, { URI_NAV_ENTITY_SET, QO_EXPAND },
{ URI_NAV_ENTITY_SET, QO_COUNT }, { URI_NAV_ENTITY_SET, QO_ORDERBY },
/* { URI_NAV_ENTITY_SET, QO_SEARCH }, */{ URI_NAV_ENTITY_SET, QO_SELECT }, { URI_NAV_ENTITY_SET, QO_SKIP },
{ URI_NAV_ENTITY_SET, QO_SKIPTOKEN }, { URI_NAV_ENTITY_SET, QO_LEVELS }, { URI_NAV_ENTITY_SET, QO_TOP },
{ "FINRTInt16()" },
{ "FICRTETKeyNav()" },
{ "FICRTESTwoKeyNavParam(ParameterInt16=1)" },
{ "FICRTCollString()" },
{ "FICRTCTTwoPrim()" },
{ "FICRTCollCTTwoPrim()" },
{ "FICRTETMedia()" },
{ "ESTwoKeyNav/com.sap.odata.test1.BAESTwoKeyNavRTESTwoKeyNav" },
{ "ESAllPrim/com.sap.odata.test1.BAESAllPrimRTETAllPrim" },
{ "AIRTPrimCollParam" },
{ "AIRTETParam" },
{ "AIRTPrimParam" },
};
private String[][] urisWithNonValidSystemQueryOptions = {
{ URI_ALL, QO_ID, }, { URI_ALL, QO_TOP },
{ URI_BATCH, QO_FILTER, }, { URI_BATCH, QO_FORMAT }, { URI_BATCH, QO_ID, }, { URI_BATCH, QO_EXPAND },
{ URI_BATCH, QO_COUNT }, { URI_BATCH, QO_ORDERBY }, /* { URI_BATCH, QO_SEARCH }, */{ URI_BATCH, QO_SELECT },
{ URI_BATCH, QO_SKIP }, { URI_BATCH, QO_SKIPTOKEN }, { URI_BATCH, QO_LEVELS }, { URI_BATCH, QO_TOP },
{ URI_CROSSJOIN, QO_ID, },
{ URI_ENTITY_ID, QO_ID, QO_FILTER, },
{ URI_ENTITY_ID, QO_ID, QO_COUNT }, { URI_ENTITY_ID, QO_ORDERBY }, /* { URI_ENTITY_ID, QO_SEARCH }, */
{ URI_ENTITY_ID, QO_ID, QO_SKIP }, { URI_ENTITY_ID, QO_ID, QO_SKIPTOKEN }, { URI_ENTITY_ID, QO_ID, QO_TOP },
{ URI_METADATA, QO_FILTER, }, { URI_METADATA, QO_ID, }, { URI_METADATA, QO_EXPAND },
{ URI_METADATA, QO_COUNT }, { URI_METADATA, QO_ORDERBY }, /* { URI_METADATA, QO_SEARCH }, */
{ URI_METADATA, QO_SELECT }, { URI_METADATA, QO_SKIP }, { URI_METADATA, QO_SKIPTOKEN },
{ URI_METADATA, QO_LEVELS }, { URI_METADATA, QO_TOP },
{ URI_SERVICE, QO_FILTER }, { URI_SERVICE, QO_ID }, { URI_SERVICE, QO_EXPAND }, { URI_SERVICE, QO_COUNT },
{ URI_SERVICE, QO_ORDERBY }, /* { URI_SERVICE, QO_SEARCH }, */{ URI_SERVICE, QO_SELECT },
{ URI_SERVICE, QO_SKIP }, { URI_SERVICE, QO_SKIPTOKEN }, { URI_SERVICE, QO_LEVELS }, { URI_SERVICE, QO_TOP },
{ URI_ENTITY_SET, QO_ID },
{ URI_ENTITY_SET_COUNT, QO_FORMAT }, { URI_ENTITY_SET_COUNT, QO_ID },
{ URI_ENTITY_SET_COUNT, QO_EXPAND }, { URI_ENTITY_SET_COUNT, QO_COUNT },
{ URI_ENTITY_SET_COUNT, QO_ORDERBY },
{ URI_ENTITY_SET_COUNT, QO_SELECT }, { URI_ENTITY_SET_COUNT, QO_SKIP }, { URI_ENTITY_SET_COUNT, QO_SKIPTOKEN },
{ URI_ENTITY_SET_COUNT, QO_LEVELS }, { URI_ENTITY_SET_COUNT, QO_TOP },
{ URI_ENTITY, QO_FILTER }, { URI_ENTITY, QO_ID }, { URI_ENTITY, QO_COUNT }, /* { URI_ENTITY, QO_ORDERBY }, */
/* { URI_ENTITY, QO_SEARCH }, */{ URI_ENTITY, QO_SKIP }, { URI_ENTITY, QO_SKIPTOKEN }, { URI_ENTITY, QO_TOP },
{ URI_MEDIA_STREAM, QO_FILTER }, { URI_MEDIA_STREAM, QO_ID, }, { URI_MEDIA_STREAM, QO_EXPAND },
{ URI_MEDIA_STREAM, QO_COUNT }, { URI_MEDIA_STREAM, QO_ORDERBY }, /* { URI_MEDIA_STREAM, QO_SEARCH }, */
{ URI_MEDIA_STREAM, QO_SELECT }, { URI_MEDIA_STREAM, QO_SKIP }, { URI_MEDIA_STREAM, QO_SKIPTOKEN },
{ URI_MEDIA_STREAM, QO_LEVELS }, { URI_MEDIA_STREAM, QO_TOP },
{ URI_REFERENCES, QO_ID, }, { URI_REFERENCES, QO_EXPAND }, { URI_REFERENCES, QO_COUNT },
{ URI_REFERENCES, QO_SELECT }, { URI_REFERENCES, QO_LEVELS },
{ URI_REFERENCE, QO_FILTER }, { URI_REFERENCE, QO_ID, }, { URI_REFERENCE, QO_EXPAND },
{ URI_REFERENCE, QO_COUNT }, { URI_REFERENCE, QO_ORDERBY }, /* { URI_REFERENCE, QO_SEARCH }, */
{ URI_REFERENCE, QO_SELECT }, { URI_REFERENCE, QO_SKIP }, { URI_REFERENCE, QO_SKIPTOKEN },
{ URI_REFERENCE, QO_LEVELS }, { URI_REFERENCE, QO_TOP },
{ URI_PROPERTY_COMPLEX, QO_FILTER }, { URI_PROPERTY_COMPLEX, QO_ID, }, { URI_PROPERTY_COMPLEX, QO_COUNT },
{ URI_PROPERTY_COMPLEX, QO_ORDERBY }, /* { URI_PROPERTY_COMPLEX, QO_SEARCH }, */
{ URI_PROPERTY_COMPLEX, QO_SKIP }, { URI_PROPERTY_COMPLEX, QO_SKIPTOKEN }, { URI_PROPERTY_COMPLEX, QO_TOP },
{ URI_PROPERTY_COMPLEX_COLLECTION, QO_ID, },
/* { URI_PROPERTY_COMPLEX_COLLECTION, QO_SEARCH }, */{ URI_PROPERTY_COMPLEX_COLLECTION, QO_SELECT },
{ URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_FORMAT },
{ URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_ID, }, { URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_EXPAND },
{ URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_COUNT }, { URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_ORDERBY },
{ URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_SELECT },
{ URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_SKIP }, { URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_SKIPTOKEN },
{ URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_LEVELS }, { URI_PROPERTY_COMPLEX_COLLECTION_COUNT, QO_TOP },
{ URI_PROPERTY_PRIMITIVE, QO_FILTER }, { URI_PROPERTY_PRIMITIVE, QO_ID, }, { URI_PROPERTY_PRIMITIVE, QO_EXPAND },
{ URI_PROPERTY_PRIMITIVE, QO_COUNT }, { URI_PROPERTY_PRIMITIVE, QO_ORDERBY },
/* { URI_PROPERTY_PRIMITIVE, QO_SEARCH }, */{ URI_PROPERTY_PRIMITIVE, QO_SELECT },
{ URI_PROPERTY_PRIMITIVE, QO_SKIP }, { URI_PROPERTY_PRIMITIVE, QO_SKIPTOKEN },
{ URI_PROPERTY_PRIMITIVE, QO_LEVELS }, { URI_PROPERTY_PRIMITIVE, QO_TOP },
{ URI_PROPERTY_PRIMITIVE_COLLECTION, QO_ID, }, { URI_PROPERTY_PRIMITIVE_COLLECTION, QO_EXPAND },
{ URI_PROPERTY_PRIMITIVE_COLLECTION, QO_COUNT }, /* { URI_PROPERTY_PRIMITIVE_COLLECTION, QO_SEARCH }, */
{ URI_PROPERTY_PRIMITIVE_COLLECTION, QO_SELECT }, { URI_PROPERTY_PRIMITIVE_COLLECTION, QO_LEVELS },
{ URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_FORMAT },
{ URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_ID, }, { URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_EXPAND },
{ URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_COUNT },
{ URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_ORDERBY },
{ URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_SELECT }, { URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_SKIP },
{ URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_SKIPTOKEN },
{ URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_LEVELS }, { URI_PROPERTY_PRIMITIVE_COLLECTION_COUNT, QO_TOP },
{ URI_PROPERTY_PRIMITIVE_VALUE, QO_FILTER }, { URI_PROPERTY_PRIMITIVE_VALUE, QO_ID, },
{ URI_PROPERTY_PRIMITIVE_VALUE, QO_EXPAND }, { URI_PROPERTY_PRIMITIVE_VALUE, QO_COUNT },
{ URI_PROPERTY_PRIMITIVE_VALUE, QO_ORDERBY },/* { URI_PROPERTY_PRIMITIVE_VALUE, QO_SEARCH }, */
{ URI_PROPERTY_PRIMITIVE_VALUE, QO_SELECT }, { URI_PROPERTY_PRIMITIVE_VALUE, QO_SKIP },
{ URI_PROPERTY_PRIMITIVE_VALUE, QO_SKIPTOKEN }, { URI_PROPERTY_PRIMITIVE_VALUE, QO_LEVELS },
{ URI_PROPERTY_PRIMITIVE_VALUE, QO_TOP },
{ URI_SINGLETON, QO_FILTER }, { URI_SINGLETON, QO_ID }, { URI_SINGLETON, QO_COUNT },
{ URI_SINGLETON, QO_ORDERBY }, /* { URI_SINGLETON, QO_SEARCH }, */{ URI_SINGLETON, QO_SKIP },
{ URI_SINGLETON, QO_SKIPTOKEN }, { URI_SINGLETON, QO_TOP },
{ URI_NAV_ENTITY, QO_FILTER }, { URI_NAV_ENTITY, QO_ID }, { URI_NAV_ENTITY, QO_COUNT },
{ URI_NAV_ENTITY, QO_ORDERBY }, /* { URI_NAV_ENTITY, QO_SEARCH }, */{ URI_NAV_ENTITY, QO_SKIP },
{ URI_NAV_ENTITY, QO_SKIPTOKEN }, { URI_SINGLETON, QO_TOP },
{ URI_NAV_ENTITY_SET, QO_ID },
};
private Parser parser;
private Edm edm;
@Before
public void before() {
parser = new Parser();
edm = new EdmProviderImpl(new EdmTechProvider());
}
@Test
public void validateSelect() throws Exception {
String[] uris = { "/ESAllPrim(1)?$select=PropertyString" };
for (String uri : uris) {
parseAndValidate(uri, "GET");
}
}
@Test(expected = UriValidationException.class)
public void validateForHttpMethodsFail() throws Exception {
String uri = URI_ENTITY;
parseAndValidate(uri, "xyz");
}
@Test
public void validateForHttpMethods() throws Exception {
String uri = URI_ENTITY;
parseAndValidate(uri, "GET");
parseAndValidate(uri, "POST");
parseAndValidate(uri, "PUT");
parseAndValidate(uri, "DELETE");
parseAndValidate(uri, "PATCH");
parseAndValidate(uri, "MERGE");
}
@Test
public void validateOrderBy() throws Exception {
String[] uris = { "/ESAllPrim?$orderby=PropertyString" };
for (String uri : uris) {
parseAndValidate(uri, "GET");
}
}
@Test(expected = UriValidationException.class)
@Ignore("uri parser doen't support orderby yet")
public void validateOrderByInvalid() throws Exception {
String uri = "/ESAllPrim(1)?$orderBy=XXXX";
parseAndValidate(uri, "GET");
}
@Test(expected = UriValidationException.class)
public void validateKeyPredicatesWrongKey() throws Exception {
String uri = "ESTwoKeyNav(xxx=1, yyy='abc')";
parseAndValidate(uri, "GET");
}
@Test
public void validateKeyPredicates() throws Exception {
String uri = "ESTwoKeyNav(PropertyInt16=1, PropertyString='abc')";
parseAndValidate(uri, "GET");
}
@Test(expected = UriValidationException.class)
public void validateKeyPredicatesWrongValueType() throws Exception {
String uri = "ESTwoKeyNav(PropertyInt16='abc', PropertyString=1)";
parseAndValidate(uri, "GET");
}
@Test
public void checkValidSystemQueryOption() throws Exception {
String[] uris = constructUri(urisWithValidSystemQueryOptions);
for (String uri : uris) {
try {
parseAndValidate(uri, "GET");
} catch (Exception e) {
throw new Exception("Faild for uri: " + uri, e);
}
}
}
@Test
public void checkNonValidSystemQueryOption() throws Exception {
String[] uris = constructUri(urisWithNonValidSystemQueryOptions);
for (String uri : uris) {
try {
parseAndValidate(uri, "GET");
fail("Validation Exception not thrown: " + uri);
} catch (UriValidationException e) {
assertTrue(e instanceof UriValidationException);
}
}
}
private String[] constructUri(final String[][] uriParameterMatrix) {
ArrayList<String> uris = new ArrayList<String>();
for (String[] uriParameter : uriParameterMatrix) {
String uri = uriParameter[0];
if (uriParameter.length > 1) {
uri += "?";
}
for (int i = 1; i < uriParameter.length; i++) {
uri += uriParameter[i];
if (i < (uriParameter.length - 1)) {
uri += "&";
}
}
uris.add(uri);
}
return uris.toArray(new String[0]);
}
private void parseAndValidate(final String uri, String method) throws UriParserException, UriValidationException {
UriInfo uriInfo = parser.parseUri(uri.trim(), edm);
UriValidator validator = new UriValidator();
validator.validate(uriInfo, method);
}
}

View File

@ -0,0 +1,20 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
org.slf4j.simpleLogger.defaultLogLevel=debug
org.slf4j.simpleLogger.logFile=System.out