Skip to content

Commit

Permalink
Remove IContentService
Browse files Browse the repository at this point in the history
  • Loading branch information
kola-samuel committed Mar 29, 2021
1 parent f2b2df9 commit a43d4b9
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class WhenIGetContentBanner : QueryBaseTest<GetContentRequestHandler, Get
public override Mock<IValidator<GetContentRequest>> RequestValidator { get; set; }

public Mock<ICacheStorageService> MockCacheStorageService;
private Mock<IContentService> _contentBannerService;
private Mock<IContentApiClient> _contentBannerService;
private string _contentType;
private string _clientId;
private Mock<ILog> _logger;
Expand All @@ -37,7 +37,7 @@ public void Arrange()
_contentType = "banner";
_clientId = "eas-acc";
_logger = new Mock<ILog>();
_contentBannerService = new Mock<IContentService>();
_contentBannerService = new Mock<IContentApiClient>();
_contentBannerService
.Setup(cbs => cbs.Get(_contentType, _clientId))
.ReturnsAsync(ContentBanner);
Expand All @@ -47,8 +47,7 @@ public void Arrange()
ContentType = "banner"
};

RequestHandler = new GetContentRequestHandler(RequestValidator.Object, _logger.Object,
_contentBannerService.Object, MockCacheStorageService.Object, EmployerAccountsConfiguration);
RequestHandler = new GetContentRequestHandler(RequestValidator.Object, _logger.Object, _contentBannerService.Object, EmployerAccountsConfiguration);
}

[Test]
Expand Down Expand Up @@ -76,7 +75,7 @@ public async Task Check_Cache_ReturnNull_CallFromClient(GetContentRequest query1
GetContentRequestHandler requestHandler1,
Mock<IValidator<GetContentRequest>> requestValidator1,
Mock<ILog> logger,
Mock<IContentService> MockContentService)
Mock<IContentApiClient> MockContentService)
{
//Arrange
var key = EmployerAccountsConfiguration.ApplicationId;
Expand All @@ -93,8 +92,7 @@ public async Task Check_Cache_ReturnNull_CallFromClient(GetContentRequest query1
MockContentService.Setup(c => c.Get(query1.ContentType, key))
.ReturnsAsync(contentBanner1);

requestHandler1 = new GetContentRequestHandler(requestValidator1.Object, logger.Object, MockContentService.Object,
cacheStorageService1.Object, EmployerAccountsConfiguration);
requestHandler1 = new GetContentRequestHandler(requestValidator1.Object, logger.Object, MockContentService.Object, EmployerAccountsConfiguration);

//Act
var result = await requestHandler1.Handle(query1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@

namespace SFA.DAS.EmployerAccounts.UnitTests.Services.ContentBanner
{
public class WhenIGetContentWithCaching
public class WhenIGetContentApiClientwITHCaching
{
private string _contentType;
private string _clientId;

private Mock<IContentService> MockContentService;
private Mock<IContentApiClient> MockContentService;
private Mock<ICacheStorageService> MockCacheStorageService;

private string CacheKey;
private string ContentFromCache;
private string ContentFromApi;
private EmployerAccountsConfiguration EmployerAccountsConfig;

private IContentService ContentServiceWithCaching;
private IContentApiClient ContentServiceWithCaching;

[SetUp]

public void Arrange()
{
_clientId = "eas-fin";
Expand All @@ -39,10 +40,10 @@ public void Arrange()
ContentFromApi = "<p> Example content from api </p>";
CacheKey = EmployerAccountsConfig.ApplicationId + "_banner";

MockContentService = new Mock<IContentService>();
MockContentService = new Mock<IContentApiClient>();
MockCacheStorageService = new Mock<ICacheStorageService>();

ContentServiceWithCaching = new ContentServiceWithCaching(MockContentService.Object, MockCacheStorageService.Object, EmployerAccountsConfig);
ContentServiceWithCaching = new ContentApiClientWithCaching(MockContentService.Object, MockCacheStorageService.Object, EmployerAccountsConfig);
}

[Test]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public ContentApiClientRegistry()
For<ContentClientApiConfiguration>().Use(c => c.GetInstance<EmployerAccountsConfiguration>().ContentApi);
For<IContentClientApiConfiguration>().Use(c => c.GetInstance<ContentClientApiConfiguration>());
For<IContentApiClient>().Use<ContentApiClient>().Ctor<HttpClient>().Is(c => CreateClient(c));
For<IContentApiClient>().DecorateAllWith<ContentApiClientWithCaching>().Ctor<HttpClient>().Is(c => CreateClient(c));
}

private HttpClient CreateClient(IContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public ServicesRegistry()
For<IReservationsService>().DecorateAllWith<ReservationsServiceWithTimeout>();
For<ICommitmentV2Service>().Use<CommitmentsV2Service>();
For<ICommitmentV2Service>().DecorateAllWith<CommitmentsV2ServiceWithTimeout>();
For<IContentService>().Use<ContentService>();
For<IContentService>().DecorateAllWith<ContentServiceWithCaching>();
}
}
}
9 changes: 0 additions & 9 deletions src/SFA.DAS.EmployerAccounts/Interfaces/IContentService.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ public class GetContentRequestHandler : IAsyncRequestHandler<GetContentRequest,
{
private readonly IValidator<GetContentRequest> _validator;
private readonly ILog _logger;
private readonly IContentService _service;
private readonly ICacheStorageService _cacheStorageService;
private readonly IContentApiClient _contentApiClient;
private readonly EmployerAccountsConfiguration _employerAccountsConfiguration;

public GetContentRequestHandler(
IValidator<GetContentRequest> validator,
ILog logger,
IContentService service, ICacheStorageService cacheStorageService, EmployerAccountsConfiguration employerAccountsConfiguration)
IContentApiClient contentApiClient, EmployerAccountsConfiguration employerAccountsConfiguration)
{
_validator = validator;
_logger = logger;
_service = service;
_cacheStorageService = cacheStorageService;
_contentApiClient = contentApiClient;
_employerAccountsConfiguration = employerAccountsConfiguration;
}

Expand All @@ -41,9 +39,8 @@ public async Task<GetContentResponse> Handle(GetContentRequest message)

try
{
var contentBanner = await _service.Get(message.ContentType, applicationId);
var contentBanner = await _contentApiClient.Get(message.ContentType, applicationId);


return new GetContentResponse
{
Content = contentBanner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
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 ContentServiceWithCaching : IContentService
public class ContentApiClientWithCaching : IContentApiClient
{
private readonly IContentService _contentService;
private readonly IContentApiClient _contentService;
private readonly ICacheStorageService _cacheStorageService;
private readonly EmployerAccountsConfiguration _employerAccountsConfiguration;

public ContentServiceWithCaching(IContentService contentService, ICacheStorageService cacheStorageService, EmployerAccountsConfiguration employerAccountsConfiguration)
public ContentApiClientWithCaching(IContentApiClient contentService, ICacheStorageService cacheStorageService, EmployerAccountsConfiguration employerAccountsConfiguration)
{
_contentService = contentService;
_cacheStorageService = cacheStorageService;
Expand Down
22 changes: 0 additions & 22 deletions src/SFA.DAS.EmployerAccounts/Services/ContentService.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
using SFA.DAS.EmployerFinance.Services;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerFinance.UnitTests.Services.ContentServiceTests.ContentServiceWithCachingTests
namespace SFA.DAS.EmployerFinance.UnitTests.Services.ContentApiClientWithCachingTests
{
class WhenIGetContentDataWithCaching
class WhenIGetContentApiClientWithCaching
{
private string _contentType;
private string _clientId;

private Mock<IContentService> MockContentService;
private Mock<IContentApiClient> MockContentApiClient;
private Mock<ICacheStorageService> MockCacheStorageService;

private string CacheKey;
private string ContentFromCache;
private string ContentFromApi;
private EmployerFinanceConfiguration EmployerFinanceConfig;

private IContentService ContentServiceWithCaching;
private IContentApiClient ContentApiClientWithCaching;

[SetUp]
public void Arrange()
Expand All @@ -37,17 +37,17 @@ public void Arrange()
ContentFromApi = "<p> Example content from api </p>";
CacheKey = EmployerFinanceConfig.ApplicationId + "_banner";

MockContentService = new Mock<IContentService>();
MockContentApiClient = new Mock<IContentApiClient>();
MockCacheStorageService = new Mock<ICacheStorageService>();

ContentServiceWithCaching = new ContentServiceWithCaching(MockContentService.Object, MockCacheStorageService.Object, EmployerFinanceConfig);
ContentApiClientWithCaching = new ContentApiClientWithCaching(MockContentApiClient.Object, MockCacheStorageService.Object, EmployerFinanceConfig);
}
[Test]
public async Task AND_ItIsStoredInTheCache_THEN_ReturnContent()
{
StoredInCacheSetup();

var result = await ContentServiceWithCaching.Get(_contentType, EmployerFinanceConfig.ApplicationId);
var result = await ContentApiClientWithCaching.Get(_contentType, EmployerFinanceConfig.ApplicationId);

Assert.AreEqual(ContentFromCache, result);
MockCacheStorageService.Verify(c => c.TryGet(CacheKey, out ContentFromCache), Times.Once);
Expand All @@ -58,32 +58,32 @@ public async Task AND_ItIsStoredInTheCache_THEN_ContentApiIsNotCalled()
{
StoredInCacheSetup();

var result = await ContentServiceWithCaching.Get(_contentType, EmployerFinanceConfig.ApplicationId);
var result = await ContentApiClientWithCaching.Get(_contentType, EmployerFinanceConfig.ApplicationId);

MockContentService.Verify(c => c.Get(_contentType, _clientId), Times.Never);
MockContentApiClient.Verify(c => c.Get(_contentType, _clientId), Times.Never);
}

[Test]
public async Task AND_ItIsNotStoredInTheCache_THEN_CallFromClient()
{
NotStoredInCacheSetup();

var result = await ContentServiceWithCaching.Get(_contentType, EmployerFinanceConfig.ApplicationId);
var result = await ContentApiClientWithCaching.Get(_contentType, EmployerFinanceConfig.ApplicationId);

MockContentService.Verify(c => c.Get(_contentType, _clientId), Times.Once);
MockContentApiClient.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);
MockContentService.Setup(c => c.Get("banner", CacheKey));
MockContentApiClient.Setup(c => c.Get("banner", CacheKey));
}

private void NotStoredInCacheSetup()
{

MockCacheStorageService.Setup(c => c.TryGet(CacheKey, out ContentFromCache)).Returns(false);
MockContentService.Setup(c => c.Get("banner", EmployerFinanceConfig.ApplicationId))
MockContentApiClient.Setup(c => c.Get("banner", EmployerFinanceConfig.ApplicationId))
.ReturnsAsync(ContentFromApi);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public static IContainer Initialize()
c.AddRegistry<EmployerFeaturesAuthorizationRegistry>();
c.AddRegistry<EmployerUserRolesAuthorizationRegistry>();
c.AddRegistry<ContentApiClientRegistry>();
c.AddRegistry<ServicesRegistry>();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public ContentApiClientRegistry()
For<ContentClientApiConfiguration>().Use(c => c.GetInstance<EmployerFinanceConfiguration>().ContentApi);
For<IContentClientApiConfiguration>().Use(c => c.GetInstance<ContentClientApiConfiguration>());
For<IContentApiClient>().Use<ContentApiClient>().Ctor<HttpClient>().Is(c => CreateClient(c));
For<IContentApiClient>().DecorateAllWith<ContentApiClientWithCaching>().Ctor<HttpClient>().Is(c => CreateClient(c));
}
private HttpClient CreateClient(IContext context)
{
Expand Down
Loading

0 comments on commit a43d4b9

Please sign in to comment.