public void Constructor_ThrowsArgumentNull_edmPrimitiveType()
 {
     Assert.ThrowsArgumentNull(() =>
     {
         var serializer = new ODataPrimitiveSerializer(edmPrimitiveType: null);
     }, "edmType");
 }
        public void CreateProperty()
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
            var serializer = new ODataPrimitiveSerializer(edmPrimitiveType);

            var odataProperty = serializer.CreateProperty(20, "elementName", writeContext: null);
            Assert.NotNull(odataProperty);
            Assert.Equal(odataProperty.Name, "elementName");
            Assert.Equal(odataProperty.Value, 20);
        }
        public void Constructor_SucceedsForValidPrimitiveType(EdmPrimitiveTypeKind primitiveTypeKind)
        {
            IEdmPrimitiveType edmPrimitiveType = EdmCoreModel.Instance.SchemaElements
                                                                .OfType<IEdmPrimitiveType>()
                                                                .Where(primitiveType => primitiveType.PrimitiveKind == primitiveTypeKind)
                                                                .FirstOrDefault();
            IEdmPrimitiveTypeReference edmPrimitiveTypeReference = new EdmPrimitiveTypeReference(edmPrimitiveType, false);

            var serializer = new ODataPrimitiveSerializer(edmPrimitiveTypeReference);

            Assert.Equal(serializer.EdmType, edmPrimitiveTypeReference);
            Assert.Equal(serializer.ODataPayloadKind, ODataPayloadKind.Property);
        }
        public void Read_Primitive(object obj)
        {
            // Arrange
            IEdmPrimitiveTypeReference primitive = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));

            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer(primitive);
            ODataPrimitiveDeserializer deserializer = new ODataPrimitiveDeserializer(primitive);

            MemoryStream stream = new MemoryStream();
            ODataMessageWrapper message = new ODataMessageWrapper(stream);

            serializer.WriteObject(
                obj,
                new ODataMessageWriter(message as IODataResponseMessage, new ODataMessageWriterSettings(), EdmCoreModel.Instance),
                new ODataSerializerContext { RootElementName = "Property" });
            stream.Seek(0, SeekOrigin.Begin);
            Assert.Equal(
                obj,
                deserializer.Read(
                new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), EdmCoreModel.Instance),
                new ODataDeserializerContext()));
        }
예제 #5
0
 public void ConvertUnsupportedPrimitives_NonStandardEdmPrimitives(object graph, object result)
 {
     Assert.Equal(
         result,
         ODataPrimitiveSerializer.ConvertUnsupportedPrimitives(graph));
 }
예제 #6
0
 public void ConvertUnsupportedPrimitives_DoesntChangeStandardEdmPrimitives(object graph)
 {
     Assert.Equal(
         graph,
         ODataPrimitiveSerializer.ConvertUnsupportedPrimitives(graph));
 }
        public void WriteObject_EdmPrimitives(object graph)
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
            var serializer = new ODataPrimitiveSerializer(edmPrimitiveType);

            ODataMessageWriter writer = new ODataMessageWriter(new ODataMessageWrapper(new MemoryStream()) as IODataResponseMessage);

            Assert.DoesNotThrow(() => serializer.WriteObject(graph, writer, new ODataSerializerContext() { ServiceOperationName = "PropertyName" }));
        }
        public void CreateProperty_ReturnsODataProperty_ForNullValue()
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string));
            var serializer = new ODataPrimitiveSerializer(edmPrimitiveType);
            var property = serializer.CreateProperty(null, "elementName", new ODataSerializerContext());

            Assert.NotNull(property);
            Assert.Null(property.Value);
        }
        public void WriteObject_Throws_RootElementNameMissing()
        {
            ODataSerializerContext writeContext = new ODataSerializerContext();
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();

            Assert.Throws<ArgumentException>(
                () => serializer.WriteObject(42, typeof(int), ODataTestUtil.GetMockODataMessageWriter(), writeContext),
                "The 'RootElementName' property is required on 'ODataSerializerContext'.\r\nParameter name: writeContext");
        }
 public void Property_ODataPayloadKind()
 {
     var serializer = new ODataPrimitiveSerializer();
     Assert.Equal(serializer.ODataPayloadKind, ODataPayloadKind.Property);
 }
        public void WriteObject_EdmPrimitives(object graph)
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
            var serializer = new ODataPrimitiveSerializer();
            ODataSerializerContext writecontext = new ODataSerializerContext() { RootElementName = "PropertyName", Model = EdmCoreModel.Instance };

            ODataMessageWriter writer = new ODataMessageWriter(new ODataMessageWrapper(new MemoryStream()) as IODataResponseMessage);

            Assert.DoesNotThrow(() => serializer.WriteObject(graph, typeof(int), writer, writecontext));
        }
        public void CreateODataValue_ReturnsODataNullValue_ForNullValue()
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string));
            var serializer = new ODataPrimitiveSerializer();
            var odataValue = serializer.CreateODataValue(null, edmPrimitiveType, new ODataSerializerContext());

            Assert.IsType<ODataNullValue>(odataValue);
        }
        public void CreateODataValue_PrimitiveValue()
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
            var serializer = new ODataPrimitiveSerializer();

            var odataValue = serializer.CreateODataValue(20, edmPrimitiveType, writeContext: null);
            Assert.NotNull(odataValue);
            ODataPrimitiveValue primitiveValue = Assert.IsType<ODataPrimitiveValue>(odataValue);
            Assert.Equal(primitiveValue.Value, 20);
        }
        public void Property_ODataPayloadKind()
        {
            var serializer = new ODataPrimitiveSerializer();

            Assert.Equal(serializer.ODataPayloadKind, ODataPayloadKind.Property);
        }
        public void WriteObject_Throws_RootElementNameMissing()
        {
            ODataSerializerContext writeContext = new ODataSerializerContext();
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer(EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: true));

            Assert.Throws<ArgumentException>(
                () => serializer.WriteObject(42, ODataTestUtil.GetMockODataMessageWriter(), writeContext),
                "The 'RootElementName' property is required on 'ODataSerializerContext'.\r\nParameter name: writeContext");
        }
        public void Read_MappedPrimitive(object obj, object expected)
        {
            // Arrange
            IEdmPrimitiveTypeReference primitive = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
            IEdmModel model = EdmCoreModel.Instance;
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            ODataPrimitiveDeserializer deserializer = new ODataPrimitiveDeserializer();

            MemoryStream stream = new MemoryStream();
            ODataMessageWrapper message = new ODataMessageWrapper(stream);
            ODataMessageWriter messageWriter = new ODataMessageWriter(message as IODataResponseMessage, new ODataMessageWriterSettings(), model);
            ODataMessageReader messageReader = new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), model);
            ODataSerializerContext writeContext = new ODataSerializerContext { RootElementName = "Property", Model = model };
            ODataDeserializerContext readContext = new ODataDeserializerContext { Model = model };

            serializer.WriteObject(obj, typeof(int), messageWriter, writeContext);
            stream.Seek(0, SeekOrigin.Begin);

            // Act && Assert
            Assert.Equal(expected, deserializer.Read(messageReader, typeof(int), readContext));
        }