From a2ebb98a4e35f173583b3c8005267e7dc0d610db Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Thu, 24 Dec 2020 07:43:52 +0000 Subject: [PATCH 01/13] FAT2-295 - Add HttpClient for calling APIM --- .../Infrastructure/WhenHandlingAGetRequest.cs | 94 +++++++++++++++++++ .../SFA.DAS.EmployerFinance.UnitTests.csproj | 1 + ...ageApprenticeshipsOuterApiConfiguration.cs | 10 ++ .../Infrastructure/ApiClient.cs | 41 ++++++++ .../Interfaces/OuterApi/IApiClient.cs | 9 ++ .../Interfaces/OuterApi/IGetApiRequest.cs | 7 ++ 6 files changed, 162 insertions(+) create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs create mode 100644 src/SFA.DAS.EmployerFinance/Configuration/ManageApprenticeshipsOuterApiConfiguration.cs create mode 100644 src/SFA.DAS.EmployerFinance/Infrastructure/ApiClient.cs create mode 100644 src/SFA.DAS.EmployerFinance/Interfaces/OuterApi/IApiClient.cs create mode 100644 src/SFA.DAS.EmployerFinance/Interfaces/OuterApi/IGetApiRequest.cs diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs new file mode 100644 index 0000000000..ff9033fb92 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs @@ -0,0 +1,94 @@ +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture.NUnit3; +using FluentAssertions; +using Moq; +using Moq.Protected; +using Newtonsoft.Json; +using NUnit.Framework; +using SFA.DAS.EmployerFinance.Configuration; +using SFA.DAS.EmployerFinance.Infrastructure; +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; + +namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure +{ + public class WhenHandlingAGetRequest + { + [Test, AutoData] + public async Task Then_The_Endpoint_Is_Called_With_Authentication_Header_And_Data_Returned( + List testObject, + string key, + GetTestRequest getTestRequest) + { + //Arrange + var config = new ManageApprenticeshipsOuterApiConfiguration {BaseUrl = "http://valid-url/", Key = key}; + + var response = new HttpResponseMessage + { + Content = new StringContent(JsonConvert.SerializeObject(testObject)), + StatusCode = HttpStatusCode.Accepted + }; + var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, $"{config.BaseUrl}{getTestRequest.GetUrl}", config.Key); + var client = new HttpClient(httpMessageHandler.Object); + var apiClient = new ApiClient(client, config); + + //Act + var actual = await apiClient.Get>(getTestRequest); + + //Assert + actual.Should().BeEquivalentTo(testObject); + } + + [Test, AutoData] + public void Then_If_It_Is_Not_Successful_An_Exception_Is_Thrown( + string key, + GetTestRequest getTestRequest) + { + //Arrange + var config = new ManageApprenticeshipsOuterApiConfiguration {BaseUrl = "http://valid-url/", Key = key }; + var response = new HttpResponseMessage + { + Content = new StringContent(""), + StatusCode = HttpStatusCode.BadRequest + }; + + var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, $"{config.BaseUrl}{getTestRequest.GetUrl}", config.Key); + var client = new HttpClient(httpMessageHandler.Object); + var apiClient = new ApiClient(client, config); + + //Act Assert + Assert.ThrowsAsync(() => apiClient.Get>(getTestRequest)); + + } + + public class GetTestRequest : IGetApiRequest + { + public string GetUrl => "test-url/get"; + } + } + public static class MessageHandler + { + public static Mock SetupMessageHandlerMock(HttpResponseMessage response, string url, string key) + { + var httpMessageHandler = new Mock(); + httpMessageHandler.Protected() + .Setup>( + "SendAsync", + ItExpr.Is(c => + c.Method.Equals(HttpMethod.Get) + && c.Headers.Contains("Ocp-Apim-Subscription-Key") + && c.Headers.GetValues("Ocp-Apim-Subscription-Key").First().Equals(key) + && c.Headers.Contains("X-Version") + && c.Headers.GetValues("X-Version").First().Equals("1") + && c.RequestUri.AbsoluteUri.Equals(url)), + ItExpr.IsAny() + ) + .ReturnsAsync((HttpRequestMessage request, CancellationToken token) => response); + return httpMessageHandler; + } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj b/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj index 0b2f1bf005..27b2712c0b 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj +++ b/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj @@ -12,6 +12,7 @@ + diff --git a/src/SFA.DAS.EmployerFinance/Configuration/ManageApprenticeshipsOuterApiConfiguration.cs b/src/SFA.DAS.EmployerFinance/Configuration/ManageApprenticeshipsOuterApiConfiguration.cs new file mode 100644 index 0000000000..b050d6fd6c --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Configuration/ManageApprenticeshipsOuterApiConfiguration.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace SFA.DAS.EmployerFinance.Configuration +{ + public class ManageApprenticeshipsOuterApiConfiguration + { + public string BaseUrl { get ; set ; } + public string Key { get ; set ; } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/ApiClient.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/ApiClient.cs new file mode 100644 index 0000000000..44cc1d2a9b --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/ApiClient.cs @@ -0,0 +1,41 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; +using SFA.DAS.EmployerFinance.Configuration; +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; + +namespace SFA.DAS.EmployerFinance.Infrastructure +{ + public class ApiClient : IApiClient + { + private readonly HttpClient _httpClient; + private readonly ManageApprenticeshipsOuterApiConfiguration _config; + + public ApiClient ( + HttpClient httpClient, + ManageApprenticeshipsOuterApiConfiguration options) + { + _httpClient = httpClient; + _config = options; + _httpClient.BaseAddress = new Uri(_config.BaseUrl); + } + + public async Task Get(IGetApiRequest request) + { + AddHeaders(); + + var response = await _httpClient.GetAsync(request.GetUrl).ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); + return JsonConvert.DeserializeObject(json); + } + + private void AddHeaders() + { + _httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _config.Key); + _httpClient.DefaultRequestHeaders.Add("X-Version", "1"); + } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Interfaces/OuterApi/IApiClient.cs b/src/SFA.DAS.EmployerFinance/Interfaces/OuterApi/IApiClient.cs new file mode 100644 index 0000000000..2c485536c1 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Interfaces/OuterApi/IApiClient.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerFinance.Interfaces.OuterApi +{ + public interface IApiClient + { + Task Get(IGetApiRequest request); + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Interfaces/OuterApi/IGetApiRequest.cs b/src/SFA.DAS.EmployerFinance/Interfaces/OuterApi/IGetApiRequest.cs new file mode 100644 index 0000000000..f9d7f55134 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Interfaces/OuterApi/IGetApiRequest.cs @@ -0,0 +1,7 @@ +namespace SFA.DAS.EmployerFinance.Interfaces.OuterApi +{ + public interface IGetApiRequest + { + string GetUrl { get; } + } +} \ No newline at end of file From b8a63b5da32cbcb5c2895b812f06ef2dd6df676d Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Thu, 24 Dec 2020 07:54:08 +0000 Subject: [PATCH 02/13] FAT2-295 - Add outer API requests --- .../WhenBuildingGetFrameworksRequest.cs | 19 ++++++++++++++++++ .../WhenBuildingGetProvidersRequest.cs | 19 ++++++++++++++++++ .../WhenBuildingGetStandardsRequest.cs | 20 +++++++++++++++++++ .../OuterApiRequests/GetFrameworksRequest.cs | 9 +++++++++ .../OuterApiRequests/GetProvidersRequest.cs | 9 +++++++++ .../OuterApiRequests/GetStandardsRequest.cs | 9 +++++++++ 6 files changed, 85 insertions(+) create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetFrameworksRequest.cs create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetProvidersRequest.cs create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetStandardsRequest.cs create mode 100644 src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetFrameworksRequest.cs create mode 100644 src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProvidersRequest.cs create mode 100644 src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetStandardsRequest.cs diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetFrameworksRequest.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetFrameworksRequest.cs new file mode 100644 index 0000000000..e56ab38098 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetFrameworksRequest.cs @@ -0,0 +1,19 @@ +using FluentAssertions; +using NUnit.Framework; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; + +namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiRequests +{ + public class WhenBuildingGetFrameworksRequest + { + [Test] + public void Then_The_Url_Is_Correctly_Constructed() + { + //Arrange + var actual = new GetFrameworksRequest(); + + //Act + actual.GetUrl.Should().Be("TrainingCourses/frameworks"); + } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetProvidersRequest.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetProvidersRequest.cs new file mode 100644 index 0000000000..6bb9047085 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetProvidersRequest.cs @@ -0,0 +1,19 @@ +using FluentAssertions; +using NUnit.Framework; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; + +namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiRequests +{ + public class WhenBuildingGetProvidersRequest + { + [Test] + public void Then_The_Url_Is_Correctly_Constructed() + { + //Arrange + var actual = new GetProvidersRequest(); + + //Act + actual.GetUrl.Should().Be("providers"); + } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetStandardsRequest.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetStandardsRequest.cs new file mode 100644 index 0000000000..094bb7d3b6 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetStandardsRequest.cs @@ -0,0 +1,20 @@ +using AutoFixture.NUnit3; +using FluentAssertions; +using NUnit.Framework; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; + +namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiRequests +{ + public class WhenBuildingGetStandardsRequest + { + [Test] + public void Then_The_Url_Is_Correctly_Constructed() + { + //Arrange + var actual = new GetStandardsRequest(); + + //Act + actual.GetUrl.Should().Be("TrainingCourses/standards"); + } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetFrameworksRequest.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetFrameworksRequest.cs new file mode 100644 index 0000000000..bd1212be2f --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetFrameworksRequest.cs @@ -0,0 +1,9 @@ +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; + +namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests +{ + public class GetFrameworksRequest: IGetApiRequest + { + public string GetUrl => "TrainingCourses/frameworks"; + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProvidersRequest.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProvidersRequest.cs new file mode 100644 index 0000000000..7bcd4da4e6 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProvidersRequest.cs @@ -0,0 +1,9 @@ +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; + +namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests +{ + public class GetProvidersRequest: IGetApiRequest + { + public string GetUrl => "providers"; + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetStandardsRequest.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetStandardsRequest.cs new file mode 100644 index 0000000000..9b382423fc --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetStandardsRequest.cs @@ -0,0 +1,9 @@ +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; + +namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests +{ + public class GetStandardsRequest : IGetApiRequest + { + public string GetUrl => "TrainingCourses/standards"; + } +} \ No newline at end of file From 2b084526d76d2ae5c8da37b206456e2d7ceaefe3 Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Thu, 24 Dec 2020 08:21:12 +0000 Subject: [PATCH 03/13] FAT2-295 - Add outer api response types --- .../GetFrameworksResponse.cs | 44 +++++++++++++++++++ .../OuterApiResponses/GetProvidersResponse.cs | 29 ++++++++++++ .../OuterApiResponses/GetStandardsResponse.cs | 29 ++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetProvidersResponse.cs create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetStandardsResponse.cs diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs new file mode 100644 index 0000000000..674b471b6f --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiResponses +{ + public class GetFrameworksResponse + { + [JsonProperty("frameworks")] + public List Frameworks { get; set; } + } + + public class FrameworkResponseItem + { + [JsonProperty("id")] + public string Id { get; set; } + + [JsonProperty("frameworkName")] + public string FrameworkName { get; set; } + + [JsonProperty("pathwayName")] + public string PathwayName { get; set; } + + [JsonProperty("title")] + public string Title { get; set; } + + [JsonProperty("level")] + public long Level { get; set; } + + [JsonProperty("frameworkCode")] + public long FrameworkCode { get; set; } + + [JsonProperty("progType")] + public long ProgType { get; set; } + + [JsonProperty("pathwayCode")] + public long PathwayCode { get; set; } + + [JsonProperty("maxFunding")] + public long MaxFunding { get; set; } + + [JsonProperty("duration")] + public long Duration { get; set; } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetProvidersResponse.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetProvidersResponse.cs new file mode 100644 index 0000000000..b29af9251c --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetProvidersResponse.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiResponses +{ + public class GetProvidersResponse + { + [JsonProperty("providers")] + public List Providers { get; set; } + } + + public class ProviderResponseItem + { + [JsonProperty("ukprn")] + public long Ukprn { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("contactUrl")] + public string ContactUrl { get; set; } + + [JsonProperty("email")] + public string Email { get; set; } + + [JsonProperty("phone")] + public string Phone { get; set; } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetStandardsResponse.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetStandardsResponse.cs new file mode 100644 index 0000000000..3f86c5e8ef --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetStandardsResponse.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiResponses +{ + public class GetStandardsResponse + { + [JsonProperty("standards")] + public List Standards { get; set; } + } + + public class StandardResponseItem + { + [JsonProperty("id")] + public long Id { get; set; } + + [JsonProperty("level")] + public long Level { get; set; } + + [JsonProperty("title")] + public string Title { get; set; } + + [JsonProperty("duration")] + public long Duration { get; set; } + + [JsonProperty("maxFunding")] + public long MaxFunding { get; set; } + } +} \ No newline at end of file From 1be72eb78332427f4436841a8984f14f67839a56 Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Thu, 24 Dec 2020 08:31:02 +0000 Subject: [PATCH 04/13] FAT2-295 - Move to correct project! --- .../Infrastructure/OuterApiResponses/GetFrameworksResponse.cs | 2 +- .../Infrastructure/OuterApiResponses/GetProvidersResponse.cs | 2 +- .../Infrastructure/OuterApiResponses/GetStandardsResponse.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/{SFA.DAS.EmployerFinance.UnitTests => SFA.DAS.EmployerFinance}/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs (93%) rename src/{SFA.DAS.EmployerFinance.UnitTests => SFA.DAS.EmployerFinance}/Infrastructure/OuterApiResponses/GetProvidersResponse.cs (89%) rename src/{SFA.DAS.EmployerFinance.UnitTests => SFA.DAS.EmployerFinance}/Infrastructure/OuterApiResponses/GetStandardsResponse.cs (89%) diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs similarity index 93% rename from src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs rename to src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs index 674b471b6f..1fca60018d 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using Newtonsoft.Json; -namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiResponses +namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses { public class GetFrameworksResponse { diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetProvidersResponse.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs similarity index 89% rename from src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetProvidersResponse.cs rename to src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs index b29af9251c..4eec453632 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetProvidersResponse.cs +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using Newtonsoft.Json; -namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiResponses +namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses { public class GetProvidersResponse { diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetStandardsResponse.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetStandardsResponse.cs similarity index 89% rename from src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetStandardsResponse.cs rename to src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetStandardsResponse.cs index 3f86c5e8ef..3820438ee4 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiResponses/GetStandardsResponse.cs +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetStandardsResponse.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using Newtonsoft.Json; -namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiResponses +namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses { public class GetStandardsResponse { From 085ed72c69861b1b811f6fc727b0558c33c0ca63 Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Thu, 24 Dec 2020 10:06:40 +0000 Subject: [PATCH 05/13] FAT2-295 - Correct property types --- .../OuterApiResponses/GetFrameworksResponse.cs | 12 ++++++------ .../OuterApiResponses/GetProvidersResponse.cs | 2 +- .../OuterApiResponses/GetStandardsResponse.cs | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs index 1fca60018d..402e76a40f 100644 --- a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetFrameworksResponse.cs @@ -24,21 +24,21 @@ public class FrameworkResponseItem public string Title { get; set; } [JsonProperty("level")] - public long Level { get; set; } + public int Level { get; set; } [JsonProperty("frameworkCode")] - public long FrameworkCode { get; set; } + public int FrameworkCode { get; set; } [JsonProperty("progType")] - public long ProgType { get; set; } + public int ProgType { get; set; } [JsonProperty("pathwayCode")] - public long PathwayCode { get; set; } + public int PathwayCode { get; set; } [JsonProperty("maxFunding")] - public long MaxFunding { get; set; } + public int MaxFunding { get; set; } [JsonProperty("duration")] - public long Duration { get; set; } + public int Duration { get; set; } } } \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs index 4eec453632..6fe82521ec 100644 --- a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs @@ -12,7 +12,7 @@ public class GetProvidersResponse public class ProviderResponseItem { [JsonProperty("ukprn")] - public long Ukprn { get; set; } + public int Ukprn { get; set; } [JsonProperty("name")] public string Name { get; set; } diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetStandardsResponse.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetStandardsResponse.cs index 3820438ee4..91f9063ea4 100644 --- a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetStandardsResponse.cs +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetStandardsResponse.cs @@ -12,18 +12,18 @@ public class GetStandardsResponse public class StandardResponseItem { [JsonProperty("id")] - public long Id { get; set; } + public int Id { get; set; } [JsonProperty("level")] - public long Level { get; set; } + public int Level { get; set; } [JsonProperty("title")] public string Title { get; set; } [JsonProperty("duration")] - public long Duration { get; set; } + public int Duration { get; set; } [JsonProperty("maxFunding")] - public long MaxFunding { get; set; } + public int MaxFunding { get; set; } } } \ No newline at end of file From 5d25d8f6350accff21998266779437e8b2bd76c8 Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Thu, 24 Dec 2020 10:07:18 +0000 Subject: [PATCH 06/13] FAT2-295 - update wrapper to use api client Added tests to cover getting the standards and frameworks --- .../SFA.DAS.EmployerFinance.UnitTests.csproj | 1 + .../WhenGettingApprenticeshipInfo.cs | 118 ++++++++++++++++++ .../ApprenticeshipInfoServiceWrapper.cs | 50 ++++---- 3 files changed, 140 insertions(+), 29 deletions(-) create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj b/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj index 27b2712c0b..b24e96996c 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj +++ b/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj @@ -23,6 +23,7 @@ + diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs new file mode 100644 index 0000000000..3e968005eb --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs @@ -0,0 +1,118 @@ +using System.Linq; +using System.Threading.Tasks; +using AutoFixture.NUnit3; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using SFA.DAS.Caches; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses; +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; +using SFA.DAS.EmployerFinance.Models.ApprenticeshipCourse; +using SFA.DAS.EmployerFinance.Services; +using SFA.DAS.Testing.AutoFixture; + +namespace SFA.DAS.EmployerFinance.UnitTests.Services.ApprenticeshipInfoServiceWrapperTests +{ + public class WhenGettingApprenticeshipInfo + { + [Test, MoqAutoData] + public async Task Then_The_Standards_Are_Retrieved_From_The_Api_And_Added_To_Cached( + GetStandardsResponse apiResponse, + [Frozen]Mock apiClient, + [Frozen]Mock cache, + ApprenticeshipInfoServiceWrapper service) + { + //Arrange + StandardsView returnData = null; + apiClient.Setup(x => x.Get(It.IsAny())) + .ReturnsAsync(apiResponse); + cache.Setup(x => x.Exists("Standards")).Returns(false); + cache.Setup(x => x.Set("Standards", It.IsAny())).Callback( + (key, value) => + { + returnData = value as StandardsView; + }); + + //Act + await service.GetStandardsAsync(); + + //Assert + apiClient.Verify(x=>x.Get(It.IsAny()),Times.Once); + returnData.Standards.Count.Should().Be(apiResponse.Standards.Count); + returnData.Standards.ToList().First().ShouldBeEquivalentTo(apiResponse.Standards.First(), options=> options + .Excluding(c=>c.Title) + .Excluding(c=>c.Code) + .Excluding(c=>c.CourseName) + ); + } + + [Test, MoqAutoData] + public async Task Then_Standards_Retrieved_From_Cache_If_Cached( + StandardsView cacheData, + [Frozen]Mock apiClient, + [Frozen]Mock cache, + ApprenticeshipInfoServiceWrapper service) + { + //Arrange + cache.Setup(x => x.Exists("Standards")).Returns(true); + cache.Setup(x => x.Get("Standards")).Returns(cacheData); + + //Act + var actual = await service.GetStandardsAsync(); + + //Assert + apiClient.Verify(x=>x.Get(It.IsAny()),Times.Never); + actual.ShouldBeEquivalentTo(cacheData); + } + + [Test, MoqAutoData] + public async Task Then_The_Frameworks_Are_Retrieved_From_The_Api_And_Added_To_Cached( + GetFrameworksResponse apiResponse, + [Frozen]Mock apiClient, + [Frozen]Mock cache, + ApprenticeshipInfoServiceWrapper service) + { + //Arrange + FrameworksView returnData = null; + apiClient.Setup(x => x.Get(It.IsAny())) + .ReturnsAsync(apiResponse); + cache.Setup(x => x.Exists("Frameworks")).Returns(false); + cache.Setup(x => x.Set("Frameworks", It.IsAny())).Callback( + (key, value) => + { + returnData = value as FrameworksView; + }); + + //Act + await service.GetFrameworksAsync(); + + //Assert + apiClient.Verify(x=>x.Get(It.IsAny()),Times.Once); + returnData.Frameworks.Count.Should().Be(apiResponse.Frameworks.Count); + returnData.Frameworks.ToList().First().ShouldBeEquivalentTo(apiResponse.Frameworks.First(), options=> options + .Excluding(c=>c.Title) + .Excluding(c=>c.ProgrammeType) + ); + } + [Test, MoqAutoData] + public async Task Then_Frameworks_Retrieved_From_Cache_If_Cached( + FrameworksView cacheData, + [Frozen]Mock apiClient, + [Frozen]Mock cache, + ApprenticeshipInfoServiceWrapper service) + { + //Arrange + cache.Setup(x => x.Exists("Frameworks")).Returns(true); + cache.Setup(x => x.Get("Frameworks")).Returns(cacheData); + + //Act + var actual = await service.GetFrameworksAsync(); + + //Assert + apiClient.Verify(x=>x.Get(It.IsAny()),Times.Never); + actual.ShouldBeEquivalentTo(cacheData); + } + + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Services/ApprenticeshipInfoServiceWrapper.cs b/src/SFA.DAS.EmployerFinance/Services/ApprenticeshipInfoServiceWrapper.cs index 3280e63286..1bb663d085 100644 --- a/src/SFA.DAS.EmployerFinance/Services/ApprenticeshipInfoServiceWrapper.cs +++ b/src/SFA.DAS.EmployerFinance/Services/ApprenticeshipInfoServiceWrapper.cs @@ -2,14 +2,12 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using SFA.DAS.Apprenticeships.Api.Client; -using SFA.DAS.Apprenticeships.Api.Types; using SFA.DAS.Caches; -using SFA.DAS.EmployerFinance.Configuration; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses; +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; using SFA.DAS.EmployerFinance.Models.ApprenticeshipCourse; using SFA.DAS.EmployerFinance.Models.ApprenticeshipProvider; -using Framework = SFA.DAS.EmployerFinance.Models.ApprenticeshipCourse.Framework; -using Standard = SFA.DAS.EmployerFinance.Models.ApprenticeshipCourse.Standard; namespace SFA.DAS.EmployerFinance.Services { @@ -19,45 +17,39 @@ public class ApprenticeshipInfoServiceWrapper : IApprenticeshipInfoServiceWrappe private const string FrameworksKey = "Frameworks"; private readonly IInProcessCache _cache; - private readonly string _apprenticeshipInfoServiceApiBase; + private readonly IApiClient _apiClient; - public ApprenticeshipInfoServiceWrapper(IInProcessCache cache, EmployerFinanceConfiguration config) + public ApprenticeshipInfoServiceWrapper(IInProcessCache cache, IApiClient apiClient) { _cache = cache ?? throw new ArgumentNullException(nameof(cache)); - _apprenticeshipInfoServiceApiBase = config?.ApprenticeshipInfoService?.BaseUrl; + _apiClient = apiClient; } - public Task GetStandardsAsync(bool refreshCache = false) + public async Task GetStandardsAsync(bool refreshCache = false) { if (!_cache.Exists(StandardsKey) || refreshCache) { - var api = new StandardApiClient(_apprenticeshipInfoServiceApiBase); + var response = await _apiClient.Get(new GetStandardsRequest()); - //BUG: FindAll should be FindAllAsync - currently a blocking call. - var standards = api.FindAll().OrderBy(x => x.Title).ToList(); - - _cache.Set(StandardsKey, MapFrom(standards)); + _cache.Set(StandardsKey, MapFrom(response.Standards)); } - return Task.FromResult(_cache.Get(StandardsKey)); + return _cache.Get(StandardsKey); } - public Task GetFrameworksAsync(bool refreshCache = false) + public async Task GetFrameworksAsync(bool refreshCache = false) { if (!_cache.Exists(FrameworksKey) || refreshCache) { - var api = new FrameworkApiClient(_apprenticeshipInfoServiceApiBase); - - //BUG: FindAll should be FindAllAsync - var frameworks = api.FindAll().OrderBy(x => x.Title).ToList(); + var response = await _apiClient.Get(new GetFrameworksRequest()); - _cache.Set(FrameworksKey, MapFrom(frameworks)); + _cache.Set(FrameworksKey, MapFrom(response.Frameworks)); } - return Task.FromResult(_cache.Get(FrameworksKey)); + return _cache.Get(FrameworksKey); } - private static FrameworksView MapFrom(List frameworks) + private static FrameworksView MapFrom(List frameworks) { return new FrameworksView { @@ -78,7 +70,7 @@ private static FrameworksView MapFrom(List frameworks) }; } - private static ProvidersView MapFrom(Apprenticeships.Api.Types.Providers.Provider provider) + private static ProvidersView MapFrom(ProviderResponseItem provider) { return new ProvidersView { @@ -86,23 +78,23 @@ private static ProvidersView MapFrom(Apprenticeships.Api.Types.Providers.Provide Provider = new Models.ApprenticeshipProvider.Provider() { Ukprn = provider.Ukprn, - Name = provider.ProviderName, + Name = provider.Name, Email = provider.Email, Phone = provider.Phone, - NationalProvider = provider.NationalProvider + NationalProvider = false // Obsolete - no longer valid at this level } }; } - private static StandardsView MapFrom(List standards) + private static StandardsView MapFrom(List standards) { return new StandardsView { CreationDate = DateTime.UtcNow, Standards = standards.Select(x => new Standard { - Id = x.Id, - Code = long.Parse(x.Id), + Id = x.Id.ToString(), + Code = x.Id, Level = x.Level, Title = GetTitle(x.Title, x.Level) + " (Standard)", CourseName = x.Title, From bf9c5c6cc4ca4393af827f38dae661824127e318 Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Thu, 24 Dec 2020 10:28:55 +0000 Subject: [PATCH 07/13] FAT2-295 - Add request for getting provider by id --- .../WhenBuildingTheGetProviderByIdRequest.cs | 20 +++++++++++++++++++ .../OuterApiRequests/GetProviderRequest.cs | 15 ++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingTheGetProviderByIdRequest.cs create mode 100644 src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProviderRequest.cs diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingTheGetProviderByIdRequest.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingTheGetProviderByIdRequest.cs new file mode 100644 index 0000000000..22c638c38a --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingTheGetProviderByIdRequest.cs @@ -0,0 +1,20 @@ +using AutoFixture.NUnit3; +using FluentAssertions; +using NUnit.Framework; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; + +namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiRequests +{ + public class WhenBuildingTheGetProviderByIdRequest + { + [Test, AutoData] + public void Then_The_Url_Is_Correctly_Constructed(int id) + { + //Arrange + var actual = new GetProviderRequest(id); + + //Act + actual.GetUrl.Should().Be($"providers/{id}"); + } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProviderRequest.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProviderRequest.cs new file mode 100644 index 0000000000..953d540368 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProviderRequest.cs @@ -0,0 +1,15 @@ +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; + +namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests +{ + public class GetProviderRequest : IGetApiRequest + { + private readonly int _id; + + public GetProviderRequest (int id) + { + _id = id; + } + public string GetUrl => $"providers/{_id}"; + } +} \ No newline at end of file From 1e939d17ae4eaa2b00d16204fc12030c6a552778 Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Thu, 24 Dec 2020 10:45:24 +0000 Subject: [PATCH 08/13] FAT2-295 - Update provider service remote to call outer api --- .../WhenIGetAProvider.cs | 61 ++++++++++--------- .../OuterApiRequests/GetProviderRequest.cs | 4 +- .../OuterApiResponses/GetProvidersResponse.cs | 3 - .../Services/ProviderServiceRemote.cs | 19 +++--- 4 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs index 5ccdbda97c..eedd522716 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs @@ -1,8 +1,13 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using AutoFixture; +using FluentAssertions; using Moq; using NUnit.Framework; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses; +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; using SFA.DAS.EmployerFinance.Services; using SFA.DAS.NLog.Logger; using SFA.DAS.Providers.Api.Client; @@ -13,29 +18,32 @@ internal class WhenIGetAProvider { private EmployerFinance.Services.ProviderServiceRemote _sut; private Mock _mockProviderService; - private Mock _mockProviderApiClient; + private Mock _mockApiClient; private Mock _mockLog; private string _providerName; + private ProviderResponseItem _provider; [SetUp] public void Arrange() { + var fixture = new Fixture(); + _provider = fixture.Create(); _mockProviderService = new Mock(); - _mockProviderApiClient = new Mock(); + _mockApiClient = new Mock(); _mockLog = new Mock(); _providerName = Guid.NewGuid().ToString(); - _mockProviderApiClient - .Setup(m => m.GetAsync(It.IsAny())) - .ReturnsAsync(new Apprenticeships.Api.Types.Providers.Provider { ProviderName = _providerName }); + _mockApiClient + .Setup(m => m.Get(It.IsAny())) + .ReturnsAsync(_provider); _mockLog .Setup(m => m.Warn(It.IsAny(), It.IsAny())) .Verifiable(); - _sut = new EmployerFinance.Services.ProviderServiceRemote(_mockProviderService.Object, _mockProviderApiClient.Object, _mockLog.Object); + _sut = new EmployerFinance.Services.ProviderServiceRemote(_mockProviderService.Object, _mockApiClient.Object, _mockLog.Object); } [Test] @@ -45,25 +53,18 @@ public async Task ThenTheProviderIsRetrievedFromTheRemoteRepository() long ukPrn = 1234567890; // act - var result = await _sut.Get(ukPrn); + var actual = await _sut.Get(ukPrn); // assert - _mockProviderApiClient.Verify(m => m.GetAsync(ukPrn), Times.Once); + _mockApiClient.Verify(m => m.Get(It.Is(c=>c.GetUrl.Equals($"providers/{ukPrn}"))), Times.Once); _mockProviderService.Verify(m => m.Get(ukPrn), Times.Never); + actual.ShouldBeEquivalentTo(_provider, options=>options + .Excluding(c=>c.Id) + .Excluding(c=>c.NationalProvider) + .Excluding(c=>c.IsHistoricProviderName) + ); } - [Test] - public async Task AndTheProviderExistsInTheRemoteRepository_ThenAValidProviderIsReturned() - { - // arrange - long ukPrn = 1234567890; - - // act - var result = await _sut.Get(ukPrn); - - // assert - Assert.AreEqual(_providerName, result.Name); - } [Test] public async Task AndTheProviderIsNotInTheRemoteRepository_ThenTheProviderServiceIsCalled() @@ -71,12 +72,12 @@ public async Task AndTheProviderIsNotInTheRemoteRepository_ThenTheProviderServic // arrange long ukPrn = 1234567890; - _mockProviderApiClient - .Setup(m => m.GetAsync(It.IsAny())) - .Returns(Task.FromResult< Apprenticeships.Api.Types.Providers.Provider>(null)); + _mockApiClient + .Setup(m => m.Get(It.IsAny())) + .ReturnsAsync((ProviderResponseItem)null); // act - var result = await _sut.Get(ukPrn); + await _sut.Get(ukPrn); // assert _mockProviderService.Verify(m => m.Get(ukPrn), Times.Once); @@ -89,12 +90,12 @@ public async Task AndTheRemoteRepositoryThrowsAnError_ThenTheErrorIsLoggedAsAWar long ukPrn = 1234567890; var exception = new Exception(); - _mockProviderApiClient - .Setup(m => m.GetAsync(ukPrn)) + _mockApiClient + .Setup(m => m.Get(It.IsAny())) .ThrowsAsync(exception); // act - var result = await _sut.Get(ukPrn); + await _sut.Get(ukPrn); // assert _mockLog.Verify(m => m.Warn(exception, $"Unable to get provider details with UKPRN {ukPrn} from apprenticeship API."), Times.Once); @@ -107,12 +108,12 @@ public async Task AndTheRemoteRepositoryThrowsAnError_ThenTheProviderServiceIsCa long ukPrn = 1234567890; var exception = new Exception(); - _mockProviderApiClient - .Setup(m => m.GetAsync(ukPrn)) + _mockApiClient + .Setup(m => m.Get(It.IsAny())) .ThrowsAsync(exception); // act - var result = await _sut.Get(ukPrn); + await _sut.Get(ukPrn); // assert _mockProviderService.Verify(m => m.Get(ukPrn), Times.Once); diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProviderRequest.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProviderRequest.cs index 953d540368..0aebd6d577 100644 --- a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProviderRequest.cs +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetProviderRequest.cs @@ -4,9 +4,9 @@ namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests { public class GetProviderRequest : IGetApiRequest { - private readonly int _id; + private readonly long _id; - public GetProviderRequest (int id) + public GetProviderRequest (long id) { _id = id; } diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs index 6fe82521ec..4583519015 100644 --- a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetProvidersResponse.cs @@ -17,9 +17,6 @@ public class ProviderResponseItem [JsonProperty("name")] public string Name { get; set; } - [JsonProperty("contactUrl")] - public string ContactUrl { get; set; } - [JsonProperty("email")] public string Email { get; set; } diff --git a/src/SFA.DAS.EmployerFinance/Services/ProviderServiceRemote.cs b/src/SFA.DAS.EmployerFinance/Services/ProviderServiceRemote.cs index cce83c4159..038658a088 100644 --- a/src/SFA.DAS.EmployerFinance/Services/ProviderServiceRemote.cs +++ b/src/SFA.DAS.EmployerFinance/Services/ProviderServiceRemote.cs @@ -1,20 +1,22 @@ using System; using System.Threading.Tasks; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses; +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; using SFA.DAS.NLog.Logger; -using SFA.DAS.Providers.Api.Client; namespace SFA.DAS.EmployerFinance.Services { public class ProviderServiceRemote : IProviderService { private readonly IProviderService _providerService; - private readonly IProviderApiClient _providerApiClient; + private readonly IApiClient _apiClient; private readonly ILog _logger; - public ProviderServiceRemote(IProviderService providerService, IProviderApiClient providerApiClient, ILog logger) + public ProviderServiceRemote(IProviderService providerService, IApiClient apiClient, ILog logger) { _providerService = providerService; - _providerApiClient = providerApiClient; + _apiClient = apiClient; _logger = logger; } @@ -22,7 +24,7 @@ public ProviderServiceRemote(IProviderService providerService, IProviderApiClien { try { - var provider = await _providerApiClient.GetAsync(ukPrn); + var provider = await _apiClient.Get(new GetProviderRequest(ukPrn)); if(provider != null) { return MapFrom(provider); @@ -36,15 +38,14 @@ public ProviderServiceRemote(IProviderService providerService, IProviderApiClien return await _providerService.Get(ukPrn); } - private static Models.ApprenticeshipProvider.Provider MapFrom(Apprenticeships.Api.Types.Providers.Provider provider) + private static Models.ApprenticeshipProvider.Provider MapFrom(ProviderResponseItem provider) { return new Models.ApprenticeshipProvider.Provider() { Ukprn = provider.Ukprn, - Name = provider.ProviderName, + Name = provider.Name, Email = provider.Email, - Phone = provider.Phone, - NationalProvider = provider.NationalProvider + Phone = provider.Phone }; } } From 32402a0564d5f793ea8e84c483cc20a5fb45f3be Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Thu, 24 Dec 2020 12:28:36 +0000 Subject: [PATCH 09/13] FAT2-295 - Update DI registration for MA outer api --- .../DependencyResolution/IoC.cs | 1 + .../DependencyResolution/IoC.cs | 5 +++-- .../WhenIResolveProviderService.cs | 3 ++- .../ApprenticeshipInfoServiceConfiguration.cs | 7 ------- .../EmployerFinanceConfiguration.cs | 2 +- .../ManageApprenticeshipsOuterApiRegistry.cs | 17 +++++++++++++++++ .../DependencyResolution/ProvidersRegistry.cs | 6 +++--- 7 files changed, 27 insertions(+), 14 deletions(-) delete mode 100644 src/SFA.DAS.EmployerFinance/Configuration/ApprenticeshipInfoServiceConfiguration.cs create mode 100644 src/SFA.DAS.EmployerFinance/DependencyResolution/ManageApprenticeshipsOuterApiRegistry.cs diff --git a/src/SFA.DAS.EmployerFinance.Jobs/DependencyResolution/IoC.cs b/src/SFA.DAS.EmployerFinance.Jobs/DependencyResolution/IoC.cs index c5d89dadac..ad84f1a486 100644 --- a/src/SFA.DAS.EmployerFinance.Jobs/DependencyResolution/IoC.cs +++ b/src/SFA.DAS.EmployerFinance.Jobs/DependencyResolution/IoC.cs @@ -10,6 +10,7 @@ public static IContainer Initialize() return new Container(c => { c.AddRegistry(); + c.AddRegistry(); c.AddRegistry(); c.AddRegistry(); c.AddRegistry(); diff --git a/src/SFA.DAS.EmployerFinance.MessageHandlers/DependencyResolution/IoC.cs b/src/SFA.DAS.EmployerFinance.MessageHandlers/DependencyResolution/IoC.cs index 6cef6b34f5..75f2f2fe2f 100644 --- a/src/SFA.DAS.EmployerFinance.MessageHandlers/DependencyResolution/IoC.cs +++ b/src/SFA.DAS.EmployerFinance.MessageHandlers/DependencyResolution/IoC.cs @@ -1,7 +1,7 @@ using SFA.DAS.EmployerFinance.Data; using SFA.DAS.EmployerFinance.DependencyResolution; -using SFA.DAS.UnitOfWork.EntityFramework.StructureMap; -using SFA.DAS.UnitOfWork.NServiceBus.DependencyResolution.StructureMap; +using SFA.DAS.UnitOfWork.EntityFramework.StructureMap; +using SFA.DAS.UnitOfWork.NServiceBus.DependencyResolution.StructureMap; using StructureMap; namespace SFA.DAS.EmployerFinance.MessageHandlers.DependencyResolution @@ -29,6 +29,7 @@ public static IContainer Initialize() c.AddRegistry(); c.AddRegistry(); c.AddRegistry(); + c.AddRegistry(); c.AddRegistry(); c.AddRegistry(); c.AddRegistry(); diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/DependencyResolution/WhenIResolveProviderService.cs b/src/SFA.DAS.EmployerFinance.UnitTests/DependencyResolution/WhenIResolveProviderService.cs index 42496aaabc..44f88caee0 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/DependencyResolution/WhenIResolveProviderService.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/DependencyResolution/WhenIResolveProviderService.cs @@ -25,10 +25,11 @@ public void Arrange() _mockDasLevyRepository = new Mock(); _mockLog = new Mock(); _mockInProcessCache = new Mock(); - var config = new EmployerFinanceConfiguration { ApprenticeshipInfoService = new ApprenticeshipInfoServiceConfiguration { BaseUrl = "http://BaseUrl.education.gov.uk" } }; + var config = new EmployerFinanceConfiguration{ManageApprenticeshipsOuterApiConfiguration = new ManageApprenticeshipsOuterApiConfiguration { BaseUrl = "http://BaseUrl.education.gov.uk" , Key="123" }}; _container = new Container(c => { + c.AddRegistry(); c.AddRegistry(); c.For().Use(_mockDasLevyRepository.Object); c.For().Use(config); diff --git a/src/SFA.DAS.EmployerFinance/Configuration/ApprenticeshipInfoServiceConfiguration.cs b/src/SFA.DAS.EmployerFinance/Configuration/ApprenticeshipInfoServiceConfiguration.cs deleted file mode 100644 index 227c4c4d54..0000000000 --- a/src/SFA.DAS.EmployerFinance/Configuration/ApprenticeshipInfoServiceConfiguration.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SFA.DAS.EmployerFinance.Configuration -{ - public class ApprenticeshipInfoServiceConfiguration - { - public string BaseUrl { get; set; } - } -} diff --git a/src/SFA.DAS.EmployerFinance/Configuration/EmployerFinanceConfiguration.cs b/src/SFA.DAS.EmployerFinance/Configuration/EmployerFinanceConfiguration.cs index d04655c226..075991b1ea 100644 --- a/src/SFA.DAS.EmployerFinance/Configuration/EmployerFinanceConfiguration.cs +++ b/src/SFA.DAS.EmployerFinance/Configuration/EmployerFinanceConfiguration.cs @@ -9,7 +9,7 @@ public class EmployerFinanceConfiguration : ITopicMessagePublisherConfiguration { public string ApplicationId { get; set; } public string AllowedHashstringCharacters { get; set; } - public ApprenticeshipInfoServiceConfiguration ApprenticeshipInfoService { get; set; } + public ManageApprenticeshipsOuterApiConfiguration ManageApprenticeshipsOuterApiConfiguration { get; set; } public ContentClientApiConfiguration ContentApi { get; set; } public string DashboardUrl { get; set; } public string DatabaseConnectionString { get; set; } diff --git a/src/SFA.DAS.EmployerFinance/DependencyResolution/ManageApprenticeshipsOuterApiRegistry.cs b/src/SFA.DAS.EmployerFinance/DependencyResolution/ManageApprenticeshipsOuterApiRegistry.cs new file mode 100644 index 0000000000..fb8bdcbad8 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/DependencyResolution/ManageApprenticeshipsOuterApiRegistry.cs @@ -0,0 +1,17 @@ +using System.Net.Http; +using SFA.DAS.EmployerFinance.Configuration; +using SFA.DAS.EmployerFinance.Infrastructure; +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; +using StructureMap; + +namespace SFA.DAS.EmployerFinance.DependencyResolution +{ + public class ManageApprenticeshipsOuterApiRegistry : Registry + { + public ManageApprenticeshipsOuterApiRegistry () + { + For().Use(c => c.GetInstance().ManageApprenticeshipsOuterApiConfiguration).Singleton(); + For().Use().Ctor().Is(new HttpClient()).Singleton(); + } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/DependencyResolution/ProvidersRegistry.cs b/src/SFA.DAS.EmployerFinance/DependencyResolution/ProvidersRegistry.cs index ab0c595be3..e4f1b1d949 100644 --- a/src/SFA.DAS.EmployerFinance/DependencyResolution/ProvidersRegistry.cs +++ b/src/SFA.DAS.EmployerFinance/DependencyResolution/ProvidersRegistry.cs @@ -1,6 +1,7 @@ -using SFA.DAS.EmployerFinance.Configuration; +using System.Net.Http; +using SFA.DAS.EmployerFinance.Infrastructure; +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; using SFA.DAS.EmployerFinance.Services; -using SFA.DAS.Providers.Api.Client; using StructureMap; namespace SFA.DAS.EmployerFinance.DependencyResolution @@ -9,7 +10,6 @@ public class ProvidersRegistry : Registry { public ProvidersRegistry() { - For().Use(c => new ProviderApiClient(c.GetInstance().ApprenticeshipInfoService.BaseUrl)); For().Use(); For().DecorateAllWith(); For().DecorateAllWith(); From 2d9288d4f9105bcc79883398bdf05b650e2b87d3 Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Thu, 24 Dec 2020 13:03:54 +0000 Subject: [PATCH 10/13] FAT2-295 - Remove FATv1 packages --- .../SFA.DAS.EAS.Application.csproj | 6 ------ .../SFA.DAS.EAS.Infrastructure.csproj | 3 --- ...SFA.DAS.EmployerFinance.MessageHandlers.UnitTests.csproj | 2 +- .../ProviderServiceRemote/WhenIGetAProvider.cs | 2 -- src/SFA.DAS.EmployerFinance/SFA.DAS.EmployerFinance.csproj | 2 -- 5 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/SFA.DAS.EmployerApprenticeshipsService.Application/SFA.DAS.EAS.Application.csproj b/src/SFA.DAS.EmployerApprenticeshipsService.Application/SFA.DAS.EAS.Application.csproj index a71613b58c..0d7d31233e 100644 --- a/src/SFA.DAS.EmployerApprenticeshipsService.Application/SFA.DAS.EAS.Application.csproj +++ b/src/SFA.DAS.EmployerApprenticeshipsService.Application/SFA.DAS.EAS.Application.csproj @@ -203,9 +203,6 @@ 1.2.71 - - 0.11.98 - 1.0.0.20553 @@ -269,9 +266,6 @@ 2.1.97 - - 0.11.39 - 1.1.82 diff --git a/src/SFA.DAS.EmployerApprenticeshipsService.Infrastructure/SFA.DAS.EAS.Infrastructure.csproj b/src/SFA.DAS.EmployerApprenticeshipsService.Infrastructure/SFA.DAS.EAS.Infrastructure.csproj index f1305536fc..db44e3faf5 100644 --- a/src/SFA.DAS.EmployerApprenticeshipsService.Infrastructure/SFA.DAS.EAS.Infrastructure.csproj +++ b/src/SFA.DAS.EmployerApprenticeshipsService.Infrastructure/SFA.DAS.EAS.Infrastructure.csproj @@ -119,9 +119,6 @@ 1.0.3 - - 0.11.98 - 1.0.0.20553 diff --git a/src/SFA.DAS.EmployerFinance.MessageHandlers.UnitTests/SFA.DAS.EmployerFinance.MessageHandlers.UnitTests.csproj b/src/SFA.DAS.EmployerFinance.MessageHandlers.UnitTests/SFA.DAS.EmployerFinance.MessageHandlers.UnitTests.csproj index d3cff9ef84..f618cf84a0 100644 --- a/src/SFA.DAS.EmployerFinance.MessageHandlers.UnitTests/SFA.DAS.EmployerFinance.MessageHandlers.UnitTests.csproj +++ b/src/SFA.DAS.EmployerFinance.MessageHandlers.UnitTests/SFA.DAS.EmployerFinance.MessageHandlers.UnitTests.csproj @@ -15,7 +15,7 @@ - + diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs index eedd522716..c093d86248 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; using AutoFixture; using FluentAssertions; @@ -10,7 +9,6 @@ using SFA.DAS.EmployerFinance.Interfaces.OuterApi; using SFA.DAS.EmployerFinance.Services; using SFA.DAS.NLog.Logger; -using SFA.DAS.Providers.Api.Client; namespace SFA.DAS.EmployerFinance.UnitTests.Services.ProviderServiceTests.ProviderServiceRemote { diff --git a/src/SFA.DAS.EmployerFinance/SFA.DAS.EmployerFinance.csproj b/src/SFA.DAS.EmployerFinance/SFA.DAS.EmployerFinance.csproj index 48efcbc145..330b71126e 100644 --- a/src/SFA.DAS.EmployerFinance/SFA.DAS.EmployerFinance.csproj +++ b/src/SFA.DAS.EmployerFinance/SFA.DAS.EmployerFinance.csproj @@ -16,7 +16,6 @@ - @@ -47,7 +46,6 @@ - From ebd94a6a77ab17c922dfdc6ed91bd98c9ceda73b Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Tue, 29 Dec 2020 10:03:15 +0000 Subject: [PATCH 11/13] FAT2-295 - Add missing dependency --- .../SFA.DAS.EmployerFinance.UnitTests.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj b/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj index b24e96996c..6124191a54 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj +++ b/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj @@ -12,6 +12,7 @@ + From 75c8e35ff1b0b22fd418572cdfe89c674bc883c5 Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Wed, 30 Dec 2020 10:27:19 +0000 Subject: [PATCH 12/13] Try to resolve build issue on VSTS --- .../WhenBuildingGetStandardsRequest.cs | 1 - .../WhenBuildingTheGetProviderByIdRequest.cs | 6 +- .../Infrastructure/WhenHandlingAGetRequest.cs | 19 ++-- .../SFA.DAS.EmployerFinance.UnitTests.csproj | 2 - .../WhenGettingApprenticeshipInfo.cs | 94 ++++++++++--------- 5 files changed, 62 insertions(+), 60 deletions(-) diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetStandardsRequest.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetStandardsRequest.cs index 094bb7d3b6..73d47039ca 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetStandardsRequest.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetStandardsRequest.cs @@ -1,4 +1,3 @@ -using AutoFixture.NUnit3; using FluentAssertions; using NUnit.Framework; using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingTheGetProviderByIdRequest.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingTheGetProviderByIdRequest.cs index 22c638c38a..f1a190b7bf 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingTheGetProviderByIdRequest.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingTheGetProviderByIdRequest.cs @@ -1,4 +1,3 @@ -using AutoFixture.NUnit3; using FluentAssertions; using NUnit.Framework; using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; @@ -7,10 +6,11 @@ namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiRequests { public class WhenBuildingTheGetProviderByIdRequest { - [Test, AutoData] - public void Then_The_Url_Is_Correctly_Constructed(int id) + [Test] + public void Then_The_Url_Is_Correctly_Constructed() { //Arrange + var id = 123; var actual = new GetProviderRequest(id); //Act diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs index ff9033fb92..8e3865961d 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/WhenHandlingAGetRequest.cs @@ -4,7 +4,6 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using AutoFixture.NUnit3; using FluentAssertions; using Moq; using Moq.Protected; @@ -18,13 +17,13 @@ namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure { public class WhenHandlingAGetRequest { - [Test, AutoData] - public async Task Then_The_Endpoint_Is_Called_With_Authentication_Header_And_Data_Returned( - List testObject, - string key, - GetTestRequest getTestRequest) + [Test] + public async Task Then_The_Endpoint_Is_Called_With_Authentication_Header_And_Data_Returned() { //Arrange + var key = "123-abc-567"; + var getTestRequest = new GetTestRequest(); + var testObject = new List(); var config = new ManageApprenticeshipsOuterApiConfiguration {BaseUrl = "http://valid-url/", Key = key}; var response = new HttpResponseMessage @@ -43,12 +42,12 @@ public async Task Then_The_Endpoint_Is_Called_With_Authentication_Header_And_Dat actual.Should().BeEquivalentTo(testObject); } - [Test, AutoData] - public void Then_If_It_Is_Not_Successful_An_Exception_Is_Thrown( - string key, - GetTestRequest getTestRequest) + [Test] + public void Then_If_It_Is_Not_Successful_An_Exception_Is_Thrown() { //Arrange + var key = "123-abc-567"; + var getTestRequest = new GetTestRequest(); var config = new ManageApprenticeshipsOuterApiConfiguration {BaseUrl = "http://valid-url/", Key = key }; var response = new HttpResponseMessage { diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj b/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj index 6124191a54..30da581191 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj +++ b/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj @@ -13,7 +13,6 @@ - @@ -24,7 +23,6 @@ - diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs index 3e968005eb..2fde8bd2a4 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs @@ -1,6 +1,6 @@ using System.Linq; using System.Threading.Tasks; -using AutoFixture.NUnit3; +using AutoFixture; using FluentAssertions; using Moq; using NUnit.Framework; @@ -10,35 +10,46 @@ using SFA.DAS.EmployerFinance.Interfaces.OuterApi; using SFA.DAS.EmployerFinance.Models.ApprenticeshipCourse; using SFA.DAS.EmployerFinance.Services; -using SFA.DAS.Testing.AutoFixture; + namespace SFA.DAS.EmployerFinance.UnitTests.Services.ApprenticeshipInfoServiceWrapperTests { public class WhenGettingApprenticeshipInfo { - [Test, MoqAutoData] - public async Task Then_The_Standards_Are_Retrieved_From_The_Api_And_Added_To_Cached( - GetStandardsResponse apiResponse, - [Frozen]Mock apiClient, - [Frozen]Mock cache, - ApprenticeshipInfoServiceWrapper service) + private Mock _apiClient; + private Mock _cache; + private ApprenticeshipInfoServiceWrapper _service; + + [SetUp] + public void Arrange() + { + _apiClient = new Mock(); + _cache = new Mock(); + + _service = new ApprenticeshipInfoServiceWrapper(_cache.Object, _apiClient.Object); + } + + [Test] + public async Task Then_The_Standards_Are_Retrieved_From_The_Api_And_Added_To_Cached() { //Arrange StandardsView returnData = null; - apiClient.Setup(x => x.Get(It.IsAny())) + var fixture = new Fixture(); + var apiResponse = fixture.Create(); + _apiClient.Setup(x => x.Get(It.IsAny())) .ReturnsAsync(apiResponse); - cache.Setup(x => x.Exists("Standards")).Returns(false); - cache.Setup(x => x.Set("Standards", It.IsAny())).Callback( + _cache.Setup(x => x.Exists("Standards")).Returns(false); + _cache.Setup(x => x.Set("Standards", It.IsAny())).Callback( (key, value) => { returnData = value as StandardsView; }); //Act - await service.GetStandardsAsync(); + await _service.GetStandardsAsync(); //Assert - apiClient.Verify(x=>x.Get(It.IsAny()),Times.Once); + _apiClient.Verify(x=>x.Get(It.IsAny()),Times.Once); returnData.Standards.Count.Should().Be(apiResponse.Standards.Count); returnData.Standards.ToList().First().ShouldBeEquivalentTo(apiResponse.Standards.First(), options=> options .Excluding(c=>c.Title) @@ -47,70 +58,65 @@ public async Task Then_The_Standards_Are_Retrieved_From_The_Api_And_Added_To_Cac ); } - [Test, MoqAutoData] - public async Task Then_Standards_Retrieved_From_Cache_If_Cached( - StandardsView cacheData, - [Frozen]Mock apiClient, - [Frozen]Mock cache, - ApprenticeshipInfoServiceWrapper service) + [Test] + public async Task Then_Standards_Retrieved_From_Cache_If_Cached() { //Arrange - cache.Setup(x => x.Exists("Standards")).Returns(true); - cache.Setup(x => x.Get("Standards")).Returns(cacheData); + var fixture = new Fixture(); + var cacheData = fixture.Create(); + _cache.Setup(x => x.Exists("Standards")).Returns(true); + _cache.Setup(x => x.Get("Standards")).Returns(cacheData); //Act - var actual = await service.GetStandardsAsync(); + var actual = await _service.GetStandardsAsync(); //Assert - apiClient.Verify(x=>x.Get(It.IsAny()),Times.Never); + _apiClient.Verify(x=>x.Get(It.IsAny()),Times.Never); actual.ShouldBeEquivalentTo(cacheData); } - [Test, MoqAutoData] - public async Task Then_The_Frameworks_Are_Retrieved_From_The_Api_And_Added_To_Cached( - GetFrameworksResponse apiResponse, - [Frozen]Mock apiClient, - [Frozen]Mock cache, - ApprenticeshipInfoServiceWrapper service) + [Test] + public async Task Then_The_Frameworks_Are_Retrieved_From_The_Api_And_Added_To_Cached() { //Arrange + var fixture = new Fixture(); + var apiResponse = fixture.Create(); + FrameworksView returnData = null; - apiClient.Setup(x => x.Get(It.IsAny())) + _apiClient.Setup(x => x.Get(It.IsAny())) .ReturnsAsync(apiResponse); - cache.Setup(x => x.Exists("Frameworks")).Returns(false); - cache.Setup(x => x.Set("Frameworks", It.IsAny())).Callback( + _cache.Setup(x => x.Exists("Frameworks")).Returns(false); + _cache.Setup(x => x.Set("Frameworks", It.IsAny())).Callback( (key, value) => { returnData = value as FrameworksView; }); //Act - await service.GetFrameworksAsync(); + await _service.GetFrameworksAsync(); //Assert - apiClient.Verify(x=>x.Get(It.IsAny()),Times.Once); + _apiClient.Verify(x=>x.Get(It.IsAny()),Times.Once); returnData.Frameworks.Count.Should().Be(apiResponse.Frameworks.Count); returnData.Frameworks.ToList().First().ShouldBeEquivalentTo(apiResponse.Frameworks.First(), options=> options .Excluding(c=>c.Title) .Excluding(c=>c.ProgrammeType) ); } - [Test, MoqAutoData] - public async Task Then_Frameworks_Retrieved_From_Cache_If_Cached( - FrameworksView cacheData, - [Frozen]Mock apiClient, - [Frozen]Mock cache, - ApprenticeshipInfoServiceWrapper service) + [Test] + public async Task Then_Frameworks_Retrieved_From_Cache_If_Cached() { //Arrange - cache.Setup(x => x.Exists("Frameworks")).Returns(true); - cache.Setup(x => x.Get("Frameworks")).Returns(cacheData); + var fixture = new Fixture(); + var cacheData = fixture.Create(); + _cache.Setup(x => x.Exists("Frameworks")).Returns(true); + _cache.Setup(x => x.Get("Frameworks")).Returns(cacheData); //Act - var actual = await service.GetFrameworksAsync(); + var actual = await _service.GetFrameworksAsync(); //Assert - apiClient.Verify(x=>x.Get(It.IsAny()),Times.Never); + _apiClient.Verify(x=>x.Get(It.IsAny()),Times.Never); actual.ShouldBeEquivalentTo(cacheData); } From 6a51fb36472c9e79e288861a41a0523fde785b7d Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Wed, 30 Dec 2020 11:45:20 +0000 Subject: [PATCH 13/13] FAT2-295 - Remove autofixture dependency --- .../SFA.DAS.EmployerFinance.UnitTests.csproj | 1 - .../WhenGettingApprenticeshipInfo.cs | 53 +++++++++++++++---- .../WhenIGetAProvider.cs | 8 +-- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj b/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj index 30da581191..0b2f1bf005 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj +++ b/src/SFA.DAS.EmployerFinance.UnitTests/SFA.DAS.EmployerFinance.UnitTests.csproj @@ -12,7 +12,6 @@ - diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs index 2fde8bd2a4..95656f8d79 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ApprenticeshipInfoServiceWrapperTests/WhenGettingApprenticeshipInfo.cs @@ -1,6 +1,6 @@ +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using AutoFixture; using FluentAssertions; using Moq; using NUnit.Framework; @@ -34,8 +34,16 @@ public async Task Then_The_Standards_Are_Retrieved_From_The_Api_And_Added_To_Cac { //Arrange StandardsView returnData = null; - var fixture = new Fixture(); - var apiResponse = fixture.Create(); + var apiResponse = new GetStandardsResponse + { + Standards = new List + { + new StandardResponseItem + { + Id = 1 + } + } + }; _apiClient.Setup(x => x.Get(It.IsAny())) .ReturnsAsync(apiResponse); _cache.Setup(x => x.Exists("Standards")).Returns(false); @@ -62,8 +70,16 @@ public async Task Then_The_Standards_Are_Retrieved_From_The_Api_And_Added_To_Cac public async Task Then_Standards_Retrieved_From_Cache_If_Cached() { //Arrange - var fixture = new Fixture(); - var cacheData = fixture.Create(); + var cacheData = new StandardsView + { + Standards = new List + { + new Standard + { + Code = 1 + } + } + }; _cache.Setup(x => x.Exists("Standards")).Returns(true); _cache.Setup(x => x.Get("Standards")).Returns(cacheData); @@ -79,8 +95,19 @@ public async Task Then_Standards_Retrieved_From_Cache_If_Cached() public async Task Then_The_Frameworks_Are_Retrieved_From_The_Api_And_Added_To_Cached() { //Arrange - var fixture = new Fixture(); - var apiResponse = fixture.Create(); + var apiResponse = new GetFrameworksResponse + { + Frameworks = new List + { + new FrameworkResponseItem + { + Id = "123", + FrameworkName = "test", + PathwayName = "test", + Title = "test" + } + } + }; FrameworksView returnData = null; _apiClient.Setup(x => x.Get(It.IsAny())) @@ -107,8 +134,16 @@ public async Task Then_The_Frameworks_Are_Retrieved_From_The_Api_And_Added_To_Ca public async Task Then_Frameworks_Retrieved_From_Cache_If_Cached() { //Arrange - var fixture = new Fixture(); - var cacheData = fixture.Create(); + var cacheData = new FrameworksView + { + Frameworks = new List + { + new Framework + { + Id = "123" + } + } + }; _cache.Setup(x => x.Exists("Frameworks")).Returns(true); _cache.Setup(x => x.Get("Frameworks")).Returns(cacheData); diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs index c093d86248..2f66bff2fb 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Services/ProviderServiceTests/ProviderServiceRemote/WhenIGetAProvider.cs @@ -1,6 +1,5 @@ using System; using System.Threading.Tasks; -using AutoFixture; using FluentAssertions; using Moq; using NUnit.Framework; @@ -25,8 +24,11 @@ internal class WhenIGetAProvider [SetUp] public void Arrange() { - var fixture = new Fixture(); - _provider = fixture.Create(); + + _provider = new ProviderResponseItem + { + Email = "test@test.com" + }; _mockProviderService = new Mock(); _mockApiClient = new Mock(); _mockLog = new Mock();