From e3121831b426e192890855a2db8671a525a76571 Mon Sep 17 00:00:00 2001 From: Michael Bolz Date: Mon, 13 Oct 2014 14:59:00 +0200 Subject: [PATCH] [OLINGO-423] Added implementation --- .../xml/MetadataDocumentXmlSerializer.java | 47 ++++++-- .../serializer/xml/MetadataDocumentTest.java | 113 +++++++++++++++++- 2 files changed, 145 insertions(+), 15 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 ec3dd5fd1..a9cf2f8b8 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 @@ -21,6 +21,8 @@ package org.apache.olingo.server.core.serializer.xml; import org.apache.olingo.commons.api.edm.*; import org.apache.olingo.server.api.edmx.EdmxReference; import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.api.edmx.EdmxReferenceInclude; +import org.apache.olingo.server.api.edmx.EdmxReferenceIncludeAnnotation; import org.apache.olingo.server.api.serializer.ODataSerializer; import javax.xml.stream.XMLStreamException; @@ -70,6 +72,13 @@ public class MetadataDocumentXmlSerializer { private static final String XML_ALIAS = "Alias"; private static final String XML_NAMESPACE = "Namespace"; private static final String XML_TYPE_DEFINITION = "TypeDefinition"; + private static final String REFERENCE = "Reference"; + private static final String INCLUDE = "Include"; + private static final String INCLUDE_ANNOTATIONS = "IncludeAnnotations"; + private static final String XML_TERM_NAMESPACE = "TermNamespace"; + private static final String XML_TARGET_NAMESPACE = "TargetNamespace"; + private static final String XML_QUALIFIER = "Qualifier"; + private static final String URI = "Uri"; private final ServiceMetadata serviceMetadata; @@ -463,7 +472,7 @@ public class MetadataDocumentXmlSerializer { writer.writeStartElement(XML_KEY); for (EdmKeyPropertyRef keyRef : keyPropertyRefs) { writer.writeEmptyElement(XML_PROPERTY_REF); - String keyName = null; + final String keyName; if (keyRef.getPath() != null) { keyName = keyRef.getPath() + "/" + keyRef.getKeyPropertyName(); } else { @@ -511,15 +520,33 @@ public class MetadataDocumentXmlSerializer { private void appendReference(final XMLStreamWriter writer) throws XMLStreamException { List references = serviceMetadata.getReferences(); for (EdmxReference reference: references) { - writer.writeStartElement(NS_EDMX, "Reference"); - // TODO: Which value can we use here? - // - // is an external site we don't want to query each time an EDM-enabled client is used. - writer.writeAttribute("Uri", reference.getUri().toASCIIString()); - // writer.writeEmptyElement(NS_EDMX, "Include"); - // writer.writeAttribute(XML_NAMESPACE, reference.getIncludeNamespace()); - // writer.writeAttribute(XML_ALIAS, reference.getIncludeAlias()); - // writer.writeEndElement(); + writer.writeStartElement(PREFIX_EDMX, REFERENCE, NS_EDMX); + writer.writeAttribute(URI, reference.getUri().toASCIIString()); + + List includes = reference.getIncludes(); + for (EdmxReferenceInclude include : includes) { + writer.writeStartElement(PREFIX_EDMX, INCLUDE, NS_EDMX); + writer.writeAttribute(XML_NAMESPACE, include.getNamespace()); + if(include.getAlias() != null) { + writer.writeAttribute(XML_ALIAS, include.getAlias()); + } + writer.writeEndElement(); + } + + List includeAnnotations = reference.getIncludeAnnotations(); + for (EdmxReferenceIncludeAnnotation includeAnnotation : includeAnnotations) { + writer.writeStartElement(PREFIX_EDMX, INCLUDE_ANNOTATIONS, NS_EDMX); + writer.writeAttribute(XML_TERM_NAMESPACE, includeAnnotation.getTermNamespace()); + if(includeAnnotation.getQualifier() != null) { + writer.writeAttribute(XML_QUALIFIER, includeAnnotation.getQualifier()); + } + if(includeAnnotation.getTargetNamespace() != null) { + writer.writeAttribute(XML_TARGET_NAMESPACE, includeAnnotation.getTargetNamespace()); + } + writer.writeEndElement(); + } + + writer.writeEndElement(); } } } 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 8fbbf159c..85ac1e88a 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 @@ -24,6 +24,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.InputStream; +import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -59,11 +60,12 @@ import org.apache.olingo.server.api.edm.provider.Schema; import org.apache.olingo.server.api.edm.provider.Singleton; import org.apache.olingo.server.api.edm.provider.TypeDefinition; import org.apache.olingo.server.api.edmx.EdmxReference; +import org.apache.olingo.server.api.edmx.EdmxReferenceInclude; import org.apache.olingo.server.api.serializer.ODataSerializer; -import org.apache.olingo.server.api.serializer.SerializerException; import org.apache.olingo.server.core.ServiceMetadataImpl; -import org.apache.olingo.server.core.edm.provider.EdmProviderImpl; -import org.apache.olingo.server.tecsvc.provider.EdmTechProvider; +import org.apache.olingo.server.core.edmx.EdmxReferenceImpl; +import org.apache.olingo.server.core.edmx.EdmxReferenceIncludeAnnotationImpl; +import org.apache.olingo.server.core.edmx.EdmxReferenceIncludeImpl; import org.junit.Test; public class MetadataDocumentTest { @@ -78,14 +80,96 @@ public class MetadataDocumentTest { } @Test - public void writeMetadataWithLocalTestEdm() throws Exception { + public void writeEdmxWithLocalTestEdm() throws Exception { ODataSerializer serializer = OData.newInstance().createSerializer(ODataFormat.XML); + + List edmxReferences = new ArrayList(); + EdmxReferenceImpl reference = new EdmxReferenceImpl(URI.create("http://example.com")); + edmxReferences.add(reference); + + EdmxReferenceImpl referenceWithInclude = new EdmxReferenceImpl( + URI.create("http://localhost/odata/odata/v4.0/referenceWithInclude")); + EdmxReferenceInclude include = new EdmxReferenceIncludeImpl("Org.OData.Core.V1", "Core"); + referenceWithInclude.addInclude(include); + edmxReferences.add(referenceWithInclude); + + EdmxReferenceImpl referenceWithTwoIncludes = new EdmxReferenceImpl( + URI.create("http://localhost/odata/odata/v4.0/referenceWithTwoIncludes")); + referenceWithTwoIncludes.addInclude(new EdmxReferenceIncludeImpl("Org.OData.Core.2", "Core2")); + referenceWithTwoIncludes.addInclude(new EdmxReferenceIncludeImpl("Org.OData.Core.3", "Core3")); + edmxReferences.add(referenceWithTwoIncludes); + + EdmxReferenceImpl referenceWithIncludeAnnos = new EdmxReferenceImpl( + URI.create("http://localhost/odata/odata/v4.0/referenceWithIncludeAnnos")); + referenceWithIncludeAnnos.addIncludeAnnotation( + new EdmxReferenceIncludeAnnotationImpl("TermNs.2", "Q.2", "TargetNS.2")); + referenceWithIncludeAnnos.addIncludeAnnotation( + new EdmxReferenceIncludeAnnotationImpl("TermNs.3", "Q.3","TargetNS.3")); + edmxReferences.add(referenceWithIncludeAnnos); + + EdmxReferenceImpl referenceWithAll = new EdmxReferenceImpl( + URI.create("http://localhost/odata/odata/v4.0/referenceWithAll")); + referenceWithAll.addInclude(new EdmxReferenceIncludeImpl("ReferenceWithAll.1", "Core1")); + referenceWithAll.addInclude(new EdmxReferenceIncludeImpl("ReferenceWithAll.2", "Core2")); + referenceWithAll.addIncludeAnnotation( + new EdmxReferenceIncludeAnnotationImpl("ReferenceWithAllTermNs.4", "Q.4", "TargetNS.4")); + referenceWithAll.addIncludeAnnotation( + new EdmxReferenceIncludeAnnotationImpl("ReferenceWithAllTermNs.5", "Q.5", "TargetNS.5")); + edmxReferences.add(referenceWithAll); + ServiceMetadata serviceMetadata = new ServiceMetadataImpl(ODataServiceVersion.V40, - new TestMetadataProvider(), Collections.emptyList()); + new TestMetadataProvider(), edmxReferences); InputStream metadata = serializer.metadataDocument(serviceMetadata); assertNotNull(metadata); String metadataString = IOUtils.toString(metadata); + // edmx reference + assertTrue(metadataString.contains( + "")); + assertTrue(metadataString.contains( + "" + + "" + + "")); + assertTrue(metadataString.contains( + "" + + "" + + "" + + "")); + assertTrue(metadataString.contains( + "" + + "" + + "" + + "")); + assertTrue(metadataString.contains( + "" + + "" + + "" + + "" + + "" + + "")); + } + + @Test + public void writeMetadataWithLocalTestEdm() throws Exception { + ODataSerializer serializer = OData.newInstance().createSerializer(ODataFormat.XML); + List edmxReferences = getEdmxReferences(); + ServiceMetadata serviceMetadata = new ServiceMetadataImpl(ODataServiceVersion.V40, + new TestMetadataProvider(), edmxReferences); + InputStream metadata = serializer.metadataDocument(serviceMetadata); + assertNotNull(metadata); + + String metadataString = IOUtils.toString(metadata); + // edmx reference + assertTrue(metadataString + .contains("" + + "" + + "")); + assertTrue(metadataString .contains("")); @@ -147,6 +231,25 @@ public class MetadataDocumentTest { assertTrue(metadataString.contains("")); } + /** + * + * + * + * + * + * + * @return default emdx reference + */ + private List getEdmxReferences() { + List edmxReferences = new ArrayList(); + EdmxReferenceImpl reference = new EdmxReferenceImpl( + URI.create("http://docs.oasis-open.org/odata/odata/v4.0/cs02/vocabularies/Org.OData.Core.V1.xml")); + EdmxReferenceInclude include = new EdmxReferenceIncludeImpl("Org.OData.Core.V1", "Core"); + reference.addInclude(include); + edmxReferences.add(reference); + return edmxReferences; + } + @Test public void writeMetadataWithTechnicalScenario() throws Exception { ODataSerializer serializer = OData.newInstance().createSerializer(ODataFormat.XML);