Skip to content

Commit 7403ec4

Browse files
Merge pull request #44 from StuartFerguson/task/#11_validateestateid
Added Estate Validation
2 parents 1d8e38c + 0449438 commit 7403ec4

7 files changed

Lines changed: 198 additions & 15 deletions

File tree

TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public async Task TransactionDomainService_ProcessLogonTransaction_TransactionIs
4747
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
4848

4949
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
50+
estateClient.Setup(e => e.GetEstate(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEstateResponse);
5051
estateClient.Setup(e => e.GetMerchant(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
5152
.ReturnsAsync(TestData.GetMerchantResponse);
5253

@@ -89,6 +90,7 @@ public async Task TransactionDomainService_ProcessLogonTransaction_MerchantWithN
8990
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
9091

9192
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
93+
estateClient.Setup(e => e.GetEstate(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEstateResponse);
9294
estateClient.Setup(e => e.GetMerchant(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
9395
.ReturnsAsync(TestData.GetMerchantResponseWithNullDevices);
9496

@@ -131,6 +133,7 @@ public async Task TransactionDomainService_ProcessLogonTransaction_MerchantWithN
131133
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
132134

133135
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
136+
estateClient.Setup(e => e.GetEstate(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEstateResponse);
134137
estateClient.Setup(e => e.GetMerchant(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
135138
.ReturnsAsync(TestData.GetMerchantResponseWithNoDevices);
136139

@@ -173,6 +176,7 @@ public async Task TransactionDomainService_ProcessLogonTransaction_IncorrectDevi
173176
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
174177

175178
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
179+
estateClient.Setup(e => e.GetEstate(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEstateResponse);
176180
estateClient.Setup(e => e.GetMerchant(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
177181
.ReturnsAsync(TestData.GetMerchantResponse);
178182

@@ -192,7 +196,48 @@ public async Task TransactionDomainService_ProcessLogonTransaction_IncorrectDevi
192196
response.ResponseMessage.ShouldBe(TestData.ResponseMessage);
193197
}
194198

195-
// Txn for a Different Device (Causes a Validation Exception)
199+
[Fact]
200+
public async Task TransactionDomainService_ProcessLogonTransaction_InvlaidEstate_TransactionIsProcessed()
201+
{
202+
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
203+
ConfigurationReader.Initialise(configurationRoot);
204+
205+
Logger.Initialise(NullLogger.Instance);
206+
207+
Mock<IAggregateRepositoryManager> aggregateRepositoryManager = new Mock<IAggregateRepositoryManager>();
208+
Mock<IAggregateRepository<TransactionAggregate>> transactionAggregateRepository = new Mock<IAggregateRepository<TransactionAggregate>>();
209+
210+
aggregateRepositoryManager.Setup(x => x.GetAggregateRepository<TransactionAggregate>(It.IsAny<Guid>())).Returns(transactionAggregateRepository.Object);
211+
transactionAggregateRepository.SetupSequence(t => t.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
212+
.ReturnsAsync(TestData.GetEmptyTransactionAggregate)
213+
.ReturnsAsync(TestData.GetStartedTransactionAggregate)
214+
.ReturnsAsync(TestData.GetLocallyAuthorisedTransactionAggregate)
215+
.ReturnsAsync(TestData.GetCompletedTransactionAggregate);
216+
transactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny<TransactionAggregate>(), It.IsAny<CancellationToken>())).Returns(Task.CompletedTask);
217+
218+
Mock<IEstateClient> estateClient = new Mock<IEstateClient>();
219+
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
220+
221+
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
222+
estateClient.Setup(e => e.GetEstate(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEmptyEstateResponse);
223+
estateClient.Setup(e => e.GetMerchant(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
224+
.ReturnsAsync(TestData.GetMerchantResponse);
225+
226+
TransactionDomainService transactionDomainService =
227+
new TransactionDomainService(aggregateRepositoryManager.Object, estateClient.Object, securityServiceClient.Object);
228+
229+
ProcessLogonTransactionResponse response = await transactionDomainService.ProcessLogonTransaction(TestData.TransactionId,
230+
TestData.EstateId,
231+
TestData.MerchantId,
232+
TestData.TransactionDateTime,
233+
TestData.TransactionNumber,
234+
TestData.DeviceIdentifier1,
235+
CancellationToken.None);
236+
237+
response.ShouldNotBeNull();
238+
response.ResponseCode.ShouldBe(TestData.ResponseCode);
239+
response.ResponseMessage.ShouldBe(TestData.ResponseMessage);
240+
}
196241

197242
}
198243
}

TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ public async Task<ProcessLogonTransactionResponse> ProcessLogonTransaction(Guid
147147
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
148148
Logger.LogInformation($"Token is {token.AccessToken}");
149149

150+
// Validate the Estate Record is a valid estate
151+
var estate = await this.EstateClient.GetEstate(token.AccessToken, estateId, cancellationToken);
152+
153+
// TODO: Remove this once GetEstate returns correct response when estate not found
154+
if (estate.EstateName == null)
155+
{
156+
157+
158+
159+
160+
161+
162+
throw new TransactionValidationException($"Estate Id [{estateId}] is not a valid estate", TransactionResponseCode.InvalidEstateId);
163+
}
164+
150165
// get the merchant record and validate the device
151166
// TODO: Token
152167
MerchantResponse merchant = await this.EstateClient.GetMerchant(token.AccessToken, estateId, merchantId, cancellationToken);
@@ -187,10 +202,4 @@ await this.EstateClient.AddDeviceToMerchant(token.AccessToken,
187202

188203
#endregion
189204
}
190-
191-
public enum TransactionResponseCode
192-
{
193-
Success = 0,
194-
InvalidDeviceIdentifier = 1000
195-
}
196205
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace TransactionProcessor.BusinessLogic.Services
2+
{
3+
public enum TransactionResponseCode
4+
{
5+
Success = 0,
6+
7+
InvalidDeviceIdentifier = 1000,
8+
InvalidEstateId = 1001
9+
}
10+
}

TransactionProcessor.IntegrationTests/Common/TestingContext.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,22 @@ public TestingContext()
5656
public EstateDetails GetEstateDetails(TableRow tableRow)
5757
{
5858
String estateName = SpecflowTableHelper.GetStringRowValue(tableRow, "EstateName");
59+
EstateDetails estateDetails = null;
60+
61+
estateDetails = this.Estates.SingleOrDefault(e => e.EstateName == estateName);
5962

60-
EstateDetails estateDetails = this.Estates.SingleOrDefault(e => e.EstateName == estateName);
63+
if (estateDetails == null && estateName == "InvalidEstate")
64+
{
65+
estateDetails = EstateDetails.Create(Guid.Parse("79902550-64DF-4491-B0C1-4E78943928A3"), estateName);
66+
estateDetails.AddMerchant(Guid.Parse("36AA0109-E2E3-4049-9575-F507A887BB1F"), "Test Merchant 1");
67+
this.Estates.Add(estateDetails);
68+
}
6169

6270
estateDetails.ShouldNotBeNull();
6371

6472
return estateDetails;
6573
}
66-
74+
6775
public EstateDetails GetEstateDetails(String estateName)
6876
{
6977
EstateDetails estateDetails = this.Estates.SingleOrDefault(e => e.EstateName == estateName);

TransactionProcessor.IntegrationTests/LogonTransaction/LogonTransaction.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,18 @@ Scenario: Logon Transaction with Invalid Device
7878
Then transaction response should contain the following information
7979
| EstateName | MerchantName | TransactionNumber | ResponseCode | ResponseMessage |
8080
| Test Estate 1 | Test Merchant 1 | 1 | 1000 | Device Identifier 123456781 not valid for Merchant Test Merchant 1 |
81+
82+
Scenario: Logon Transaction with Invalid Estate
83+
84+
Given I have assigned the following devices to the merchants
85+
| DeviceIdentifier | MerchantName | MerchantNumber | EstateName |
86+
| 123456780 | Test Merchant 1 | 00000001 | Test Estate 1 |
87+
88+
When I perform the following transactions
89+
| DateTime | TransactionNumber | TransactionType | MerchantName | DeviceIdentifier | EstateName |
90+
| Today | 1 | Logon | Test Merchant 1 | 123456781 | InvalidEstate |
91+
92+
Then transaction response should contain the following information
93+
| EstateName | MerchantName | TransactionNumber | ResponseCode | ResponseMessage |
94+
| InvalidEstate | Test Merchant 1 | 1 | 1001 | Estate Id [79902550-64df-4491-b0c1-4e78943928a3] is not a valid estate |
95+

TransactionProcessor.IntegrationTests/LogonTransaction/LogonTransaction.feature.cs

Lines changed: 88 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TransactionProcessor.Testing/TestData.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,19 @@ public static TokenResponse TokenResponse()
126126
MerchantId = TestData.MerchantId,
127127
Devices = new Dictionary<Guid, String>()
128128
};
129+
130+
public static EstateResponse GetEmptyEstateResponse = new EstateResponse
131+
{
132+
EstateName = null,
133+
EstateId = TestData.EstateId
134+
};
135+
public static String EstateName = "Test Estate 1";
136+
public static EstateResponse GetEstateResponse = new EstateResponse
137+
{
138+
EstateName = TestData.EstateName,
139+
EstateId = TestData.EstateId
140+
};
141+
142+
129143
}
130144
}

0 commit comments

Comments
 (0)