Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/HealthChecks/HealthCheckResultsTests.cs
Nikolaj Geisle 7aeb400fce V10: fix build warnings in test projects (#12509)
* Run code cleanup

* Dotnet format benchmarks project

* Fix up Test.Common

* Run dotnet format + manual cleanup

* Run code cleanup for unit tests

* Run dotnet format

* Fix up errors

* Manual cleanup of Unit test project

* Update tests/Umbraco.Tests.Benchmarks/HexStringBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/TestDbMeta.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Benchmarks/TypeFinderBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Umbraco.Core/Events/EventAggregatorTests.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Fix according to review

* Fix after merge

* Fix errors

Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk>
Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
Co-authored-by: Zeegaan <nge@umbraco.dk>
2022-06-21 08:09:38 +02:00

147 lines
5.9 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;
using Umbraco.Cms.Core.HealthChecks;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.HealthChecks;
[TestFixture]
public class HealthCheckResultsTests
{
[HealthCheck("CFD6FC34-59C9-4402-B55F-C8BC96B628A1", "Stub check")]
public abstract class StubHealthCheck : HealthCheck
{
private readonly string _message;
private readonly StatusResultType _resultType;
public StubHealthCheck(StatusResultType resultType, string message)
{
_resultType = resultType;
_message = message;
}
public override HealthCheckStatus ExecuteAction(HealthCheckAction action) =>
throw new NotImplementedException();
public override async Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
new List<HealthCheckStatus> { new(_message) { ResultType = _resultType } };
}
[HealthCheck("CFD6FC34-59C9-4402-B55F-C8BC96B628A1", "Stub check 1")]
public class StubHealthCheck1 : StubHealthCheck
{
public StubHealthCheck1(StatusResultType resultType, string message)
: base(resultType, message)
{
}
}
[HealthCheck("CFD6FC34-59C9-4402-B55F-C8BC96B628A2", "Stub check 2")]
public class StubHealthCheck2 : StubHealthCheck
{
public StubHealthCheck2(StatusResultType resultType, string message)
: base(resultType, message)
{
}
}
[HealthCheck("CFD6FC34-59C9-4402-B55F-C8BC96B628A3", "Stub check 3")]
public class StubHealthCheck3 : StubHealthCheck
{
public StubHealthCheck3(StatusResultType resultType, string message)
: base(resultType, message)
{
}
public override async Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
throw new Exception("Check threw exception");
}
[Test]
public async Task HealthCheckResults_WithSuccessfulChecks_ReturnsCorrectResultDescription()
{
var checks = new List<HealthCheck>
{
new StubHealthCheck1(StatusResultType.Success, "First check was successful"),
new StubHealthCheck2(StatusResultType.Success, "Second check was successful"),
};
var results = await HealthCheckResults.Create(checks);
Assert.IsTrue(results.AllChecksSuccessful);
var resultAsMarkdown = results.ResultsAsMarkDown(HealthCheckNotificationVerbosity.Summary);
Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 1' all completed successfully.") > -1);
Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 2' all completed successfully.") > -1);
}
[Test]
public async Task HealthCheckResults_WithFailingChecks_ReturnsCorrectResultDescription()
{
var checks = new List<HealthCheck>
{
new StubHealthCheck1(StatusResultType.Success, "First check was successful"),
new StubHealthCheck2(StatusResultType.Error, "Second check was not successful"),
};
var results = await HealthCheckResults.Create(checks);
Assert.IsFalse(results.AllChecksSuccessful);
var resultAsMarkdown = results.ResultsAsMarkDown(HealthCheckNotificationVerbosity.Summary);
Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 1' all completed successfully.") > -1);
Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 2' completed with errors.") > -1);
}
[Test]
public async Task HealthCheckResults_WithErroringCheck_ReturnsCorrectResultDescription()
{
var checks = new List<HealthCheck>
{
new StubHealthCheck1(StatusResultType.Success, "First check was successful"),
new StubHealthCheck3(StatusResultType.Error, "Third check was not successful"),
new StubHealthCheck2(StatusResultType.Error, "Second check was not successful"),
};
var results = await HealthCheckResults.Create(checks);
Assert.IsFalse(results.AllChecksSuccessful);
var resultAsMarkdown = results.ResultsAsMarkDown(HealthCheckNotificationVerbosity.Summary);
Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 1' all completed successfully.") > -1);
Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 2' completed with errors.") > -1);
Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 3' completed with errors.") > -1);
}
[Test]
public async Task HealthCheckResults_WithSummaryVerbosity_ReturnsCorrectResultDescription()
{
var checks = new List<HealthCheck>
{
new StubHealthCheck1(StatusResultType.Success, "First check was successful"),
new StubHealthCheck2(StatusResultType.Success, "Second check was successful"),
};
var results = await HealthCheckResults.Create(checks);
var resultAsMarkdown = results.ResultsAsMarkDown(HealthCheckNotificationVerbosity.Summary);
Assert.IsTrue(resultAsMarkdown.IndexOf("Result: 'Success'" + Environment.NewLine) > -1);
}
[Test]
public async Task HealthCheckResults_WithDetailedVerbosity_ReturnsCorrectResultDescription()
{
var checks = new List<HealthCheck>
{
new StubHealthCheck1(StatusResultType.Success, "First check was successful"),
new StubHealthCheck2(StatusResultType.Success, "Second check was successful"),
};
var results = await HealthCheckResults.Create(checks);
var resultAsMarkdown = results.ResultsAsMarkDown(HealthCheckNotificationVerbosity.Detailed);
Assert.IsFalse(resultAsMarkdown.IndexOf("Result: 'Success'" + Environment.NewLine) > -1);
Assert.IsTrue(resultAsMarkdown.IndexOf("Result: 'Success', Message: 'First check was successful'" +
Environment.NewLine) > -1);
}
}