public void Translate_ReturnsODataKeySegment()
        {
            // Arrange
            EdmModel model = new EdmModel();

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

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

            RouteValueDictionary routeValueDictionary = new RouteValueDictionary(new { key = "42" });

            KeySegmentTemplate template = new KeySegmentTemplate(keys, _customerType, _customers);

            HttpContext httpContext = new DefaultHttpContext();
            ODataTemplateTranslateContext context = new ODataTemplateTranslateContext(httpContext, routeValueDictionary, model);

            // Act
            ODataPathSegment actual = template.Translate(context);

            // Assert
            Assert.NotNull(actual);
            KeySegment keySegment = Assert.IsType <KeySegment>(actual);
            var        actualKey  = Assert.Single(keySegment.Keys);

            Assert.Equal("customerId", actualKey.Key);
            Assert.Equal(42, actualKey.Value);
        }
        public void Translate_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
            {
                { "key1;key2", "firstName='Peter',lastName='Sam'" }
            };

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

            HttpContext httpContext = new DefaultHttpContext();
            ODataTemplateTranslateContext context = new ODataTemplateTranslateContext(httpContext, routeValueDictionary, model);

            // Act
            ODataPathSegment actual = template.Translate(context);

            // Assert
            Assert.NotNull(actual);
            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()));
        }