Beispiel #1
0
        /// <summary>
        /// Annotates the payload element with atom:updated value.
        /// </summary>
        /// <param name="payloadElement">The payload element to annotate.</param>
        /// <param name="updatedValue">The value of the atom:update element.</param>
        /// <returns>The payload element with the annotation applied.</returns>
        public static T AtomUpdated <T>(this T payloadElement, string updatedValue) where T : ODataPayloadElement
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");

            payloadElement.Annotations.Add(XmlTreeAnnotation.Atom(TestAtomConstants.AtomUpdatedElementName, updatedValue));
            return(payloadElement);
        }
Beispiel #2
0
        /// <summary>
        /// Annotates the resource collection with out-of-line app:categories values.
        /// </summary>
        /// <param name="resourceCollection">The resource collection to annotate.</param>
        /// <param name="uri">The value of the app:categories href property.</param>
        /// <returns>The resource collection with the annotation applied.</returns>
        public static ResourceCollectionInstance AppOutOfLineCategories(this ResourceCollectionInstance resourceCollection, string uri)
        {
            ExceptionUtilities.CheckArgumentNotNull(resourceCollection, "resourceCollection");

            resourceCollection.AddAnnotation(CreateAppElement("categories", null, XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomHRefAttributeName, uri)));
            return(resourceCollection);
        }
Beispiel #3
0
        /// <summary>
        /// Annotates the entry with atom:published values.
        /// </summary>
        /// <param name="entry">The entry to annotate.</param>
        /// <param name="publishedDate">The value of the atom:published element.</param>
        /// <returns>The feed with the annotation applied.</returns>
        public static EntityInstance AtomPublished(this EntityInstance entry, string publishedDate)
        {
            ExceptionUtilities.CheckArgumentNotNull(entry, "entry");

            entry.AddAnnotation(XmlTreeAnnotation.Atom(TestAtomConstants.AtomPublishedElementName, publishedDate));
            return(entry);
        }
Beispiel #4
0
        /// <summary>
        /// Annotates the feed with atom:logo values.
        /// </summary>
        /// <param name="feed">The feed to annotate.</param>
        /// <param name="uri">The value of the atom:logo element.</param>
        /// <returns>The feed with the annotation applied.</returns>
        public static EntitySetInstance AtomLogo(this EntitySetInstance feed, string uri)
        {
            ExceptionUtilities.CheckArgumentNotNull(feed, "feed");

            feed.AddAnnotation(XmlTreeAnnotation.Atom(TestAtomConstants.AtomLogoElementName, uri));
            return(feed);
        }
Beispiel #5
0
        private static AtomGeneratorMetadata CreateGeneratorMetadata(XmlTreeAnnotation epmTree)
        {
            AtomGeneratorMetadata metadata = new AtomGeneratorMetadata()
            {
                Name = epmTree.PropertyValue
            };

            foreach (XmlTreeAnnotation attribute in epmTree.Children.Where(child => child.IsAttribute))
            {
                if (string.IsNullOrEmpty(attribute.NamespaceName))
                {
                    string localName = attribute.LocalName;
                    if (localName == TestAtomConstants.AtomGeneratorUriAttributeName)
                    {
                        metadata.Uri = string.IsNullOrEmpty(attribute.PropertyValue) ? null : new Uri(attribute.PropertyValue);
                    }
                    else if (localName == TestAtomConstants.AtomGeneratorVersionAttributeName)
                    {
                        metadata.Version = attribute.PropertyValue;
                    }
                    else
                    {
                        throw new NotSupportedException("Unsupported metadata '" + localName + "' found for a generator.");
                    }
                }
            }

            return(metadata);
        }
        /// <summary>
        /// Normalizes a workspace or resource collection's Atom title property value so that it matches the element's title annotation.
        /// </summary>
        /// <param name="payloadElement">The payload element to normalize</param>
        /// <param name="titlePropertyValue">The value of the payload element's title property</param>
        /// <param name="setPropertyValue">Delegate for setting the payload element's title property</param>
        private void NormalizeTitleValue(ODataPayloadElement payloadElement, string titlePropertyValue, Action <string> setPropertyValue)
        {
            XmlTreeAnnotation titleXmlTreeAnnotation = payloadElement.Annotations
                                                       .OfType <XmlTreeAnnotation>()
                                                       .SingleOrDefault(a => a.LocalName.Equals(TestAtomConstants.AtomTitleElementName));

            if (titlePropertyValue != null)
            {
                if (titleXmlTreeAnnotation != null)
                {
                    string titleAnnotationValue = titleXmlTreeAnnotation.PropertyValue;
                    if (titleAnnotationValue == null)
                    {
                        titleXmlTreeAnnotation.PropertyValue = titlePropertyValue;
                    }
                    else
                    {
                        ExceptionUtilities.Assert(titlePropertyValue == titleAnnotationValue, "Title in workspace or resource collection has different values : Property=" + titlePropertyValue + ", Annotation=" + titleAnnotationValue);
                    }
                }
                else
                {
                    payloadElement.AtomTitle(titlePropertyValue, TestAtomConstants.AtomTextConstructTextKind);
                }
            }
            else if (titleXmlTreeAnnotation != null)
            {
                setPropertyValue(titleXmlTreeAnnotation.PropertyValue);
            }
        }
        private static void SerializeXmlTree(XmlTreeAnnotation xmlTree, XElement parent)
        {
            ExceptionUtilities.CheckArgumentNotNull(xmlTree, "xmlTree");

            XNamespace targetNamespace = XNamespace.Get(xmlTree.NamespaceName);

            if (xmlTree.IsAttribute)
            {
                CreateAttribute(parent, xmlTree.LocalName, xmlTree.NamespaceName, xmlTree.PropertyValue);
            }
            else
            {
                XElement element = parent.Element(targetNamespace.GetName(xmlTree.LocalName));

                // create new element for repeatable elements
                if (element == null || IsRepeatableElement(element))
                {
                    element = CreateElement(parent, xmlTree.NamespacePrefix, xmlTree.LocalName, xmlTree.NamespaceName);
                }

                foreach (var child in xmlTree.Children)
                {
                    SerializeXmlTree(child, element);
                }

                if (xmlTree.PropertyValue != null)
                {
                    element.Value = xmlTree.PropertyValue;
                }
            }
        }
Beispiel #8
0
        public static T WithDefaultAtomIDAnnotation <T>(this T payloadElement)
            where T : ODataPayloadElement
        {
            XmlTreeAnnotation id = XmlTreeAnnotation.Atom(TestAtomConstants.AtomIdElementName, "urn:" + TestAtomConstants.AtomGuid);

            payloadElement.Annotations.Add(id);
            return(payloadElement);
        }
Beispiel #9
0
        /// <summary>
        /// Annotates an ODataPayloadElement with an Atom Text construct.
        /// </summary>
        /// <typeparam name="T">The type of ODataPayloadElement.</typeparam>
        /// <param name="payloadElement">The payload element to annotate.</param>
        /// <param name="elementName">The name of the Text element.</param>
        /// <param name="textValue">The value of the Text element.</param>
        /// <returns>The payload element with annotation applied.</returns>
        private static T AtomText <T>(this T payloadElement, string elementName, string textValue, string textType) where T : ODataPayloadElement
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");

            var textAttributes = CreateAtomAttributes(new KeyValuePair <string, string>(TestAtomConstants.AtomTypeAttributeName, textType));

            payloadElement.AddAnnotation(XmlTreeAnnotation.Atom(elementName, textValue, textAttributes));
            return(payloadElement);
        }
Beispiel #10
0
        private static T WithDefaultAtomTitleAnnotation <T>(this T payloadElement)
            where T : ODataPayloadElement
        {
            XmlTreeAnnotation title = XmlTreeAnnotation.Atom("title", null);

            title.Children.Add(XmlTreeAnnotation.AtomAttribute("type", "text"));
            payloadElement.Annotations.Add(title);
            return(payloadElement);
        }
Beispiel #11
0
        private static T WithDefaultAtomUpdatedAnnotation <T>(this T payloadElement)
            where T : ODataPayloadElement
        {
            XmlTreeAnnotation updated = XmlTreeAnnotation.Atom(TestAtomConstants.AtomUpdatedElementName,
                                                               "2013-08-13T01:03:16.7800000-7");

            payloadElement.Annotations.Add(updated);
            return(payloadElement);
        }
Beispiel #12
0
        /// <summary>
        /// Converts the ATOM metadata annotation of stream properties and assocation links
        /// into XmlTree annotations and removes the stream properties/association links from the payload.
        /// </summary>
        /// <param name="payloadElement">The entity instance to visit.</param>
        public override void Visit(EntityInstance payloadElement)
        {
            IEnumerable <PropertyInstance> properties = payloadElement.Properties;

            if (properties != null)
            {
                // First remove all the stream properties in request payloads
                if (this.readerTestConfiguration.IsRequest)
                {
                    List <NamedStreamInstance> streamProperties = properties.OfType <NamedStreamInstance>().ToList();
                    foreach (var streamProperty in streamProperties)
                    {
                        // Edit link
                        string            editRelationValue = TestAtomConstants.ODataStreamPropertyEditMediaRelatedLinkRelationPrefix + streamProperty.Name;
                        XmlTreeAnnotation annotation        = GetStreamPropertyLinkXmlTreeAnnotation(streamProperty, editRelationValue);
                        if (annotation != null)
                        {
                            payloadElement.AddAnnotation(annotation);
                        }

                        // Self link
                        string sourceRelationValue = TestAtomConstants.ODataStreamPropertyMediaResourceRelatedLinkRelationPrefix + streamProperty.Name;
                        annotation = GetStreamPropertyLinkXmlTreeAnnotation(streamProperty, sourceRelationValue);
                        if (annotation != null)
                        {
                            payloadElement.AddAnnotation(annotation);
                        }

                        payloadElement.Remove(streamProperty);
                    }
                }

                // Then also convert the association links of navigation properties and
                // remove all navigation properties that do not have a URI - for request
                if (this.readerTestConfiguration.IsRequest)
                {
                    List <NavigationPropertyInstance> navigationProperties = properties.OfType <NavigationPropertyInstance>().ToList();
                    foreach (var navProperty in navigationProperties)
                    {
                        XmlTreeAnnotation annotation = GetAssociationLinkXmlTreeAnnotation(navProperty.AssociationLink);
                        navProperty.AssociationLink = null;

                        if (navProperty.Value == null)
                        {
                            payloadElement.Remove(navProperty);
                        }

                        if (annotation != null)
                        {
                            payloadElement.AddAnnotation(annotation);
                        }
                    }
                }
            }

            base.Visit(payloadElement);
        }
Beispiel #13
0
        /// <summary>
        /// Creates an annotation representing an atom:category element.
        /// </summary>
        /// <param name="term">The value of the atom:category's term property.</param>
        /// <param name="scheme">The value of the atom:category's scheme property.</param>
        /// <param name="label">The value of the atom:category's label property.</param>
        /// <returns>An annotation representing the atom:category.</returns>
        public static XmlTreeAnnotation AtomCategory(string term, string scheme, string label)
        {
            var categoryAttributes = CreateAtomAttributes(
                new KeyValuePair <string, string>(TestAtomConstants.AtomCategoryTermAttributeName, term),
                new KeyValuePair <string, string>(TestAtomConstants.AtomCategorySchemeAttributeName, scheme),
                new KeyValuePair <string, string>(TestAtomConstants.AtomCategoryLabelAttributeName, label));

            return(XmlTreeAnnotation.Atom(TestAtomConstants.AtomCategoryElementName, null, categoryAttributes));
        }
Beispiel #14
0
        /// <summary>
        /// Creates the XmlTreeAnnotation annotations that represent the attributes with non-null values.
        /// </summary>
        /// <param name="attributeNameValuePairs">The attribute name-value pairs to convert to Atom attribute annotations.</param>
        /// <returns>An XmlTreeAnnotation for each non-null value attribute.</returns>
        private static XmlTreeAnnotation[] CreateAtomAttributes(params KeyValuePair <string, string>[] attributeNameValuePairs)
        {
            var atomAttributes = new List <XmlTreeAnnotation>();

            foreach (var attribute in attributeNameValuePairs.Where(p => p.Value != null))
            {
                atomAttributes.Add(XmlTreeAnnotation.AtomAttribute(attribute.Key, attribute.Value));
            }
            return(atomAttributes.ToArray());
        }
Beispiel #15
0
        /// <summary>
        /// Creates the XmlTreeAnnotation annotations that represent the elements with non-null values.
        /// </summary>
        /// <param name="attributeNameValuePairs">The element name-value pairs to convert to Atom element annotations.</param>
        /// <returns>An XmlTreeAnnotation for each non-null value element.</returns>
        private static XmlTreeAnnotation[] CreateAtomElements(params KeyValuePair <string, string>[] elementNameValuePairs)
        {
            var atomElements = new List <XmlTreeAnnotation>();

            foreach (var element in elementNameValuePairs.Where(p => p.Value != null))
            {
                atomElements.Add(XmlTreeAnnotation.Atom(element.Key, element.Value));
            }
            return(atomElements.ToArray());
        }
Beispiel #16
0
        /// <summary>
        /// Annotates the feed with atom:generator values.
        /// </summary>
        /// <param name="feed">The feed to annotate.</param>
        /// <param name="name">The value of the atom:generator element.</param>
        /// <param name="uri">The value of the atom:generator's URI property.</param>
        /// <param name="version">The value of the atom:generator's version property.</param>
        /// <returns>The feed with the annotation applied.</returns>
        public static EntitySetInstance AtomGenerator(this EntitySetInstance feed, string name, string uri, string version)
        {
            ExceptionUtilities.CheckArgumentNotNull(feed, "feed");

            var generatorAttributes = CreateAtomAttributes(
                new KeyValuePair <string, string>(TestAtomConstants.AtomGeneratorUriAttributeName, uri),
                new KeyValuePair <string, string>(TestAtomConstants.AtomGeneratorVersionAttributeName, version));

            feed.AddAnnotation(XmlTreeAnnotation.Atom(TestAtomConstants.AtomGeneratorElementName, name, generatorAttributes));
            return(feed);
        }
Beispiel #17
0
        /// <summary>
        /// Computes the XmlTreeAnnotation for an association link.
        /// </summary>
        /// <param name="associationLink">The association link to compute the stream property for.</param>
        /// <returns>The <see cref="XmlTreeAnnotation"/> for the link specified in <paramref name="associationLink"/>.</returns>
        private XmlTreeAnnotation GetAssociationLinkXmlTreeAnnotation(DeferredLink associationLink)
        {
            if (associationLink == null)
            {
                return(null);
            }

            // Add all the attributes that are already stored on the association link as annotations
            List <XmlTreeAnnotation> attributes = new List <XmlTreeAnnotation>();

            attributes.AddRange(associationLink.Annotations.OfType <XmlTreeAnnotation>().Where(a => a.IsAttribute));
            return(XmlTreeAnnotation.Atom(TestAtomConstants.AtomLinkElementName, null, attributes.ToArray()));
        }
Beispiel #18
0
        /// <summary>
        /// Annotates an ODataPayloadElement with an Atom Person construct.
        /// </summary>
        /// <typeparam name="T">The type of ODataPayloadElement.</typeparam>
        /// <param name="payloadElement">The payload element to annotate.</param>
        /// <param name="elementName">The name of the Person element.</param>
        /// <param name="personName">The value of the Person's Name property.</param>
        /// <param name="personUri">The value of the Person's Uri property.</param>
        /// <param name="personEmail">The value of the Person's Email Address property.</param>
        /// <returns>The payload element with annotation applied.</returns>
        private static T AtomPerson <T>(this T payloadElement, string elementName, string personName, string personUri, string personEmail) where T : ODataPayloadElement
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");

            var personChildren = CreateAtomElements(
                new KeyValuePair <string, string>(TestAtomConstants.AtomPersonNameElementName, personName),
                new KeyValuePair <string, string>(TestAtomConstants.AtomPersonUriElementName, personUri),
                new KeyValuePair <string, string>(TestAtomConstants.AtomPersonEmailElementName, personEmail));

            XmlTreeAnnotation person = XmlTreeAnnotation.Atom(elementName, null, personChildren);

            payloadElement.AddAnnotation(person);
            return(payloadElement);
        }
Beispiel #19
0
        /// <summary>
        /// Annotates the payload element with a link annotation and adds the given atom:link attribute values as children on that annotation.
        /// </summary>
        /// <param name="payloadElement">The payload element to annotate.</param>
        /// <param name="href">The value of the atom:link's href property</param>
        /// <param name="rel">The value of the atom:link's rel property</param>
        /// <param name="type">The value of the atom:link's type property</param>
        /// <param name="hrefLang">The optional value of the atom:link's hrefLang property</param>
        /// <param name="title">The optional value of the atom:link's title property</param>
        /// <param name="length">The optional value of the atom:link's length property</param>
        /// <returns>The payload element with the annotation applied.</returns>
        public static T AtomLink <T>(this T payloadElement, string href, string rel, string type, string hrefLang = null, string title = null, string length = null) where T : ODataPayloadElement
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");

            var linkAttributes = CreateAtomAttributes(
                new KeyValuePair <string, string>(TestAtomConstants.AtomLinkHrefAttributeName, href),
                new KeyValuePair <string, string>(TestAtomConstants.AtomLinkRelationAttributeName, rel),
                new KeyValuePair <string, string>(TestAtomConstants.AtomLinkTypeAttributeName, type),
                new KeyValuePair <string, string>(TestAtomConstants.AtomLinkHrefLangAttributeName, hrefLang),
                new KeyValuePair <string, string>(TestAtomConstants.AtomLinkTitleAttributeName, title),
                new KeyValuePair <string, string>(TestAtomConstants.AtomLinkLengthAttributeName, length));

            payloadElement.AddAnnotation(XmlTreeAnnotation.Atom(TestAtomConstants.AtomLinkElementName, null, linkAttributes));
            return(payloadElement);
        }
Beispiel #20
0
        /// <summary>
        /// Annotates the entry with atom:source values.
        /// </summary>
        /// <param name="entry">The entry to annotate.</param>
        /// <param name="sourceFeed">The feed containing metadata to copy.</param>
        /// <returns>The entry with the annotation applied.</returns>
        public static EntityInstance AtomSource(this EntityInstance entry, EntitySetInstance sourceFeed)
        {
            ExceptionUtilities.CheckArgumentNotNull(entry, "entry");
            ExceptionUtilities.CheckArgumentNotNull(sourceFeed, "sourceFeed");

            var sourceAnnotations = sourceFeed.Annotations.OfType <XmlTreeAnnotation>();

            entry.AddAnnotation(
                XmlTreeAnnotation.Atom(
                    TestAtomConstants.AtomSourceElementName,
                    null,
                    sourceAnnotations.Select(a => (XmlTreeAnnotation)a.Clone()).ToArray()));

            return(entry);
        }
        /// <summary>
        /// Normalizes navigation property specific atom metadata.
        /// </summary>
        /// <param name="payloadElement">The payload element to normalize.</param>
        public override void Visit(NavigationPropertyInstance payloadElement)
        {
            DeferredLink deferredLink = payloadElement.Value as DeferredLink;

            if (deferredLink != null)
            {
                // If there is a type annotation specified as a XmlTreeAnnotion, copy its value over to a ContentTypeAnnotation as well.
                XmlTreeAnnotation typeAnnotation = deferredLink.Annotations.OfType <XmlTreeAnnotation>().SingleOrDefault(a => a.LocalName == TestAtomConstants.AtomLinkTypeAttributeName);
                if (typeAnnotation != null)
                {
                    deferredLink.WithContentType(typeAnnotation.PropertyValue);
                }
            }

            base.Visit(payloadElement);
        }
Beispiel #22
0
        /// <summary>
        /// Computes the XmlTreeAnnotation for a stream property from the <see cref="NamedStreamAtomLinkMetadataAnnotation"/>.
        /// </summary>
        /// <param name="streampProperty">The stream property to compute the Xml annotation for.</param>
        /// <param name="relation">The relation of the link we are converting</param>
        /// <returns>The <see cref="XmlTreeAnnotation"/> for the link with the specified <paramref name="relation"/>.</returns>
        private XmlTreeAnnotation GetStreamPropertyLinkXmlTreeAnnotation(NamedStreamInstance streampProperty, string relation)
        {
            // Look it up again since it was created above.
            NamedStreamAtomLinkMetadataAnnotation linkAnnotation = streampProperty.Annotations.OfType <NamedStreamAtomLinkMetadataAnnotation>().SingleOrDefault(a => a.Relation == relation);

            if (linkAnnotation == null)
            {
                return(null);
            }

            List <XmlTreeAnnotation> attributes = new List <XmlTreeAnnotation>();

            if (linkAnnotation.Href != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkHrefAttributeName, linkAnnotation.Href));
            }

            if (linkAnnotation.HrefLang != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkHrefLangAttributeName, linkAnnotation.HrefLang));
            }

            if (linkAnnotation.Length != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkLengthAttributeName, linkAnnotation.Length));
            }

            if (linkAnnotation.Relation != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkRelationAttributeName, linkAnnotation.Relation));
            }

            if (linkAnnotation.Title != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkTitleAttributeName, linkAnnotation.Title));
            }

            if (linkAnnotation.Type != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkTypeAttributeName, linkAnnotation.Type));
            }

            return(XmlTreeAnnotation.Atom(TestAtomConstants.AtomLinkElementName, null, attributes.ToArray()));
        }
            /// <summary>
            /// Converts the Object Model representation of Atom link metadata into appropriate annotations for a payload element representing a link.
            /// </summary>
            /// <param name="linkMetadata">The Atom link metadata, in Object Model representation, to convert.</param>
            /// <param name="linkPayloadElement">The link payload element to annotate.</param>
            /// <remarks>This method is only for use with payload elements that represent links, as it will skip over the root link annotation.</remarks>
            private static void ConvertAtomLinkChildrenMetadata(AtomLinkMetadata linkMetadata, ODataPayloadElement linkPayloadElement)
            {
                ExceptionUtilities.CheckArgumentNotNull(linkMetadata, "linkMetadata");
                ExceptionUtilities.CheckArgumentNotNull(linkPayloadElement, "linkPayloadElement");

                // Since the payload element already represents a link, we annotate a temporary element
                // and copy the "children" annotations onto the actual payload element.
                var tempPayloadElement = new EntityInstance();

                ExceptionUtilities.Assert(!tempPayloadElement.Annotations.OfType <XmlTreeAnnotation>().Any(), "Payload element should not have XmlTreeAnnotations after construction");
                ConvertAtomLinkMetadata(linkMetadata, tempPayloadElement);

                XmlTreeAnnotation linkAnnotation = tempPayloadElement.Annotations.OfType <XmlTreeAnnotation>().Single();

                foreach (XmlTreeAnnotation childAnnotation in linkAnnotation.Children)
                {
                    linkPayloadElement.AddAnnotation(childAnnotation);
                }
            }
        /// <summary>
        /// Normalizes an Atom link property value between an XmlTreeAnnotation and another property.
        /// </summary>
        /// <typeparam name="T">The type of payload element.</typeparam>
        /// <param name="payloadElement">The payload element to normalize.</param>
        /// <param name="relationValue">The relation value of the link to normalize.</param>
        /// <param name="attributeName">The name of the link attribute to normalize.</param>
        /// <param name="getOtherLinkValue">Function for retrieving the other value for the link property.</param>
        /// <param name="setOtherLinkValue">Delegate for setting the other value for the link property.</param>
        private void NormalizeLinkValue <T>(T payloadElement, string relationValue, string attributeName, Func <T, string> getOtherLinkValue, Action <T, string> setOtherLinkValue) where T : ODataPayloadElement
        {
            string            otherLinkValue        = getOtherLinkValue(payloadElement);
            XmlTreeAnnotation linkXmlTreeAnnotation = payloadElement.Annotations
                                                      .OfType <XmlTreeAnnotation>()
                                                      .SingleOrDefault(a => a.LocalName.Equals(TestAtomConstants.AtomLinkElementName) && HasAttribute(a, TestAtomConstants.AtomLinkRelationAttributeName, relationValue));

            if (otherLinkValue != null)
            {
                if (linkXmlTreeAnnotation != null)
                {
                    var linkAttribute = GetAttributeAnnotation(linkXmlTreeAnnotation, attributeName);
                    if (linkAttribute == null)
                    {
                        linkXmlTreeAnnotation.Children.Add(XmlTreeAnnotation.AtomAttribute(attributeName, otherLinkValue));
                    }
                    else if (linkAttribute.PropertyValue != otherLinkValue)
                    {
                        ExceptionUtilities.Assert(otherLinkValue.Equals(linkAttribute.PropertyValue), attributeName + " value for link '" + relationValue + "' does not match value on Xml Tree");
                    }
                }
                else
                {
                    // Add an xml tree annotation to match the value from the other source.
                    payloadElement.Add(
                        XmlTreeAnnotation.Atom(
                            TestAtomConstants.AtomLinkElementName,
                            null,
                            XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkRelationAttributeName, relationValue),
                            XmlTreeAnnotation.AtomAttribute(attributeName, otherLinkValue)));
                }
            }
            else if (linkXmlTreeAnnotation != null)
            {
                // Set the other source to match the atom:link href value of the annotation
                var linkAttribute = GetAttributeAnnotation(linkXmlTreeAnnotation, attributeName);
                if (linkAttribute != null)
                {
                    setOtherLinkValue(payloadElement, linkAttribute.PropertyValue);
                }
            }
        }
Beispiel #25
0
        /// <summary>
        /// Annotates a payload element representing a link with atom:link attribute values.
        /// </summary>
        /// <param name="linkPayloadElement">The payload element to annotate.</param>
        /// <param name="href">The value of the atom:link's href property</param>
        /// <param name="rel">The value of the atom:link's rel property</param>
        /// <param name="type">The value of the atom:link's type property</param>
        /// <param name="hrefLang">The optional value of the atom:link's hrefLang property</param>
        /// <param name="title">The optional value of the atom:link's title property</param>
        /// <param name="length">The optional value of the atom:link's length property</param>
        /// <returns>The payload element with the annotation applied.</returns>
        public static T AtomLinkAttributes <T>(this T linkPayloadElement, string href, string rel, string type, string hrefLang = null, string title = null, string length = null) where T : ODataPayloadElement
        {
            ExceptionUtilities.CheckArgumentNotNull(linkPayloadElement, "linkPayloadElement");

            // Since the payload element already represents a link, we annotate a temporary element
            // and copy the "children" annotations onto the actual payload element.
            var tempPayloadElement = new EntityInstance();

            ExceptionUtilities.Assert(!tempPayloadElement.Annotations.OfType <XmlTreeAnnotation>().Any(), "Payload element should not have XmlTreeAnnotations after construction");
            tempPayloadElement.AtomLink(href, rel, type, hrefLang, title, length);

            XmlTreeAnnotation linkAnnotation = tempPayloadElement.Annotations.OfType <XmlTreeAnnotation>().Single();

            foreach (XmlTreeAnnotation childAnnotation in linkAnnotation.Children)
            {
                linkPayloadElement.AddAnnotation(childAnnotation);
            }

            return(linkPayloadElement);
        }
        private bool CompareAnnotation(ODataPayloadElementAnnotation expectedAnnotation, ODataPayloadElementAnnotation observedAnnotation, out string errorMessage)
        {
            try
            {
                errorMessage = null;
                this.Assert.AreEqual(expectedAnnotation.GetType(), observedAnnotation.GetType(), "Annotation types should be same");
                if (expectedAnnotation is XmlTreeAnnotation)
                {
                    XmlTreeAnnotation e = expectedAnnotation as XmlTreeAnnotation;
                    XmlTreeAnnotation o = observedAnnotation as XmlTreeAnnotation;
                    this.Assert.IsNotNull(o, "Observed annotation cannot be null");
                    this.Assert.AreEqual(e.LocalName, o.LocalName, "Local names should match");
                    this.Assert.AreEqual(e.NamespaceName, o.NamespaceName, "Namespace names should be equal");
                    this.Assert.AreEqual(e.PropertyValue, o.PropertyValue, "Property values must be equal");
                    this.Assert.AreEqual(e.IsAttribute, o.IsAttribute, "IsAttribute values should be equal");
                    this.Assert.AreEqual(e.Children.Count, o.Children.Count, "Children count must be equal");
                    Compare(e.Children.Cast <ODataPayloadElementAnnotation>().ToList(), o.Children.Cast <ODataPayloadElementAnnotation>().ToList());
                }
                else if (expectedAnnotation is SelfLinkAnnotation)
                {
                    SelfLinkAnnotation e = expectedAnnotation as SelfLinkAnnotation;
                    SelfLinkAnnotation o = observedAnnotation as SelfLinkAnnotation;
                    this.Assert.IsNotNull(o, "Observed annotation cannot be null");
                    this.Assert.AreEqual(e.Value, o.Value, "Values should match");
                }
                else if (expectedAnnotation is ContentTypeAnnotation)
                {
                    ContentTypeAnnotation e = expectedAnnotation as ContentTypeAnnotation;
                    ContentTypeAnnotation o = observedAnnotation as ContentTypeAnnotation;
                    this.Assert.IsNotNull(o, "Observed annotation cannot be null");
                    this.Assert.AreEqual(e.Value, o.Value, "Values should match");
                }

                return(true);
            }
            catch (DataComparisonException e)
            {
                errorMessage = "Message: " + e.Message + " Exception type:" + e.GetType() + " Stack:" + e.StackTrace;
                return(false);
            }
        }
        /// <summary>
        /// Builds the entity-property-mapping tree represented by the given xml element
        /// </summary>
        /// <param name="element">The element to build from</param>
        /// <returns>The entity-property-mapping tree</returns>
        private XmlTreeAnnotation BuildEpmTree(XElement element)
        {
            var mappedProperty = new XmlTreeAnnotation()
            {
                IsAttribute     = false,
                LocalName       = element.Name.LocalName,
                NamespaceName   = element.Name.NamespaceName,
                NamespacePrefix = element.GetPrefixOfNamespace(element.Name.Namespace),
                PropertyValue   = element.Nodes().OfType <XText>().Select(t => t.Value).SingleOrDefault(),
            };

            foreach (var subElement in element.Elements())
            {
                mappedProperty.Children.Add(this.BuildEpmTree(subElement));
            }

            foreach (var attribute in element.Attributes().Where(a => !a.IsNamespaceDeclaration))
            {
                mappedProperty.Children.Add(this.BuildEpmTree(attribute));
            }

            return(mappedProperty);
        }
Beispiel #28
0
        /// <summary>
        /// Visits the payload element
        /// </summary>
        /// <param name="payloadElement">The payload element to visit</param>
        public override void Visit(EntitySetInstance payloadElement)
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");
            XmlTreeAnnotation updated = XmlTreeAnnotation.Atom(TestAtomConstants.AtomUpdatedElementName, "2013-08-13T01:03:16.7800000");

            updated.SetValueEqualityFunc((obj1, obj2) =>
            {
                return(true);
            });
            payloadElement.Annotations.Add(updated);
            payloadElement.Add(XmlTreeAnnotation.Atom("title", null));

            //If a feed is empty it must have an author
            bool feedEmpty = payloadElement.Count == 0;

            if (feedEmpty)
            {
                XmlTreeAnnotation author = XmlTreeAnnotation.Atom(TestAtomConstants.AtomAuthorElementName,
                                                                  null, XmlTreeAnnotation.Atom(TestAtomConstants.AtomAuthorNameElementName, null));
                payloadElement.Annotations.Add(author);
            }

            base.Visit(payloadElement);
        }
 /// <summary>
 /// Searches the tree annotation for an attribute of specific name and value.
 /// </summary>
 /// <param name="annotation">The tree annotation to search.</param>
 /// <param name="attributeName">The name of attribute to find.</param>
 /// <param name="attributeValue">The value of attribute to find.</param>
 /// <returns>true if the specified attribute name/value was found, false otherwise.</returns>
 private static bool HasAttribute(XmlTreeAnnotation annotation, string attributeName, string attributeValue)
 {
     return(annotation.Children.Any(a => a.IsAttribute && a.LocalName == attributeName && a.PropertyValue == attributeValue));
 }
 /// <summary>
 /// Retrieves the attribute annotation that is an immediate child of the tree annotation.
 /// </summary>
 /// <param name="annotation">The tree annotation to search.</param>
 /// <param name="attributeName">The name of the attribute to retrieve.</param>
 /// <returns>The specified attribute annotation, or null if no matching attributes found.</returns>
 private static XmlTreeAnnotation GetAttributeAnnotation(XmlTreeAnnotation annotation, string attributeName)
 {
     return(annotation.Children.SingleOrDefault(a => a.IsAttribute && a.LocalName.Equals(attributeName)));
 }