Skip to content

Commit 145775b

Browse files
committed
Wiring up of the MockSamDataFactory and associated builders.
1 parent 1d4b093 commit 145775b

10 files changed

Lines changed: 315 additions & 17 deletions

File tree

tests/KeeperData.Core.Tests.Unit/ApiContracts/MockCtsDataFactoryTests.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@ public class MockCtsDataFactoryTests
1111
public void GivenMockCtsDataFactory_WhenCallingCreateMockHolding_ShouldProduceValidHoldingModel()
1212
{
1313
var factory = new MockCtsDataFactory();
14+
var holdingIdentifier = CphGenerator.GenerateFormattedCph();
1415

1516
var result = factory.CreateMockHolding(
1617
changeType: DataBridgeConstants.ChangeTypeInsert,
18+
batchId: 1,
19+
holdingIdentifier: holdingIdentifier,
1720
locType: "AG",
1821
endDate: DateTime.UtcNow.Date);
1922

2023
result.Should().NotBeNull();
24+
2125
result.CHANGE_TYPE.Should().Be(DataBridgeConstants.ChangeTypeInsert);
26+
result.BATCH_ID.Should().Be(1);
27+
2228
result.LID_FULL_IDENTIFIER.Should().NotBeNull();
23-
result.LID_FULL_IDENTIFIER.Length.Should().Be(11);
29+
result.LID_FULL_IDENTIFIER.Should().Be(holdingIdentifier);
30+
2431
result.LTY_LOC_TYPE.Should().Be("AG");
2532
result.LOC_EFFECTIVE_TO.Should().NotBeNull();
2633
}
@@ -33,14 +40,16 @@ public void GivenMockCtsDataFactory_WhenCallingCreateMockAgentOrKeeper_ShouldPro
3340

3441
var result = factory.CreateMockAgentOrKeeper(
3542
changeType: DataBridgeConstants.ChangeTypeInsert,
36-
holdingIdentifier: holdingIdentifier,
3743
batchId: 1,
44+
holdingIdentifier: holdingIdentifier,
3845
endDate: DateTime.UtcNow.Date);
3946

4047
result.Should().NotBeNull();
48+
4149
result.CHANGE_TYPE.Should().Be(DataBridgeConstants.ChangeTypeInsert);
42-
result.LID_FULL_IDENTIFIER.Should().NotBeNull().And.Be(holdingIdentifier);
4350
result.BATCH_ID.Should().Be(1);
51+
52+
result.LID_FULL_IDENTIFIER.Should().NotBeNull().And.Be(holdingIdentifier);
4453
result.LPR_EFFECTIVE_TO_DATE.Should().NotBeNull();
4554
}
4655

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using FluentAssertions;
2+
using KeeperData.Core.ApiClients.DataBridgeApi;
3+
using KeeperData.Tests.Common.Factories;
4+
using KeeperData.Tests.Common.Generators;
5+
6+
namespace KeeperData.Core.Tests.Unit.ApiContracts;
7+
8+
public class MockSamDataFactoryTests
9+
{
10+
11+
}

tests/KeeperData.Tests.Common/Factories/MockCtsDataFactory.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AutoFixture;
22
using KeeperData.Core.ApiClients.DataBridgeApi.Contracts;
3+
using KeeperData.Tests.Common.Generators;
34
using KeeperData.Tests.Common.SpecimenBuilders;
45

56
namespace KeeperData.Tests.Common.Factories;
@@ -16,16 +17,34 @@ public MockCtsDataFactory()
1617
_fixture = new Fixture();
1718
}
1819

19-
public CtsCphHolding CreateMockHolding(string changeType, string? locType = null, DateTime? endDate = null)
20+
public CtsCphHolding CreateMockHolding(
21+
string changeType,
22+
int batchId,
23+
string holdingIdentifier,
24+
string? locType = null,
25+
DateTime? endDate = null)
2026
{
21-
_fixture.Customizations.Add(new CtsCphHoldingBuilder(changeType, locType, endDate));
27+
_fixture.Customizations.Add(new CtsCphHoldingBuilder(
28+
changeType,
29+
batchId,
30+
holdingIdentifier,
31+
locType,
32+
endDate));
2233

2334
return _fixture.Create<CtsCphHolding>();
2435
}
2536

26-
public CtsAgentOrKeeper CreateMockAgentOrKeeper(string changeType, string holdingIdentifier, int batchId = 1, DateTime? endDate = null)
37+
public CtsAgentOrKeeper CreateMockAgentOrKeeper(
38+
string changeType,
39+
int batchId,
40+
string holdingIdentifier,
41+
DateTime? endDate = null)
2742
{
28-
_fixture.Customizations.Add(new CtsAgentOrKeeperBuilder(changeType, holdingIdentifier, batchId, endDate));
43+
_fixture.Customizations.Add(new CtsAgentOrKeeperBuilder(
44+
changeType,
45+
batchId,
46+
holdingIdentifier,
47+
endDate));
2948

3049
return _fixture.Create<CtsAgentOrKeeper>();
3150
}
@@ -36,13 +55,14 @@ public CtsAgentOrKeeper CreateMockAgentOrKeeper(string changeType, string holdin
3655
int agentCount,
3756
int keeperCount)
3857
{
39-
_fixture.Customizations.Add(new CtsCphHoldingBuilder(changeType));
58+
var holdingIdentifier = CphGenerator.GenerateFormattedCph();
59+
var batchId = 1;
4060

41-
var holdings = _fixture.CreateMany<CtsCphHolding>(holdingCount).ToList();
42-
var holdingIdentifier = holdings.First().LID_FULL_IDENTIFIER;
43-
var batchId = holdings.First().BATCH_ID;
61+
_fixture.Customizations.Add(new CtsCphHoldingBuilder(changeType, batchId, holdingIdentifier));
4462

45-
_fixture.Customizations.Add(new CtsAgentOrKeeperBuilder(changeType, holdingIdentifier, batchId));
63+
var holdings = _fixture.CreateMany<CtsCphHolding>(holdingCount).ToList();
64+
65+
_fixture.Customizations.Add(new CtsAgentOrKeeperBuilder(changeType, batchId, holdingIdentifier));
4666

4767
var agents = _fixture.CreateMany<CtsAgentOrKeeper>(agentCount).ToList();
4868
var keepers = _fixture.CreateMany<CtsAgentOrKeeper>(keeperCount).ToList();
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using AutoFixture;
2+
using KeeperData.Core.ApiClients.DataBridgeApi.Contracts;
3+
using KeeperData.Tests.Common.Generators;
4+
using KeeperData.Tests.Common.SpecimenBuilders;
5+
6+
namespace KeeperData.Tests.Common.Factories;
7+
8+
/// <summary>
9+
/// Factory to be used to create mock objects for <see cref="IDataBridgeClient"/> to be used by all tests.
10+
/// </summary>
11+
public class MockSamDataFactory
12+
{
13+
private readonly Fixture _fixture;
14+
15+
public MockSamDataFactory()
16+
{
17+
_fixture = new Fixture();
18+
}
19+
20+
public SamCphHolding CreateMockHolding(
21+
string changeType,
22+
int batchId,
23+
string holdingIdentifier,
24+
DateTime? endDate = null)
25+
{
26+
_fixture.Customizations.Add(new SamCphHoldingBuilder(
27+
changeType,
28+
batchId,
29+
holdingIdentifier,
30+
endDate));
31+
32+
return _fixture.Create<SamCphHolding>();
33+
}
34+
35+
public SamCphHolder CreateMockHolder(
36+
string changeType,
37+
int batchId,
38+
string holdingIdentifier)
39+
{
40+
_fixture.Customizations.Add(new SamCphHolderBuilder(
41+
changeType,
42+
batchId,
43+
holdingIdentifier));
44+
45+
return _fixture.Create<SamCphHolder>();
46+
}
47+
48+
public SamHerd CreateMockHerd(
49+
string changeType,
50+
int batchId,
51+
string holdingIdentifier,
52+
List<string> partyIds)
53+
{
54+
_fixture.Customizations.Add(new SamHerdBuilder(
55+
changeType,
56+
batchId,
57+
holdingIdentifier,
58+
partyIds));
59+
60+
return _fixture.Create<SamHerd>();
61+
}
62+
63+
public SamParty CreateMockParty(
64+
string changeType,
65+
int batchId,
66+
string partyId)
67+
{
68+
_fixture.Customizations.Add(new SamPartyBuilder(
69+
changeType,
70+
batchId,
71+
[partyId]));
72+
73+
return _fixture.Create<SamParty>();
74+
}
75+
76+
public (List<SamCphHolding> holdings, List<SamCphHolder> holders, List<SamHerd> herds, List<SamParty> parties) CreateMockData(
77+
string changeType,
78+
int holdingCount,
79+
int holderCount,
80+
int herdCount,
81+
int partyCount)
82+
{
83+
var holdingIdentifier = CphGenerator.GenerateFormattedCph();
84+
var batchId = 1;
85+
86+
var partyIds = Enumerable.Range(1, partyCount)
87+
.Select(i => $"C{i:D6}")
88+
.ToList();
89+
90+
_fixture.Customizations.Add(new SamCphHoldingBuilder(changeType, batchId, holdingIdentifier));
91+
92+
var holdings = _fixture.CreateMany<SamCphHolding>(holdingCount).ToList();
93+
94+
_fixture.Customizations.Add(new SamCphHolderBuilder(changeType, batchId, holdingIdentifier));
95+
96+
var holders = _fixture.CreateMany<SamCphHolder>(holderCount).ToList();
97+
98+
_fixture.Customizations.Add(new SamHerdBuilder(changeType, batchId, holdingIdentifier, partyIds));
99+
100+
var herds = _fixture.CreateMany<SamHerd>(herdCount).ToList();
101+
102+
_fixture.Customizations.Add(new SamPartyBuilder(changeType, batchId, partyIds));
103+
104+
var parties = _fixture.CreateMany<SamParty>(partyCount).ToList();
105+
106+
return (holdings, holders, herds, parties);
107+
}
108+
}

tests/KeeperData.Tests.Common/SpecimenBuilders/CtsAgentOrKeeperBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ namespace KeeperData.Tests.Common.SpecimenBuilders;
66

77
public class CtsAgentOrKeeperBuilder(
88
string fixedChangeType,
9-
string holdingIdentifier,
109
int batchId,
10+
string holdingIdentifier,
1111
DateTime? fixedEndDate = null) : ISpecimenBuilder
1212
{
1313
private readonly Random _random = new();
1414

1515
private readonly string _fixedChangeType = fixedChangeType;
16+
private readonly int _batchId = batchId;
1617
private readonly string _holdingIdentifier = holdingIdentifier;
1718
private readonly DateTime? _fixedEndDate = fixedEndDate;
1819

@@ -48,7 +49,7 @@ public object Create(object request, ISpecimenContext context)
4849
LPR_EFFECTIVE_FROM_DATE = DateTime.Today.AddDays(-_random.Next(500)),
4950
LPR_EFFECTIVE_TO_DATE = _fixedEndDate,
5051

51-
BATCH_ID = batchId,
52+
BATCH_ID = _batchId,
5253
CHANGE_TYPE = _fixedChangeType
5354
};
5455
}

tests/KeeperData.Tests.Common/SpecimenBuilders/CtsCphHoldingBuilder.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace KeeperData.Tests.Common.SpecimenBuilders;
66

77
public class CtsCphHoldingBuilder(
88
string fixedChangeType,
9+
int batchId,
10+
string holdingIdentifier,
911
string? fixedLocType = null,
1012
DateTime? fixedEndDate = null) : ISpecimenBuilder
1113
{
@@ -14,19 +16,20 @@ public class CtsCphHoldingBuilder(
1416
private readonly string[] _locationTypes = ["AG", "SH", "AH", "CL", "CC"];
1517

1618
private readonly string _fixedChangeType = fixedChangeType;
19+
private readonly int _batchId = batchId;
20+
private readonly string _holdingIdentifier = holdingIdentifier;
1721
private readonly string? _fixedLocType = fixedLocType;
1822
private readonly DateTime? _fixedEndDate = fixedEndDate;
1923

2024
public object Create(object request, ISpecimenContext context)
2125
{
2226
if (request is Type type && type == typeof(CtsCphHolding))
2327
{
24-
var holdingIdentifier = CphGenerator.GenerateFormattedCph();
2528
var (addressName, address2, address3, address4, address5, postCode) = AddressGenerator.GenerateCtsAddress();
2629

2730
return new CtsCphHolding
2831
{
29-
LID_FULL_IDENTIFIER = holdingIdentifier,
32+
LID_FULL_IDENTIFIER = _holdingIdentifier,
3033
LTY_LOC_TYPE = _fixedLocType ?? _locationTypes[_random.Next(_locationTypes.Length)],
3134

3235
ADR_NAME = addressName,
@@ -43,7 +46,7 @@ public object Create(object request, ISpecimenContext context)
4346
LOC_EFFECTIVE_FROM = DateTime.Today.AddDays(-_random.Next(1000)),
4447
LOC_EFFECTIVE_TO = _fixedEndDate,
4548

46-
BATCH_ID = _random.Next(1000),
49+
BATCH_ID = _batchId,
4750
CHANGE_TYPE = _fixedChangeType
4851
};
4952
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using AutoFixture.Kernel;
2+
using KeeperData.Core.ApiClients.DataBridgeApi.Contracts;
3+
using KeeperData.Tests.Common.Generators;
4+
5+
namespace KeeperData.Tests.Common.SpecimenBuilders;
6+
7+
public class SamCphHolderBuilder(
8+
string fixedChangeType,
9+
int batchId,
10+
string holdingIdentifier) : ISpecimenBuilder
11+
{
12+
private readonly Random _random = new();
13+
14+
private readonly string _fixedChangeType = fixedChangeType;
15+
private readonly int _batchId = batchId;
16+
private readonly string _holdingIdentifier = holdingIdentifier;
17+
18+
public object Create(object request, ISpecimenContext context)
19+
{
20+
if (request is Type type && type == typeof(SamCphHolder))
21+
{
22+
return new SamCphHolder
23+
{
24+
CPHS = $"{_holdingIdentifier}",
25+
26+
BATCH_ID = _batchId,
27+
CHANGE_TYPE = _fixedChangeType
28+
};
29+
}
30+
31+
return new NoSpecimen();
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using AutoFixture.Kernel;
2+
using KeeperData.Core.ApiClients.DataBridgeApi.Contracts;
3+
using KeeperData.Tests.Common.Generators;
4+
5+
namespace KeeperData.Tests.Common.SpecimenBuilders;
6+
7+
public class SamCphHoldingBuilder(
8+
string fixedChangeType,
9+
int batchId,
10+
string holdingIdentifier,
11+
DateTime? fixedEndDate = null) : ISpecimenBuilder
12+
{
13+
private readonly Random _random = new();
14+
15+
private readonly string _fixedChangeType = fixedChangeType;
16+
private readonly int _batchId = batchId;
17+
private readonly string _holdingIdentifier = holdingIdentifier;
18+
private readonly DateTime? _fixedEndDate = fixedEndDate;
19+
20+
public object Create(object request, ISpecimenContext context)
21+
{
22+
if (request is Type type && type == typeof(SamCphHolding))
23+
{
24+
return new SamCphHolding
25+
{
26+
CPH = _holdingIdentifier,
27+
28+
FEATURE_ADDRESS_TO_DATE = _fixedEndDate,
29+
30+
BATCH_ID = _batchId,
31+
CHANGE_TYPE = _fixedChangeType
32+
};
33+
}
34+
35+
return new NoSpecimen();
36+
}
37+
}

0 commit comments

Comments
 (0)