From c0512f7ae4d2e8e7608d9895d8073e5336e042c7 Mon Sep 17 00:00:00 2001 From: cairnsj <51908793+cairnsj@users.noreply.github.com> Date: Tue, 3 Oct 2023 10:39:57 +0100 Subject: [PATCH 1/2] Add CancellationToken into all Controller methods calling Contentful --- .../ContentController.ServerTests.cs | 5 +++-- .../Controllers/ContentControllerTests.cs | 20 ++++++++++------- .../Controllers/CookieControllerTests.cs | 16 +++++++++----- .../ErrorController.ServerTests.cs | 7 +++--- .../Controllers/ResourcesControllerTests.cs | 22 ++++++++++--------- .../CpdTestServerApplication.cs | 2 ++ .../Controllers/ContentController.cs | 18 ++++++--------- .../Controllers/CookieController.cs | 4 ++-- .../Controllers/ResourcesController.cs | 18 +++++++-------- 9 files changed, 61 insertions(+), 51 deletions(-) diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/ContentController.ServerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/ContentController.ServerTests.cs index d7d85d03..de87e688 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/ContentController.ServerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/ContentController.ServerTests.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Net; using System.Net.Http; +using System.Threading; using System.Threading.Tasks; namespace Childrens_Social_Care_CPD_Tests.Controllers; @@ -36,7 +37,7 @@ public async Task Content_Will_Contain_Warning_If_Data_Is_Self_Referential() var content = new Content(); content.Items = new List { content }; var contentCollection = new ContentfulCollection() { Items = new List() { content } }; - _application.CpdContentfulClient.GetEntries(Arg.Any>(), default).Returns(contentCollection); + _application.CpdContentfulClient.GetEntries(Arg.Any>(), Arg.Any()).Returns(contentCollection); // act var response = await _httpClient.GetAsync(ContentUrl); @@ -54,7 +55,7 @@ public async Task Content_Will_Contain_Warning_If_Data_Has_An_Unknown_Content_Ty var content = new Content(); content.Items = new List { new TestingContentItem() }; var contentCollection = new ContentfulCollection() { Items = new List() { content } }; - _application.CpdContentfulClient.GetEntries(Arg.Any>(), default).Returns(contentCollection); + _application.CpdContentfulClient.GetEntries(Arg.Any>(), Arg.Any()).Returns(contentCollection); // act var response = await _httpClient.GetAsync(ContentUrl); diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/ContentControllerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/ContentControllerTests.cs index f87aa43c..089477e2 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/ContentControllerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/ContentControllerTests.cs @@ -11,6 +11,7 @@ using NSubstitute; using NUnit.Framework; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; namespace Childrens_Social_Care_CPD_Tests.Controllers; @@ -22,6 +23,7 @@ public class ContentControllerTests private HttpContext _httpContext; private HttpRequest _httpRequest; private ICpdContentfulClient _contentfulClient; + private CancellationTokenSource _cancellationTokenSource; private void SetContent(Content content) { @@ -32,8 +34,10 @@ private void SetContent(Content content) : contentCollection.Items = new List { content }; _contentfulClient - .GetEntries(Arg.Any>(), default) + .GetEntries(Arg.Any>(), Arg.Any()) .Returns(contentCollection); + + _cancellationTokenSource = new CancellationTokenSource(); } [SetUp] @@ -62,7 +66,7 @@ public async Task Index_Returns_404_When_No_Content_Found() SetContent(null); // act - var actual = await _contentController.Index("home"); + var actual = await _contentController.Index(_cancellationTokenSource.Token, "home"); // assert actual.Should().BeOfType(); @@ -75,7 +79,7 @@ public async Task Index_Returns_View() SetContent(new Content()); // act - var actual = await _contentController.Index("home"); + var actual = await _contentController.Index(_cancellationTokenSource.Token, "home"); // assert actual.Should().BeOfType(); @@ -94,7 +98,7 @@ public async Task Index_Sets_The_ViewState_ContextModel() SetContent(rootContent); // act - await _contentController.Index("home"); + await _contentController.Index(_cancellationTokenSource.Token, "home"); var actual = _contentController.ViewData["ContextModel"] as ContextModel; // assert @@ -112,7 +116,7 @@ public async Task Index_Sets_The_ContextModel_Preferences_Set_Value_Correctly(bo SetContent(new Content()); // act - await _contentController.Index("home", preferenceSet); + await _contentController.Index(_cancellationTokenSource.Token, "home", preferenceSet); var actual = _contentController.ViewData["ContextModel"] as ContextModel; // assert @@ -138,7 +142,7 @@ public async Task Index_Sets_The_ContextModel_UseContainers_From_SideMenu_Value_ SetContent(rootContent); // act - await _contentController.Index("home"); + await _contentController.Index(_cancellationTokenSource.Token, "home"); var actual = _contentController.ViewData["ContextModel"] as ContextModel; // assert @@ -152,10 +156,10 @@ public async Task Index_Trims_Trailing_Slashes() // arrange SetContent(new Content()); var query = ""; - await _contentfulClient.GetEntries(Arg.Do>(value => query = value.Build())); + await _contentfulClient.GetEntries(Arg.Do>(value => query = value.Build()), Arg.Any()); // act - var actual = await _contentController.Index("home/"); + var actual = await _contentController.Index(_cancellationTokenSource.Token, "home/"); // assert query.Should().Contain("fields.id=home&"); diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/CookieControllerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/CookieControllerTests.cs index ff584f72..800c50f3 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/CookieControllerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/CookieControllerTests.cs @@ -16,6 +16,7 @@ using System; using Childrens_Social_Care_CPD; using Childrens_Social_Care_CPD.Configuration; +using System.Threading; namespace Childrens_Social_Care_CPD_Tests.Controllers; @@ -26,6 +27,7 @@ public partial class CookieControllerTests private HttpContext _httpContext; private HttpRequest _httpRequest; private ICpdContentfulClient _contentfulClient; + private CancellationTokenSource _cancellationTokenSource; private void SetContent(Content content) { @@ -36,8 +38,10 @@ private void SetContent(Content content) : contentCollection.Items = new List { content }; _contentfulClient - .GetEntries(Arg.Any>(), default) + .GetEntries(Arg.Any>(), Arg.Any()) .Returns(contentCollection); + + _cancellationTokenSource = new CancellationTokenSource(); } [SetUp] @@ -71,7 +75,7 @@ public async Task Cookies_Returns_404_When_No_Content_Found() SetContent(null); // act - var actual = await _cookieController.Cookies(); + var actual = await _cookieController.Cookies(_cancellationTokenSource.Token); // assert actual.Should().BeOfType(); @@ -90,7 +94,7 @@ public async Task Cookies_Sets_The_ViewState_ContextModel() SetContent(rootContent); // act - await _cookieController.Cookies(); + await _cookieController.Cookies(_cancellationTokenSource.Token); var actual = _cookieController.ViewData["ContextModel"] as ContextModel; // assert @@ -108,7 +112,7 @@ public async Task Cookies_Sets_The_ContextModel_Preferences_Set_Value_Correctly( SetContent(new Content()); // act - await _cookieController.Cookies(preferenceSet: preferenceSet); + await _cookieController.Cookies(_cancellationTokenSource.Token, preferenceSet: preferenceSet); var actual = _cookieController.ViewData["ContextModel"] as ContextModel; // assert @@ -133,7 +137,7 @@ public async Task Cookies_Sets_The_ContextModel_UseContainers_Ignoring_The_SideM SetContent(rootContent); // act - await _cookieController.Cookies(); + await _cookieController.Cookies(_cancellationTokenSource.Token); var actual = _cookieController.ViewData["ContextModel"] as ContextModel; // assert @@ -148,7 +152,7 @@ public async Task Cookies_Action_Should_Not_Show_Consent_Panel() SetContent(new Content()); // act - await _cookieController.Cookies(); + await _cookieController.Cookies(_cancellationTokenSource.Token); var actual = _cookieController.ViewData["ContextModel"] as ContextModel; // assert diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/ErrorController.ServerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/ErrorController.ServerTests.cs index 6d004f07..b2217f8c 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/ErrorController.ServerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/ErrorController.ServerTests.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using System.Net; using System.Net.Http; +using System.Threading; using System.Threading.Tasks; namespace Childrens_Social_Care_CPD_Tests.Controllers; @@ -37,7 +38,7 @@ public async Task Non_Existant_Page_Will_Return_404() { // arrange var contentCollection = new ContentfulCollection() { Items = new List() }; - _application.CpdContentfulClient.GetEntries(Arg.Any>(), default).Returns(contentCollection); + _application.CpdContentfulClient.GetEntries(Arg.Any>(), Arg.Any()).Returns(contentCollection); var url = "/does_not_exist"; // act @@ -52,7 +53,7 @@ public async Task Non_Existant_Page_Will_Return_404() public async Task Exception_Will_Return_500() { // arrange - _application.CpdContentfulClient.GetEntries(Arg.Any>(), default).Throws(new Exception("Test exception")); + _application.CpdContentfulClient.GetEntries(Arg.Any>(), Arg.Any()).Throws(new Exception("Test exception")); var url = "/something"; // act @@ -72,7 +73,7 @@ public async Task Exception_Will_Be_Logged() var exception = new TestException(); var logger = Substitute.For>(); _application.LoggerFactory.CreateLogger().Returns(logger); - _application.CpdContentfulClient.GetEntries(Arg.Any>(), default).Throws(exception); + _application.CpdContentfulClient.GetEntries(Arg.Any>(), Arg.Any()).Throws(exception); var url = "/something"; // act diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs index 4088081e..51adb3af 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs @@ -11,11 +11,10 @@ using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.Extensions.Logging; using NSubstitute; -using NSubstitute.ExceptionExtensions; using NUnit.Framework; using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; namespace Childrens_Social_Care_CPD_Tests.Controllers; @@ -28,13 +27,14 @@ public class ResourcesControllerTests private HttpRequest _httpRequest; private ICpdContentfulClient _contentfulClient; private ILogger _logger; + private CancellationTokenSource _cancellationTokenSource; private void SetContent(Content content, ContentfulCollection resourceCollection) { resourceCollection ??= new (); _contentfulClient - .GetEntries(Arg.Any>(), default) + .GetEntries(Arg.Any>(), Arg.Any()) .Returns(resourceCollection); var contentCollection = new ContentfulCollection(); @@ -44,8 +44,10 @@ private void SetContent(Content content, ContentfulCollection resource : contentCollection.Items = new List { content }; _contentfulClient - .GetEntries(Arg.Any>(), default) + .GetEntries(Arg.Any>(), Arg.Any()) .Returns(contentCollection); + + _cancellationTokenSource = new CancellationTokenSource(); } [SetUp] @@ -77,7 +79,7 @@ public async Task Search_With_Empty_Query_Returns_View() SetContent(null, null); // act - var actual = await _resourcesController.Search(query: null) as ViewResult; + var actual = await _resourcesController.Search(_cancellationTokenSource.Token, query: null) as ViewResult; // assert actual.Should().BeOfType(); @@ -97,7 +99,7 @@ public async Task Search_Page_Resource_Is_Passed_To_View() }; // act - var actual = (await _resourcesController.Search(query: null) as ViewResult)?.Model as ResourcesListViewModel; + var actual = (await _resourcesController.Search(_cancellationTokenSource.Token, query: null) as ViewResult)?.Model as ResourcesListViewModel; // assert actual.Content.Should().Be(content); @@ -110,7 +112,7 @@ public async Task Search_Sets_The_ViewState_ContextModel() SetContent(null, null); // act - await _resourcesController.Search(null); + await _resourcesController.Search(_cancellationTokenSource.Token, null); var actual = _resourcesController.ViewData["ContextModel"] as ContextModel; // assert @@ -132,7 +134,7 @@ public async Task Search_Selected_Tags_Are_Passed_Into_View() }; // act - var actual = (await _resourcesController.Search(query) as ViewResult)?.Model as ResourcesListViewModel; + var actual = (await _resourcesController.Search(_cancellationTokenSource.Token, query) as ViewResult)?.Model as ResourcesListViewModel; // assert actual.SelectedTags.Should().Equal(query.Tags); @@ -157,7 +159,7 @@ public async Task Search_Page_Set_To_Be_In_Bounds() }; // act - var actual = (await _resourcesController.Search(query) as ViewResult)?.Model as ResourcesListViewModel; + var actual = (await _resourcesController.Search(_cancellationTokenSource.Token, query) as ViewResult)?.Model as ResourcesListViewModel; // assert actual.CurrentPage.Should().Be(1); @@ -176,7 +178,7 @@ public async Task Search_Invalid_Tags_Logs_Warning() }; // act - await _resourcesController.Search(query); + await _resourcesController.Search(_cancellationTokenSource.Token, query); //assert _logger.ReceivedWithAnyArgs(1).LogWarning(default, args: default); diff --git a/Childrens-Social-Care-CPD-Tests/CpdTestServerApplication.cs b/Childrens-Social-Care-CPD-Tests/CpdTestServerApplication.cs index 00aa7262..1601c7a4 100644 --- a/Childrens-Social-Care-CPD-Tests/CpdTestServerApplication.cs +++ b/Childrens-Social-Care-CPD-Tests/CpdTestServerApplication.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NSubstitute; +using System.Threading; namespace Childrens_Social_Care_CPD_Tests; @@ -24,6 +25,7 @@ protected override IHost CreateHost(IHostBuilder builder) { services.AddTransient((_) => _cpdContentfulClient); services.AddSingleton(_loggerFactory); + services.AddScoped(typeof(CancellationToken), s => new CancellationTokenSource().Token); }); return base.CreateHost(builder); } diff --git a/Childrens-Social-Care-CPD/Controllers/ContentController.cs b/Childrens-Social-Care-CPD/Controllers/ContentController.cs index 5c274443..21ccfaa4 100644 --- a/Childrens-Social-Care-CPD/Controllers/ContentController.cs +++ b/Childrens-Social-Care-CPD/Controllers/ContentController.cs @@ -9,24 +9,20 @@ namespace Childrens_Social_Care_CPD.Controllers; public class ContentController : Controller { private readonly ICpdContentfulClient _cpdClient; - private readonly static int ContentFetchDepth = 10; - private readonly static string ContentTypeId = "content"; - private readonly static string DefaultHomePageName = "home"; public ContentController(ICpdContentfulClient cpdClient) { _cpdClient = cpdClient; } - private async Task FetchPageContentAsync(string contentId) + private async Task FetchPageContentAsync(string contentId, CancellationToken cancellationToken) { var queryBuilder = QueryBuilder.New - .ContentTypeIs(ContentTypeId) - .FieldEquals("fields.id", contentId ?? DefaultHomePageName) - //.FieldEquals("fields.items.id", "foo9") - .Include(ContentFetchDepth); + .ContentTypeIs("content") + .FieldEquals("fields.id", contentId) + .Include(10); - var result = await _cpdClient.GetEntries(queryBuilder); + var result = await _cpdClient.GetEntries(queryBuilder, cancellationToken); return result?.FirstOrDefault(); } @@ -46,10 +42,10 @@ private async Task FetchPageContentAsync(string contentId) Etc. */ [Route("/{*pagename:regex(^[[0-9a-z]](\\/?[[0-9a-z\\-]])*\\/?$)}")] - public async Task Index(string pageName, bool preferenceSet = false) + public async Task Index(CancellationToken cancellationToken, string pageName = "home", bool preferenceSet = false) { pageName = pageName?.TrimEnd('/'); - var pageContent = await FetchPageContentAsync(pageName); + var pageContent = await FetchPageContentAsync(pageName, cancellationToken); if (pageContent == null) { return NotFound(); diff --git a/Childrens-Social-Care-CPD/Controllers/CookieController.cs b/Childrens-Social-Care-CPD/Controllers/CookieController.cs index 28935640..e2e501f7 100644 --- a/Childrens-Social-Care-CPD/Controllers/CookieController.cs +++ b/Childrens-Social-Care-CPD/Controllers/CookieController.cs @@ -37,7 +37,7 @@ public IActionResult SetPreferences(string consentValue, string sourcePage = nul [HttpGet] [Route("/cookies")] - public async Task Cookies(string sourcePage = null, bool preferenceSet = false) + public async Task Cookies(CancellationToken cancellationToken, string sourcePage = null, bool preferenceSet = false) { sourcePage = sourcePage ?? string.Empty; @@ -46,7 +46,7 @@ public async Task Cookies(string sourcePage = null, bool preferen .FieldEquals("fields.id", PageName) .Include(10); - var result = await _cpdClient.GetEntries(queryBuilder); + var result = await _cpdClient.GetEntries(queryBuilder, cancellationToken); var pageContent = result.FirstOrDefault(); if (pageContent == null) diff --git a/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs b/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs index 097bcaa4..9637af3a 100644 --- a/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs +++ b/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs @@ -52,17 +52,17 @@ private IEnumerable GetQueryTags(int[] tags) return tags.Select(x => { return _tagInfos[x].TagName; }); } - private Task> FetchResourcesContentAsync() + private Task> FetchResourcesContentAsync(CancellationToken cancellationToken) { var queryBuilder = QueryBuilder.New .ContentTypeIs("content") .Include(10) .FieldEquals("fields.id", "resources"); - return _cpdClient.GetEntries(queryBuilder); + return _cpdClient.GetEntries(queryBuilder, cancellationToken); } - private Task> FetchResourceSearchResultsAsync(int[] tags, int skip = 0, int limit = 5) + private Task> FetchResourceSearchResultsAsync(CancellationToken cancellationToken, int[] tags, int skip = 0, int limit = 5) { var queryBuilder = QueryBuilder.New .ContentTypeIs("resource") @@ -72,13 +72,13 @@ private Task> FetchResourceSearchResultsAsync(int .Skip(skip) .Limit(limit); - return _cpdClient.GetEntries(queryBuilder); + return _cpdClient.GetEntries(queryBuilder, cancellationToken); } - private async Task>> GetContentAsync(int[] tags, int skip = 0, int limit = 5) + private async Task>> GetContentAsync(CancellationToken cancellationToken, int[] tags, int skip = 0, int limit = 5) { - var pageContentTask = FetchResourcesContentAsync(); - var searchContentTask = FetchResourceSearchResultsAsync(tags, skip, limit); + var pageContentTask = FetchResourcesContentAsync(cancellationToken); + var searchContentTask = FetchResourceSearchResultsAsync(cancellationToken, tags, skip, limit); await Task.WhenAll(pageContentTask, searchContentTask); return Tuple.Create(pageContentTask.Result?.FirstOrDefault(), searchContentTask.Result); @@ -105,13 +105,13 @@ private static string GetPagingFormatString(int[] tags) [Route("resources", Name = "Resource")] [HttpGet] - public async Task Search([FromQuery] ResourcesQuery query, bool preferencesSet = false) + public async Task Search(CancellationToken cancellationToken, [FromQuery] ResourcesQuery query, bool preferencesSet = false) { query ??= new ResourcesQuery(); query.Tags ??= Array.Empty(); (var page, var skip, var pageSize) = CalculatePaging(query); - (var pageContent, var contentCollection) = await GetContentAsync(query.Tags, skip, pageSize); + (var pageContent, var contentCollection) = await GetContentAsync(cancellationToken, query.Tags, skip, pageSize); var totalPages = (int)Math.Ceiling((decimal)contentCollection.Total / pageSize); page = Math.Min(page, totalPages); From 8fd23e421662f9ecafd883523a482f0b9de9fcb9 Mon Sep 17 00:00:00 2001 From: cairnsj <51908793+cairnsj@users.noreply.github.com> Date: Tue, 3 Oct 2023 10:55:27 +0100 Subject: [PATCH 2/2] Fix compiler hints --- .../ContentController.ServerTests.cs | 12 +++++++----- .../Controllers/ContentControllerTests.cs | 12 +++++++----- .../Controllers/CookieControllerTests.cs | 12 +++++++----- .../Controllers/ResourcesControllerTests.cs | 19 +++++++------------ .../CpdTestServerApplication.cs | 2 +- .../Controllers/CookieController.cs | 8 ++++---- .../Controllers/ResourcesController.cs | 10 +++++----- 7 files changed, 38 insertions(+), 37 deletions(-) diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/ContentController.ServerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/ContentController.ServerTests.cs index de87e688..0b8fd21e 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/ContentController.ServerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/ContentController.ServerTests.cs @@ -18,7 +18,7 @@ public class ContentControllerServerTests { private CpdTestServerApplication _application; private HttpClient _httpClient; - private static string ContentUrl = "/"; + private static readonly string _contentUrl = "/"; [SetUp] public void SetUp() @@ -40,7 +40,7 @@ public async Task Content_Will_Contain_Warning_If_Data_Is_Self_Referential() _application.CpdContentfulClient.GetEntries(Arg.Any>(), Arg.Any()).Returns(contentCollection); // act - var response = await _httpClient.GetAsync(ContentUrl); + var response = await _httpClient.GetAsync(_contentUrl); var responseContent = await response.Content.ReadAsStringAsync(); // assert @@ -52,13 +52,15 @@ public async Task Content_Will_Contain_Warning_If_Data_Is_Self_Referential() public async Task Content_Will_Contain_Warning_If_Data_Has_An_Unknown_Content_Type() { // arrange - var content = new Content(); - content.Items = new List { new TestingContentItem() }; + var content = new Content + { + Items = new List { new TestingContentItem() } + }; var contentCollection = new ContentfulCollection() { Items = new List() { content } }; _application.CpdContentfulClient.GetEntries(Arg.Any>(), Arg.Any()).Returns(contentCollection); // act - var response = await _httpClient.GetAsync(ContentUrl); + var response = await _httpClient.GetAsync(_contentUrl); var responseContent = await response.Content.ReadAsStringAsync(); // assert diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/ContentControllerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/ContentControllerTests.cs index 089477e2..a3c89caa 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/ContentControllerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/ContentControllerTests.cs @@ -54,9 +54,11 @@ public void SetUp() _contentfulClient = Substitute.For(); - _contentController = new ContentController(_contentfulClient); - _contentController.ControllerContext = controllerContext; - _contentController.TempData = Substitute.For(); + _contentController = new ContentController(_contentfulClient) + { + ControllerContext = controllerContext, + TempData = Substitute.For() + }; } [Test] @@ -124,13 +126,13 @@ public async Task Index_Sets_The_ContextModel_Preferences_Set_Value_Correctly(bo actual.PreferenceSet.Should().Be(preferenceSet); } - public static object[] SideMenuContent = + private static readonly object[] _sideMenuContent = { new object[] { new SideMenu() }, new object[] { null }, }; - [TestCaseSource(nameof(SideMenuContent))] + [TestCaseSource(nameof(_sideMenuContent))] public async Task Index_Sets_The_ContextModel_UseContainers_From_SideMenu_Value_Correctly(SideMenu sideMenu) { // arrange diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/CookieControllerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/CookieControllerTests.cs index 800c50f3..1acd856c 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/CookieControllerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/CookieControllerTests.cs @@ -63,9 +63,11 @@ public void SetUp() _contentfulClient = Substitute.For(); - _cookieController = new CookieController(_contentfulClient, new CookieHelper(new ApplicationConfiguration())); - _cookieController.ControllerContext = controllerContext; - _cookieController.TempData = Substitute.For(); + _cookieController = new CookieController(_contentfulClient, new CookieHelper(new ApplicationConfiguration())) + { + ControllerContext = controllerContext, + TempData = Substitute.For() + }; } [Test] @@ -120,13 +122,13 @@ public async Task Cookies_Sets_The_ContextModel_Preferences_Set_Value_Correctly( actual.PreferenceSet.Should().Be(preferenceSet); } - public static object[] SideMenuContent = + private static readonly object[] _sideMenuContent = { new object[] { new SideMenu() }, new object[] { null }, }; - [TestCaseSource(nameof(SideMenuContent))] + [TestCaseSource(nameof(_sideMenuContent))] public async Task Cookies_Sets_The_ContextModel_UseContainers_Ignoring_The_SideMenu_Value(SideMenu sideMenu) { // arrange diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs index 51adb3af..60ad48bf 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs @@ -79,7 +79,7 @@ public async Task Search_With_Empty_Query_Returns_View() SetContent(null, null); // act - var actual = await _resourcesController.Search(_cancellationTokenSource.Token, query: null) as ViewResult; + var actual = await _resourcesController.Search(query: null, _cancellationTokenSource.Token) as ViewResult; // assert actual.Should().BeOfType(); @@ -92,14 +92,9 @@ public async Task Search_Page_Resource_Is_Passed_To_View() // arrange var content = new Content(); SetContent(content, null); - var query = new ResourcesQuery - { - Page = 1, - Tags = Array.Empty() - }; // act - var actual = (await _resourcesController.Search(_cancellationTokenSource.Token, query: null) as ViewResult)?.Model as ResourcesListViewModel; + var actual = (await _resourcesController.Search(query: null, _cancellationTokenSource.Token) as ViewResult)?.Model as ResourcesListViewModel; // assert actual.Content.Should().Be(content); @@ -112,7 +107,7 @@ public async Task Search_Sets_The_ViewState_ContextModel() SetContent(null, null); // act - await _resourcesController.Search(_cancellationTokenSource.Token, null); + await _resourcesController.Search(null, _cancellationTokenSource.Token); var actual = _resourcesController.ViewData["ContextModel"] as ContextModel; // assert @@ -134,7 +129,7 @@ public async Task Search_Selected_Tags_Are_Passed_Into_View() }; // act - var actual = (await _resourcesController.Search(_cancellationTokenSource.Token, query) as ViewResult)?.Model as ResourcesListViewModel; + var actual = (await _resourcesController.Search(query, _cancellationTokenSource.Token) as ViewResult)?.Model as ResourcesListViewModel; // assert actual.SelectedTags.Should().Equal(query.Tags); @@ -159,7 +154,7 @@ public async Task Search_Page_Set_To_Be_In_Bounds() }; // act - var actual = (await _resourcesController.Search(_cancellationTokenSource.Token, query) as ViewResult)?.Model as ResourcesListViewModel; + var actual = (await _resourcesController.Search(query, _cancellationTokenSource.Token) as ViewResult)?.Model as ResourcesListViewModel; // assert actual.CurrentPage.Should().Be(1); @@ -178,9 +173,9 @@ public async Task Search_Invalid_Tags_Logs_Warning() }; // act - await _resourcesController.Search(_cancellationTokenSource.Token, query); + await _resourcesController.Search(query, _cancellationTokenSource.Token); //assert _logger.ReceivedWithAnyArgs(1).LogWarning(default, args: default); } -} +} \ No newline at end of file diff --git a/Childrens-Social-Care-CPD-Tests/CpdTestServerApplication.cs b/Childrens-Social-Care-CPD-Tests/CpdTestServerApplication.cs index 1601c7a4..899fdbe6 100644 --- a/Childrens-Social-Care-CPD-Tests/CpdTestServerApplication.cs +++ b/Childrens-Social-Care-CPD-Tests/CpdTestServerApplication.cs @@ -10,7 +10,7 @@ namespace Childrens_Social_Care_CPD_Tests; internal class CpdTestServerApplication : WebApplicationFactory { - private ICpdContentfulClient _cpdContentfulClient; + private readonly ICpdContentfulClient _cpdContentfulClient; private ILoggerFactory _loggerFactory; public CpdTestServerApplication() diff --git a/Childrens-Social-Care-CPD/Controllers/CookieController.cs b/Childrens-Social-Care-CPD/Controllers/CookieController.cs index e2e501f7..7161577f 100644 --- a/Childrens-Social-Care-CPD/Controllers/CookieController.cs +++ b/Childrens-Social-Care-CPD/Controllers/CookieController.cs @@ -11,7 +11,7 @@ public class CookieController : Controller { private readonly ICpdContentfulClient _cpdClient; private readonly ICookieHelper _cookieHelper; - private const string PageName = "cookies"; + private const string _pageName = "cookies"; public CookieController(ICpdContentfulClient cpdClient, ICookieHelper cookieHelper) { @@ -39,11 +39,11 @@ public IActionResult SetPreferences(string consentValue, string sourcePage = nul [Route("/cookies")] public async Task Cookies(CancellationToken cancellationToken, string sourcePage = null, bool preferenceSet = false) { - sourcePage = sourcePage ?? string.Empty; + sourcePage ??= string.Empty; var queryBuilder = QueryBuilder.New .ContentTypeIs("content") - .FieldEquals("fields.id", PageName) + .FieldEquals("fields.id", _pageName) .Include(10); var result = await _cpdClient.GetEntries(queryBuilder, cancellationToken); @@ -60,7 +60,7 @@ public async Task Cookies(CancellationToken cancellationToken, st } var consentState = _cookieHelper.GetRequestAnalyticsCookieState(HttpContext); - var contextModel = new ContextModel(pageContent.Id, pageContent.Title, PageName, pageContent.Category, true, preferenceSet, true); + var contextModel = new ContextModel(pageContent.Id, pageContent.Title, _pageName, pageContent.Category, true, preferenceSet, true); ViewData["ContextModel"] = contextModel; var model = new CookiesAndAnalyticsConsentModel diff --git a/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs b/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs index 9637af3a..a243e073 100644 --- a/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs +++ b/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs @@ -62,7 +62,7 @@ private Task> FetchResourcesContentAsync(Cancellat return _cpdClient.GetEntries(queryBuilder, cancellationToken); } - private Task> FetchResourceSearchResultsAsync(CancellationToken cancellationToken, int[] tags, int skip = 0, int limit = 5) + private Task> FetchResourceSearchResultsAsync(int[] tags, CancellationToken cancellationToken, int skip = 0, int limit = 5) { var queryBuilder = QueryBuilder.New .ContentTypeIs("resource") @@ -75,10 +75,10 @@ private Task> FetchResourceSearchResultsAsync(Can return _cpdClient.GetEntries(queryBuilder, cancellationToken); } - private async Task>> GetContentAsync(CancellationToken cancellationToken, int[] tags, int skip = 0, int limit = 5) + private async Task>> GetContentAsync(int[] tags, CancellationToken cancellationToken, int skip = 0, int limit = 5) { var pageContentTask = FetchResourcesContentAsync(cancellationToken); - var searchContentTask = FetchResourceSearchResultsAsync(cancellationToken, tags, skip, limit); + var searchContentTask = FetchResourceSearchResultsAsync(tags, cancellationToken, skip, limit); await Task.WhenAll(pageContentTask, searchContentTask); return Tuple.Create(pageContentTask.Result?.FirstOrDefault(), searchContentTask.Result); @@ -105,13 +105,13 @@ private static string GetPagingFormatString(int[] tags) [Route("resources", Name = "Resource")] [HttpGet] - public async Task Search(CancellationToken cancellationToken, [FromQuery] ResourcesQuery query, bool preferencesSet = false) + public async Task Search([FromQuery] ResourcesQuery query, CancellationToken cancellationToken, bool preferencesSet = false) { query ??= new ResourcesQuery(); query.Tags ??= Array.Empty(); (var page, var skip, var pageSize) = CalculatePaging(query); - (var pageContent, var contentCollection) = await GetContentAsync(cancellationToken, query.Tags, skip, pageSize); + (var pageContent, var contentCollection) = await GetContentAsync(query.Tags, cancellationToken, skip, pageSize); var totalPages = (int)Math.Ceiling((decimal)contentCollection.Total / pageSize); page = Math.Min(page, totalPages);