From ba6d0529f03aaba47ca93eacae9f8274bb282fb9 Mon Sep 17 00:00:00 2001 From: Ben Stone Date: Thu, 13 Aug 2020 15:31:32 +0100 Subject: [PATCH] CON-2182 adds unit tests --- .../GetClientContent/WhenIGetContent.cs | 123 ++++++++++++++++++ .../WhenIGetContentData.cs | 38 ++++++ 2 files changed, 161 insertions(+) create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Queries/GetClientContent/WhenIGetContent.cs create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Services/ClientContentServiceTests/WhenIGetContentData.cs diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Queries/GetClientContent/WhenIGetContent.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Queries/GetClientContent/WhenIGetContent.cs new file mode 100644 index 0000000000..836cbfa2e1 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Queries/GetClientContent/WhenIGetContent.cs @@ -0,0 +1,123 @@ +using Moq; +using NUnit.Framework; +using SFA.DAS.EmployerFinance.Configuration; +using SFA.DAS.EmployerFinance.Interfaces; +using SFA.DAS.EmployerFinance.Queries.GetClientContent; +using SFA.DAS.NLog.Logger; +using SFA.DAS.Validation; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerFinance.UnitTests.Queries.GetClientContent +{ + public class WhenIGetContent : QueryBaseTest + { + public override GetClientContentRequest Query { get; set; } + public override GetClientContentRequestHandler RequestHandler { get; set; } + public override Mock> RequestValidator { get; set; } + + private string _contentType; + private string _clientId; + + private string CacheKey; + private string Content; + public EmployerFinanceConfiguration EmployerFinanceConfiguration; + + private Mock MockLogger; + private Mock MockClientContentService; + private Mock MockCacheStorageService; + private Mock> MockValidator; + + [SetUp] + public void Arrange() + { + _clientId = "eas-fin"; + _contentType = "banner"; + + EmployerFinanceConfiguration = new EmployerFinanceConfiguration() + { + ApplicationId = "eas-fin", + DefaultCacheExpirationInMinutes = 1 + }; + Content = "

Example content

"; + CacheKey = EmployerFinanceConfiguration.ApplicationId; + + MockLogger = new Mock(); + MockClientContentService = new Mock(); + MockCacheStorageService = new Mock(); + MockValidator = new Mock>(); + + MockClientContentService + .Setup(cs => cs.Get(_contentType, _clientId)) + .ReturnsAsync(Content); + + Query = new GetClientContentRequest + { + ContentType = "banner" + }; + + RequestHandler = new GetClientContentRequestHandler(RequestValidator.Object, MockLogger.Object, + MockClientContentService.Object, MockCacheStorageService.Object, EmployerFinanceConfiguration); + } + + [Test] + public override async Task ThenIfTheMessageIsValidTheRepositoryIsCalled() + { + await RequestHandler.Handle(Query); + + MockClientContentService.Verify(x => x.Get(_contentType, _clientId), Times.Once); + } + + [Test] + public override async Task ThenIfTheMessageIsValidTheValueIsReturnedInTheResponse() + { + await RequestHandler.Handle(Query); + + MockClientContentService.Verify(x => x.Get(_contentType, _clientId), Times.Once); + } + + [Test] + public async Task AND_it_is_stored_in_the_cache_THEN_return_content() + { + StoredInCacheSetup(); + + var result = await RequestHandler.Handle(Query); + + Assert.AreEqual(Content, result.Content); + MockCacheStorageService.Verify(c => c.TryGet(CacheKey, out Content), Times.Once); + } + + [Test] + public async Task AND_it_is_stored_in_the_cache_THEN_content_api_is_not_called() + { + StoredInCacheSetup(); + + var result = await RequestHandler.Handle(Query); + + MockClientContentService.Verify(c => c.Get(_contentType, _clientId), Times.Never); + } + + [Test] + public async Task AND_it_is_not_stored_in_the_cache_THEN_call_from_client() + { + MockValidator.Setup(r => r.Validate(Query)).Returns(new ValidationResult()); + MockCacheStorageService.Setup(c => c.TryGet(CacheKey, out Content)).Returns(false); + MockClientContentService.Setup(c => c.Get("banner", CacheKey)) + .ReturnsAsync(Content); + + var result = await RequestHandler.Handle(Query); + + Assert.AreEqual(Content, result.Content); + } + + private void StoredInCacheSetup() + { + MockValidator.Setup(r => r.Validate(Query)).Returns(new ValidationResult()); + MockCacheStorageService.Setup(c => c.TryGet(CacheKey, out Content)).Returns(true); + MockClientContentService.Setup(c => c.Get("banner", CacheKey)); + } + } +} diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ClientContentServiceTests/WhenIGetContentData.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ClientContentServiceTests/WhenIGetContentData.cs new file mode 100644 index 0000000000..9c49e2e260 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ClientContentServiceTests/WhenIGetContentData.cs @@ -0,0 +1,38 @@ +using FluentAssertions; +using Moq; +using NUnit.Framework; +using SFA.DAS.EmployerFinance.Interfaces; +using SFA.DAS.EmployerFinance.Services; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerFinance.UnitTests.Services.ClientContentServiceTests +{ + class WhenIGetContentData + { + private Mock MockClientContentApiClient; + private IClientContentService ClientContentService; + private string TestContent = "

My First Paragraph

"; + + [SetUp] + public void SetUp() + { + MockClientContentApiClient = new Mock(); + MockClientContentApiClient + .Setup(c => c.Get("banner", "eas-fin")) + .ReturnsAsync(TestContent); + + ClientContentService = new ClientContentService(MockClientContentApiClient.Object); + } + + public async Task THEN_Content_is_returned_from_api() + { + var result = await ClientContentService.Get("banner", "eas-fin"); + + result.Should().Be(TestContent); + } + } +}