Repository: olingo-odata4 Updated Branches: refs/heads/master 19cfe4b49 -> 7a56dfa39
[OLINGO-1013] Added support for aliase in core and tecsvc. Signed-off-by: Michael Bolz <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/7a56dfa3 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/7a56dfa3 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/7a56dfa3 Branch: refs/heads/master Commit: 7a56dfa3930e8ba48f5202cb33e0b2d5375e8267 Parents: 19cfe4b Author: Morten Riedel <[email protected]> Authored: Fri Aug 26 14:17:27 2016 +0200 Committer: Michael Bolz <[email protected]> Committed: Mon Aug 29 07:45:32 2016 +0200 ---------------------------------------------------------------------- .../olingo/fit/tecsvc/client/BasicITCase.java | 30 ++++++++++++++ .../olingo/server/core/uri/UriHelperImpl.java | 41 +++++++++++++++++++- .../olingo/server/tecsvc/data/DataCreator.java | 2 +- .../olingo/server/tecsvc/data/DataProvider.java | 35 +++++++++++++++-- .../json/ODataJsonSerializerTest.java | 5 ++- .../serializer/utils/ContextURLHelperTest.java | 24 ++++++++++++ .../serializer/xml/ODataXmlSerializerTest.java | 15 ++++--- 7 files changed, 138 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7a56dfa3/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java ---------------------------------------------------------------------- diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java index 101f8a9..e8dd53f 100644 --- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java @@ -35,6 +35,7 @@ import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import org.apache.olingo.client.api.ODataClient; import org.apache.olingo.client.api.communication.ODataClientErrorException; @@ -582,6 +583,35 @@ public class BasicITCase extends AbstractParamTecSvcITCase { } @Test + public void readPropertyValueFromEntityWithAlias() { + Map<String, Object> segmentValues = new LinkedHashMap<String, Object>(); + segmentValues.put("PropertyInt16", 1); + segmentValues.put("KeyAlias1", 11); + segmentValues.put("KeyAlias2", "Num11"); + segmentValues.put("KeyAlias3", "Num111"); + + final URIBuilder uriBuilder = getClient().newURIBuilder(SERVICE_URI). + appendEntitySetSegment("ESFourKeyAlias") + .appendKeySegment(segmentValues) + .appendPropertySegment("PropertyCompComp"); + final ODataPropertyRequest<ClientProperty> req = getClient().getRetrieveRequestFactory(). + getPropertyRequest(uriBuilder.build()); + req.setFormat(contentType); + + final ClientProperty prop = req.execute().getBody(); + assertNotNull(prop); + final ClientComplexValue complexValue = prop.getComplexValue(); + assertNotNull(complexValue); + final ClientValue propertyComp = complexValue.get("PropertyComp").getValue(); + assertNotNull(propertyComp); + final ClientProperty propertyInt = propertyComp.asComplex().get("PropertyInt16"); + assertNotNull(propertyInt); + final ClientPrimitiveValue clientValue = propertyInt.getPrimitiveValue(); + assertNotNull(clientValue); + assertEquals("111", clientValue.toString()); + } + + @Test public void updateCollectionOfComplexCollection() { final ClientEntity entity = getFactory().newEntity(ET_KEY_NAV); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7a56dfa3/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/UriHelperImpl.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/UriHelperImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/UriHelperImpl.java index b2917f4..f5d4bd0 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/UriHelperImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/UriHelperImpl.java @@ -21,9 +21,11 @@ package org.apache.olingo.server.core.uri; import java.util.List; import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmKeyPropertyRef; import org.apache.olingo.commons.api.edm.EdmPrimitiveType; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.edm.EdmProperty; @@ -68,6 +70,7 @@ public class UriHelperImpl implements UriHelper { final List<String> keyNames = edmEntityType.getKeyPredicateNames(); boolean first = true; for (final String keyName : keyNames) { + EdmKeyPropertyRef refType = edmEntityType.getKeyPropertyRef(keyName); if (first) { first = false; } else { @@ -76,13 +79,13 @@ public class UriHelperImpl implements UriHelper { if (keyNames.size() > 1) { result.append(Encoder.encode(keyName)).append('='); } - final EdmProperty edmProperty = edmEntityType.getStructuralProperty(keyName); + final EdmProperty edmProperty = refType.getProperty(); if (edmProperty == null) { throw new SerializerException("Property not found (possibly an alias): " + keyName, SerializerException.MessageKeys.MISSING_PROPERTY, keyName); } final EdmPrimitiveType type = (EdmPrimitiveType) edmProperty.getType(); - final Object propertyValue = entity.getProperty(keyName).getValue(); + final Object propertyValue = findPropertyRefValue(entity, refType); try { final String value = type.toUriLiteral( type.valueToString(propertyValue, @@ -96,7 +99,41 @@ public class UriHelperImpl implements UriHelper { } return result.toString(); } + + private Object findPropertyRefValue(Entity entity, EdmKeyPropertyRef refType) { + final int INDEX_ERROR_CODE = -1; + final String propertyPath = refType.getName(); + String tmpPropertyName; + int lastIndex; + int index = propertyPath.indexOf('/'); + if (index == INDEX_ERROR_CODE) { + index = propertyPath.length(); + } + tmpPropertyName = propertyPath.substring(0, index); + //get first property + Property prop = entity.getProperty(tmpPropertyName); + //get following properties + while (index < propertyPath.length()) { + lastIndex = ++index; + index = propertyPath.indexOf('/', index+1); + if (index == INDEX_ERROR_CODE) { + index = propertyPath.length(); + } + tmpPropertyName = propertyPath.substring(lastIndex, index); + prop = findProperty(tmpPropertyName, prop.asComplex().getValue()); + } + return prop.getValue(); + } + private Property findProperty(final String propertyName, final List<Property> properties) { + for (final Property property : properties) { + if (propertyName.equals(property.getName())) { + return property; + } + } + return null; + } + @Override public UriResourceEntitySet parseEntityId(final Edm edm, final String entityId, final String rawServiceRoot) throws DeserializerException { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7a56dfa3/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataCreator.java ---------------------------------------------------------------------- diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataCreator.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataCreator.java index 542e393..60f0273 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataCreator.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataCreator.java @@ -1494,7 +1494,7 @@ public class DataCreator { try { entity.setId(URI.create(helper.buildCanonicalURL(entitySet, entity))); } catch (final SerializerException e) { - entity.setId(URI.create("id")); + entity.setId(null); } } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7a56dfa3/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataProvider.java ---------------------------------------------------------------------- diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataProvider.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataProvider.java index da5da65..465ab80 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataProvider.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataProvider.java @@ -43,6 +43,7 @@ import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; import org.apache.olingo.commons.api.edm.EdmEnumType; import org.apache.olingo.commons.api.edm.EdmFunction; +import org.apache.olingo.commons.api.edm.EdmKeyPropertyRef; import org.apache.olingo.commons.api.edm.EdmNavigationProperty; import org.apache.olingo.commons.api.edm.EdmParameter; import org.apache.olingo.commons.api.edm.EdmPrimitiveType; @@ -95,16 +96,44 @@ public class DataProvider { final EntityCollection entitySet = readAll(edmEntitySet); return entitySet == null ? null : read(edmEntitySet.getEntityType(), entitySet, keys); } - + + private Object findPropertyRefValue(Entity entity, EdmKeyPropertyRef refType) { + final int INDEX_ERROR_CODE = -1; + final String propertyPath = refType.getName(); + String tmpPropertyName; + int lastIndex; + int index = propertyPath.indexOf('/'); + if (index == INDEX_ERROR_CODE) { + index = propertyPath.length(); + } + tmpPropertyName = propertyPath.substring(0, index); + //get first property + Property prop = entity.getProperty(tmpPropertyName); + //get following properties + while (index < propertyPath.length()) { + lastIndex = ++index; + index = propertyPath.indexOf('/', index+1); + if (index == INDEX_ERROR_CODE) { + index = propertyPath.length(); + } + tmpPropertyName = propertyPath.substring(lastIndex, index); + prop = findProperty(tmpPropertyName, prop.asComplex().getValue()); + } + return prop.getValue(); + } + public Entity read(final EdmEntityType edmEntityType, final EntityCollection entitySet, final List<UriParameter> keys) throws DataProviderException { try { for (final Entity entity : entitySet.getEntities()) { boolean found = true; for (final UriParameter key : keys) { - final EdmProperty property = (EdmProperty) edmEntityType.getProperty(key.getName()); + EdmKeyPropertyRef refType = edmEntityType.getKeyPropertyRef(key.getName()); + Object value = findPropertyRefValue(entity, refType); + + final EdmProperty property = refType.getProperty(); final EdmPrimitiveType type = (EdmPrimitiveType) property.getType(); - final Object value = entity.getProperty(key.getName()).getValue(); + if (key.getExpression() != null && !(key.getExpression() instanceof Literal)) { throw new DataProviderException("Expression in key value is not supported yet!", HttpStatusCode.NOT_IMPLEMENTED); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7a56dfa3/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java index 568ae52..2e86298 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java @@ -971,7 +971,8 @@ public class ODataJsonSerializerTest { + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + "\"value\":[" + "{" - + "\"@odata.id\":\"id\"," + + "\"@odata.id\":\"" + + "ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')\"," + "\"PropertyInt16\":1," + "\"PropertyCompComp\":{" + "\"PropertyComp\":{" @@ -1036,7 +1037,7 @@ public class ODataJsonSerializerTest { + "(PropertyComp/PropertyString,PropertyCompComp/PropertyComp)\"," + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + "\"value\":[{" - + "\"@odata.id\":\"id\"," + + "\"@odata.id\":\"ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')\"," + "\"PropertyComp\":{" + "\"PropertyString\":\"Num11\"" + "}," http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7a56dfa3/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java index d59dbfd..ec94fb0 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java @@ -296,4 +296,28 @@ public class ContextURLHelperTest { assertEquals("$metadata#ESTwoKeyNav(PropertyInt16=1,PropertyString='2')/PropertyInt16", ContextURLBuilder.create(contextURL).toASCIIString()); } + + @Test + public void buildKeyAlias() throws Exception { + final EdmEntitySet entitySet = entityContainer.getEntitySet("ESFourKeyAlias"); + final EdmProperty edmProperty = entitySet.getEntityType().getStructuralProperty("PropertyComp"); + UriParameter key1 = Mockito.mock(UriParameter.class); + Mockito.when(key1.getName()).thenReturn("PropertyInt16"); + Mockito.when(key1.getText()).thenReturn("1"); + UriParameter key2 = Mockito.mock(UriParameter.class); + Mockito.when(key2.getName()).thenReturn("KeyAlias1"); + Mockito.when(key2.getText()).thenReturn("11"); + UriParameter key3 = Mockito.mock(UriParameter.class); + Mockito.when(key3.getName()).thenReturn("KeyAlias2"); + Mockito.when(key3.getText()).thenReturn("'Num11'"); + UriParameter key4 = Mockito.mock(UriParameter.class); + Mockito.when(key4.getName()).thenReturn("KeyAlias3"); + Mockito.when(key4.getText()).thenReturn("'Num111'"); + final ContextURL contextURL = ContextURL.with().entitySet(entitySet) + .keyPath(ContextURLHelper.buildKeyPredicate(Arrays.asList(key1, key2, key3, key4))) + .navOrPropertyPath(edmProperty.getName()).build(); + assertEquals("$metadata#ESFourKeyAlias" + + "(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')/PropertyComp", + ContextURLBuilder.create(contextURL).toASCIIString()); + } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7a56dfa3/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializerTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializerTest.java index 7d718bd..64acb48 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializerTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializerTest.java @@ -1179,14 +1179,15 @@ public class ODataXmlSerializerTest { "m:metadata-etag=\"metadataETag\">\n" + "<a:id>http://host/svc/ESFourKeyAlias</a:id>\n" + "<a:entry>\n" + - "<a:id>id</a:id>\n" + + "<a:id>ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')</a:id>\n" + "<a:title />\n" + "<a:summary />\n" + "<a:updated>" + UPDATED_FORMAT.format(new Date(currentTimeMillis)) + "</a:updated>" + "<a:author>\n" + "<a:name/>\n" + "</a:author>\n" + - "<a:link rel=\"edit\" href=\"id\"/>\n" + + "<a:link rel=\"edit\" " + + "href=\"ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')\"/>\n" + "<a:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\" " + "term=\"#olingo.odata.test1.ETFourKeyAlias\"/>\n" + "<a:content type=\"application/xml\">\n" + @@ -1225,14 +1226,15 @@ public class ODataXmlSerializerTest { "m:metadata-etag=\"metadataETag\">\n" + "<a:id>http://host/svc/ESFourKeyAlias</a:id>\n" + "<a:entry>\n" + - "<a:id>id</a:id>\n" + + "<a:id>ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')</a:id>\n" + "<a:title />\n" + "<a:summary />\n" + "<a:updated>" + UPDATED_FORMAT.format(new Date(currentTimeMillis)) + "</a:updated>" + "<a:author>\n" + "<a:name/>\n" + "</a:author>\n" + - "<a:link rel=\"edit\" href=\"id\"/>\n" + + "<a:link rel=\"edit\" " + + "href=\"ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')\"/>\n" + "<a:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\" " + "term=\"#olingo.odata.test1.ETFourKeyAlias\"/>\n" + "<a:content type=\"application/xml\">\n" + @@ -1284,14 +1286,15 @@ public class ODataXmlSerializerTest { "m:metadata-etag=\"metadataETag\">\n" + "<a:id>http://host/svc/ESFourKeyAlias</a:id>\n" + "<a:entry>\n" + - "<a:id>id</a:id>\n" + + "<a:id>ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')</a:id>\n" + "<a:title />\n" + "<a:summary />\n" + "<a:updated>" + UPDATED_FORMAT.format(new Date(currentTimeMillis)) + "</a:updated>" + "<a:author>\n" + "<a:name/>\n" + "</a:author>\n" + - "<a:link rel=\"edit\" href=\"id\"/>\n" + + "<a:link rel=\"edit\" " + + "href=\"ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')\"/>\n" + "<a:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\" " + "term=\"#olingo.odata.test1.ETFourKeyAlias\"/>\n" + "<a:content type=\"application/xml\">\n" +
