public void FillInPrices_ValidInputProvided_ReturnsCorrectAmounts(
            decimal netAmountInput, decimal grossAmountInput, decimal vatAmountInput,
            decimal vatRate,
            decimal expectedNet, decimal expectedGross, decimal expectedVatAmount)
        {
            // Arrange
            var service = CreateService();
            PriceDetailsInput details = new PriceDetailsInput()
            {
                NetAmount   = netAmountInput,
                GrossAmount = grossAmountInput,
                VatAmount   = vatAmountInput,
                VatRate     = vatRate
            };

            // Act
            var calculatedPrice = service.GetCalculatedPrice(
                details);

            // Assert

            Assert.Equal(expectedNet, calculatedPrice.NetAmount);
            Assert.Equal(expectedGross, calculatedPrice.GrossAmount);
            Assert.Equal(expectedVatAmount, calculatedPrice.VatAmount);
            _mockRepository.VerifyAll();
        }
        public PriceDetails Calculate(PriceDetailsInput details)
        {
            if (!details.NetAmount.HasValue)
            {
                throw new ArgumentNullException(nameof(details.NetAmount));
            }
            if (details.VatRate <= 0 || details.VatRate > 100)
            {
                throw new ArgumentOutOfRangeException(nameof(details.VatRate));
            }
            var vatAmount   = details.NetAmount.Value * details.VatRate / 100;
            var grossAmount = details.NetAmount.Value + vatAmount;

            return(new PriceDetails(details.NetAmount.Value, grossAmount, vatAmount));
        }
Exemple #3
0
        public PriceDetails Calculate(PriceDetailsInput details)
        {
            if (!details.VatAmount.HasValue)
            {
                throw new ArgumentNullException(nameof(details.VatAmount));
            }
            if (details.VatRate <= 0 || details.VatRate > 100)
            {
                throw new ArgumentOutOfRangeException(nameof(details.VatRate));
            }

            var netAmount   = 100 / details.VatRate * details.VatAmount.Value;
            var grossAmount = netAmount + details.VatAmount.Value;

            return(new PriceDetails(netAmount, grossAmount, details.VatAmount.Value));
        }
Exemple #4
0
 private IPriceCalculationStrategy ChoosePriceCalculationStrategy(PriceDetailsInput details)
 {
     if (details.NetAmount.HasValue && details.NetAmount.Value > 0)
     {
         return(new PriceCalculationFromNetAmountStrategy());
     }
     if (details.GrossAmount.HasValue && details.GrossAmount.Value > 0)
     {
         return(new PriceCalculationFromGrossAmountStrategy());
     }
     if (details.VatAmount.HasValue && details.VatAmount.Value > 0)
     {
         return(new PriceCalculationFromVatAmountStrategy());
     }
     _logger.LogWarning("Unexpected State: No data to perform calculation. Vat ammount, net ammount and gross amount were all null");
     throw new ArgumentException("Unexpected State: No data to perform calculation. Vat ammount, net ammount and gross amount were all null");
 }
        public void FillInPrices_NoValidDataProided_ArgumentExceptionRaised(
            decimal netAmountInput, decimal grossAmountInput, decimal vatAmountInput,
            decimal vatRate)
        {
            // Arrange
            var service = CreateService();
            PriceDetailsInput details = new PriceDetailsInput()
            {
                NetAmount   = netAmountInput,
                GrossAmount = grossAmountInput,
                VatAmount   = vatAmountInput,
                VatRate     = vatRate
            };

            // Act and Assert
            Assert.Throws <ArgumentException>(() => service.GetCalculatedPrice(
                                                  details));
            _mockRepository.VerifyAll();
        }
Exemple #6
0
        public PriceDetails GetCalculatedPrice(PriceDetailsInput details)
        {
            IPriceCalculationStrategy priceCalculationStrategy = ChoosePriceCalculationStrategy(details);

            return(priceCalculationStrategy.Calculate(details));
        }