From 9e03a981a3da9b4dcdc84bc38fa4757a2f5a5af6 Mon Sep 17 00:00:00 2001 From: Steven Scaife Date: Thu, 9 Sep 2021 14:16:46 +0100 Subject: [PATCH] added unit tests for the cohort service and modified the existing MA tests to use the new concrete class --- .../Infrastructure/WhenHandlingAGetRequest.cs | 4 +- .../WhenIGetACohortsCount.cs | 73 +++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Services/CohortsServiceTests/WhenIGetACohortsCount.cs diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs index 8e3865961d..2a8b1fc74e 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs @@ -33,7 +33,7 @@ public async Task Then_The_Endpoint_Is_Called_With_Authentication_Header_And_Dat }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, $"{config.BaseUrl}{getTestRequest.GetUrl}", config.Key); var client = new HttpClient(httpMessageHandler.Object); - var apiClient = new ApiClient(client, config); + var apiClient = new ManageApprenticeshipsApiClient(client, config); //Act var actual = await apiClient.Get>(getTestRequest); @@ -57,7 +57,7 @@ public void Then_If_It_Is_Not_Successful_An_Exception_Is_Thrown() var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, $"{config.BaseUrl}{getTestRequest.GetUrl}", config.Key); var client = new HttpClient(httpMessageHandler.Object); - var apiClient = new ApiClient(client, config); + var apiClient = new ManageApprenticeshipsApiClient(client, config); //Act Assert Assert.ThrowsAsync(() => apiClient.Get>(getTestRequest)); diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Services/CohortsServiceTests/WhenIGetACohortsCount.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Services/CohortsServiceTests/WhenIGetACohortsCount.cs new file mode 100644 index 0000000000..8ae58e4938 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Services/CohortsServiceTests/WhenIGetACohortsCount.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DocumentFormat.OpenXml.Office2010.CustomUI; +using Moq; +using NUnit.Framework; +using SFA.DAS.CommitmentsV2.Types; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses; +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; +using SFA.DAS.EmployerFinance.Services; + +namespace SFA.DAS.EmployerFinance.UnitTests.Services.CohortsServiceTests +{ + public class WhenIGetACohortsCount + { + private Mock _apiClient; + private CohortsService _service; + + [SetUp] + public void Setup() + { + _apiClient = new Mock(); + _service = new CohortsService(_apiClient.Object); + } + + [Test] + public async Task ThenTheApiIsCalledWithAValidAccountIdAndTheCohortsCountIsReturned() + { + SetupApiClient(1); + + var actual = await _service.GetCohortsCount(1); + + Assert.AreEqual(1, actual); + } + + [Test] + public async Task ThenTheApiIsCalledWithAnInvalidAccountIdAndTheCohortsCountIsZero() + { + SetupApiClient(0, false); + + var actual = await _service.GetCohortsCount(0); + + Assert.AreEqual(0, actual); + } + + private void SetupApiClient(long accountId, bool addItem = true) + { + var items = new List(); + + if (addItem) + { + items = new List + { + new CohortSummary + { + AccountId = accountId + } + }; + } + + _apiClient.Setup(o => + o.Get( + It.Is(i => i.GetUrl.Equals($"api/cohorts?accountId={accountId}")))) + .ReturnsAsync(new GetCohortsResponse + { + Cohorts = items + }); + } + } +}