// ------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. // ------------------------------------------------------------ using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; using Microsoft.OData.Edm.Validation; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Properties; using Microsoft.OpenApi.OData.Tests; using Xunit; namespace Microsoft.OpenApi.OData.PathItem.Tests { public class SingletonPathItemHandlerTest { private SingletonPathItemHandler _pathItemHandler = new SingletonPathItemHandler(); [Fact] public void CreatePathItemThrowsForNullContext() { // Arrange & Act & Assert Assert.Throws("context", () => _pathItemHandler.CreatePathItem(context: null, path: new ODataPath())); } [Fact] public void CreatePathItemThrowsForNullPath() { // Arrange & Act & Assert Assert.Throws("path", () => _pathItemHandler.CreatePathItem(new ODataContext(EdmCoreModel.Instance), path: null)); } [Fact] public void CreatePathItemThrowsForNonSingletonPath() { // Arrange IEdmModel model = EdmModelHelper.BasicEdmModel; ODataContext context = new ODataContext(model); var entitySet = model.EntityContainer.FindEntitySet("People"); Assert.NotNull(entitySet); // guard var path = new ODataPath(new ODataNavigationSourceSegment(entitySet)); // Act Action test = () => _pathItemHandler.CreatePathItem(context, path); // Assert var exception = Assert.Throws(test); Assert.Equal(String.Format(SRResource.InvalidPathKindForPathItemHandler, "SingletonPathItemHandler", path.Kind), exception.Message); } [Fact] public void CreateSingletonPathItemReturnsCorrectPathItem() { // Arrange IEdmModel model = EdmModelHelper.BasicEdmModel; ODataContext context = new ODataContext(model); IEdmSingleton singleton = model.EntityContainer.FindSingleton("Me"); Assert.NotNull(singleton); // guard ODataPath path = new ODataPath(new ODataNavigationSourceSegment(singleton)); // Act var pathItem = _pathItemHandler.CreatePathItem(context, path); // Assert Assert.NotNull(pathItem); Assert.NotNull(pathItem.Operations); Assert.NotEmpty(pathItem.Operations); Assert.Equal(2, pathItem.Operations.Count); Assert.Equal(new OperationType[] { OperationType.Get, OperationType.Patch }, pathItem.Operations.Select(o => o.Key)); } [Theory] [InlineData("None")] [InlineData("Single")] [InlineData("Recursive")] public void CreateSingletonPathItemWorksForNavigationRestrictionsCapablities(string navigationType) { // Arrange string annotation = String.Format(@" Org.OData.Capabilities.V1.NavigationType/{0} ", navigationType); IEdmModel model = GetEdmModel(annotation); ODataContext context = new ODataContext(model); IEdmSingleton singleton = model.EntityContainer.FindSingleton("Me"); Assert.NotNull(singleton); // guard ODataPath path = new ODataPath(new ODataNavigationSourceSegment(singleton)); // Act var pathItem = _pathItemHandler.CreatePathItem(context, path); // Assert Assert.NotNull(pathItem); Assert.NotNull(pathItem.Operations); Assert.NotEmpty(pathItem.Operations); if (navigationType == "None") { var operation = Assert.Single(pathItem.Operations); Assert.Equal(OperationType.Patch, operation.Key); } else { Assert.Equal(2, pathItem.Operations.Count); Assert.Equal(new OperationType[] { OperationType.Get, OperationType.Patch }, pathItem.Operations.Select(o => o.Key)); } } [Fact] public void CreateSingletonPathItemWorksForUpdateRestrictionsCapablities() { // Arrange string annotation = @" "; IEdmModel model = GetEdmModel(annotation); ODataContext context = new ODataContext(model); IEdmSingleton singleton = model.EntityContainer.FindSingleton("Me"); Assert.NotNull(singleton); // guard ODataPath path = new ODataPath(new ODataNavigationSourceSegment(singleton)); // Act var pathItem = _pathItemHandler.CreatePathItem(context, path); // Assert Assert.NotNull(pathItem); Assert.NotNull(pathItem.Operations); Assert.NotEmpty(pathItem.Operations); var operation = Assert.Single(pathItem.Operations); Assert.Equal(OperationType.Get, operation.Key); } private IEdmModel GetEdmModel(string annotation) { const string template = @" {0} "; string modelText = string.Format(template, annotation); IEdmModel model; IEnumerable errors; bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out errors); Assert.True(result); return model; } } }