Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/HostedServices/ServerRegistration/TouchServerTaskTests.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

94 lines
3.4 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.HostedServices.ServerRegistration;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.HostedServices.ServerRegistration
{
[TestFixture]
public class TouchServerTaskTests
{
private Mock<IServerRegistrationService> _mockServerRegistrationService;
private const string ApplicationUrl = "https://mysite.com/";
private readonly TimeSpan _staleServerTimeout = TimeSpan.FromMinutes(2);
[TestCase(RuntimeLevel.Boot)]
[TestCase(RuntimeLevel.Install)]
[TestCase(RuntimeLevel.Unknown)]
[TestCase(RuntimeLevel.Upgrade)]
[TestCase(RuntimeLevel.BootFailed)]
public async Task Does_Not_Execute_When_Runtime_State_Is_Not_Run(RuntimeLevel runtimeLevel)
{
TouchServerTask sut = CreateTouchServerTask(runtimeLevel: runtimeLevel);
await sut.PerformExecuteAsync(null);
VerifyServerNotTouched();
}
[Test]
public async Task Does_Not_Execute_When_Application_Url_Is_Not_Available()
{
TouchServerTask sut = CreateTouchServerTask(applicationUrl: string.Empty);
await sut.PerformExecuteAsync(null);
VerifyServerNotTouched();
}
[Test]
public async Task Executes_And_Touches_Server()
{
TouchServerTask sut = CreateTouchServerTask();
await sut.PerformExecuteAsync(null);
VerifyServerTouched();
}
private TouchServerTask CreateTouchServerTask(RuntimeLevel runtimeLevel = RuntimeLevel.Run, string applicationUrl = ApplicationUrl)
{
var mockRequestAccessor = new Mock<IHostingEnvironment>();
mockRequestAccessor.SetupGet(x => x.ApplicationMainUrl).Returns(!string.IsNullOrEmpty(applicationUrl) ? new Uri(ApplicationUrl) : null);
var mockRunTimeState = new Mock<IRuntimeState>();
mockRunTimeState.SetupGet(x => x.Level).Returns(runtimeLevel);
var mockLogger = new Mock<ILogger<TouchServerTask>>();
_mockServerRegistrationService = new Mock<IServerRegistrationService>();
var settings = new GlobalSettings
{
DatabaseServerRegistrar = new DatabaseServerRegistrarSettings
{
StaleServerTimeout = _staleServerTimeout,
}
};
return new TouchServerTask(
mockRunTimeState.Object,
_mockServerRegistrationService.Object,
mockRequestAccessor.Object,
mockLogger.Object,
Options.Create(settings));
}
private void VerifyServerNotTouched() => VerifyServerTouchedTimes(Times.Never());
private void VerifyServerTouched() => VerifyServerTouchedTimes(Times.Once());
private void VerifyServerTouchedTimes(Times times) => _mockServerRegistrationService
.Verify(
x => x.TouchServer(
It.Is<string>(y => y == ApplicationUrl),
It.Is<TimeSpan>(y => y == _staleServerTimeout)),
times);
}
}