-
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.
CON-2182 Refactor content service to use decorator pattern
- Loading branch information
1 parent
d322ae5
commit 9438c68
Showing
13 changed files
with
314 additions
and
111 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
93 changes: 93 additions & 0 deletions
93
src/SFA.DAS.EmployerAccounts.UnitTests/Services/ContentBanner/WhenIGetContentWithCaching.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,93 @@ | ||
| ||
|
||
using Moq; | ||
using NUnit.Framework; | ||
using SFA.DAS.EmployerAccounts.Configuration; | ||
using SFA.DAS.EmployerAccounts.Interfaces; | ||
using SFA.DAS.EmployerAccounts.Services; | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerAccounts.UnitTests.Services.ContentBanner | ||
{ | ||
public class WhenIGetContentWithCaching | ||
{ | ||
private string _contentType; | ||
private string _clientId; | ||
|
||
private Mock<IClientContentService> MockClientContentService; | ||
private Mock<ICacheStorageService> MockCacheStorageService; | ||
|
||
private string CacheKey; | ||
private string ContentFromCache; | ||
private string ContentFromApi; | ||
private EmployerAccountsConfiguration EmployerAccountsConfig; | ||
|
||
private IClientContentService ClientContentServiceWithCaching; | ||
|
||
[SetUp] | ||
public void Arrange() | ||
{ | ||
_clientId = "eas-fin"; | ||
_contentType = "banner"; | ||
|
||
EmployerAccountsConfig = new EmployerAccountsConfiguration() | ||
{ | ||
ApplicationId = "eas-fin", | ||
DefaultCacheExpirationInMinutes = 1 | ||
}; | ||
ContentFromCache = "<p> Example content from cache </p>"; | ||
ContentFromApi = "<p> Example content from api </p>"; | ||
CacheKey = EmployerAccountsConfig.ApplicationId + "_banner"; | ||
|
||
MockClientContentService = new Mock<IClientContentService>(); | ||
MockCacheStorageService = new Mock<ICacheStorageService>(); | ||
|
||
ClientContentServiceWithCaching = new ClientContentServiceWithCaching(MockClientContentService.Object, MockCacheStorageService.Object, EmployerAccountsConfig); | ||
} | ||
|
||
[Test] | ||
public async Task AND_ItIsStoredInTheCache_THEN_ReturnContent() | ||
{ | ||
StoredInCacheSetup(); | ||
|
||
var result = await ClientContentServiceWithCaching.Get(_contentType, EmployerAccountsConfig.ApplicationId); | ||
|
||
Assert.AreEqual(ContentFromCache, result); | ||
MockCacheStorageService.Verify(c => c.TryGet(CacheKey, out ContentFromCache), Times.Once); | ||
} | ||
|
||
[Test] | ||
public async Task AND_ItIsStoredInTheCache_THEN_ContentApiIsNotCalled() | ||
{ | ||
StoredInCacheSetup(); | ||
|
||
var result = await ClientContentServiceWithCaching.Get(_contentType, EmployerAccountsConfig.ApplicationId); | ||
|
||
MockClientContentService.Verify(c => c.Get(_contentType, _clientId), Times.Never); | ||
} | ||
|
||
[Test] | ||
public async Task AND_ItIsNotStoredInTheCache_THEN_CallFromClient() | ||
{ | ||
NotStoredInCacheSetup(); | ||
|
||
var result = await ClientContentServiceWithCaching.Get(_contentType, EmployerAccountsConfig.ApplicationId); | ||
|
||
MockClientContentService.Verify(c => c.Get(_contentType, _clientId), Times.Once); | ||
Assert.AreEqual(ContentFromApi, result); | ||
} | ||
private void StoredInCacheSetup() | ||
{ | ||
MockCacheStorageService.Setup(c => c.TryGet(CacheKey, out ContentFromCache)).Returns(true); | ||
MockClientContentService.Setup(c => c.Get("banner", CacheKey)); | ||
} | ||
|
||
private void NotStoredInCacheSetup() | ||
{ | ||
|
||
MockCacheStorageService.Setup(c => c.TryGet(CacheKey, out ContentFromCache)).Returns(false); | ||
MockClientContentService.Setup(c => c.Get("banner", EmployerAccountsConfig.ApplicationId)) | ||
.ReturnsAsync(ContentFromApi); | ||
} | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
src/SFA.DAS.EmployerAccounts/Services/ClientContentServiceWithCaching.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,49 @@ | ||
using SFA.DAS.EmployerAccounts.Configuration; | ||
using SFA.DAS.EmployerAccounts.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerAccounts.Services | ||
{ | ||
public class ClientContentServiceWithCaching : IClientContentService | ||
{ | ||
private readonly IClientContentService _contentService; | ||
private readonly ICacheStorageService _cacheStorageService; | ||
private readonly EmployerAccountsConfiguration _employerAccountsConfiguration; | ||
|
||
public ClientContentServiceWithCaching(IClientContentService contentService, ICacheStorageService cacheStorageService, EmployerAccountsConfiguration employerAccountsConfiguration) | ||
{ | ||
_contentService = contentService; | ||
_cacheStorageService = cacheStorageService; | ||
_employerAccountsConfiguration = employerAccountsConfiguration; | ||
} | ||
public async Task<string> Get(string type, string applicationId) | ||
{ | ||
var cacheKey = $"{applicationId}_{type}".ToLowerInvariant(); | ||
|
||
try | ||
{ | ||
if (_cacheStorageService.TryGet(cacheKey, out string cachedContentBanner)) | ||
{ | ||
return cachedContentBanner; | ||
} | ||
|
||
var content = await _contentService.Get(type, applicationId); | ||
|
||
if (content != null) | ||
{ | ||
await _cacheStorageService.Save(cacheKey, content, _employerAccountsConfiguration.DefaultCacheExpirationInMinutes); | ||
} | ||
|
||
return content; | ||
} | ||
catch | ||
{ | ||
throw new ArgumentException($"Failed to get content for {cacheKey}"); | ||
} | ||
} | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
...ontentServiceTests/ClientContentServiceWithCachingTests/WhenIGetContentDataWithCaching.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,90 @@ | ||
using Moq; | ||
using NUnit.Framework; | ||
using SFA.DAS.EmployerFinance.Configuration; | ||
using SFA.DAS.EmployerFinance.Interfaces; | ||
using SFA.DAS.EmployerFinance.Services; | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerFinance.UnitTests.Services.ClientContentServiceTests.ClientContentServiceWithCachingTests | ||
{ | ||
class WhenIGetContentDataWithCaching | ||
{ | ||
private string _contentType; | ||
private string _clientId; | ||
|
||
private Mock<IClientContentService> MockClientContentService; | ||
private Mock<ICacheStorageService> MockCacheStorageService; | ||
|
||
private string CacheKey; | ||
private string ContentFromCache; | ||
private string ContentFromApi; | ||
private EmployerFinanceConfiguration EmployerFinanceConfig; | ||
|
||
private IClientContentService ClientContentServiceWithCaching; | ||
|
||
[SetUp] | ||
public void Arrange() | ||
{ | ||
_clientId = "eas-fin"; | ||
_contentType = "banner"; | ||
|
||
EmployerFinanceConfig = new EmployerFinanceConfiguration() | ||
{ | ||
ApplicationId = "eas-fin", | ||
DefaultCacheExpirationInMinutes = 1 | ||
}; | ||
ContentFromCache = "<p> Example content from cache </p>"; | ||
ContentFromApi = "<p> Example content from api </p>"; | ||
CacheKey = EmployerFinanceConfig.ApplicationId + "_banner"; | ||
|
||
MockClientContentService = new Mock<IClientContentService>(); | ||
MockCacheStorageService = new Mock<ICacheStorageService>(); | ||
|
||
ClientContentServiceWithCaching = new ClientContentServiceWithCaching(MockClientContentService.Object, MockCacheStorageService.Object, EmployerFinanceConfig); | ||
} | ||
[Test] | ||
public async Task AND_ItIsStoredInTheCache_THEN_ReturnContent() | ||
{ | ||
StoredInCacheSetup(); | ||
|
||
var result = await ClientContentServiceWithCaching.Get(_contentType, EmployerFinanceConfig.ApplicationId); | ||
|
||
Assert.AreEqual(ContentFromCache, result); | ||
MockCacheStorageService.Verify(c => c.TryGet(CacheKey, out ContentFromCache), Times.Once); | ||
} | ||
|
||
[Test] | ||
public async Task AND_ItIsStoredInTheCache_THEN_ContentApiIsNotCalled() | ||
{ | ||
StoredInCacheSetup(); | ||
|
||
var result = await ClientContentServiceWithCaching.Get(_contentType, EmployerFinanceConfig.ApplicationId); | ||
|
||
MockClientContentService.Verify(c => c.Get(_contentType, _clientId), Times.Never); | ||
} | ||
|
||
[Test] | ||
public async Task AND_ItIsNotStoredInTheCache_THEN_CallFromClient() | ||
{ | ||
NotStoredInCacheSetup(); | ||
|
||
var result = await ClientContentServiceWithCaching.Get(_contentType, EmployerFinanceConfig.ApplicationId); | ||
|
||
MockClientContentService.Verify(c => c.Get(_contentType, _clientId), Times.Once); | ||
Assert.AreEqual(ContentFromApi, result); | ||
} | ||
private void StoredInCacheSetup() | ||
{ | ||
MockCacheStorageService.Setup(c => c.TryGet(CacheKey, out ContentFromCache)).Returns(true); | ||
MockClientContentService.Setup(c => c.Get("banner", CacheKey)); | ||
} | ||
|
||
private void NotStoredInCacheSetup() | ||
{ | ||
|
||
MockCacheStorageService.Setup(c => c.TryGet(CacheKey, out ContentFromCache)).Returns(false); | ||
MockClientContentService.Setup(c => c.Get("banner", EmployerFinanceConfig.ApplicationId)) | ||
.ReturnsAsync(ContentFromApi); | ||
} | ||
} | ||
} |
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.