Skip to content

Commit

Permalink
CON-2182 adds unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ben1stone-leftdfe committed Aug 13, 2020
1 parent 6d9ee85 commit ba6d052
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<GetClientContentRequestHandler, GetClientContentRequest, GetClientContentResponse>
{
public override GetClientContentRequest Query { get; set; }
public override GetClientContentRequestHandler RequestHandler { get; set; }
public override Mock<IValidator<GetClientContentRequest>> RequestValidator { get; set; }

private string _contentType;
private string _clientId;

private string CacheKey;
private string Content;
public EmployerFinanceConfiguration EmployerFinanceConfiguration;

private Mock<ILog> MockLogger;
private Mock<IClientContentService> MockClientContentService;
private Mock<ICacheStorageService> MockCacheStorageService;
private Mock<IValidator<GetClientContentRequest>> MockValidator;

[SetUp]
public void Arrange()
{
_clientId = "eas-fin";
_contentType = "banner";

EmployerFinanceConfiguration = new EmployerFinanceConfiguration()
{
ApplicationId = "eas-fin",
DefaultCacheExpirationInMinutes = 1
};
Content = "<p> Example content </p>";
CacheKey = EmployerFinanceConfiguration.ApplicationId;

MockLogger = new Mock<ILog>();
MockClientContentService = new Mock<IClientContentService>();
MockCacheStorageService = new Mock<ICacheStorageService>();
MockValidator = new Mock<IValidator<GetClientContentRequest>>();

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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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<IClientContentApiClient> MockClientContentApiClient;
private IClientContentService ClientContentService;
private string TestContent = "<h1My First Heading</h1><p>My First Paragraph</p>";

[SetUp]
public void SetUp()
{
MockClientContentApiClient = new Mock<IClientContentApiClient>();
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);
}
}
}

0 comments on commit ba6d052

Please sign in to comment.