예제 #1
0
        public void TranslateFunctionImportSegmentTemplate_ReturnsNull_WithOptionalParameterMisMatch()
        {
            // Arrange
            _function.AddOptionalParameter("min", IntPrimitive);
            _function.AddOptionalParameter("max", IntPrimitive);
            EdmModel model = new EdmModel();

            model.AddElement(_function);
            model.AddElement(_container);

            IDictionary <string, string> parameters = new Dictionary <string, string>
            {
                { "name", "{nameTemp}" },
                { "title", "{titleTemp}" },
                { "min", "{minTemp}" },
            };
            FunctionImportSegmentTemplate template = new FunctionImportSegmentTemplate(parameters, _functionImport, null);

            RouteValueDictionary          routeValues = new RouteValueDictionary(new { name = "'pt'", title = "'abc'", min = "42,max=5" });
            HttpContext                   httpContext = new DefaultHttpContext();
            ODataTemplateTranslateContext context     = new ODataTemplateTranslateContext(httpContext, routeValues, model);

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

            // Assert
            Assert.Null(actual);
        }
        public void Translate_ReturnsODataFunctionImportSegment()
        {
            // Arrange
            var         primitive = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, false);
            EdmFunction function  = new EdmFunction("NS", "MyFunction", primitive, false, null, false);

            function.AddParameter("name", primitive);
            function.AddParameter("title", primitive);
            EdmEntityContainer            container      = new EdmEntityContainer("NS", "Default");
            EdmFunctionImport             functionImport = new EdmFunctionImport(container, "MyFunctionImport", function);
            FunctionImportSegmentTemplate template       = new FunctionImportSegmentTemplate(functionImport, null);

            Mock <HttpContext>            httpContext = new Mock <HttpContext>();
            Mock <IEdmModel>              edmModel    = new Mock <IEdmModel>();
            ODataTemplateTranslateContext context     = new ODataTemplateTranslateContext(httpContext.Object,
                                                                                          new RouteValueDictionary(), edmModel.Object);

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

            // Assert
            Assert.NotNull(actual);
            OperationImportSegment functionImportSegment = Assert.IsType <OperationImportSegment>(actual);

            Assert.Same(function, functionImportSegment.OperationImports.First().Operation);
        }
        public void CtorFunctionImportSegmentTemplate_SetsProperties()
        {
            // Arrange & Act
            FunctionImportSegmentTemplate functionImportSegment = new FunctionImportSegmentTemplate(_functionImport, null);

            // Assert
            Assert.Same(_functionImport, functionImportSegment.FunctionImport);
            Assert.Null(functionImportSegment.NavigationSource);
            Assert.NotNull(functionImportSegment.ParameterMappings);
            Assert.Collection(functionImportSegment.ParameterMappings,
                              e =>
            {
                Assert.Equal("name", e.Key);
                Assert.Equal("name", e.Value);
            },
                              e =>
            {
                Assert.Equal("title", e.Key);
                Assert.Equal("title", e.Value);
            });

            // Arrange & Act
            OperationImportSegment importSegment = new OperationImportSegment(_functionImport, null);

            functionImportSegment = new FunctionImportSegmentTemplate(importSegment);
            Assert.Empty(functionImportSegment.ParameterMappings);
        }
예제 #4
0
        public void ParseODataUriTemplate_ForFunctionImport(string template, int parameterCount)
        {
            // Arrange
            EdmFunction getSalaray = new EdmFunction("NS", "GetWholeSalaryImport", IntType, isBound: false, entitySetPathExpression: null, isComposable: false);

            getSalaray.AddParameter("minSalary", IntType);
            getSalaray.AddOptionalParameter("maxSalary", IntType);
            getSalaray.AddOptionalParameter("aveSalary", IntType, "129");
            _edmModel.AddElement(getSalaray);

            EdmEntityContainer container = _edmModel.SchemaElements.OfType <EdmEntityContainer>().First();

            container.AddFunctionImport(getSalaray);

            IODataPathTemplateParser parser = new DefaultODataPathTemplateParser();

            // Act
            ODataPathTemplate path = parser.Parse(_edmModel, template, null);

            // Assert
            Assert.NotNull(path);
            ODataSegmentTemplate          segmentTemplate       = Assert.Single(path);
            FunctionImportSegmentTemplate functionImportSegment = Assert.IsType <FunctionImportSegmentTemplate>(segmentTemplate);

            if (parameterCount == 2)
            {
                Assert.Equal(new[] { "minSalary", "maxSalary" }, functionImportSegment.ParameterMappings.Keys);
                Assert.Equal(new[] { "min", "max" }, functionImportSegment.ParameterMappings.Values);
            }
            else
            {
                Assert.Equal(new[] { "minSalary", "maxSalary", "aveSalary" }, functionImportSegment.ParameterMappings.Keys);
                Assert.Equal(new[] { "min", "max", "ave" }, functionImportSegment.ParameterMappings.Values);
            }
        }
        public void TranslateFunctionImportSegmentTemplate_ReturnsODataFunctionImportSegment_WithOptionalParameter()
        {
            // Arrange
            _function.AddOptionalParameter("min", IntPrimitive);
            _function.AddOptionalParameter("max", IntPrimitive);
            EdmModel model = new EdmModel();

            model.AddElement(_function);
            model.AddElement(_container);

            IDictionary <string, string> parameters = new Dictionary <string, string>
            {
                { "name", "{nameTemp}" },
                { "title", "{titleTemp}" },
                { "min", "{minTemp}" },
            };
            FunctionImportSegmentTemplate template = new FunctionImportSegmentTemplate(parameters, _functionImport, null);

            RouteValueDictionary          routeValues = new RouteValueDictionary(new { nameTemp = "'pt'", titleTemp = "'abc'", minTemp = "42" });
            HttpContext                   httpContext = new DefaultHttpContext();
            ODataTemplateTranslateContext context     = new ODataTemplateTranslateContext(httpContext, routeValues, model);

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

            // Assert
            Assert.NotNull(actual);
            OperationImportSegment functionImportSegment = Assert.IsType <OperationImportSegment>(actual);

            Assert.Same(_function, functionImportSegment.OperationImports.First().Operation);

            Assert.Equal("pt", routeValues["nameTemp"]);
            Assert.Equal("abc", routeValues["titleTemp"]);
            Assert.Equal(42, routeValues["minTemp"]);
        }
        public void TryTranslateFunctionImportSegmentTemplate_ReturnsFalse_WithMisMatchParameter()
        {
            // Arrange
            EdmModel model = new EdmModel();

            model.AddElement(_function);
            model.AddElement(_container);

            IDictionary <string, string> parameters = new Dictionary <string, string>
            {
                { "name", "{nameTemp}" },
                { "title", "{titleTemp}" },
            };
            FunctionImportSegmentTemplate template = new FunctionImportSegmentTemplate(parameters, _functionImport, null);

            RouteValueDictionary          routeValues = new RouteValueDictionary(new { name = "'pt'" });
            ODataTemplateTranslateContext context     = new ODataTemplateTranslateContext
            {
                RouteValues = routeValues,
                Model       = model
            };

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

            // Assert
            Assert.False(ok);
            Assert.Empty(context.Segments);
        }
        public void TryTranslateActionImportSegmentTemplate_ThrowsArgumentNull_Context()
        {
            // Arrange
            FunctionImportSegmentTemplate functionImportSegment = new FunctionImportSegmentTemplate(_functionImport, null);

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(() => functionImportSegment.TryTranslate(null), "context");
        }
예제 #8
0
        public void CommonFunctionImportTemplateProperties_ReturnsAsExpected()
        {
            // Assert
            FunctionImportSegmentTemplate functionImportSegment = new FunctionImportSegmentTemplate(_functionImport, null);

            // Act & Assert
            Assert.Equal("MyFunctionImport(name={name},title={title})", functionImportSegment.Literal);
            Assert.Equal(ODataSegmentKind.FunctionImport, functionImportSegment.Kind);
            Assert.True(functionImportSegment.IsSingle);
            Assert.Same(IntPrimitive.Definition, functionImportSegment.EdmType);
            Assert.Null(functionImportSegment.NavigationSource);
        }
        public void GetTemplatesFunctionImportSegmentTemplate_ReturnsTemplates()
        {
            // Arrange
            FunctionImportSegmentTemplate functionImportSegment = new FunctionImportSegmentTemplate(_functionImport, null);

            // Act & Assert
            IEnumerable <string> templates = functionImportSegment.GetTemplates();
            string template = Assert.Single(templates);

            Assert.Equal("/MyFunctionImport(name={name},title={title})", template);
            Assert.Null(functionImportSegment.NavigationSource);
        }
        public void FunctionCommonProperties_ReturnsAsExpected()
        {
            // Assert
            var         primitive = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, false);
            EdmFunction function  = new EdmFunction("NS", "MyFunction", primitive, false, null, false);

            function.AddParameter("name", primitive);
            function.AddParameter("title", primitive);
            EdmEntityContainer container      = new EdmEntityContainer("NS", "Default");
            EdmFunctionImport  functionImport = new EdmFunctionImport(container, "MyFunctionImport", function);

            FunctionImportSegmentTemplate functionImportSegment = new FunctionImportSegmentTemplate(functionImport, null);

            // Act & Assert
            Assert.Equal("MyFunctionImport(name={name},title={title})", functionImportSegment.Literal);
            Assert.Equal(ODataSegmentKind.FunctionImport, functionImportSegment.Kind);
            Assert.True(functionImportSegment.IsSingle);
            Assert.Same(primitive.Definition, functionImportSegment.EdmType);
            Assert.Null(functionImportSegment.NavigationSource);
        }
        public void TryTranslateFunctionImportSegmentTemplate_ReturnsODataFunctionImportSegment_WithOptionalParameter()
        {
            // Arrange
            _function.AddOptionalParameter("min", IntPrimitive);
            _function.AddOptionalParameter("max", IntPrimitive);
            EdmModel model = new EdmModel();

            model.AddElement(_function);
            model.AddElement(_container);

            IDictionary <string, string> parameters = new Dictionary <string, string>
            {
                { "name", "{nameTemp}" },
                { "title", "{titleTemp}" },
                { "min", "{minTemp}" },
            };
            FunctionImportSegmentTemplate template = new FunctionImportSegmentTemplate(parameters, _functionImport, null);

            RouteValueDictionary          routeValues = new RouteValueDictionary(new { nameTemp = "'pt'", titleTemp = "'abc'", minTemp = "42" });
            ODataTemplateTranslateContext context     = new ODataTemplateTranslateContext
            {
                RouteValues = routeValues,
                Model       = model
            };

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

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

            Assert.Same(_function, functionImportSegment.OperationImports.First().Operation);
            Assert.Equal(3, functionImportSegment.Parameters.Count());
            Assert.Equal(new[] { "name", "title", "min" }, functionImportSegment.Parameters.Select(p => p.Name));

            Assert.Equal("pt", context.UpdatedValues["nameTemp"]);
            Assert.Equal("abc", context.UpdatedValues["titleTemp"]);
            Assert.Equal(42, context.UpdatedValues["minTemp"]);
        }
        public void GetTemplatesFunctionImportSegmentTemplate_ReturnsTemplates_ForEmptyParameter()
        {
            // Arrange
            EdmFunction                   function              = new EdmFunction("NS", "MyFunctionImport", IntPrimitive, false, null, false);
            EdmEntityContainer            container             = new EdmEntityContainer("NS", "Default");
            EdmFunctionImport             functionImport        = new EdmFunctionImport(container, "MyFunctionImport", function);
            FunctionImportSegmentTemplate functionImportSegment = new FunctionImportSegmentTemplate(functionImport, null);

            // Act & Assert
            IEnumerable <string> templates = functionImportSegment.GetTemplates();
            string template = Assert.Single(templates);

            Assert.Equal("/MyFunctionImport()", template);

            // Act & Assert
            templates = functionImportSegment.GetTemplates(new ODataRouteOptions
            {
                EnableNonParenthesisForEmptyParameterFunction = true
            });
            template = Assert.Single(templates);
            Assert.Equal("/MyFunctionImport", template);
        }
        public void TryTranslateFunctionImportSegmentTemplate_ReturnsODataFunctionImportSegment()
        {
            // Arrange
            EdmFunction        function       = new EdmFunction("NS", "MyFunctionImport", IntPrimitive, false, null, false);
            EdmEntityContainer container      = new EdmEntityContainer("NS", "Default");
            EdmFunctionImport  functionImport = new EdmFunctionImport(container, "MyFunctionImport", function);

            FunctionImportSegmentTemplate template = new FunctionImportSegmentTemplate(functionImport, null);
            ODataTemplateTranslateContext context  = new ODataTemplateTranslateContext
            {
                RouteValues = new RouteValueDictionary()
            };

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

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

            Assert.Same(function, functionImportSegment.OperationImports.First().Operation);
        }
예제 #14
0
 public static ODataOperationImportSegment ConvertTo(this FunctionImportSegmentTemplate functionImport)
 {
     return(new ODataOperationImportSegment(functionImport.FunctionImport, functionImport.ParameterMappings));
 }
예제 #15
0
        public static ODataPath Translate(this ODataPathTemplate pathTemplate)
        {
            if (pathTemplate.Count == 0)
            {
                // It's service root, so far, let's skip it.
                return(null);
            }

            IList <ODataSegment> newSegments = new List <ODataSegment>();

            foreach (var segment in pathTemplate)
            {
                switch (segment.Kind)
                {
                case ODataSegmentKind.Metadata:
                    newSegments.Add(new ODataMetadataSegment());
                    break;

                case ODataSegmentKind.EntitySet:
                    EntitySetSegmentTemplate entitySet = (EntitySetSegmentTemplate)segment;
                    newSegments.Add(entitySet.ConvertTo());
                    break;

                case ODataSegmentKind.Singleton:
                    SingletonSegmentTemplate singleton = (SingletonSegmentTemplate)segment;
                    newSegments.Add(singleton.ConvertTo());
                    break;

                case ODataSegmentKind.Key:
                    KeySegmentTemplate key = (KeySegmentTemplate)segment;
                    newSegments.Add(key.ConvertTo());
                    break;

                case ODataSegmentKind.Cast:
                    CastSegmentTemplate cast = (CastSegmentTemplate)segment;
                    newSegments.Add(cast.ConvertTo());
                    break;

                case ODataSegmentKind.Property:
                    // TODO:
                    return(null);

                //PropertySegmentTemplate property = (PropertySegmentTemplate)segment;
                //newSegments.Add(property.ConvertTo());
                //break;

                case ODataSegmentKind.Navigation:
                    NavigationSegmentTemplate navigation = (NavigationSegmentTemplate)segment;
                    newSegments.Add(navigation.ConvertTo());
                    break;

                case ODataSegmentKind.Function:
                    FunctionSegmentTemplate function = (FunctionSegmentTemplate)segment;
                    newSegments.Add(function.ConvertTo());
                    break;

                case ODataSegmentKind.Action:
                    ActionSegmentTemplate action = (ActionSegmentTemplate)segment;
                    newSegments.Add(action.ConvertTo());
                    break;

                case ODataSegmentKind.FunctionImport:
                    FunctionImportSegmentTemplate functionImport = (FunctionImportSegmentTemplate)segment;
                    newSegments.Add(functionImport.ConvertTo());
                    break;

                case ODataSegmentKind.ActionImport:
                    ActionImportSegmentTemplate actionImport = (ActionImportSegmentTemplate)segment;
                    newSegments.Add(actionImport.ConvertTo());
                    break;

                case ODataSegmentKind.Value:
                    return(null);

                //ValueSegmentTemplate value = (ValueSegmentTemplate)segment;
                //newSegments.Add(value.ConvertTo());
                //break;

                case ODataSegmentKind.Ref:
                    return(null);

                //KeySegmentTemplate key = (KeySegmentTemplate)segment;
                //newSegments.Add(key.ConvertTo());
                //break;

                case ODataSegmentKind.NavigationLink:
                    return(null);

                //NavigationLinkSegmentTemplate navigationLink = (NavigationLinkSegmentTemplate)segment;
                //newSegments.Add(navigationLink.ConvertTo());
                //break;

                case ODataSegmentKind.Count:
                    CountSegmentTemplate count = (CountSegmentTemplate)segment;
                    newSegments.Add(count.ConvertTo());
                    break;

                case ODataSegmentKind.PathTemplate:
                    return(null);

                //KeySegmentTemplate key = (KeySegmentTemplate)segment;
                //newSegments.Add(key.ConvertTo());
                //break;

                case ODataSegmentKind.Dynamic:
                    return(null);

                //KeySegmentTemplate key = (KeySegmentTemplate)segment;
                //newSegments.Add(key.ConvertTo());
                //break;

                default:
                    throw new NotSupportedException();
                }
            }

            return(new ODataPath(newSegments));
        }