[OLINGO-821] Support alias for enum values in URI
This commit is contained in:
parent
639362caa3
commit
c7838a678d
|
@ -43,7 +43,7 @@ public class EdmEnumTypeImpl extends EdmTypeImpl implements EdmEnumType {
|
||||||
|
|
||||||
private final EdmPrimitiveType underlyingType;
|
private final EdmPrimitiveType underlyingType;
|
||||||
private final CsdlEnumType enumType;
|
private final CsdlEnumType enumType;
|
||||||
private final String uriPrefix;
|
private final FullQualifiedName enumName;
|
||||||
private final String uriSuffix;
|
private final String uriSuffix;
|
||||||
private List<String> memberNames;
|
private List<String> memberNames;
|
||||||
private Map<String, EdmMember> membersMap;
|
private Map<String, EdmMember> membersMap;
|
||||||
|
@ -67,7 +67,7 @@ public class EdmEnumTypeImpl extends EdmTypeImpl implements EdmEnumType {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.enumType = enumType;
|
this.enumType = enumType;
|
||||||
uriPrefix = enumName.getFullQualifiedNameAsString() + '\'';
|
this.enumName = enumName;
|
||||||
uriSuffix = "'";
|
uriSuffix = "'";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,19 +228,35 @@ public class EdmEnumTypeImpl extends EdmTypeImpl implements EdmEnumType {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toUriLiteral(final String literal) {
|
public String toUriLiteral(final String literal) {
|
||||||
return literal == null ? null : uriPrefix + literal + uriSuffix;
|
return literal == null ? null : enumName.getFullQualifiedNameAsString() + "'" + literal + uriSuffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String fromUriLiteral(final String literal) throws EdmPrimitiveTypeException {
|
public String fromUriLiteral(final String literal) throws EdmPrimitiveTypeException {
|
||||||
if (literal == null) {
|
if (literal == null) {
|
||||||
return 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
|
@Override
|
||||||
|
|
|
@ -26,10 +26,13 @@ import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
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.Arrays;
|
||||||
import java.util.List;
|
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.EdmEnumType;
|
||||||
import org.apache.olingo.commons.api.edm.EdmException;
|
import org.apache.olingo.commons.api.edm.EdmException;
|
||||||
import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
|
import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
|
||||||
|
@ -53,15 +56,18 @@ public class EdmEnumTest {
|
||||||
private final EdmEnumType int32FlagType;
|
private final EdmEnumType int32FlagType;
|
||||||
|
|
||||||
public EdmEnumTest() {
|
public EdmEnumTest() {
|
||||||
|
Edm edm = mock(Edm.class);
|
||||||
final List<CsdlEnumMember> memberList = Arrays.asList(
|
final List<CsdlEnumMember> memberList = Arrays.asList(
|
||||||
new CsdlEnumMember().setName("first").setValue("1"),
|
new CsdlEnumMember().setName("first").setValue("1"),
|
||||||
new CsdlEnumMember().setName("second").setValue("64"));
|
new CsdlEnumMember().setName("second").setValue("64"));
|
||||||
|
|
||||||
final FullQualifiedName enumName = new FullQualifiedName("namespace", "name");
|
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)
|
new CsdlEnumType().setName("name").setMembers(memberList).setFlags(true)
|
||||||
.setUnderlyingType(EdmPrimitiveTypeKind.SByte.getFullQualifiedName()));
|
.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,
|
otherInstance = new EdmEnumTypeImpl(null, enumName,
|
||||||
new CsdlEnumType().setName("name").setMembers(memberList).setFlags(true)
|
new CsdlEnumType().setName("name").setMembers(memberList).setFlags(true)
|
||||||
|
@ -170,11 +176,15 @@ public class EdmEnumTest {
|
||||||
public void fromUriLiteral() throws Exception {
|
public void fromUriLiteral() throws Exception {
|
||||||
assertNull(instance.fromUriLiteral(null));
|
assertNull(instance.fromUriLiteral(null));
|
||||||
assertEquals("first", instance.fromUriLiteral("namespace.name'first'"));
|
assertEquals("first", instance.fromUriLiteral("namespace.name'first'"));
|
||||||
|
assertEquals("first", instance.fromUriLiteral("alias.name'first'"));
|
||||||
|
|
||||||
expectErrorInFromUriLiteral(instance, "");
|
expectErrorInFromUriLiteral(instance, "", null);
|
||||||
expectErrorInFromUriLiteral(instance, "name'first'");
|
expectErrorInFromUriLiteral(instance, "name'first'", null);
|
||||||
expectErrorInFromUriLiteral(instance, "namespace.name'first");
|
expectErrorInFromUriLiteral(instance, "namespace.name'first", null);
|
||||||
expectErrorInFromUriLiteral(instance, "namespace.namespace'first");
|
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
|
@Test
|
||||||
|
@ -279,13 +289,17 @@ public class EdmEnumTest {
|
||||||
expectContentErrorInValueToString(int16EnumType, Integer.MAX_VALUE);
|
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 {
|
try {
|
||||||
instance.fromUriLiteral(value);
|
instance.fromUriLiteral(value);
|
||||||
fail("Expected exception not thrown");
|
fail("Expected exception not thrown");
|
||||||
} catch (final EdmPrimitiveTypeException e) {
|
} catch (final EdmPrimitiveTypeException e) {
|
||||||
assertNotNull(e.getLocalizedMessage());
|
assertNotNull(e.getLocalizedMessage());
|
||||||
|
if(error == null){
|
||||||
assertThat(e.getLocalizedMessage(), containsString("' has illegal content."));
|
assertThat(e.getLocalizedMessage(), containsString("' has illegal content."));
|
||||||
|
}else{
|
||||||
|
assertThat(e.getLocalizedMessage(), containsString(error));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -88,6 +88,14 @@ public class TestFullResourcePath {
|
||||||
.isEntitySet("ESMixEnumDefCollComp")
|
.isEntitySet("ESMixEnumDefCollComp")
|
||||||
.goUpUriValidator()
|
.goUpUriValidator()
|
||||||
.goFilter().is("<<PropertyEnumString> has <olingo.odata.test1.ENString<String1>>>");
|
.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
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue