Skip to content

Commit 68a33bf

Browse files
Merge pull request #59 from StuartFerguson/bug/failedresponsehandling
Better error handling added
2 parents a7fffb5 + 66f1e05 commit 68a33bf

3 files changed

Lines changed: 174 additions & 10 deletions

File tree

TransactionProcessorACL.BusinessLogic.Tests/TransactionProcessorACLApplicationServiceTests.cs

Lines changed: 129 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace TransactionProcessorACL.BusinesssLogic.Tests
66
{
7+
using System.Net.Http;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using BusinessLogic.Services;
@@ -64,14 +65,14 @@ public async Task TransactionProcessorACLApplicationService_ProcessLogonTransact
6465
}
6566

6667
[Fact]
67-
public async Task TransactionProcessorACLApplicationService_ProcessLogonTransaction_ErrorInLogon_TransactionIsNotSuccessful()
68+
public async Task TransactionProcessorACLApplicationService_ProcessLogonTransaction_InvalidOperationExceptionErrorInLogon_TransactionIsNotSuccessful()
6869
{
6970
IConfigurationRoot configuration = this.SetupMemoryConfiguration();
7071
ConfigurationReader.Initialise(configuration);
7172

7273
Mock<ITransactionProcessorClient> transactionProcessorClient = new Mock<ITransactionProcessorClient>();
7374
transactionProcessorClient.Setup(t => t.PerformTransaction(It.IsAny<String>(), It.IsAny<SerialisedMessage>(), It.IsAny<CancellationToken>()))
74-
.ThrowsAsync(new Exception("Error", new InvalidOperationException(TestData.ErrorResponseMessage)));
75+
.ThrowsAsync(new Exception("Error", new InvalidOperationException(TestData.InvalidOperationErrorResponseMessage)));
7576
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
7677
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
7778

@@ -86,8 +87,62 @@ public async Task TransactionProcessorACLApplicationService_ProcessLogonTransact
8687
CancellationToken.None);
8788

8889
logonResponse.ShouldNotBeNull();
89-
logonResponse.ResponseMessage.ShouldBe(TestData.ErrorResponseMessage);
90-
logonResponse.ResponseCode.ShouldBe(TestData.ErrorResponseCode);
90+
logonResponse.ResponseMessage.ShouldBe(TestData.InvalidOperationErrorResponseMessage);
91+
logonResponse.ResponseCode.ShouldBe(TestData.InvalidOperationErrorResponseCode);
92+
}
93+
94+
[Fact]
95+
public async Task TransactionProcessorACLApplicationService_ProcessLogonTransaction_HttpRequestExceptionErrorInLogon_TransactionIsNotSuccessful()
96+
{
97+
IConfigurationRoot configuration = this.SetupMemoryConfiguration();
98+
ConfigurationReader.Initialise(configuration);
99+
100+
Mock<ITransactionProcessorClient> transactionProcessorClient = new Mock<ITransactionProcessorClient>();
101+
transactionProcessorClient.Setup(t => t.PerformTransaction(It.IsAny<String>(), It.IsAny<SerialisedMessage>(), It.IsAny<CancellationToken>()))
102+
.ThrowsAsync(new Exception("Error", new HttpRequestException(TestData.HttpRequestErrorResponseMessage)));
103+
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
104+
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
105+
106+
ITransactionProcessorACLApplicationService applicationService =
107+
new TransactionProcessorACLApplicationService(transactionProcessorClient.Object, securityServiceClient.Object);
108+
109+
ProcessLogonTransactionResponse logonResponse = await applicationService.ProcessLogonTransaction(TestData.EstateId,
110+
TestData.MerchantId,
111+
TestData.TransactionDateTime,
112+
TestData.TransactionNumber,
113+
TestData.DeviceIdentifier,
114+
CancellationToken.None);
115+
116+
logonResponse.ShouldNotBeNull();
117+
logonResponse.ResponseMessage.ShouldBe(TestData.HttpRequestErrorResponseMessage);
118+
logonResponse.ResponseCode.ShouldBe(TestData.HttpRequestErrorResponseCode);
119+
}
120+
121+
[Fact]
122+
public async Task TransactionProcessorACLApplicationService_ProcessLogonTransaction_OtherExceptionErrorInLogon_TransactionIsNotSuccessful()
123+
{
124+
IConfigurationRoot configuration = this.SetupMemoryConfiguration();
125+
ConfigurationReader.Initialise(configuration);
126+
127+
Mock<ITransactionProcessorClient> transactionProcessorClient = new Mock<ITransactionProcessorClient>();
128+
transactionProcessorClient.Setup(t => t.PerformTransaction(It.IsAny<String>(), It.IsAny<SerialisedMessage>(), It.IsAny<CancellationToken>()))
129+
.ThrowsAsync(new Exception("Error", new Exception(TestData.GeneralErrorResponseMessage)));
130+
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
131+
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
132+
133+
ITransactionProcessorACLApplicationService applicationService =
134+
new TransactionProcessorACLApplicationService(transactionProcessorClient.Object, securityServiceClient.Object);
135+
136+
ProcessLogonTransactionResponse logonResponse = await applicationService.ProcessLogonTransaction(TestData.EstateId,
137+
TestData.MerchantId,
138+
TestData.TransactionDateTime,
139+
TestData.TransactionNumber,
140+
TestData.DeviceIdentifier,
141+
CancellationToken.None);
142+
143+
logonResponse.ShouldNotBeNull();
144+
logonResponse.ResponseMessage.ShouldBe(TestData.GeneralErrorResponseMessage);
145+
logonResponse.ResponseCode.ShouldBe(TestData.GeneralErrorResponseCode);
91146
}
92147

93148
[Fact]
@@ -124,14 +179,80 @@ public async Task TransactionProcessorACLApplicationService_ProcessSaleTransacti
124179
}
125180

126181
[Fact]
127-
public async Task TransactionProcessorACLApplicationService_ProcessSaleTransaction_ErrorInSale_TransactionIsNotSuccessful()
182+
public async Task TransactionProcessorACLApplicationService_ProcessSaleTransaction_InvalidOperationExceptionErrorInSale_TransactionIsNotSuccessful()
183+
{
184+
IConfigurationRoot configuration = this.SetupMemoryConfiguration();
185+
ConfigurationReader.Initialise(configuration);
186+
187+
Mock<ITransactionProcessorClient> transactionProcessorClient = new Mock<ITransactionProcessorClient>();
188+
transactionProcessorClient.Setup(t => t.PerformTransaction(It.IsAny<String>(), It.IsAny<SerialisedMessage>(), It.IsAny<CancellationToken>()))
189+
.ThrowsAsync(new Exception("Error", new InvalidOperationException(TestData.InvalidOperationErrorResponseMessage)));
190+
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
191+
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
192+
193+
ITransactionProcessorACLApplicationService applicationService =
194+
new TransactionProcessorACLApplicationService(transactionProcessorClient.Object, securityServiceClient.Object);
195+
196+
ProcessSaleTransactionResponse saleResponse = await applicationService.ProcessSaleTransaction(TestData.EstateId,
197+
TestData.MerchantId,
198+
TestData.TransactionDateTime,
199+
TestData.TransactionNumber,
200+
TestData.DeviceIdentifier,
201+
TestData.OperatorIdentifier,
202+
TestData.SaleAmount,
203+
TestData.CustomerAccountNumber,
204+
TestData.CustomerEmailAddress,
205+
TestData.ContractId,
206+
TestData.ProductId,
207+
CancellationToken.None);
208+
209+
saleResponse.ShouldNotBeNull();
210+
saleResponse.ResponseMessage.ShouldBe(TestData.InvalidOperationErrorResponseMessage);
211+
saleResponse.ResponseCode.ShouldBe(TestData.InvalidOperationErrorResponseCode);
212+
}
213+
214+
[Fact]
215+
public async Task TransactionProcessorACLApplicationService_ProcessSaleTransaction_HttpRequestExceptionErrorInSale_TransactionIsNotSuccessful()
216+
{
217+
IConfigurationRoot configuration = this.SetupMemoryConfiguration();
218+
ConfigurationReader.Initialise(configuration);
219+
220+
Mock<ITransactionProcessorClient> transactionProcessorClient = new Mock<ITransactionProcessorClient>();
221+
transactionProcessorClient.Setup(t => t.PerformTransaction(It.IsAny<String>(), It.IsAny<SerialisedMessage>(), It.IsAny<CancellationToken>()))
222+
.ThrowsAsync(new Exception("Error", new HttpRequestException(TestData.HttpRequestErrorResponseMessage)));
223+
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
224+
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
225+
226+
ITransactionProcessorACLApplicationService applicationService =
227+
new TransactionProcessorACLApplicationService(transactionProcessorClient.Object, securityServiceClient.Object);
228+
229+
ProcessSaleTransactionResponse saleResponse = await applicationService.ProcessSaleTransaction(TestData.EstateId,
230+
TestData.MerchantId,
231+
TestData.TransactionDateTime,
232+
TestData.TransactionNumber,
233+
TestData.DeviceIdentifier,
234+
TestData.OperatorIdentifier,
235+
TestData.SaleAmount,
236+
TestData.CustomerAccountNumber,
237+
TestData.CustomerEmailAddress,
238+
TestData.ContractId,
239+
TestData.ProductId,
240+
CancellationToken.None);
241+
242+
saleResponse.ShouldNotBeNull();
243+
saleResponse.ResponseMessage.ShouldBe(TestData.HttpRequestErrorResponseMessage);
244+
saleResponse.ResponseCode.ShouldBe(TestData.HttpRequestErrorResponseCode);
245+
}
246+
247+
[Fact]
248+
public async Task TransactionProcessorACLApplicationService_ProcessSaleTransaction_OtherExceptionErrorInSale_TransactionIsNotSuccessful()
128249
{
129250
IConfigurationRoot configuration = this.SetupMemoryConfiguration();
130251
ConfigurationReader.Initialise(configuration);
131252

132253
Mock<ITransactionProcessorClient> transactionProcessorClient = new Mock<ITransactionProcessorClient>();
133254
transactionProcessorClient.Setup(t => t.PerformTransaction(It.IsAny<String>(), It.IsAny<SerialisedMessage>(), It.IsAny<CancellationToken>()))
134-
.ThrowsAsync(new Exception("Error", new InvalidOperationException(TestData.ErrorResponseMessage)));
255+
.ThrowsAsync(new Exception("Error", new Exception(TestData.GeneralErrorResponseMessage)));
135256
Mock<ISecurityServiceClient> securityServiceClient = new Mock<ISecurityServiceClient>();
136257
securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
137258

@@ -152,8 +273,8 @@ public async Task TransactionProcessorACLApplicationService_ProcessSaleTransacti
152273
CancellationToken.None);
153274

154275
saleResponse.ShouldNotBeNull();
155-
saleResponse.ResponseMessage.ShouldBe(TestData.ErrorResponseMessage);
156-
saleResponse.ResponseCode.ShouldBe(TestData.ErrorResponseCode);
276+
saleResponse.ResponseMessage.ShouldBe(TestData.GeneralErrorResponseMessage);
277+
saleResponse.ResponseCode.ShouldBe(TestData.GeneralErrorResponseCode);
157278
}
158279
}
159280
}

TransactionProcessorACL.BusinessLogic/Services/TransactionProcessorACLApplicationService.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Net.Http;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using Models;
@@ -118,6 +119,27 @@ public async Task<ProcessLogonTransactionResponse> ProcessLogonTransaction(Guid
118119
MerchantId = merchantId
119120
};
120121
}
122+
else if (ex.InnerException is HttpRequestException)
123+
{
124+
// Request Send Exception
125+
response = new ProcessLogonTransactionResponse
126+
{
127+
ResponseCode = "0002", // Request Message error
128+
ResponseMessage = "Error Sending Request Message",
129+
EstateId = estateId,
130+
MerchantId = merchantId
131+
};
132+
}
133+
else
134+
{
135+
response = new ProcessLogonTransactionResponse
136+
{
137+
ResponseCode = "0003", // General error
138+
ResponseMessage = "General Error",
139+
EstateId = estateId,
140+
MerchantId = merchantId
141+
};
142+
}
121143
}
122144

123145
return response;
@@ -210,6 +232,23 @@ public async Task<ProcessSaleTransactionResponse> ProcessSaleTransaction(Guid es
210232
ResponseMessage = ex.InnerException.Message
211233
};
212234
}
235+
else if (ex.InnerException is HttpRequestException)
236+
{
237+
// Request Send Exception
238+
response = new ProcessSaleTransactionResponse
239+
{
240+
ResponseCode = "0002", // Request Message error
241+
ResponseMessage = "Error Sending Request Message"
242+
};
243+
}
244+
else
245+
{
246+
response = new ProcessSaleTransactionResponse
247+
{
248+
ResponseCode = "0003", // General error
249+
ResponseMessage = "General Error"
250+
};
251+
}
213252
}
214253

215254
return response;

TransactionProcessorACL.Testing/TestData.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ public class TestData
6969
TestData.RequireConfigurationInResponseTrue);
7070

7171
public static String ResponseCode = "0000";
72-
public static String ErrorResponseCode = "0001";
72+
public static String InvalidOperationErrorResponseCode = "0001";
73+
public static String HttpRequestErrorResponseCode = "0002";
74+
public static String GeneralErrorResponseCode = "0003";
7375

7476
public static String ResponseMessage = "SUCCESS";
75-
public static String ErrorResponseMessage = "ERROR";
77+
public static String InvalidOperationErrorResponseMessage = "ERROR";
78+
public static String HttpRequestErrorResponseMessage = "Error Sending Request Message";
79+
public static String GeneralErrorResponseMessage = "General Error";
7680

7781
public static ProcessLogonTransactionResponse ProcessLogonTransactionResponse = new ProcessLogonTransactionResponse
7882
{

0 commit comments

Comments
 (0)