Repository: olingo-odata4
Updated Branches:
  refs/heads/master 639362caa -> c7838a678


[OLINGO-821] Support alias for enum values in URI


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/c7838a67
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/c7838a67
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/c7838a67

Branch: refs/heads/master
Commit: c7838a678d96a92f6c77aae617b0e840ebbbb9a5
Parents: 639362c
Author: Christian Amend <[email protected]>
Authored: Mon Nov 9 15:46:12 2015 +0100
Committer: Christian Amend <[email protected]>
Committed: Mon Nov 9 15:46:12 2015 +0100

----------------------------------------------------------------------
 .../commons/core/edm/EdmEnumTypeImpl.java       | 36 ++++++++++++++------
 .../server/core/edm/provider/EdmEnumTest.java   | 30 +++++++++++-----
 .../core/uri/antlr/TestFullResourcePath.java    |  8 +++++
 3 files changed, 56 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/c7838a67/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEnumTypeImpl.java
----------------------------------------------------------------------
diff --git 
a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEnumTypeImpl.java
 
b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEnumTypeImpl.java
index 98a0962..1f7f070 100644
--- 
a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEnumTypeImpl.java
+++ 
b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmEnumTypeImpl.java
@@ -6,9 +6,9 @@
  * 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
@@ -43,7 +43,7 @@ public class EdmEnumTypeImpl extends EdmTypeImpl implements 
EdmEnumType {
 
   private final EdmPrimitiveType underlyingType;
   private final CsdlEnumType enumType;
-  private final String uriPrefix;
+  private final FullQualifiedName enumName;
   private final String uriSuffix;
   private List<String> memberNames;
   private Map<String, EdmMember> membersMap;
@@ -67,7 +67,7 @@ public class EdmEnumTypeImpl extends EdmTypeImpl implements 
EdmEnumType {
     }
 
     this.enumType = enumType;
-    uriPrefix = enumName.getFullQualifiedNameAsString() + '\'';
+    this.enumName = enumName;
     uriSuffix = "'";
   }
 
@@ -228,19 +228,35 @@ public class EdmEnumTypeImpl extends EdmTypeImpl 
implements EdmEnumType {
 
   @Override
   public String toUriLiteral(final String literal) {
-    return literal == null ? null : uriPrefix + literal + uriSuffix;
+    return literal == null ? null : enumName.getFullQualifiedNameAsString() + 
"'" + literal + uriSuffix;
   }
 
   @Override
   public String fromUriLiteral(final String literal) throws 
EdmPrimitiveTypeException {
     if (literal == null) {
       return null;
-    } else if (literal.length() >= uriPrefix.length() + uriSuffix.length()
-        && literal.startsWith(uriPrefix) && literal.endsWith(uriSuffix)) {
-      return literal.substring(uriPrefix.length(), literal.length() - 
uriSuffix.length());
-    } else {
-      throw new EdmPrimitiveTypeException("The literal '" + literal + "' has 
illegal content.");
     }
+
+    if (literal.endsWith(uriSuffix)) {
+      String[] splitLiteral = literal.split("'");
+      if (splitLiteral.length != 2) {
+        throw new EdmPrimitiveTypeException("The literal '" + literal
+            + "' must be of format FullQuallifiedTypeName'literal'");
+      }
+      // First part must be the FullQualifiedName
+      FullQualifiedName typeFqn = null;
+      try {
+        typeFqn = new FullQualifiedName(splitLiteral[0]);
+      } catch (IllegalArgumentException e) {
+        throw new EdmPrimitiveTypeException("The literal '" + literal + "' has 
illegal content.", e);
+      }
+      // Get itself. This will also resolve a possible alias
+      EdmEnumType prospect = edm.getEnumType(typeFqn);
+      if (prospect != null && 
enumName.equals(prospect.getFullQualifiedName())) {
+        return splitLiteral[1];
+      }
+    }
+    throw new EdmPrimitiveTypeException("The literal '" + literal + "' has 
illegal content.");
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/c7838a67/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEnumTest.java
----------------------------------------------------------------------
diff --git 
a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEnumTest.java
 
b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEnumTest.java
index b027c64..dbcda3a 100644
--- 
a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEnumTest.java
+++ 
b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEnumTest.java
@@ -26,10 +26,13 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
 import java.util.List;
 
+import org.apache.olingo.commons.api.edm.Edm;
 import org.apache.olingo.commons.api.edm.EdmEnumType;
 import org.apache.olingo.commons.api.edm.EdmException;
 import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
@@ -53,15 +56,18 @@ public class EdmEnumTest {
   private final EdmEnumType int32FlagType;
 
   public EdmEnumTest() {
+    Edm edm = mock(Edm.class);
     final List<CsdlEnumMember> memberList = Arrays.asList(
         new CsdlEnumMember().setName("first").setValue("1"),
         new CsdlEnumMember().setName("second").setValue("64"));
 
     final FullQualifiedName enumName = new FullQualifiedName("namespace", 
"name");
 
-    instance = new EdmEnumTypeImpl(null, enumName,
+    instance = new EdmEnumTypeImpl(edm, enumName,
         new 
CsdlEnumType().setName("name").setMembers(memberList).setFlags(true)
             
.setUnderlyingType(EdmPrimitiveTypeKind.SByte.getFullQualifiedName()));
+    when(edm.getEnumType(new 
FullQualifiedName("namespace.name"))).thenReturn(instance);
+    when(edm.getEnumType(new 
FullQualifiedName("alias.name"))).thenReturn(instance);
     
     otherInstance = new EdmEnumTypeImpl(null, enumName,
         new 
CsdlEnumType().setName("name").setMembers(memberList).setFlags(true)
@@ -170,11 +176,15 @@ public class EdmEnumTest {
   public void fromUriLiteral() throws Exception {
     assertNull(instance.fromUriLiteral(null));
     assertEquals("first", instance.fromUriLiteral("namespace.name'first'"));
-
-    expectErrorInFromUriLiteral(instance, "");
-    expectErrorInFromUriLiteral(instance, "name'first'");
-    expectErrorInFromUriLiteral(instance, "namespace.name'first");
-    expectErrorInFromUriLiteral(instance, "namespace.namespace'first");
+    assertEquals("first", instance.fromUriLiteral("alias.name'first'"));
+
+    expectErrorInFromUriLiteral(instance, "", null);
+    expectErrorInFromUriLiteral(instance, "name'first'", null);
+    expectErrorInFromUriLiteral(instance, "namespace.name'first", null);
+    expectErrorInFromUriLiteral(instance, "namespace.namespace'first", null);
+    expectErrorInFromUriLiteral(instance, "namespace.namespace'fi'rst", null);
+    expectErrorInFromUriLiteral(instance, "namespace.namespace'first'", null);
+    expectErrorInFromUriLiteral(instance, "namespace.name'fir'st'", "must be 
of format");
   }
 
   @Test
@@ -279,13 +289,17 @@ public class EdmEnumTest {
     expectContentErrorInValueToString(int16EnumType, Integer.MAX_VALUE);
   }
 
-  protected void expectErrorInFromUriLiteral(final EdmPrimitiveType instance, 
final String value) {
+  protected void expectErrorInFromUriLiteral(final EdmPrimitiveType instance, 
final String value, final String error) {
     try {
       instance.fromUriLiteral(value);
       fail("Expected exception not thrown");
     } catch (final EdmPrimitiveTypeException e) {
       assertNotNull(e.getLocalizedMessage());
-      assertThat(e.getLocalizedMessage(), containsString("' has illegal 
content."));
+      if(error == null){
+        assertThat(e.getLocalizedMessage(), containsString("' has illegal 
content."));
+      }else{
+        assertThat(e.getLocalizedMessage(), containsString(error));
+      }
     }
   }
 

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/c7838a67/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java
----------------------------------------------------------------------
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 69458ab..58ccf16 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
@@ -88,6 +88,14 @@ public class TestFullResourcePath {
         .isEntitySet("ESMixEnumDefCollComp")
         .goUpUriValidator()
         .goFilter().is("<<PropertyEnumString> has 
<olingo.odata.test1.ENString<String1>>>");
+
+    testUri
+        
.run("ESMixEnumDefCollComp(PropertyEnumString=Namespace1_Alias.ENString'String1',PropertyDefString='abc')")
+        .goPath()
+        .at(0)
+        .isEntitySet("ESMixEnumDefCollComp")
+        .isKeyPredicate(0, "PropertyEnumString", 
"Namespace1_Alias.ENString'String1'")
+        .isKeyPredicate(1, "PropertyDefString", "'abc'");
   }
 
   @Test

Reply via email to