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

This commit is contained in:
Christian Amend 2015-11-09 15:46:12 +01:00
parent 639362caa3
commit c7838a678d
3 changed files with 55 additions and 17 deletions

View File

@ -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

View File

@ -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'"));
assertEquals("first", instance.fromUriLiteral("alias.name'first'"));
expectErrorInFromUriLiteral(instance, "");
expectErrorInFromUriLiteral(instance, "name'first'");
expectErrorInFromUriLiteral(instance, "namespace.name'first");
expectErrorInFromUriLiteral(instance, "namespace.namespace'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));
}
}
}

View File

@ -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