Skip to content

Commit 36a8eb6

Browse files
committed
Added additional unit tests
1 parent 25930df commit 36a8eb6

File tree

3 files changed

+693
-0
lines changed

3 files changed

+693
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This file is part of Maven Version Checker <https://github.com/StevenJDH/maven-version-checker>.
3+
* Copyright (C) 2024 Steven Jenkins De Haro.
4+
*
5+
* Maven Version Checker is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* Maven Version Checker is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with Maven Version Checker. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
using MavenVersionChecker.Action.Chaos;
20+
using MavenVersionChecker.Action.Tests.Support;
21+
using Microsoft.Extensions.Logging;
22+
using Moq;
23+
using Polly;
24+
25+
namespace MavenVersionChecker.Action.Tests.Chaos;
26+
27+
[TestFixture]
28+
public class ChaosManagerTests
29+
{
30+
// ReSharper disable InconsistentNaming
31+
private const string ASPNETCORE_ENVIRONMENT = "ASPNETCORE_ENVIRONMENT";
32+
private const string CHAOS_ENV = "Chaos";
33+
private const string CHAOS_LOG_MESSAGE = "Chaos strategies are active.\n";
34+
// ReSharper restore InconsistentNaming
35+
36+
[TearDown]
37+
public void TearDown()
38+
{
39+
Environment.SetEnvironmentVariable(ASPNETCORE_ENVIRONMENT, null);
40+
}
41+
42+
[Test, Description("Should enable chaos strategies when environment is set to chaos.")]
43+
public async Task Should_EnableChaosStrategies_When_EnvironmentIsSetToChaos()
44+
{
45+
Environment.SetEnvironmentVariable(ASPNETCORE_ENVIRONMENT, CHAOS_ENV);
46+
var mockLogger = new Mock<ILogger<ChaosManager>>();
47+
var manager = new ChaosManager(mockLogger.Object);
48+
49+
var context = ResilienceContextPool.Shared.Get();
50+
bool status = await manager.IsChaosEnabledAsync(context);
51+
ResilienceContextPool.Shared.Return(context);
52+
53+
Assert.That(status, Is.True);
54+
mockLogger.VerifyLog(LogLevel.Warning, Times.Once, CHAOS_LOG_MESSAGE);
55+
}
56+
57+
[Test, Description("Should disable chaos strategies when environment is not set to chaos.")]
58+
public async Task Should_DisableChaosStrategies_When_EnvironmentIsNotSetToChaos()
59+
{
60+
var mockLogger = new Mock<ILogger<ChaosManager>>();
61+
var manager = new ChaosManager(mockLogger.Object);
62+
63+
var context = ResilienceContextPool.Shared.Get();
64+
bool status = await manager.IsChaosEnabledAsync(context);
65+
ResilienceContextPool.Shared.Return(context);
66+
67+
Assert.That(status, Is.False);
68+
mockLogger.VerifyLog(LogLevel.Warning, Times.Never);
69+
}
70+
71+
[Test, Description("Should return injection rate greater than zero when environment is set to chaos.")]
72+
public async Task Should_ReturnInjectionRateGreaterThanZero_When_EnvironmentIsSetToChaos()
73+
{
74+
Environment.SetEnvironmentVariable(ASPNETCORE_ENVIRONMENT, CHAOS_ENV);
75+
var mockLogger = new Mock<ILogger<ChaosManager>>();
76+
var manager = new ChaosManager(mockLogger.Object);
77+
78+
var context = ResilienceContextPool.Shared.Get();
79+
double injectionRate = await manager.GetInjectionRateAsync(context);
80+
ResilienceContextPool.Shared.Return(context);
81+
82+
Assert.That(injectionRate, Is.EqualTo(0.1));
83+
mockLogger.VerifyLog(LogLevel.Warning, Times.Once, CHAOS_LOG_MESSAGE);
84+
}
85+
86+
[Test, Description("Should return injection rate of zero when environment is not set to chaos.")]
87+
public async Task Should_ReturnInjectionRateOfZero_When_EnvironmentIsNotSetToChaos()
88+
{
89+
var mockLogger = new Mock<ILogger<ChaosManager>>();
90+
var manager = new ChaosManager(mockLogger.Object);
91+
92+
var context = ResilienceContextPool.Shared.Get();
93+
double injectionRate = await manager.GetInjectionRateAsync(context);
94+
ResilienceContextPool.Shared.Return(context);
95+
96+
Assert.That(injectionRate, Is.EqualTo(0.0));
97+
mockLogger.VerifyLog(LogLevel.Warning, Times.Never);
98+
}
99+
}

MavenVersionChecker.Action.Tests/Extensions/ServiceCollectionExtensionsTests.cs

+48
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
// Ignore Spelling: Api
2020

21+
using System.Text.Json;
2122
using MavenVersionChecker.Action.Chaos;
2223
using MavenVersionChecker.Action.Data;
2324
using MavenVersionChecker.Action.Extensions;
@@ -184,4 +185,51 @@ public void Should_RemoveHttpMessageHandlerBuilderFilter_ForDependencyInjection(
184185
Assert.That(messageHandlerBuilderFilter, Is.Null);
185186
});
186187
}
188+
189+
[Test, Description("Should use SourceGenerationContext for serializing and deserializing maven query response.")]
190+
public void Should_UseSourceGenerationContext_ForSerializingAndDeserializingMavenQueryResponse()
191+
{
192+
var mavenQueryResponse = new MavenQueryResponse
193+
{
194+
Result = new QueryResult
195+
{
196+
NumberFound = 1,
197+
Artifacts = [new Artifact { LatestVersion = "999.9.9" }]
198+
}
199+
};
200+
201+
string json = JsonSerializer.Serialize(mavenQueryResponse, SourceGenerationContext.Default.MavenQueryResponse);
202+
var model = JsonSerializer.Deserialize<MavenQueryResponse>(json, SourceGenerationContext.Default.MavenQueryResponse);
203+
204+
Assert.Multiple(() =>
205+
{
206+
Assert.That(model, Is.Not.Null);
207+
Assert.That(model!.Result.NumberFound, Is.EqualTo(1));
208+
Assert.That(model!.Result.Artifacts, Has.Count.EqualTo(1));
209+
Assert.That(model!.Result.Artifacts[0].LatestVersion, Is.EqualTo("999.9.9"));
210+
});
211+
}
212+
213+
[Test, Description("Should use SourceGenerationContext for serializing and deserializing dictionaries.")]
214+
public void Should_UseSourceGenerationContext_ForSerializingAndDeserializingDictionaries()
215+
{
216+
const string expectedKey = "dependencies";
217+
const string expectedValue = "foo:bar:2.0.0";
218+
var dictionary = new Dictionary<string, List<string>>
219+
{
220+
{ expectedKey, [expectedValue] }
221+
};
222+
223+
string json = JsonSerializer.Serialize(dictionary, SourceGenerationContext.Default.DictionaryStringListString);
224+
var model = JsonSerializer.Deserialize(json, SourceGenerationContext.Default.DictionaryStringListString);
225+
226+
Assert.Multiple(() =>
227+
{
228+
Assert.That(model, Is.Not.Null);
229+
Assert.That(model, Has.Count.EqualTo(1));
230+
Assert.That(model!, Contains.Key(expectedKey));
231+
Assert.That(model!.GetValueOrDefault(expectedKey), Has.Count.EqualTo(1));
232+
Assert.That(model!.GetValueOrDefault(expectedKey)![0], Is.EqualTo(expectedValue));
233+
});
234+
}
187235
}

0 commit comments

Comments
 (0)