Skip to content

Commit 5cf1412

Browse files
Merge pull request #59 from StuartFerguson/task/#55_operatormessageproxy
Safaricom Proxy unit tests added
2 parents 9b04b64 + fa2a572 commit 5cf1412

7 files changed

Lines changed: 157 additions & 8 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TransactionProcessor.BusinessLogic.Tests.OperatorInterfaces
6+
{
7+
using System.Net;
8+
using System.Net.Http;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using BusinessLogic.OperatorInterfaces;
12+
using BusinessLogic.OperatorInterfaces.SafaricomPinless;
13+
using Moq;
14+
using Moq.Protected;
15+
using Shouldly;
16+
using Testing;
17+
using Xunit;
18+
19+
public class SafaricomPinlessProxyTests
20+
{
21+
[Fact]
22+
public async Task SafaricomPinlessProxy_ProcessSaleMessage_TopupSuccessful_SaleMessageIsProcessed()
23+
{
24+
HttpResponseMessage responseMessage = new HttpResponseMessage
25+
{
26+
StatusCode = HttpStatusCode.OK,
27+
Content = new StringContent(TestData.SuccessfulSafaricomTopup)
28+
};
29+
30+
SafaricomConfiguration safaricomConfiguration = TestData.SafaricomConfiguration;
31+
HttpClient httpClient = SetupMockHttpClient(responseMessage);
32+
33+
IOperatorProxy safaricomPinlessproxy = new SafaricomPinlessProxy(safaricomConfiguration, httpClient);
34+
35+
OperatorResponse operatorResponse = await safaricomPinlessproxy.ProcessSaleMessage(TestData.TransactionId,
36+
TestData.Merchant,
37+
TestData.TransactionDateTime,
38+
TestData.AdditionalTransactionMetaData,
39+
CancellationToken.None);
40+
41+
operatorResponse.ShouldNotBeNull();
42+
operatorResponse.IsSuccessful.ShouldBeTrue();
43+
operatorResponse.ResponseCode.ShouldBe("200");
44+
operatorResponse.ResponseMessage.ShouldBe("Topup Successful");
45+
}
46+
47+
[Fact]
48+
public async Task SafaricomPinlessProxy_ProcessSaleMessage_TopupFailed_SaleMessageIsProcessed()
49+
{
50+
HttpResponseMessage responseMessage = new HttpResponseMessage
51+
{
52+
StatusCode = HttpStatusCode.OK,
53+
Content = new StringContent(TestData.FailedSafaricomTopup)
54+
};
55+
56+
SafaricomConfiguration safaricomConfiguration = TestData.SafaricomConfiguration;
57+
HttpClient httpClient = SetupMockHttpClient(responseMessage);
58+
59+
IOperatorProxy safaricomPinlessproxy = new SafaricomPinlessProxy(safaricomConfiguration, httpClient);
60+
61+
OperatorResponse operatorResponse = await safaricomPinlessproxy.ProcessSaleMessage(TestData.TransactionId,
62+
TestData.Merchant,
63+
TestData.TransactionDateTime,
64+
TestData.AdditionalTransactionMetaData,
65+
CancellationToken.None);
66+
67+
operatorResponse.ShouldNotBeNull();
68+
operatorResponse.IsSuccessful.ShouldBeFalse();
69+
operatorResponse.ResponseCode.ShouldBe("500");
70+
operatorResponse.ResponseMessage.ShouldBe("Topup failed");
71+
}
72+
73+
[Fact]
74+
public async Task SafaricomPinlessProxy_ProcessSaleMessage_FailedToSend_ErrorThrown()
75+
{
76+
HttpResponseMessage responseMessage = new HttpResponseMessage
77+
{
78+
StatusCode = HttpStatusCode.InternalServerError
79+
};
80+
81+
SafaricomConfiguration safaricomConfiguration = TestData.SafaricomConfiguration;
82+
HttpClient httpClient = SetupMockHttpClient(responseMessage);
83+
84+
IOperatorProxy safaricomPinlessproxy = new SafaricomPinlessProxy(safaricomConfiguration, httpClient);
85+
86+
Should.Throw<Exception>(async () =>
87+
{
88+
await safaricomPinlessproxy.ProcessSaleMessage(TestData.TransactionId,
89+
TestData.Merchant,
90+
TestData.TransactionDateTime,
91+
TestData.AdditionalTransactionMetaData,
92+
CancellationToken.None);
93+
});
94+
}
95+
96+
private HttpClient SetupMockHttpClient(HttpResponseMessage responseMessage)
97+
{
98+
Mock<HttpMessageHandler> handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
99+
handlerMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
100+
.ReturnsAsync(responseMessage);
101+
102+
var httpClient = new HttpClient(handlerMock.Object)
103+
{
104+
BaseAddress = new Uri("http://test.com")
105+
};
106+
107+
return httpClient;
108+
}
109+
}
110+
}

TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services
66
{
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using BusinessLogic.OperatorInterfaces;
910
using BusinessLogic.Services;
1011
using EstateManagement.Client;
1112
using Microsoft.Extensions.Configuration;

TransactionProcessor.BusinessLogic/OperatorInterfaces/OperatorResponse.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
namespace TransactionProcessor.BusinessLogic.OperatorInterfaces
22
{
33
using System;
4+
using System.Diagnostics.CodeAnalysis;
45

56
/// <summary>
67
///
78
/// </summary>
9+
[ExcludeFromCodeCoverage]
810
public class OperatorResponse
911
{
1012
#region Properties
@@ -33,6 +35,14 @@ public class OperatorResponse
3335
/// </value>
3436
public String ResponseMessage { get; set; }
3537

38+
/// <summary>
39+
/// Gets or sets a value indicating whether this instance is successful.
40+
/// </summary>
41+
/// <value>
42+
/// <c>true</c> if this instance is successful; otherwise, <c>false</c>.
43+
/// </value>
44+
public Boolean IsSuccessful { get; set; }
45+
3646
#endregion
3747
}
3848
}

TransactionProcessor.BusinessLogic/OperatorInterfaces/SafaricomPinless/SafaricomConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
namespace TransactionProcessor.BusinessLogic.OperatorInterfaces.SafaricomPinless
22
{
33
using System;
4+
using System.Diagnostics.CodeAnalysis;
45

56
/// <summary>
67
///
78
/// </summary>
9+
[ExcludeFromCodeCoverage]
810
public class SafaricomConfiguration
911
{
1012
#region Properties

TransactionProcessor.BusinessLogic/OperatorInterfaces/SafaricomPinless/SafaricomPinlessProxy.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ private OperatorResponse CreateFrom(String responseContent)
135135
return new OperatorResponse
136136
{
137137
AuthorisationCode = "ABCD1234",
138-
ResponseCode = cl.TXNSTATUS.ToString(),
139-
ResponseMessage = cl.MESSAGE
138+
ResponseCode = cl.TransactionStatus.ToString(),
139+
ResponseMessage = cl.Message,
140+
IsSuccessful = cl.TransactionStatus == 200
141+
140142
};
141143
}
142144

TransactionProcessor.BusinessLogic/OperatorInterfaces/SafaricomPinless/SafaricomResponse.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.ComponentModel;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Xml.Serialization;
67

78
/// <summary>
@@ -11,6 +12,7 @@
1112
[DesignerCategory("code")]
1213
[XmlType(AnonymousType = true)]
1314
[XmlRoot(Namespace = "", IsNullable = false, ElementName = "COMMAND")]
15+
[ExcludeFromCodeCoverage]
1416
public class SafaricomResponse
1517
{
1618
#region Properties
@@ -22,7 +24,7 @@ public class SafaricomResponse
2224
/// The type.
2325
/// </value>
2426
[XmlElement(ElementName = "TYPE")]
25-
public String TYPE { get; set; }
27+
public String TransactionType { get; set; }
2628

2729
/// <summary>
2830
/// Gets or sets the txnstatus.
@@ -31,7 +33,7 @@ public class SafaricomResponse
3133
/// The txnstatus.
3234
/// </value>
3335
[XmlElement(ElementName = "TXNSTATUS")]
34-
public Int32 TXNSTATUS { get; set; }
36+
public Int32 TransactionStatus { get; set; }
3537

3638
/// <summary>
3739
/// Gets or sets the date.
@@ -40,7 +42,7 @@ public class SafaricomResponse
4042
/// The date.
4143
/// </value>
4244
[XmlElement(ElementName = "DATE")]
43-
public String DATE { get; set; }
45+
public String TransactionDate { get; set; }
4446

4547
/// <summary>
4648
/// Gets or sets the extrefnum.
@@ -49,7 +51,7 @@ public class SafaricomResponse
4951
/// The extrefnum.
5052
/// </value>
5153
[XmlElement(ElementName = "EXTREFNUM")]
52-
public String EXTREFNUM { get; set; }
54+
public String ExternalReferenceNumber { get; set; }
5355

5456
/// <summary>
5557
/// Gets or sets the txnid.
@@ -58,7 +60,7 @@ public class SafaricomResponse
5860
/// The txnid.
5961
/// </value>
6062
[XmlElement(ElementName = "TXNID")]
61-
public String TXNID { get; set; }
63+
public String TransactionId { get; set; }
6264

6365
/// <summary>
6466
/// Gets or sets the message.
@@ -67,7 +69,7 @@ public class SafaricomResponse
6769
/// The message.
6870
/// </value>
6971
[XmlElement(ElementName = "MESSAGE")]
70-
public String MESSAGE { get; set; }
72+
public String Message { get; set; }
7173

7274
#endregion
7375
}

TransactionProcessor.Testing/TestData.cs

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

55
namespace TransactionProcessor.Testing
66
{
7+
using BusinessLogic.OperatorInterfaces.SafaricomPinless;
78
using BusinessLogic.Requests;
89
using BusinessLogic.Services;
910
using EstateManagement.DataTransferObjects.Responses;
@@ -197,5 +198,26 @@ public static String GetResponseCodeMessage(TransactionResponseCode transactionR
197198
{
198199
return transactionResponseCode.ToString();
199200
}
201+
202+
public static SafaricomConfiguration SafaricomConfiguration = new SafaricomConfiguration
203+
{
204+
ExtCode = "ExtCode1",
205+
LoginId = "LoginId",
206+
MSISDN = "123456789",
207+
Password = "Password",
208+
Url = "http://localhost",
209+
Pin = "1234"
210+
};
211+
212+
public static MerchantResponse Merchant = new MerchantResponse
213+
{
214+
EstateId = TestData.EstateId,
215+
MerchantId = TestData.MerchantId,
216+
MerchantName = TestData.MerchantName
217+
};
218+
219+
public static String SuccessfulSafaricomTopup = "<COMMAND xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><TYPE>EXRCTRFRESP</TYPE><TXNSTATUS>200</TXNSTATUS><DATE>02-JUL-2018</DATE><EXTREFNUM>100022814</EXTREFNUM><TXNID>20200314231322847</TXNID><MESSAGE>Topup Successful</MESSAGE></COMMAND>";
220+
221+
public static String FailedSafaricomTopup = "<COMMAND xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><TYPE>EXRCTRFRESP</TYPE><TXNSTATUS>500</TXNSTATUS><DATE>02-JUL-2018</DATE><EXTREFNUM>100022814</EXTREFNUM><TXNID>20200314231322847</TXNID><MESSAGE>Topup failed</MESSAGE></COMMAND>";
200222
}
201223
}

0 commit comments

Comments
 (0)