forked from asyncapi/saunter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
asyncapi#196 add unitTests for tofile classes
- Loading branch information
Senn Geerts
authored and
Senn Geerts
committed
Jul 10, 2024
1 parent
240c9ed
commit 715a62d
Showing
14 changed files
with
430 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project> | ||
<ItemGroup> | ||
<!-- Make it possible for NSubstitube to make substitues of internal classes --> | ||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" /> | ||
|
||
<!-- Make project internals visible to their respective .Tests project --> | ||
<InternalsVisibleTo Include="$(AssemblyName).Tests" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,3 @@ public IServiceProvider BuildServiceProvider(string startupAssembly) | |
return serviceProvider; | ||
} | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
src/AsyncAPI.Saunter.Generator.Cli/ToFile/StreamProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace AsyncAPI.Saunter.Generator.Cli.ToFile; | ||
|
||
internal interface IStreamProvider | ||
{ | ||
Stream GetStreamFor(string path); | ||
} | ||
|
||
internal class StreamProvider : IStreamProvider | ||
{ | ||
public Stream GetStreamFor(string path) => path != null ? File.Create(path) : Console.OpenStandardOutput(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
test/AsyncAPI.Saunter.Generator.Cli.Tests/ToFile/AsyncApiDocumentExtractorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
using AsyncAPI.Saunter.Generator.Cli.ToFile; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using NSubstitute; | ||
using NSubstitute.Community.Logging; | ||
using Saunter; | ||
using Saunter.AsyncApiSchema.v2; | ||
using Saunter.Serialization; | ||
using Shouldly; | ||
using Xunit.Abstractions; | ||
|
||
namespace AsyncAPI.Saunter.Generator.Cli.Tests.ToFile; | ||
|
||
public class AsyncApiDocumentExtractorTests | ||
{ | ||
private readonly AsyncApiDocumentExtractor _extractor; | ||
private readonly ILogger<AsyncApiDocumentExtractor> _logger; | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly IAsyncApiDocumentProvider _documentProvider; | ||
private readonly IOptions<AsyncApiOptions> _asyncApiOptions; | ||
private readonly IAsyncApiDocumentSerializer _documentSerializer; | ||
|
||
public AsyncApiDocumentExtractorTests(ITestOutputHelper output) | ||
{ | ||
var services = new ServiceCollection(); | ||
this._documentProvider = Substitute.For<IAsyncApiDocumentProvider>(); | ||
this._asyncApiOptions = Substitute.For<IOptions<AsyncApiOptions>>(); | ||
var options = new AsyncApiOptions(); | ||
this._asyncApiOptions.Value.Returns(options); | ||
this._documentSerializer = Substitute.For<IAsyncApiDocumentSerializer>(); | ||
services.AddSingleton(this._documentProvider); | ||
services.AddSingleton(this._asyncApiOptions); | ||
services.AddSingleton(this._documentSerializer); | ||
this._serviceProvider = services.BuildServiceProvider(); | ||
|
||
this._logger = Substitute.For<ILogger<AsyncApiDocumentExtractor>>(); | ||
this._extractor = new AsyncApiDocumentExtractor(this._logger); | ||
} | ||
|
||
[Fact] | ||
public void GetAsyncApiDocument_Null_NoMarkerAssemblies() | ||
{ | ||
var documents = this._extractor.GetAsyncApiDocument(this._serviceProvider, null).ToList(); | ||
|
||
this._logger.Received(1).CallToLog(LogLevel.Critical); | ||
} | ||
|
||
[Fact] | ||
public void GetAsyncApiDocument_Default_WithMarkerAssembly() | ||
{ | ||
this._asyncApiOptions.Value.AssemblyMarkerTypes = [typeof(AsyncApiDocumentExtractorTests)]; | ||
var doc = new AsyncApiDocument(); | ||
this._documentProvider.GetDocument(default, default).ReturnsForAnyArgs(doc); | ||
this._documentSerializer.Serialize(doc).ReturnsForAnyArgs(""" | ||
asyncapi: 2.6.0 | ||
info: | ||
title: Streetlights API | ||
"""); | ||
|
||
var documents = this._extractor.GetAsyncApiDocument(this._serviceProvider, null).ToList(); | ||
|
||
this._logger.Received(0).CallToLog(LogLevel.Critical); | ||
documents.Count.ShouldBe(1); | ||
documents[0].name.ShouldBeNull(); | ||
documents[0].document.Info.Title.ShouldBe("Streetlights API"); | ||
} | ||
|
||
[Fact] | ||
public void GetAsyncApiDocument_1NamedDocument() | ||
{ | ||
this._asyncApiOptions.Value.AssemblyMarkerTypes = [typeof(AsyncApiDocumentExtractorTests)]; | ||
var doc = new AsyncApiDocument(); | ||
this._asyncApiOptions.Value.NamedApis["service 1"] = doc; | ||
this._documentProvider.GetDocument(default, default).ReturnsForAnyArgs(doc); | ||
this._documentSerializer.Serialize(doc).ReturnsForAnyArgs(""" | ||
asyncapi: 2.6.0 | ||
info: | ||
title: Streetlights API | ||
"""); | ||
|
||
var documents = this._extractor.GetAsyncApiDocument(this._serviceProvider, null).ToList(); | ||
|
||
this._logger.Received(0).CallToLog(LogLevel.Critical); | ||
documents.Count.ShouldBe(1); | ||
documents[0].name.ShouldBe("service 1"); | ||
documents[0].document.Info.Title.ShouldBe("Streetlights API"); | ||
} | ||
|
||
[Fact] | ||
public void GetAsyncApiDocument_2NamedDocument() | ||
{ | ||
this._asyncApiOptions.Value.AssemblyMarkerTypes = [typeof(AsyncApiDocumentExtractorTests)]; | ||
var doc1 = new AsyncApiDocument { Id = "1" }; | ||
var doc2 = new AsyncApiDocument { Id = "2" }; | ||
this._asyncApiOptions.Value.NamedApis["service 1"] = doc1; | ||
this._asyncApiOptions.Value.NamedApis["service 2"] = doc2; | ||
this._documentProvider.GetDocument(Arg.Any<AsyncApiOptions>(), Arg.Is(doc1)).Returns(doc1); | ||
this._documentProvider.GetDocument(Arg.Any<AsyncApiOptions>(), Arg.Is(doc2)).Returns(doc2); | ||
this._documentSerializer.Serialize(doc1).Returns(""" | ||
asyncapi: 2.6.0 | ||
info: | ||
title: Streetlights API 1 | ||
"""); | ||
this._documentSerializer.Serialize(doc2).Returns(""" | ||
asyncapi: 2.6.0 | ||
info: | ||
title: Streetlights API 2 | ||
"""); | ||
|
||
var documents = this._extractor.GetAsyncApiDocument(this._serviceProvider, null).OrderBy(x => x.name).ToList(); | ||
|
||
this._logger.Received(0).CallToLog(LogLevel.Critical); | ||
documents.Count.ShouldBe(2); | ||
documents[0].name.ShouldBe("service 1"); | ||
documents[0].document.Info.Title.ShouldBe("Streetlights API 1"); | ||
documents[1].name.ShouldBe("service 2"); | ||
documents[1].document.Info.Title.ShouldBe("Streetlights API 2"); | ||
} | ||
|
||
[Fact] | ||
public void GetAsyncApiDocument_LogErrors() | ||
{ | ||
this._asyncApiOptions.Value.AssemblyMarkerTypes = [typeof(AsyncApiDocumentExtractorTests)]; | ||
var doc = new AsyncApiDocument(); | ||
this._documentProvider.GetDocument(default, default).ReturnsForAnyArgs(doc); | ||
this._documentSerializer.Serialize(doc).ReturnsForAnyArgs(""" | ||
asyncapi: 2.6.0 | ||
info: | ||
title: Streetlights API | ||
channels: | ||
publish/light/measured: | ||
servers: | ||
- webapi | ||
publish: | ||
operationId: MeasureLight | ||
summary: Inform about environmental lighting conditions for a particular streetlight. | ||
tags: | ||
- name: Light | ||
message: | ||
$ref: '#/components/messages/lightMeasuredEvent' | ||
"""); | ||
|
||
var documents = this._extractor.GetAsyncApiDocument(this._serviceProvider, null).ToList(); | ||
|
||
this._logger.Received(0).CallToLog(LogLevel.Critical); | ||
this._logger.Received(3).CallToLog(LogLevel.Error); | ||
this._logger.Received(0).CallToLog(LogLevel.Warning); | ||
documents.Count.ShouldBe(1); | ||
documents[0].name.ShouldBeNull(); | ||
documents[0].document.Info.Title.ShouldBe("Streetlights API"); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
test/AsyncAPI.Saunter.Generator.Cli.Tests/ToFile/EnvironmentBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System.Collections; | ||
using AsyncAPI.Saunter.Generator.Cli.ToFile; | ||
using Microsoft.Extensions.Logging; | ||
using NSubstitute; | ||
using NSubstitute.Community.Logging; | ||
using Shouldly; | ||
using Xunit.Abstractions; | ||
|
||
namespace AsyncAPI.Saunter.Generator.Cli.Tests.ToFile; | ||
|
||
public class EnvironmentBuilderTests : IDisposable | ||
{ | ||
private readonly IDictionary _variablesBefore = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process); | ||
private readonly EnvironmentBuilder _environment; | ||
private readonly ILogger<EnvironmentBuilder> _logger; | ||
|
||
public EnvironmentBuilderTests() | ||
{ | ||
this._logger = Substitute.For<ILogger<EnvironmentBuilder>>(); | ||
this._environment = new EnvironmentBuilder(this._logger); | ||
} | ||
|
||
private Dictionary<string, string> GetAddedEnvironmentVariables() | ||
{ | ||
var after = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process); | ||
return after.Cast<DictionaryEntry>().ExceptBy(this._variablesBefore.Keys.Cast<string>(), x => x.Key).ToDictionary(x => x.Key.ToString(), x => x.Value?.ToString()); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
foreach (var variable in this.GetAddedEnvironmentVariables()) | ||
{ | ||
Environment.SetEnvironmentVariable(variable.Key, null, EnvironmentVariableTarget.Process); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
public void EmptyEnvStringProvided(string env) | ||
{ | ||
this._environment.SetEnvironmentVariables(env); | ||
|
||
this._logger.ReceivedCalls().Count().ShouldBe(0); | ||
this.GetAddedEnvironmentVariables().ShouldBeEmpty(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("env1=val1", 1)] | ||
[InlineData("a=b,b=c", 2)] | ||
public void ValidEnvStringProvided(string env, int expectedSets) | ||
{ | ||
this._environment.SetEnvironmentVariables(env); | ||
|
||
this._logger.Received(expectedSets).CallToLog(LogLevel.Debug); | ||
this.GetAddedEnvironmentVariables().ShouldNotBeEmpty(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(",", 2)] | ||
[InlineData(",,,,", 5)] | ||
[InlineData("=a", 1)] | ||
[InlineData("b", 1)] | ||
[InlineData("=", 1)] | ||
[InlineData("====", 1)] | ||
public void InvalidEnvStringProvided(string env, int expectedSets) | ||
{ | ||
this._environment.SetEnvironmentVariables(env); | ||
|
||
this._logger.Received(expectedSets).CallToLog(LogLevel.Critical); | ||
this.GetAddedEnvironmentVariables().ShouldBeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void ValidateEnvValues() | ||
{ | ||
this._environment.SetEnvironmentVariables("ENV=1,,Test=two"); | ||
|
||
Environment.GetEnvironmentVariable("ENV").ShouldBe("1"); | ||
Environment.GetEnvironmentVariable("Test").ShouldBe("two"); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
[InlineData(" ")] | ||
public void EmptyValueDeletesEnvValue(string value) | ||
{ | ||
this._environment.SetEnvironmentVariables($"ENV=1,,ENV={value}"); | ||
|
||
Environment.GetEnvironmentVariable("ENV").ShouldBe(null); | ||
} | ||
} |
Oops, something went wrong.