Skip to content

Commit e0d08f8

Browse files
committed
Added tests for mermaid diagrams
1 parent 3f59784 commit e0d08f8

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,13 @@ private static void LogErrors(ILogger<OpenApiService> logger, ReadResult result)
549549
}
550550
}
551551

552-
private static void WriteTreeDocument(string openapi, OpenApiDocument document, StreamWriter writer)
552+
internal static void WriteTreeDocument(string openapiUrl, OpenApiDocument document, StreamWriter writer)
553553
{
554554
var rootNode = OpenApiUrlTreeNode.Create(document, "main");
555555

556556
writer.WriteLine("# " + document.Info.Title);
557557
writer.WriteLine();
558-
writer.WriteLine("OpenAPI: " + openapi);
558+
writer.WriteLine("OpenAPI: " + openapiUrl);
559559

560560
writer.WriteLine(@"<div>");
561561
// write a span for each mermaidcolorscheme

src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public void AddAdditionalData(Dictionary<string, List<string>> additionalData)
242242
/// Write tree as Mermaid syntax
243243
/// </summary>
244244
/// <param name="writer">StreamWriter to write the Mermaid content to</param>
245-
public void WriteMermaid(StreamWriter writer)
245+
public void WriteMermaid(TextWriter writer)
246246
{
247247
writer.WriteLine("graph LR");
248248
foreach (var style in MermaidNodeStyles)
@@ -269,7 +269,7 @@ public void WriteMermaid(StreamWriter writer)
269269
{ "OTHER", new MermaidNodeStyle("White", MermaidNodeShape.SquareCornerRectangle) },
270270
};
271271

272-
private static void ProcessNode(OpenApiUrlTreeNode node, StreamWriter writer)
272+
private static void ProcessNode(OpenApiUrlTreeNode node, TextWriter writer)
273273
{
274274
var path = string.IsNullOrEmpty(node.Path) ? "/" : SanitizeMermaidNode(node.Path);
275275
var methods = GetMethods(node);

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System.Text;
45
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.Logging;
57
using Microsoft.OpenApi.Hidi;
8+
using Microsoft.OpenApi.Models;
69
using Microsoft.OpenApi.OData;
710
using Microsoft.OpenApi.Services;
11+
using Microsoft.VisualStudio.TestPlatform.Utilities;
812
using Xunit;
913

1014
namespace Microsoft.OpenApi.Tests.Services
@@ -71,5 +75,24 @@ public void ReturnOpenApiConvertSettingsWhenSettingsFileIsProvided(string filePa
7175
Assert.NotNull(settings);
7276
}
7377
}
78+
79+
[Fact]
80+
public void ShowCommandGeneratesMermaidDiagram()
81+
{
82+
var openApiDoc = new OpenApiDocument();
83+
openApiDoc.Info = new OpenApiInfo
84+
{
85+
Title = "Test",
86+
Version = "1.0.0"
87+
};
88+
var stream = new MemoryStream();
89+
using var writer = new StreamWriter(stream);
90+
OpenApiService.WriteTreeDocument("https://example.org/openapi.json", openApiDoc, writer);
91+
writer.Flush();
92+
stream.Position = 0;
93+
using var reader = new StreamReader(stream);
94+
var output = reader.ReadToEnd();
95+
Assert.Contains("graph LR", output);
96+
}
7497
}
7598
}

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ namespace Microsoft.OpenApi.Services
11181118
public void Attach(Microsoft.OpenApi.Models.OpenApiDocument doc, string label) { }
11191119
public Microsoft.OpenApi.Services.OpenApiUrlTreeNode Attach(string path, Microsoft.OpenApi.Models.OpenApiPathItem pathItem, string label) { }
11201120
public bool HasOperations(string label) { }
1121-
public void WriteMermaid(System.IO.StreamWriter writer) { }
1121+
public void WriteMermaid(System.IO.TextWriter writer) { }
11221122
public static Microsoft.OpenApi.Services.OpenApiUrlTreeNode Create() { }
11231123
public static Microsoft.OpenApi.Services.OpenApiUrlTreeNode Create(Microsoft.OpenApi.Models.OpenApiDocument doc, string label) { }
11241124
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
graph LR
2+
classDef GET fill:lightSteelBlue,stroke:#333,stroke-width:2px
3+
classDef POST fill:Lightcoral,stroke:#333,stroke-width:2px
4+
classDef GET_POST fill:forestGreen,stroke:#333,stroke-width:2px
5+
classDef DELETE_GET_PATCH fill:yellowGreen,stroke:#333,stroke-width:2px
6+
classDef DELETE_GET_PATCH_PUT fill:oliveDrab,stroke:#333,stroke-width:2px
7+
classDef DELETE_GET_PUT fill:olive,stroke:#333,stroke-width:2px
8+
classDef DELETE_GET fill:DarkSeaGreen,stroke:#333,stroke-width:2px
9+
classDef DELETE fill:Tomato,stroke:#333,stroke-width:2px
10+
classDef OTHER fill:White,stroke:#333,stroke-width:2px
11+
/["/"] --> /houses["houses"]
12+
class /houses OTHER
13+
/["/"] --> /cars["cars"]
14+
class /cars OTHER
15+
class / OTHER

test/Microsoft.OpenApi.Tests/Services/OpenApiUrlTreeNodeTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Threading.Tasks;
68
using Microsoft.OpenApi.Models;
79
using Microsoft.OpenApi.Services;
10+
using VerifyXunit;
811
using Xunit;
912

1013
namespace Microsoft.OpenApi.Tests.Services
1114
{
15+
[UsesVerify]
1216
public class OpenApiUrlTreeNodeTests
1317
{
1418
private OpenApiDocument OpenApiDocumentSample_1 => new OpenApiDocument()
@@ -443,5 +447,21 @@ public void ThrowsArgumentNullExceptionForNullArgumentInAddAdditionalDataMethod(
443447

444448
Assert.Throws<ArgumentNullException>(() => rootNode.AddAdditionalData(null));
445449
}
450+
451+
[Fact]
452+
public async Task VerifyDiagramFromSampleOpenAPI()
453+
{
454+
var doc1 = OpenApiDocumentSample_1;
455+
456+
var label1 = "personal";
457+
var rootNode = OpenApiUrlTreeNode.Create(doc1, label1);
458+
459+
var writer = new StringWriter();
460+
rootNode.WriteMermaid(writer);
461+
writer.Flush();
462+
var diagram = writer.GetStringBuilder().ToString();
463+
464+
await Verifier.Verify(diagram);
465+
}
446466
}
447467
}

0 commit comments

Comments
 (0)