From 9fd9fb7f5837768c02c89b7d8fd1c0d47919a32d Mon Sep 17 00:00:00 2001 From: Farshad DASHTI <78855469+FrostyApeOne@users.noreply.github.com> Date: Thu, 10 Oct 2024 12:44:42 +0100 Subject: [PATCH] Updated to use new NuGet packages --- .../Caching/CacheSettings.cs | 8 - .../Caching/MemoryCacheService.cs | 52 ---- .../Dfe.Academies.Infrastructure.csproj | 2 +- ...frastructureServiceCollectionExtensions.cs | 6 +- .../GetMemberOfParliamentByConstituencies.cs | 6 +- .../GetMemberOfParliamentByConstituency.cs | 8 +- .../Dfe.Academies.Application.csproj | 3 +- .../EducationalPerformanceQueries.cs | 2 +- .../IEducationalPerformanceQueries.cs | 2 +- .../Establishment/EstablishmentDtoBuilder.cs | 8 +- .../Establishment/EstablishmentQueries.cs | 4 +- .../Establishment/IEstablishmentQueries.cs | 2 +- ...GetAllPersonsAssociatedWithAcademyByUrn.cs | 6 +- .../Trust/ITrustQueries.cs | 4 +- ...lPersonsAssociatedWithTrustByTrnOrUkprn.cs | 6 +- .../Trust/TrustQueries.cs | 6 +- .../Dfe.Academies.Domain.csproj | 2 +- .../Interfaces/Caching/ICacheService.cs | 8 - Dfe.Academies.Utils/Caching/CacheKeyHelper.cs | 43 ---- PersonsApi/PersonsApi.csproj | 5 +- PersonsApi/Startup.cs | 3 +- PersonsApi/appsettings.json | 12 +- .../EducationalPerformanceQueriesTests.cs | 2 +- .../EstablishmentQueriesTests.cs | 2 +- .../Queries/Trust/TrustQueriesTests.cs | 4 +- ...rliamentByConstituencyQueryHandlerTests.cs | 13 +- ...amentsByConstituenciesQueryHandlerTests.cs | 13 +- ...lPersonsAssociatedWithAcademyByUrnTests.cs | 13 +- ...onsAssociatedWithTrustByTrnOrUkprnTests.cs | 14 +- .../Aggregates/ConstituencyTests.cs | 4 +- .../Aggregates/MemberContactDetailsTests.cs | 2 +- .../ConstituenciesControllerTests.cs | 4 +- .../EstablishmentsControllerTests.cs | 6 +- .../Controllers/TrustsControllerTests.cs | 6 +- .../OpenApiTests/OpenApiDocumentTests.cs | 8 +- .../Attributes/CustomAutoDataAttribute.cs | 17 -- .../InlineCustomAutoDataAttribute.cs | 7 - .../Customizations/AutoMapperCustomization.cs | 31 --- ...pplicationDbContextFactoryCustomization.cs | 234 ++++++++++++++++- ...ustomWebApplicationFactoryCustomization.cs | 2 +- .../Customizations/DateOnlyCustomization.cs | 14 - .../Customizations/DbContextCustomization.cs | 23 -- .../NSubstituteCustomization.cs | 13 - .../OmitCircularReferenceCustomization.cs | 15 -- .../Dfe.Academies.Tests.Common.csproj | 1 + .../Helpers/DbContextHelper.cs | 240 ------------------ .../Helpers/FixtureFactoryHelper.cs | 20 -- .../CustomWebApplicationDbContextFactory.cs | 62 ----- .../Mocks/CustomWebApplicationFactory.cs | 41 --- .../Mocks/MockJwtBearerHandler.cs | 29 --- .../V4/EstablishmentsController.cs | 12 +- .../Controllers/V4/TrustsController.cs | 4 +- .../EducationalPerformanceResponse.cs | 6 +- .../ResponseModels/PagingResponseFactory.cs | 4 +- TramsDataApi/Startup.cs | 3 +- TramsDataApi/TramsDataApi.csproj | 3 +- 56 files changed, 336 insertions(+), 734 deletions(-) delete mode 100644 Dfe.Academies.Api.Infrastructure/Caching/CacheSettings.cs delete mode 100644 Dfe.Academies.Api.Infrastructure/Caching/MemoryCacheService.cs delete mode 100644 Dfe.Academies.Domain/Interfaces/Caching/ICacheService.cs delete mode 100644 Dfe.Academies.Utils/Caching/CacheKeyHelper.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Attributes/CustomAutoDataAttribute.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Attributes/InlineCustomAutoDataAttribute.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Customizations/AutoMapperCustomization.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Customizations/DateOnlyCustomization.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Customizations/DbContextCustomization.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Customizations/NSubstituteCustomization.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Customizations/OmitCircularReferenceCustomization.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Helpers/DbContextHelper.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Helpers/FixtureFactoryHelper.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Mocks/CustomWebApplicationDbContextFactory.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Mocks/CustomWebApplicationFactory.cs delete mode 100644 Tests/Dfe.Academies.Tests.Common/Mocks/MockJwtBearerHandler.cs diff --git a/Dfe.Academies.Api.Infrastructure/Caching/CacheSettings.cs b/Dfe.Academies.Api.Infrastructure/Caching/CacheSettings.cs deleted file mode 100644 index edb7602b1..000000000 --- a/Dfe.Academies.Api.Infrastructure/Caching/CacheSettings.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Dfe.Academies.Infrastructure.Caching -{ - public class CacheSettings - { - public int DefaultDurationInSeconds { get; set; } = 5; - public Dictionary Durations { get; set; } = new(); - } -} diff --git a/Dfe.Academies.Api.Infrastructure/Caching/MemoryCacheService.cs b/Dfe.Academies.Api.Infrastructure/Caching/MemoryCacheService.cs deleted file mode 100644 index a988af425..000000000 --- a/Dfe.Academies.Api.Infrastructure/Caching/MemoryCacheService.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Microsoft.Extensions.Caching.Memory; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System.Diagnostics.CodeAnalysis; -using Dfe.Academies.Domain.Interfaces.Caching; - -namespace Dfe.Academies.Infrastructure.Caching -{ - [ExcludeFromCodeCoverage] - public class MemoryCacheService( - IMemoryCache memoryCache, - ILogger logger, - IOptions cacheSettings) - : ICacheService - { - private readonly CacheSettings _cacheSettings = cacheSettings.Value; - - public async Task GetOrAddAsync(string cacheKey, Func> fetchFunction, string methodName) - { - if (memoryCache.TryGetValue(cacheKey, out T? cachedValue)) - { - logger.LogInformation("Cache hit for key: {CacheKey}", cacheKey); - return cachedValue!; - } - - logger.LogInformation("Cache miss for key: {CacheKey}. Fetching from source...", cacheKey); - var result = await fetchFunction(); - - if (Equals(result, default(T))) return result; - var cacheDuration = GetCacheDurationForMethod(methodName); - memoryCache.Set(cacheKey, result, cacheDuration); - logger.LogInformation("Cached result for key: {CacheKey} for duration: {CacheDuration}", cacheKey, cacheDuration); - - return result; - } - - public void Remove(string cacheKey) - { - memoryCache.Remove(cacheKey); - logger.LogInformation("Cache removed for key: {CacheKey}", cacheKey); - } - - private TimeSpan GetCacheDurationForMethod(string methodName) - { - if (_cacheSettings.Durations.TryGetValue(methodName, out int durationInSeconds)) - { - return TimeSpan.FromSeconds(durationInSeconds); - } - return TimeSpan.FromSeconds(_cacheSettings.DefaultDurationInSeconds); - } - } -} diff --git a/Dfe.Academies.Api.Infrastructure/Dfe.Academies.Infrastructure.csproj b/Dfe.Academies.Api.Infrastructure/Dfe.Academies.Infrastructure.csproj index 0238d0b2b..2dfff1496 100644 --- a/Dfe.Academies.Api.Infrastructure/Dfe.Academies.Infrastructure.csproj +++ b/Dfe.Academies.Api.Infrastructure/Dfe.Academies.Infrastructure.csproj @@ -8,7 +8,7 @@ - + diff --git a/Dfe.Academies.Api.Infrastructure/InfrastructureServiceCollectionExtensions.cs b/Dfe.Academies.Api.Infrastructure/InfrastructureServiceCollectionExtensions.cs index 1e10a1105..44ff4a9be 100644 --- a/Dfe.Academies.Api.Infrastructure/InfrastructureServiceCollectionExtensions.cs +++ b/Dfe.Academies.Api.Infrastructure/InfrastructureServiceCollectionExtensions.cs @@ -1,13 +1,11 @@ using Dfe.Academies.Application.Common.Interfaces; using Dfe.Academies.Domain.Constituencies; using Dfe.Academies.Infrastructure; -using Dfe.Academies.Infrastructure.Caching; using Dfe.Academies.Infrastructure.Repositories; using Dfe.Academies.Infrastructure.Security.Authorization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Dfe.Academies.Domain.Interfaces.Repositories; -using Dfe.Academies.Domain.Interfaces.Caching; using Dfe.Academies.Infrastructure.QueryServices; using Dfe.Academies.Domain.ValueObjects; @@ -48,10 +46,8 @@ public static IServiceCollection AddPersonsApiInfrastructureDependencyGroup( services.AddScoped(); services.AddScoped(); - //Cache service - services.Configure(config.GetSection("CacheSettings")); - services.AddSingleton(); + services.AddServiceCaching(config); //Db var connectionString = config.GetConnectionString("DefaultConnection"); diff --git a/Dfe.Academies.Application/Constituencies/Queries/GetMemberOfParliamentByConstituencies/GetMemberOfParliamentByConstituencies.cs b/Dfe.Academies.Application/Constituencies/Queries/GetMemberOfParliamentByConstituencies/GetMemberOfParliamentByConstituencies.cs index 4b3820c89..2646404d8 100644 --- a/Dfe.Academies.Application/Constituencies/Queries/GetMemberOfParliamentByConstituencies/GetMemberOfParliamentByConstituencies.cs +++ b/Dfe.Academies.Application/Constituencies/Queries/GetMemberOfParliamentByConstituencies/GetMemberOfParliamentByConstituencies.cs @@ -1,9 +1,9 @@ using AutoMapper; using AutoMapper.QueryableExtensions; using Dfe.Academies.Application.Common.Models; -using Dfe.Academies.Domain.Interfaces.Caching; using Dfe.Academies.Domain.Interfaces.Repositories; -using Dfe.Academies.Utils.Caching; +using DfE.CoreLibs.Caching.Helpers; +using DfE.CoreLibs.Caching.Interfaces; using MediatR; using Microsoft.EntityFrameworkCore; @@ -14,7 +14,7 @@ public record GetMembersOfParliamentByConstituenciesQuery(List Constitue public class GetMembersOfParliamentByConstituenciesQueryHandler( IConstituencyRepository constituencyRepository, IMapper mapper, - ICacheService cacheService) + ICacheService cacheService) : IRequestHandler> { public async Task> Handle(GetMembersOfParliamentByConstituenciesQuery request, CancellationToken cancellationToken) diff --git a/Dfe.Academies.Application/Constituencies/Queries/GetMemberOfParliamentByConstituency/GetMemberOfParliamentByConstituency.cs b/Dfe.Academies.Application/Constituencies/Queries/GetMemberOfParliamentByConstituency/GetMemberOfParliamentByConstituency.cs index a61ab6543..4eab63ef3 100644 --- a/Dfe.Academies.Application/Constituencies/Queries/GetMemberOfParliamentByConstituency/GetMemberOfParliamentByConstituency.cs +++ b/Dfe.Academies.Application/Constituencies/Queries/GetMemberOfParliamentByConstituency/GetMemberOfParliamentByConstituency.cs @@ -1,9 +1,9 @@ using AutoMapper; using Dfe.Academies.Application.Common.Models; -using MediatR; -using Dfe.Academies.Utils.Caching; -using Dfe.Academies.Domain.Interfaces.Caching; using Dfe.Academies.Domain.Interfaces.Repositories; +using DfE.CoreLibs.Caching.Helpers; +using DfE.CoreLibs.Caching.Interfaces; +using MediatR; namespace Dfe.Academies.Application.Constituencies.Queries.GetMemberOfParliamentByConstituency { @@ -12,7 +12,7 @@ public record GetMemberOfParliamentByConstituencyQuery(string ConstituencyName) public class GetMemberOfParliamentByConstituencyQueryHandler( IConstituencyRepository constituencyRepository, IMapper mapper, - ICacheService cacheService) + ICacheService cacheService) : IRequestHandler { public async Task Handle(GetMemberOfParliamentByConstituencyQuery request, CancellationToken cancellationToken) diff --git a/Dfe.Academies.Application/Dfe.Academies.Application.csproj b/Dfe.Academies.Application/Dfe.Academies.Application.csproj index e841ad797..f3d9d2981 100644 --- a/Dfe.Academies.Application/Dfe.Academies.Application.csproj +++ b/Dfe.Academies.Application/Dfe.Academies.Application.csproj @@ -8,7 +8,8 @@ - + + diff --git a/Dfe.Academies.Application/EducationalPerformance/EducationalPerformanceQueries.cs b/Dfe.Academies.Application/EducationalPerformance/EducationalPerformanceQueries.cs index 2c9c8c2a9..38cd4dd9f 100644 --- a/Dfe.Academies.Application/EducationalPerformance/EducationalPerformanceQueries.cs +++ b/Dfe.Academies.Application/EducationalPerformance/EducationalPerformanceQueries.cs @@ -1,6 +1,6 @@ using Dfe.Academies.Domain.Interfaces.Repositories; -using Dfe.Academies.Contracts.V1.EducationalPerformance; using Dfe.Academies.Domain.EducationalPerformance; +using DfE.CoreLibs.Contracts.Academies.V1.EducationalPerformance; namespace Dfe.Academies.Application.EducationalPerformance { diff --git a/Dfe.Academies.Application/EducationalPerformance/IEducationalPerformanceQueries.cs b/Dfe.Academies.Application/EducationalPerformance/IEducationalPerformanceQueries.cs index 3a48c30d6..0aaa00da7 100644 --- a/Dfe.Academies.Application/EducationalPerformance/IEducationalPerformanceQueries.cs +++ b/Dfe.Academies.Application/EducationalPerformance/IEducationalPerformanceQueries.cs @@ -1,4 +1,4 @@ -using Dfe.Academies.Contracts.V1.EducationalPerformance; +using DfE.CoreLibs.Contracts.Academies.V1.EducationalPerformance; namespace Dfe.Academies.Application.EducationalPerformance { diff --git a/Dfe.Academies.Application/Establishment/EstablishmentDtoBuilder.cs b/Dfe.Academies.Application/Establishment/EstablishmentDtoBuilder.cs index c850358da..d62ae3a04 100644 --- a/Dfe.Academies.Application/Establishment/EstablishmentDtoBuilder.cs +++ b/Dfe.Academies.Application/Establishment/EstablishmentDtoBuilder.cs @@ -1,9 +1,7 @@ -using Dfe.Academies.Contracts.V4; -using Dfe.Academies.Contracts.V4.Establishments; -using Dfe.Academies.Domain.Census; -using System; -using System.Globalization; +using Dfe.Academies.Domain.Census; using Dfe.Academies.Utils.Extensions; +using DfE.CoreLibs.Contracts.Academies.V4; +using DfE.CoreLibs.Contracts.Academies.V4.Establishments; namespace Dfe.Academies.Application.Establishment { diff --git a/Dfe.Academies.Application/Establishment/EstablishmentQueries.cs b/Dfe.Academies.Application/Establishment/EstablishmentQueries.cs index f91886d5b..d39b90af8 100644 --- a/Dfe.Academies.Application/Establishment/EstablishmentQueries.cs +++ b/Dfe.Academies.Application/Establishment/EstablishmentQueries.cs @@ -1,5 +1,5 @@ -using Dfe.Academies.Contracts.V4.Establishments; -using Dfe.Academies.Domain.Interfaces.Repositories; +using Dfe.Academies.Domain.Interfaces.Repositories; +using DfE.CoreLibs.Contracts.Academies.V4.Establishments; namespace Dfe.Academies.Application.Establishment { diff --git a/Dfe.Academies.Application/Establishment/IEstablishmentQueries.cs b/Dfe.Academies.Application/Establishment/IEstablishmentQueries.cs index 31c1f742c..360473644 100644 --- a/Dfe.Academies.Application/Establishment/IEstablishmentQueries.cs +++ b/Dfe.Academies.Application/Establishment/IEstablishmentQueries.cs @@ -1,4 +1,4 @@ -using Dfe.Academies.Contracts.V4.Establishments; +using DfE.CoreLibs.Contracts.Academies.V4.Establishments; namespace Dfe.Academies.Application.Establishment { diff --git a/Dfe.Academies.Application/Establishment/Queries/GetAllPersonsAssociatedWithAcademyByUrn/GetAllPersonsAssociatedWithAcademyByUrn.cs b/Dfe.Academies.Application/Establishment/Queries/GetAllPersonsAssociatedWithAcademyByUrn/GetAllPersonsAssociatedWithAcademyByUrn.cs index 581207f6c..8f99dc3d4 100644 --- a/Dfe.Academies.Application/Establishment/Queries/GetAllPersonsAssociatedWithAcademyByUrn/GetAllPersonsAssociatedWithAcademyByUrn.cs +++ b/Dfe.Academies.Application/Establishment/Queries/GetAllPersonsAssociatedWithAcademyByUrn/GetAllPersonsAssociatedWithAcademyByUrn.cs @@ -2,8 +2,8 @@ using AutoMapper.QueryableExtensions; using Dfe.Academies.Application.Common.Interfaces; using Dfe.Academies.Application.Common.Models; -using Dfe.Academies.Domain.Interfaces.Caching; -using Dfe.Academies.Utils.Caching; +using DfE.CoreLibs.Caching.Helpers; +using DfE.CoreLibs.Caching.Interfaces; using MediatR; using Microsoft.EntityFrameworkCore; @@ -14,7 +14,7 @@ public record GetAllPersonsAssociatedWithAcademyByUrnQuery(int Urn) : IRequest cacheService) : IRequestHandler?> { public async Task?> Handle(GetAllPersonsAssociatedWithAcademyByUrnQuery request, CancellationToken cancellationToken) diff --git a/Dfe.Academies.Application/Trust/ITrustQueries.cs b/Dfe.Academies.Application/Trust/ITrustQueries.cs index 17e1a408b..00aba8e08 100644 --- a/Dfe.Academies.Application/Trust/ITrustQueries.cs +++ b/Dfe.Academies.Application/Trust/ITrustQueries.cs @@ -1,5 +1,5 @@ -using Dfe.Academies.Contracts.V4.Trusts; -using Dfe.Academies.Domain.Trust; +using Dfe.Academies.Domain.Trust; +using DfE.CoreLibs.Contracts.Academies.V4.Trusts; namespace Dfe.Academies.Application.Trust { diff --git a/Dfe.Academies.Application/Trust/Queries/GetAllPersonsAssociatedWithTrustByTrnOrUkprn/GetAllPersonsAssociatedWithTrustByTrnOrUkprn.cs b/Dfe.Academies.Application/Trust/Queries/GetAllPersonsAssociatedWithTrustByTrnOrUkprn/GetAllPersonsAssociatedWithTrustByTrnOrUkprn.cs index 5b1a268e7..655117e07 100644 --- a/Dfe.Academies.Application/Trust/Queries/GetAllPersonsAssociatedWithTrustByTrnOrUkprn/GetAllPersonsAssociatedWithTrustByTrnOrUkprn.cs +++ b/Dfe.Academies.Application/Trust/Queries/GetAllPersonsAssociatedWithTrustByTrnOrUkprn/GetAllPersonsAssociatedWithTrustByTrnOrUkprn.cs @@ -2,10 +2,10 @@ using AutoMapper.QueryableExtensions; using Dfe.Academies.Application.Common.Interfaces; using Dfe.Academies.Application.Common.Models; -using Dfe.Academies.Domain.Interfaces.Caching; -using Dfe.Academies.Utils.Caching; using Dfe.Academies.Utils.Enums; using Dfe.Academies.Utils.Helpers; +using DfE.CoreLibs.Caching.Helpers; +using DfE.CoreLibs.Caching.Interfaces; using MediatR; using Microsoft.EntityFrameworkCore; @@ -16,7 +16,7 @@ public record GetAllPersonsAssociatedWithTrustByTrnOrUkprnQuery(string Id) : IRe public class GetAllPersonsAssociatedWithTrustByTrnOrUkprnQueryHandler( ITrustQueryService trustQueryService, IMapper mapper, - ICacheService cacheService) + ICacheService cacheService) : IRequestHandler?> { public async Task?> Handle(GetAllPersonsAssociatedWithTrustByTrnOrUkprnQuery request, CancellationToken cancellationToken) diff --git a/Dfe.Academies.Application/Trust/TrustQueries.cs b/Dfe.Academies.Application/Trust/TrustQueries.cs index 76de537be..ba7c4228f 100644 --- a/Dfe.Academies.Application/Trust/TrustQueries.cs +++ b/Dfe.Academies.Application/Trust/TrustQueries.cs @@ -1,7 +1,7 @@ using Dfe.Academies.Domain.Interfaces.Repositories; -using Dfe.Academies.Contracts.V4; -using Dfe.Academies.Contracts.V4.Trusts; using Dfe.Academies.Domain.Trust; +using DfE.CoreLibs.Contracts.Academies.V4; +using DfE.CoreLibs.Contracts.Academies.V4.Trusts; namespace Dfe.Academies.Application.Trust { @@ -51,7 +51,7 @@ private static TrustDto MapToTrustDto(Domain.Trust.Trust trust) CompaniesHouseNumber = trust.CompaniesHouseNumber, ReferenceNumber = trust.GroupID, Ukprn = trust.UKPRN, - Type = new Contracts.V4.Establishments.NameAndCodeDto() { Code = trust.TrustType?.Code, Name = trust.TrustType?.Name }, + Type = new DfE.CoreLibs.Contracts.Academies.V4.Establishments.NameAndCodeDto() { Code = trust.TrustType?.Code, Name = trust.TrustType?.Name }, Address = new AddressDto() { Street = trust.AddressLine1, diff --git a/Dfe.Academies.Domain/Dfe.Academies.Domain.csproj b/Dfe.Academies.Domain/Dfe.Academies.Domain.csproj index 7a5397523..0c348018e 100644 --- a/Dfe.Academies.Domain/Dfe.Academies.Domain.csproj +++ b/Dfe.Academies.Domain/Dfe.Academies.Domain.csproj @@ -7,7 +7,7 @@ - + diff --git a/Dfe.Academies.Domain/Interfaces/Caching/ICacheService.cs b/Dfe.Academies.Domain/Interfaces/Caching/ICacheService.cs deleted file mode 100644 index 45509b850..000000000 --- a/Dfe.Academies.Domain/Interfaces/Caching/ICacheService.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Dfe.Academies.Domain.Interfaces.Caching -{ - public interface ICacheService - { - Task GetOrAddAsync(string cacheKey, Func> fetchFunction, string methodName); - void Remove(string cacheKey); - } -} diff --git a/Dfe.Academies.Utils/Caching/CacheKeyHelper.cs b/Dfe.Academies.Utils/Caching/CacheKeyHelper.cs deleted file mode 100644 index cab1a4a94..000000000 --- a/Dfe.Academies.Utils/Caching/CacheKeyHelper.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Security.Cryptography; -using System.Text; - -namespace Dfe.Academies.Utils.Caching -{ - public static class CacheKeyHelper - { - /// - /// Generates a hashed cache key for any given input string. - /// - /// The input string to be hashed. - /// A hashed string that can be used as a cache key. - public static string GenerateHashedCacheKey(string input) - { - if (string.IsNullOrWhiteSpace(input)) - { - throw new ArgumentException("Input cannot be null or empty", nameof(input)); - } - - var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(input)); - - return BitConverter.ToString(bytes).Replace("-", "").ToLower(); - } - - /// - /// Generates a hashed cache key for a collection of strings by concatenating them. - /// - /// A collection of strings to be concatenated and hashed. - /// A hashed string that can be used as a cache key. - public static string GenerateHashedCacheKey(IEnumerable inputs) - { - if (inputs == null || !inputs.Any()) - { - throw new ArgumentException("Input collection cannot be null or empty", nameof(inputs)); - } - - var concatenatedInput = string.Join(",", inputs); - - return GenerateHashedCacheKey(concatenatedInput); - } - } - -} diff --git a/PersonsApi/PersonsApi.csproj b/PersonsApi/PersonsApi.csproj index 7b560ce98..7ae554bb6 100644 --- a/PersonsApi/PersonsApi.csproj +++ b/PersonsApi/PersonsApi.csproj @@ -10,9 +10,7 @@ - - - + @@ -37,6 +35,7 @@ + diff --git a/PersonsApi/Startup.cs b/PersonsApi/Startup.cs index 61d9681a9..2c222d88d 100644 --- a/PersonsApi/Startup.cs +++ b/PersonsApi/Startup.cs @@ -1,5 +1,6 @@ using Dfe.Academies.Application.MappingProfiles; -using Dfe.Academisation.CorrelationIdMiddleware; +using DfE.CoreLibs.Http.Interfaces; +using DfE.CoreLibs.Http.Middlewares.CorrelationId; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.FeatureManagement; diff --git a/PersonsApi/appsettings.json b/PersonsApi/appsettings.json index fd4db87f5..d83db5ffb 100644 --- a/PersonsApi/appsettings.json +++ b/PersonsApi/appsettings.json @@ -37,11 +37,13 @@ "FeatureManagement": { }, "CacheSettings": { - "DefaultDurationInSeconds": 60, - "Durations": { - "GetMembersOfParliamentByConstituenciesQueryHandler": 86400, - "GetMemberOfParliamentByConstituencyQueryHandler": 86400, - "GetAllPersonsAssociatedWithTrustByTrnOrUkprnQueryHandler": 86400 + "Memory": { + "DefaultDurationInSeconds": 60, + "Durations": { + "GetMembersOfParliamentByConstituenciesQueryHandler": 86400, + "GetMemberOfParliamentByConstituencyQueryHandler": 86400, + "GetAllPersonsAssociatedWithTrustByTrnOrUkprnQueryHandler": 86400 + } } }, "Authorization": { diff --git a/Tests/Dfe.Academies.Application.Tests/Queries/EducationalPerformance/EducationalPerformanceQueriesTests.cs b/Tests/Dfe.Academies.Application.Tests/Queries/EducationalPerformance/EducationalPerformanceQueriesTests.cs index ee7b0bfe4..e98295cb0 100644 --- a/Tests/Dfe.Academies.Application.Tests/Queries/EducationalPerformance/EducationalPerformanceQueriesTests.cs +++ b/Tests/Dfe.Academies.Application.Tests/Queries/EducationalPerformance/EducationalPerformanceQueriesTests.cs @@ -1,8 +1,8 @@ using AutoFixture; using Dfe.Academies.Application.EducationalPerformance; -using Dfe.Academies.Contracts.V1.EducationalPerformance; using Dfe.Academies.Domain.EducationalPerformance; using Dfe.Academies.Domain.Interfaces.Repositories; +using DfE.CoreLibs.Contracts.Academies.V1.EducationalPerformance; using FluentAssertions; using Moq; diff --git a/Tests/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs b/Tests/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs index 911b2cd58..92c46fdf3 100644 --- a/Tests/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs +++ b/Tests/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs @@ -1,7 +1,7 @@ using AutoFixture; using Dfe.Academies.Application.Establishment; -using Dfe.Academies.Contracts.V4.Establishments; using Dfe.Academies.Domain.Interfaces.Repositories; +using DfE.CoreLibs.Contracts.Academies.V4.Establishments; using FluentAssertions; using Moq; using System.Globalization; diff --git a/Tests/Dfe.Academies.Application.Tests/Queries/Trust/TrustQueriesTests.cs b/Tests/Dfe.Academies.Application.Tests/Queries/Trust/TrustQueriesTests.cs index 16f71047b..677fb4223 100644 --- a/Tests/Dfe.Academies.Application.Tests/Queries/Trust/TrustQueriesTests.cs +++ b/Tests/Dfe.Academies.Application.Tests/Queries/Trust/TrustQueriesTests.cs @@ -1,10 +1,10 @@ using AutoFixture; using Dfe.Academies.Application.Trust; -using Dfe.Academies.Contracts.V4.Trusts; +using Dfe.Academies.Domain.Interfaces.Repositories; using Dfe.Academies.Domain.Trust; +using DfE.CoreLibs.Contracts.Academies.V4.Trusts; using FluentAssertions; using Moq; -using Dfe.Academies.Domain.Interfaces.Repositories; namespace Dfe.Academies.Application.Tests.Queries.Trust { diff --git a/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Constituency/GetMemberOfParliamentByConstituencyQueryHandlerTests.cs b/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Constituency/GetMemberOfParliamentByConstituencyQueryHandlerTests.cs index b98fbcb0b..653f5d91b 100644 --- a/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Constituency/GetMemberOfParliamentByConstituencyQueryHandlerTests.cs +++ b/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Constituency/GetMemberOfParliamentByConstituencyQueryHandlerTests.cs @@ -2,13 +2,14 @@ using AutoFixture.Xunit2; using Dfe.Academies.Application.Common.Models; using Dfe.Academies.Application.Constituencies.Queries.GetMemberOfParliamentByConstituency; -using Dfe.Academies.Domain.Interfaces.Caching; +using Dfe.Academies.Application.MappingProfiles; using Dfe.Academies.Domain.Interfaces.Repositories; using Dfe.Academies.Testing.Common.Customizations.Models; -using Dfe.Academies.Tests.Common.Attributes; -using Dfe.Academies.Tests.Common.Customizations; using Dfe.Academies.Tests.Common.Customizations.Entities; -using Dfe.Academies.Utils.Caching; +using DfE.CoreLibs.Caching.Helpers; +using DfE.CoreLibs.Caching.Interfaces; +using DfE.CoreLibs.Testing.AutoFixture.Attributes; +using DfE.CoreLibs.Testing.AutoFixture.Customizations; using NSubstitute; namespace Dfe.Academies.Application.Tests.QueryHandlers.Constituency @@ -19,10 +20,10 @@ public class GetMemberOfParliamentByConstituencyQueryHandlerTests [CustomAutoData( typeof(MemberOfParliamentCustomization), typeof(ConstituencyCustomization), - typeof(AutoMapperCustomization))] + typeof(AutoMapperCustomization))] public async Task Handle_ShouldReturnMemberOfParliament_WhenConstituencyExists( [Frozen] IConstituencyRepository mockConstituencyRepository, - [Frozen] ICacheService mockCacheService, + [Frozen] ICacheService mockCacheService, GetMemberOfParliamentByConstituencyQueryHandler handler, GetMemberOfParliamentByConstituencyQuery query, Domain.Constituencies.Constituency constituency, diff --git a/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Constituency/GetMemberOfParliamentsByConstituenciesQueryHandlerTests.cs b/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Constituency/GetMemberOfParliamentsByConstituenciesQueryHandlerTests.cs index 974749c46..628b4aafd 100644 --- a/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Constituency/GetMemberOfParliamentsByConstituenciesQueryHandlerTests.cs +++ b/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Constituency/GetMemberOfParliamentsByConstituenciesQueryHandlerTests.cs @@ -2,13 +2,14 @@ using AutoFixture.Xunit2; using Dfe.Academies.Application.Common.Models; using Dfe.Academies.Application.Constituencies.Queries.GetMemberOfParliamentByConstituencies; -using Dfe.Academies.Domain.Interfaces.Caching; +using Dfe.Academies.Application.MappingProfiles; using Dfe.Academies.Domain.Interfaces.Repositories; using Dfe.Academies.Testing.Common.Customizations.Models; -using Dfe.Academies.Tests.Common.Attributes; -using Dfe.Academies.Tests.Common.Customizations; using Dfe.Academies.Tests.Common.Customizations.Entities; -using Dfe.Academies.Utils.Caching; +using DfE.CoreLibs.Caching.Helpers; +using DfE.CoreLibs.Caching.Interfaces; +using DfE.CoreLibs.Testing.AutoFixture.Attributes; +using DfE.CoreLibs.Testing.AutoFixture.Customizations; using MockQueryable; using NSubstitute; @@ -20,10 +21,10 @@ public class GetMemberOfParliamentByConstituenciesQueryHandlerTests [CustomAutoData( typeof(MemberOfParliamentCustomization), typeof(ConstituencyCustomization), - typeof(AutoMapperCustomization))] + typeof(AutoMapperCustomization))] public async Task Handle_ShouldReturnMemberOfParliament_WhenConstituencyExists( [Frozen] IConstituencyRepository mockConstituencyRepository, - [Frozen] ICacheService mockCacheService, + [Frozen] ICacheService mockCacheService, GetMembersOfParliamentByConstituenciesQueryHandler handler, GetMembersOfParliamentByConstituenciesQuery query, List constituencies, diff --git a/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Establishment/GetAllPersonsAssociatedWithAcademyByUrnTests.cs b/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Establishment/GetAllPersonsAssociatedWithAcademyByUrnTests.cs index 33407c2bd..1d32dac46 100644 --- a/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Establishment/GetAllPersonsAssociatedWithAcademyByUrnTests.cs +++ b/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Establishment/GetAllPersonsAssociatedWithAcademyByUrnTests.cs @@ -3,11 +3,12 @@ using Dfe.Academies.Application.Common.Interfaces; using Dfe.Academies.Application.Common.Models; using Dfe.Academies.Application.Establishment.Queries.GetAllPersonsAssociatedWithAcademyByUrn; -using Dfe.Academies.Domain.Interfaces.Caching; +using Dfe.Academies.Application.MappingProfiles; using Dfe.Academies.Testing.Common.Customizations.Models; -using Dfe.Academies.Tests.Common.Attributes; -using Dfe.Academies.Tests.Common.Customizations; -using Dfe.Academies.Utils.Caching; +using DfE.CoreLibs.Caching.Helpers; +using DfE.CoreLibs.Caching.Interfaces; +using DfE.CoreLibs.Testing.AutoFixture.Attributes; +using DfE.CoreLibs.Testing.AutoFixture.Customizations; using MockQueryable; using NSubstitute; @@ -20,10 +21,10 @@ public class GetAllPersonsAssociatedWithAcademyByUrnQueryHandlerTests typeof(OmitCircularReferenceCustomization), typeof(AcademyGovernanceCustomization), typeof(AcademyGovernanceQueryModelCustomization), - typeof(AutoMapperCustomization))] + typeof(AutoMapperCustomization))] public async Task Handle_ShouldReturnPersonsAssociatedWithAcademy_WhenUrnExists( [Frozen] IEstablishmentQueryService mockEstablishmentQueryService, - [Frozen] ICacheService mockCacheService, + [Frozen] ICacheService mockCacheService, GetAllPersonsAssociatedWithAcademyByUrnQueryHandler handler, GetAllPersonsAssociatedWithAcademyByUrnQuery query, List governanceQueryModels, diff --git a/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Trust/GetAllPersonsAssociatedWithTrustByTrnOrUkprnTests.cs b/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Trust/GetAllPersonsAssociatedWithTrustByTrnOrUkprnTests.cs index eaa42f340..22944dd55 100644 --- a/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Trust/GetAllPersonsAssociatedWithTrustByTrnOrUkprnTests.cs +++ b/Tests/Dfe.Academies.Application.Tests/QueryHandlers/Trust/GetAllPersonsAssociatedWithTrustByTrnOrUkprnTests.cs @@ -2,13 +2,13 @@ using AutoFixture.Xunit2; using Dfe.Academies.Application.Common.Interfaces; using Dfe.Academies.Application.Common.Models; -using Dfe.Academies.Application.Establishment.Queries.GetAllPersonsAssociatedWithAcademyByUrn; +using Dfe.Academies.Application.MappingProfiles; using Dfe.Academies.Application.Trust.Queries.GetAllPersonsAssociatedWithTrustByTrnOrUkprn; -using Dfe.Academies.Domain.Interfaces.Caching; using Dfe.Academies.Testing.Common.Customizations.Models; -using Dfe.Academies.Tests.Common.Attributes; -using Dfe.Academies.Tests.Common.Customizations; -using Dfe.Academies.Utils.Caching; +using DfE.CoreLibs.Caching.Helpers; +using DfE.CoreLibs.Caching.Interfaces; +using DfE.CoreLibs.Testing.AutoFixture.Attributes; +using DfE.CoreLibs.Testing.AutoFixture.Customizations; using MockQueryable; using NSubstitute; @@ -21,10 +21,10 @@ public class GetAllPersonsAssociatedWithTrustByTrnOrUkprnQueryHandlerTests typeof(OmitCircularReferenceCustomization), typeof(TrustGovernanceCustomization), typeof(TrustGovernanceQueryModelCustomization), - typeof(AutoMapperCustomization))] + typeof(AutoMapperCustomization))] public async Task Handle_ShouldReturnPersonsAssociatedWithTrust_WhenTrustExists( [Frozen] ITrustQueryService mockTrustQueryService, - [Frozen] ICacheService mockCacheService, + [Frozen] ICacheService mockCacheService, GetAllPersonsAssociatedWithTrustByTrnOrUkprnQueryHandler handler, List governanceQueryModels, IFixture fixture) diff --git a/Tests/Dfe.Academies.Domain.Tests/Aggregates/ConstituencyTests.cs b/Tests/Dfe.Academies.Domain.Tests/Aggregates/ConstituencyTests.cs index 6386f1e0f..2c7fc3eb9 100644 --- a/Tests/Dfe.Academies.Domain.Tests/Aggregates/ConstituencyTests.cs +++ b/Tests/Dfe.Academies.Domain.Tests/Aggregates/ConstituencyTests.cs @@ -1,8 +1,8 @@ using Dfe.Academies.Domain.Constituencies; using Dfe.Academies.Domain.ValueObjects; using Dfe.Academies.Testing.Common.Customizations.Models; -using Dfe.Academies.Tests.Common.Attributes; -using Dfe.Academies.Tests.Common.Customizations; +using DfE.CoreLibs.Testing.AutoFixture.Attributes; +using DfE.CoreLibs.Testing.AutoFixture.Customizations; namespace Dfe.Academies.Domain.Tests.Aggregates { diff --git a/Tests/Dfe.Academies.Domain.Tests/Aggregates/MemberContactDetailsTests.cs b/Tests/Dfe.Academies.Domain.Tests/Aggregates/MemberContactDetailsTests.cs index 727fa666c..5510d7e5f 100644 --- a/Tests/Dfe.Academies.Domain.Tests/Aggregates/MemberContactDetailsTests.cs +++ b/Tests/Dfe.Academies.Domain.Tests/Aggregates/MemberContactDetailsTests.cs @@ -1,6 +1,6 @@ using Dfe.Academies.Domain.Constituencies; using Dfe.Academies.Domain.ValueObjects; -using Dfe.Academies.Tests.Common.Attributes; +using DfE.CoreLibs.Testing.AutoFixture.Attributes; namespace Dfe.Academies.Domain.Tests.Aggregates { diff --git a/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/ConstituenciesControllerTests.cs b/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/ConstituenciesControllerTests.cs index 1af641374..81a5207c6 100644 --- a/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/ConstituenciesControllerTests.cs +++ b/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/ConstituenciesControllerTests.cs @@ -4,9 +4,9 @@ using Microsoft.EntityFrameworkCore; using PersonsApi; using System.Security.Claims; -using Dfe.Academies.Tests.Common.Attributes; using Dfe.Academies.Tests.Common.Customizations; -using Dfe.Academies.Tests.Common.Mocks; +using DfE.CoreLibs.Testing.AutoFixture.Attributes; +using DfE.CoreLibs.Testing.Mocks.WebApplicationFactory; namespace Dfe.Academies.PersonsApi.Tests.Integration.Controllers { diff --git a/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/EstablishmentsControllerTests.cs b/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/EstablishmentsControllerTests.cs index b0ff5e1bb..914e481ac 100644 --- a/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/EstablishmentsControllerTests.cs +++ b/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/EstablishmentsControllerTests.cs @@ -1,11 +1,11 @@ using Dfe.Academies.Infrastructure; +using Dfe.Academies.Tests.Common.Customizations; using Dfe.PersonsApi.Client.Contracts; using Microsoft.EntityFrameworkCore; using PersonsApi; using System.Security.Claims; -using Dfe.Academies.Tests.Common.Attributes; -using Dfe.Academies.Tests.Common.Customizations; -using Dfe.Academies.Tests.Common.Mocks; +using DfE.CoreLibs.Testing.AutoFixture.Attributes; +using DfE.CoreLibs.Testing.Mocks.WebApplicationFactory; namespace Dfe.Academies.PersonsApi.Tests.Integration.Controllers { diff --git a/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/TrustsControllerTests.cs b/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/TrustsControllerTests.cs index ccb187573..2bc839a2c 100644 --- a/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/TrustsControllerTests.cs +++ b/Tests/Dfe.Academies.PersonsApi.Tests.Integration/Controllers/TrustsControllerTests.cs @@ -1,10 +1,10 @@ using Dfe.Academies.Infrastructure; +using Dfe.Academies.Tests.Common.Customizations; using Dfe.PersonsApi.Client.Contracts; using PersonsApi; using System.Security.Claims; -using Dfe.Academies.Tests.Common.Attributes; -using Dfe.Academies.Tests.Common.Customizations; -using Dfe.Academies.Tests.Common.Mocks; +using DfE.CoreLibs.Testing.AutoFixture.Attributes; +using DfE.CoreLibs.Testing.Mocks.WebApplicationFactory; namespace Dfe.Academies.PersonsApi.Tests.Integration.Controllers { diff --git a/Tests/Dfe.Academies.PersonsApi.Tests.Integration/OpenApiTests/OpenApiDocumentTests.cs b/Tests/Dfe.Academies.PersonsApi.Tests.Integration/OpenApiTests/OpenApiDocumentTests.cs index cb339218f..408481ac9 100644 --- a/Tests/Dfe.Academies.PersonsApi.Tests.Integration/OpenApiTests/OpenApiDocumentTests.cs +++ b/Tests/Dfe.Academies.PersonsApi.Tests.Integration/OpenApiTests/OpenApiDocumentTests.cs @@ -1,8 +1,8 @@ -using PersonsApi; +using Dfe.Academies.Tests.Common.Customizations; +using DfE.CoreLibs.Testing.AutoFixture.Attributes; +using DfE.CoreLibs.Testing.Mocks.WebApplicationFactory; +using PersonsApi; using System.Net; -using Dfe.Academies.Tests.Common.Attributes; -using Dfe.Academies.Tests.Common.Customizations; -using Dfe.Academies.Tests.Common.Mocks; namespace Dfe.Academies.PersonsApi.Tests.Integration.OpenApiTests; diff --git a/Tests/Dfe.Academies.Tests.Common/Attributes/CustomAutoDataAttribute.cs b/Tests/Dfe.Academies.Tests.Common/Attributes/CustomAutoDataAttribute.cs deleted file mode 100644 index 9367bc15e..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Attributes/CustomAutoDataAttribute.cs +++ /dev/null @@ -1,17 +0,0 @@ -using AutoFixture.Xunit2; -using Dfe.Academies.Testing.Common.Customizations; -using Dfe.Academies.Tests.Common.Customizations; -using Dfe.Academies.Tests.Common.Helpers; - -namespace Dfe.Academies.Tests.Common.Attributes -{ - public class CustomAutoDataAttribute(params Type[] customizations) - : AutoDataAttribute(() => FixtureFactoryHelper.ConfigureFixtureFactory(CombineCustomizations(customizations))) - { - private static Type[] CombineCustomizations(Type[] customizations) - { - var defaultCustomizations = new[] { typeof(NSubstituteCustomization) }; - return defaultCustomizations.Concat(customizations).ToArray(); - } - } -} diff --git a/Tests/Dfe.Academies.Tests.Common/Attributes/InlineCustomAutoDataAttribute.cs b/Tests/Dfe.Academies.Tests.Common/Attributes/InlineCustomAutoDataAttribute.cs deleted file mode 100644 index 1ab3ff006..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Attributes/InlineCustomAutoDataAttribute.cs +++ /dev/null @@ -1,7 +0,0 @@ -using AutoFixture.Xunit2; - -namespace Dfe.Academies.Tests.Common.Attributes -{ - public class InlineCustomAutoDataAttribute(object[] values, params Type[] customizations) - : InlineAutoDataAttribute(new CustomAutoDataAttribute(customizations), values); -} diff --git a/Tests/Dfe.Academies.Tests.Common/Customizations/AutoMapperCustomization.cs b/Tests/Dfe.Academies.Tests.Common/Customizations/AutoMapperCustomization.cs deleted file mode 100644 index 31c4d45cd..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Customizations/AutoMapperCustomization.cs +++ /dev/null @@ -1,31 +0,0 @@ -using AutoFixture; -using AutoMapper; -using Dfe.Academies.Application.MappingProfiles; - -namespace Dfe.Academies.Tests.Common.Customizations -{ - public class AutoMapperCustomization : ICustomization - { - public void Customize(IFixture fixture) - { - fixture.Customize(composer => composer.FromFactory(() => - { - var profiles = typeof(ConstituencyProfile).Assembly - .GetTypes() - .Where(t => typeof(Profile).IsAssignableFrom(t) && !t.IsAbstract) - .ToList(); - - var config = new MapperConfiguration(cfg => - { - foreach (var profileType in profiles) - { - var profileInstance = (Profile)Activator.CreateInstance(profileType)!; - cfg.AddProfile(profileInstance); - } - }); - - return config.CreateMapper(); - })); - } - } -} diff --git a/Tests/Dfe.Academies.Tests.Common/Customizations/CustomWebApplicationDbContextFactoryCustomization.cs b/Tests/Dfe.Academies.Tests.Common/Customizations/CustomWebApplicationDbContextFactoryCustomization.cs index f7527c4bf..1e713bc93 100644 --- a/Tests/Dfe.Academies.Tests.Common/Customizations/CustomWebApplicationDbContextFactoryCustomization.cs +++ b/Tests/Dfe.Academies.Tests.Common/Customizations/CustomWebApplicationDbContextFactoryCustomization.cs @@ -1,25 +1,53 @@ using System.Security.Claims; using AutoFixture; -using Dfe.Academies.Tests.Common.Mocks; +using DfE.CoreLibs.Testing.Mocks.WebApplicationFactory; using Dfe.PersonsApi.Client; using Dfe.PersonsApi.Client.Contracts; using Dfe.PersonsApi.Client.Extensions; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using DfE.CoreLibs.Testing.Mocks.Authentication; +using Microsoft.AspNetCore.Authentication; +using System.Net.Http.Headers; +using Dfe.Academies.Domain.Constituencies; +using Dfe.Academies.Domain.Establishment; +using Dfe.Academies.Domain.Trust; +using Dfe.Academies.Domain.ValueObjects; +using Dfe.Academies.Infrastructure; +using TrustGovernance = Dfe.Academies.Domain.Trust.TrustGovernance; namespace Dfe.Academies.Tests.Common.Customizations { public class CustomWebApplicationDbContextFactoryCustomization : ICustomization where TProgram : class where TDbContext : DbContext { + public Action? SeedAction { get; set; } + public void Customize(IFixture fixture) { fixture.Customize>(composer => composer.FromFactory(() => { - var factory = new CustomWebApplicationDbContextFactory(); + var factory = new CustomWebApplicationDbContextFactory() + { + SeedData = SeedAction ?? DefaultSeedData, + ExternalServicesConfiguration = services => + { + services.PostConfigure(options => + { + options.DefaultAuthenticateScheme = "TestScheme"; + options.DefaultChallengeScheme = "TestScheme"; + }); + services.AddAuthentication("TestScheme") + .AddScheme("TestScheme", options => { }); + }, + ExternalHttpClientConfiguration = client => + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "external-mock-token"); + } + }; var client = factory.CreateClient(); var config = new ConfigurationBuilder() @@ -49,5 +77,207 @@ public void Customize(IFixture fixture) return factory; })); } + + private static void DefaultSeedData(TDbContext context) + { + if (context is MstrContext mstrContext) + { + if (!mstrContext.Trusts.Any() && !mstrContext.Establishments.Any() && + !mstrContext.EducationEstablishmentTrusts.Any() && !mstrContext.GovernanceRoleTypes.Any() && + !mstrContext.EducationEstablishmentGovernances.Any()) + { + + // Populate Trust + var trust1 = new Trust + { + SK = 1, + Name = "Trust A", + TrustTypeId = mstrContext.TrustTypes.FirstOrDefault()?.SK, + GroupUID = "G1", + Modified = DateTime.UtcNow, + ModifiedBy = "System", + UKPRN = "12345678", + GroupID = "TR00024" + }; + var trust2 = new Trust + { + SK = 2, + Name = "Trust B", + TrustTypeId = mstrContext.TrustTypes.FirstOrDefault()?.SK, + GroupUID = "G2", + Modified = DateTime.UtcNow, + ModifiedBy = "System", + UKPRN = "87654321", + GroupID = "TR00025" + }; + mstrContext.Trusts.AddRange(trust1, trust2); + + // Populate Establishment + var establishment1 = new Establishment + { + SK = 1, + EstablishmentName = "School A", + LocalAuthorityId = mstrContext.LocalAuthorities.FirstOrDefault()?.SK, + EstablishmentTypeId = mstrContext.EstablishmentTypes.FirstOrDefault()?.SK, + Latitude = 54.9784, + Longitude = -1.6174, + MainPhone = "01234567890", + Email = "schoolA@example.com", + Modified = DateTime.UtcNow, + ModifiedBy = "System" + }; + var establishment2 = new Establishment + { + SK = 2, + EstablishmentName = "School B", + LocalAuthorityId = mstrContext.LocalAuthorities.FirstOrDefault()?.SK, + EstablishmentTypeId = mstrContext.EstablishmentTypes.FirstOrDefault()?.SK, + Latitude = 50.3763, + Longitude = -4.1427, + MainPhone = "09876543210", + Email = "schoolB@example.com", + Modified = DateTime.UtcNow, + ModifiedBy = "System" + }; + mstrContext.Establishments.AddRange(establishment1, establishment2); + + // Populate EducationEstablishmentTrust + var educationEstablishmentTrust1 = new EducationEstablishmentTrust + { + SK = 1, + EducationEstablishmentId = (int)establishment1.SK, + TrustId = (int)trust1.SK, + }; + var educationEstablishmentTrust2 = new EducationEstablishmentTrust + { + SK = 2, + EducationEstablishmentId = (int)establishment2.SK, + TrustId = (int)trust2.SK, + + }; + mstrContext.EducationEstablishmentTrusts.AddRange(educationEstablishmentTrust1, educationEstablishmentTrust2); + + // Populate GovernanceRoleType + var governanceRoleType1 = new GovernanceRoleType + { SK = 1, Name = "Chair of Governors", Modified = DateTime.UtcNow, ModifiedBy = "System" }; + var governanceRoleType2 = new GovernanceRoleType + { SK = 2, Name = "Vice Chair of Governors", Modified = DateTime.UtcNow, ModifiedBy = "System" }; + var governanceRoleType3 = new GovernanceRoleType + { SK = 3, Name = "Trustee", Modified = DateTime.UtcNow, ModifiedBy = "System" }; + mstrContext.GovernanceRoleTypes.AddRange(governanceRoleType1, governanceRoleType2, governanceRoleType3); + + // Populate EducationEstablishmentGovernance + var governance1 = new EducationEstablishmentGovernance + { + SK = 1, + EducationEstablishmentId = establishment1.SK, + GovernanceRoleTypeId = governanceRoleType1.SK, + GID = "GID1", + Title = "Mr.", + Forename1 = "John", + Surname = "Doe", + Email = "johndoe@example.com", + Modified = DateTime.UtcNow, + ModifiedBy = "System" + }; + var governance3 = new EducationEstablishmentGovernance + { + SK = 3, + EducationEstablishmentId = establishment1.SK, + GovernanceRoleTypeId = governanceRoleType2.SK, + GID = "GID2", + Title = "Ms.", + Forename1 = "Anna", + Surname = "Smith", + Email = "annasmith@example.com", + Modified = DateTime.UtcNow, + ModifiedBy = "System" + }; + mstrContext.EducationEstablishmentGovernances.AddRange(governance1, governance3); + + // Populate TrustGovernance + var trustGovernance1 = new TrustGovernance + { + SK = 1, + TrustId = trust2.SK, + GovernanceRoleTypeId = governanceRoleType3.SK, + GID = "GID1", + Title = "Mr.", + Forename1 = "John", + Surname = "Wood", + Email = "johnWood@example.com", + Modified = DateTime.UtcNow, + ModifiedBy = "System" + }; + var trustGovernance2 = new TrustGovernance + { + SK = 2, + TrustId = trust2.SK, + GovernanceRoleTypeId = governanceRoleType3.SK, + GID = "GID1", + Title = "Mr.", + Forename1 = "Joe", + Surname = "Wood", + Email = "joeWood@example.com", + Modified = DateTime.UtcNow, + ModifiedBy = "System" + }; + mstrContext.TrustGovernances.AddRange(trustGovernance1, trustGovernance2); + + // Save changes + mstrContext.SaveChanges(); + } + } + + if (context is MopContext mopContext) + { + var memberContact1 = new MemberContactDetails( + new MemberId(1), + 1, + "test1@example.com", + null + ); + + var memberContact2 = new MemberContactDetails( + new MemberId(2), + 1, + "test2@example.com", + null + ); + + var constituency1 = new Constituency( + new ConstituencyId(1), + new MemberId(1), + "Test Constituency 1", + new NameDetails( + "Wood, John", + "John Wood", + "Mr. John Wood MP" + ), + DateTime.UtcNow, + null, + memberContact1 + ); + + var constituency2 = new Constituency( + new ConstituencyId(2), + new MemberId(2), + "Test Constituency 2", + new NameDetails( + "Wood, Joe", + "Joe Wood", + "Mr. Joe Wood MP" + ), + DateTime.UtcNow, + null, + memberContact2 + ); + + mopContext.Constituencies.Add(constituency1); + mopContext.Constituencies.Add(constituency2); + + mopContext.SaveChanges(); + } + } } } diff --git a/Tests/Dfe.Academies.Tests.Common/Customizations/CustomWebApplicationFactoryCustomization.cs b/Tests/Dfe.Academies.Tests.Common/Customizations/CustomWebApplicationFactoryCustomization.cs index 523d53ecc..115af7936 100644 --- a/Tests/Dfe.Academies.Tests.Common/Customizations/CustomWebApplicationFactoryCustomization.cs +++ b/Tests/Dfe.Academies.Tests.Common/Customizations/CustomWebApplicationFactoryCustomization.cs @@ -1,6 +1,6 @@ using System.Security.Claims; using AutoFixture; -using Dfe.Academies.Tests.Common.Mocks; +using DfE.CoreLibs.Testing.Mocks.WebApplicationFactory; using Dfe.PersonsApi.Client; using Dfe.PersonsApi.Client.Contracts; using Dfe.PersonsApi.Client.Extensions; diff --git a/Tests/Dfe.Academies.Tests.Common/Customizations/DateOnlyCustomization.cs b/Tests/Dfe.Academies.Tests.Common/Customizations/DateOnlyCustomization.cs deleted file mode 100644 index 96b39fc98..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Customizations/DateOnlyCustomization.cs +++ /dev/null @@ -1,14 +0,0 @@ -using AutoFixture; - -namespace Dfe.Academies.Tests.Common.Customizations -{ - public class DateOnlyCustomization : ICustomization - { - public void Customize(IFixture fixture) - { - fixture.Customize(composer => - composer.FromFactory(() => - DateOnly.FromDateTime(fixture.Create()))); - } - } -} diff --git a/Tests/Dfe.Academies.Tests.Common/Customizations/DbContextCustomization.cs b/Tests/Dfe.Academies.Tests.Common/Customizations/DbContextCustomization.cs deleted file mode 100644 index 0fb493287..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Customizations/DbContextCustomization.cs +++ /dev/null @@ -1,23 +0,0 @@ -using AutoFixture; -using Dfe.Academies.Tests.Common.Helpers; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; - -namespace Dfe.Academies.Tests.Common.Customizations -{ - public class DbContextCustomization : ICustomization where TContext : DbContext - { - public void Customize(IFixture fixture) - { - fixture.Register>(() => null!); - - fixture.Customize(composer => composer.FromFactory(() => - { - var services = new ServiceCollection(); - var dbContext = DbContextHelper.CreateDbContext(services); - fixture.Inject(dbContext); - return dbContext; - }).OmitAutoProperties()); - } - } -} diff --git a/Tests/Dfe.Academies.Tests.Common/Customizations/NSubstituteCustomization.cs b/Tests/Dfe.Academies.Tests.Common/Customizations/NSubstituteCustomization.cs deleted file mode 100644 index bd8ccf227..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Customizations/NSubstituteCustomization.cs +++ /dev/null @@ -1,13 +0,0 @@ -using AutoFixture; -using AutoFixture.AutoNSubstitute; - -namespace Dfe.Academies.Tests.Common.Customizations -{ - public class NSubstituteCustomization : ICustomization - { - public void Customize(IFixture fixture) - { - fixture.Customize(new AutoNSubstituteCustomization()); - } - } -} diff --git a/Tests/Dfe.Academies.Tests.Common/Customizations/OmitCircularReferenceCustomization.cs b/Tests/Dfe.Academies.Tests.Common/Customizations/OmitCircularReferenceCustomization.cs deleted file mode 100644 index c38afcb76..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Customizations/OmitCircularReferenceCustomization.cs +++ /dev/null @@ -1,15 +0,0 @@ -using AutoFixture; - -namespace Dfe.Academies.Tests.Common.Customizations -{ - public class OmitCircularReferenceCustomization : ICustomization - { - public void Customize(IFixture fixture) - { - fixture.Behaviors.OfType().ToList() - .ForEach(b => fixture.Behaviors.Remove(b)); - - fixture.Behaviors.Add(new OmitOnRecursionBehavior()); - } - } -} diff --git a/Tests/Dfe.Academies.Tests.Common/Dfe.Academies.Tests.Common.csproj b/Tests/Dfe.Academies.Tests.Common/Dfe.Academies.Tests.Common.csproj index 4a3a8a2d6..af009cb05 100644 --- a/Tests/Dfe.Academies.Tests.Common/Dfe.Academies.Tests.Common.csproj +++ b/Tests/Dfe.Academies.Tests.Common/Dfe.Academies.Tests.Common.csproj @@ -20,6 +20,7 @@ + diff --git a/Tests/Dfe.Academies.Tests.Common/Helpers/DbContextHelper.cs b/Tests/Dfe.Academies.Tests.Common/Helpers/DbContextHelper.cs deleted file mode 100644 index b0a379c3c..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Helpers/DbContextHelper.cs +++ /dev/null @@ -1,240 +0,0 @@ -using System.Data.Common; -using Dfe.Academies.Domain.Constituencies; -using Dfe.Academies.Domain.Establishment; -using Dfe.Academies.Domain.Trust; -using Dfe.Academies.Domain.ValueObjects; -using Dfe.Academies.Infrastructure; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; - -namespace Dfe.Academies.Tests.Common.Helpers -{ - public static class DbContextHelper where TContext : DbContext - { - public static TContext CreateDbContext(IServiceCollection services) - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - services.AddSingleton(_ => connection); - - services.AddDbContext((sp, options) => - { - var conn = sp.GetRequiredService(); - options.UseSqlite(conn); - }); - - var serviceProvider = services.BuildServiceProvider(); - var dbContext = serviceProvider.GetRequiredService(); - - dbContext.Database.EnsureCreated(); - SeedTestData(dbContext); - - return dbContext; - } - - private static void SeedTestData(TContext context) - { - if (context is MstrContext mstrContext) - { - if (!mstrContext.Trusts.Any() && !mstrContext.Establishments.Any() && - !mstrContext.EducationEstablishmentTrusts.Any() && !mstrContext.GovernanceRoleTypes.Any() && - !mstrContext.EducationEstablishmentGovernances.Any()) - { - - // Populate Trust - var trust1 = new Trust - { - SK = 1, - Name = "Trust A", - TrustTypeId = mstrContext.TrustTypes.FirstOrDefault()?.SK, - GroupUID = "G1", - Modified = DateTime.UtcNow, - ModifiedBy = "System", - UKPRN = "12345678", - GroupID = "TR00024" - }; - var trust2 = new Trust - { - SK = 2, - Name = "Trust B", - TrustTypeId = mstrContext.TrustTypes.FirstOrDefault()?.SK, - GroupUID = "G2", - Modified = DateTime.UtcNow, - ModifiedBy = "System", - UKPRN = "87654321", - GroupID = "TR00025" - }; - mstrContext.Trusts.AddRange(trust1, trust2); - - // Populate Establishment - var establishment1 = new Establishment - { - SK = 1, - EstablishmentName = "School A", - LocalAuthorityId = mstrContext.LocalAuthorities.FirstOrDefault()?.SK, - EstablishmentTypeId = mstrContext.EstablishmentTypes.FirstOrDefault()?.SK, - Latitude = 54.9784, - Longitude = -1.6174, - MainPhone = "01234567890", - Email = "schoolA@example.com", - Modified = DateTime.UtcNow, - ModifiedBy = "System" - }; - var establishment2 = new Establishment - { - SK = 2, - EstablishmentName = "School B", - LocalAuthorityId = mstrContext.LocalAuthorities.FirstOrDefault()?.SK, - EstablishmentTypeId = mstrContext.EstablishmentTypes.FirstOrDefault()?.SK, - Latitude = 50.3763, - Longitude = -4.1427, - MainPhone = "09876543210", - Email = "schoolB@example.com", - Modified = DateTime.UtcNow, - ModifiedBy = "System" - }; - mstrContext.Establishments.AddRange(establishment1, establishment2); - - // Populate EducationEstablishmentTrust - var educationEstablishmentTrust1 = new EducationEstablishmentTrust - { - SK = 1, - EducationEstablishmentId = (int)establishment1.SK, - TrustId = (int)trust1.SK, - }; - var educationEstablishmentTrust2 = new EducationEstablishmentTrust - { - SK = 2, - EducationEstablishmentId = (int)establishment2.SK, - TrustId = (int)trust2.SK, - - }; - mstrContext.EducationEstablishmentTrusts.AddRange(educationEstablishmentTrust1, educationEstablishmentTrust2); - - // Populate GovernanceRoleType - var governanceRoleType1 = new GovernanceRoleType - { SK = 1, Name = "Chair of Governors", Modified = DateTime.UtcNow, ModifiedBy = "System" }; - var governanceRoleType2 = new GovernanceRoleType - { SK = 2, Name = "Vice Chair of Governors", Modified = DateTime.UtcNow, ModifiedBy = "System" }; - var governanceRoleType3 = new GovernanceRoleType - { SK = 3, Name = "Trustee", Modified = DateTime.UtcNow, ModifiedBy = "System" }; - mstrContext.GovernanceRoleTypes.AddRange(governanceRoleType1, governanceRoleType2, governanceRoleType3); - - // Populate EducationEstablishmentGovernance - var governance1 = new EducationEstablishmentGovernance - { - SK = 1, - EducationEstablishmentId = establishment1.SK, - GovernanceRoleTypeId = governanceRoleType1.SK, - GID = "GID1", - Title = "Mr.", - Forename1 = "John", - Surname = "Doe", - Email = "johndoe@example.com", - Modified = DateTime.UtcNow, - ModifiedBy = "System" - }; - var governance3 = new EducationEstablishmentGovernance - { - SK = 3, - EducationEstablishmentId = establishment1.SK, - GovernanceRoleTypeId = governanceRoleType2.SK, - GID = "GID2", - Title = "Ms.", - Forename1 = "Anna", - Surname = "Smith", - Email = "annasmith@example.com", - Modified = DateTime.UtcNow, - ModifiedBy = "System" - }; - mstrContext.EducationEstablishmentGovernances.AddRange(governance1, governance3); - - // Populate TrustGovernance - var trustGovernance1 = new TrustGovernance - { - SK = 1, - TrustId = trust2.SK, - GovernanceRoleTypeId = governanceRoleType3.SK, - GID = "GID1", - Title = "Mr.", - Forename1 = "John", - Surname = "Wood", - Email = "johnWood@example.com", - Modified = DateTime.UtcNow, - ModifiedBy = "System" - }; - var trustGovernance2 = new TrustGovernance - { - SK = 2, - TrustId = trust2.SK, - GovernanceRoleTypeId = governanceRoleType3.SK, - GID = "GID1", - Title = "Mr.", - Forename1 = "Joe", - Surname = "Wood", - Email = "joeWood@example.com", - Modified = DateTime.UtcNow, - ModifiedBy = "System" - }; - mstrContext.TrustGovernances.AddRange(trustGovernance1, trustGovernance2); - - // Save changes - mstrContext.SaveChanges(); - } - } - - if (context is MopContext mopContext) - { - var memberContact1 = new MemberContactDetails( - new MemberId(1), - 1, - "test1@example.com", - null - ); - - var memberContact2 = new MemberContactDetails( - new MemberId(2), - 1, - "test2@example.com", - null - ); - - var constituency1 = new Constituency( - new ConstituencyId(1), - new MemberId(1), - "Test Constituency 1", - new NameDetails( - "Wood, John", - "John Wood", - "Mr. John Wood MP" - ), - DateTime.UtcNow, - null, - memberContact1 - ); - - var constituency2 = new Constituency( - new ConstituencyId(2), - new MemberId(2), - "Test Constituency 2", - new NameDetails( - "Wood, Joe", - "Joe Wood", - "Mr. Joe Wood MP" - ), - DateTime.UtcNow, - null, - memberContact2 - ); - - mopContext.Constituencies.Add(constituency1); - mopContext.Constituencies.Add(constituency2); - - mopContext.SaveChanges(); - } - } - - } -} diff --git a/Tests/Dfe.Academies.Tests.Common/Helpers/FixtureFactoryHelper.cs b/Tests/Dfe.Academies.Tests.Common/Helpers/FixtureFactoryHelper.cs deleted file mode 100644 index f76995a8c..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Helpers/FixtureFactoryHelper.cs +++ /dev/null @@ -1,20 +0,0 @@ -using AutoFixture; - -namespace Dfe.Academies.Tests.Common.Helpers -{ - public static class FixtureFactoryHelper - { - public static IFixture ConfigureFixtureFactory(Type[] customizations) - { - var fixture = new Fixture(); - - foreach (var customizationType in customizations) - { - var customization = (ICustomization)Activator.CreateInstance(customizationType)!; - fixture.Customize(customization); - } - - return fixture; - } - } -} diff --git a/Tests/Dfe.Academies.Tests.Common/Mocks/CustomWebApplicationDbContextFactory.cs b/Tests/Dfe.Academies.Tests.Common/Mocks/CustomWebApplicationDbContextFactory.cs deleted file mode 100644 index f2384d58c..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Mocks/CustomWebApplicationDbContextFactory.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System.Data.Common; -using System.Net.Http.Headers; -using System.Security.Claims; -using Dfe.Academies.Tests.Common.Helpers; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; - -namespace Dfe.Academies.Tests.Common.Mocks -{ - public class CustomWebApplicationDbContextFactory : WebApplicationFactory - where TProgram : class where TDbContext : DbContext - { - public List? TestClaims { get; set; } = []; - - protected override void ConfigureWebHost(IWebHostBuilder builder) - { - builder.ConfigureServices(services => - { - var dbContextDescriptor = services.SingleOrDefault( - d => d.ServiceType == typeof(DbContextOptions)); - services.Remove(dbContextDescriptor!); - - var dbConnectionDescriptor = services.SingleOrDefault( - d => d.ServiceType == typeof(DbConnection)); - services.Remove(dbConnectionDescriptor!); - - DbContextHelper.CreateDbContext(services); - - services.PostConfigure(options => - { - options.DefaultAuthenticateScheme = "TestScheme"; - options.DefaultChallengeScheme = "TestScheme"; - }); - - services.AddAuthentication("TestScheme") - .AddScheme("TestScheme", options => { }); - - services.AddSingleton>(sp => TestClaims ?? []); - }); - - builder.UseEnvironment("Development"); - } - - protected override void ConfigureClient(HttpClient client) - { - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "mock-token"); - - base.ConfigureClient(client); - } - - public TDbContext GetDbContext() - { - var scopeFactory = Services.GetRequiredService(); - var scope = scopeFactory.CreateScope(); - return scope.ServiceProvider.GetRequiredService(); - } - - } -} diff --git a/Tests/Dfe.Academies.Tests.Common/Mocks/CustomWebApplicationFactory.cs b/Tests/Dfe.Academies.Tests.Common/Mocks/CustomWebApplicationFactory.cs deleted file mode 100644 index 30d3a700e..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Mocks/CustomWebApplicationFactory.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Net.Http.Headers; -using System.Security.Claims; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.Extensions.DependencyInjection; - -namespace Dfe.Academies.Tests.Common.Mocks -{ - public class CustomWebApplicationFactory : WebApplicationFactory - where TProgram : class - { - public List? TestClaims { get; set; } = []; - - protected override void ConfigureWebHost(IWebHostBuilder builder) - { - builder.ConfigureServices(services => - { - services.PostConfigure(options => - { - options.DefaultAuthenticateScheme = "TestScheme"; - options.DefaultChallengeScheme = "TestScheme"; - }); - - services.AddAuthentication("TestScheme") - .AddScheme("TestScheme", options => { }); - - services.AddSingleton>(sp => TestClaims ?? []); - }); - - builder.UseEnvironment("Development"); - } - - protected override void ConfigureClient(HttpClient client) - { - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "mock-token"); - - base.ConfigureClient(client); - } - } -} diff --git a/Tests/Dfe.Academies.Tests.Common/Mocks/MockJwtBearerHandler.cs b/Tests/Dfe.Academies.Tests.Common/Mocks/MockJwtBearerHandler.cs deleted file mode 100644 index 6e1ee6d09..000000000 --- a/Tests/Dfe.Academies.Tests.Common/Mocks/MockJwtBearerHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.Security.Claims; -using System.Text.Encodings.Web; -using Microsoft.AspNetCore.Authentication; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; - -namespace Dfe.Academies.Tests.Common.Mocks -{ -#pragma warning disable CS0618 - public class MockJwtBearerHandler( - IOptionsMonitor options, - ILoggerFactory logger, - UrlEncoder encoder, - ISystemClock clock, - IEnumerable claims) - : AuthenticationHandler(options, logger, encoder, clock) - { - protected override Task HandleAuthenticateAsync() - { - var identity = new ClaimsIdentity(claims, "mock"); - var principal = new ClaimsPrincipal(identity); - var ticket = new AuthenticationTicket(principal, "mock"); - - return Task.FromResult(AuthenticateResult.Success(ticket)); - } - } -#pragma warning restore CS0618 - -} diff --git a/TramsDataApi/Controllers/V4/EstablishmentsController.cs b/TramsDataApi/Controllers/V4/EstablishmentsController.cs index a7c7b9323..df0853ca2 100644 --- a/TramsDataApi/Controllers/V4/EstablishmentsController.cs +++ b/TramsDataApi/Controllers/V4/EstablishmentsController.cs @@ -1,14 +1,12 @@ -using System.Collections.Generic; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; -using Azure.Core; using Dfe.Academies.Application.Establishment; -using Dfe.Academies.Contracts.V4.Establishments; +using DfE.CoreLibs.Contracts.Academies.V4.Establishments; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Swashbuckle.AspNetCore.Annotations; -using TramsDataApi.RequestModels; +using System.Collections.Generic; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; using TramsDataApi.ResponseModels; namespace TramsDataApi.Controllers.V4 diff --git a/TramsDataApi/Controllers/V4/TrustsController.cs b/TramsDataApi/Controllers/V4/TrustsController.cs index 77ecba855..ec904f239 100644 --- a/TramsDataApi/Controllers/V4/TrustsController.cs +++ b/TramsDataApi/Controllers/V4/TrustsController.cs @@ -1,6 +1,4 @@ using Dfe.Academies.Application.Trust; -using Dfe.Academies.Contracts.V4; -using Dfe.Academies.Contracts.V4.Trusts; using Dfe.Academies.Domain.Trust; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -10,6 +8,8 @@ using System.Text.Json; using System.Threading; using System.Threading.Tasks; +using DfE.CoreLibs.Contracts.Academies.V4; +using DfE.CoreLibs.Contracts.Academies.V4.Trusts; using TramsDataApi.ResponseModels; namespace TramsDataApi.Controllers.V4 diff --git a/TramsDataApi/ResponseModels/EducationalPerformance/EducationalPerformanceResponse.cs b/TramsDataApi/ResponseModels/EducationalPerformance/EducationalPerformanceResponse.cs index e6a473390..5c0411d1b 100644 --- a/TramsDataApi/ResponseModels/EducationalPerformance/EducationalPerformanceResponse.cs +++ b/TramsDataApi/ResponseModels/EducationalPerformance/EducationalPerformanceResponse.cs @@ -1,5 +1,4 @@ -using Dfe.Academies.Application.EducationalPerformance; -using Dfe.Academies.Contracts.V1.EducationalPerformance; +using DfE.CoreLibs.Contracts.Academies.V1.EducationalPerformance; using System.Collections.Generic; namespace TramsDataApi.ResponseModels.EducationalPerformance @@ -10,10 +9,7 @@ public class EducationalPerformanceResponse public List KeyStage1 { get; set; } public List KeyStage2 { get; set; } public List KeyStage4 { get; set; } - public List KeyStage5 { get; set; } public List AbsenceData { get; set; } - - } } \ No newline at end of file diff --git a/TramsDataApi/ResponseModels/PagingResponseFactory.cs b/TramsDataApi/ResponseModels/PagingResponseFactory.cs index f8e25c3ef..43edceb32 100644 --- a/TramsDataApi/ResponseModels/PagingResponseFactory.cs +++ b/TramsDataApi/ResponseModels/PagingResponseFactory.cs @@ -32,9 +32,9 @@ public static PagingResponse Create(int page, int count, int recordCount, HttpRe return pagingResponse; } - public static Dfe.Academies.Contracts.V4.PagingResponse CreateV4PagingResponse(int page, int count, int recordCount, HttpRequest request) + public static DfE.CoreLibs.Contracts.Academies.V4.PagingResponse CreateV4PagingResponse(int page, int count, int recordCount, HttpRequest request) { - var pagingResponse = new Dfe.Academies.Contracts.V4.PagingResponse + var pagingResponse = new DfE.CoreLibs.Contracts.Academies.V4.PagingResponse { RecordCount = recordCount, Page = page diff --git a/TramsDataApi/Startup.cs b/TramsDataApi/Startup.cs index f4da1f08b..31a3ae130 100644 --- a/TramsDataApi/Startup.cs +++ b/TramsDataApi/Startup.cs @@ -1,6 +1,7 @@ -using Dfe.Academisation.CorrelationIdMiddleware; using Microsoft.AspNetCore.HttpOverrides; using System.Text.Json.Serialization; +using DfE.CoreLibs.Http.Interfaces; +using DfE.CoreLibs.Http.Middlewares.CorrelationId; namespace TramsDataApi { diff --git a/TramsDataApi/TramsDataApi.csproj b/TramsDataApi/TramsDataApi.csproj index c3dfae228..4943acd65 100644 --- a/TramsDataApi/TramsDataApi.csproj +++ b/TramsDataApi/TramsDataApi.csproj @@ -18,8 +18,7 @@ - - +