Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace TransactionProcessor.BusinessLogic.Tests.OperatorInterfaces
{
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using BusinessLogic.OperatorInterfaces;
using BusinessLogic.OperatorInterfaces.SafaricomPinless;
using Moq;
using Moq.Protected;
using Shouldly;
using Testing;
using Xunit;

public class SafaricomPinlessProxyTests
{
[Fact]
public async Task SafaricomPinlessProxy_ProcessSaleMessage_TopupSuccessful_SaleMessageIsProcessed()
{
HttpResponseMessage responseMessage = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(TestData.SuccessfulSafaricomTopup)
};

SafaricomConfiguration safaricomConfiguration = TestData.SafaricomConfiguration;
HttpClient httpClient = SetupMockHttpClient(responseMessage);

IOperatorProxy safaricomPinlessproxy = new SafaricomPinlessProxy(safaricomConfiguration, httpClient);

OperatorResponse operatorResponse = await safaricomPinlessproxy.ProcessSaleMessage(TestData.TransactionId,
TestData.Merchant,
TestData.TransactionDateTime,
TestData.AdditionalTransactionMetaData,
CancellationToken.None);

operatorResponse.ShouldNotBeNull();
operatorResponse.IsSuccessful.ShouldBeTrue();
operatorResponse.ResponseCode.ShouldBe("200");
operatorResponse.ResponseMessage.ShouldBe("Topup Successful");
}

[Fact]
public async Task SafaricomPinlessProxy_ProcessSaleMessage_TopupFailed_SaleMessageIsProcessed()
{
HttpResponseMessage responseMessage = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(TestData.FailedSafaricomTopup)
};

SafaricomConfiguration safaricomConfiguration = TestData.SafaricomConfiguration;
HttpClient httpClient = SetupMockHttpClient(responseMessage);

IOperatorProxy safaricomPinlessproxy = new SafaricomPinlessProxy(safaricomConfiguration, httpClient);

OperatorResponse operatorResponse = await safaricomPinlessproxy.ProcessSaleMessage(TestData.TransactionId,
TestData.Merchant,
TestData.TransactionDateTime,
TestData.AdditionalTransactionMetaData,
CancellationToken.None);

operatorResponse.ShouldNotBeNull();
operatorResponse.IsSuccessful.ShouldBeFalse();
operatorResponse.ResponseCode.ShouldBe("500");
operatorResponse.ResponseMessage.ShouldBe("Topup failed");
}

[Fact]
public async Task SafaricomPinlessProxy_ProcessSaleMessage_FailedToSend_ErrorThrown()
{
HttpResponseMessage responseMessage = new HttpResponseMessage
{
StatusCode = HttpStatusCode.InternalServerError
};

SafaricomConfiguration safaricomConfiguration = TestData.SafaricomConfiguration;
HttpClient httpClient = SetupMockHttpClient(responseMessage);

IOperatorProxy safaricomPinlessproxy = new SafaricomPinlessProxy(safaricomConfiguration, httpClient);

Should.Throw<Exception>(async () =>
{
await safaricomPinlessproxy.ProcessSaleMessage(TestData.TransactionId,
TestData.Merchant,
TestData.TransactionDateTime,
TestData.AdditionalTransactionMetaData,
CancellationToken.None);
});
}

private HttpClient SetupMockHttpClient(HttpResponseMessage responseMessage)
{
Mock<HttpMessageHandler> handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
handlerMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(responseMessage);

var httpClient = new HttpClient(handlerMock.Object)
{
BaseAddress = new Uri("http://test.com")
};

return httpClient;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services
{
using System.Threading;
using System.Threading.Tasks;
using BusinessLogic.OperatorInterfaces;
using BusinessLogic.Services;
using EstateManagement.Client;
using Microsoft.Extensions.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace TransactionProcessor.BusinessLogic.OperatorInterfaces
{
using System;
using System.Diagnostics.CodeAnalysis;

/// <summary>
///
/// </summary>
[ExcludeFromCodeCoverage]
public class OperatorResponse
{
#region Properties
Expand Down Expand Up @@ -33,6 +35,14 @@ public class OperatorResponse
/// </value>
public String ResponseMessage { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this instance is successful.
/// </summary>
/// <value>
/// <c>true</c> if this instance is successful; otherwise, <c>false</c>.
/// </value>
public Boolean IsSuccessful { get; set; }

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace TransactionProcessor.BusinessLogic.OperatorInterfaces.SafaricomPinless
{
using System;
using System.Diagnostics.CodeAnalysis;

/// <summary>
///
/// </summary>
[ExcludeFromCodeCoverage]
public class SafaricomConfiguration
{
#region Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ private OperatorResponse CreateFrom(String responseContent)
return new OperatorResponse
{
AuthorisationCode = "ABCD1234",
ResponseCode = cl.TXNSTATUS.ToString(),
ResponseMessage = cl.MESSAGE
ResponseCode = cl.TransactionStatus.ToString(),
ResponseMessage = cl.Message,
IsSuccessful = cl.TransactionStatus == 200

};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Xml.Serialization;

/// <summary>
Expand All @@ -11,6 +12,7 @@
[DesignerCategory("code")]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false, ElementName = "COMMAND")]
[ExcludeFromCodeCoverage]
public class SafaricomResponse
{
#region Properties
Expand All @@ -22,7 +24,7 @@ public class SafaricomResponse
/// The type.
/// </value>
[XmlElement(ElementName = "TYPE")]
public String TYPE { get; set; }
public String TransactionType { get; set; }

/// <summary>
/// Gets or sets the txnstatus.
Expand All @@ -31,7 +33,7 @@ public class SafaricomResponse
/// The txnstatus.
/// </value>
[XmlElement(ElementName = "TXNSTATUS")]
public Int32 TXNSTATUS { get; set; }
public Int32 TransactionStatus { get; set; }

/// <summary>
/// Gets or sets the date.
Expand All @@ -40,7 +42,7 @@ public class SafaricomResponse
/// The date.
/// </value>
[XmlElement(ElementName = "DATE")]
public String DATE { get; set; }
public String TransactionDate { get; set; }

/// <summary>
/// Gets or sets the extrefnum.
Expand All @@ -49,7 +51,7 @@ public class SafaricomResponse
/// The extrefnum.
/// </value>
[XmlElement(ElementName = "EXTREFNUM")]
public String EXTREFNUM { get; set; }
public String ExternalReferenceNumber { get; set; }

/// <summary>
/// Gets or sets the txnid.
Expand All @@ -58,7 +60,7 @@ public class SafaricomResponse
/// The txnid.
/// </value>
[XmlElement(ElementName = "TXNID")]
public String TXNID { get; set; }
public String TransactionId { get; set; }

/// <summary>
/// Gets or sets the message.
Expand All @@ -67,7 +69,7 @@ public class SafaricomResponse
/// The message.
/// </value>
[XmlElement(ElementName = "MESSAGE")]
public String MESSAGE { get; set; }
public String Message { get; set; }

#endregion
}
Expand Down
22 changes: 22 additions & 0 deletions TransactionProcessor.Testing/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TransactionProcessor.Testing
{
using BusinessLogic.OperatorInterfaces.SafaricomPinless;
using BusinessLogic.Requests;
using BusinessLogic.Services;
using EstateManagement.DataTransferObjects.Responses;
Expand Down Expand Up @@ -197,5 +198,26 @@ public static String GetResponseCodeMessage(TransactionResponseCode transactionR
{
return transactionResponseCode.ToString();
}

public static SafaricomConfiguration SafaricomConfiguration = new SafaricomConfiguration
{
ExtCode = "ExtCode1",
LoginId = "LoginId",
MSISDN = "123456789",
Password = "Password",
Url = "http://localhost",
Pin = "1234"
};

public static MerchantResponse Merchant = new MerchantResponse
{
EstateId = TestData.EstateId,
MerchantId = TestData.MerchantId,
MerchantName = TestData.MerchantName
};

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>";

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>";
}
}