Ejemplo n.º 1
0
        public IActionResult OnPost()
        {
            dto.CashFlows = new List <CashFlowDTO>();

            for (int i = 0; i < Request.Form["y.Id"].Count; i++)
            {
                dto.CashFlows.Add(new CashFlowDTO
                {
                    Id     = Convert.ToInt32(Request.Form["y.Id"][i]),
                    Amount = string.IsNullOrEmpty(Request.Form["y.Amount"][i]) ? 0 : Convert.ToDouble(Request.Form["y.Amount"][i])
                });
            }
            NpvValidate   validate = new NpvValidate();
            NpvCalculator calc     = new NpvCalculator(validate);

            dto = calc.Compute(dto);

            if (ModelState.IsValid)
            {
                var npv = mapper.Map <Npv>(dto);
                npvData.Update(npv);

                return(RedirectToPage("./Detail", new { npvId = dto.NpvId }));
            }
            else
            {
                ModelState.AddModelError("error", "An error occured");
            }

            return(Page());
        }
Ejemplo n.º 2
0
        public void MoreThan20ResultShouldOnlyReturn20Records(
            double[] expectedNpvResult, decimal lowerDiscount, decimal upperDiscount,
            decimal increment, decimal outflow, double[] inflows)
        {
            var sut = new NpvCalculator();

            //Validation has been tested on project NpvApi.Dtos.Test so will not test again here.
            var npvOptions = new NpvOptions
            {
                RateOption = new RateOption
                {
                    LowerDiscount     = lowerDiscount,
                    UpperDiscount     = upperDiscount,
                    DiscountIncrement = increment
                },
                Outflow = outflow
            };

            foreach (var inflow in inflows.ToList())
            {
                npvOptions.Inflows.Add(new Inflow()
                {
                    Value = (decimal)inflow
                });
            }

            var result = sut.Calculate(npvOptions);

            Assert.Equal(20, result.Count);
        }
Ejemplo n.º 3
0
        public void Should_Not_Accept_Lower_Rate_Higher_Than_Upper_Rate()
        {
            var dto = new NpvDTO
            {
                Name           = "Cashflow 1",
                IncrementRate  = 25,
                LowerRate      = 2,
                UpperRate      = 1,
                InitialValue   = 10000,
                TotalNpvAmount = null,
                CashFlows      = new List <CashFlowDTO> {
                    new CashFlowDTO {
                        Amount = 1000
                    },
                    new CashFlowDTO {
                        Amount = 2000
                    }
                }
            };

            Mock <INpvValidate> mockValidator = new Mock <INpvValidate>();

            mockValidator.Setup(x => x.Validate(It.Is <NpvDTO>(n => n.LowerRate > n.UpperRate))).Throws <Exception>();

            var sut = new NpvCalculator(mockValidator.Object);

            Assert.Throws <Exception>(() => sut.Compute(dto));
        }
Ejemplo n.º 4
0
        public void Compute_NPV()
        {
            var dto = new NpvDTO
            {
                Name           = "Cashflow 1",
                IncrementRate  = 25,
                LowerRate      = 1,
                UpperRate      = 2,
                InitialValue   = 10000,
                TotalNpvAmount = null,
                CashFlows      = new List <CashFlowDTO> {
                    new CashFlowDTO {
                        Amount = 1000
                    },
                    new CashFlowDTO {
                        Amount = 2000
                    }
                }
            };

            Mock <INpvValidate> mockValidator = new Mock <INpvValidate>();

            mockValidator.Setup(x => x.Validate(It.IsAny <NpvDTO>())).Returns(true);

            var sut = new NpvCalculator(mockValidator.Object);

            var n = sut.Compute(dto);

            Assert.Equal("-7750.14", String.Format("{0:0.##}", n.TotalNpvAmount));
        }
        public void CalculateCashFlows_Should_be_passed_when_testing_npv_amount_for_given_inputs()
        {
            // Arrange
            INpvCalculator calculator = new NpvCalculator();
            NpvInput       input      = new NpvInput();

            input.Cash                   = 100m;
            input.CashSeries             = 10m;
            input.IncrementDiscountRate  = 0.0025m;
            input.LowerDiscountRateBound = 0.01m;
            input.UpperDiscountRateBound = 0.15m;

            // Act
            var npvCashFlows = calculator.CalculateCashFlows(input);

            // Assert
            var npvCashFlow  = npvCashFlows.Where(c => c.Rate == 0.01m).SingleOrDefault <NpvCashFlow>();
            var npvCashFlow2 = npvCashFlows.Where(c => c.Rate == 0.1m).SingleOrDefault <NpvCashFlow>();
            var npvCashFlow3 = npvCashFlows.Where(c => c.Rate == 0.15m).SingleOrDefault <NpvCashFlow>();

            Assert.IsTrue(npvCashFlows.Count() > 0);

            Assert.IsTrue(Math.Round(npvCashFlow.Amount, 2) == 947.13m);
            Assert.IsTrue(Math.Round(npvCashFlow2.Amount, 2) == 614.46m);
            Assert.IsTrue(Math.Round(npvCashFlow3.Amount, 2) == 501.88m);
        }
Ejemplo n.º 6
0
        public void Should_Not_Accept_If_Empty_Cashflow()
        {
            var dto = new NpvDTO
            {
                Name           = "Cashflow 1",
                IncrementRate  = 25,
                LowerRate      = 2,
                UpperRate      = 1,
                InitialValue   = 10000,
                TotalNpvAmount = null,
                CashFlows      = null
            };

            Mock <INpvValidate> mockValidator = new Mock <INpvValidate>();

            mockValidator.Setup(x => x.Validate(It.Is <NpvDTO>(n => n.CashFlows == null))).Throws <Exception>();

            var sut = new NpvCalculator(mockValidator.Object);

            Assert.Throws <Exception>(() => sut.Compute(dto));
        }
Ejemplo n.º 7
0
        public IActionResult OnGet()
        {
            try
            {
                dto = TempData.Get <NpvDTO>("npvDTO");

                if (dto == null)
                {
                    return(RedirectToPage("./NotFound"));
                }

                NpvValidate   validate = new NpvValidate();
                NpvCalculator calc     = new NpvCalculator(validate);
                dto = calc.Compute(dto);

                return(Page());
            }
            catch (Exception e)
            {
                return(RedirectToPage("./Error"));
            }
        }
Ejemplo n.º 8
0
        public void ValidInputShouldGetCorrectNpvResults(double[] expectedNpvResult,
                                                         decimal lowerDiscount, decimal upperDiscount, decimal increment, decimal outflow,
                                                         double[] inflows)
        {
            var sut = new NpvCalculator();

            //Validation testing has been done on project NpvApi.Dtos.Test so will not test again here.
            var npvOptions = new NpvOptions
            {
                RateOption = new RateOption
                {
                    LowerDiscount     = lowerDiscount,
                    UpperDiscount     = upperDiscount,
                    DiscountIncrement = increment
                },
                Outflow = outflow
            };

            foreach (var inflow in inflows.ToList())
            {
                npvOptions.Inflows.Add(new Inflow()
                {
                    Value = (decimal)inflow
                });
            }

            var result = sut.Calculate(npvOptions);

            Assert.Equal(expectedNpvResult.Length, result.Count);

            for (int i = 0; i < expectedNpvResult.Length; i++)
            {
                var expected = expectedNpvResult[i];
                var actual   = (double)result[i].NetPresentValue;
                Assert.Equal(expected, actual);
            }
        }
Ejemplo n.º 9
0
        public void RateIncrement0ShouldOnlyReturn1ResultBack(
            double[] expectedNpvResult, decimal lowerDiscount, decimal upperDiscount,
            decimal increment, decimal outflow, double[] inflows)
        {
            //This case need to check the result set which is different than the previous one\
            //so just keep it seperate.
            var sut = new NpvCalculator();

            var npvOptions = new NpvOptions
            {
                RateOption = new RateOption
                {
                    LowerDiscount     = lowerDiscount,
                    UpperDiscount     = upperDiscount,
                    DiscountIncrement = increment
                },
                Outflow = outflow
            };

            foreach (var inflow in inflows.ToList())
            {
                npvOptions.Inflows.Add(new Inflow()
                {
                    Value = (decimal)inflow
                });
            }

            var result = sut.Calculate(npvOptions);

            Assert.Equal(1, result.Count);

            var expected = expectedNpvResult.First();
            var actual   = (double)result.First().NetPresentValue;

            Assert.Equal(expected, actual);
        }