From 3b9eb2fb8af171246f7b3c4aad5088eb94520bc8 Mon Sep 17 00:00:00 2001 From: mibo Date: Tue, 14 Oct 2014 09:49:20 +0200 Subject: [PATCH] [OLINGO-428] Fixed optional alias attribute for Schema --- .../xml/MetadataDocumentXmlSerializer.java | 14 +++--- .../serializer/xml/MetadataDocumentTest.java | 44 ++++++++++++++++--- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java index a9cf2f8b8..7a9ad7ea4 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java @@ -79,6 +79,8 @@ public class MetadataDocumentXmlSerializer { private static final String XML_TARGET_NAMESPACE = "TargetNamespace"; private static final String XML_QUALIFIER = "Qualifier"; private static final String URI = "Uri"; + private static final String SCHEMA = "Schema"; + private static final String DATA_SERVICES = "DataServices"; private final ServiceMetadata serviceMetadata; @@ -88,8 +90,8 @@ public class MetadataDocumentXmlSerializer { private final static String NS_EDM = "http://docs.oasis-open.org/odata/ns/edm"; - public MetadataDocumentXmlSerializer(final ServiceMetadata edm) { - this.serviceMetadata = edm; + public MetadataDocumentXmlSerializer(final ServiceMetadata serviceMetadata) { + this.serviceMetadata = serviceMetadata; } public void writeMetadataDocument(final XMLStreamWriter writer) throws XMLStreamException { @@ -108,7 +110,7 @@ public class MetadataDocumentXmlSerializer { private void appendDataServices(final XMLStreamWriter writer) throws XMLStreamException { writer.setDefaultNamespace(NS_EDM); - writer.writeStartElement(NS_EDMX, "DataServices"); + writer.writeStartElement(NS_EDMX, DATA_SERVICES); for (EdmSchema schema : serviceMetadata.getEdm().getSchemas()) { appendSchema(writer, schema); } @@ -116,10 +118,12 @@ public class MetadataDocumentXmlSerializer { } private void appendSchema(final XMLStreamWriter writer, final EdmSchema schema) throws XMLStreamException { - writer.writeStartElement(NS_EDM, "Schema"); + writer.writeStartElement(NS_EDM, SCHEMA); writer.writeDefaultNamespace(NS_EDM); writer.writeAttribute(XML_NAMESPACE, schema.getNamespace()); - writer.writeAttribute(XML_ALIAS, schema.getAlias()); + if(schema.getAlias() != null) { + writer.writeAttribute(XML_ALIAS, schema.getAlias()); + } // EnumTypes appendEnumTypes(writer, schema.getEnumTypes()); diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java index 2856cc49a..be1f8083c 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java @@ -18,6 +18,7 @@ */ package org.apache.olingo.server.core.serializer.xml; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -257,14 +258,45 @@ public class MetadataDocumentTest { } /** - * - * - * - * - * + * Write simplest (empty) Schema. * - * @return default emdx reference + * @throws Exception */ + @Test + public void writeMetadataWithEmptySchema() throws Exception { + ODataSerializer serializer = OData.newInstance().createSerializer(ODataFormat.XML); + List edmxReferences = Collections.emptyList(); + ServiceMetadata serviceMetadata = new ServiceMetadataImpl(ODataServiceVersion.V40, + new EdmProvider() { + @Override + public List getSchemas() throws ODataException { + return Arrays.asList(new Schema().setNamespace("MyNamespace")); + } + }, + edmxReferences); + + InputStream metadata = serializer.metadataDocument(serviceMetadata); + assertNotNull(metadata); + + String metadataString = IOUtils.toString(metadata); + assertEquals("" + + "" + + "" + + "" + + "" + + "", + metadataString); + } + + /** + * + * + * + * + * + * + * @return default emdx reference + */ private List getEdmxReferences() { List edmxReferences = new ArrayList(); EdmxReferenceImpl reference = new EdmxReferenceImpl(