public async Task BillingService_ShouldNotBeAddTaskInQueue_WhenCurrencyRateListIsEmpty() { var sapConfigMock = new Mock <IOptions <SapConfig> >(); var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var queuingServiceMock = new Mock <IQueuingService>(); 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 billingService = new BillingService(queuingServiceMock.Object, Mock.Of <IDateTimeProvider>(), Mock.Of <ILogger <BillingService> >(), Mock.Of <ISlackService>(), billingMappers, null); var currencyList = new List <CurrencyRateDto>(); await billingService.SendCurrencyToSap(currencyList); queuingServiceMock.Verify(x => x.AddToTaskQueue(It.IsAny <SapTask>()), Times.Never); }
public async Task BillingService_ShouldBeAddOneTaskInQueue_WhenUpdateBillingRequestHasOneValidElement() { var itemCode = "1.0.1"; var items = new List <BillingItemPlanDescriptionModel> { new BillingItemPlanDescriptionModel { ItemCode = "1.0.1", description = "Test" } }; var billingValidations = new List <IBillingValidation> { new BillingForArValidation(Mock.Of <ILogger <BillingForArValidation> >()), new BillingForUsValidation(Mock.Of <ILogger <BillingForUsValidation> >()) }; var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var sapBillingItemsServiceMock = new Mock <ISapBillingItemsService>(); sapBillingItemsServiceMock.Setup(x => x.GetItemCode(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <bool>())).Returns(itemCode); sapBillingItemsServiceMock.Setup(x => x.GetItems(It.IsAny <int>())).Returns(items); var dateTimeProviderMock = new Mock <IDateTimeProvider>(); dateTimeProviderMock.Setup(x => x.UtcNow) .Returns(new DateTime(2019, 09, 25)); var billingMappers = new List <IBillingMapper> { new BillingForArMapper(sapBillingItemsServiceMock.Object, dateTimeProviderMock.Object, timeZoneConfigurations), new BillingForUsMapper(sapBillingItemsServiceMock.Object, dateTimeProviderMock.Object, timeZoneConfigurations) }; var queuingServiceMock = new Mock <IQueuingService>(); var billingService = new BillingService(queuingServiceMock.Object, dateTimeProviderMock.Object, Mock.Of <ILogger <BillingService> >(), Mock.Of <ISlackService>(), billingMappers, billingValidations, Mock.Of <ISapServiceSettingsFactory>()); var updateBillingRequestList = new UpdatePaymentStatusRequest { BillingSystemId = 9, InvoiceId = 1 }; await billingService.UpdatePaymentStatus(updateBillingRequestList); queuingServiceMock.Verify(x => x.AddToTaskQueue(It.IsAny <SapTask>()), Times.Once); }
public async Task BillingRequestHandler_ShouldBeNotUpdatedBillingInSap_WhenQueueHasOneElementButNotExistsInvoiceInSap() { 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>(); var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations), new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations) }; var httpClientFactory = new Mock <IHttpClientFactory>(); var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.TryGetInvoiceByInvoiceIdAndOrigin(It.IsAny <int>(), It.IsAny <string>())) .ReturnsAsync((SapSaleOrderInvoiceResponse)null); 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.Object, billingValidations, billingMappers); var sapTask = new SapTask { BillingRequest = new SapSaleOrderModel { InvoiceId = 1 }, TaskType = Enums.SapTaskEnum.UpdateBilling }; 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 updating billing request for the invoice: {sapTask.BillingRequest.InvoiceId}.'.", result.SapResponseContent); Assert.Equal("Updating Billing Request", result.TaskName); sapServiceSettingsFactoryMock.Verify(x => x.CreateHandler(It.IsAny <string>()), Times.Once); sapTaskHandlerMock.Verify(x => x.TryGetInvoiceByInvoiceIdAndOrigin(It.IsAny <int>(), It.IsAny <string>()), Times.Once); }
public async Task BillingService_Us_ShouldBeAddInvoiceWithoutPeriodicityProperty_WhenClientIsMonthly() { var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var billingMappers = new List <IBillingMapper> { new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), Mock.Of <IDateTimeProvider>(), timeZoneConfigurations), }; var slackServiceMock = new Mock <ISlackService>(); slackServiceMock.Setup(x => x.SendNotification(It.IsAny <string>())) .Returns(Task.CompletedTask); var queuingServiceMock = new Mock <IQueuingService>(); var billingValidations = new List <IBillingValidation> { new BillingForUsValidation(Mock.Of <ILogger <BillingForUsValidation> >()) }; var billingService = new BillingService(queuingServiceMock.Object, Mock.Of <IDateTimeProvider>(), Mock.Of <ILogger <BillingService> >(), slackServiceMock.Object, billingMappers, billingValidations, Mock.Of <ISapServiceSettingsFactory>()); var billingRequestList = new List <BillingRequest> { new BillingRequest { Id = 1, BillingSystemId = 2, FiscalID = "123", Periodicity = 0, PlanFee = 15 } }; // Act await billingService.CreateBillingRequest(billingRequestList); // Assert queuingServiceMock.Verify(x => x.AddToTaskQueue( It.Is <SapTask>(y => y.BillingRequest.DocumentLines.FirstOrDefault() .FreeText == "$ 15 - Monthly Plan - Period 00 0")), Times.Once); }
public void DateTimeProvider_PredefinedValues_ShouldBeReturnPreviousDateInArgentinaTimezone_WhenTimeZoneIsNotNullAndTheDateAfter0clockAndKingUnspecified() { var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var dateTimeProvider = new DateTimeProvider(); var date = new DateTime(2020, 12, 13, 0, 0, 0, DateTimeKind.Unspecified); var expectedDate = new DateTime(2020, 12, 12, 21, 0, 0); var result = dateTimeProvider.GetDateByTimezoneId(date, timeZoneConfigurations.InvoicesTimeZone); Assert.Equal(expectedDate, result); }
public void DateTimeProvider_PredefinedValues_ShouldBeReturnAnArgumentException_WhenTheKingLocal() { var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var dateTimeProvider = new DateTimeProvider(); var date = new DateTime(2020, 12, 13, 0, 0, 0, DateTimeKind.Local); var ex = Assert.Throws <ArgumentException>(() => dateTimeProvider.GetDateByTimezoneId(date, timeZoneConfigurations.InvoicesTimeZone)); var isExpectedMessage = ex.Message.Contains("The conversion could not be completed because the supplied DateTime did not have the Kind property set correctly"); Assert.True(isExpectedMessage); }
public void DateTimeProvider_ShouldBeReturnPreviousDateInArgentinaTimezone_WhenTimeZoneIsNotNullAndTheDateAfter0clock() { var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var dateTimeProvider = new DateTimeProvider(); var utcDate = new DateTime(2020, 12, 13, 0, 10, 0); var expectedDate = new DateTime(2020, 12, 12, 0, 10, 0); var result = dateTimeProvider.GetDateByTimezoneId(utcDate, timeZoneConfigurations.InvoicesTimeZone); Assert.Equal(expectedDate.Date, result.Date); }
public void DateTimeProvider_PredefinedValues_ShouldBeReturnDateInArgentinaTimezone_WhenTimeZoneIsNotNull() { var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var dateTimeProvider = new DateTimeProvider(); var utcDate = new DateTime(2020, 12, 14, 14, 0, 0, DateTimeKind.Utc); var expectedDate = new DateTime(2020, 12, 14, 11, 0, 0); var result = dateTimeProvider.GetDateByTimezoneId(utcDate, timeZoneConfigurations.InvoicesTimeZone); Assert.Equal(expectedDate, result); }
public void DateTimeProvider_ShouldBeReturnDateInArgentinaTimezone_WhenTimeZoneIsNotNull() { var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var dateTimeProvider = new DateTimeProvider(); var utcDate = dateTimeProvider.UtcNow; var cstZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneConfigurations.InvoicesTimeZone); var expectedDate = utcDate.AddMinutes(cstZone.BaseUtcOffset.TotalMinutes); var result = dateTimeProvider.GetDateByTimezoneId(utcDate, timeZoneConfigurations.InvoicesTimeZone); Assert.Equal(expectedDate, result); }
public async Task BillingService_ShouldBeReturnInvoice_WhenTheRequestIsValid() { var invoiceId = 1; var billingSystemId = 2; var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), Mock.Of <IDateTimeProvider>(), timeZoneConfigurations), new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), Mock.Of <IDateTimeProvider>(), timeZoneConfigurations) }; var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.TryGetInvoiceByInvoiceIdAndOrigin(It.IsAny <int>(), It.IsAny <string>())) .ReturnsAsync(new SapSaleOrderInvoiceResponse { BillingSystemId = billingSystemId, CardCode = "CD001", DocEntry = 1 }); var sapServiceSettingsFactoryMock = new Mock <ISapServiceSettingsFactory>(); sapServiceSettingsFactoryMock.Setup(x => x.CreateHandler("US")).Returns(sapTaskHandlerMock.Object); var billingService = new BillingService(Mock.Of <IQueuingService>(), Mock.Of <IDateTimeProvider>(), Mock.Of <ILogger <BillingService> >(), Mock.Of <ISlackService>(), billingMappers, null, sapServiceSettingsFactoryMock.Object); var response = await billingService.GetInvoiceByDopplerInvoiceIdAndOrigin(billingSystemId, invoiceId, "doppler"); Assert.NotNull(response); Assert.Equal(billingSystemId, response.BillingSystemId); Assert.Equal("CD001", response.CardCode); Assert.Equal(1, response.DocEntry); sapTaskHandlerMock.Verify(x => x.TryGetInvoiceByInvoiceIdAndOrigin(invoiceId, "doppler"), Times.Once); }
public async Task BillingService_ShouldBeAddOneTaskInQueue_WhenBillingRequestListHasOneInvalidCountryCodeElement() { 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 slackServiceMock = new Mock <ISlackService>(); slackServiceMock.Setup(x => x.SendNotification(It.IsAny <string>())).Returns(Task.CompletedTask); var queuingServiceMock = new Mock <IQueuingService>(); var billingService = new BillingService(queuingServiceMock.Object, dateTimeProviderMock.Object, Mock.Of <ILogger <BillingService> >(), slackServiceMock.Object, billingMappers, null); var billingRequestList = new List <BillingRequest> { new BillingRequest { BillingSystemId = 16, FiscalID = "123" } }; await billingService.CreateBillingRequest(billingRequestList); queuingServiceMock.Verify(x => x.AddToTaskQueue(It.IsAny <SapTask>()), Times.Never); slackServiceMock.Verify(x => x.SendNotification(It.IsAny <string>()), Times.Once); }
public async Task BillingService_ShouldBeNotAddOneTaskInQueue_WhenUpdateBillingRequestHasOneInvalidElement() { 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 slackServiceMock = new Mock <ISlackService>(); slackServiceMock.Setup(x => x.SendNotification(It.IsAny <string>())).Returns(Task.CompletedTask); var queuingServiceMock = new Mock <IQueuingService>(); var billingService = new BillingService(queuingServiceMock.Object, dateTimeProviderMock.Object, Mock.Of <ILogger <BillingService> >(), slackServiceMock.Object, billingMappers, null, Mock.Of <ISapServiceSettingsFactory>()); var updateBillingRequestList = new UpdatePaymentStatusRequest { BillingSystemId = 9, InvoiceId = 0 }; await billingService.UpdatePaymentStatus(updateBillingRequestList); queuingServiceMock.Verify(x => x.AddToTaskQueue(It.IsAny <SapTask>()), Times.Never); slackServiceMock.Verify(x => x.SendNotification(It.IsAny <string>()), Times.Once); }
public async Task BillingService_ShouldBeAddThreeTasksInQueue_WhenListHasOneValidElementWithFridayDay() { 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(2020, 12, 04)); var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations), new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations) }; var queuingServiceMock = new Mock <IQueuingService>(); var billingService = new BillingService(queuingServiceMock.Object, dateTimeProviderMock.Object, Mock.Of <ILogger <BillingService> >(), Mock.Of <ISlackService>(), billingMappers, null); var currencyList = new List <CurrencyRateDto> { new CurrencyRateDto { SaleValue = 3, CurrencyCode = "ARS", CurrencyName = "Pesos Argentinos", Date = DateTime.UtcNow } }; await billingService.SendCurrencyToSap(currencyList); queuingServiceMock.Verify(x => x.AddToTaskQueue(It.IsAny <SapTask>()), Times.Exactly(3)); }
public async Task BillingService_ShouldBeReturnNull_WhenInvoiceNotExistInSap() { var invoiceId = 1; var billingSystemId = 2; var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), Mock.Of <IDateTimeProvider>(), timeZoneConfigurations), new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), Mock.Of <IDateTimeProvider>(), timeZoneConfigurations) }; var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.TryGetInvoiceByInvoiceIdAndOrigin(invoiceId, "doppler")) .ReturnsAsync((SapSaleOrderInvoiceResponse)null); var sapServiceSettingsFactoryMock = new Mock <ISapServiceSettingsFactory>(); sapServiceSettingsFactoryMock.Setup(x => x.CreateHandler("US")).Returns(sapTaskHandlerMock.Object); var billingService = new BillingService(Mock.Of <IQueuingService>(), Mock.Of <IDateTimeProvider>(), Mock.Of <ILogger <BillingService> >(), Mock.Of <ISlackService>(), billingMappers, null, sapServiceSettingsFactoryMock.Object); var response = await billingService.GetInvoiceByDopplerInvoiceIdAndOrigin(billingSystemId, invoiceId, "doppler"); Assert.Null(response); sapTaskHandlerMock.Verify(x => x.TryGetInvoiceByInvoiceIdAndOrigin(invoiceId, "doppler"), Times.Once); }
public async Task BillingService_ShouldBeNotAddOneTaskInQueue_WhenCreateCreditNotesHasOneElementWithInvalidClientId() { var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), Mock.Of <IDateTimeProvider>(), timeZoneConfigurations), new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), Mock.Of <IDateTimeProvider>(), timeZoneConfigurations) }; var slackServiceMock = new Mock <ISlackService>(); slackServiceMock.Setup(x => x.SendNotification(It.IsAny <string>())).Returns(Task.CompletedTask); var queuingServiceMock = new Mock <IQueuingService>(); var billingService = new BillingService(queuingServiceMock.Object, Mock.Of <IDateTimeProvider>(), Mock.Of <ILogger <BillingService> >(), slackServiceMock.Object, billingMappers, null, Mock.Of <ISapServiceSettingsFactory>()); var creditNote = new CreditNoteRequest { Amount = 100, BillingSystemId = 2, ClientId = 0, InvoiceId = 1, Type = 1 }; await billingService.CreateCreditNote(creditNote); queuingServiceMock.Verify(x => x.AddToTaskQueue(It.IsAny <SapTask>()), Times.Never); slackServiceMock.Verify(x => x.SendNotification(It.IsAny <string>()), Times.Once); }
public async Task BillingService_ShouldBeAddOneTaskInQueue_WhenUpdateCreditNotePaymentStatusRequestHasValidElement() { var billingValidations = new List <IBillingValidation> { new BillingForArValidation(Mock.Of <ILogger <BillingForArValidation> >()), new BillingForUsValidation(Mock.Of <ILogger <BillingForUsValidation> >()) }; var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), Mock.Of <IDateTimeProvider>(), timeZoneConfigurations), new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), Mock.Of <IDateTimeProvider>(), timeZoneConfigurations) }; var queuingServiceMock = new Mock <IQueuingService>(); var billingService = new BillingService(queuingServiceMock.Object, Mock.Of <IDateTimeProvider>(), Mock.Of <ILogger <BillingService> >(), Mock.Of <ISlackService>(), billingMappers, billingValidations, Mock.Of <ISapServiceSettingsFactory>()); var updateCreditNotePaymentStatusRequest = new UpdateCreditNotePaymentStatusRequest { BillingSystemId = 2, Type = 2, CreditNoteId = 1 }; await billingService.UpdateCreditNotePaymentStatus(updateCreditNotePaymentStatusRequest); queuingServiceMock.Verify(x => x.AddToTaskQueue(It.IsAny <SapTask>()), Times.Once); }
public async Task CreditNoteHandler_ShouldBeNotCancelCreditNoteInSap_WhenQueueHasElementButCreditNoteNotExistsInSAP() { 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 httpClientFactoryMock = new Mock <IHttpClientFactory>(); var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(@"") }); var httpClient = new HttpClient(httpMessageHandlerMock.Object); httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>())) .Returns(httpClient); var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.StartSession()) .ReturnsAsync(new SapLoginCookies { B1Session = "session", RouteId = "route" }); sapTaskHandlerMock.Setup(x => x.TryGetCreditNoteByCreditNoteId(It.IsAny <int>())) .ReturnsAsync((SapCreditNoteResponse)null); var sapServiceSettingsFactoryMock = new Mock <ISapServiceSettingsFactory>(); sapServiceSettingsFactoryMock.Setup(x => x.CreateHandler("US")).Returns(sapTaskHandlerMock.Object); var handler = new CreditNoteHandler( sapConfigMock.Object, Mock.Of <ILogger <CreditNoteHandler> >(), sapServiceSettingsFactoryMock.Object, httpClientFactoryMock.Object, billingMappers); var sapTask = new SapTask { CancelCreditNoteRequest = new CancelCreditNoteRequest { BillingSystemId = 2, CreditNoteId = 1 }, TaskType = Enums.SapTaskEnum.CancelCreditNote }; var result = await handler.CancelCreditNoteHandle(sapTask); Assert.False(result.IsSuccessful); Assert.Equal($"Credit Note could'n cancel to SAP because the credit note does not exist: '{sapTask.CancelCreditNoteRequest.CreditNoteId}'.", result.SapResponseContent); Assert.Equal("Canceling Credit Note 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_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 BillingForArMapper(ISapBillingItemsService sapBillingItemsService, IDateTimeProvider dateTimeProvider, TimeZoneConfigurations timezoneConfig) { _sapBillingItemsService = sapBillingItemsService; _dateTimeProvider = dateTimeProvider; _timezoneConfig = timezoneConfig; }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddControllers() .AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; }); services.AddSwaggerGen(c => { c.EnableAnnotations(); c.SwaggerDoc("v1", new OpenApiInfo { Title = "Doppler SAP API", Version = "v1", Description = "API for Doppler SAP" }); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "JWT Authorization header using the Bearer scheme (Example: 'Bearer 12345abcdef')", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, Scheme = "Bearer" }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, Array.Empty <string>() } }); }); services.AddDopplerSecurity(); services.AddCors(); services.AddHttpClient("", c => { }) .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator, UseCookies = false }); services.AddSapServices(); var timezoneConfig = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem(Configuration["SapConfig:InvoicesTimeZone"]) }; services.AddSingleton(timezoneConfig); services.AddSingleton <IQueuingService, QueuingService>(); services.AddTransient <ISapService, SapService>(); services.Configure <SapConfig>(Configuration.GetSection(nameof(SapConfig))); services.AddTransient <SetCurrencyRateHandler>(); services.AddTransient <BillingRequestHandler>(); services.AddTransient <CreditNoteHandler>(); services.AddTransient <CreateOrUpdateBusinessPartnerHandler>(); services.AddTransient <ISapTaskFactory, SapTaskFactory>(); services.AddTransient <ISapServiceSettingsFactory, SapServiceSettingsFactory>(); services.AddSingleton <IDateTimeProvider, DateTimeProvider>(); services.AddTransient <ISlackService, SlackService>(); services.AddTransient <ITestSapService, TestSapService>(); //Create the MapperFactory and also initializes the mappers services.AddSapMappers(); services.AddSapBillingItems(); //Validators services.AddTransient <IBillingValidation, BillingForArValidation>(); services.AddTransient <IBillingValidation, BillingForUsValidation>(); services.AddTransient <IBusinessPartnerValidation, BusinessPartnerForArValidation>(); services.AddTransient <IBusinessPartnerValidation, BusinessPartnerForUsValidation>(); }
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 httpClientFactoryMock = new Mock <IHttpClientFactory>(); var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(@"") }); var httpClient = new HttpClient(httpMessageHandlerMock.Object); httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>())) .Returns(httpClient); 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" }); 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, httpClientFactoryMock.Object, billingValidations, billingMappers); var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent("") }; httpResponseMessage.Headers.Add("Set-Cookie", ""); httpMessageHandlerMock.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(httpResponseMessage); var result = await handler.Handle(new SapTask { CurrencyRate = new SapCurrencyRate { Currency = "Test" }, BillingRequest = new SapSaleOrderModel { BillingSystemId = 9 } }); Assert.True(result.IsSuccessful); Assert.Equal("Creating Billing Request", result.TaskName); }
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 httpClientFactoryMock = new Mock <IHttpClientFactory>(); var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(@"") }); var httpClient = new HttpClient(httpMessageHandlerMock.Object); httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>())) .Returns(httpClient); 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" }); 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, httpClientFactoryMock.Object, billingValidations, billingMappers); var sapTask = new SapTask { BillingRequest = new SapSaleOrderModel { UserId = 1 } }; var result = await handler.Handle(sapTask); Assert.False(result.IsSuccessful); Assert.Equal($"Failed at generating billing request for the user: {sapTask.BillingRequest.UserId}.", result.SapResponseContent); Assert.Equal("Creating Billing Request", result.TaskName); }
public async Task CreditNoteHandler_ShouldBeUpdatePaymentStatusInSap_WhenQueueHasValidElement() { var sapCreditNoteResponse = new SapCreditNoteResponse { DocEntry = 1, CardCode = "CD001" }; 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> { { "US", new SapServiceConfig { CompanyDB = "CompanyDb", Password = "******", UserName = "******", BaseServerUrl = "http://123.123.123/", BusinessPartnerConfig = new BusinessPartnerConfig { Endpoint = "BusinessPartners" }, BillingConfig = new BillingConfig { Endpoint = "Invoices", CreditNotesEndpoint = "CreditNotes" } } } } }); var httpClientFactoryMock = new Mock <IHttpClientFactory>(); var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(@"") }); var httpClient = new HttpClient(httpMessageHandlerMock.Object); httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>())) .Returns(httpClient); var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.StartSession()) .ReturnsAsync(new SapLoginCookies { B1Session = "session", RouteId = "route" }); sapTaskHandlerMock.Setup(x => x.TryGetCreditNoteByCreditNoteId(It.IsAny <int>())) .ReturnsAsync(sapCreditNoteResponse); var sapServiceSettingsFactoryMock = new Mock <ISapServiceSettingsFactory>(); sapServiceSettingsFactoryMock.Setup(x => x.CreateHandler("US")).Returns(sapTaskHandlerMock.Object); var handler = new CreditNoteHandler( sapConfigMock.Object, Mock.Of <ILogger <CreditNoteHandler> >(), sapServiceSettingsFactoryMock.Object, httpClientFactoryMock.Object, billingMappers); var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent("") }; httpResponseMessage.Headers.Add("Set-Cookie", ""); httpMessageHandlerMock.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(httpResponseMessage); var result = await handler.UpdatePaymentStatusHandle(new SapTask { CreditNoteRequest = new CreditNoteRequest { BillingSystemId = 2, CreditNoteId = 1, Type = 1 }, TaskType = Enums.SapTaskEnum.UpdateCreditNote }); Assert.True(result.IsSuccessful); Assert.Equal("Updating Credit Note", result.TaskName); }
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_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); }