Skip to content

Commit 7267987

Browse files
Merge pull request #45 from StuartFerguson/task/#10_validatemerchantid
Merchant Id validated
2 parents 7403ec4 + d7a273b commit 7267987

File tree

8 files changed

+213
-34
lines changed

8 files changed

+213
-34
lines changed

TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public async Task TransactionDomainService_ProcessLogonTransaction_TransactionIs
6262
TestData.DeviceIdentifier,
6363
CancellationToken.None);
6464

65-
response.ShouldNotBeNull();
66-
response.ResponseCode.ShouldBe(TestData.ResponseCode);
67-
response.ResponseMessage.ShouldBe(TestData.ResponseMessage);
65+
this.ValidateLogonResponse(response, TransactionResponseCode.Success);
6866
}
6967

7068
[Fact]
@@ -105,9 +103,7 @@ public async Task TransactionDomainService_ProcessLogonTransaction_MerchantWithN
105103
TestData.DeviceIdentifier,
106104
CancellationToken.None);
107105

108-
response.ShouldNotBeNull();
109-
response.ResponseCode.ShouldBe(TestData.ResponseCode);
110-
response.ResponseMessage.ShouldBe(TestData.ResponseMessage);
106+
this.ValidateLogonResponse(response, TransactionResponseCode.Success);
111107
}
112108

113109
[Fact]
@@ -148,9 +144,7 @@ public async Task TransactionDomainService_ProcessLogonTransaction_MerchantWithN
148144
TestData.DeviceIdentifier,
149145
CancellationToken.None);
150146

151-
response.ShouldNotBeNull();
152-
response.ResponseCode.ShouldBe(TestData.ResponseCode);
153-
response.ResponseMessage.ShouldBe(TestData.ResponseMessage);
147+
this.ValidateLogonResponse(response, TransactionResponseCode.Success);
154148
}
155149

156150
[Fact]
@@ -168,7 +162,7 @@ public async Task TransactionDomainService_ProcessLogonTransaction_IncorrectDevi
168162
transactionAggregateRepository.SetupSequence(t => t.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
169163
.ReturnsAsync(TestData.GetEmptyTransactionAggregate)
170164
.ReturnsAsync(TestData.GetStartedTransactionAggregate)
171-
.ReturnsAsync(TestData.GetLocallyAuthorisedTransactionAggregate)
165+
.ReturnsAsync(TestData.GetLocallyDeclinedTransactionAggregate(TransactionResponseCode.InvalidDeviceIdentifier))
172166
.ReturnsAsync(TestData.GetCompletedTransactionAggregate);
173167
transactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny<TransactionAggregate>(), It.IsAny<CancellationToken>())).Returns(Task.CompletedTask);
174168

@@ -191,13 +185,11 @@ public async Task TransactionDomainService_ProcessLogonTransaction_IncorrectDevi
191185
TestData.DeviceIdentifier1,
192186
CancellationToken.None);
193187

194-
response.ShouldNotBeNull();
195-
response.ResponseCode.ShouldBe(TestData.ResponseCode);
196-
response.ResponseMessage.ShouldBe(TestData.ResponseMessage);
188+
this.ValidateLogonResponse(response, TransactionResponseCode.InvalidDeviceIdentifier);
197189
}
198190

199191
[Fact]
200-
public async Task TransactionDomainService_ProcessLogonTransaction_InvlaidEstate_TransactionIsProcessed()
192+
public async Task TransactionDomainService_ProcessLogonTransaction_InvalidEstate_TransactionIsProcessed()
201193
{
202194
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
203195
ConfigurationReader.Initialise(configurationRoot);
@@ -211,7 +203,7 @@ public async Task TransactionDomainService_ProcessLogonTransaction_InvlaidEstate
211203
transactionAggregateRepository.SetupSequence(t => t.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
212204
.ReturnsAsync(TestData.GetEmptyTransactionAggregate)
213205
.ReturnsAsync(TestData.GetStartedTransactionAggregate)
214-
.ReturnsAsync(TestData.GetLocallyAuthorisedTransactionAggregate)
206+
.ReturnsAsync(TestData.GetLocallyDeclinedTransactionAggregate(TransactionResponseCode.InvalidEstateId))
215207
.ReturnsAsync(TestData.GetCompletedTransactionAggregate);
216208
transactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny<TransactionAggregate>(), It.IsAny<CancellationToken>())).Returns(Task.CompletedTask);
217209

@@ -234,10 +226,63 @@ public async Task TransactionDomainService_ProcessLogonTransaction_InvlaidEstate
234226
TestData.DeviceIdentifier1,
235227
CancellationToken.None);
236228

237-
response.ShouldNotBeNull();
238-
response.ResponseCode.ShouldBe(TestData.ResponseCode);
239-
response.ResponseMessage.ShouldBe(TestData.ResponseMessage);
229+
this.ValidateLogonResponse(response, TransactionResponseCode.InvalidEstateId);
240230
}
241231

232+
[Fact]
233+
public async Task TransactionDomainService_ProcessLogonTransaction_InvalidMerchant_TransactionIsProcessed()
234+
{
235+
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
236+
ConfigurationReader.Initialise(configurationRoot);
237+
238+
Logger.Initialise(NullLogger.Instance);
239+
240+
Mock<IAggregateRepositoryManager> aggregateRepositoryManager = new Mock<IAggregateRepositoryManager>();
241+
Mock<IAggregateRepository<TransactionAggregate>> transactionAggregateRepository = new Mock<IAggregateRepository<TransactionAggregate>>();
242+
243+
aggregateRepositoryManager.Setup(x => x.GetAggregateRepository<TransactionAggregate>(It.IsAny<Guid>())).Returns(transactionAggregateRepository.Object);
244+
transactionAggregateRepository.SetupSequence(t => t.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
245+
.ReturnsAsync(TestData.GetEmptyTransactionAggregate)
246+
.ReturnsAsync(TestData.GetStartedTransactionAggregate)
247+
.ReturnsAsync(TestData.GetLocallyDeclinedTransactionAggregate(TransactionResponseCode.InvalidMerchantId))
248+
.ReturnsAsync(TestData.GetCompletedTransactionAggregate);
249+
transactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny<TransactionAggregate>(), It.IsAny<CancellationToken>())).Returns(Task.CompletedTask);
250+
251+
Mock<IEstateClient> estateClient = new Mock<IEstateClient>();
252+
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
253+
254+
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
255+
estateClient.Setup(e => e.GetEstate(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEstateResponse);
256+
estateClient.Setup(e => e.GetMerchant(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
257+
.ReturnsAsync(TestData.GetEmptyMerchantResponse);
258+
259+
TransactionDomainService transactionDomainService =
260+
new TransactionDomainService(aggregateRepositoryManager.Object, estateClient.Object, securityServiceClient.Object);
261+
262+
ProcessLogonTransactionResponse response = await transactionDomainService.ProcessLogonTransaction(TestData.TransactionId,
263+
TestData.EstateId,
264+
TestData.MerchantId,
265+
TestData.TransactionDateTime,
266+
TestData.TransactionNumber,
267+
TestData.DeviceIdentifier1,
268+
CancellationToken.None);
269+
270+
this.ValidateLogonResponse(response, TransactionResponseCode.InvalidMerchantId);
271+
}
272+
273+
private void ValidateLogonResponse(ProcessLogonTransactionResponse response,
274+
TransactionResponseCode transactionResponseCode)
275+
{
276+
response.ShouldNotBeNull();
277+
response.ResponseCode.ShouldBe(TestData.GetResponseCodeAsString(transactionResponseCode));
278+
279+
String messageToValidate = TestData.GetResponseCodeMessage(transactionResponseCode);
280+
if (transactionResponseCode == TransactionResponseCode.Success)
281+
{
282+
messageToValidate = messageToValidate.ToUpper();
283+
}
284+
285+
response.ResponseMessage.ShouldBe(messageToValidate);
286+
}
242287
}
243288
}

TransactionProcessor.BusinessLogic.Tests/TransactionProcessor.BusinessLogic.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.1</TargetFramework>
5-
<DebugType>None</DebugType>
5+
<DebugType>Full</DebugType>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,19 @@ public async Task<ProcessLogonTransactionResponse> ProcessLogonTransaction(Guid
153153
// TODO: Remove this once GetEstate returns correct response when estate not found
154154
if (estate.EstateName == null)
155155
{
156-
157-
158-
159-
160-
161-
162156
throw new TransactionValidationException($"Estate Id [{estateId}] is not a valid estate", TransactionResponseCode.InvalidEstateId);
163157
}
164158

165159
// get the merchant record and validate the device
166160
// TODO: Token
167161
MerchantResponse merchant = await this.EstateClient.GetMerchant(token.AccessToken, estateId, merchantId, cancellationToken);
168162

163+
// TODO: Remove this once GetMerchant returns correct response when merchant not found
164+
if (merchant.MerchantName == null)
165+
{
166+
throw new TransactionValidationException($"Merchant Id [{merchantId}] is not a valid merchant for estate [{estate.EstateName}]", TransactionResponseCode.InvalidMerchantId);
167+
}
168+
169169
if (merchant.Devices == null || merchant.Devices.Any() == false)
170170
{
171171
// Add the device to the merchant

TransactionProcessor.BusinessLogic/Services/TransactionResponseCode.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public enum TransactionResponseCode
55
Success = 0,
66

77
InvalidDeviceIdentifier = 1000,
8-
InvalidEstateId = 1001
8+
InvalidEstateId = 1001,
9+
InvalidMerchantId = 1002
910
}
1011
}

TransactionProcessor.IntegrationTests/Common/TestingContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ public void AddMerchant(Guid merchantId,
167167

168168
public Guid GetMerchantId(String merchantName)
169169
{
170+
if (merchantName == "InvalidMerchant")
171+
{
172+
return Guid.Parse("D59320FA-4C3E-4900-A999-483F6A10C69A");
173+
}
174+
170175
return this.Merchants.Single(m => m.Key == merchantName).Value;
171176
}
172177

TransactionProcessor.IntegrationTests/LogonTransaction/LogonTransaction.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,17 @@ Scenario: Logon Transaction with Invalid Estate
9393
| EstateName | MerchantName | TransactionNumber | ResponseCode | ResponseMessage |
9494
| InvalidEstate | Test Merchant 1 | 1 | 1001 | Estate Id [79902550-64df-4491-b0c1-4e78943928a3] is not a valid estate |
9595

96+
Scenario: Logon Transaction with Invalid Merchant
97+
98+
Given I have assigned the following devices to the merchants
99+
| DeviceIdentifier | MerchantName | MerchantNumber | EstateName |
100+
| 123456780 | Test Merchant 1 | 00000001 | Test Estate 1 |
101+
102+
When I perform the following transactions
103+
| DateTime | TransactionNumber | TransactionType | MerchantName | DeviceIdentifier | EstateName |
104+
| Today | 1 | Logon | InvalidMerchant | 123456781 | Test Estate 1 |
105+
106+
Then transaction response should contain the following information
107+
| EstateName | MerchantName | TransactionNumber | ResponseCode | ResponseMessage |
108+
| Test Estate 1 | InvalidMerchant | 1 | 1002 | Merchant Id [d59320fa-4c3e-4900-a999-483f6a10c69a] is not a valid merchant for estate [Test Estate 1] |
109+

TransactionProcessor.IntegrationTests/LogonTransaction/LogonTransaction.feature.cs

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

0 commit comments

Comments
 (0)