Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Omondi committed Jun 3, 2024
1 parent c0366a7 commit 4cf1773
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Plugins/PluginsGenerationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private OpenApiDocument GetDocumentWithTrimmedComponentsAndResponses(OpenApiDocu
if (string.IsNullOrEmpty(doc.Info?.Version)) // filtering fails if there's no version.
return doc;

//empty out the responses with a single 2XX
//empty out all the responses with a single empty 2XX
foreach (var operation in doc.Paths.SelectMany(static item => item.Value.Operations.Values))
{
operation.Responses = new OpenApiResponses()
Expand All @@ -130,7 +130,7 @@ private OpenApiDocument GetDocumentWithTrimmedComponentsAndResponses(OpenApiDocu
};
}

// remove unused components
// remove unused components using the OpenApi.Net
var requestUrls = new Dictionary<string, List<string>>();
var basePath = doc.GetAPIRootUrl(Configuration.OpenAPIFilePath);
foreach (var path in doc.Paths.Where(static path => path.Value.Operations.Count > 0))
Expand Down
111 changes: 111 additions & 0 deletions tests/Kiota.Builder.Tests/Plugins/PluginsGenerationServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Kiota.Builder.Configuration;
using Kiota.Builder.Plugins;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using Microsoft.OpenApi.Services;
using Microsoft.Plugins.Manifest;
using Moq;
Expand Down Expand Up @@ -109,4 +111,113 @@ public async Task GeneratesManifest()
private const string ManifestFileName = "client-microsoft.json";
private const string OpenAIPluginFileName = "openai-plugins.json";
private const string OpenApiFileName = "client-openapi.yml";

[Fact]
public async Task GeneratesManifestAndCleansUpInputDescription()
{
var simpleDescriptionContent = @"openapi: 3.0.0
info:
title: test
version: 1.0
x-test-root-extension: test
servers:
- url: http://localhost/
description: There's no place like home
paths:
/test:
get:
description: description for test path
responses:
'200':
description: test
'400':
description: client error response
/test/{id}:
get:
description: description for test path with id
operationId: test.WithId
parameters:
- name: id
in: path
required: true
description: The id of the test
schema:
type: integer
format: int32
responses:
'200':
description: test
'500':
description: api error response
components:
schemas:
microsoft.graph.entity:
title: entity
required:
- '@odata.type'
type: object
properties:
id:
type: string
'@odata.type':
type: string";
var workingDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var simpleDescriptionPath = Path.Combine(workingDirectory) + "description.yaml";
await File.WriteAllTextAsync(simpleDescriptionPath, simpleDescriptionContent);
var mockLogger = new Mock<ILogger<PluginsGenerationService>>();
var openAPIDocumentDS = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object);
var outputDirectory = Path.Combine(workingDirectory, "output");
var generationConfiguration = new GenerationConfiguration
{
OutputPath = outputDirectory,
OpenAPIFilePath = "openapiPath",
PluginTypes = [PluginType.Microsoft],
ClientClassName = "client",
ApiRootUrl = "http://localhost/", //Kiota builder would set this for us
};
var (openAPIDocumentStream, _) = await openAPIDocumentDS.LoadStreamAsync(simpleDescriptionPath, generationConfiguration, null, false);
var openApiDocument = await openAPIDocumentDS.GetDocumentFromStreamAsync(openAPIDocumentStream, generationConfiguration);
KiotaBuilder.CleanupOperationIdForPlugins(openApiDocument);
var urlTreeNode = OpenApiUrlTreeNode.Create(openApiDocument, Constants.DefaultOpenApiLabel);

var pluginsGenerationService = new PluginsGenerationService(openApiDocument, urlTreeNode, generationConfiguration, workingDirectory);
await pluginsGenerationService.GenerateManifestAsync();

Assert.True(File.Exists(Path.Combine(outputDirectory, ManifestFileName)));
Assert.True(File.Exists(Path.Combine(outputDirectory, OpenApiFileName)));

// Validate the v2 plugin
var manifestContent = await File.ReadAllTextAsync(Path.Combine(outputDirectory, ManifestFileName));
using var jsonDocument = JsonDocument.Parse(manifestContent);
var resultingManifest = PluginManifestDocument.Load(jsonDocument.RootElement);
Assert.NotNull(resultingManifest.Document);
Assert.Equal(OpenApiFileName, resultingManifest.Document.Runtimes.OfType<OpenApiRuntime>().First().Spec.Url);
Assert.Equal(2, resultingManifest.Document.Functions.Count);// all functions are generated despite missing operationIds
Assert.Empty(resultingManifest.Problems);// no problems are expected with names

var openApiReader = new OpenApiStreamReader();

// Validate the original file.
var originalOpenApiFile = File.OpenRead(simpleDescriptionPath);
var originalDocument = openApiReader.Read(originalOpenApiFile, out var originalDiagnostic);
Assert.Empty(originalDiagnostic.Errors);

Assert.Single(originalDocument.Components.Schemas);// one schema originally
Assert.Single(originalDocument.Extensions); // single unsupported extension at root
Assert.Equal(2, originalDocument.Paths.Count); // document has only two paths
Assert.Equal(2, originalDocument.Paths["/test"].Operations[OperationType.Get].Responses.Count); // 2 responses originally
Assert.Equal(2, originalDocument.Paths["/test/{id}"].Operations[OperationType.Get].Responses.Count); // 2 responses originally

// Validate the output open api file
var resultOpenApiFile = File.OpenRead(Path.Combine(outputDirectory, OpenApiFileName));
var resultDocument = openApiReader.Read(resultOpenApiFile, out var diagnostic);
Assert.Empty(diagnostic.Errors);

// Assertions / validations
Assert.Empty(resultDocument.Components.Schemas);// no schema is referenced. so ensure they are all removed
Assert.Empty(resultDocument.Extensions); // no extension at root (unsupported extension is removed)
Assert.Equal(2, resultDocument.Paths.Count); // document has only two paths
Assert.Single(resultDocument.Paths["/test"].Operations[OperationType.Get].Responses); // other responses are removed from the document
Assert.Single(resultDocument.Paths["/test/{id}"].Operations[OperationType.Get].Responses); // 2 responses originally
}
}

0 comments on commit 4cf1773

Please sign in to comment.