Exemplo n.º 1
0
        public void GetETagTentity_RoundTrip_ETagInHeader()
        {
            // Arrange
            HttpRequestMessage request       = new HttpRequestMessage();
            HttpConfiguration  configuration = new HttpConfiguration();

            request.SetConfiguration(configuration);
            Dictionary <string, object> properties = new Dictionary <string, object> {
                { "City", "Foo" }
            };
            EntityTagHeaderValue value = new DefaultODataETagHandler().CreateETag(properties);

            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            Mock <ODataPathSegment> mockSegment = new Mock <ODataPathSegment> {
                CallBase = true
            };

            mockSegment.Setup(s => s.GetEdmType(null)).Returns(model.Customer);
            mockSegment.Setup(s => s.GetEntitySet(null)).Returns((IEdmEntitySet)null);
            ODataPath odataPath = new ODataPath(new[] { mockSegment.Object });

            request.ODataProperties().Path = odataPath;

            // Act
            ETag <Customer> result        = request.GetETag <Customer>(value);
            dynamic         dynamicResult = result;

            // Assert
            Assert.Equal("Foo", result["City"]);
            Assert.Equal("Foo", dynamicResult.City);
        }
        public void GetETagTEntity_Returns_ETagInHeader()
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            HttpRequestMessage request = new HttpRequestMessage();

            request.EnableHttpDependencyInjectionSupport(model.Model);

            Dictionary <string, object> properties = new Dictionary <string, object> {
                { "City", "Foo" }
            };
            EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties);

            ODataPath odataPath = new ODataPath(new EntitySetSegment(model.Customers));

            request.ODataProperties().Path = odataPath;

            // Act
            ETag <Customer> result        = request.GetETag <Customer>(etagHeaderValue);
            dynamic         dynamicResult = result;

            // Assert
            Assert.Equal("Foo", result["City"]);
            Assert.Equal("Foo", dynamicResult.City);
        }
Exemplo n.º 3
0
        public void GetETag_ThrowsArgumentNull_Request()
        {
            // Arrange
            HttpRequestMessage   request = null;
            EntityTagHeaderValue value   = new EntityTagHeaderValue("\"any\"");

            // Act & Assert
            Assert.ThrowsArgumentNull(() => request.GetETag(value), "request");
        }
Exemplo n.º 4
0
        public void GetETag_ThrowsInvalidOperation_EmptyRequest()
        {
            // Arrange
            HttpRequestMessage   request = new HttpRequestMessage();
            EntityTagHeaderValue value   = new EntityTagHeaderValue("\"any\"");

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => request.GetETag(value),
                                                      "Request message does not contain an HttpConfiguration object.");
        }
Exemplo n.º 5
0
        public void GetETagTEntity_Returns_ETagAny()
        {
            // Arrange
            var request         = new HttpRequestMessage();
            var etagHeaderValue = EntityTagHeaderValue.Any;

            // Act
            var result = request.GetETag <Customer>(etagHeaderValue);

            // Assert
            Assert.True(result.IsAny);
        }
        public void GetETag_Returns_ETagInHeader_ForInteger(byte byteValue, short shortValue, long longValue)
        {
            // Arrange
            Dictionary <string, object> properties = new Dictionary <string, object>
            {
                { "ByteVal", byteValue },
                { "LongVal", longValue },
                { "ShortVal", shortValue }
            };
            EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties);

            var builder = new ODataConventionModelBuilder();

            builder.EntitySet <MyEtagOrder>("Orders");
            IEdmModel               model       = builder.GetEdmModel();
            IEdmEntityType          order       = model.SchemaElements.OfType <IEdmEntityType>().FirstOrDefault(e => e.Name == "MyEtagOrder");
            IEdmEntitySet           orders      = model.FindDeclaredEntitySet("Orders");
            Mock <ODataPathSegment> mockSegment = new Mock <ODataPathSegment> {
                CallBase = true
            };

            mockSegment.Setup(s => s.GetEdmType(null)).Returns(order);
            mockSegment.Setup(s => s.GetNavigationSource(null)).Returns(orders);
            ODataPath odataPath = new ODataPath(new[] { mockSegment.Object });

            HttpRequestMessage request      = new HttpRequestMessage();
            HttpConfiguration  cofiguration = new HttpConfiguration();

            request.SetConfiguration(cofiguration);
            request.ODataProperties().Path  = odataPath;
            request.ODataProperties().Model = model;

            // Act
            ETag    result        = request.GetETag(etagHeaderValue);
            dynamic dynamicResult = result;

            // Assert
            byte actualByte = Assert.IsType <byte>(result["ByteVal"]);

            Assert.Equal(actualByte, dynamicResult.ByteVal);
            Assert.Equal(byteValue, actualByte);

            short actualShort = Assert.IsType <short>(result["ShortVal"]);

            Assert.Equal(actualShort, dynamicResult.ShortVal);
            Assert.Equal(shortValue, actualShort);

            long actualLong = Assert.IsType <long>(result["LongVal"]);

            Assert.Equal(actualLong, dynamicResult.LongVal);
            Assert.Equal(longValue, actualLong);
        }
        public void GetETag_Returns_ETagInHeader_ForDouble(double value, bool isEqual)
        {
            // Arrange
            Dictionary <string, object> properties = new Dictionary <string, object> {
                { "Version", value }
            };
            EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties);

            var builder = new ODataConventionModelBuilder();

            builder.EntitySet <MyEtagCustomer>("Customers");
            IEdmModel               model       = builder.GetEdmModel();
            IEdmEntityType          customer    = model.SchemaElements.OfType <IEdmEntityType>().FirstOrDefault(e => e.Name == "MyEtagCustomer");
            IEdmEntitySet           customers   = model.FindDeclaredEntitySet("Customers");
            Mock <ODataPathSegment> mockSegment = new Mock <ODataPathSegment> {
                CallBase = true
            };

            mockSegment.Setup(s => s.GetEdmType(null)).Returns(customer);
            mockSegment.Setup(s => s.GetNavigationSource(null)).Returns(customers);
            ODataPath odataPath = new ODataPath(new[] { mockSegment.Object });

            HttpRequestMessage request      = new HttpRequestMessage();
            HttpConfiguration  cofiguration = new HttpConfiguration();

            request.SetConfiguration(cofiguration);
            request.ODataProperties().Path  = odataPath;
            request.ODataProperties().Model = model;

            // Act
            ETag    result        = request.GetETag(etagHeaderValue);
            dynamic dynamicResult = result;

            // Assert
            double actual = Assert.IsType <double>(result["Version"]);

            Assert.Equal(actual, dynamicResult.Version);

            if (isEqual)
            {
                Assert.Equal(value, actual);
            }
            else
            {
                Assert.NotEqual(value, actual);

                Assert.True(actual - value < 0.0000001);
            }
        }
        public void GetETag_Returns_ETagInHeader_ForInteger(byte byteValue, short shortValue, long longValue)
        {
            // Arrange
            Dictionary <string, object> properties = new Dictionary <string, object>
            {
                { "ByteVal", byteValue },
                { "LongVal", longValue },
                { "ShortVal", shortValue }
            };
            EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties);

            var builder = new ODataConventionModelBuilder();

            builder.EntitySet <MyEtagOrder>("Orders");
            IEdmModel          model     = builder.GetEdmModel();
            IEdmEntitySet      orders    = model.FindDeclaredEntitySet("Orders");
            ODataPath          odataPath = new ODataPath(new EntitySetSegment(orders));
            HttpRequestMessage request   = new HttpRequestMessage();

            request.EnableHttpDependencyInjectionSupport(model);
            request.ODataProperties().Path = odataPath;

            // Act
            ETag    result        = request.GetETag(etagHeaderValue);
            dynamic dynamicResult = result;

            // Assert
            byte actualByte = Assert.IsType <byte>(result["ByteVal"]);

            Assert.Equal(actualByte, dynamicResult.ByteVal);
            Assert.Equal(byteValue, actualByte);

            short actualShort = Assert.IsType <short>(result["ShortVal"]);

            Assert.Equal(actualShort, dynamicResult.ShortVal);
            Assert.Equal(shortValue, actualShort);

            long actualLong = Assert.IsType <long>(result["LongVal"]);

            Assert.Equal(actualLong, dynamicResult.LongVal);
            Assert.Equal(longValue, actualLong);
        }
        public void GetETag_Returns_ETagInHeader_ForDouble(double value, bool isEqual)
        {
            // Arrange
            Dictionary <string, object> properties = new Dictionary <string, object> {
                { "Version", value }
            };
            EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties);

            var builder = new ODataConventionModelBuilder();

            builder.EntitySet <MyEtagCustomer>("Customers");
            IEdmModel     model     = builder.GetEdmModel();
            IEdmEntitySet customers = model.FindDeclaredEntitySet("Customers");
            ODataPath     odataPath = new ODataPath(new EntitySetSegment(customers));

            HttpRequestMessage request = new HttpRequestMessage();

            request.EnableHttpDependencyInjectionSupport(model);
            request.ODataProperties().Path = odataPath;

            // Act
            ETag    result        = request.GetETag(etagHeaderValue);
            dynamic dynamicResult = result;

            // Assert
            double actual = Assert.IsType <double>(result["Version"]);

            Assert.Equal(actual, dynamicResult.Version);

            if (isEqual)
            {
                Assert.Equal(value, actual);
            }
            else
            {
                Assert.NotEqual(value, actual);

                Assert.True(actual - value < 0.0000001);
            }
        }
        public void GetETagTEntity_Returns_ETagInHeader()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();
            HttpConfiguration cofiguration = new HttpConfiguration();
            request.SetConfiguration(cofiguration);
            Dictionary<string, object> properties = new Dictionary<string, object> { { "City", "Foo" } };
            EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties);

            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            Mock<ODataPathSegment> mockSegment = new Mock<ODataPathSegment> { CallBase = true };
            mockSegment.Setup(s => s.GetEdmType(null)).Returns(model.Customer);
            mockSegment.Setup(s => s.GetNavigationSource(null)).Returns((IEdmNavigationSource)null);
            ODataPath odataPath = new ODataPath(new[] { mockSegment.Object });
            request.ODataProperties().Path = odataPath;

            // Act
            ETag<Customer> result = request.GetETag<Customer>(etagHeaderValue);
            dynamic dynamicResult = result;

            // Assert
            Assert.Equal("Foo", result["City"]);
            Assert.Equal("Foo", dynamicResult.City);
        }
        public void GetETagTEntity_Returns_ETagAny()
        {
            // Arrange
            var request = new HttpRequestMessage();
            var etagHeaderValue = EntityTagHeaderValue.Any;

            // Act
            var result = request.GetETag<Customer>(etagHeaderValue);

            // Assert
            Assert.True(result.IsAny);
        }
        public void GetETag_ThrowsInvalidOperation_EmptyRequest()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();
            EntityTagHeaderValue headerValue = new EntityTagHeaderValue("\"any\"");

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() => request.GetETag(headerValue),
                "Request message does not contain an HttpConfiguration object.");
        }
        public void GetETag_Returns_ETagInHeader_ForInteger(byte byteValue, short shortValue, long longValue)
        {
            // Arrange
            Dictionary<string, object> properties = new Dictionary<string, object>
            {
                { "ByteVal", byteValue },
                { "LongVal", longValue },
                { "ShortVal", shortValue }
            };
            EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties);

            var builder = new ODataConventionModelBuilder();
            builder.EntitySet<MyEtagOrder>("Orders");
            IEdmModel model = builder.GetEdmModel();
            IEdmEntityType order = model.SchemaElements.OfType<IEdmEntityType>().FirstOrDefault(e => e.Name == "MyEtagOrder");
            IEdmEntitySet orders = model.FindDeclaredEntitySet("Orders");
            Mock<ODataPathSegment> mockSegment = new Mock<ODataPathSegment> { CallBase = true };
            mockSegment.Setup(s => s.GetEdmType(null)).Returns(order);
            mockSegment.Setup(s => s.GetNavigationSource(null)).Returns(orders);
            ODataPath odataPath = new ODataPath(new[] { mockSegment.Object });

            HttpRequestMessage request = new HttpRequestMessage();
            HttpConfiguration cofiguration = new HttpConfiguration();
            request.SetConfiguration(cofiguration);
            request.ODataProperties().Path = odataPath;
            request.ODataProperties().Model = model;

            // Act
            ETag result = request.GetETag(etagHeaderValue);
            dynamic dynamicResult = result;

            // Assert
            byte actualByte = Assert.IsType<byte>(result["ByteVal"]);
            Assert.Equal(actualByte, dynamicResult.ByteVal);
            Assert.Equal(byteValue, actualByte);

            short actualShort = Assert.IsType<short>(result["ShortVal"]);
            Assert.Equal(actualShort, dynamicResult.ShortVal);
            Assert.Equal(shortValue, actualShort);

            long actualLong = Assert.IsType<long>(result["LongVal"]);
            Assert.Equal(actualLong, dynamicResult.LongVal);
            Assert.Equal(longValue, actualLong);

        }
        public void GetETag_Returns_ETagInHeader_ForDouble(double value, bool isEqual)
        {
            // Arrange
            Dictionary<string, object> properties = new Dictionary<string, object> { { "Version", value } };
            EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties);

            var builder = new ODataConventionModelBuilder();
            builder.EntitySet<MyEtagCustomer>("Customers");
            IEdmModel model = builder.GetEdmModel();
            IEdmEntityType customer = model.SchemaElements.OfType<IEdmEntityType>().FirstOrDefault(e => e.Name == "MyEtagCustomer");
            IEdmEntitySet customers = model.FindDeclaredEntitySet("Customers");
            Mock<ODataPathSegment> mockSegment = new Mock<ODataPathSegment> { CallBase = true };
            mockSegment.Setup(s => s.GetEdmType(null)).Returns(customer);
            mockSegment.Setup(s => s.GetNavigationSource(null)).Returns(customers);
            ODataPath odataPath = new ODataPath(new[] { mockSegment.Object });

            HttpRequestMessage request = new HttpRequestMessage();
            HttpConfiguration cofiguration = new HttpConfiguration();
            request.SetConfiguration(cofiguration);
            request.ODataProperties().Path = odataPath;
            request.ODataProperties().Model = model;

            // Act
            ETag result = request.GetETag(etagHeaderValue);
            dynamic dynamicResult = result;

            // Assert
            double actual = Assert.IsType<double>(result["Version"]);
            Assert.Equal(actual, dynamicResult.Version);

            if (isEqual)
            {
                Assert.Equal(value, actual);
            }
            else
            {
                Assert.NotEqual(value, actual);

                Assert.True(actual - value < 0.0000001);
            }
        }
Exemplo n.º 15
0
        public void ApplyTo_NewQueryReturned_ForInteger(sbyte byteVal, short shortVal, bool ifMatch, IList<int> expect)
        {
            // Arrange
            var mycustomers = new List<MyETagOrder>
            {
                new MyETagOrder
                {
                    ID = 1,
                    ByteVal = 7,
                    ShortVal = 8
                },
                new MyETagOrder
                {
                    ID = 2,
                    ByteVal = SByte.MaxValue,
                    ShortVal = Int16.MaxValue
                },
                new MyETagOrder
                {
                    ID = 3,
                    ByteVal = SByte.MinValue,
                    ShortVal = Int16.MinValue
                },
            };
            IETagHandler handerl = new DefaultODataETagHandler();
            Dictionary<string, object> properties = new Dictionary<string, object>
            {
                { "ByteVal", byteVal },
                { "ShortVal", shortVal }
            };
            EntityTagHeaderValue etagHeaderValue = handerl.CreateETag(properties);

            HttpRequestMessage request = new HttpRequestMessage();
            HttpConfiguration cofiguration = new HttpConfiguration();
            request.SetConfiguration(cofiguration);

            var builder = new ODataConventionModelBuilder();
            builder.EntitySet<MyETagOrder>("Orders");
            IEdmModel model = builder.GetEdmModel();
            IEdmEntityType order = model.SchemaElements.OfType<IEdmEntityType>().FirstOrDefault(e => e.Name == "MyETagOrder");
            IEdmEntitySet orders = model.FindDeclaredEntitySet("Orders");
            Mock<ODataPathSegment> mockSegment = new Mock<ODataPathSegment> { CallBase = true };
            mockSegment.Setup(s => s.GetEdmType(null)).Returns(order);
            mockSegment.Setup(s => s.GetNavigationSource(null)).Returns(orders);
            ODataPath odataPath = new ODataPath(new[] { mockSegment.Object });
            request.ODataProperties().Path = odataPath;
            request.ODataProperties().Model = model;

            ETag etagCustomer = request.GetETag(etagHeaderValue);
            etagCustomer.EntityType = typeof(MyETagOrder);
            etagCustomer.IsIfNoneMatch = !ifMatch;

            // Act
            IQueryable queryable = etagCustomer.ApplyTo(mycustomers.AsQueryable());

            // Assert
            Assert.NotNull(queryable);
            IEnumerable<MyETagOrder> actualOrders = Assert.IsAssignableFrom<IEnumerable<MyETagOrder>>(queryable);
            Assert.Equal(expect, actualOrders.Select(c => c.ID));
            MethodCallExpression methodCall = queryable.Expression as MethodCallExpression;
            Assert.NotNull(methodCall);
            Assert.Equal(2, methodCall.Arguments.Count);

            if (ifMatch)
            {
                Assert.Equal(
                    "Param_0 => ((Param_0.ByteVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.SByte]).TypedProperty) " +
                    "AndAlso (Param_0.ShortVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Int16]).TypedProperty))",
                    methodCall.Arguments[1].ToString());
            }
            else
            {
                Assert.Equal(
                    "Param_0 => Not(((Param_0.ByteVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.SByte]).TypedProperty) " +
                    "AndAlso (Param_0.ShortVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Int16]).TypedProperty)))",
                    methodCall.Arguments[1].ToString());
            }
        }