public void InstanceAnnotationWriteTrackerTracksWrites()
 {
     var tracker = new InstanceAnnotationWriteTracker();
     tracker.IsAnnotationWritten("key").Should().BeFalse();
     tracker.MarkAnnotationWritten("key");
     tracker.IsAnnotationWritten("key").Should().BeTrue();
 }
        public void InstanceAnnotationWriteTrackerIsCaseSensitive()
        {
            var tracker = new InstanceAnnotationWriteTracker();
            string key = "key";

            tracker.MarkAnnotationWritten(key.ToUpper());
            tracker.MarkAnnotationWritten(key.ToLower());

            tracker.IsAnnotationWritten(key.ToLower()).Should().BeTrue();
            tracker.IsAnnotationWritten(key.ToUpper()).Should().BeTrue();
        }
        public void Init()
        {
            model = new EdmModel();
            EdmComplexType complexType = new EdmComplexType("ns", "complex");
            complexType.AddProperty(new EdmStructuralProperty(complexType, "StringProperty", EdmCoreModel.Instance.GetString(isNullable: false)));
            model.AddElement(complexType);

            this.stream = new MemoryStream();
            this.settings = new ODataMessageWriterSettings { Version = ODataVersion.V4, ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*") };
            this.settings.SetServiceDocumentUri(ServiceDocumentUri);
            this.serializer = new ODataAtomPropertyAndValueSerializer(this.CreateAtomOutputContext(model, this.stream));
            this.instanceAnnotationWriteTracker = new InstanceAnnotationWriteTracker();
        }
        /// <summary>
        /// Writes all the instance annotations specified in <paramref name="instanceAnnotations"/>.
        /// </summary>
        /// <param name="instanceAnnotations">Collection of instance annotations to write.</param>
        /// <param name="tracker">The tracker to track if instance annotations are written.</param>
        /// <param name="ignoreFilter">Whether to ignore the filter in settings.</param>
        /// <param name="propertyName">The name of the property this instance annotation applies to</param>
        internal void WriteInstanceAnnotations(IEnumerable<ODataInstanceAnnotation> instanceAnnotations, InstanceAnnotationWriteTracker tracker, bool ignoreFilter = false, string propertyName = null)
        {
            Debug.Assert(instanceAnnotations != null, "instanceAnnotations should not be null if we called this");
            Debug.Assert(tracker != null, "tracker should not be null if we called this");

            HashSet<string> instanceAnnotationNames = new HashSet<string>(StringComparer.Ordinal);
            foreach (var annotation in instanceAnnotations)
            {
                if (!instanceAnnotationNames.Add(annotation.Name))
                {
                    throw new ODataException(ODataErrorStrings.JsonLightInstanceAnnotationWriter_DuplicateAnnotationNameInCollection(annotation.Name));
                }

                if (!tracker.IsAnnotationWritten(annotation.Name))
                {
                    this.WriteInstanceAnnotation(annotation, ignoreFilter, propertyName);
                    tracker.MarkAnnotationWritten(annotation.Name);
                }
            }
        }
        public void WriteInstanceAnnotationsWithTrackerShouldNotThrowOnNamesWithDifferentCasing()
        {
            var annotations = new List<ODataInstanceAnnotation>();
            annotations.Add(new ODataInstanceAnnotation("term.one", new ODataPrimitiveValue(123)));
            var verifierCalls = 0;

            this.jsonWriter.WriteNameVerifier = (name) => verifierCalls++;
            this.valueWriter.WritePrimitiveVerifier = (value, reference) => verifierCalls++;

            InstanceAnnotationWriteTracker tracker = new InstanceAnnotationWriteTracker();
            this.jsonLightInstanceAnnotationWriter.WriteInstanceAnnotations(annotations, tracker);
            verifierCalls.Should().Be(2);
            tracker.IsAnnotationWritten("term.one").Should().BeTrue();

            annotations.Add(new ODataInstanceAnnotation("term.One", new ODataPrimitiveValue(456)));
            this.jsonLightInstanceAnnotationWriter.WriteInstanceAnnotations(annotations, tracker);
            verifierCalls.Should().Be(4);
            tracker.IsAnnotationWritten("term.one").Should().BeTrue();
            tracker.IsAnnotationWritten("term.One").Should().BeTrue();
        }
        public void WriteInstanceAnnotationCollection_NewAnnotationsGetWritten()
        {
            var annotations = new List<ODataInstanceAnnotation>();
            annotations.Add(new ODataInstanceAnnotation("term.one", new ODataPrimitiveValue(123)));
            var verifierCalls = 0;

            this.jsonWriter.WriteNameVerifier = (name) => verifierCalls++;
            this.valueWriter.WritePrimitiveVerifier = (value, reference) => verifierCalls++;
            InstanceAnnotationWriteTracker tracker = new InstanceAnnotationWriteTracker();

            this.jsonLightInstanceAnnotationWriter.WriteInstanceAnnotations(annotations, tracker);
            verifierCalls.Should().Be(2);

            annotations.Add(new ODataInstanceAnnotation("term.two", new ODataPrimitiveValue("456")));
            tracker.IsAnnotationWritten("term.two").Should().BeFalse();

            this.jsonLightInstanceAnnotationWriter.WriteInstanceAnnotations(annotations, tracker);
            verifierCalls.Should().Be(4);

            tracker.IsAnnotationWritten("term.two").Should().BeTrue();
        }
        public void WriteInstanceAnnotationsWithTrackerShouldThrowOnDuplicatedAnnotationNames()
        {
            var annotations = new List<ODataInstanceAnnotation>();
            annotations.Add(new ODataInstanceAnnotation("term.one", new ODataPrimitiveValue(123)));
            var verifierCalls = 0;

            this.jsonWriter.WriteNameVerifier = (name) => verifierCalls++;
            this.valueWriter.WritePrimitiveVerifier = (value, reference) => verifierCalls++;

            InstanceAnnotationWriteTracker tracker = new InstanceAnnotationWriteTracker();
            this.jsonLightInstanceAnnotationWriter.WriteInstanceAnnotations(annotations, tracker);
            verifierCalls.Should().Be(2);
            tracker.IsAnnotationWritten("term.one").Should().BeTrue();

            annotations.Add(new ODataInstanceAnnotation("term.one", new ODataPrimitiveValue(456)));
            Action test = () => this.jsonLightInstanceAnnotationWriter.WriteInstanceAnnotations(annotations, tracker);
            test.ShouldThrow<ODataException>().WithMessage(Strings.JsonLightInstanceAnnotationWriter_DuplicateAnnotationNameInCollection("term.one"));
        }
        public void WriteInstanceAnnotationsShouldNotWriteAnnotationsThatHasAlreadyBeenWriten()
        {
            var primitive = AtomInstanceAnnotation.CreateFrom(new ODataInstanceAnnotation("ns.primitive", new ODataPrimitiveValue(123)), /*target*/ null);
            var complex = AtomInstanceAnnotation.CreateFrom(new ODataInstanceAnnotation("ns.complex", new ODataComplexValue { Properties = new[] { new ODataProperty { Name = "p1", Value = 123 } }, TypeName = "ns.complex1" }), /*target*/ null);
            var collection = AtomInstanceAnnotation.CreateFrom(new ODataInstanceAnnotation("ns.collection", new ODataCollectionValue { Items = new[] { 123 } }), /*target*/ null);

            var instanceAnnotationWriteTracker = new InstanceAnnotationWriteTracker();
            instanceAnnotationWriteTracker.MarkAnnotationWritten("ns.primitive");
            instanceAnnotationWriteTracker.MarkAnnotationWritten("ns.collection");
            this.serializer.WriteInstanceAnnotations(new[] { primitive, complex, collection }, instanceAnnotationWriteTracker);
            this.ValidatePayload("<annotation term=\"ns.complex\" m:type=\"#ns.complex1\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\" xmlns=\"http://docs.oasis-open.org/odata/ns/metadata\"><d:p1 m:type=\"Int32\" xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\">123</d:p1></annotation>");

            instanceAnnotationWriteTracker.IsAnnotationWritten("ns.primitive").Should().BeTrue();
            instanceAnnotationWriteTracker.IsAnnotationWritten("ns.complex").Should().BeTrue();
            instanceAnnotationWriteTracker.IsAnnotationWritten("ns.collection").Should().BeTrue();
        }
 public void WriteInstanceAnnotationsShouldTrackWhichAnnotationHasBeenWriten()
 {
     var primitive = AtomInstanceAnnotation.CreateFrom(new ODataInstanceAnnotation("ns.primitive", new ODataPrimitiveValue(123)), /*target*/ null);
     var instanceAnnotationWriteTracker = new InstanceAnnotationWriteTracker();
     this.serializer.WriteInstanceAnnotations(new[] { primitive }, instanceAnnotationWriteTracker);
     instanceAnnotationWriteTracker.IsAnnotationWritten("ns.primitive").Should().BeTrue();
 }
        public void WriteInstanceAnnotationsShouldThrowWhenThereAreDuplicatedTermNames()
        {
            var primitive = AtomInstanceAnnotation.CreateFrom(new ODataInstanceAnnotation("ns.term", new ODataPrimitiveValue(123)), /*target*/ null);
            var complex = AtomInstanceAnnotation.CreateFrom(new ODataInstanceAnnotation("ns.term", new ODataComplexValue { Properties = new[] { new ODataProperty { Name = "p1", Value = 123 } }, TypeName = "ns.complex1" }), /*target*/ null);

            var instanceAnnotationWriteTracker = new InstanceAnnotationWriteTracker();
            instanceAnnotationWriteTracker.MarkAnnotationWritten("ns.term");
            Action test = () => this.serializer.WriteInstanceAnnotations(new[] { primitive, complex }, instanceAnnotationWriteTracker);
            test.ShouldThrow<ODataException>().WithMessage(Strings.JsonLightInstanceAnnotationWriter_DuplicateAnnotationNameInCollection("ns.term"));
        }
        public void WriteInstanceAnnotationsShouldWriteAllAnnotationsInTheEnumerable()
        {
            var primitive = AtomInstanceAnnotation.CreateFrom(new ODataInstanceAnnotation("ns.primitive", new ODataPrimitiveValue(123)), /*target*/ null);
            var complex = AtomInstanceAnnotation.CreateFrom(new ODataInstanceAnnotation("ns.complex", new ODataComplexValue { Properties = new[] { new ODataProperty { Name = "p1", Value = 123 } }, TypeName = "ns.complex1" }), /*target*/ null);
            var collection = AtomInstanceAnnotation.CreateFrom(new ODataInstanceAnnotation("ns.collection", new ODataCollectionValue { Items = new[] { 123 } }), /*target*/ null);

            var instanceAnnotationWriteTracker = new InstanceAnnotationWriteTracker();
            this.serializer.XmlWriter.WriteStartElement("wrapper");
            this.serializer.WriteInstanceAnnotations(new[] {primitive, complex, collection}, instanceAnnotationWriteTracker);
            this.serializer.XmlWriter.WriteEndElement();
            this.ValidatePayload(
                "<wrapper>" +
                    "<annotation term=\"ns.primitive\" int=\"123\" xmlns=\"http://docs.oasis-open.org/odata/ns/metadata\" />" +
                    "<annotation term=\"ns.complex\" m:type=\"#ns.complex1\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\" xmlns=\"http://docs.oasis-open.org/odata/ns/metadata\"><d:p1 m:type=\"Int32\" xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\">123</d:p1></annotation>" +
                    "<annotation term=\"ns.collection\" xmlns=\"http://docs.oasis-open.org/odata/ns/metadata\"><m:element m:type=\"Int32\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\">123</m:element></annotation>" +
                "</wrapper>");
            instanceAnnotationWriteTracker.IsAnnotationWritten("ns.primitive").Should().BeTrue();
            instanceAnnotationWriteTracker.IsAnnotationWritten("ns.complex").Should().BeTrue();
            instanceAnnotationWriteTracker.IsAnnotationWritten("ns.collection").Should().BeTrue();
        }
예제 #12
0
        /// <summary>
        /// Writes all the instance annotations specified in <paramref name="instanceAnnotations"/>.
        /// </summary>
        /// <param name="instanceAnnotations">Collection of instance annotations to write.</param>
        /// <param name="tracker">The tracker to track if instance annotations are written.</param>
        /// <param name="ignoreFilter">Whether to ingore the filter in settings.</param>
        /// <param name="propertyName">The name of the property this instance annotation applies to</param>
        internal void WriteInstanceAnnotations(IEnumerable <ODataInstanceAnnotation> instanceAnnotations, InstanceAnnotationWriteTracker tracker, bool ignoreFilter = false, string propertyName = null)
        {
            Debug.Assert(instanceAnnotations != null, "instanceAnnotations should not be null if we called this");
            Debug.Assert(tracker != null, "tracker should not be null if we called this");

            HashSet <string> instanceAnnotationNames = new HashSet <string>(StringComparer.Ordinal);

            foreach (var annotation in instanceAnnotations)
            {
                if (!instanceAnnotationNames.Add(annotation.Name))
                {
                    throw new ODataException(ODataErrorStrings.JsonLightInstanceAnnotationWriter_DuplicateAnnotationNameInCollection(annotation.Name));
                }

                if (!tracker.IsAnnotationWritten(annotation.Name))
                {
                    this.WriteInstanceAnnotation(annotation, ignoreFilter, propertyName);
                    tracker.MarkAnnotationWritten(annotation.Name);
                }
            }
        }