Repository: olingo-odata2 Updated Branches: refs/heads/master 14e9a0cb7 -> d8afd0183
[OLINGO-308] fix + test implemented Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/d8afd018 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/d8afd018 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/d8afd018 Branch: refs/heads/master Commit: d8afd018323100534823d4974bf36f555653b8b9 Parents: 14e9a0c Author: Stephan Klevenz <[email protected]> Authored: Wed May 28 15:39:37 2014 +0200 Committer: Stephan Klevenz <[email protected]> Committed: Wed May 28 15:39:37 2014 +0200 ---------------------------------------------------------------------- .../api/ep/EntityProviderWriteProperties.java | 11 ++ .../ep/producer/AtomEntryEntityProducer.java | 131 +++++++++++-------- .../core/ep/producer/AtomEntryProducerTest.java | 22 ++++ 3 files changed, 107 insertions(+), 57 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/d8afd018/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/ep/EntityProviderWriteProperties.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/ep/EntityProviderWriteProperties.java b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/ep/EntityProviderWriteProperties.java index 822fc84..1fa674f 100644 --- a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/ep/EntityProviderWriteProperties.java +++ b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/ep/EntityProviderWriteProperties.java @@ -44,6 +44,7 @@ public class EntityProviderWriteProperties { private URI selfLink; private boolean includeSimplePropertyType; private Map<String, Map<String, Object>> additionalLinks; + private boolean ignoreKey = false; private EntityProviderWriteProperties() {} @@ -239,6 +240,12 @@ public class EntityProviderWriteProperties { this.properties.selfLink = properties.getSelfLink(); this.properties.includeSimplePropertyType = properties.includeSimplePropertyType; this.properties.additionalLinks = properties.additionalLinks; + this.properties.ignoreKey = properties.ignoreKey; + return this; + } + + public ODataEntityProviderPropertiesBuilder ignoreKey(boolean ignoreKey) { + properties.ignoreKey = ignoreKey; return this; } } @@ -248,4 +255,8 @@ public class EntityProviderWriteProperties { EntityProviderWriteProperties.serviceRoot(properties.getServiceRoot()); return builder.fromProperties(properties); } + + public boolean isIgnoreKey() { + return ignoreKey; + } } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/d8afd018/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/AtomEntryEntityProducer.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/AtomEntryEntityProducer.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/AtomEntryEntityProducer.java index 95702dd..4036df5 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/AtomEntryEntityProducer.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/AtomEntryEntityProducer.java @@ -95,7 +95,11 @@ public class AtomEntryEntityProducer { } // write all atom infos (mandatory and optional) - String selfLink = createSelfLink(eia, data, null); + String selfLink = null; + if (!properties.isIgnoreKey()) { + selfLink = createSelfLink(eia, data, null); + } + appendAtomMandatoryParts(writer, eia, data, selfLink); appendAtomOptionalParts(writer, eia, data); @@ -185,7 +189,9 @@ public class AtomEntryEntityProducer { final Map<String, Map<String, Object>> links = properties.getAdditionalLinks(); final Map<String, Object> key = links == null ? null : links.get(name); if (key == null || key.isEmpty()) { - appendAtomNavigationLink(writer, createSelfLink(eia, data, name), name, isFeed, eia, data); + if (!properties.isIgnoreKey()) { + appendAtomNavigationLink(writer, createSelfLink(eia, data, name), name, isFeed, eia, data); + } } else { final EntityInfoAggregator targetEntityInfo = EntityInfoAggregator.create( eia.getEntitySet().getRelatedEntitySet((EdmNavigationProperty) eia.getEntityType().getProperty(name))); @@ -306,11 +312,13 @@ public class AtomEntryEntityProducer { private void appendAtomEditLink(final XMLStreamWriter writer, final EntityInfoAggregator eia, final Map<String, Object> data, final String selfLink) throws EntityProviderException { try { - writer.writeStartElement(FormatXml.ATOM_LINK); - writer.writeAttribute(FormatXml.ATOM_HREF, selfLink); - writer.writeAttribute(FormatXml.ATOM_REL, Edm.LINK_REL_EDIT); - writer.writeAttribute(FormatXml.ATOM_TITLE, eia.getEntityType().getName()); - writer.writeEndElement(); + if (selfLink != null) { + writer.writeStartElement(FormatXml.ATOM_LINK); + writer.writeAttribute(FormatXml.ATOM_HREF, selfLink); + writer.writeAttribute(FormatXml.ATOM_REL, Edm.LINK_REL_EDIT); + writer.writeAttribute(FormatXml.ATOM_TITLE, eia.getEntityType().getName()); + writer.writeEndElement(); + } } catch (XMLStreamException e) { throw new EntityProviderException(EntityProviderException.COMMON, e); } catch (EdmException e) { @@ -321,25 +329,27 @@ public class AtomEntryEntityProducer { private void appendAtomContentLink(final XMLStreamWriter writer, final EntityInfoAggregator eia, final Map<String, Object> data, final String selfLink) throws EntityProviderException, EdmException { try { - String mediaResourceMimeType = properties.getMediaResourceMimeType(); - if (mediaResourceMimeType == null) { - EdmMapping entityTypeMapping = eia.getEntityType().getMapping(); - if (entityTypeMapping != null) { - String mediaResourceMimeTypeKey = entityTypeMapping.getMediaResourceMimeTypeKey(); - if (mediaResourceMimeTypeKey != null) { - mediaResourceMimeType = (String) data.get(mediaResourceMimeTypeKey); - } - } + if (selfLink != null) { + String mediaResourceMimeType = properties.getMediaResourceMimeType(); if (mediaResourceMimeType == null) { - mediaResourceMimeType = ContentType.APPLICATION_OCTET_STREAM.toString(); + EdmMapping entityTypeMapping = eia.getEntityType().getMapping(); + if (entityTypeMapping != null) { + String mediaResourceMimeTypeKey = entityTypeMapping.getMediaResourceMimeTypeKey(); + if (mediaResourceMimeTypeKey != null) { + mediaResourceMimeType = (String) data.get(mediaResourceMimeTypeKey); + } + } + if (mediaResourceMimeType == null) { + mediaResourceMimeType = ContentType.APPLICATION_OCTET_STREAM.toString(); + } } - } - writer.writeStartElement(FormatXml.ATOM_LINK); - writer.writeAttribute(FormatXml.ATOM_HREF, selfLink + "/$value"); - writer.writeAttribute(FormatXml.ATOM_REL, Edm.LINK_REL_EDIT_MEDIA); - writer.writeAttribute(FormatXml.ATOM_TYPE, mediaResourceMimeType); - writer.writeEndElement(); + writer.writeStartElement(FormatXml.ATOM_LINK); + writer.writeAttribute(FormatXml.ATOM_HREF, selfLink + "/$value"); + writer.writeAttribute(FormatXml.ATOM_REL, Edm.LINK_REL_EDIT_MEDIA); + writer.writeAttribute(FormatXml.ATOM_TYPE, mediaResourceMimeType); + writer.writeEndElement(); + } } catch (XMLStreamException e) { throw new EntityProviderException(EntityProviderException.COMMON, e); } @@ -348,42 +358,44 @@ public class AtomEntryEntityProducer { private void appendAtomContentPart(final XMLStreamWriter writer, final EntityInfoAggregator eia, final Map<String, Object> data, final String selfLink) throws EntityProviderException, EdmException { try { + if (selfLink != null) { + // We have to support the media resource mime type at the properties till version 1.2 then this can be + // refactored + String mediaResourceMimeType = properties.getMediaResourceMimeType(); + EdmMapping entityTypeMapping = eia.getEntityType().getMapping(); + String self = null; - // We have to support the media resource mime type at the properties till version 1.2 then this can be refactored - String mediaResourceMimeType = properties.getMediaResourceMimeType(); - EdmMapping entityTypeMapping = eia.getEntityType().getMapping(); - String self = null; - - if (entityTypeMapping != null) { - String mediaResourceSourceKey = entityTypeMapping.getMediaResourceSourceKey(); - if (mediaResourceSourceKey != null) { - self = (String) data.get(mediaResourceSourceKey); - } - if (self == null) { - self = selfLink + "/$value"; - } - if (mediaResourceMimeType == null) { - String mediaResourceMimeTypeKey = - entityTypeMapping.getMimeType() != null ? entityTypeMapping.getMimeType() - : entityTypeMapping.getMediaResourceMimeTypeKey(); - if (mediaResourceMimeTypeKey != null) { - mediaResourceMimeType = (String) data.get(mediaResourceMimeTypeKey); + if (entityTypeMapping != null) { + String mediaResourceSourceKey = entityTypeMapping.getMediaResourceSourceKey(); + if (mediaResourceSourceKey != null) { + self = (String) data.get(mediaResourceSourceKey); + } + if (self == null) { + self = selfLink + "/$value"; } if (mediaResourceMimeType == null) { + String mediaResourceMimeTypeKey = + entityTypeMapping.getMimeType() != null ? entityTypeMapping.getMimeType() + : entityTypeMapping.getMediaResourceMimeTypeKey(); + if (mediaResourceMimeTypeKey != null) { + mediaResourceMimeType = (String) data.get(mediaResourceMimeTypeKey); + } + if (mediaResourceMimeType == null) { + mediaResourceMimeType = ContentType.APPLICATION_OCTET_STREAM.toString(); + } + } + } else { + self = selfLink + "/$value"; + if (mediaResourceMimeType == null) { mediaResourceMimeType = ContentType.APPLICATION_OCTET_STREAM.toString(); } } - } else { - self = selfLink + "/$value"; - if (mediaResourceMimeType == null) { - mediaResourceMimeType = ContentType.APPLICATION_OCTET_STREAM.toString(); - } - } - writer.writeStartElement(FormatXml.ATOM_CONTENT); - writer.writeAttribute(FormatXml.ATOM_TYPE, mediaResourceMimeType); - writer.writeAttribute(FormatXml.ATOM_SRC, self); - writer.writeEndElement(); + writer.writeStartElement(FormatXml.ATOM_CONTENT); + writer.writeAttribute(FormatXml.ATOM_TYPE, mediaResourceMimeType); + writer.writeAttribute(FormatXml.ATOM_SRC, self); + writer.writeEndElement(); + } } catch (XMLStreamException e) { throw new EntityProviderException(EntityProviderException.COMMON, e); } @@ -392,10 +404,12 @@ public class AtomEntryEntityProducer { private void appendAtomMandatoryParts(final XMLStreamWriter writer, final EntityInfoAggregator eia, final Map<String, Object> data, final String selfLink) throws EntityProviderException { try { - writer.writeStartElement(FormatXml.ATOM_ID); - location = properties.getServiceRoot().toASCIIString() + selfLink; - writer.writeCharacters(location); - writer.writeEndElement(); + if (selfLink != null) { + writer.writeStartElement(FormatXml.ATOM_ID); + location = properties.getServiceRoot().toASCIIString() + selfLink; + writer.writeCharacters(location); + writer.writeEndElement(); + } writer.writeStartElement(FormatXml.ATOM_TITLE); writer.writeAttribute(FormatXml.ATOM_TYPE, FormatXml.ATOM_TEXT); @@ -571,7 +585,10 @@ public class AtomEntryEntityProducer { if (isNotMappedViaCustomMapping(propertyInfo)) { Object value = data.get(propertyName); XmlPropertyEntityProducer aps = new XmlPropertyEntityProducer(properties.isIncludeSimplePropertyType()); - aps.append(writer, propertyInfo.getName(), propertyInfo, value); + + if (!(eia.getKeyPropertyInfos().contains(propertyInfo) && properties.isIgnoreKey())) { + aps.append(writer, propertyInfo.getName(), propertyInfo, value); + } } } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/d8afd018/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/AtomEntryProducerTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/AtomEntryProducerTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/AtomEntryProducerTest.java index a87698d..cd65a8c 100644 --- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/AtomEntryProducerTest.java +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/AtomEntryProducerTest.java @@ -56,6 +56,7 @@ import org.apache.olingo.odata2.api.processor.ODataResponse; import org.apache.olingo.odata2.core.commons.ContentType; import org.apache.olingo.odata2.core.ep.AbstractProviderTest; import org.apache.olingo.odata2.core.ep.AtomEntityProvider; +import org.apache.olingo.odata2.core.ep.consumer.XmlEntityConsumer; import org.apache.olingo.odata2.testutil.helper.StringHelper; import org.apache.olingo.odata2.testutil.helper.XMLUnitHelper; import org.apache.olingo.odata2.testutil.mock.MockFacade; @@ -862,4 +863,25 @@ public class AtomEntryProducerTest extends AbstractProviderTest { private void verifyTagOrdering(final String xmlString, final String... toCheckTags) { XMLUnitHelper.verifyTagOrdering(xmlString, toCheckTags); } + + @Test + public void testPostEntryWithoutId() throws Exception { + roomData.remove("Id"); + + final EntityProviderWriteProperties properties = + EntityProviderWriteProperties.serviceRoot(BASE_URI).ignoreKey(true).build(); + AtomEntityProvider ser = createAtomEntityProvider(); + ODataResponse response = + ser.writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms"), roomData, properties); + + String xmlString = verifyResponse(response); + + assertXpathExists("/a:entry", xmlString); + assertXpathEvaluatesTo(BASE_URI.toASCIIString(), "/a:entry/@xml:base", xmlString); + + assertXpathExists("/a:entry/a:content", xmlString); + assertXpathEvaluatesTo(ContentType.APPLICATION_XML.toString(), "/a:entry/a:content/@type", xmlString); + + assertXpathExists("/a:entry/a:content/m:properties", xmlString); + } }
