generated from DFE-Digital/govuk-dotnet-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/picture-card
- Loading branch information
Showing
71 changed files
with
1,077 additions
and
151 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
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
3 changes: 1 addition & 2 deletions
3
Childrens-Social-Care-CPD-Tests/Childrens-Social-Care-CPD-Tests.csproj
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
72 changes: 72 additions & 0 deletions
72
Childrens-Social-Care-CPD-Tests/Configuration/FeaturesConfigBackgroundServiceTests.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,72 @@ | ||
using Childrens_Social_Care_CPD.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
namespace Childrens_Social_Care_CPD_Tests.Configuration; | ||
|
||
public class FeaturesConfigBackgroundServiceTests | ||
{ | ||
private ILogger<FeaturesConfigBackgroundService> _logger; | ||
private IApplicationConfiguration _applicationConfiguration; | ||
private IFeaturesConfigUpdater _featuresConfigUpdater; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_logger = Substitute.For<ILogger<FeaturesConfigBackgroundService>>(); | ||
|
||
_applicationConfiguration = Substitute.For<IApplicationConfiguration>(); | ||
_featuresConfigUpdater = Substitute.For<IFeaturesConfigUpdater>(); | ||
} | ||
|
||
[TestCase(1)] | ||
[TestCase(2)] | ||
public async Task Calls_Updater_At_Specified_Interval(int interval) | ||
{ | ||
// arrange | ||
_applicationConfiguration.FeaturePollingInterval.Returns(interval); | ||
var featuresConfigBackgroundService = new FeaturesConfigBackgroundService( | ||
_logger, | ||
_applicationConfiguration, | ||
_featuresConfigUpdater | ||
); | ||
|
||
// act | ||
using (var cancellationTokenSource = new CancellationTokenSource()) | ||
{ | ||
var task = featuresConfigBackgroundService.StartAsync(cancellationTokenSource.Token); | ||
await Task.Delay((int)(interval * 1100)); | ||
cancellationTokenSource.Cancel(); | ||
task.Wait(); | ||
} | ||
|
||
// assert | ||
await _featuresConfigUpdater.Received(1).UpdateFeaturesAsync(Arg.Any<CancellationToken>()); | ||
} | ||
|
||
[Test] | ||
public async Task Returns_If_Interval_Is_Zero() | ||
{ | ||
// arrange | ||
_applicationConfiguration.FeaturePollingInterval.Returns(0); | ||
var featuresConfigBackgroundService = new FeaturesConfigBackgroundService( | ||
_logger, | ||
_applicationConfiguration, | ||
_featuresConfigUpdater | ||
); | ||
|
||
// act | ||
using (var cancellationTokenSource = new CancellationTokenSource()) | ||
{ | ||
var task = featuresConfigBackgroundService.StartAsync(cancellationTokenSource.Token); | ||
await Task.Delay(50); | ||
cancellationTokenSource.Cancel(); | ||
task.Wait(); | ||
} | ||
|
||
// assert | ||
await _featuresConfigUpdater.DidNotReceive().UpdateFeaturesAsync(Arg.Any<CancellationToken>()); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Childrens-Social-Care-CPD-Tests/Configuration/FeaturesConfigTests.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,47 @@ | ||
using Childrens_Social_Care_CPD.Configuration; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace Childrens_Social_Care_CPD_Tests.Configuration; | ||
|
||
public class FeaturesConfigTests | ||
{ | ||
[Test] | ||
public void UpdateFeaturesConfig_Adds_New_Feature() | ||
{ | ||
// arrange | ||
var featureName = "foo"; | ||
var featuresConfig = new FeaturesConfig(); | ||
|
||
// act | ||
featuresConfig.AddOrUpdateFeature(featureName, true); | ||
|
||
// assert | ||
featuresConfig.IsEnabled(featureName).Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void IsEnabled_Returns_False_For_Non_Existant_Features() | ||
{ | ||
// arrange | ||
var featuresConfig = new FeaturesConfig(); | ||
|
||
// assert | ||
featuresConfig.IsEnabled("foo").Should().BeFalse(); | ||
} | ||
|
||
[Test] | ||
public void UpdateFeaturesConfig_Updates_Features() | ||
{ | ||
// arrange | ||
var featureName = "foo"; | ||
var featuresConfig = new FeaturesConfig(); | ||
|
||
// act | ||
featuresConfig.AddOrUpdateFeature(featureName, false); | ||
featuresConfig.AddOrUpdateFeature(featureName, true); | ||
|
||
// assert | ||
featuresConfig.IsEnabled("foo").Should().BeTrue(); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
Childrens-Social-Care-CPD-Tests/Configuration/FeaturesConfigUpdaterTests.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,116 @@ | ||
using Childrens_Social_Care_CPD.Configuration; | ||
using Childrens_Social_Care_CPD.Contentful; | ||
using Childrens_Social_Care_CPD.Contentful.Models; | ||
using Contentful.Core.Models; | ||
using Contentful.Core.Search; | ||
using Microsoft.Extensions.Logging; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using NSubstitute.ExceptionExtensions; | ||
|
||
namespace Childrens_Social_Care_CPD_Tests.Configuration; | ||
|
||
public class FeaturesConfigurationUpdaterTest | ||
{ | ||
private ILogger<FeaturesConfigUpdater> _logger; | ||
private ICpdContentfulClient _contentfulClient; | ||
private IFeaturesConfig _featuresConfig; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_logger = Substitute.For<ILogger<FeaturesConfigUpdater>>(); | ||
_contentfulClient = Substitute.For<ICpdContentfulClient>(); | ||
_featuresConfig = Substitute.For<IFeaturesConfig>(); | ||
} | ||
|
||
[Test] | ||
public async Task Updates_Features() | ||
{ | ||
// arrange | ||
var featureName = "foo"; | ||
var applicationFeatures = new ApplicationFeatures | ||
{ | ||
Features = new List<ApplicationFeature> | ||
{ | ||
new ApplicationFeature | ||
{ | ||
Name = featureName, | ||
IsEnabled = true | ||
} | ||
} | ||
}; | ||
|
||
_contentfulClient | ||
.GetEntries(Arg.Any<QueryBuilder<ApplicationFeatures>>(), Arg.Any<CancellationToken>()) | ||
.Returns( | ||
new ContentfulCollection<ApplicationFeatures> | ||
{ | ||
Items = new List<ApplicationFeatures> { | ||
applicationFeatures | ||
} | ||
} | ||
); | ||
|
||
var featuresConfigUpdater = new FeaturesConfigUpdater(_logger, _contentfulClient, _featuresConfig); | ||
|
||
// act | ||
using (var cancellationTokenSource = new CancellationTokenSource()) | ||
{ | ||
await featuresConfigUpdater.UpdateFeaturesAsync(cancellationTokenSource.Token); | ||
} | ||
|
||
// assert | ||
_featuresConfig.Received().AddOrUpdateFeature(featureName, true); | ||
} | ||
|
||
|
||
[Test] | ||
public async Task Poll_Ignores_Empty_Response() | ||
{ | ||
// arrange | ||
_contentfulClient | ||
.GetEntries(Arg.Any<QueryBuilder<ApplicationFeatures>>(), Arg.Any<CancellationToken>()) | ||
.Returns( | ||
new ContentfulCollection<ApplicationFeatures> | ||
{ | ||
Items = new List<ApplicationFeatures>() | ||
} | ||
); | ||
|
||
var featuresConfigUpdater = new FeaturesConfigUpdater(_logger, _contentfulClient, _featuresConfig); | ||
|
||
// act | ||
using (var cancellationTokenSource = new CancellationTokenSource()) | ||
{ | ||
await featuresConfigUpdater.UpdateFeaturesAsync(cancellationTokenSource.Token); | ||
} | ||
|
||
// assert | ||
_featuresConfig.DidNotReceive().AddOrUpdateFeature(Arg.Any<string>(), Arg.Any<bool>()); | ||
} | ||
|
||
[Test] | ||
public async Task Poll_catches_exceptions() | ||
{ | ||
// arrange | ||
var exception = new TestException(); | ||
_contentfulClient | ||
.GetEntries(Arg.Any<QueryBuilder<ApplicationFeatures>>(), Arg.Any<CancellationToken>()) | ||
.Throws(exception); | ||
|
||
var featuresConfigUpdater = new FeaturesConfigUpdater(_logger, _contentfulClient, _featuresConfig); | ||
|
||
// act | ||
using (var cancellationTokenSource = new CancellationTokenSource()) | ||
{ | ||
await featuresConfigUpdater.UpdateFeaturesAsync(cancellationTokenSource.Token); | ||
} | ||
|
||
// assert | ||
_logger.Received().LogError(exception, "Exception querying for feature configuration. Does the ApplicationFeatures model exist in Contentful?"); | ||
} | ||
} |
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
Oops, something went wrong.