public async Task CreditNoteHandler_AR_ShouldBeSendCreditNoteInSap_WhenCreditNoteHasNotReasonValid() { var dateTimeProviderMock = new Mock <IDateTimeProvider>(); dateTimeProviderMock.Setup(x => x.GetDateByTimezoneId(It.IsAny <DateTime>(), It.IsAny <string>())) .Returns(new DateTime(2051, 2, 3)); var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations) }; var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.StartSession()) .ReturnsAsync(new SapLoginCookies { B1Session = "session", RouteId = "route" }); sapTaskHandlerMock.Setup(x => x.TryGetInvoiceByInvoiceIdAndOrigin(It.IsAny <int>(), It.IsAny <string>())) .ReturnsAsync(new SapSaleOrderInvoiceResponse { BillingSystemId = 9, CardCode = "CD001", DocumentLines = new List <SapDocumentLineResponse> { new SapDocumentLineResponse { FreeText = "1" }, new SapDocumentLineResponse { FreeText = "2" } } }); var sapServiceSettingsFactoryMock = new Mock <ISapServiceSettingsFactory>(); sapServiceSettingsFactoryMock.Setup(x => x.CreateHandler("AR")) .Returns(sapTaskHandlerMock.Object); var sapConfig = new SapConfig { SapServiceConfigsBySystem = new Dictionary <string, SapServiceConfig> { { "AR", new SapServiceConfig { CompanyDB = "CompanyDb", Password = "******", UserName = "******", BaseServerUrl = "http://123.123.123/", BusinessPartnerConfig = new BusinessPartnerConfig { Endpoint = "BusinessPartners" }, BillingConfig = new BillingConfig { CreditNotesEndpoint = "CreditNotes" } } } } }; var sapConfigMock = new Mock <IOptions <SapConfig> >(); sapConfigMock.Setup(x => x.Value) .Returns(sapConfig); var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); var handler = new CreditNoteHandler( sapConfigMock.Object, Mock.Of <ILogger <CreditNoteHandler> >(), sapServiceSettingsFactoryMock.Object, HttpHelperExtension.GetHttpClientMock("", HttpStatusCode.OK, httpMessageHandlerMock), billingMappers); var sapTask = new SapTask { CreditNoteRequest = new CreditNoteRequest { BillingSystemId = 9, CreditNoteId = 1, Amount = 20 }, TaskType = Enums.SapTaskEnum.CreateCreditNote }; // Act await handler.Handle(sapTask); // Assert var uriForCreateCreditNote = sapConfig.SapServiceConfigsBySystem["AR"].BaseServerUrl + sapConfig.SapServiceConfigsBySystem["AR"].BillingConfig.CreditNotesEndpoint; httpMessageHandlerMock.Protected().Verify("SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>( req => req.Method == HttpMethod.Post && req.RequestUri == new Uri(uriForCreateCreditNote) && req.Content.ReadAsStringAsync().Result.Contains("\"ReturnReason\":-1")), ItExpr.IsAny <CancellationToken>()); }
public async Task BillingRequestHandler_ShouldSendDateOfPaymentNow_WhenPaymentProcessIsExecuted() { var sapTask = new SapTask { BillingRequest = new SapSaleOrderModel { InvoiceId = 1, TransactionApproved = true, BillingSystemId = 2 }, TaskType = Enums.SapTaskEnum.BillingRequest }; var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.TryGetBusinessPartner(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>())) .ReturnsAsync(new SapBusinessPartner { FederalTaxID = string.Empty, CardCode = "2323423" }); sapTaskHandlerMock.Setup(x => x.TryGetInvoiceByInvoiceIdAndOrigin(It.IsAny <int>(), It.IsAny <string>())) .ReturnsAsync((SapSaleOrderInvoiceResponse)null); sapTaskHandlerMock.Setup(x => x.StartSession()) .ReturnsAsync(new SapLoginCookies { B1Session = "session", RouteId = "route" }); var sapServiceSettingsFactoryMock = new Mock <ISapServiceSettingsFactory>(); sapServiceSettingsFactoryMock.Setup(x => x.CreateHandler(It.IsAny <string>())) .Returns(sapTaskHandlerMock.Object); var billingValidations = new List <IBillingValidation> { new BillingForUsValidation(Mock.Of <ILogger <BillingForUsValidation> >()) }; var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var dateTimeProviderMock = new Mock <IDateTimeProvider>(); dateTimeProviderMock.Setup(x => x.GetDateByTimezoneId(It.IsAny <DateTime>(), It.IsAny <string>())) .Returns(new DateTime(2051, 2, 3)); var billingMappers = new List <IBillingMapper> { new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations) }; var sapConfig = new SapConfig { SapServiceConfigsBySystem = new Dictionary <string, SapServiceConfig> { { "US", new SapServiceConfig { CompanyDB = "CompanyDb", Password = "******", UserName = "******", BaseServerUrl = "http://123.123.123/", BusinessPartnerConfig = new BusinessPartnerConfig { Endpoint = "BusinessPartners" }, BillingConfig = new BillingConfig { Endpoint = "Orders", NeedCreateIncomingPayments = true } } } } }; var sapConfigMock = new Mock <IOptions <SapConfig> >(); sapConfigMock.Setup(x => x.Value) .Returns(sapConfig); var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); var handler = new BillingRequestHandler( sapConfigMock.Object, Mock.Of <ILogger <BillingRequestHandler> >(), sapServiceSettingsFactoryMock.Object, HttpHelperExtension.GetHttpClientMock("{\"DocEntry\":3783,\"CardCode\":\"345\"}", HttpStatusCode.OK, httpMessageHandlerMock), billingValidations, billingMappers); await handler.Handle(sapTask); var uriForIncomingPayment = sapConfig.SapServiceConfigsBySystem["US"].BaseServerUrl + sapConfig.SapServiceConfigsBySystem["US"].BillingConfig.IncomingPaymentsEndpoint; httpMessageHandlerMock.Protected().Verify("SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>( req => req.Method == HttpMethod.Post && req.RequestUri == new Uri(uriForIncomingPayment) && req.Content.ReadAsStringAsync().Result.Contains("\"DocDate\":\"2051-02-03\"")), ItExpr.IsAny <CancellationToken>()); }
public async Task BillingRequestHandler_ShouldBeCreateBillingInSap_WhenQueueHasOneValidElement() { var billingValidations = new List <IBillingValidation> { new BillingForArValidation(Mock.Of <ILogger <BillingForArValidation> >()), new BillingForUsValidation(Mock.Of <ILogger <BillingForUsValidation> >()) }; var sapConfigMock = new Mock <IOptions <SapConfig> >(); var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var dateTimeProviderMock = new Mock <IDateTimeProvider>(); dateTimeProviderMock.Setup(x => x.UtcNow) .Returns(new DateTime(2019, 09, 25)); var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations), new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations) }; sapConfigMock.Setup(x => x.Value) .Returns(new SapConfig { SapServiceConfigsBySystem = new Dictionary <string, SapServiceConfig> { { "AR", new SapServiceConfig { CompanyDB = "CompanyDb", Password = "******", UserName = "******", BaseServerUrl = "http://123.123.123/", BusinessPartnerConfig = new BusinessPartnerConfig { Endpoint = "BusinessPartners" }, BillingConfig = new BillingConfig { Endpoint = "Orders" } } } } }); var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); var httpClientFactory = HttpHelperExtension.GetHttpClientMock(string.Empty, HttpStatusCode.OK, httpMessageHandlerMock); var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.StartSession()) .ReturnsAsync(new SapLoginCookies { B1Session = "session", RouteId = "route" }); sapTaskHandlerMock.Setup(x => x.TryGetBusinessPartner(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>())) .ReturnsAsync(new SapBusinessPartner { FederalTaxID = "FederalTaxId", CardCode = "2323423" }); sapTaskHandlerMock.Setup(x => x.TryGetInvoiceByInvoiceIdAndOrigin(It.IsAny <int>(), It.IsAny <string>())) .ReturnsAsync((SapSaleOrderInvoiceResponse)null); var sapServiceSettingsFactoryMock = new Mock <ISapServiceSettingsFactory>(); sapServiceSettingsFactoryMock.Setup(x => x.CreateHandler("AR")).Returns(sapTaskHandlerMock.Object); var handler = new BillingRequestHandler( sapConfigMock.Object, Mock.Of <ILogger <BillingRequestHandler> >(), sapServiceSettingsFactoryMock.Object, httpClientFactory, billingValidations, billingMappers); var result = await handler.Handle(new SapTask { CurrencyRate = new SapCurrencyRate { Currency = "Test" }, BillingRequest = new SapSaleOrderModel { BillingSystemId = 9, CardCode = "CD123" }, TaskType = Enums.SapTaskEnum.BillingRequest }); Assert.True(result.IsSuccessful); Assert.Equal("Creating Billing Request", result.TaskName); }
public async Task BillingRequestHandler_ShouldBeUpdatedBillingInSapAndCreateThePayment_WhenQueueHasOneValidElementAndTransactionApproved() { var billingValidations = new List <IBillingValidation> { new BillingForArValidation(Mock.Of <ILogger <BillingForArValidation> >()), new BillingForUsValidation(Mock.Of <ILogger <BillingForUsValidation> >()) }; var sapConfig = new SapConfig { SapServiceConfigsBySystem = new Dictionary <string, SapServiceConfig> { { "US", new SapServiceConfig { CompanyDB = "CompanyDb", Password = "******", UserName = "******", BaseServerUrl = "http://123.123.123/", BusinessPartnerConfig = new BusinessPartnerConfig { Endpoint = "BusinessPartners" }, BillingConfig = new BillingConfig { Endpoint = "Invoices", NeedCreateIncomingPayments = true, IncomingPaymentsEndpoint = "IncomingPayments" } } } } }; var uriForIncomingPayment = sapConfig.SapServiceConfigsBySystem["US"].BaseServerUrl + sapConfig.SapServiceConfigsBySystem["US"].BillingConfig.IncomingPaymentsEndpoint; var sapConfigMock = new Mock <IOptions <SapConfig> >(); sapConfigMock.Setup(x => x.Value) .Returns(sapConfig); var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var dateTimeProviderMock = new Mock <IDateTimeProvider>(); var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations), new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations) }; var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); var httpClientFactory = HttpHelperExtension.GetHttpClientMock(string.Empty, HttpStatusCode.OK, httpMessageHandlerMock); var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.TryGetInvoiceByInvoiceIdAndOrigin(It.IsAny <int>(), It.IsAny <string>())) .ReturnsAsync(new SapSaleOrderInvoiceResponse { CardCode = "0001", DocEntry = 1, DocTotal = 50 }); sapTaskHandlerMock.Setup(x => x.StartSession()) .ReturnsAsync(new SapLoginCookies { B1Session = "session", RouteId = "route" }); var sapServiceSettingsFactoryMock = new Mock <ISapServiceSettingsFactory>(); sapServiceSettingsFactoryMock.Setup(x => x.CreateHandler(It.IsAny <string>())).Returns(sapTaskHandlerMock.Object); var handler = new BillingRequestHandler( sapConfigMock.Object, Mock.Of <ILogger <BillingRequestHandler> >(), sapServiceSettingsFactoryMock.Object, httpClientFactory, billingValidations, billingMappers); var sapTask = new SapTask { BillingRequest = new SapSaleOrderModel { InvoiceId = 1, TransactionApproved = true, BillingSystemId = 2 }, TaskType = Enums.SapTaskEnum.UpdateBilling }; var result = await handler.Handle(sapTask); Assert.True(result.IsSuccessful); Assert.Equal("Creating/Updating Billing with Payment Request", result.TaskName); sapServiceSettingsFactoryMock.Verify(x => x.CreateHandler(It.IsAny <string>()), Times.Exactly(3)); sapTaskHandlerMock.Verify(x => x.TryGetInvoiceByInvoiceIdAndOrigin(It.IsAny <int>(), It.IsAny <string>()), Times.Once); httpMessageHandlerMock.Protected().Verify("SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Patch), ItExpr.IsAny <CancellationToken>()); httpMessageHandlerMock.Protected().Verify("SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post && req.RequestUri == new Uri(uriForIncomingPayment)), ItExpr.IsAny <CancellationToken>()); }
public async Task BillingRequestHandler_ShouldBeNotCreateBillingInSap_WhenQueueHasOneElementButBusinessPartnerHasFederalTaxIdEmpty() { var billingValidations = new List <IBillingValidation> { new BillingForArValidation(Mock.Of <ILogger <BillingForArValidation> >()), new BillingForUsValidation(Mock.Of <ILogger <BillingForUsValidation> >()) }; var sapConfigMock = new Mock <IOptions <SapConfig> >(); var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var dateTimeProviderMock = new Mock <IDateTimeProvider>(); dateTimeProviderMock.Setup(x => x.UtcNow) .Returns(new DateTime(2019, 09, 25)); var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations), new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations) }; var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); var httpClientFactory = HttpHelperExtension.GetHttpClientMock(string.Empty, HttpStatusCode.OK, httpMessageHandlerMock); var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.TryGetBusinessPartner(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>())) .ReturnsAsync(new SapBusinessPartner { FederalTaxID = string.Empty, CardCode = "2323423" }); sapTaskHandlerMock.Setup(x => x.TryGetInvoiceByInvoiceIdAndOrigin(It.IsAny <int>(), It.IsAny <string>())) .ReturnsAsync((SapSaleOrderInvoiceResponse)null); var sapServiceSettingsFactoryMock = new Mock <ISapServiceSettingsFactory>(); sapServiceSettingsFactoryMock.Setup(x => x.CreateHandler("AR")).Returns(sapTaskHandlerMock.Object); var handler = new BillingRequestHandler( sapConfigMock.Object, Mock.Of <ILogger <BillingRequestHandler> >(), sapServiceSettingsFactoryMock.Object, httpClientFactory, billingValidations, billingMappers); var sapTask = new SapTask { BillingRequest = new SapSaleOrderModel { UserId = 1 }, TaskType = Enums.SapTaskEnum.BillingRequest }; var result = await handler.Handle(sapTask); Assert.False(result.IsSuccessful); Assert.Equal($"Invoice/Sales Order could'n create to SAP because exists an error: 'Failed at generating billing request for the user: {sapTask.BillingRequest.UserId}.'.", result.SapResponseContent); Assert.Equal("Creating Billing Request", result.TaskName); }