Beispiel #1
0
        public void TryTranslateKeySegmentTemplate_ReturnsODataKeySegment()
        {
            // Arrange
            EdmModel model = new EdmModel();

            model.AddElement(_customerType);
            model.AddElement(_container);

            IDictionary <string, string> keys = new Dictionary <string, string>
            {
                { "customerId", "{key}" }
            };

            KeySegmentTemplate            template = new KeySegmentTemplate(keys, _customerType, _customers);
            ODataTemplateTranslateContext context  = new ODataTemplateTranslateContext
            {
                RouteValues = new RouteValueDictionary(new { key = "42" }),
                Model       = model
            };

            // Act
            bool ok = template.TryTranslate(context);

            // Assert
            Assert.True(ok);
            ODataPathSegment actual     = Assert.Single(context.Segments);
            KeySegment       keySegment = Assert.IsType <KeySegment>(actual);
            var actualKey = Assert.Single(keySegment.Keys);

            Assert.Equal("customerId", actualKey.Key);
            Assert.Equal(42, actualKey.Value);
        }
Beispiel #2
0
        public void TryTranslateKeySegmentTemplate_ThrowsArgumentNull_Context()
        {
            // Arrange
            IDictionary <string, string> keys = new Dictionary <string, string>
            {
                { "customerId", "{key}" }
            };
            KeySegmentTemplate keySegment = new KeySegmentTemplate(keys, _customerType, _customers);

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(() => keySegment.TryTranslate(null), "context");
        }
Beispiel #3
0
        public void TryTranslateKeySegmentTemplate_WorksWithKeyValue_UsingEscapedString()
        {
            // Arrange
            EdmModel              model        = new EdmModel();
            EdmEntityType         customerType = new EdmEntityType("NS", "Customer");
            EdmStructuralProperty firstName    = customerType.AddStructuralProperty("FirstName", EdmPrimitiveTypeKind.String);
            EdmStructuralProperty lastName     = customerType.AddStructuralProperty("LastName", EdmPrimitiveTypeKind.String);

            customerType.AddKeys(firstName, lastName);
            model.AddElement(customerType);
            EdmEntityContainer container = new EdmEntityContainer("NS", "Default");
            EdmEntitySet       customers = container.AddEntitySet("Customers", customerType);

            model.AddElement(container);
            RouteValueDictionary         routeValueDictionary = new RouteValueDictionary(new { First = "'Zhang'", Last = "'Gan%2Fnng%23%20T'" });
            IDictionary <string, string> keys = new Dictionary <string, string>
            {
                { "FirstName", "{first}" },
                { "LastName", "{last}" }
            };
            KeySegmentTemplate template = new KeySegmentTemplate(keys, customerType, customers);

            ODataTemplateTranslateContext context = new ODataTemplateTranslateContext
            {
                RouteValues = routeValueDictionary,
                Model       = model
            };

            // Act
            bool ok = template.TryTranslate(context);

            // Assert
            Assert.True(ok);
            ODataPathSegment actual     = Assert.Single(context.Segments);
            KeySegment       keySegment = Assert.IsType <KeySegment>(actual);

            Assert.Collection(keySegment.Keys,
                              e =>
            {
                Assert.Equal("FirstName", e.Key);
                Assert.Equal("Zhang", e.Value);
            },
                              e =>
            {
                Assert.Equal("LastName", e.Key);
                Assert.Equal("Gan/nng# T", e.Value);
            });
        }
Beispiel #4
0
        public void TryTranslateKeySegmentTemplate_ReturnsODataKeySegment_ForKeyAsSegment()
        {
            // Arrange
            EdmModel              model        = new EdmModel();
            EdmEntityType         customerType = new EdmEntityType("NS", "Customer");
            EdmStructuralProperty idProperty   = customerType.AddStructuralProperty("customerId", EdmPrimitiveTypeKind.String);

            customerType.AddKeys(idProperty);
            model.AddElement(customerType);
            EdmEntityContainer container = new EdmEntityContainer("NS", "Default");
            EdmEntitySet       customers = container.AddEntitySet("Customers", customerType);

            model.AddElement(container);
            RouteValueDictionary         routeValueDictionary = new RouteValueDictionary(new { key = "Peter" });
            IDictionary <string, string> keys = new Dictionary <string, string>
            {
                { "customerId", "{key}" }
            };
            KeySegmentTemplate template = new KeySegmentTemplate(keys, customerType, customers);

            RouteEndpoint endpoint = new RouteEndpoint(
                c => Task.CompletedTask,
                RoutePatternFactory.Parse("odata/customers/{key}/Name"),
                0,
                EndpointMetadataCollection.Empty,
                "test");

            ODataTemplateTranslateContext context = new ODataTemplateTranslateContext
            {
                RouteValues = routeValueDictionary,
                Model       = model,
                Endpoint    = endpoint
            };

            // Act
            bool ok = template.TryTranslate(context);

            // Assert
            Assert.True(ok);
            ODataPathSegment actual     = Assert.Single(context.Segments);
            KeySegment       keySegment = Assert.IsType <KeySegment>(actual);
            KeyValuePair <string, object> actualKeys = Assert.Single(keySegment.Keys);

            Assert.Equal("customerId", actualKeys.Key);
            Assert.Equal("Peter", actualKeys.Value);
        }
Beispiel #5
0
        public void TryTranslateKeySegmentTemplate_ReturnsODataKeySegment_ForCompositeKey()
        {
            // Arrange
            EdmModel              model         = new EdmModel();
            EdmEntityType         customerType  = new EdmEntityType("NS", "Customer");
            EdmStructuralProperty firstProperty = customerType.AddStructuralProperty("firstName", EdmPrimitiveTypeKind.String);
            EdmStructuralProperty lastProperty  = customerType.AddStructuralProperty("lastName", EdmPrimitiveTypeKind.String);

            customerType.AddKeys(firstProperty, lastProperty);
            IDictionary <string, string> keys = new Dictionary <string, string>
            {
                { "firstName", "{key1}" },
                { "lastName", "{key2}" }
            };

            model.AddElement(customerType);
            EdmEntityContainer container = new EdmEntityContainer("NS", "Default");
            EdmEntitySet       customers = container.AddEntitySet("Customers", customerType);

            model.AddElement(container);
            RouteValueDictionary routeValueDictionary = new RouteValueDictionary(new { key1 = "'Peter'", key2 = "'Sam'" });

            KeySegmentTemplate template = new KeySegmentTemplate(keys, customerType, customers);

            ODataTemplateTranslateContext context = new ODataTemplateTranslateContext
            {
                RouteValues = routeValueDictionary,
                Model       = model
            };

            // Act
            bool ok = template.TryTranslate(context);

            // Assert
            Assert.True(ok);
            ODataPathSegment actual     = Assert.Single(context.Segments);
            KeySegment       keySegment = Assert.IsType <KeySegment>(actual);

            Assert.Equal(2, keySegment.Keys.Count());
            Assert.Equal(new[] { "firstName", "lastName" }, keySegment.Keys.Select(c => c.Key));
            Assert.Equal(new[] { "Peter", "Sam" }, keySegment.Keys.Select(c => c.Value.ToString()));
        }
Beispiel #6
0
        public void TryTranslateKeySegmentTemplate_ThrowsODataException_ForInvalidKey()
        {
            // Arrange
            EdmModel              model        = new EdmModel();
            EdmEntityType         customerType = new EdmEntityType("NS", "Customer");
            EdmStructuralProperty idProperty   = customerType.AddStructuralProperty("customerId", EdmPrimitiveTypeKind.Int32);

            customerType.AddKeys(idProperty);
            model.AddElement(customerType);
            EdmEntityContainer container = new EdmEntityContainer("NS", "Default");
            EdmEntitySet       customers = container.AddEntitySet("Customers", customerType);

            model.AddElement(container);
            RouteValueDictionary         routeValueDictionary = new RouteValueDictionary(new { key = "abc12" });
            IDictionary <string, string> keys = new Dictionary <string, string>
            {
                { "customerId", "{key}" }
            };
            KeySegmentTemplate template = new KeySegmentTemplate(keys, customerType, customers);

            RouteEndpoint endpoint = new RouteEndpoint(
                c => Task.CompletedTask,
                RoutePatternFactory.Parse("odata/customers/{key}/Name"),
                0,
                EndpointMetadataCollection.Empty,
                "test");

            ODataTemplateTranslateContext context = new ODataTemplateTranslateContext
            {
                RouteValues = routeValueDictionary,
                Model       = model,
                Endpoint    = endpoint
            };

            // Act
            Action test = () => template.TryTranslate(context);

            // Assert
            ExceptionAssert.Throws <ODataException>(test, "The key value (abc12) from request is not valid. The key value should be format of type 'Edm.Int32'.");
        }
Beispiel #7
0
        public void TryTranslateKeySegmentTemplate_WorksWithKeyParametersAlias()
        {
            // Arrange
            IDictionary <string, string> keys = new Dictionary <string, string>
            {
                { "customerId", "{key}" }
            };
            KeySegmentTemplate template = new KeySegmentTemplate(keys, _customerType, _customers);

            RouteValueDictionary routeValues = new RouteValueDictionary()
            {
                { "key", "@p" }
            };
            EdmModel model = new EdmModel();

            model.AddElement(_customerType);
            HttpContext httpContext = new DefaultHttpContext();

            httpContext.Request.QueryString = new QueryString("?@p=42");
            ODataTemplateTranslateContext context = new ODataTemplateTranslateContext
            {
                HttpContext = httpContext,
                RouteValues = routeValues,
                Model       = model
            };

            // Act
            bool ok = template.TryTranslate(context);

            // Assert
            Assert.True(ok);
            ODataPathSegment actual     = Assert.Single(context.Segments);
            KeySegment       keySegment = Assert.IsType <KeySegment>(actual);
            KeyValuePair <string, object> actualKeys = Assert.Single(keySegment.Keys);

            Assert.Equal("customerId", actualKeys.Key);
            Assert.Equal(42, actualKeys.Value);
        }