diff --git a/TransactionProcessor.BusinessLogic.Tests/OperatorInterfaces/PataPawaPostPayProxyTests.cs b/TransactionProcessor.BusinessLogic.Tests/OperatorInterfaces/PataPawaPostPayProxyTests.cs index 8ee96467..21abb0a8 100644 --- a/TransactionProcessor.BusinessLogic.Tests/OperatorInterfaces/PataPawaPostPayProxyTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/OperatorInterfaces/PataPawaPostPayProxyTests.cs @@ -9,6 +9,7 @@ namespace TransactionProcessor.BusinessLogic.Tests.OperatorInterfaces using System.Threading; using BusinessLogic.OperatorInterfaces; using Common; + using Microsoft.Data.SqlClient; using Microsoft.Extensions.Caching.Memory; using Moq; using PataPawaPostPay; @@ -17,24 +18,37 @@ namespace TransactionProcessor.BusinessLogic.Tests.OperatorInterfaces using TransactionProcessor.BusinessLogic.OperatorInterfaces.PataPawaPostPay; using Xunit; - public class PataPawaPostPayProxyTests - { + public class PataPawaPostPayProxyTests{ + private readonly Mock PataPawaPostPayService; + + private readonly Mock PataPawaPostPayServiceClient; + + private readonly Func ChannelResolver; + + private readonly PataPawaPostPayProxy PataPawaPostPayProxy; + + private readonly IMemoryCache MemoryCache; + + public PataPawaPostPayProxyTests(){ + PataPawaPostPayService = new Mock(); + PataPawaPostPayServiceClient = new Mock(); + + ChannelResolver = (client, + clientName, + s) => { + return PataPawaPostPayService.Object; + }; + + MemoryCache = new MemoryCache(new MemoryCacheOptions()); + PataPawaPostPayProxy = new PataPawaPostPayProxy(PataPawaPostPayServiceClient.Object, ChannelResolver, TestData.PataPawaPostPaidConfiguration, this.MemoryCache); + } + [Fact] public async Task PataPawaPostPayProxy_ProcessLogonMessage_SuccessfulResponse_MessageIsProcessed() { - Mock< IPataPawaPostPayService> service = new Mock(); - service.Setup(s => s.getLoginRequestAsync(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.PataPawaPostPaidSuccessfulLoginResponse); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); + PataPawaPostPayService.Setup(s => s.getLoginRequestAsync(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.PataPawaPostPaidSuccessfulLoginResponse); - OperatorResponse logonResponse = await proxy.ProcessLogonMessage("", CancellationToken.None); + OperatorResponse logonResponse = await PataPawaPostPayProxy.ProcessLogonMessage("", CancellationToken.None); logonResponse.ShouldNotBeNull(); logonResponse.IsSuccessful.ShouldBeTrue(); @@ -51,19 +65,9 @@ public async Task PataPawaPostPayProxy_ProcessLogonMessage_SuccessfulResponse_Me [Fact] public async Task PataPawaPostPayProxy_ProcessLogonMessage_FailedResponse_MessageIsProcessed() { - Mock service = new Mock(); - service.Setup(s => s.getLoginRequestAsync(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.PataPawaPostPaidFailedLoginResponse); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); - - OperatorResponse logonResponse = await proxy.ProcessLogonMessage(TestData.TokenResponse().AccessToken, CancellationToken.None); + PataPawaPostPayService.Setup(s => s.getLoginRequestAsync(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.PataPawaPostPaidFailedLoginResponse); + + OperatorResponse logonResponse = await this.PataPawaPostPayProxy.ProcessLogonMessage(TestData.TokenResponse().AccessToken, CancellationToken.None); logonResponse.ShouldNotBeNull(); logonResponse.IsSuccessful.ShouldBeFalse(); @@ -77,29 +81,18 @@ public async Task PataPawaPostPayProxy_ProcessLogonMessage_FailedResponse_Messag [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_SuccessfulResponse_MessageIsProcessed() { - Mock service = new Mock(); - service.Setup(s => s.getVerifyRequestAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .ReturnsAsync(TestData.PataPawaPostPaidSuccessfulVerifyAccountResponse); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); + PataPawaPostPayService.Setup(s => s.getVerifyRequestAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.PataPawaPostPaidSuccessfulVerifyAccountResponse); + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); - - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount(), - CancellationToken.None); + OperatorResponse saleResponse = await this.PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount(), + CancellationToken.None); saleResponse.ShouldNotBeNull(); saleResponse.IsSuccessful.ShouldBeTrue(); @@ -116,58 +109,36 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_Successf [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_FailedLogon_ErrorIsThrown() { - Mock service = new Mock(); - service.Setup(s => s.getVerifyRequestAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .ReturnsAsync(TestData.PataPawaPostPaidSuccessfulVerifyAccountResponse); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidFailedLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); - + this.PataPawaPostPayService.Setup(s => s.getVerifyRequestAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.PataPawaPostPaidSuccessfulVerifyAccountResponse); + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidFailedLoginOperatorResponse); + ArgumentNullException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount(), - CancellationToken.None); + OperatorResponse saleResponse = await this.PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount(), + CancellationToken.None); }); ex.ParamName.ShouldBe("PataPawaPostPaidAPIKey"); } [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_MissingMessageTypeFromMetadata_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); ArgumentNullException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount_NoMessageType(), - CancellationToken.None); + OperatorResponse saleResponse = await this.PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount_NoMessageType(), + CancellationToken.None); }); ex.ParamName.ShouldBe("PataPawaPostPaidMessageType"); } @@ -175,28 +146,17 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_MissingM [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_MissingCustomerAccountNumberFromMetadata_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); - + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); + ArgumentNullException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount_NoCustomerAccountNumber(), - CancellationToken.None); + OperatorResponse saleResponse = await this.PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount_NoCustomerAccountNumber(), + CancellationToken.None); }); ex.ParamName.ShouldBe("CustomerAccountNumber"); } @@ -204,28 +164,17 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_MissingC [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_InvalidMessageType_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); ArgumentOutOfRangeException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount(pataPawaPostPaidMessageType:"Unknown"), - CancellationToken.None); + OperatorResponse saleResponse = await this.PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount(pataPawaPostPaidMessageType:"Unknown"), + CancellationToken.None); }); ex.ParamName.ShouldBe("PataPawaPostPaidMessageType"); @@ -234,30 +183,19 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_InvalidM [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_RequestFailedAtHost_ErrorIsThrown() { - Mock service = new Mock(); - service.Setup(s => s.getVerifyRequestAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .ReturnsAsync(TestData.PataPawaPostPaidFailedVerifyAccountResponse); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); - + this.PataPawaPostPayService.Setup(s => s.getVerifyRequestAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.PataPawaPostPaidFailedVerifyAccountResponse); + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); + Exception ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount(customerAccountNumber: TestData.PataPawaPostPaidAccountNumber), - CancellationToken.None); + OperatorResponse saleResponse = await this.PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaVerifyAccount(customerAccountNumber: TestData.PataPawaPostPaidAccountNumber), + CancellationToken.None); }); ex.Message.ShouldBe($"Error verifying account number {TestData.PataPawaPostPaidAccountNumber}"); } @@ -265,30 +203,19 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_VerifyAccount_RequestF [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_SuccessfulResponse_MessageIsProcessed() { - Mock service = new Mock(); - service.Setup(s => s.getPayBillRequestAsync(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny())) - .ReturnsAsync(TestData.PataPawaPostPaidSuccessfulProcessBillResponse); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); - - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaProcessBill(), - CancellationToken.None); + this.PataPawaPostPayService.Setup(s => s.getPayBillRequestAsync(It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.PataPawaPostPaidSuccessfulProcessBillResponse); + this.MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); + + OperatorResponse saleResponse = await this.PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaProcessBill(), + CancellationToken.None); saleResponse.ShouldNotBeNull(); saleResponse.IsSuccessful.ShouldBeTrue(); @@ -300,56 +227,34 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_Successful [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_FailedLogon_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidFailedLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); + this.MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidFailedLoginOperatorResponse); ArgumentNullException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaProcessBill(), - CancellationToken.None); + OperatorResponse saleResponse = await this.PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaProcessBill(), + CancellationToken.None); }); ex.ParamName.ShouldBe("PataPawaPostPaidAPIKey"); } [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_MissingMessageTypeFromMetadata_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); + this.MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); ArgumentNullException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaProcessBill_NoMessageType(), - CancellationToken.None); + OperatorResponse saleResponse = await PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaProcessBill_NoMessageType(), + CancellationToken.None); }); ex.ParamName.ShouldBe("PataPawaPostPaidMessageType"); } @@ -357,28 +262,17 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_MissingMes [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_MissingCustomerAccountNumberFromMetadata_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); ArgumentNullException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaProcessBill_NoCustomerAccountNumber(), - CancellationToken.None); + OperatorResponse saleResponse = await PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaProcessBill_NoCustomerAccountNumber(), + CancellationToken.None); }); ex.ParamName.ShouldBe("CustomerAccountNumber"); } @@ -386,28 +280,17 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_MissingCus [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_MissingMobileNumberFromMetadata_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); ArgumentNullException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaProcessBill_NoMobileNumber(), - CancellationToken.None); + OperatorResponse saleResponse = await PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaProcessBill_NoMobileNumber(), + CancellationToken.None); }); ex.ParamName.ShouldBe("MobileNumber"); } @@ -415,28 +298,17 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_MissingMob [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_MissingCustomerNameFromMetadata_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); ArgumentNullException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaProcessBill_NoCustomerName(), - CancellationToken.None); + OperatorResponse saleResponse = await PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaProcessBill_NoCustomerName(), + CancellationToken.None); }); ex.ParamName.ShouldBe("CustomerName"); } @@ -444,28 +316,17 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_MissingCus [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_MissingAmountFromMetadata_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); - + this.MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); + ArgumentNullException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaProcessBill_NoAmount(), - CancellationToken.None); + OperatorResponse saleResponse = await PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaProcessBill_NoAmount(), + CancellationToken.None); }); ex.ParamName.ShouldBe("Amount"); } @@ -473,28 +334,17 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_MissingAmo [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_InvalidAmountFromMetadata_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); ArgumentOutOfRangeException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaProcessBill(pataPawaPostPaidAmount:"A1"), - CancellationToken.None); + OperatorResponse saleResponse = await PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaProcessBill(pataPawaPostPaidAmount:"A1"), + CancellationToken.None); }); ex.ParamName.ShouldBe("Amount"); } @@ -502,28 +352,17 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_InvalidAmo [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_InvalidMessageType_ErrorIsThrown() { - Mock service = new Mock(); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); - + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); + ArgumentOutOfRangeException ex = Should.Throw(async () => { - OperatorResponse saleResponse = await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaProcessBill(pataPawaPostPaidMessageType: "Unknown"), - CancellationToken.None); + OperatorResponse saleResponse = await PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaProcessBill(pataPawaPostPaidMessageType: "Unknown"), + CancellationToken.None); }); ex.ParamName.ShouldBe("PataPawaPostPaidMessageType"); } @@ -531,32 +370,20 @@ public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_InvalidMes [Fact] public async Task PataPawaPostPayProxy_ProcessSaleMessage_ProcessBill_RequestFailedAtHost_ErrorThrown() { - Mock service = new Mock(); - service.Setup(s => s.getPayBillRequestAsync(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny())) - .ReturnsAsync(TestData.PataPawaPostPaidFailedProcessBillResponse); - Mock serviceClient = new Mock(); - - Func channelResolver = (client, - s) => { - return service.Object; - }; - PataPawaPostPaidConfiguration configuration = TestData.PataPawaPostPaidConfiguration; - IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); - - PataPawaPostPayProxy proxy = new PataPawaPostPayProxy(serviceClient.Object, channelResolver, configuration, memoryCache); - + this.PataPawaPostPayService.Setup(s => s.getPayBillRequestAsync(It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.PataPawaPostPaidFailedProcessBillResponse); + MemoryCache.Set("PataPawaPostPayLogon", TestData.PataPawaPostPaidSuccessfulLoginOperatorResponse); Exception ex = Should.Throw(async () => { - await proxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, - TestData.TransactionId, - TestData.OperatorIdentifier1, - TestData.Merchant, - TestData.TransactionDateTime, - TestData.TransactionReference, - TestData.AdditionalTransactionMetaDataForPataPawaProcessBill(customerAccountNumber:TestData.PataPawaPostPaidAccountNumber), - CancellationToken.None); + await PataPawaPostPayProxy.ProcessSaleMessage(TestData.TokenResponse().AccessToken, + TestData.TransactionId, + TestData.OperatorIdentifier1, + TestData.Merchant, + TestData.TransactionDateTime, + TestData.TransactionReference, + TestData.AdditionalTransactionMetaDataForPataPawaProcessBill(customerAccountNumber:TestData.PataPawaPostPaidAccountNumber), + CancellationToken.None); }); ex.Message.ShouldBe($"Error paying bill for account number {TestData.PataPawaPostPaidAccountNumber}"); diff --git a/TransactionProcessor.BusinessLogic.Tests/OperatorInterfaces/SafaricomPinlessProxyTests.cs b/TransactionProcessor.BusinessLogic.Tests/OperatorInterfaces/SafaricomPinlessProxyTests.cs index 81a65849..5a7c8801 100644 --- a/TransactionProcessor.BusinessLogic.Tests/OperatorInterfaces/SafaricomPinlessProxyTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/OperatorInterfaces/SafaricomPinlessProxyTests.cs @@ -12,12 +12,17 @@ namespace TransactionProcessor.BusinessLogic.Tests.OperatorInterfaces using BusinessLogic.OperatorInterfaces.SafaricomPinless; using Moq; using Moq.Protected; + using Shared.Logger; using Shouldly; using Testing; using Xunit; public class SafaricomPinlessProxyTests { + public SafaricomPinlessProxyTests(){ + Logger.Initialise(NullLogger.Instance); + } + [Fact] public async Task SafaricomPinlessProxy_ProcessLogonMessage_NullIsReturned() { HttpResponseMessage responseMessage = new HttpResponseMessage diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs index 10e7ddc6..c3d2aaf8 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs @@ -337,8 +337,8 @@ public async Task TransactionDomainService_ProcessSaleTransaction_OperatorProxyT response.EstateId.ShouldBe(TestData.EstateId); response.MerchantId.ShouldBe(TestData.MerchantId); - response.ResponseCode.ShouldBe("0000"); - response.ResponseMessage.ShouldBe("SUCCESS"); + response.ResponseCode.ShouldBe("1010"); + response.ResponseMessage.ShouldBe("OPERATOR COMMS ERROR"); response.TransactionId.ShouldBe(TestData.TransactionId); } diff --git a/TransactionProcessor.BusinessLogic/OperatorInterfaces/PataPawaPostPay/PataPawaPostPayProxy.cs b/TransactionProcessor.BusinessLogic/OperatorInterfaces/PataPawaPostPay/PataPawaPostPayProxy.cs index f6caf8b8..6bd9740e 100644 --- a/TransactionProcessor.BusinessLogic/OperatorInterfaces/PataPawaPostPay/PataPawaPostPayProxy.cs +++ b/TransactionProcessor.BusinessLogic/OperatorInterfaces/PataPawaPostPay/PataPawaPostPayProxy.cs @@ -20,14 +20,14 @@ public class PataPawaPostPayProxy : IOperatorProxy private readonly PataPawaPostPayServiceClient ServiceClient; - private readonly Func ChannelResolver; + private readonly Func ChannelResolver; #endregion #region Constructors public PataPawaPostPayProxy(PataPawaPostPayServiceClient serviceClient, - Func channelResolver, + Func channelResolver, PataPawaPostPaidConfiguration configuration, IMemoryCache memoryCache) { this.ServiceClient = serviceClient; @@ -42,7 +42,7 @@ public PataPawaPostPayProxy(PataPawaPostPayServiceClient serviceClient, public async Task ProcessLogonMessage(String accessToken, CancellationToken cancellationToken) { - IPataPawaPostPayService channel = this.ChannelResolver(this.ServiceClient, this.Configuration.Url); + IPataPawaPostPayService channel = this.ChannelResolver(this.ServiceClient, "PataPawaPostPay", this.Configuration.Url); login logonResponse = await channel.getLoginRequestAsync(this.Configuration.Username, this.Configuration.Password); if (logonResponse.status != 0) { return new OperatorResponse { @@ -146,13 +146,13 @@ private async Task PerformProcessBillTransaction(String accoun } Decimal operatorTransactionAmount = amountAsDecimal * 100; - IPataPawaPostPayService channel = this.ChannelResolver(this.ServiceClient, this.Configuration.Url); + IPataPawaPostPayService channel = this.ChannelResolver(this.ServiceClient, "PataPawaPostPay", this.Configuration.Url); paybill payBillResponse = await channel.getPayBillRequestAsync(this.Configuration.Username, - apiKey, - accountNumber, - mobileNumber, - customerName, - operatorTransactionAmount); + apiKey, + accountNumber, + mobileNumber, + customerName, + operatorTransactionAmount); if (payBillResponse.status != 0) { throw new Exception($"Error paying bill for account number {accountNumber}"); @@ -169,7 +169,7 @@ private async Task PerformProcessBillTransaction(String accoun private async Task PerformVerifyAccountTransaction(String accountNumber, String apiKey) { - IPataPawaPostPayService channel = this.ChannelResolver(this.ServiceClient, this.Configuration.Url); + IPataPawaPostPayService channel = this.ChannelResolver(this.ServiceClient, "PataPawaPostPay", this.Configuration.Url); verify verifyResponse = await channel.getVerifyRequestAsync(this.Configuration.Username, apiKey, accountNumber); if (String.IsNullOrEmpty(verifyResponse.account_name)) { diff --git a/TransactionProcessor.BusinessLogic/OperatorInterfaces/SafaricomPinless/SafaricomPinlessProxy.cs b/TransactionProcessor.BusinessLogic/OperatorInterfaces/SafaricomPinless/SafaricomPinlessProxy.cs index f7cbdca1..c4295f33 100644 --- a/TransactionProcessor.BusinessLogic/OperatorInterfaces/SafaricomPinless/SafaricomPinlessProxy.cs +++ b/TransactionProcessor.BusinessLogic/OperatorInterfaces/SafaricomPinless/SafaricomPinlessProxy.cs @@ -12,6 +12,7 @@ using System.Xml.Serialization; using Common; using EstateManagement.DataTransferObjects.Responses; + using Shared.Logger; /// /// @@ -104,6 +105,8 @@ public async Task ProcessSaleMessage(String accessToken, String requestUrl = this.BuildRequest(transactionDateTime, transactionReference, customerMsisdn, operatorTransactionAmount); + Logger.LogInformation($"Sending message to Safaricom [{requestUrl}]"); + // Concatenate the request message HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri(requestUrl)); @@ -119,6 +122,8 @@ public async Task ProcessSaleMessage(String accessToken, // Get the response String responseContent = await responseMessage.Content.ReadAsStringAsync(); + Logger.LogInformation($"Received response message from Safaricom [{responseContent}]"); + return this.CreateFrom(responseContent); } diff --git a/TransactionProcessor.BusinessLogic/OperatorInterfaces/VoucherManagement/VoucherManagementProxy.cs b/TransactionProcessor.BusinessLogic/OperatorInterfaces/VoucherManagement/VoucherManagementProxy.cs index 6b91a161..bfaa54e3 100644 --- a/TransactionProcessor.BusinessLogic/OperatorInterfaces/VoucherManagement/VoucherManagementProxy.cs +++ b/TransactionProcessor.BusinessLogic/OperatorInterfaces/VoucherManagement/VoucherManagementProxy.cs @@ -9,10 +9,12 @@ namespace TransactionProcessor.BusinessLogic.OperatorInterfaces.VoucherManagemen using System.Threading.Tasks; using Common; using EstateManagement.DataTransferObjects.Responses; + using IdentityModel.Client; using MediatR; //using global::VoucherManagement.Client; //using global::VoucherManagement.DataTransferObjects; using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal; + using Microsoft.Identity.Client; using Models; using Requests; @@ -84,6 +86,7 @@ public async Task ProcessSaleMessage(String accessToken, amountAsDecimal, recipientEmail, recipientMobile); + IssueVoucherResponse response = await this.Mediator.Send(request, cancellationToken); if (response != null) { diff --git a/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs b/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs index b8ae1a61..2805217a 100644 --- a/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs @@ -302,7 +302,7 @@ await this.ValidateSaleTransaction(estateId, TransactionResponseCode transactionResponseCode = TransactionResponseCode.OperatorCommsError; String responseMessage = "OPERATOR COMMS ERROR"; - transactionAggregate.DeclineTransactionLocally(((Int32)validationResult.responseCode).ToString().PadLeft(4, '0'), validationResult.responseMessage); + transactionAggregate.DeclineTransactionLocally(((Int32)transactionResponseCode).ToString().PadLeft(4, '0'), responseMessage); } else { if (operatorResponse.IsSuccessful) { diff --git a/TransactionProcessor/Bootstrapper/OperatorRegistry.cs b/TransactionProcessor/Bootstrapper/OperatorRegistry.cs index df4fb995..8c64b67d 100644 --- a/TransactionProcessor/Bootstrapper/OperatorRegistry.cs +++ b/TransactionProcessor/Bootstrapper/OperatorRegistry.cs @@ -4,12 +4,16 @@ using System.Diagnostics.CodeAnalysis; using System.Net.Http; using System.ServiceModel; + using System.ServiceModel.Channels; using System.ServiceModel.Description; + using System.ServiceModel.Dispatcher; using BusinessLogic.OperatorInterfaces; using BusinessLogic.OperatorInterfaces.PataPawaPostPay; using BusinessLogic.OperatorInterfaces.SafaricomPinless; using BusinessLogic.OperatorInterfaces.VoucherManagement; + using Common; using Lamar; + using Microsoft.Data.SqlClient; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -40,8 +44,9 @@ public OperatorRegistry() this.For().Add().Named("PataPawaPostPay").Singleton(); this.For().Add().Named("Voucher").Singleton(); - this.AddTransient>(context => (client, + this.AddTransient>(context => (client,clientName, url) => { + client.Endpoint.SetTraceLogging(clientName); IPataPawaPostPayService channel = client.ChannelFactory.CreateChannel(new EndpointAddress(url)); return channel; @@ -50,7 +55,6 @@ public OperatorRegistry() this.AddTransient>(context => operatorIdentifier => { return Startup.Container.GetInstance(operatorIdentifier); - }); } diff --git a/TransactionProcessor/Common/ClientMessageLogger.cs b/TransactionProcessor/Common/ClientMessageLogger.cs new file mode 100644 index 00000000..af6a988c --- /dev/null +++ b/TransactionProcessor/Common/ClientMessageLogger.cs @@ -0,0 +1,37 @@ +namespace TransactionProcessor.Common; + +using System; +using System.ServiceModel; +using System.ServiceModel.Channels; +using System.ServiceModel.Dispatcher; +using Shared.Logger; + +internal sealed class ClientMessageLogger : + IClientMessageInspector{ + #region Fields + + private readonly String ClientName; + + #endregion + + #region Constructors + + public ClientMessageLogger(String clientName){ + this.ClientName = clientName; + } + + #endregion + + #region Methods + + public void AfterReceiveReply(ref Message reply, Object correlationState){ + Logger.LogInformation($"Received SOAP reply from {this.ClientName}:\r\n{reply}"); + } + + public Object BeforeSendRequest(ref Message request, IClientChannel channel){ + Logger.LogInformation($"Sending SOAP request to {this.ClientName}:\r\n{request}"); + return null; + } + + #endregion +} \ No newline at end of file diff --git a/TransactionProcessor/Common/ClientMessageLoggingBehavior.cs b/TransactionProcessor/Common/ClientMessageLoggingBehavior.cs new file mode 100644 index 00000000..a974b6ee --- /dev/null +++ b/TransactionProcessor/Common/ClientMessageLoggingBehavior.cs @@ -0,0 +1,40 @@ +namespace TransactionProcessor.Common; + +using System; +using System.ServiceModel.Channels; +using System.ServiceModel.Description; +using System.ServiceModel.Dispatcher; + +internal sealed class ClientMessageLoggingBehavior : + IEndpointBehavior{ + #region Fields + + private readonly String ClientName; + + #endregion + + #region Constructors + + public ClientMessageLoggingBehavior(String clientName){ + this.ClientName = clientName; + } + + #endregion + + #region Methods + + public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters){ + } + + public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime){ + clientRuntime.ClientMessageInspectors.Add(new ClientMessageLogger(this.ClientName)); + } + + public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher){ + } + + public void Validate(ServiceEndpoint endpoint){ + } + + #endregion +} \ No newline at end of file diff --git a/TransactionProcessor/Common/ServiceEndpointExtensions.cs b/TransactionProcessor/Common/ServiceEndpointExtensions.cs new file mode 100644 index 00000000..5e869c12 --- /dev/null +++ b/TransactionProcessor/Common/ServiceEndpointExtensions.cs @@ -0,0 +1,16 @@ +namespace TransactionProcessor.Common; + +using System; +using System.ServiceModel.Description; + +public static class ServiceEndpointExtensions{ + #region Methods + + public static void SetTraceLogging(this ServiceEndpoint serviceEndpoint, String clientName){ + if (serviceEndpoint.EndpointBehaviors.Contains(typeof(ClientMessageLoggingBehavior)) == false){ + serviceEndpoint.EndpointBehaviors.Add(new ClientMessageLoggingBehavior(clientName)); + } + } + + #endregion +} \ No newline at end of file