-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d9ee85
commit ba6d052
Showing
2 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
src/SFA.DAS.EmployerFinance.UnitTests/Queries/GetClientContent/WhenIGetContent.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,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)); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...A.DAS.EmployerFinance.UnitTests/Services/ClientContentServiceTests/WhenIGetContentData.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,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); | ||
} | ||
} | ||
} |