Exemplo n.º 1
0
        public void WriteObject_Throws_ObjectCannotBeWritten_IfGraphIsNotUri()
        {
            // Arrange
            ODataEntityReferenceLinksSerializer serializer = new ODataEntityReferenceLinksSerializer();

            // Act & Assert
            ExceptionAssert.Throws <SerializationException>(
                () => serializer.WriteObject(graph: "not uri", type: typeof(ODataEntityReferenceLinks),
                                             messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: new ODataSerializerContext()),
                "ODataEntityReferenceLinksSerializer cannot write an object of type 'System.String'.");
        }
Exemplo n.º 2
0
        public async Task ReadAsync_ThrowsArgumentNull_ReadContext()
        {
            // Arrange
            var deserializer = new ODataEntityReferenceLinkDeserializer();
            ODataMessageReader messageReader = ODataTestUtil.GetMockODataMessageReader();

            // Act & Assert
            await ExceptionAssert.ThrowsArgumentNullAsync(
                () => deserializer.ReadAsync(messageReader, type: null, readContext: null),
                "readContext");
        }
Exemplo n.º 3
0
        public void WriteObject_ThrowsArgumentNull_WriteContext()
        {
            // Arrange
            ODataEntityReferenceLinksSerializer serializer = new ODataEntityReferenceLinksSerializer();

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(
                () => serializer.WriteObject(graph: null, type: typeof(ODataEntityReferenceLinks),
                                             messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: null),
                "writeContext");
        }
        public void WriteObject_Throws_ODataPathMissing()
        {
            ODataEntityReferenceLinksSerializer serializer = new ODataEntityReferenceLinksSerializer();
            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_NavigationPropertyMissingDuringSerialization()
        {
            ODataEntityReferenceLinksSerializer serializer = new ODataEntityReferenceLinksSerializer();
            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_ThrowsArgumentNull_WriteContext()
        {
            // Arrange
            Mock <ODataSerializerProvider> provider   = new Mock <ODataSerializerProvider>();
            ODataDeltaFeedSerializer       serializer = new ODataDeltaFeedSerializer(provider.Object);

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(
                () => serializer.WriteObject(graph: null, type: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: null),
                "writeContext");
        }
        public void Read_Throws_SerializationException_ODataPathMissing()
        {
            // Arrange
            ODataActionPayloadDeserializer deserializer  = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
            ODataMessageReader             messageReader = ODataTestUtil.GetMockODataMessageReader();

            // Act & Assert
            Assert.Throws <SerializationException>(
                () => deserializer.Read(messageReader, typeof(ODataActionParameters), readContext: new ODataDeserializerContext()),
                "The operation cannot be completed because no ODataPath is available for the request.");
        }
        public void Read_ThrowsArgumentNull_ReadContext()
        {
            // Arrange
            ODataActionPayloadDeserializer deserializer  = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
            ODataMessageReader             messageReader = ODataTestUtil.GetMockODataMessageReader();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => deserializer.Read(messageReader, typeof(ODataActionParameters), readContext: null),
                "readContext");
        }
Exemplo n.º 9
0
        public async Task WriteObjectAsync_ThrowsAsync_RootElementNameMissing()
        {
            // Arrange
            ODataSerializerContext   writeContext = new ODataSerializerContext();
            ODataPrimitiveSerializer serializer   = new ODataPrimitiveSerializer();

            // Act & Assert
            await ExceptionAssert.ThrowsAsync <ArgumentException>(
                () => serializer.WriteObjectAsync(42, typeof(int), ODataTestUtil.GetMockODataMessageWriter(), writeContext),
                "The 'RootElementName' property is required on 'ODataSerializerContext'. (Parameter 'writeContext')");
        }
        public void Read_ThrowsArgumentMustBeOfType_Type()
        {
            var deserializer = new ODataCollectionDeserializer(new DefaultODataDeserializerProvider());

            Assert.ThrowsArgument(
                () => deserializer.Read(messageReader: ODataTestUtil.GetMockODataMessageReader(), type: typeof(int),
                                        readContext: new ODataDeserializerContext {
                Model = _model
            }),
                "type", "The argument must be of type 'Collection'.");
        }
        public void Read_ThrowsArgumentMustBeOfType_Type()
        {
            // Arrange
            var deserializer = new ODataCollectionDeserializer(DeserializerProvider);

            // Act & Assert
            ExceptionAssert.ThrowsArgument(() => deserializer.Read(messageReader: ODataTestUtil.GetMockODataMessageReader(),
                                                                   type: typeof(int), readContext: new ODataDeserializerContext {
                Model = Model
            }),
                                           "type", "The argument must be of type 'Collection'.");
        }
        public void WriteObject_Throws_ArgumentNull_WriteContext()
        {
            // Arrange
            ODataMessageWriter        messageWriter = ODataTestUtil.GetMockODataMessageWriter();
            ODataSerializerProvider   provider      = new Mock <ODataSerializerProvider>().Object;
            ODataCollectionSerializer serializer    = new ODataCollectionSerializer(provider);

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(
                () => serializer.WriteObject(graph: null, type: typeof(int[]), messageWriter, writeContext: null),
                "writeContext");
        }
        public void DollarMetaData_Works_WithoutAcceptHeader()
        {
            HttpServer server = new HttpServer();

            server.Configuration.EnableOData(ODataTestUtil.GetEdmModel());

            HttpClient client   = new HttpClient(server);
            var        response = client.GetAsync("http://localhost/$metadata").Result;

            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);
            Assert.Contains("<edmx:Edmx", response.Content.ReadAsStringAsync().Result);
        }
        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'.");
        }
Exemplo n.º 15
0
        public void DollarMetaDataWorks_AfterTracingIsEnabled()
        {
            HttpServer server = new HttpServer();

            server.Configuration.Routes.MapODataServiceRoute(ODataTestUtil.GetEdmModel());
            server.Configuration.Services.Replace(typeof(ITraceWriter), new Mock <ITraceWriter>().Object);

            HttpClient client   = new HttpClient(server);
            var        response = client.GetAsync("http://localhost/$metadata").Result;

            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);
            Assert.Contains("<edmx:Edmx", response.Content.ReadAsStringAsync().Result);
        }
Exemplo n.º 16
0
        public void WriteObject_Throws_EntitySetMissingDuringSerialization()
        {
            // Arrange
            ODataEntityReferenceLinksSerializer serializer = new ODataEntityReferenceLinksSerializer();
            ODataSerializerContext writeContext            = new ODataSerializerContext {
                Path = new ODataPath()
            };

            // Act & Assert
            Assert.Throws <SerializationException>(
                () => serializer.WriteObject(graph: null, type: typeof(ODataEntityReferenceLinks),
                                             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 ServiceDocumentWorks_AfterTracingIsEnabled_IfModelIsSetOnConfiguration()
        {
            HttpServer server = new HttpServer();

            server.Configuration.EnableOData(ODataTestUtil.GetEdmModel());
            server.Configuration.Services.Replace(typeof(ITraceWriter), new Mock <ITraceWriter>().Object);

            HttpClient client   = new HttpClient(server);
            var        response = client.GetAsync("http://localhost/").Result;

            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);
            Assert.Contains("<workspace>", response.Content.ReadAsStringAsync().Result);
        }
Exemplo n.º 18
0
        public void WriteObject_Calls_WriteObjectInline()
        {
            // Arrange
            object graph = new object();
            Mock <ODataFeedSerializer> serializer = new Mock <ODataFeedSerializer>(_customersType, new DefaultODataSerializerProvider());

            serializer.CallBase = true;
            serializer.Setup(s => s.WriteObjectInline(graph, It.IsAny <ODataWriter>(), _writeContext)).Verifiable();

            // Act
            serializer.Object.WriteObject(graph, ODataTestUtil.GetMockODataMessageWriter(), _writeContext);

            // Assert
            serializer.Verify();
        }
Exemplo n.º 19
0
        public void WriteObject_Throws_RootElementNameMissing()
        {
            ODataSerializerContext   writeContext = new ODataSerializerContext();
            ODataPrimitiveSerializer serializer   = new ODataPrimitiveSerializer();

#if NETCOREAPP3_1
            ExceptionAssert.Throws <ArgumentException>(
                () => serializer.WriteObject(42, typeof(int), ODataTestUtil.GetMockODataMessageWriter(), writeContext),
                "The 'RootElementName' property is required on 'ODataSerializerContext'. (Parameter 'writeContext')");
#else
            ExceptionAssert.Throws <ArgumentException>(
                () => serializer.WriteObject(42, typeof(int), ODataTestUtil.GetMockODataMessageWriter(), writeContext),
                "The 'RootElementName' property is required on 'ODataSerializerContext'.\r\nParameter name: writeContext");
#endif
        }
        public void WriteObject_Calls_CreateODataPrimitiveValue()
        {
            ODataSerializerContext writeContext = new ODataSerializerContext {
                RootElementName = "Property"
            };
            Mock <ODataPrimitiveSerializer> serializer = new Mock <ODataPrimitiveSerializer>(
                EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: true));

            serializer.CallBase = true;
            serializer.Setup(s => s.CreateODataPrimitiveValue(42, writeContext)).Returns(new ODataPrimitiveValue(42)).Verifiable();

            serializer.Object.WriteObject(42, ODataTestUtil.GetMockODataMessageWriter(), writeContext);

            serializer.Verify();
        }
        public void DollarMetaData_Works_WithoutAcceptHeader()
        {
            HttpServer server = new HttpServer();
            ODataMediaTypeFormatter odataFormatter = new ODataMediaTypeFormatter(ODataTestUtil.GetEdmModel());

            server.Configuration.Formatters.Add(odataFormatter);
            server.Configuration.Routes.MapHttpRoute(ODataRouteNames.Metadata, "$metadata", new { Controller = "ODataMetadata", Action = "GetMetadata" });

            HttpClient client   = new HttpClient(server);
            var        response = client.GetAsync("http://localhost/$metadata").Result;

            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal(response.Content.Headers.ContentType.MediaType, "application/xml");
            Assert.Contains("<edmx:Edmx", response.Content.ReadAsStringAsync().Result);
        }
        public void Read_ThrowsArgument_ODataPathMissing_ForEntity()
        {
            // Arrange
            var deserializer = new ODataResourceDeserializer(_deserializerProvider);
            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(Product)
            };

            // Act & Assert
            Assert.ThrowsArgument(
                () => deserializer.Read(ODataTestUtil.GetMockODataMessageReader(), typeof(Product), readContext),
                "readContext",
                "The operation cannot be completed because no ODataPath is available for the request.");
        }
        public void WriteDeltaFeedInline_WritesEachEntityInstance()
        {
            // Arrange
            Mock <ODataResourceSerializer> customerSerializer = new Mock <ODataResourceSerializer>(_serializerProvider);
            ODataSerializerProvider        provider           = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object);
            var mockWriter = new Mock <ODataWriter>();

            customerSerializer.Setup(s => s.WriteDeltaObjectInline(_deltaFeedCustomers[0], _customersType.ElementType(), mockWriter.Object, _writeContext)).Verifiable();
            _serializer = new ODataDeltaFeedSerializer(provider);

            // Act
            _serializer.WriteDeltaFeedInline(_deltaFeedCustomers, _customersType, mockWriter.Object, _writeContext);

            // Assert
            customerSerializer.Verify();
        }
        public void WriteObject_Calls_CreateODataPrimitiveValue()
        {
            ODataSerializerContext writeContext = new ODataSerializerContext {
                RootElementName = "Property", Model = EdmCoreModel.Instance
            };
            Mock <ODataPrimitiveSerializer> serializer = new Mock <ODataPrimitiveSerializer>();

            serializer.CallBase = true;
            serializer.Setup(s => s.CreateODataPrimitiveValue(
                                 42, It.Is <IEdmPrimitiveTypeReference>(t => t.PrimitiveKind() == EdmPrimitiveTypeKind.Int32), writeContext))
            .Returns(new ODataPrimitiveValue(42)).Verifiable();

            serializer.Object.WriteObject(42, typeof(int), ODataTestUtil.GetMockODataMessageWriter(), writeContext);

            serializer.Verify();
        }
        public void ServiceDocumentWorks_AfterTracingIsEnabled_IfModelIsSetOnConfiguration()
        {
            HttpServer server = new HttpServer();
            ODataMediaTypeFormatter odataFormatter = new ODataMediaTypeFormatter(ODataTestUtil.GetEdmModel());

            server.Configuration.Formatters.Insert(0, odataFormatter);
            server.Configuration.Routes.MapHttpRoute(ODataRouteNames.ServiceDocument, "", new { Controller = "ODataMetadata", Action = "GetServiceDocument" });
            server.Configuration.Services.Replace(typeof(ITraceWriter), new Mock <ITraceWriter>().Object);

            HttpClient client   = new HttpClient(server);
            var        response = client.GetAsync("http://localhost/").Result;

            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal(response.Content.Headers.ContentType.MediaType, "application/xml");
            Assert.Contains("<workspace>", response.Content.ReadAsStringAsync().Result);
        }
Exemplo n.º 26
0
        private static void SetupController(AsyncEntitySetController <FormatterPerson, int> controller)
        {
            var       config    = new HttpConfiguration();
            IEdmModel model     = ODataTestUtil.GetEdmModel();
            string    routeName = "Route";

            config.Routes.MapODataServiceRoute(routeName, null, model);
            HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("Mock"), "http://localhost/People");

            request.SetConfiguration(config);
            request.ODataProperties().Path      = new DefaultODataPathHandler().Parse(model, "People");
            request.ODataProperties().RouteName = routeName;
            controller.Request       = request;
            controller.Configuration = config;
            controller.Url           = new UrlHelper(request);
        }
        public void Read_ThrowsArgument_EntitysetMissing()
        {
            // Arrange
            var deserializer = new ODataResourceDeserializer(_deserializerProvider);
            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Path         = new ODataPath(),
                Model        = _edmModel,
                ResourceType = typeof(Product)
            };

            // Act & Assert
            Assert.Throws <SerializationException>(
                () => deserializer.Read(ODataTestUtil.GetMockODataMessageReader(), typeof(Product), readContext),
                "The related entity set or singleton cannot be found from the OData path. The related entity set or singleton is required to deserialize the payload.");
        }
        public async Task WriteObjectInlineAsync_WritesEachEntityInstance()
        {
            // Arrange
            Mock <IODataSerializerProvider> serializeProvider  = new Mock <IODataSerializerProvider>();
            Mock <ODataResourceSerializer>  customerSerializer = new Mock <ODataResourceSerializer>(serializeProvider.Object);
            IODataSerializerProvider        provider           = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object);
            var mockWriter = new Mock <ODataWriter>();

            customerSerializer.Setup(s => s.WriteDeltaObjectInlineAsync(_deltaResourceSetCustomers[0], _customersType.ElementType(), mockWriter.Object, _writeContext)).Verifiable();
            _serializer = new ODataDeltaResourceSetSerializer(provider);

            // Act
            await _serializer.WriteObjectInlineAsync(_deltaResourceSetCustomers, _customersType, mockWriter.Object, _writeContext);

            // Assert
            customerSerializer.Verify();
        }
Exemplo n.º 29
0
        public void GetQueryOptions_ReturnsRequestQueryOptions()
        {
            var controller    = new Mock <AsyncEntitySetController <FormatterPerson, int> >().Object;
            var request       = new HttpRequestMessage();
            var configuration = new HttpConfiguration();
            var model         = ODataTestUtil.GetEdmModel();

            request.ODataProperties().Model = model;
            controller.Request       = request;
            controller.Configuration = configuration;

            var queryOptions = controller.QueryOptions;

            Assert.Equal(request, queryOptions.Request);
            Assert.Equal(model, queryOptions.Context.Model);
            Assert.Equal(typeof(FormatterPerson), queryOptions.Context.ElementClrType);
        }
Exemplo n.º 30
0
        public void WriteObject_Calls_WriteObjectInline()
        {
            // Arrange
            object graph = new object();
            Mock <ODataResourceSetSerializer> serializer = new Mock <ODataResourceSetSerializer>(_serializerProvider);

            serializer.CallBase = true;
            serializer
            .Setup(s => s.WriteObjectInline(graph, It.Is <IEdmTypeReference>(e => _customersType.IsEquivalentTo(e)),
                                            It.IsAny <ODataWriter>(), _writeContext))
            .Verifiable();

            // Act
            serializer.Object.WriteObject(graph, typeof(Customer[]), ODataTestUtil.GetMockODataMessageWriter(), _writeContext);

            // Assert
            serializer.Verify();
        }