http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/EdmTechTestProvider.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/EdmTechTestProvider.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/EdmTechTestProvider.java deleted file mode 100644 index a5d2e0f..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/EdmTechTestProvider.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.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; - -import java.util.Arrays; -import java.util.List; - -/** - * 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 = "olingo.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); - } - -}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ExpandValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ExpandValidator.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ExpandValidator.java deleted file mode 100644 index d79dfd3..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ExpandValidator.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * 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.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.ODataApplicationException; -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.FilterOption; -import org.apache.olingo.server.api.uri.queryoption.QueryOption; -import org.apache.olingo.server.api.uri.queryoption.SelectItem; -import org.apache.olingo.server.api.uri.queryoption.SelectOption; -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.OrderByOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.QueryOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.SelectOptionImpl; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -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 enough segments"); - } - return this; - - } - - public ExpandValidator isSegmentStar() { - assertTrue(expandItem.isStar()); - return this; - } - - public ExpandValidator isSegmentRef() { - assertTrue(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) { - SelectOption select = expandItem.getSelectOption(); - SelectItem item = select.getSelectItems().get(index); - assertTrue(item.isStar()); - return this; - } - - public ExpandValidator isSelectItemAllOperations(final int index, final FullQualifiedName fqn) { - SelectOption select = expandItem.getSelectOption(); - SelectItem item = select.getSelectItems().get(index); - assertEquals(fqn.toString(), item.getAllOperationsInSchemaNameSpace().toString()); - return this; - } - - public ExpandValidator isFilterOptionText(final String text) { - QueryOption option = expandItem.getFilterOption(); - assertEquals(text, option.getText()); - return this; - } - - public ExpandValidator isFilterSerialized(final String serialized) { - FilterOption filter = expandItem.getFilterOption(); - - try { - String tmp = FilterTreeToText.Serialize(filter); - assertEquals(serialized, tmp); - } catch (ExpressionVisitException e) { - fail("Exception occurred while converting the filterTree into text" + "\n" - + " Exception: " + e.getMessage()); - } catch (ODataApplicationException e) { - fail("Exception occurred 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; - - } - -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/FilterTreeToText.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/FilterTreeToText.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/FilterTreeToText.java deleted file mode 100644 index 9664b7e..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/FilterTreeToText.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * 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.apache.olingo.commons.api.edm.EdmEnumType; -import org.apache.olingo.commons.api.edm.EdmType; -import org.apache.olingo.server.api.ODataApplicationException; -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; - -import java.util.List; - -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 + ">>"; - } - -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/FilterValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/FilterValidator.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/FilterValidator.java deleted file mode 100644 index f530456..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/FilterValidator.java +++ /dev/null @@ -1,529 +0,0 @@ -/* - * 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.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.ODataApplicationException; -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; - -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -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 { - return runUriOrderBy("ESAllPrim", "$orderby=" + orderBy.trim()); - } - - public FilterValidator runOrderByOnETTwoKeyNav(final String orderBy) throws UriParserException { - return runUriOrderBy("ESTwoKeyNav", "$orderby=" + orderBy.trim()); - } - - public FilterValidator runOrderByOnETMixEnumDefCollComp(final String orderBy) throws UriParserException { - return runUriOrderBy("ESMixEnumDefCollComp", "$orderby=" + orderBy.trim()); - } - - public FilterValidator runOrderByOnETTwoKeyNavEx(final String orderBy) throws UriParserException { - return runUriOrderByEx("ESTwoKeyNav", "$orderby=" + orderBy.trim()); - } - - public FilterValidator runOnETTwoKeyNav(final String filter) throws UriParserException { - return runUri("ESTwoKeyNav", "$filter=" + filter.trim()); - } - - public FilterValidator runOnETMixEnumDefCollComp(final String filter) throws UriParserException { - return runUri("ESMixEnumDefCollComp", "$filter=" + filter.trim()); - } - - public FilterValidator runOnETTwoKeyNavSingle(final String filter) throws UriParserException { - return runUri("SINav", "$filter=" + filter.trim()); - } - - public FilterValidator runOnETTwoKeyNavEx(final String filter) throws UriParserException { - return runUriEx("ESTwoKeyNav", "$filter=" + filter.trim()); - } - - public FilterValidator runOnETAllPrim(final String filter) throws UriParserException { - return runUri("ESAllPrim(1)", "$filter=" + filter.trim()); - } - - public FilterValidator runOnETKeyNav(final String filter) throws UriParserException { - return runUri("ESKeyNav(1)", "$filter=" + filter.trim()); - } - - public FilterValidator runOnETKeyNavEx(final String filter) throws UriParserException { - return runUriEx("ESKeyNav(1)", "$filter=" + filter.trim()); - } - - public FilterValidator runOnCTTwoPrim(final String filter) throws UriParserException { - return runUri("SINav/PropertyCompTwoPrim", "$filter=" + filter.trim()); - } - - public FilterValidator runOnString(final String filter) throws UriParserException { - return runUri("SINav/PropertyString", "$filter=" + filter.trim()); - } - - public FilterValidator runOnInt32(final String filter) throws UriParserException { - return runUri("ESCollAllPrim(1)/CollPropertyInt32", "$filter=" + filter.trim()); - } - - public FilterValidator runOnDateTimeOffset(final String filter) throws UriParserException { - return runUri("ESCollAllPrim(1)/CollPropertyDateTimeOffset", "$filter=" + filter.trim()); - } - - public FilterValidator runOnDuration(final String filter) throws UriParserException { - return runUri("ESCollAllPrim(1)/CollPropertyDuration", "$filter=" + filter.trim()); - } - - public FilterValidator runOnTimeOfDay(final String filter) throws UriParserException { - return runUri("ESCollAllPrim(1)/CollPropertyTimeOfDay", "$filter=" + filter.trim()); - } - - public FilterValidator runESabc(final String filter) throws UriParserException { - return runUri("ESabc", "$filter=" + filter.trim()); - } - - public FilterValidator runUri(final String path, final String query) throws UriParserException { - Parser parser = new Parser(); - UriInfo uriInfo = null; - - uriInfo = parser.parseUri(path, query, null, 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 path, final String query) { - Parser parser = new Parser(); - UriInfo uriInfo = null; - - try { - uriInfo = parser.parseUri(path, query, null, 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 path, final String query) throws UriParserException { - Parser parser = new Parser(); - UriInfo uriInfo = null; - - uriInfo = parser.parseUri(path, query, null, 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 path, final String query) { - Parser parser = new Parser(); - UriInfo uriInfo = null; - - try { - uriInfo = parser.parseUri(path, query, null, edm); - fail("Expected exception not thrown."); - } 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(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 UriParserSyntaxException.MessageKeys messageKey) { - assertEquals(UriParserSyntaxException.class, exception.getClass()); - assertEquals(messageKey, exception.getMessageKey()); - return this; - } - - public FilterValidator isExSemantic(final UriParserSemanticException.MessageKeys messageKey) { - assertEquals(UriParserSemanticException.class, exception.getClass()); - assertEquals(messageKey, exception.getMessageKey()); - 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; - } - -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParseTreeToText.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParseTreeToText.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParseTreeToText.java deleted file mode 100644 index 1859ea2..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParseTreeToText.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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.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; - -import java.util.Arrays; -import java.util.List; - -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(); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserValidator.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserValidator.java deleted file mode 100644 index aeb1309..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserValidator.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * 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.ParserRuleContext; -import org.apache.olingo.server.core.uri.antlr.UriParserParser; - -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -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; - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserWithLogging.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserWithLogging.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserWithLogging.java deleted file mode 100644 index 524a38a..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserWithLogging.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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()); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java deleted file mode 100644 index 5459c84..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java +++ /dev/null @@ -1,612 +0,0 @@ -/* - * 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.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.commons.api.http.HttpMethod; -import org.apache.olingo.server.api.ODataApplicationException; -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.parser.UriParserException; -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.UriValidationException; -import org.apache.olingo.server.core.uri.validator.UriValidator; - -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -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 UriInfo uriInfoPath) { - uriInfo = uriInfoPath; - last(); - return this; - } - - // --- Execution --- - - public ResourceValidator run(final String path) { - ParserWithLogging testParser = new ParserWithLogging(); - - UriInfo uriInfoTmp = null; - uriPathInfo = null; - try { - uriInfoTmp = testParser.parseUri(path, null, null, edm); - } catch (final UriParserException e) { - fail("Exception occurred while parsing the URI: " + path + "\n" - + " Message: " + e.getMessage()); - } - - try { - new UriValidator().validate(uriInfoTmp, HttpMethod.GET); - } catch (final UriValidationException e) { - fail("Exception occurred while validating the URI: " + path + "\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).getExpression()) - .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).getReferencedProperty()); - 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; - } - - public ResourceValidator isInAliasToValueMap(String alias, String value) { - String valueForAlias = uriInfo.getValueForAlias(alias); - assertEquals(value, valueForAlias); - return this; - } - -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestErrorLogger.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestErrorLogger.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestErrorLogger.java deleted file mode 100644 index fda6683..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestErrorLogger.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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.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; - -import java.util.BitSet; -import java.util.Collections; -import java.util.List; - -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); - } - - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestUriValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestUriValidator.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestUriValidator.java deleted file mode 100644 index 2339cc2..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestUriValidator.java +++ /dev/null @@ -1,267 +0,0 @@ -/* - * 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.commons.api.http.HttpMethod; -import org.apache.olingo.server.api.ODataTranslatedException; -import org.apache.olingo.server.api.uri.UriInfo; -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.UriValidationException; -import org.apache.olingo.server.core.uri.validator.UriValidator; - -public class TestUriValidator implements TestValidator { - private Edm edm; - - private UriInfo uriInfo; - private ODataTranslatedException exception; - - // Setup - public TestUriValidator setEdm(final Edm edm) { - this.edm = edm; - return this; - } - - // Execution - public TestUriValidator run(final String path) throws UriParserException, UriValidationException { - return run(path, null); - } - - public TestUriValidator run(final String path, final String query) - throws UriParserException, UriValidationException { - Parser parser = new Parser(); - UriValidator validator = new UriValidator(); - - uriInfo = parser.parseUri(path, query, null, edm); - validator.validate(uriInfo, HttpMethod.GET); - return this; - } - - public TestUriValidator run(final String path, final String query, final String fragment) - throws UriParserException, UriValidationException { - uriInfo = new Parser().parseUri(path, query, fragment, edm); - new UriValidator().validate(uriInfo, HttpMethod.GET); - return this; - } - - public TestUriValidator runEx(final String path) { - return runEx(path, null); - } - - public TestUriValidator runEx(final String path, final String query) { - Parser parser = new Parser(); - uriInfo = null; - try { - uriInfo = parser.parseUri(path, query, null, edm); - new UriValidator().validate(uriInfo, HttpMethod.GET); - fail("Exception expected"); - } catch (UriParserException e) { - exception = e; - } catch (UriValidationException e) { - exception = e; - } - - 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 enough 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 UriParserSyntaxException.MessageKeys messageKey) { - assertEquals(UriParserSyntaxException.class, exception.getClass()); - assertEquals(messageKey, exception.getMessageKey()); - return this; - } - - public TestUriValidator isExSemantic(final UriParserSemanticException.MessageKeys messageKey) { - assertEquals(UriParserSemanticException.class, exception.getClass()); - assertEquals(messageKey, exception.getMessageKey()); - return this; - } - - public TestUriValidator isExValidation(final UriValidationException.MessageKeys messageKey) { - assertEquals(UriValidationException.class, exception.getClass()); - assertEquals(messageKey, exception.getMessageKey()); - 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; - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestValidator.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestValidator.java deleted file mode 100644 index 7e64f86..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestValidator.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 { - -}
