From 2f924eda902f3f2e6dac818ee42eaa2397184b80 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 28 Mar 2024 11:04:18 -0400 Subject: [PATCH] - adds unit tests for plugin generation service Signed-off-by: Vincent Biret --- .../Plugins/PluginsGenerationServiceTests.cs | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/Kiota.Builder.Tests/Plugins/PluginsGenerationServiceTests.cs diff --git a/tests/Kiota.Builder.Tests/Plugins/PluginsGenerationServiceTests.cs b/tests/Kiota.Builder.Tests/Plugins/PluginsGenerationServiceTests.cs new file mode 100644 index 0000000000..74be5b809d --- /dev/null +++ b/tests/Kiota.Builder.Tests/Plugins/PluginsGenerationServiceTests.cs @@ -0,0 +1,64 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Threading.Tasks; +using Kiota.Builder.Configuration; +using Kiota.Builder.Plugins; +using Microsoft.Extensions.Logging; +using Microsoft.OpenApi.Services; +using Moq; +using Xunit; + +namespace Kiota.Builder.Tests.Plugins; +public sealed class PluginsGenerationServiceTests : IDisposable +{ + private readonly HttpClient _httpClient = new(); + [Fact] + public void Defensive() + { + Assert.Throws(() => new PluginsGenerationService(null, OpenApiUrlTreeNode.Create(), new())); + Assert.Throws(() => new PluginsGenerationService(new(), null, new())); + Assert.Throws(() => new PluginsGenerationService(new(), OpenApiUrlTreeNode.Create(), null)); + } + + public void Dispose() + { + _httpClient.Dispose(); + } + + [Fact] + public async Task GeneratesManifest() + { + var simpleDescriptionContent = @"openapi: 3.0.0 +info: + title: test + version: 1.0 +paths: + /test: + get: + responses: + '200': + description: test"; + var simpleDescriptionPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()) + ".yaml"; + await File.WriteAllTextAsync(simpleDescriptionPath, simpleDescriptionContent); + var mockLogger = new Mock>(); + var openAPIDocumentDS = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object); + var outputDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + var generationConfiguration = new GenerationConfiguration + { + OutputPath = outputDirectory, + OpenAPIFilePath = "openapiPath", + PluginTypes = [PluginType.APIManifest], + ClientClassName = "client", + }; + var (openAPIDocumentStream, _) = await openAPIDocumentDS.LoadStreamAsync(simpleDescriptionPath, generationConfiguration, null, false); + var openApiDocument = await openAPIDocumentDS.GetDocumentFromStreamAsync(openAPIDocumentStream, generationConfiguration); + var urlTreeNode = OpenApiUrlTreeNode.Create(openApiDocument, Constants.DefaultOpenApiLabel); + + var pluginsGenerationService = new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration); + await pluginsGenerationService.GenerateManifestAsync(); + + Assert.True(File.Exists(Path.Combine(outputDirectory, "manifest.json"))); + Assert.True(File.Exists(Path.Combine(outputDirectory, "openapi.yml"))); + } +}