Skip to content

Commit

Permalink
Implement biz lic app update service (#1161)
Browse files Browse the repository at this point in the history
Implementation business licence application update service and minor
refactoring. The modifications include:

- Implementation of business licence application update service
- Modification of task repository to support linking between account and
task
- Add integrated test for task repository
- Add missing validation and members update in renew service
- Plug request validation in change endpoint
- Suppression of failed tests related to the problem described in
SPDBT-2716
- General refactoring
  • Loading branch information
ruaraya authored Jun 22, 2024
2 parents 54129a8 + abf12e0 commit 4659063
Show file tree
Hide file tree
Showing 14 changed files with 506 additions and 49 deletions.
218 changes: 211 additions & 7 deletions src/Spd.Manager.Licence.UnitTest/BizLicenceAppManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Spd.Resource.Repository.LicApp;
using Spd.Resource.Repository.Licence;
using Spd.Resource.Repository.LicenceFee;
using Spd.Resource.Repository.PersonLicApplication;
using Spd.Resource.Repository.Tasks;
using Spd.Utilities.FileStorage;
using Spd.Utilities.Shared.Exceptions;

Expand All @@ -23,6 +25,7 @@ public class BizLicenceAppManagerTest
private Mock<ITransientFileStorageService> mockTransientFileStorageService = new();
private Mock<IBizLicApplicationRepository> mockBizLicAppRepo = new();
private Mock<IBizContactRepository> mockBizContactRepo = new();
private Mock<ITaskRepository> mockTaskRepo = new();
private BizLicAppManager sut;

public BizLicenceAppManagerTest()
Expand All @@ -47,7 +50,8 @@ public BizLicenceAppManagerTest()
mockMainFileService.Object,
mockTransientFileStorageService.Object,
mockBizContactRepo.Object,
mockBizLicAppRepo.Object);
mockBizLicAppRepo.Object,
mockTaskRepo.Object);
}

[Fact]
Expand Down Expand Up @@ -252,6 +256,8 @@ public async void Handle_BizLicAppRenewCommand_Return_BizLicAppCommandResponse()
{
Items = new List<LicenceResp> { originalLicence }
});
mockBizLicAppRepo.Setup(a => a.GetBizLicApplicationAsync(It.Is<Guid>(m => m == originalApplicationId), CancellationToken.None))
.ReturnsAsync(new BizLicApplicationResp() { LicenceAppId = originalApplicationId, BizId = bizId });
mockBizLicAppRepo.Setup(a => a.CreateBizLicApplicationAsync(It.Is<CreateBizLicApplicationCmd>(
m => m.OriginalApplicationId == originalApplicationId &&
m.OriginalLicenceId == originalLicenceId), CancellationToken.None))
Expand All @@ -261,7 +267,7 @@ public async void Handle_BizLicAppRenewCommand_Return_BizLicAppCommandResponse()

BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = Shared.ApplicationTypeCode.Renewal,
ApplicationTypeCode = ApplicationTypeCode.Renewal,
OriginalLicenceId = originalLicenceId,
OriginalApplicationId = originalApplicationId,
NoBranding = false,
Expand Down Expand Up @@ -297,7 +303,7 @@ public async void Handle_BizLicAppRenewCommand_WithWrongApplicationType_Throw_Ex
// Arrange
BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = Shared.ApplicationTypeCode.New
ApplicationTypeCode = ApplicationTypeCode.New
};
BizLicAppRenewCommand cmd = new(request, new List<LicAppFileInfo>());

Expand All @@ -320,7 +326,7 @@ public async void Handle_BizLicAppRenewCommand_WithoutOriginalLicence_Throw_Exce

BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = Shared.ApplicationTypeCode.Renewal
ApplicationTypeCode = ApplicationTypeCode.Renewal
};
BizLicAppRenewCommand cmd = new(request, new List<LicAppFileInfo>());

Expand Down Expand Up @@ -348,7 +354,36 @@ public async void Handle_BizLicAppRenewCommand_WithInvalidExpirationDate_Throw_E

BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = Shared.ApplicationTypeCode.Renewal
ApplicationTypeCode = ApplicationTypeCode.Renewal
};
BizLicAppRenewCommand cmd = new(request, new List<LicAppFileInfo>());

// Action
Func<Task> act = () => sut.Handle(cmd, CancellationToken.None);

// Assert
await Assert.ThrowsAsync<ArgumentException>(act);
}

[Fact]
public async void Handle_BizLicAppRenewCommand_WithoutLinkedBusiness_Throw_Exception()
{
// Arrange
DateTime dateTime = DateTime.UtcNow.AddDays(Constants.LicenceWith123YearsRenewValidBeforeExpirationInDays);
DateOnly expiryDate = new(dateTime.Year, dateTime.Month, dateTime.Day);
LicenceResp originalLicence = new() { LicenceAppId = Guid.NewGuid(), ExpiryDate = expiryDate };
mockLicRepo.Setup(a => a.QueryAsync(It.IsAny<LicenceQry>(), CancellationToken.None))
.ReturnsAsync(new LicenceListResp()
{
Items = new List<LicenceResp>() { originalLicence }
});
mockBizLicAppRepo.Setup(a => a.GetBizLicApplicationAsync(It.IsAny<Guid>(), CancellationToken.None))
.ReturnsAsync(new BizLicApplicationResp());

BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = ApplicationTypeCode.Renewal,
OriginalApplicationId = Guid.NewGuid(),
};
BizLicAppRenewCommand cmd = new(request, new List<LicAppFileInfo>());

Expand All @@ -374,10 +409,13 @@ public async void Handle_BizLicAppRenewCommand_WithMissingFiles_Throw_Exception(
{
Items = new List<LicenceResp> { originalLicence }
});

mockBizLicAppRepo.Setup(a => a.GetBizLicApplicationAsync(It.IsAny<Guid>(), CancellationToken.None))
.ReturnsAsync(new BizLicApplicationResp() { LicenceAppId = Guid.NewGuid(), BizId = Guid.NewGuid() });

BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = Shared.ApplicationTypeCode.Renewal,
ApplicationTypeCode = ApplicationTypeCode.Renewal,
OriginalApplicationId = Guid.NewGuid(),
NoBranding = true,
UseDogs = false
};
Expand All @@ -391,6 +429,172 @@ public async void Handle_BizLicAppRenewCommand_WithMissingFiles_Throw_Exception(
await Assert.ThrowsAsync<ApiException>(act);
}

[Fact]
public async void Handle_BizLicAppUpdateCommand_CreateNewApplication_Return_BizLicAppCommandResponse()
{
// Arrange
Guid originalApplicationId = Guid.NewGuid();
Guid originalLicenceId = Guid.NewGuid();
Guid newLicAppId = Guid.NewGuid();
Guid bizId = Guid.NewGuid();
LicenceResp originalLicence = fixture.Build<LicenceResp>()
.With(r => r.LicenceAppId, originalApplicationId)
.With(r => r.LicenceId, originalLicenceId)
.Create();
LicenceFeeResp licenceFeeResp = new() { Amount = 100 };
mockLicRepo.Setup(a => a.QueryAsync(It.Is<LicenceQry>(q => q.LicenceId == originalLicenceId), CancellationToken.None))
.ReturnsAsync(new LicenceListResp()
{
Items = new List<LicenceResp> { originalLicence }
});
mockBizLicAppRepo.Setup(a => a.GetBizLicApplicationAsync(It.Is<Guid>(m => m == originalApplicationId), CancellationToken.None))
.ReturnsAsync(new BizLicApplicationResp() { LicenceAppId = originalApplicationId, BizId = bizId });
mockBizLicAppRepo.Setup(a => a.CreateBizLicApplicationAsync(It.Is<CreateBizLicApplicationCmd>(
m => m.OriginalApplicationId == originalApplicationId &&
m.OriginalLicenceId == originalLicenceId), CancellationToken.None))
.ReturnsAsync(new BizLicApplicationCmdResp(newLicAppId, bizId));
mockLicFeeRepo.Setup(m => m.QueryAsync(It.IsAny<LicenceFeeQry>(), CancellationToken.None))
.ReturnsAsync(new LicenceFeeListResp() { LicenceFees = new List<LicenceFeeResp> { licenceFeeResp } });

BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = ApplicationTypeCode.Update,
OriginalLicenceId = originalLicenceId,
OriginalApplicationId = originalApplicationId,
NoBranding = false,
UseDogs = true,
Reprint = true,
CategoryCodes = new List<WorkerCategoryTypeCode>() { WorkerCategoryTypeCode.ArmouredCarGuard }
};
BizLicAppUpdateCommand cmd = new(request, new List<LicAppFileInfo>());

// Action
var result = await sut.Handle(cmd, CancellationToken.None);

// Assert
Assert.IsType<BizLicAppCommandResponse>(result);
Assert.Equal(newLicAppId, result.LicenceAppId);
Assert.Equal(licenceFeeResp.Amount, result.Cost);
}

[Fact]
public async void Handle_BizLicAppUpdateCommand_UpdateApplication_Return_BizLicAppCommandResponse()
{
// Arrange
Guid originalApplicationId = Guid.NewGuid();
Guid originalLicenceId = Guid.NewGuid();
Guid bizId = Guid.NewGuid();
LicenceResp originalLicence = fixture.Build<LicenceResp>()
.With(r => r.LicenceAppId, originalApplicationId)
.With(r => r.LicenceId, originalLicenceId)
.Create();

mockLicRepo.Setup(a => a.QueryAsync(It.Is<LicenceQry>(q => q.LicenceId == originalLicenceId), CancellationToken.None))
.ReturnsAsync(new LicenceListResp()
{
Items = new List<LicenceResp> { originalLicence }
});
mockBizLicAppRepo.Setup(a => a.GetBizLicApplicationAsync(It.Is<Guid>(m => m == originalApplicationId), CancellationToken.None))
.ReturnsAsync(new BizLicApplicationResp()
{
LicenceAppId = originalApplicationId,
BizId = bizId ,
UseDogs = true,
CategoryCodes = new List<WorkerCategoryTypeEnum>() { WorkerCategoryTypeEnum.ArmouredCarGuard }
});
mockBizLicAppRepo.Setup(a => a.SaveBizLicApplicationAsync(It.Is<SaveBizLicApplicationCmd>(
m => m.LicenceAppId == originalApplicationId &&
m.ApplicantId == bizId), CancellationToken.None))
.ReturnsAsync(new BizLicApplicationCmdResp(originalApplicationId, bizId));

BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = ApplicationTypeCode.Update,
OriginalLicenceId = originalLicenceId,
OriginalApplicationId = originalApplicationId,
NoBranding = false,
UseDogs = true,
Reprint = false,
CategoryCodes = new List<WorkerCategoryTypeCode>() { WorkerCategoryTypeCode.ArmouredCarGuard }
};
BizLicAppUpdateCommand cmd = new(request, new List<LicAppFileInfo>());

// Action
var result = await sut.Handle(cmd, CancellationToken.None);

// Assert
Assert.IsType<BizLicAppCommandResponse>(result);
Assert.Equal(originalApplicationId, result.LicenceAppId);
Assert.Equal(0, result.Cost);
}

[Fact]
public async void Handle_BizLicAppUpdateCommand_WithWrongApplicationType_Throw_Exception()
{
// Arrange
BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = ApplicationTypeCode.New
};
BizLicAppUpdateCommand cmd = new(request, new List<LicAppFileInfo>());

// Action
Func<Task> act = () => sut.Handle(cmd, CancellationToken.None);

// Assert
await Assert.ThrowsAsync<ArgumentException>(act);
}

[Fact]
public async void Handle_BizLicAppUpdateCommand_WithoutOriginalLicence_Throw_Exception()
{
// Arrange
mockLicRepo.Setup(a => a.QueryAsync(It.IsAny<LicenceQry>(), CancellationToken.None))
.ReturnsAsync(new LicenceListResp()
{
Items = new List<LicenceResp>()
});

BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = ApplicationTypeCode.Update
};
BizLicAppUpdateCommand cmd = new(request, new List<LicAppFileInfo>());

// Action
Func<Task> act = () => sut.Handle(cmd, CancellationToken.None);

// Assert
await Assert.ThrowsAsync<ArgumentException>(act);
}

[Fact]
public async void Handle_BizLicAppUpdateCommand_WithoutLinkedBusiness_Throw_Exception()
{
// Arrange
LicenceResp originalLicence = new() { LicenceAppId = Guid.NewGuid() };
mockLicRepo.Setup(a => a.QueryAsync(It.IsAny<LicenceQry>(), CancellationToken.None))
.ReturnsAsync(new LicenceListResp()
{
Items = new List<LicenceResp>() { originalLicence }
});
mockBizLicAppRepo.Setup(a => a.GetBizLicApplicationAsync(It.IsAny<Guid>(), CancellationToken.None))
.ReturnsAsync(new BizLicApplicationResp());

BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = ApplicationTypeCode.Update,
OriginalApplicationId = Guid.NewGuid(),
};
BizLicAppUpdateCommand cmd = new(request, new List<LicAppFileInfo>());

// Action
Func<Task> act = () => sut.Handle(cmd, CancellationToken.None);

// Assert
await Assert.ThrowsAsync<ArgumentException>(act);
}

[Fact]
public async void Handle_BrandImageQuery_WithDraftApplication_ShouldGetImageFromTransientStorage()
{
Expand Down
1 change: 1 addition & 0 deletions src/Spd.Manager.Licence/BizLicAppContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public record BizLicAppSubmitRequest : BizLicenceApp
public IEnumerable<Guid>? PreviousDocumentIds { get; set; } //documentUrlId, used for renew
public Guid? OriginalApplicationId { get; set; } //for new, it should be null. for renew, replace, update, it should be original application id.
public Guid? OriginalLicenceId { get; set; } //for new, it should be null. for renew, replace, update, it should be original licence id.
public bool? Reprint { get; set; }
}
public record BizLicAppCommandResponse : LicenceAppUpsertResponse
{
Expand Down
Loading

0 comments on commit 4659063

Please sign in to comment.