public void WriteObject_ThrowsArgumentNull_WriteContext()
 {
     ODataEntityReferenceLinkSerializer serializer = new ODataEntityReferenceLinkSerializer();
     Assert.ThrowsArgumentNull(
         () => serializer.WriteObject(graph: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: null),
         "writeContext");
 }
 public void WriteObject_ThrowsArgumentNull_MessageWriter()
 {
     ODataEntityReferenceLinkSerializer serializer = new ODataEntityReferenceLinkSerializer();
     Assert.ThrowsArgumentNull(
         () => serializer.WriteObject(graph: null, messageWriter: null, writeContext: new ODataSerializerContext()),
         "messageWriter");
 }
        public void WriteObject_ThrowsArgumentNull_WriteContext()
        {
            ODataEntityReferenceLinkSerializer serializer = new ODataEntityReferenceLinkSerializer();

            Assert.ThrowsArgumentNull(
                () => serializer.WriteObject(graph: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: null),
                "writeContext");
        }
        public void WriteObject_ThrowsArgumentNull_MessageWriter()
        {
            ODataEntityReferenceLinkSerializer serializer = new ODataEntityReferenceLinkSerializer();

            Assert.ThrowsArgumentNull(
                () => serializer.WriteObject(graph: null, messageWriter: null, writeContext: new ODataSerializerContext()),
                "messageWriter");
        }
        public void WriteObject_Throws_EntitySetMissingDuringSerialization()
        {
            ODataEntityReferenceLinkSerializer serializer   = new ODataEntityReferenceLinkSerializer();
            ODataSerializerContext             writeContext = new ODataSerializerContext();

            Assert.Throws <SerializationException>(
                () => serializer.WriteObject(graph: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: writeContext),
                "The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.");
        }
        public void WriteObject_Throws_NavigationPropertyMissingDuringSerialization()
        {
            ODataEntityReferenceLinkSerializer serializer = new ODataEntityReferenceLinkSerializer();
            ODataSerializerContext writeContext = new ODataSerializerContext { EntitySet = _customerSet, Path = new ODataPath() };

            Assert.Throws<SerializationException>(
                () => serializer.WriteObject(graph: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: writeContext),
                "The related navigation property could not be found from the OData path. The related navigation property is required to serialize the payload.");
        }
        public void WriteObject_Throws_ODataPathMissing()
        {
            ODataEntityReferenceLinkSerializer serializer = new ODataEntityReferenceLinkSerializer();
            ODataSerializerContext writeContext = new ODataSerializerContext { EntitySet = _customerSet };

            Assert.Throws<SerializationException>(
                () => serializer.WriteObject(graph: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: writeContext),
                "The operation cannot be completed because no ODataPath is available for the request.");
        }
        public void WriteObject_Throws_EntitySetMissingDuringSerialization()
        {
            ODataEntityReferenceLinkSerializer serializer = new ODataEntityReferenceLinkSerializer();
            ODataSerializerContext writeContext = new ODataSerializerContext();

            Assert.Throws<SerializationException>(
                () => serializer.WriteObject(graph: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: writeContext),
                "The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.");
        }
        public void WriteObject_Throws_NavigationPropertyMissingDuringSerialization()
        {
            ODataEntityReferenceLinkSerializer serializer   = new ODataEntityReferenceLinkSerializer();
            ODataSerializerContext             writeContext = new ODataSerializerContext {
                EntitySet = _customerSet, Path = new ODataPath()
            };

            Assert.Throws <SerializationException>(
                () => serializer.WriteObject(graph: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: writeContext),
                "The related navigation property could not be found from the OData path. The related navigation property is required to serialize the payload.");
        }
        public void WriteObject_Throws_ODataPathMissing()
        {
            ODataEntityReferenceLinkSerializer serializer   = new ODataEntityReferenceLinkSerializer();
            ODataSerializerContext             writeContext = new ODataSerializerContext {
                EntitySet = _customerSet
            };

            Assert.Throws <SerializationException>(
                () => serializer.WriteObject(graph: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: writeContext),
                "The operation cannot be completed because no ODataPath is available for the request.");
        }
        public void WriteObject_Throws_ObjectCannotBeWritten_IfGraphIsNotUri()
        {
            IEdmNavigationProperty navigationProperty = _customerSet.ElementType.NavigationProperties().First();
            ODataEntityReferenceLinkSerializer serializer = new ODataEntityReferenceLinkSerializer();
            ODataPath path = new ODataPath(new NavigationPathSegment(navigationProperty));
            ODataSerializerContext writeContext = new ODataSerializerContext { EntitySet = _customerSet, Path = path };

            Assert.Throws<SerializationException>(
                () => serializer.WriteObject(graph: "not uri", messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: writeContext),
                "ODataEntityReferenceLinkSerializer cannot write an object of type 'System.String'.");
        }
        public void WriteObject_Throws_ObjectCannotBeWritten_IfGraphIsNotUri()
        {
            IEdmNavigationProperty             navigationProperty = _customerSet.ElementType.NavigationProperties().First();
            ODataEntityReferenceLinkSerializer serializer         = new ODataEntityReferenceLinkSerializer();
            ODataPath path = new ODataPath(new NavigationPathSegment(navigationProperty));
            ODataSerializerContext writeContext = new ODataSerializerContext {
                EntitySet = _customerSet, Path = path
            };

            Assert.Throws <SerializationException>(
                () => serializer.WriteObject(graph: "not uri", messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: writeContext),
                "ODataEntityReferenceLinkSerializer cannot write an object of type 'System.String'.");
        }
        public void ODataEntityReferenceLinkSerializer_Serializes_Uri()
        {
            // Arrange
            ODataEntityReferenceLinkSerializer serializer         = new ODataEntityReferenceLinkSerializer();
            IEdmNavigationProperty             navigationProperty = _customerSet.ElementType.NavigationProperties().First();
            ODataPath path = new ODataPath(new NavigationPathSegment(navigationProperty));
            ODataSerializerContext writeContext = new ODataSerializerContext {
                EntitySet = _customerSet, Path = path
            };
            MemoryStream          stream  = new MemoryStream();
            IODataResponseMessage message = new ODataMessageWrapper(stream);

            // Act
            serializer.WriteObject(new Uri("http://sampleuri/"), new ODataMessageWriter(message), writeContext);

            // Assert
            stream.Seek(0, SeekOrigin.Begin);
            XElement element = XElement.Load(stream);

            Assert.Equal("http://sampleuri/", element.Value);
        }
        public void ODataEntityReferenceLinkSerializer_Serializes_Uri(object link)
        {
            // Arrange
            ODataEntityReferenceLinkSerializer serializer = new ODataEntityReferenceLinkSerializer();
            IEdmNavigationProperty navigationProperty = _customerSet.ElementType.NavigationProperties().First();
            ODataPath path = new ODataPath(new NavigationPathSegment(navigationProperty));
            ODataSerializerContext writeContext = new ODataSerializerContext { EntitySet = _customerSet, Path = path };
            MemoryStream stream = new MemoryStream();
            IODataResponseMessage message = new ODataMessageWrapper(stream);

            // Act
            serializer.WriteObject(link, typeof(ODataEntityReferenceLink), new ODataMessageWriter(message), writeContext);

            // Assert
            stream.Seek(0, SeekOrigin.Begin);
            XElement element = XElement.Load(stream);
            Assert.Equal("http://sampleuri/", element.Value);
        }