Exemple #1
0
        public void CreatePathItemsReturnsForBasicModel()
        {
            // Arrange
            IEdmModel model = EdmModelHelper.BasicEdmModel;
            OpenApiConvertSettings settings = new OpenApiConvertSettings
            {
                EnableKeyAsSegment = true
            };
            ODataContext context = new ODataContext(model, settings);

            // Act
            var pathItems = context.CreatePathItems();

            // Assert
            Assert.NotNull(pathItems);
            Assert.Equal(7, pathItems.Count);

            Assert.Contains("/People", pathItems.Keys);
            Assert.Contains("/People/{UserName}", pathItems.Keys);
            Assert.Contains("/City", pathItems.Keys);
            Assert.Contains("/City/{Name}", pathItems.Keys);
            Assert.Contains("/CountryOrRegion", pathItems.Keys);
            Assert.Contains("/CountryOrRegion/{Name}", pathItems.Keys);
            Assert.Contains("/Me", pathItems.Keys);
        }
Exemple #2
0
        public void CreatePathItemsThrowArgumentNullContext()
        {
            // Arrange
            ODataContext context = null;

            // Act & Assert
            Assert.Throws <ArgumentNullException>("context", () => context.CreatePathItems());
        }
Exemple #3
0
        public void CreatePathItemsReturnsForBasicModel(bool useAnnotationToGeneratePath, int pathCount)
        {
            // Arrange
            IEdmModel model = EdmModelHelper.BasicEdmModel;
            OpenApiConvertSettings settings = new OpenApiConvertSettings
            {
                EnableKeyAsSegment = true,
                RequireRestrictionAnnotationsToGenerateComplexPropertyPaths = useAnnotationToGeneratePath
            };
            ODataContext context = new ODataContext(model, settings);

            // Act
            var pathItems = context.CreatePathItems();

            // Assert
            Assert.NotNull(pathItems);
            Assert.Equal(pathCount, pathItems.Count);

            if (useAnnotationToGeneratePath)
            {
                Assert.Contains("/People", pathItems.Keys);
                Assert.Contains("/People/$count", pathItems.Keys);
                Assert.Contains("/People/{UserName}", pathItems.Keys);
                Assert.Contains("/City", pathItems.Keys);
                Assert.Contains("/City/$count", pathItems.Keys);
                Assert.Contains("/City/{Name}", pathItems.Keys);
                Assert.Contains("/CountryOrRegion", pathItems.Keys);
                Assert.Contains("/CountryOrRegion/$count", pathItems.Keys);
                Assert.Contains("/CountryOrRegion/{Name}", pathItems.Keys);
                Assert.Contains("/Me", pathItems.Keys);
            }
            else
            {
                Assert.Contains("/People", pathItems.Keys);
                Assert.Contains("/People/$count", pathItems.Keys);
                Assert.Contains("/People/{UserName}", pathItems.Keys);
                Assert.Contains("/People/{UserName}/Addresses", pathItems.Keys);
                Assert.Contains("/People/{UserName}/Addresses/$count", pathItems.Keys);
                Assert.Contains("/People/{UserName}/HomeAddress", pathItems.Keys);
                Assert.Contains("/People/{UserName}/HomeAddress/City", pathItems.Keys);
                Assert.Contains("/City", pathItems.Keys);
                Assert.Contains("/City/$count", pathItems.Keys);
                Assert.Contains("/City/{Name}", pathItems.Keys);
                Assert.Contains("/CountryOrRegion", pathItems.Keys);
                Assert.Contains("/CountryOrRegion/$count", pathItems.Keys);
                Assert.Contains("/CountryOrRegion/{Name}", pathItems.Keys);
                Assert.Contains("/Me", pathItems.Keys);
                Assert.Contains("/Me/Addresses", pathItems.Keys);
                Assert.Contains("/Me/Addresses/$count", pathItems.Keys);
                Assert.Contains("/Me/HomeAddress", pathItems.Keys);
                Assert.Contains("/Me/HomeAddress/City", pathItems.Keys);
                Assert.Contains("/Me/WorkAddress", pathItems.Keys);
                Assert.Contains("/Me/WorkAddress/City", pathItems.Keys);
            }
        }
Exemple #4
0
        public void CreatePathItemsReturnsForEmptyModel()
        {
            // Arrange
            IEdmModel    model   = EdmModelHelper.EmptyModel;
            ODataContext context = new ODataContext(model);

            // Act
            var pathItems = context.CreatePathItems();

            // Assert
            Assert.NotNull(pathItems);
            Assert.Empty(pathItems);
        }
Exemple #5
0
        /// <summary>
        /// Create a <see cref="OpenApiPaths"/>
        /// The value of paths is a Paths Object.
        /// It is the main source of information on how to use the described API.
        /// It consists of name/value pairs whose name is a path template relative to the service root URL,
        /// and whose value is a Path Item Object.
        /// </summary>
        /// <param name="context">The OData context.</param>
        /// <returns>The created <see cref="OpenApiPaths"/> object.</returns>
        public static OpenApiPaths CreatePaths(this ODataContext context)
        {
            Utils.CheckArgumentNull(context, nameof(context));

            // Due to the power and flexibility of OData a full representation of all service capabilities
            // in the Paths Object is typically not feasible, so this mapping only describes the minimum
            // information desired in the Paths Object.
            OpenApiPaths paths = new OpenApiPaths();

            foreach (var item in context.CreatePathItems())
            {
                paths.Add(item.Key, item.Value);
            }

            return(paths);
        }
Exemple #6
0
        public void CreatePathItemsReturnsForEscapeFunctionModel(bool enableEscaped, bool hasEscapedAnnotation, bool isComposable, string expected)
        {
            // Arrange
            EdmModel      model    = new EdmModel();
            EdmEntityType customer = new EdmEntityType("NS", "Customer");

            customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            model.AddElement(customer);
            EdmFunction function = new EdmFunction("NS", "MyFunction", EdmCoreModel.Instance.GetString(false), true, null, isComposable);

            function.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            function.AddParameter("param", EdmCoreModel.Instance.GetString(false));
            model.AddElement(function);
            EdmEntityContainer container = new EdmEntityContainer("NS", "Default");
            EdmEntitySet       customers = new EdmEntitySet(container, "Customers", customer);

            container.AddElement(customers);
            model.AddElement(container);

            if (hasEscapedAnnotation)
            {
                IEdmBooleanConstantExpression booleanConstant = new EdmBooleanConstant(true);
                IEdmTerm term = CommunityVocabularyModel.UrlEscapeFunctionTerm;
                EdmVocabularyAnnotation annotation = new EdmVocabularyAnnotation(function, term, booleanConstant);
                annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
                model.SetVocabularyAnnotation(annotation);
            }

            OpenApiConvertSettings settings = new OpenApiConvertSettings
            {
                EnableUriEscapeFunctionCall        = enableEscaped,
                AddSingleQuotesForStringParameters = true,
            };
            ODataContext context = new ODataContext(model, settings);

            // Act
            var pathItems = context.CreatePathItems();

            // Assert
            Assert.NotNull(pathItems);
            Assert.Equal(4, pathItems.Count);

            Assert.Contains("/Customers", pathItems.Keys);
            Assert.Contains("/Customers/$count", pathItems.Keys);
            Assert.Contains("/Customers({ID})", pathItems.Keys);
            Assert.Contains(expected, pathItems.Keys);
        }