Exemplo n.º 1
0
        public async Task ShouldGetCorrectTotalDebt()
        {
            var chargeRepositoryMock = new Mock <IRepository <Charge> >();

            chargeRepositoryMock.Setup(x => x.ListAsync(
                                           It.IsAny <Expression <Func <Charge, bool> > >(),
                                           It.IsAny <SortOptions>(),
                                           It.IsAny <int?>(),
                                           It.IsAny <int?>()))
            .Returns((Expression <Func <Charge, bool> > predicate, SortOptions sortOptions, int?pageSize, int?pageNum) =>
                     //Return charges after applying UserDebtService's predicate
                     Task.FromResult(GetTestCharges().Where(predicate.Compile()).ToList()));

            UserDebtService userDebtService = new UserDebtService(chargeRepositoryMock.Object);

            UserDebt debt = await userDebtService.GetUserDebt(MockData.GetTestUser().Id);

            Assert.Multiple(() =>
            {
                var arDebt = debt.GetDebtAmountsByCurrency().FirstOrDefault(x => x.Currency == Enumerations.Currency.ARS);
                var usDebt = debt.GetDebtAmountsByCurrency().FirstOrDefault(x => x.Currency == Enumerations.Currency.US);

                Assert.AreEqual(2, debt.GetDebtAmountsByCurrency().Count, "User should have debt in 2 currencies");

                Assert.IsNotNull(arDebt, "Debt in AR$ currency should not be null");
                Assert.AreEqual(63.75M, arDebt.Amount, "AR$ debt should be $63.75");

                Assert.IsNotNull(usDebt, "Debt in U$ currency should not be null");
                Assert.AreEqual(100, usDebt.Amount, "U$ debt should be $100");
            });
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Create(
            [FromRoute] long userId,
            [FromBody] CreatePaymentRequest createPaymentRequest)
        {
            UserDebtService userDebtService = new UserDebtService(_chargeRepository);
            InvoiceService  invoiceService  = new InvoiceService(_invoiceRepository);

            //Get charge domain service
            PaymentService paymentService = new PaymentService(
                _paymentRepository,
                _userRepository,
                userDebtService,
                invoiceService);

            try
            {
                Payment createdPayment =
                    await paymentService.ExecutePayment(createPaymentRequest.Amount, createPaymentRequest.Currency, userId);

                await _dbContext.SaveChangesAsync();

                return(Ok(_mapper.Map <GetPaymentResponse>(createdPayment)));
            }
            catch (InvalidEntityException ex)
            {
                return(BadRequest(ex.Model));
            }
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Get(long userId)
        {
            UserDebtService userDebtService = new UserDebtService(_chargeRepository);
            var             userDebt        = await userDebtService.GetUserDebt(userId);

            if (userDebt != null && userDebt.GetDebtAmountsByCurrency().Count > 0)
            {
                return(Ok(_mapper.Map <GetUserDebtResponse>(userDebt)));
            }
            else
            {
                return(Ok("User has no debt"));
            }
        }
Exemplo n.º 4
0
        public async Task PaymentShouldBeValidForUserDebt()
        {
            var chargeRepositoryMock = new Mock <IRepository <Charge> >();

            chargeRepositoryMock.Setup(x => x.ListAsync(
                                           It.IsAny <Expression <Func <Charge, bool> > >(),
                                           It.IsAny <SortOptions>(),
                                           It.IsAny <int?>(),
                                           It.IsAny <int?>()))
            .Returns((Expression <Func <Charge, bool> > predicate, SortOptions sortOptions, int?pageSize, int?pageNum) =>
                     //Return charges after applying UserDebtService's predicate
                     Task.FromResult(GetTestCharges().Where(predicate.Compile()).ToList()));

            UserDebtService userDebtService = new UserDebtService(chargeRepositoryMock.Object);

            bool isValidPayment = await userDebtService.IsValidPayment(
                new Payment(63.75M, Enumerations.Currency.ARS, MockData.GetTestUser()));

            Assert.IsTrue(isValidPayment, "A payment of 63.75 AR$ should be valid for a user with a 63.75 AR$ debt");
        }
Exemplo n.º 5
0
        public async Task ShouldGetCorrectDebtByCurrency()
        {
            var chargeRepositoryMock = new Mock <IRepository <Charge> >();

            chargeRepositoryMock.Setup(x => x.ListAsync(
                                           It.IsAny <Expression <Func <Charge, bool> > >(),
                                           It.IsAny <SortOptions>(),
                                           It.IsAny <int?>(),
                                           It.IsAny <int?>()))
            .Returns((Expression <Func <Charge, bool> > predicate, SortOptions sortOptions, int?pageSize, int?pageNum) =>
                     //Return charges after applying UserDebtService's predicate
                     Task.FromResult(GetTestCharges().Where(predicate.Compile()).ToList()));

            UserDebtService userDebtService = new UserDebtService(chargeRepositoryMock.Object);

            AmountCurrency debt = await userDebtService.GetUserDebtByCurrency(
                MockData.GetTestUser().Id, Enumerations.Currency.ARS);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(63.75M, debt.Amount, "AR$ debt should be $63.75");
                Assert.AreEqual(Enumerations.Currency.ARS, debt.Currency, "Debt currency should be AR$");
            });
        }