Skip to content

Commit

Permalink
SPDBT-2890: A big refactoring of tests, cache and more (#1305)
Browse files Browse the repository at this point in the history
  • Loading branch information
ytqsl authored Aug 16, 2024
1 parent 8eeeb00 commit 61d34bc
Show file tree
Hide file tree
Showing 66 changed files with 893 additions and 943 deletions.
13 changes: 7 additions & 6 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@
<PackageVersion Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageVersion Include="AutoFixture" Version="4.18.1" />
<PackageVersion Include="AutoMapper" Version="13.0.1" />
<PackageVersion Include="AWSSDK.S3" Version="3.7.400.4" />
<PackageVersion Include="AWSSDK.S3" Version="3.7.401" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="FluentValidation" Version="11.9.2" />
<PackageVersion Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageVersion Include="Flurl" Version="4.0.0" />
<PackageVersion Include="IdentityModel" Version="7.0.0" />
<PackageVersion Include="MartinCostello.Logging.XUnit" Version="0.4.0" />
<PackageVersion Include="MediatR" Version="12.4.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
<PackageVersion Include="Microsoft.AspNetCore.SpaProxy" Version="8.0.7" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageVersion Include="Microsoft.AspNetCore.SpaProxy" Version="8.0.8" />
<PackageVersion Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.7" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.8" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="8.0.7" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="8.0.8" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
Expand Down
18 changes: 11 additions & 7 deletions src/Spd.Manager.Common.UnitTest/AdminManagerTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using AutoFixture;
using AutoMapper;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Moq;
using Spd.Manager.Common.Admin;
using Spd.Resource.Repository.Config;
Expand All @@ -11,11 +14,12 @@ namespace Spd.Manager.Common.UnitTest;
public class AdminManagerTest
{
private readonly IFixture fixture;
private Mock<IAddressAutocompleteClient> mockAddressClient = new();
private Mock<IConfigRepository> mockConfigRepo = new();
private Mock<IOrgRepository> mockOrgRepo = new();
private Mock<IMapper> mockMapper = new();
private AdminManager sut;
private readonly Mock<IAddressAutocompleteClient> mockAddressClient = new();
private readonly Mock<IConfigRepository> mockConfigRepo = new();
private readonly Mock<IOrgRepository> mockOrgRepo = new();
private readonly Mock<IMapper> mockMapper = new();
private readonly IDistributedCache cache = new MemoryDistributedCache(Options.Create<MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions()));
private readonly AdminManager sut;

public AdminManagerTest()
{
Expand Down Expand Up @@ -51,11 +55,11 @@ public AdminManagerTest()
mockOrgRepo.Setup(m => m.QueryOrgAsync(It.IsAny<OrgsQry>(), CancellationToken.None))
.ReturnsAsync(orgsQryResult);

sut = new AdminManager(mockAddressClient.Object, mockMapper.Object, mockConfigRepo.Object, mockOrgRepo.Object);
sut = new AdminManager(mockAddressClient.Object, mockMapper.Object, mockConfigRepo.Object, mockOrgRepo.Object, cache);
}

[Fact]
public async void Handle_FindAddressQuery_Return_AddressFindResponse()
public async Task Handle_FindAddressQuery_Return_AddressFindResponse()
{
FindAddressQuery request = new FindAddressQuery("test");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="AutoFixture" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit" />
Expand Down
40 changes: 26 additions & 14 deletions src/Spd.Manager.Common/Admin/AdminManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoMapper;
using MediatR;
using Microsoft.Extensions.Caching.Distributed;
using Spd.Resource.Repository.Config;
using Spd.Resource.Repository.Org;
using Spd.Utilities.Address;
Expand All @@ -11,11 +12,12 @@ internal class AdminManager(
IAddressAutocompleteClient _addressClient,
IMapper _mapper,
IConfigRepository _configRepo,
IOrgRepository _orgRepo)
IOrgRepository _orgRepo,
IDistributedCache _cache)
: IRequestHandler<FindAddressQuery, IEnumerable<AddressFindResponse>>,
IRequestHandler<RetrieveAddressByIdQuery, IEnumerable<AddressRetrieveResponse>>,
IRequestHandler<GetBannerMsgQuery, string>,
IRequestHandler<GetReplacementProcessingTimeQuery, string>,
IRequestHandler<GetBannerMsgQuery, string?>,
IRequestHandler<GetReplacementProcessingTimeQuery, string?>,
IRequestHandler<GetMinistryQuery, IEnumerable<MinistryResponse>>,
IAdminManager
{
Expand All @@ -35,25 +37,35 @@ public async Task<IEnumerable<AddressRetrieveResponse>> Handle(RetrieveAddressBy
return _mapper.Map<IEnumerable<AddressRetrieveResponse>>(result);
}

public async Task<string> Handle(GetBannerMsgQuery query, CancellationToken cancellationToken)
public async Task<string?> Handle(GetBannerMsgQuery query, CancellationToken cancellationToken)
{
return (await _configRepo.Query(new ConfigQuery(IConfigRepository.BANNER_MSG_CONFIG_KEY), cancellationToken))
.ConfigItems
.First()
.Value;
var result = await _cache.GetAsync(
IConfigRepository.BANNER_MSG_CONFIG_KEY,
async ct => (await _configRepo.Query(new ConfigQuery(IConfigRepository.BANNER_MSG_CONFIG_KEY), cancellationToken)).ConfigItems,
TimeSpan.FromMinutes(15),
cancellationToken);

return result?.FirstOrDefault()?.Value;
}

public async Task<string> Handle(GetReplacementProcessingTimeQuery query, CancellationToken cancellationToken)
public async Task<string?> Handle(GetReplacementProcessingTimeQuery query, CancellationToken cancellationToken)
{
return (await _configRepo.Query(new ConfigQuery(IConfigRepository.LICENSING_REPLACEMENTPROCESSINGTIME_KEY), cancellationToken))
.ConfigItems
.First()
.Value;
var result = await _cache.GetAsync(
IConfigRepository.LICENSING_REPLACEMENTPROCESSINGTIME_KEY,
async ct => (await _configRepo.Query(new ConfigQuery(IConfigRepository.LICENSING_REPLACEMENTPROCESSINGTIME_KEY), cancellationToken)).ConfigItems,
TimeSpan.FromMinutes(15),
cancellationToken);

return result?.FirstOrDefault()?.Value;
}

public async Task<IEnumerable<MinistryResponse>> Handle(GetMinistryQuery request, CancellationToken cancellationToken)
{
var result = (OrgsQryResult)await _orgRepo.QueryOrgAsync(new OrgsQry(null, ParentOrgId: SpdConstants.BcGovOrgId, IncludeInactive: true), cancellationToken);
var result = await _cache.GetAsync(
"ministries_list",
async ct => (OrgsQryResult)await _orgRepo.QueryOrgAsync(new OrgsQry(null, ParentOrgId: SpdConstants.BcGovOrgId, IncludeInactive: true), ct),
TimeSpan.FromMinutes(15),
cancellationToken);
return _mapper.Map<IEnumerable<MinistryResponse>>(result.OrgResults);
}
}
12 changes: 7 additions & 5 deletions src/Spd.Manager.Common/Admin/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ namespace Spd.Manager.Common.Admin
public interface IAdminManager
{
public Task<IEnumerable<AddressFindResponse>> Handle(FindAddressQuery request, CancellationToken cancellationToken);

public Task<IEnumerable<AddressRetrieveResponse>> Handle(RetrieveAddressByIdQuery request, CancellationToken cancellationToken);
public Task<string> Handle(GetBannerMsgQuery request, CancellationToken cancellationToken);
public Task<string> Handle(GetReplacementProcessingTimeQuery request, CancellationToken cancellationToken);
public Task<IEnumerable<MinistryResponse>> Handle(GetMinistryQuery request, CancellationToken cancellationToken);

public Task<string?> Handle(GetBannerMsgQuery request, CancellationToken cancellationToken);

public Task<string?> Handle(GetReplacementProcessingTimeQuery request, CancellationToken cancellationToken);

public Task<IEnumerable<MinistryResponse>> Handle(GetMinistryQuery request, CancellationToken cancellationToken);
}

public record GetBannerMsgQuery : IRequest<string>;
Expand Down Expand Up @@ -85,5 +88,4 @@ public record MinistryResponse
public bool IsActive { get; set; }
public IEnumerable<ServiceTypeCode> ServiceTypeCodes { get; set; }
}

}
}
1 change: 0 additions & 1 deletion src/Spd.Manager.Licence/Spd.Manager.Licence.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<ProjectReference Include="..\Spd.Manager.Shared\Spd.Manager.Shared.csproj" />
<ProjectReference Include="..\Spd.Resource.Repository\Spd.Resource.Repository.csproj" />
<ProjectReference Include="..\Spd.Utilities.BCeIDWS\Spd.Utilities.BCeIDWS.csproj" />
<ProjectReference Include="..\Spd.Utilities.Cache\Spd.Utilities.Cache.csproj" />
<ProjectReference Include="..\Spd.Utilities.FileStorage\Spd.Utilities.FileStorage.csproj" />
<ProjectReference Include="..\Spd.Utilities.LogonUser\Spd.Utilities.LogonUser.csproj" />
</ItemGroup>
Expand Down
25 changes: 11 additions & 14 deletions src/Spd.Manager.Payment/PaymentManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Net;
using System.Text.Json;
using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Polly;
using Polly.Retry;
using Spd.Manager.Shared;
Expand All @@ -18,12 +19,10 @@
using Spd.Resource.Repository.Payment;
using Spd.Resource.Repository.PersonLicApplication;
using Spd.Resource.Repository.ServiceTypes;
using Spd.Utilities.Cache;
using Spd.Utilities.FileStorage;
using Spd.Utilities.Payment;
using Spd.Utilities.Shared;
using Spd.Utilities.Shared.Exceptions;
using System.Net;

namespace Spd.Manager.Payment
{
Expand Down Expand Up @@ -156,7 +155,7 @@ public async Task<PaymentLinkResponse> Handle(PaymentLinkCreateCommand command,
SpdPaymentConfig spdPaymentConfig = await GetSpdPaymentInfoAsync(app, ct);
Guid transNumber = Guid.NewGuid();

//generate the link string
//generate the link string
//payment utility
var linkResult = (CreateDirectPaymentLinkResult)await _paymentService.HandleCommand(
new CreateDirectPaymentLinkCommand
Expand Down Expand Up @@ -339,12 +338,12 @@ public async Task<UpdateInvoicesFromCasResponse> Handle(UpdateInvoicesFromCasCom

private async Task<SpdPaymentConfig> GetSpdPaymentInfoAsync(ApplicationResult app, CancellationToken ct)
{
ConfigResult? configResult = await _cache.Get<ConfigResult>("spdPayBCConfigs");
if (configResult == null)
{
configResult = await _configRepository.Query(new ConfigQuery(null, IConfigRepository.PAYBC_GROUP), ct);
await _cache.Set<ConfigResult>("spdPayBCConfigs", configResult, new TimeSpan(1, 0, 0));
}
ConfigResult? configResult = await _cache.GetAsync(
"spdPayBCConfigs",
async ct => await _configRepository.Query(new ConfigQuery(Group: IConfigRepository.PAYBC_GROUP), ct),
TimeSpan.FromMinutes(60),
ct);

if (IApplicationRepository.ScreeningServiceTypes.Contains((ServiceTypeEnum)app.ServiceType))
{
//screening price and payment setting
Expand Down Expand Up @@ -454,16 +453,14 @@ private string CutOffResponse(string response)
{
try
{
var result = JsonConvert.DeserializeObject<CasInvoiceCreateRespCompact>(response);
return JsonConvert.SerializeObject(result);
var result = JsonSerializer.Deserialize<CasInvoiceCreateRespCompact>(response);
return JsonSerializer.Serialize(result);
}
catch
{
return response;
}
}


}

internal class CasInvoiceCreateRespCompact
Expand Down
9 changes: 7 additions & 2 deletions src/Spd.Manager.Screening/OrgContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ namespace Spd.Manager.Screening
public interface IOrgManager
{
public Task<OrgResponse> Handle(OrgUpdateCommand request, CancellationToken cancellationToken);
public Task<OrgResponse> Handle(OrgGetQuery request, CancellationToken cancellationToken);

public Task<OrgResponse?> Handle(OrgGetQuery request, CancellationToken cancellationToken);

public Task<OrgInvitationLinkResponse?> Handle(OrgInvitationLinkCreateCommand cmd, CancellationToken cancellationToken);

public Task<OrgInviteVerifyResponse?> Handle(OrgInvitationLinkVerifyCommand cmd, CancellationToken cancellationToken);
}

Expand Down Expand Up @@ -44,6 +47,7 @@ public record OrgResponse : OrgInfo
public VolunteerOrganizationTypeCode? VolunteerOrganizationTypeCode { get; set; }
public IEnumerable<ServiceTypeCode> ServiceTypes { get; set; } = Array.Empty<ServiceTypeCode>();
}

public class OrgUpdateRequestValidator : AbstractValidator<OrgUpdateRequest>
{
public OrgUpdateRequestValidator()
Expand Down Expand Up @@ -91,6 +95,7 @@ public OrgUpdateRequestValidator()
.MaximumLength(100);
}
}

public record OrgInvitationLinkResponse(string OrgInvitationLinkUrl);
public record OrgInviteVerifyResponse(Guid? OrgId, bool LinkIsValid = false, string? ErrMsg = null);
}
}
32 changes: 12 additions & 20 deletions src/Spd.Manager.Screening/OrgManager.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using AutoMapper;
using System.Net;
using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Caching.Distributed;
using Spd.Resource.Repository.Application;
using Spd.Resource.Repository.Org;
using Spd.Utilities.Cache;
using Spd.Utilities.Shared;
using Spd.Utilities.Shared.Exceptions;
using System.Net;

namespace Spd.Manager.Screening
{
internal class OrgManager :
IRequestHandler<OrgUpdateCommand, OrgResponse>,
IRequestHandler<OrgGetQuery, OrgResponse>,
IRequestHandler<OrgGetQuery, OrgResponse?>,
IRequestHandler<OrgInvitationLinkCreateCommand, OrgInvitationLinkResponse>,
IRequestHandler<OrgInvitationLinkVerifyCommand, OrgInviteVerifyResponse>,
IRequestHandler<OrgInvitationLinkVerifyCommand, OrgInviteVerifyResponse?>,
IOrgManager
{
private readonly IOrgRepository _orgRepository;
Expand All @@ -39,20 +38,13 @@ public async Task<OrgResponse> Handle(OrgUpdateCommand request, CancellationToke
}

public async Task<OrgResponse?> Handle(OrgGetQuery request, CancellationToken cancellationToken)
{
OrgResponse response;
if (request.AccessCode != null)
{
response = await _cache.Get<OrgResponse>($"org-response-{request.AccessCode}");
if (response != null) return response;
}
var result = (OrgQryResult)await _orgRepository.QueryOrgAsync(new OrgByIdentifierQry(request.OrgId, request.AccessCode), cancellationToken);
if (result == null) return null;

response = _mapper.Map<OrgResponse>(result.OrgResult);
await _cache.Set<OrgResponse>($"org-response-{response.AccessCode}", response, new TimeSpan(0, 30, 0));
return response;
}
=> await _cache.GetAsync($"org-response-{request.AccessCode}", async ct =>
{
var result = (OrgQryResult)await _orgRepository.QueryOrgAsync(new OrgByIdentifierQry(request.OrgId, request.AccessCode), ct);
return _mapper.Map<OrgResponse>(result.OrgResult);
},
TimeSpan.FromMinutes(30),
cancellationToken);

public async Task<OrgInvitationLinkResponse?> Handle(OrgInvitationLinkCreateCommand cmd, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -94,4 +86,4 @@ public async Task<OrgResponse> Handle(OrgUpdateCommand request, CancellationToke
return new OrgInviteVerifyResponse(orgId, true);
}
}
}
}
2 changes: 0 additions & 2 deletions src/Spd.Presentation.Dynamics/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Newtonsoft.Json;
using Spd.Presentation.Dynamics.Swagger;
using Spd.Utilities.BCeIDWS;
using Spd.Utilities.Dynamics;
Expand Down Expand Up @@ -95,7 +94,6 @@

// Initialize slow dependencies
app.Services.GetRequiredService<AutoMapper.IConfigurationProvider>().CompileMappings();
JsonConvert.SerializeObject(new { property = 1 });

if (app.Environment.IsDevelopment())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</ItemGroup>

<ItemGroup>
<None Include="..\..\.editorconfig" Link=".editorconfig" />
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 61d34bc

Please sign in to comment.