Skip to content

Commit

Permalink
[SPDBT-3209] fixed Repository integration tests (#1615)
Browse files Browse the repository at this point in the history
# Description

This PR includes the following proposed change(s):
  • Loading branch information
esdd1995 authored Oct 26, 2024
1 parent 5d432e8 commit 60a51ea
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,22 @@ public async Task QueryAsync_WithNoFilteringCriteria_Throw_Exception()
public async Task CreateAddressesAsync_Run_Correctly()
{
// Arrange
Guid bizId = Guid.NewGuid();
CreateBizCmd createBizCmd = new()
{
BizGuid = Guid.NewGuid(),
Id = bizId,
BizLegalName = IntegrationTestSetup.DataPrefix + "test",
BizType = BizTypeEnum.Corporation,
ServiceTypes = new List<ServiceTypeEnum>() { ServiceTypeEnum.MDRA }
};

await _bizRepository.ManageBizAsync(createBizCmd, CancellationToken.None);
BizResult bizResult = await _bizRepository.ManageBizAsync(createBizCmd, CancellationToken.None);

BranchAddr addressToCreate = fixture.Build<BranchAddr>()
.With(a => a.BranchId, Guid.NewGuid())
.With(a => a.BranchPhoneNumber, "90000000")
.With(a => a.PostalCode, "V7N 5J2")
.Create();
UpsertAddressCmd upsertAddressCmd = new() { BizId = bizId, Addresses = new List<BranchAddr> { addressToCreate } };
UpsertAddressCmd upsertAddressCmd = new() { BizId = bizResult.Id, Addresses = new List<BranchAddr> { addressToCreate } };

// Act
await _addressRepository.CreateAddressesAsync(upsertAddressCmd, CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,24 @@ public async Task ManageBizContactsAsync_WithNoExistingContacts_Correctly()
account biz = await CreateAccountAsync();
await _context.SaveChangesAsync(CancellationToken.None);

BizContactUpsertCmd cmd = new((Guid)biz.accountid, new List<BizContactResp>());
BizContactCreateCmd createCmd = new BizContactCreateCmd(new()
{
BizId = (Guid)biz.accountid,
GivenName = "newFirstName3",
EmailAddress = "[email protected]",
BizContactRoleCode = BizContactRoleEnum.ControllingMember
});

try
{
// Action
var response = await _bizContactRepo.ManageBizContactsAsync(cmd, CancellationToken.None);
var response = await _bizContactRepo.ManageBizContactsAsync(createCmd, CancellationToken.None);

// Assert
var contact = await _context.spd_businesscontacts
.Where(c => c._spd_organizationid_value == biz.accountid)
.FirstOrDefaultAsync(CancellationToken.None);
Assert.Equal(null, contact);
Assert.Equal(response, contact.spd_businesscontactid);
}
finally
{
Expand Down Expand Up @@ -205,38 +211,55 @@ public async Task ManageBizContactsAsync_WithExistingContacts_Correctly()
public async Task ManageBizContactsAsync_WithExistingBizContacts_Correctly()
{
// Arrange

account biz = await CreateAccountAsync();
await _context.SaveChangesAsync(CancellationToken.None);
spd_businesscontact bizContact = await CreateBizContactAsync(biz, "firstName1", BizContactRoleOptionSet.ControllingMember);
spd_businesscontact bizContact2 = await CreateBizContactAsync(biz, "firstName2", BizContactRoleOptionSet.ControllingMember);
await _context.SaveChangesAsync(CancellationToken.None);

Guid? bizContactId1 = bizContact.spd_businesscontactid;
Guid? bizContactId2 = bizContact2.spd_businesscontactid;
Guid? bizContactId3 = null;

try
{
List<BizContactResp> requests = new()
BizContactUpdateCmd updateCmd = new BizContactUpdateCmd((Guid)bizContact.spd_businesscontactid, new()
{
new BizContactResp{ BizContactId = bizContact.spd_businesscontactid, GivenName = "newFirstName1", EmailAddress="[email protected]", BizContactRoleCode=BizContactRoleEnum.ControllingMember},
new BizContactResp{ GivenName = "newFirstName3", EmailAddress="[email protected]", BizContactRoleCode=BizContactRoleEnum.ControllingMember},
};

BizContactUpsertCmd cmd = new((Guid)biz.accountid, requests);

// Action
var response = await _bizContactRepo.ManageBizContactsAsync(cmd, CancellationToken.None);

GivenName = "newFirstName1",
EmailAddress = "[email protected]",
BizContactRoleCode = BizContactRoleEnum.ControllingMember
});
BizContactCreateCmd createCmd = new BizContactCreateCmd(new()
{
BizId = (Guid) biz.accountid,
GivenName = "newFirstName3",
EmailAddress = "[email protected]",
BizContactRoleCode = BizContactRoleEnum.ControllingMember
});
// Action
var response = await _bizContactRepo.ManageBizContactsAsync(updateCmd, CancellationToken.None);
var response2 = await _bizContactRepo.ManageBizContactsAsync(createCmd, CancellationToken.None);
bizContactId3 = response2;
// Assert
var bizContacts = _context.spd_businesscontacts
.Where(c => c._spd_organizationid_value == biz.accountid)
.Where(c => c.statecode == DynamicsConstants.StateCode_Active)
.ToList();
Assert.Equal(2, bizContacts.Count());
Assert.Equal(3, bizContacts.Count());
Assert.Equal(true, bizContacts.Any(c => c.spd_firstname == "newFirstName1")); //updated
Assert.Equal(true, bizContacts.Any(c => c.spd_firstname == "newFirstName3")); //created
}
finally
{
//Annihilate
_context.DeleteObject(bizContact);
_context.DeleteObject(bizContact2);
spd_businesscontact bizContact1ToRemove = _context.spd_businesscontacts.Where(b => b.spd_businesscontactid == bizContactId1).FirstOrDefault();
spd_businesscontact bizContact2ToRemove = _context.spd_businesscontacts.Where(b => b.spd_businesscontactid == bizContactId2).FirstOrDefault();
spd_businesscontact bizContact3ToRemove = _context.spd_businesscontacts.Where(b => b.spd_businesscontactid == bizContactId3).FirstOrDefault();
_context.DeleteObject(bizContact1ToRemove);
_context.DeleteObject(bizContact2ToRemove);
_context.DeleteObject(bizContact3ToRemove);
await _context.SaveChangesAsync(CancellationToken.None);
_context.DeleteObject(biz);
await _context.SaveChangesAsync(CancellationToken.None);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,6 @@ public async Task SaveBizLicApplicationAsync_AddNewPrivateInvestigatorWithExisti
await _context.SaveChangesAsync();
}

//TODO: Adjust verification according to ticket SPDBT-2796
[Fact]
public async Task SaveBizLicApplicationAsync_UpdateExistingPrivateInvestigator_Run_Correctly()
{
Expand Down
35 changes: 13 additions & 22 deletions src/Spd.Resource.Repository.IntegrationTest/BizRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ public BizRepositoryTest(IntegrationTestSetup testSetup)
public async Task GetBizAsync_Run_Correctly()
{
// Arrange
Guid bizId = Guid.NewGuid();
CreateBizCmd cmd = new()
{
BizGuid = Guid.NewGuid(),
Id = bizId,
BizLegalName = IntegrationTestSetup.DataPrefix + "test",
BizType = BizTypeEnum.Corporation,
ServiceTypes = new List<ServiceTypeEnum>() { ServiceTypeEnum.MDRA }
};

// Act
await _bizRepository.ManageBizAsync(cmd, CancellationToken.None);
var result = await _bizRepository.GetBizAsync(bizId, CancellationToken.None);
var biz = await _bizRepository.ManageBizAsync(cmd, CancellationToken.None);
var result = await _bizRepository.GetBizAsync(biz.Id, CancellationToken.None);

Assert.NotNull(result);
Assert.IsType<BizResult>(result);
Expand Down Expand Up @@ -77,22 +75,20 @@ public async Task GetBizAsync_BizWithoutServiceType_Throw_Exception()
public async Task CreateBizAsync_Run_Correctly()
{
// Arrange
Guid bizId = Guid.NewGuid();
CreateBizCmd cmd = new()
{
BizGuid = Guid.NewGuid(),
Id = bizId,
BizLegalName = IntegrationTestSetup.DataPrefix + "test",
BizType = BizTypeEnum.Corporation,
ServiceTypes = new List<ServiceTypeEnum>() { ServiceTypeEnum.MDRA }
};

// Act
await _bizRepository.ManageBizAsync(cmd, CancellationToken.None);
var biz = await _bizRepository.ManageBizAsync(cmd, CancellationToken.None);

// Assert
account? account = await _context.accounts.Expand(a => a.spd_account_spd_servicetype)
.Where(c => c.accountid == bizId).FirstOrDefaultAsync();
.Where(c => c.accountid == biz.Id).FirstOrDefaultAsync();
Guid? serviceTypeId = DynamicsContextLookupHelpers.GetServiceTypeGuid(cmd.ServiceTypes.FirstOrDefault().ToString());
spd_servicetype? serviceType = account.spd_account_spd_servicetype.Where(s => s.spd_servicetypeid == serviceTypeId).FirstOrDefault();

Expand All @@ -112,22 +108,20 @@ public async Task CreateBizAsync_Run_Correctly()
public async Task AddBizServiceTypeAsync_Run_Correctly()
{
// Arrange
Guid bizId = Guid.NewGuid();
CreateBizCmd createCmd = new()
{
BizGuid = Guid.NewGuid(),
Id = bizId,
BizLegalName = IntegrationTestSetup.DataPrefix + "test"
};
var biz = await _bizRepository.ManageBizAsync(createCmd, CancellationToken.None);
AddBizServiceTypeCmd cmd = new(bizId, ServiceTypeEnum.SecurityBusinessLicence);
AddBizServiceTypeCmd cmd = new(biz.Id, ServiceTypeEnum.SecurityBusinessLicence);

// Act
await _bizRepository.ManageBizAsync(cmd, CancellationToken.None);

// Assert
account? account = await _context.accounts.Expand(a => a.spd_account_spd_servicetype)
.Where(c => c.accountid == bizId).FirstOrDefaultAsync();
.Where(c => c.accountid == biz.Id).FirstOrDefaultAsync();
Guid? serviceTypeId = DynamicsContextLookupHelpers.GetServiceTypeGuid(cmd.ServiceTypeEnum.ToString());
spd_servicetype? serviceType = account.spd_account_spd_servicetype.Where(s => s.spd_servicetypeid == serviceTypeId).FirstOrDefault();

Expand Down Expand Up @@ -287,10 +281,8 @@ public async void UpdateBizAsync_BizTypeChangeFromSoleProprietorToNonSoleProprie
.With(c => c.PhoneNumber, "90000000")
.Create();

Guid bizId = Guid.NewGuid();

CreateBizCmd createCmd = fixture.Build<CreateBizCmd>()
.With(c => c.Id, bizId)
.With(c => c.BizLegalName, IntegrationTestSetup.DataPrefix + "test")
.With(c => c.ServiceTypes, new List<ServiceTypeEnum>() { ServiceTypeEnum.MCFD })
.With(c => c.BranchAddresses, new List<BranchAddr>() { branchAddress })
Expand All @@ -301,9 +293,11 @@ public async void UpdateBizAsync_BizTypeChangeFromSoleProprietorToNonSoleProprie
.With(c => c.MailingAddress, address)
.With(c => c.BizManagerContactInfo, bizManagerContactInfo)
.Create();

var biz = await _bizRepository.ManageBizAsync(createCmd, CancellationToken.None);

UpdateBizCmd updateCmd = fixture.Build<UpdateBizCmd>()
.With(c => c.Id, bizId)
.With(c => c.Id, biz.Id)
.With(c => c.BizLegalName, IntegrationTestSetup.DataPrefix + "updated test")
.With(c => c.ServiceTypes, new List<ServiceTypeEnum>() { ServiceTypeEnum.MDRA })
.With(c => c.BranchAddresses, new List<BranchAddr>() { branchAddress })
Expand All @@ -318,13 +312,12 @@ public async void UpdateBizAsync_BizTypeChangeFromSoleProprietorToNonSoleProprie
.Create();

// Act
await _bizRepository.ManageBizAsync(createCmd, CancellationToken.None);
await _bizRepository.ManageBizAsync(updateCmd, CancellationToken.None);

account? account = await _context.accounts
.Expand(a => a.spd_account_spd_servicetype)
.Expand(a => a.spd_organization_spd_licence_soleproprietor)
.Where(c => c.accountid == bizId).FirstOrDefaultAsync();
.Where(c => c.accountid == biz.Id).FirstOrDefaultAsync();

// Assert
Assert.NotNull(account);
Expand Down Expand Up @@ -479,10 +472,8 @@ public async void UpdateBizServiceTypeAsync_Run_Correctly()
.With(c => c.PhoneNumber, "80000000")
.Create();

Guid bizId = Guid.NewGuid();

CreateBizCmd createCmd = fixture.Build<CreateBizCmd>()
.With(c => c.Id, bizId)
.With(c => c.BizLegalName, IntegrationTestSetup.DataPrefix + "test")
.With(c => c.ServiceTypes, new List<ServiceTypeEnum>() { ServiceTypeEnum.MCFD })
.With(c => c.BranchAddresses, new List<BranchAddr>() { branchAddress })
Expand All @@ -493,15 +484,15 @@ public async void UpdateBizServiceTypeAsync_Run_Correctly()
.With(c => c.BizManagerContactInfo, bizManagerContactInfo)
.Create();

UpdateBizServiceTypeCmd updateServiceTypeCmd = new(bizId, ServiceTypeEnum.PSSO);
var biz = await _bizRepository.ManageBizAsync(createCmd, CancellationToken.None);
UpdateBizServiceTypeCmd updateServiceTypeCmd = new(biz.Id, ServiceTypeEnum.PSSO);

// Act
await _bizRepository.ManageBizAsync(createCmd, CancellationToken.None);
await _bizRepository.ManageBizAsync(updateServiceTypeCmd, CancellationToken.None);

// Assert
account? account = await _context.accounts.Expand(a => a.spd_account_spd_servicetype)
.Where(c => c.accountid == bizId).FirstOrDefaultAsync();
.Where(c => c.accountid == biz.Id).FirstOrDefaultAsync();
Guid? serviceTypeId = DynamicsContextLookupHelpers.GetServiceTypeGuid(updateServiceTypeCmd.ServiceTypeEnum.ToString());
spd_servicetype? serviceType = account.spd_account_spd_servicetype.Where(s => s.spd_servicetypeid == serviceTypeId).FirstOrDefault();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,29 @@ public async Task MergeContacts_Run_Correctly()
}
} */

[Theory]
[InlineData("spd_integration_firstname", "spd_integration_lastname", null, null, "2024-01-01", null, 2)]
[InlineData("spd_integration_firstname", "spd_integration_lastname", "", null, "2024-01-01", null, 2)]
[InlineData("spd_integration_firstname", "spd_integration_lastname", "", "", "2024-01-01", null, 1)]
[InlineData("spd_integration_firstname", "spd_integration_lastname3", "", "", "2023-01-01", null, 0)]
[InlineData("spd_integration_firstname", "spd_integration_lastname", "", null, "2024-01-01", "EmptyGuid", 1)]
public async Task QueryAsync_Run_Correctly(string? firstName, string? lastName, string? midName1, string? midName2, string? birthDate, string? identityIdStr, int resultCount)
[Fact]
public async Task QueryAsync_Run_Correctly()
{
//Arrange
Guid contact1Id = Guid.NewGuid();
contact contact1 = new()
{
contactid = contact1Id,
firstname = IntegrationTestSetup.DataPrefix + "firstname",
lastname = IntegrationTestSetup.DataPrefix + "lastname",
firstname = IntegrationTestSetup.DataPrefix + "firstname_test",
lastname = IntegrationTestSetup.DataPrefix + "lastname_test",
spd_middlename1 = null,
spd_middlename2 = "test",
spd_middlename2 = IntegrationTestSetup.DataPrefix + "test",
birthdate = new Microsoft.OData.Edm.Date(2024, 1, 1)
};
_context.AddTocontacts(contact1);
Guid contact2Id = Guid.NewGuid();
contact contact2 = new()
{
contactid = contact2Id,
firstname = IntegrationTestSetup.DataPrefix + "firstname",
lastname = IntegrationTestSetup.DataPrefix + "lastname",
firstname = IntegrationTestSetup.DataPrefix + "firstname_test1",
lastname = IntegrationTestSetup.DataPrefix + "lastname_test2",
spd_middlename1 = "",
spd_middlename2 = null,
spd_middlename2 = IntegrationTestSetup.DataPrefix + "test",
birthdate = new Microsoft.OData.Edm.Date(2024, 1, 1)
};
_context.AddTocontacts(contact2);
Expand All @@ -96,19 +91,22 @@ public async Task QueryAsync_Run_Correctly(string? firstName, string? lastName,
//Act
ContactQry qry = new()
{
FirstName = firstName,
LastName = lastName,
MiddleName1 = midName1,
MiddleName2 = midName2,
FirstName = IntegrationTestSetup.DataPrefix + "firstname_test",
LastName = IntegrationTestSetup.DataPrefix + "lastname_test",
BirthDate = new DateOnly(2024, 1, 1),
IdentityId = identityIdStr == "EmptyGuid" ? Guid.Empty : null
};
ContactListResp result = await _contactRepository.QueryAsync(qry, CancellationToken.None);

try
{
//Assert
Assert.Equal(resultCount, result.Items.Count());
Assert.Equal(1, result.Items.Count());
qry = new()
{
MiddleName2 = IntegrationTestSetup.DataPrefix + "test",
};
result = await _contactRepository.QueryAsync(qry, CancellationToken.None);
Assert.Equal(2, result.Items.Count());
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public async Task<LicenceApplicationResp> GetLicenceApplicationAsync(Guid licenc
.Expand(a => a.spd_CurrentExpiredLicenceId)
.Where(a => a.spd_applicationid == licenceApplicationId)
.SingleOrDefaultAsync(ct);
if (app == null)
throw new ArgumentException("application not found");
}
catch (DataServiceQueryException ex)
{
Expand All @@ -143,7 +145,6 @@ public async Task<LicenceApplicationResp> GetLicenceApplicationAsync(Guid licenc
}

LicenceApplicationResp appResp = _mapper.Map<LicenceApplicationResp>(app);

if (app.spd_ApplicantId_contact?.contactid != null)
{
var aliases = SharedRepositoryFuncs.GetAliases((Guid)app.spd_ApplicantId_contact.contactid, _context);
Expand Down

0 comments on commit 60a51ea

Please sign in to comment.