From d19afa013bfe819234f981039b613ca8210a591a Mon Sep 17 00:00:00 2001 From: Christian Amend Date: Wed, 6 Aug 2014 17:09:29 +0200 Subject: [PATCH] [OLINGO-348] Make Uri Parser exception translatable --- .../core/edm/primitivetype/EdmDecimal.java | 3 +- .../edm/primitivetype/EdmTimeOfDayTest.java | 6 + .../server/api/uri/UriInfoResource.java | 6 + .../olingo/server/core/ODataHandler.java | 2 +- .../olingo/server/core/uri/UriInfoImpl.java | 9 ++ .../olingo/server/core/uri/parser/Parser.java | 13 +- .../core/uri/parser/UriParseTreeVisitor.java | 132 +++++++++++------- .../core/uri/parser/UriParserException.java | 20 +-- .../parser/UriParserSemanticException.java | 16 ++- .../uri/parser/UriParserSyntaxException.java | 19 +-- .../uri/validator/UriValidationException.java | 42 +++++- .../core/uri/validator/UriValidator.java | 70 +++++++--- .../olingo/server/core/ODataHandlerTest.java | 73 +++++++++- .../server/core/uri/UriInfoImplTest.java | 30 ++-- .../core/uri/antlr/TestFullResourcePath.java | 24 ++-- .../core/uri/antlr/TestUriParserImpl.java | 8 +- .../core/uri/testutil/ResourceValidator.java | 6 + .../core/uri/testutil/TestUriValidator.java | 8 ++ .../core/uri/validator/UriValidatorTest.java | 6 + 19 files changed, 353 insertions(+), 140 deletions(-) diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDecimal.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDecimal.java index 66b0d70cf..f67caf93a 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDecimal.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDecimal.java @@ -68,7 +68,8 @@ public final class EdmDecimal extends SingletonPrimitiveType { } private static boolean validateLiteral(final String value) { - return PATTERN.matcher(value).matches(); + boolean valid = PATTERN.matcher(value).matches(); + return valid; } private static final boolean validatePrecisionAndScale(final String value, final Integer precision, diff --git a/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/primitivetype/EdmTimeOfDayTest.java b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/primitivetype/EdmTimeOfDayTest.java index 297fd82bd..917814d25 100644 --- a/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/primitivetype/EdmTimeOfDayTest.java +++ b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/primitivetype/EdmTimeOfDayTest.java @@ -90,4 +90,10 @@ public class EdmTimeOfDayTest extends PrimitiveTypeBaseTest { expectTypeErrorInValueOfString(instance, "11:12:13"); } + + @Test + public void tests() throws Exception { + instance.validate("12:34:55", null, null, null, null, null); + } + } diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/uri/UriInfoResource.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/uri/UriInfoResource.java index f663c2f9e..7a62c365d 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/uri/UriInfoResource.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/uri/UriInfoResource.java @@ -115,4 +115,10 @@ public interface UriInfoResource { */ List getUriResourceParts(); + /** + * @param alias + * @return the value for the given alias or null if no value is defined + */ + String getValueForAlias(String alias); + } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java index f364a22a2..3b9f06883 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java @@ -74,7 +74,7 @@ public class ODataHandler { processInternal(request, requestedContentType, response); } catch (UriParserException e) { - + e.printStackTrace(); } catch (ContentNegotiatorException e) { Locale requestedLocale = null; ODataServerError serverError = diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/UriInfoImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/UriInfoImpl.java index ee855eb6f..2b4b266e3 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/UriInfoImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/UriInfoImpl.java @@ -62,6 +62,7 @@ public class UriInfoImpl implements UriInfo { private EdmEntityType entityTypeCast; // for $entity private List customQueryOptions = new ArrayList(); + private HashMap aliasToValue = new HashMap(); HashMap systemQueryOptions = new HashMap(); @@ -134,6 +135,11 @@ public class UriInfoImpl implements UriInfo { return retList; } + @Override + public String getValueForAlias(String alias) { + return aliasToValue.get(alias); + } + @Override public EdmEntityType getEntityTypeCast() { return entityTypeCast; @@ -228,6 +234,9 @@ public class UriInfoImpl implements UriInfo { public void addCustomQueryOption(final QueryOptionImpl item) { customQueryOptions.add((CustomQueryOptionImpl) item); + if (item.getName().startsWith("@")) { + aliasToValue.put(item.getName(), item.getText()); + } } public UriInfoImpl setSystemQueryOption(final SystemQueryOptionImpl systemOption) { diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java index 535359f8c..11368114f 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java @@ -221,7 +221,8 @@ public class Parser { try { inlineCountOption.setValue(Integer.parseInt(option.value)); } catch (final NumberFormatException e) { - throw new UriParserSemanticException("Illegal value of $skip option!", e); + throw new UriParserSemanticException("Illegal value of $skip option!", e, + UriParserSemanticException.MessageKeys.TEST); } context.contextUriInfo.setSystemQueryOption(inlineCountOption); } else if (option.name.equals("$skiptoken")) { @@ -237,7 +238,8 @@ public class Parser { try { inlineCountOption.setValue(Integer.parseInt(option.value)); } catch (final NumberFormatException e) { - throw new UriParserSemanticException("Illegal value of $top option!", e); + throw new UriParserSemanticException("Illegal value of $top option!", e, + UriParserSemanticException.MessageKeys.TEST); } context.contextUriInfo.setSystemQueryOption(inlineCountOption); } else if (option.name.equals("$count")) { @@ -248,7 +250,8 @@ public class Parser { if (option.value.equals("true") || option.value.equals("false")) { inlineCountOption.setValue(Boolean.parseBoolean(option.value)); } else { - throw new UriParserSemanticException("Illegal value of $count option!"); + throw new UriParserSemanticException("Illegal value of $count option!", + UriParserSemanticException.MessageKeys.TEST); } context.contextUriInfo.setSystemQueryOption(inlineCountOption); } @@ -396,12 +399,12 @@ public class Parser { } } catch (Exception weakException) { - throw new UriParserSyntaxException("Error in syntax", weakException); + throw new UriParserSyntaxException("Error in syntax", weakException, UriParserSyntaxException.MessageKeys.TEST); // exceptionOnStage = 2; } } catch (Exception hardException) { - throw new UriParserSyntaxException("Error in syntax", hardException); + throw new UriParserSyntaxException("Error in syntax", hardException, UriParserSyntaxException.MessageKeys.TEST); } return ret; diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java index ea242689b..13900d6c3 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java @@ -343,7 +343,7 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { tmp += (tmp.length() != 0 ? "," : "") + name; } throw wrap(new UriParserSemanticException("Function of functionimport '" + edmFunctionImport.getName() - + "' with parameters [" + tmp + "] not found")); + + "' with parameters [" + tmp + "] not found", UriParserSemanticException.MessageKeys.TEST)); } uriResource.setFunction(edmFunctionImport.getUnboundFunction(names)); @@ -358,7 +358,7 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (lastResourcePart == null) { if (context.contextTypes.size() == 0) { throw wrap(new UriParserSemanticException("Resource part '" + odi + "' can only applied on typed " - + "resource parts")); + + "resource parts", UriParserSemanticException.MessageKeys.TEST)); } source = context.contextTypes.peek(); } else { @@ -366,7 +366,7 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (source.type == null) { throw wrap(new UriParserSemanticException("Resource part '" + odi + "' can only applied on typed " - + "resource parts")); + + "resource parts", UriParserSemanticException.MessageKeys.TEST)); } } @@ -385,7 +385,7 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (!(source.type instanceof EdmStructuredType)) { throw wrap(new UriParserSemanticException("Can not parse'" + odi - + "'Previous path segment not a structural type.")); + + "'Previous path segment not a structural type.", UriParserSemanticException.MessageKeys.TEST)); } EdmStructuredType structType = (EdmStructuredType) source.type; @@ -393,7 +393,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { EdmElement property = structType.getProperty(odi); if (property == null) { throw wrap(new UriParserSemanticException("Property '" + odi + "' not found in type '" - + structType.getNamespace() + "." + structType.getName() + "'")); + + structType.getNamespace() + "." + structType.getName() + "'", + UriParserSemanticException.MessageKeys.TEST)); } if (property instanceof EdmProperty) { @@ -417,7 +418,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { context.contextUriInfo.addResourcePart(navigationResource); return null; } else { - throw wrap(new UriParserSemanticException("Unkown type for property '" + property + "'")); + throw wrap(new UriParserSemanticException("Unkown type for property '" + property + "'", + UriParserSemanticException.MessageKeys.TEST)); } } else { // with namespace @@ -432,7 +434,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { // is entity type cast if (!(filterEntityType.compatibleTo(source.type))) { throw wrap(new UriParserSemanticException( - "Entity typefilter not compatible to previous path segment: " + fullFilterName.toString())); + "Entity typefilter not compatible to previous path segment: " + fullFilterName.toString(), + UriParserSemanticException.MessageKeys.TEST)); } if (lastResourcePart == null) { @@ -457,7 +460,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (lastPartWithKeys.getTypeFilterOnEntry() != null) { throw wrap(new UriParserSemanticException("Entry typefilters are not chainable, used '" + getName(filterEntityType) + "' behind '" - + getName(lastPartWithKeys.getTypeFilterOnEntry()) + "'")); + + getName(lastPartWithKeys.getTypeFilterOnEntry()) + "'", + UriParserSemanticException.MessageKeys.TEST)); } lastPartWithKeys.setEntryTypeFilter(filterEntityType); return null; @@ -465,7 +469,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (lastPartWithKeys.getTypeFilterOnCollection() != null) { throw wrap(new UriParserSemanticException("Collection typefilters are not chainable, used '" + getName(filterEntityType) + "' behind '" - + getName(lastPartWithKeys.getTypeFilterOnCollection()) + "'")); + + getName(lastPartWithKeys.getTypeFilterOnCollection()) + "'", + UriParserSemanticException.MessageKeys.TEST)); } lastPartWithKeys.setCollectionTypeFilter(filterEntityType); return null; @@ -475,14 +480,14 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (lastPartTyped.getTypeFilter() != null) { throw wrap(new UriParserSemanticException("Typefilters are not chainable, used '" + getName(filterEntityType) + "' behind '" - + getName(lastPartTyped.getTypeFilter()) + "'")); + + getName(lastPartTyped.getTypeFilter()) + "'", UriParserSemanticException.MessageKeys.TEST)); } lastPartTyped.setTypeFilter(filterEntityType); return null; } else { throw wrap(new UriParserSemanticException("Path segment before '" + getName(filterEntityType) - + "' not typed")); + + "' not typed", UriParserSemanticException.MessageKeys.TEST)); } } } @@ -497,7 +502,7 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (!(filterComplexType.compatibleTo(source.type))) { throw wrap(new UriParserSemanticException( "Complex typefilter '" + getName(source.type) + "'not compatible type of previous path segment '" - + getName(filterComplexType) + "'")); + + getName(filterComplexType) + "'", UriParserSemanticException.MessageKeys.TEST)); } // is simple complex type cast @@ -523,7 +528,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (lastPartWithKeys.getTypeFilterOnEntry() != null) { throw wrap(new UriParserSemanticException("Entry typefilters are not chainable, used '" + getName(filterComplexType) + "' behind '" - + getName(lastPartWithKeys.getTypeFilterOnEntry()) + "'")); + + getName(lastPartWithKeys.getTypeFilterOnEntry()) + "'", + UriParserSemanticException.MessageKeys.TEST)); } lastPartWithKeys.setEntryTypeFilter(filterComplexType); return null; @@ -531,7 +537,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (lastPartWithKeys.getTypeFilterOnCollection() != null) { throw wrap(new UriParserSemanticException("Collection typefilters are not chainable, used '" + getName(filterComplexType) + "' behind '" - + getName(lastPartWithKeys.getTypeFilterOnCollection()) + "'")); + + getName(lastPartWithKeys.getTypeFilterOnCollection()) + "'", + UriParserSemanticException.MessageKeys.TEST)); } lastPartWithKeys.setCollectionTypeFilter(filterComplexType); return null; @@ -542,14 +549,14 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (lastPartTyped.getTypeFilter() != null) { throw wrap(new UriParserSemanticException("Typefilters are not chainable, used '" + getName(filterComplexType) + "' behind '" - + getName(lastPartTyped.getTypeFilter()) + "'")); + + getName(lastPartTyped.getTypeFilter()) + "'", UriParserSemanticException.MessageKeys.TEST)); } lastPartTyped.setTypeFilter(filterComplexType); return null; } else { throw wrap(new UriParserSemanticException("Path segment before '" + getName(filterComplexType) - + "' not typed")); + + "' not typed", UriParserSemanticException.MessageKeys.TEST)); } } } @@ -569,7 +576,7 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { // do a check for bound functions (which requires a parameter list) if (ctx.vlNVO.size() == 0) { throw wrap(new UriParserSemanticException("Expected function parameters for '" + fullBindingTypeName.toString() - + "'")); + + "'", UriParserSemanticException.MessageKeys.TEST)); } context.contextReadingFunctionParameters = true; @@ -610,7 +617,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { return null; } - throw wrap(new UriParserSemanticException("Unknown resource path segment:" + fullFilterName.toString())); + throw wrap(new UriParserSemanticException("Unknown resource path segment:" + fullFilterName.toString(), + UriParserSemanticException.MessageKeys.TEST)); } } @@ -631,7 +639,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { UriResource obj = context.contextUriInfo.getLastResourcePart(); if (!(obj instanceof UriResourcePartTyped)) { - throw wrap(new UriParserSemanticException("any only allowed on typed path segments")); + throw wrap(new UriParserSemanticException("any only allowed on typed path segments", + UriParserSemanticException.MessageKeys.TEST)); } UriContext.LambdaVariables var = new UriContext.LambdaVariables(); @@ -770,7 +779,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { EdmEntityType type = edm.getEntityType(fullName); if (type == null) { - throw wrap(new UriParserSemanticException("Expected EntityTypeName")); + throw wrap(new UriParserSemanticException("Expected EntityTypeName", + UriParserSemanticException.MessageKeys.TEST)); } context.contextUriInfo.setEntityTypeCast(type); @@ -855,7 +865,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (ctx.vLV != null) { UriResourceImpl lastResourcePart = (UriResourceImpl) context.contextUriInfo.getLastResourcePart(); if (!(lastResourcePart instanceof UriResourcePartTyped)) { - throw wrap(new UriParserSemanticException("any only allowed on typed path segments")); + throw wrap(new UriParserSemanticException("any only allowed on typed path segments", + UriParserSemanticException.MessageKeys.TEST)); } UriContext.LambdaVariables var = new UriContext.LambdaVariables(); @@ -957,11 +968,13 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (!((UriResourcePartTyped) pathInfo).isCollection()) { context.contextUriInfo.addResourcePart(new UriResourceValueImpl()); } else { - throw wrap(new UriParserSemanticException("$value only allowed on typed path segments")); + throw wrap(new UriParserSemanticException("$value only allowed on typed path segments", + UriParserSemanticException.MessageKeys.TEST)); } return null; } else { - throw wrap(new UriParserSemanticException("$value only allowed on typed path segments")); + throw wrap(new UriParserSemanticException("$value only allowed on typed path segments", + UriParserSemanticException.MessageKeys.TEST)); } } else if (ctx.vC != null) { @@ -969,10 +982,12 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (((UriResourcePartTyped) pathInfo).isCollection()) { context.contextUriInfo.addResourcePart(new UriResourceCountImpl()); } else { - throw wrap(new UriParserSemanticException("$count only allowed on collection properties")); + throw wrap(new UriParserSemanticException("$count only allowed on collection properties", + UriParserSemanticException.MessageKeys.TEST)); } } else { - throw wrap(new UriParserSemanticException("$count only allowed on typed properties")); + throw wrap(new UriParserSemanticException("$count only allowed on typed properties", + UriParserSemanticException.MessageKeys.TEST)); } } else if (ctx.vR != null) { if (pathInfo instanceof UriResourcePartTyped) { @@ -980,10 +995,12 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { if (type instanceof EdmEntityType) { context.contextUriInfo.addResourcePart(new UriResourceRefImpl()); } else { - throw wrap(new UriParserSemanticException("$ref only allowed on endity types")); + throw wrap(new UriParserSemanticException("$ref only allowed on endity types", + UriParserSemanticException.MessageKeys.TEST)); } } else { - throw wrap(new UriParserSemanticException("$ref only allowed on typed properties")); + throw wrap(new UriParserSemanticException("$ref only allowed on typed properties", + UriParserSemanticException.MessageKeys.TEST)); } } else if (ctx.vAll != null) { @@ -1393,13 +1410,15 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { try { expression = (ExpressionImpl) ctx.vVO.accept(this); } catch (Exception ex) { - throw wrap(new UriParserSemanticException("Invalid key value: " + valueText)); + throw wrap(new UriParserSemanticException("Invalid key value: " + valueText, + UriParserSemanticException.MessageKeys.TEST)); } // get type of last resource part UriResource last = context.contextUriInfo.getLastResourcePart(); if (!(last instanceof UriResourcePartTyped)) { - throw wrap(new UriParserSemanticException("Paramterslist on untyped resource path segement not allowed")); + throw wrap(new UriParserSemanticException("Paramterslist on untyped resource path segement not allowed", + UriParserSemanticException.MessageKeys.TEST)); } EdmEntityType lastType = (EdmEntityType) ((UriResourcePartTyped) last).getType(); @@ -1420,14 +1439,16 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { // key. // for using referential constrains the last resource part must be a navigation property if (!(context.contextUriInfo.getLastResourcePart() instanceof UriResourceNavigationPropertyImpl)) { - throw wrap(new UriParserSemanticException("Not enougth keyproperties defined")); + throw wrap(new UriParserSemanticException("Not enougth keyproperties defined", + UriParserSemanticException.MessageKeys.TEST)); } UriResourceNavigationPropertyImpl lastNav = (UriResourceNavigationPropertyImpl) last; // get the partner of the navigation property EdmNavigationProperty partner = lastNav.getProperty().getPartner(); if (partner == null) { - throw wrap(new UriParserSemanticException("Not enougth keyproperties defined")); + throw wrap(new UriParserSemanticException("Not enougth keyproperties defined", + UriParserSemanticException.MessageKeys.TEST)); } // create the keylist @@ -1445,7 +1466,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { missedKey = item; } else { // two of more keys are missing - throw wrap(new UriParserSemanticException("Not enougth referntial contrains defined")); + throw wrap(new UriParserSemanticException("Not enougth referntial contrains defined", + UriParserSemanticException.MessageKeys.TEST)); } } } @@ -1479,7 +1501,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { // get type of last resource part if (!(last instanceof UriResourcePartTyped)) { - throw wrap(new UriParserSemanticException("Parameterslist on untyped resource path segement not allowed")); + throw wrap(new UriParserSemanticException("Parameterslist on untyped resource path segement not allowed", + UriParserSemanticException.MessageKeys.TEST)); } EdmEntityType lastType = (EdmEntityType) ((UriResourcePartTyped) last).getType(); @@ -1494,14 +1517,16 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { // if not, check if the missing key predicates can be satisfied with help of the defined referential constrains // for using referential constrains the last resource part must be a navigation property if (!(context.contextUriInfo.getLastResourcePart() instanceof UriResourceNavigationPropertyImpl)) { - throw wrap(new UriParserSemanticException("Not enougth keyproperties defined")); + throw wrap(new UriParserSemanticException("Not enougth keyproperties defined", + UriParserSemanticException.MessageKeys.TEST)); } UriResourceNavigationPropertyImpl lastNav = (UriResourceNavigationPropertyImpl) last; // get the partner of the navigation property EdmNavigationProperty partner = lastNav.getProperty().getPartner(); if (partner == null) { - throw wrap(new UriParserSemanticException("Not enougth keyproperties defined")); + throw wrap(new UriParserSemanticException("Not enougth keyproperties defined", + UriParserSemanticException.MessageKeys.TEST)); } // fill missing keys from referential constrains @@ -1528,7 +1553,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { return list; } - throw wrap(new UriParserSemanticException("Not enougth keyproperties defined")); + throw wrap(new UriParserSemanticException("Not enougth keyproperties defined", + UriParserSemanticException.MessageKeys.TEST)); } return new ArrayList(); } @@ -1542,7 +1568,7 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { uriParameter.setText(ctx.vCOM.getText()); uriParameter.setExpression((ExpressionImpl) ctx.vCOM.accept(this)); } else { - uriParameter.setAlias(ctx.vALI.getText()); + uriParameter.setAlias("@" + ctx.vALI.getText()); } return uriParameter; @@ -1631,7 +1657,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { ((UriResourceWithKeysImpl) pathInfoSegment) .setKeyPredicates(list); } else { - throw wrap(new UriParserSemanticException("Key properties not allowed")); + throw wrap(new UriParserSemanticException("Key properties not allowed", + UriParserSemanticException.MessageKeys.TEST)); // throw UriSemanticError.addKrepredicatesNotAllowed(); } } @@ -1698,7 +1725,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { UriResource lastResource = context.contextUriInfo.getLastResourcePart(); if (!(lastResource instanceof UriResourcePartTyped)) { - throw wrap(new UriParserSemanticException("Resource path not typed")); + throw wrap(new UriParserSemanticException("Resource path not typed", + UriParserSemanticException.MessageKeys.TEST)); } UriResourcePartTyped lastType = (UriResourcePartTyped) lastResource; @@ -1803,18 +1831,21 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { prevType = getTypeInformation(last).type; if (prevType == null) { - throw wrap(new UriParserSemanticException("prev segement not typed")); + throw wrap(new UriParserSemanticException("prev segement not typed", + UriParserSemanticException.MessageKeys.TEST)); } } if (!(prevType instanceof EdmStructuredType)) { - throw wrap(new UriParserSemanticException("Previous select item is not a structural type")); + throw wrap(new UriParserSemanticException("Previous select item is not a structural type", + UriParserSemanticException.MessageKeys.TEST)); } EdmStructuredType structType = (EdmStructuredType) prevType; EdmElement element = structType.getProperty(odi); if (element == null) { - throw wrap(new UriParserSemanticException("Previous select item has not property: " + odi)); + throw wrap(new UriParserSemanticException("Previous select item has not property: " + odi, + UriParserSemanticException.MessageKeys.TEST)); } // create new segment @@ -1864,7 +1895,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { return this; } } else { - throw wrap(new UriParserSemanticException("Only Simple and Complex properties within select allowed")); + throw wrap(new UriParserSemanticException("Only Simple and Complex properties within select allowed", + UriParserSemanticException.MessageKeys.TEST)); } } else { String namespace = ctx.vNS.getText(); @@ -1916,14 +1948,16 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { } } } else { - throw wrap(new UriParserSemanticException("prev segement must be comlex of entity type")); + throw wrap(new UriParserSemanticException("prev segement must be comlex of entity type", + UriParserSemanticException.MessageKeys.TEST)); } } else { UriInfoImpl uriInfo = (UriInfoImpl) context.contextSelectItem.getResourcePath(); UriResource last = uriInfo.getLastResourcePart(); if (!(last instanceof UriResourceTypedImpl)) { - throw wrap(new UriParserSemanticException("prev segement typed")); + throw wrap(new UriParserSemanticException("prev segement typed", + UriParserSemanticException.MessageKeys.TEST)); } EdmType prevType = getTypeInformation(last).type; @@ -1939,7 +1973,7 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { } } } else if (prevType instanceof EdmEntityType) { - throw wrap(new UriParserSemanticException("Error")); + throw wrap(new UriParserSemanticException("Error", UriParserSemanticException.MessageKeys.TEST)); /* * EdmEntityType et = edm.getEntityType(fullName); * if (et != null) { @@ -1953,7 +1987,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { * } */ } else { - throw wrap(new UriParserSemanticException("prev segement must be comlex of entity type")); + throw wrap(new UriParserSemanticException("prev segement must be comlex of entity type", + UriParserSemanticException.MessageKeys.TEST)); } } @@ -1964,7 +1999,8 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor { UriInfoImpl uriInfo = (UriInfoImpl) context.contextSelectItem.getResourcePath(); UriResource last = uriInfo.getLastResourcePart(); if (!(last instanceof UriResourceTypedImpl)) { - throw wrap(new UriParserSemanticException("prev segement typed")); + throw wrap(new UriParserSemanticException("prev segement typed", + UriParserSemanticException.MessageKeys.TEST)); } prevType = getTypeInformation(last).type; } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserException.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserException.java index adb5d01de..faa3c6346 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserException.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserException.java @@ -18,18 +18,18 @@ */ package org.apache.olingo.server.core.uri.parser; -public class UriParserException extends Exception { - /** - * - */ - private static final long serialVersionUID = 1L; +import org.apache.olingo.server.api.ODataTranslatedException; - public UriParserException(final String message, final Throwable cause) { - super(message, cause); +public class UriParserException extends ODataTranslatedException { + + private static final long serialVersionUID = -6438700016830955949L; + + public UriParserException(String developmentMessage, MessageKey messageKey, String... parameters) { + super(developmentMessage, messageKey, parameters); } - public UriParserException(final String message) { - super(message, null); + public UriParserException(String developmentMessage, Throwable cause, MessageKey messageKey, + String... parameters) { + super(developmentMessage, cause, messageKey, parameters); } - } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSemanticException.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSemanticException.java index 1dd952b6f..41d178f2a 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSemanticException.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSemanticException.java @@ -20,14 +20,18 @@ package org.apache.olingo.server.core.uri.parser; public class UriParserSemanticException extends UriParserException { - private static final long serialVersionUID = 1L; - - public UriParserSemanticException(final String message, final Throwable cause) { - super(message, cause); + private static final long serialVersionUID = 3850285860949809622L; + + public static enum MessageKeys implements MessageKey { + TEST } - public UriParserSemanticException(final String message) { - super(message, null); + public UriParserSemanticException(String developmentMessage, MessageKey messageKey, String... parameters) { + super(developmentMessage, messageKey, parameters); } + public UriParserSemanticException(String developmentMessage, Throwable cause, MessageKey messageKey, + String... parameters) { + super(developmentMessage, cause, messageKey, parameters); + } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSyntaxException.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSyntaxException.java index da7f62cc8..a7dfdd99a 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSyntaxException.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSyntaxException.java @@ -20,17 +20,18 @@ package org.apache.olingo.server.core.uri.parser; public class UriParserSyntaxException extends UriParserException { - /** - * - */ - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 5887744747812478226L; - public UriParserSyntaxException(final String message, final Throwable cause) { - super(message, cause); + public static enum MessageKeys implements MessageKey { + TEST + } + + public UriParserSyntaxException(String developmentMessage, MessageKey messageKey, String... parameters) { + super(developmentMessage, messageKey, parameters); } - public UriParserSyntaxException(final String message) { - super(message, null); + public UriParserSyntaxException(String developmentMessage, Throwable cause, MessageKey messageKey, + String... parameters) { + super(developmentMessage, cause, messageKey, parameters); } - } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidationException.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidationException.java index 25849329d..f17548382 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidationException.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidationException.java @@ -18,17 +18,47 @@ */ package org.apache.olingo.server.core.uri.validator; -import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; +import org.apache.olingo.server.api.ODataTranslatedException; -public class UriValidationException extends Exception { +public class UriValidationException extends ODataTranslatedException { private static final long serialVersionUID = -3179078078053564742L; - public UriValidationException(final String msg) { - super(msg); + public static enum MessageKeys implements MessageKey { + /** parameter: unsupported query option */ + UNSUPPORTED_QUERY_OPTION, + /** parameter: unsupported uri kind */ + UNSUPPORTED_URI_KIND, + /** parameter: unsupported uri resource kind */ + UNSUPPORTED_URI_RESOURCE_KIND, + /** parameter: unsupported function return type */ + UNSUPPORTED_FUNCTION_RETURN_TYPE, + /** parameter: unsupported action return type */ + UNSUPPORTED_ACTION_RETURN_TYPE, + /** parameter: unsupported http method */ + UNSUPPORTED_HTTP_METHOD, + /** parameter: system query option */ + SYSTEM_QUERY_OPTION_NOT_ALLOWED, + /** parameters: system query option, http method */ + SYSTEM_QUERY_OPTION_NOT_ALLOWED_FOR_HTTP_METHOD, + /** parameter: invalid key property */ + INVALID_KEY_PROPERTY, + /** parameter: untyped segment name */ + LAST_SEGMENT_NOT_TYPED, + /** parameter: untyped segment name */ + SECOND_LAST_SEGMENT_NOT_TYPED, + /** parameter: unallowed kind before $value */ + UNALLOWED_KIND_BEFORE_VALUE, + /** parameter: unallowed kind before $count */ + UNALLOWED_KIND_BEFORE_COUNT, + } + + public UriValidationException(String developmentMessage, MessageKey messageKey, String... parameters) { + super(developmentMessage, messageKey, parameters); } - public UriValidationException(final EdmPrimitiveTypeException e) { - super(e); + public UriValidationException(String developmentMessage, Throwable cause, MessageKey messageKey, + String... parameters) { + super(developmentMessage, cause, messageKey, parameters); } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java index f547c4875..0cbabbc5d 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java @@ -41,6 +41,7 @@ import org.apache.olingo.server.api.uri.UriResourceKind; import org.apache.olingo.server.api.uri.UriResourceNavigation; import org.apache.olingo.server.api.uri.UriResourcePartTyped; import org.apache.olingo.server.api.uri.UriResourceSingleton; +import org.apache.olingo.server.api.uri.queryoption.CustomQueryOption; import org.apache.olingo.server.api.uri.queryoption.SystemQueryOption; import org.apache.olingo.server.api.uri.queryoption.SystemQueryOptionKind; @@ -219,7 +220,8 @@ public class UriValidator { idx = ColumnIndex.top; break; default: - throw new UriValidationException("Unsupported option: " + queryOptionKind); + throw new UriValidationException("Unsupported option: " + queryOptionKind.toString(), + UriValidationException.MessageKeys.UNSUPPORTED_QUERY_OPTION, queryOptionKind.toString()); } return idx; @@ -251,7 +253,8 @@ public class UriValidator { idx = RowIndexForUriType.service; break; default: - throw new UriValidationException("Unsupported uriInfo kind: " + uriInfo.getKind()); + throw new UriValidationException("Unsupported uriInfo kind: " + uriInfo.getKind(), + UriValidationException.MessageKeys.UNSUPPORTED_URI_KIND, uriInfo.getKind().toString()); } return idx; @@ -300,7 +303,8 @@ public class UriValidator { idx = rowIndexForValue(uriInfo); break; default: - throw new UriValidationException("Unsupported uriResource kind: " + lastPathSegment.getKind()); + throw new UriValidationException("Unsupported uriResource kind: " + lastPathSegment.getKind(), + UriValidationException.MessageKeys.UNSUPPORTED_URI_RESOURCE_KIND, lastPathSegment.getKind().toString()); } return idx; @@ -367,7 +371,8 @@ public class UriValidator { break; default: throw new UriValidationException("Unexpected kind in path segment before $value: " - + secondLastPathSegment.getKind()); + + secondLastPathSegment.getKind(), UriValidationException.MessageKeys.UNALLOWED_KIND_BEFORE_VALUE, + secondLastPathSegment.toString()); } return idx; } @@ -388,7 +393,8 @@ public class UriValidator { idx = isCollection ? RowIndexForUriType.propertyPrimitiveCollection : RowIndexForUriType.propertyPrimitive; break; default: - throw new UriValidationException("Unsupported function return type: " + functionReturnTypeKind); + throw new UriValidationException("Unsupported function return type: " + functionReturnTypeKind, + UriValidationException.MessageKeys.UNSUPPORTED_FUNCTION_RETURN_TYPE, functionReturnTypeKind.toString()); } return idx; } @@ -405,7 +411,8 @@ public class UriValidator { : RowIndexForUriType.reference; } else { throw new UriValidationException("secondLastPathSegment not a class of UriResourcePartTyped: " - + lastPathSegment.getClass()); + + lastPathSegment.getClass(), UriValidationException.MessageKeys.LAST_SEGMENT_NOT_TYPED, lastPathSegment + .toString()); } return idx; @@ -420,7 +427,8 @@ public class UriValidator { : RowIndexForUriType.propertyPrimitive; } else { throw new UriValidationException("lastPathSegment not a class of UriResourcePartTyped: " - + lastPathSegment.getClass()); + + lastPathSegment.getClass(), UriValidationException.MessageKeys.LAST_SEGMENT_NOT_TYPED, lastPathSegment + .toString()); } return idx; } @@ -444,7 +452,9 @@ public class UriValidator { idx = rt.isCollection() ? RowIndexForUriType.propertyComplexCollection : RowIndexForUriType.propertyComplex; break; default: - throw new UriValidationException("Unsupported function return type: " + rt.getType().getKind()); + throw new UriValidationException("Unsupported function return type: " + rt.getType().getKind(), + UriValidationException.MessageKeys.UNSUPPORTED_FUNCTION_RETURN_TYPE, + rt.getType().getKind().toString()); } return idx; @@ -458,7 +468,8 @@ public class UriValidator { : RowIndexForUriType.entity; } else { throw new UriValidationException("lastPathSegment not a class of UriResourcePartTyped: " - + lastPathSegment.getClass()); + + lastPathSegment.getClass(), UriValidationException.MessageKeys.LAST_SEGMENT_NOT_TYPED, lastPathSegment + .toString()); } return idx; } @@ -472,7 +483,8 @@ public class UriValidator { : RowIndexForUriType.propertyComplex; } else { throw new UriValidationException("lastPathSegment not a class of UriResourcePartTyped: " - + lastPathSegment.getClass()); + + lastPathSegment.getClass(), UriValidationException.MessageKeys.LAST_SEGMENT_NOT_TYPED, lastPathSegment + .toString()); } return idx; } @@ -496,7 +508,8 @@ public class UriValidator { idx = rt.isCollection() ? RowIndexForUriType.propertyComplexCollection : RowIndexForUriType.propertyComplex; break; default: - throw new UriValidationException("Unsupported action return type: " + rt.getType().getKind()); + throw new UriValidationException("Unsupported action return type: " + rt.getType().getKind(), + UriValidationException.MessageKeys.UNSUPPORTED_ACTION_RETURN_TYPE, rt.getType().getKind().toString()); } return idx; @@ -535,7 +548,8 @@ public class UriValidator { idx = determineReturnType(functionReturnTypeKind, isCollection); break; default: - throw new UriValidationException("Illegal path part kind: " + secondLastPathSegment.getKind()); + throw new UriValidationException("Illegal path part kind before $count: " + secondLastPathSegment.getKind(), + UriValidationException.MessageKeys.UNALLOWED_KIND_BEFORE_COUNT, secondLastPathSegment.toString()); } return idx; @@ -548,7 +562,8 @@ public class UriValidator { ColumnIndex col = colIndex(option.getKind()); if (!decisionMatrix[row.getIndex()][col.getIndex()]) { - throw new UriValidationException("System query option not allowed: " + option.getName()); + throw new UriValidationException("System query option not allowed: " + option.getName(), + UriValidationException.MessageKeys.SYSTEM_QUERY_OPTION_NOT_ALLOWED, option.getName()); } } @@ -561,7 +576,8 @@ public class UriValidator { ColumnIndex col = colIndex(option.getKind()); if (!decisionMatrixForHttpMethod[row.getIndex()][col.getIndex()]) { throw new UriValidationException("System query option " + option.getName() + " not allowed for method " - + httpMethod); + + httpMethod, UriValidationException.MessageKeys.SYSTEM_QUERY_OPTION_NOT_ALLOWED_FOR_HTTP_METHOD, option + .getName(), httpMethod.toString()); } } @@ -590,7 +606,8 @@ public class UriValidator { idx = RowIndexForHttpMethod.MERGE; break; default: - throw new UriValidationException("HTTP method not supported: " + httpMethod); + throw new UriValidationException("HTTP method not supported: " + httpMethod, + UriValidationException.MessageKeys.UNSUPPORTED_HTTP_METHOD, httpMethod.toString()); } return idx; @@ -619,26 +636,39 @@ public class UriValidator { for (UriParameter keyPredicate : keyPredicates) { String name = keyPredicate.getName(); + String alias = keyPredicate.getAlias(); String value = keyPredicate.getText(); + if (alias != null) { + value = uriInfo.getValueForAlias(alias); + } EdmKeyPropertyRef edmKey = edmKeys.get(name); if (edmKey == null) { - throw new UriValidationException("Unknown key property: " + name); + throw new UriValidationException("Unknown key property: " + name, + UriValidationException.MessageKeys.INVALID_KEY_PROPERTY, name); } EdmType edmType = edmKey.getProperty().getType(); EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType; String edmLiteral = edmPrimitiveType.fromUriLiteral(value); - edmPrimitiveType.validate(edmLiteral, edmKey.getProperty().isNullable(), edmKey.getProperty() - .getMaxLength(), edmKey.getProperty().getPrecision(), edmKey.getProperty().getScale(), edmKey - .getProperty().isUnicode()); + boolean isValid = + edmPrimitiveType.validate(edmLiteral, edmKey.getProperty().isNullable(), edmKey.getProperty() + .getMaxLength(), edmKey.getProperty().getPrecision(), edmKey.getProperty().getScale(), edmKey + .getProperty().isUnicode()); + if (!isValid) { + // TODO: Check exception here + throw new UriValidationException("PrimitiveTypeException", + UriValidationException.MessageKeys.INVALID_KEY_PROPERTY); + } } } } } } catch (EdmPrimitiveTypeException e) { - throw new UriValidationException(e); + // TODO: Check exception here + throw new UriValidationException("PrimitiveTypeException", e, + UriValidationException.MessageKeys.INVALID_KEY_PROPERTY); } } } diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java index 5eaff892d..7e55ab88b 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java @@ -29,7 +29,9 @@ import java.util.Arrays; import java.util.Locale; import org.apache.commons.io.IOUtils; +import org.apache.olingo.commons.api.ODataException; import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.commons.api.http.HttpContentType; @@ -40,11 +42,14 @@ import org.apache.olingo.server.api.OData; import org.apache.olingo.server.api.ODataApplicationException; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.edm.provider.EdmProvider; +import org.apache.olingo.server.api.edm.provider.EntitySet; import org.apache.olingo.server.api.processor.MetadataProcessor; import org.apache.olingo.server.api.processor.ServiceDocumentProcessor; import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.tecsvc.provider.EdmTechProvider; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; public class ODataHandlerTest { @@ -219,6 +224,18 @@ public class ODataHandlerTest { assertEquals(406, response.getStatusCode()); } + @Test + public void testUnregisteredProcessor() { + ODataRequest request = new ODataRequest(); + + request.setMethod(HttpMethod.GET); + request.setRawODataPath("ESAllPrim"); + + ODataResponse response = handler.process(request); + assertNotNull(response); + assertEquals(501, response.getStatusCode()); + } + @Test public void testWithApplicationExceptionInProcessor() throws Exception { ODataRequest request = new ODataRequest(); @@ -227,13 +244,65 @@ public class ODataHandlerTest { request.setRawODataPath("$metadata"); MetadataProcessor metadataProcessor = mock(MetadataProcessor.class); - doThrow(new ODataApplicationException("msg", 412, Locale.ENGLISH)).when(metadataProcessor).readMetadata( + doThrow(new ODataApplicationException("msg", 425, Locale.ENGLISH)).when(metadataProcessor).readMetadata( any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class)); handler.register(metadataProcessor); ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(412, response.getStatusCode()); + assertEquals(425, response.getStatusCode()); + } + + //TODO: Use this test + @Ignore + @Test + public void testUriParserExceptionResultsInRightResponseNotFound() throws Exception { + ODataRequest request = new ODataRequest(); + + request.setMethod(HttpMethod.GET); + request.setRawODataPath("NotFound"); + + ODataResponse response = handler.process(request); + assertNotNull(response); + assertEquals(404, response.getStatusCode()); + } + + //TODO: Use this test + @Ignore + @Test + public void testUriParserExceptionResultsInRightResponseBadRequest() throws Exception { + ODataRequest request = new ODataRequest(); + + request.setMethod(HttpMethod.GET); + request.setRawODataPath("ESAllPrim()"); + + ODataResponse response = handler.process(request); + assertNotNull(response); + assertEquals(404, response.getStatusCode()); + } + + @Test + public void testUriParserExceptionResultsInRightResponseEdmCause() throws Exception { + ODataRequest request = new ODataRequest(); + + request.setMethod(HttpMethod.GET); + request.setRawODataPath("EdmException"); + + OData odata = OData.newInstance(); + Edm edm = odata.createEdm(new EdmProvider() { + public EntitySet getEntitySet(final FullQualifiedName entityContainer, final String entitySetName) + throws ODataException { + throw new ODataException("msg"); + } + }); + + ODataHandler localHandler = new ODataHandler(odata, edm); + + ODataResponse response = localHandler.process(request); + assertNotNull(response); + assertEquals(500, response.getStatusCode()); + // TODO: Check for message in case of EdmException + // System.out.println(IOUtils.toString(response.getContent())); } } diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java index d5adc7912..91b58c0c3 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java @@ -148,21 +148,21 @@ public class UriInfoImplTest { 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 + queryOptions.add(expand.setName("")); + queryOptions.add(filter.setName("")); + queryOptions.add(format.setName("")); + queryOptions.add(id.setName("")); + queryOptions.add(inlinecount.setName("")); + queryOptions.add(orderby.setName("")); + queryOptions.add(search.setName("")); + queryOptions.add(select.setName("")); + queryOptions.add(skip.setName("")); + queryOptions.add(skipToken.setName("")); + queryOptions.add(top.setName("")); + queryOptions.add(customOption0.setName("")); + queryOptions.add(customOption1.setName("")); + queryOptions.add(levels.setName(""));// not stored + queryOptions.add(queryOption.setName(""));// not stored uriInfo.setQueryOptions(queryOptions); assertEquals(expand, uriInfo.getExpandOption()); diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java index a6f33ec9c..37da2239a 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java @@ -1067,21 +1067,18 @@ public class TestFullResourcePath { .isKeyPredicate(2, "KeyAlias2", "'3'") .isKeyPredicate(3, "KeyAlias3", "'4'"); - testUri.run("ESCollAllPrim(null)") - .isKind(UriInfoKind.resource).goPath() - .first() - .isEntitySet("ESCollAllPrim"); + testUri.runEx("ESCollAllPrim(null)").isExValidation(""); } @Test public void runEsNameParaKeys() throws Exception { testUri.run(encode("ESAllKey(PropertyString='O''Neil',PropertyBoolean=true,PropertyByte=255," + "PropertySByte=-128,PropertyInt16=-32768,PropertyInt32=-2147483648," - + "PropertyInt64=-9223372036854775808,PropertyDecimal=0.1,PropertyDate=2013-09-25," + + "PropertyInt64=-9223372036854775808,PropertyDecimal=1,PropertyDate=2013-09-25," + "PropertyDateTimeOffset=2002-10-10T12:00:00-05:00," - + "PropertyDuration=duration'P10DT5H34M21.123456789012S'," + + "PropertyDuration=duration'P50903316DT2H25M4S'," + "PropertyGuid=12345678-1234-1234-1234-123456789012," - + "PropertyTimeOfDay=12:34:55.123456789012)")) + + "PropertyTimeOfDay=12:34:55)")) .isKind(UriInfoKind.resource).goPath() .first() .isEntitySet("ESAllKey") @@ -1092,12 +1089,12 @@ public class TestFullResourcePath { .isKeyPredicate(4, "PropertyInt16", "-32768") .isKeyPredicate(5, "PropertyInt32", "-2147483648") .isKeyPredicate(6, "PropertyInt64", "-9223372036854775808") - .isKeyPredicate(7, "PropertyDecimal", "0.1") + .isKeyPredicate(7, "PropertyDecimal", "1") .isKeyPredicate(8, "PropertyDate", "2013-09-25") .isKeyPredicate(9, "PropertyDateTimeOffset", "2002-10-10T12:00:00-05:00") - .isKeyPredicate(10, "PropertyDuration", "duration'P10DT5H34M21.123456789012S'") + .isKeyPredicate(10, "PropertyDuration", "duration'P50903316DT2H25M4S'") .isKeyPredicate(11, "PropertyGuid", "12345678-1234-1234-1234-123456789012") - .isKeyPredicate(12, "PropertyTimeOfDay", "12:34:55.123456789012"); + .isKeyPredicate(12, "PropertyTimeOfDay", "12:34:55"); } @Test @@ -3321,7 +3318,7 @@ public class TestFullResourcePath { .goPath() .first() .isFunction("UFCRTETTwoKeyNavParamCTTwoPrim") - .isParameterAlias(0, "ParameterCTTwoPrim", "ParamAlias"); + .isParameterAlias(0, "ParameterCTTwoPrim", "@ParamAlias"); testFilter.runOnETTwoKeyNav("PropertyComp" + "/com.sap.odata.test1.BFCCTPrimCompRTESTwoKeyNavParam" @@ -3446,7 +3443,7 @@ public class TestFullResourcePath { .root().left().goPath() .first() .isFunction("UFCRTETTwoKeyNavParam") - .isParameterAlias(0, "ParameterInt16", "Param1Alias") + .isParameterAlias(0, "ParameterInt16", "@Param1Alias") .n() .isPrimitiveProperty("PropertyInt16", PropertyProvider.nameInt16, false); @@ -5091,7 +5088,8 @@ public class TestFullResourcePath { public void testAlias() throws Exception { testUri.run("ESTwoKeyNav(PropertyInt16=1,PropertyString=@A)?@A='2'").goPath() .isKeyPredicate(0, "PropertyInt16", "1") - .isKeyPredicateAlias(1, "PropertyString", "A") + .isKeyPredicateAlias(1, "PropertyString", "@A") + .isInAliasToValueMap("@A", "'2'") .goUpUriValidator() .isCustomParameter(0, "@A", "'2'"); diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java index 299af4187..bd9166660 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java @@ -47,14 +47,14 @@ public class TestUriParserImpl { private final String PropertyDate = "PropertyDate=2013-09-25"; private final String PropertyDateTimeOffset = "PropertyDateTimeOffset=2002-10-10T12:00:00-05:00"; private final String PropertyDecimal = "PropertyDecimal=12"; - private final String PropertyDuration = "PropertyDuration=duration'P10DT5H34M21.123456789012S'"; + private final String PropertyDuration = "PropertyDuration=duration'P50903316DT2H25M4S'"; private final String PropertyGuid = "PropertyGuid=12345678-1234-1234-1234-123456789012"; private final String PropertyInt16 = "PropertyInt16=1"; private final String PropertyInt32 = "PropertyInt32=12"; private final String PropertyInt64 = "PropertyInt64=64"; private final String PropertySByte = "PropertySByte=1"; private final String PropertyString = "PropertyString='ABC'"; - private final String PropertyTimeOfDay = "PropertyTimeOfDay=12:34:55.123456789012"; + private final String PropertyTimeOfDay = "PropertyTimeOfDay=12:34:55"; private final String allKeys = PropertyString + "," + PropertyInt16 + "," + PropertyBoolean + "," + PropertyByte + "," + PropertySByte + "," + PropertyInt32 + "," + PropertyInt64 + "," + PropertyDecimal + "," + PropertyDate @@ -358,9 +358,9 @@ public class TestUriParserImpl { .isKeyPredicate(7, "PropertyDecimal", "12") .isKeyPredicate(8, "PropertyDate", "2013-09-25") .isKeyPredicate(9, "PropertyDateTimeOffset", "2002-10-10T12:00:00-05:00") - .isKeyPredicate(10, "PropertyDuration", "duration'P10DT5H34M21.123456789012S'") + .isKeyPredicate(10, "PropertyDuration", "duration'P50903316DT2H25M4S'") .isKeyPredicate(11, "PropertyGuid", "12345678-1234-1234-1234-123456789012") - .isKeyPredicate(12, "PropertyTimeOfDay", "12:34:55.123456789012"); + .isKeyPredicate(12, "PropertyTimeOfDay", "12:34:55"); } @Test 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 index 4502ca613..0332c7351 100644 --- 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 @@ -597,4 +597,10 @@ public class ResourceValidator implements TestValidator { return this; } + public ResourceValidator isInAliasToValueMap(String alias, String value) { + String valueForAlias = uriInfo.getValueForAlias(alias); + assertEquals(value, valueForAlias); + return this; + } + } 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 index 022954b21..300c8fc2d 100644 --- 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 @@ -71,9 +71,12 @@ public class TestUriValidator implements TestValidator { uriInfo = null; try { uriInfo = (UriInfoImpl) parser.parseUri(uri, edm); + new UriValidator().validate(uriInfo, HttpMethod.GET); fail("Exception expected"); } catch (UriParserException e) { exception = e; + } catch (UriValidationException e) { + exception = e; } return this; @@ -252,4 +255,9 @@ public class TestUriValidator implements TestValidator { return this; } + public TestUriValidator isExValidation(String string) { + assertEquals(UriValidationException.class, exception.getClass()); + return this; + } + } diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/validator/UriValidatorTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/validator/UriValidatorTest.java index cdb8bc985..3b8ccb91d 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/validator/UriValidatorTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/validator/UriValidatorTest.java @@ -331,6 +331,12 @@ public class UriValidatorTest { parseAndValidate(uri, HttpMethod.GET); } + @Test(expected = UriValidationException.class) + public void validateKeyPredicatesWrongValueTypeForValidateMethod() throws Exception { + String uri = "ESTwoKeyNav(PropertyInt16='abc', PropertyString='abc')"; + parseAndValidate(uri, HttpMethod.GET); + } + @Test public void checkValidSystemQueryOption() throws Exception { String[] uris = constructUri(urisWithValidSystemQueryOptions);