From 6a7203d08820e8cfb03f015bedbdc8fcf8b2839d Mon Sep 17 00:00:00 2001 From: Sam Xu Date: Mon, 27 Nov 2017 10:22:09 -0800 Subject: [PATCH] add unit test for OpenApiDocumentGenerator --- .../Generator/OpenApiComponentsGenerator.cs | 6 +++ .../Generator/OpenApiDocumentGenerator.cs | 6 ++- .../Generator/OpenApiInfoGenerator.cs | 2 +- .../Generator/OpenApiTagGenerator.cs | 5 +- .../OpenApiDocumentGeneratorTests.cs | 48 +++++++++++++++++++ .../Generator/OpenApiTagGeneratorTests.cs | 1 - 6 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiDocumentGeneratorTests.cs diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiComponentsGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiComponentsGenerator.cs index 9d0043c..70cee72 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiComponentsGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiComponentsGenerator.cs @@ -46,11 +46,17 @@ namespace Microsoft.OpenApi.OData.Generator // It allows defining responses that can be reused across operations of the service. Responses = context.CreateResponses(), + // Make others as null. RequestBodies = null, + Examples = null, + SecuritySchemes = null, + Links = null, + Callbacks = null, + Extensions = null }; } diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiDocumentGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiDocumentGenerator.cs index 643a5d4..67d83a0 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiDocumentGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiDocumentGenerator.cs @@ -46,7 +46,11 @@ namespace Microsoft.OpenApi.OData.Generator Paths = context.CreatePaths(), - Components = context.CreateComponents() + Components = context.CreateComponents(), + + SecurityRequirements = null, + + ExternalDocs = null }; } } diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiInfoGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiInfoGenerator.cs index 2edd445..dff50f1 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiInfoGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiInfoGenerator.cs @@ -78,7 +78,7 @@ namespace Microsoft.OpenApi.OData.Generator // TODO: https://github.com/Microsoft/OpenAPI.NET.OData/issues/2 // or the entity container - if (context.Model.EntityContainer != null) + if (context.EntityContainer != null) { string longDescription = context.Model.GetLongDescriptionAnnotation(context.Model.EntityContainer); if (longDescription != null) diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiTagGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiTagGenerator.cs index bf0c0ac..9c571da 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiTagGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiTagGenerator.cs @@ -36,7 +36,7 @@ namespace Microsoft.OpenApi.OData.Generator // and it optionally can contain the field description, whose value is the value of the unqualified annotation // Core.Description of the entity set or singleton. IList tags = new List(); - if (context.Model.EntityContainer != null) + if (context.EntityContainer != null) { foreach (IEdmEntityContainerElement element in context.Model.EntityContainer.Elements) { @@ -81,8 +81,7 @@ namespace Microsoft.OpenApi.OData.Generator } } - // Tags is optional. - return tags.Any() ? tags : null; + return tags; } private static OpenApiTag CreateOperationImportTag(this ODataContext context, IEdmOperationImport operationImport) diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiDocumentGeneratorTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiDocumentGeneratorTests.cs new file mode 100644 index 0000000..187b150 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiDocumentGeneratorTests.cs @@ -0,0 +1,48 @@ +// ------------------------------------------------------------ +// 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 Microsoft.OData.Edm; +using Microsoft.OpenApi.OData.Tests; +using Xunit; + +namespace Microsoft.OpenApi.OData.Generator.Tests +{ + public class OpenApiDocumentGeneratorTest + { + [Fact] + public void CreateDocumentThrowArgumentNullContext() + { + // Arrange + ODataContext context = null; + + // Act & Assert + Assert.Throws("context", () => context.CreateDocument()); + } + + [Fact] + public void CreateDocumentReturnsForEmptyModel() + { + // Arrange + IEdmModel model = EdmModelHelper.EmptyModel; + ODataContext context = new ODataContext(model); + + // Act + var document = context.CreateDocument(); + + // Assert + Assert.NotNull(document); + Assert.Equal("3.0.0", document.SpecVersion.ToString()); + Assert.NotNull(document.Info); + Assert.NotNull(document.Tags); + Assert.NotNull(document.Servers); + Assert.NotNull(document.Paths); + Assert.NotNull(document.Components); + + Assert.Null(document.ExternalDocs); + Assert.Null(document.SecurityRequirements); + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiTagGeneratorTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiTagGeneratorTests.cs index 2df1fcd..0fbca2f 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiTagGeneratorTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiTagGeneratorTests.cs @@ -5,7 +5,6 @@ using System; using System.Linq; -using Microsoft.OData.Edm; using Microsoft.OpenApi.OData.Tests; using Xunit;