Ejemplo n.º 1
0
        public async Task <IActionResult> Compute([Bind("CashFlowsDescription,Increment,UpperBoundDiscountRate,LowerBoundDiscountRate,InitialCost")] NPVModel nPVModel)
        {
            var cashFlows = nPVModel.CashFlowsDescription.Split(',').Select(double.Parse).ToList <double>();

            var requestModel = new NPVRequestModel
            {
                CashFlow = cashFlows
                ,
                Increment                = nPVModel.Increment
                , InitialCost            = nPVModel.InitialCost
                , LowerBoundDiscountRate = nPVModel.LowerBoundDiscountRate
                , UpperBoundDiscountRate = nPVModel.UpperBoundDiscountRate
            };

            var npvResults = await _httpService.GetAsJson(_npvApiUrl, requestModel);

            if (string.IsNullOrEmpty(npvResults))
            {
                npvResults = await _httpService.PostAsyncReturnAsJson(_npvApiUrl, requestModel);
            }

            TempData["npvResults"] = npvResults;
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public void Compute_Given_Request_Has_Not_Been_Made_Before_Returns_Object_From_Database(
            string cashFlowDescription
            , double increment
            , double upperBoundDiscountRate
            , double lowerBoundDiscountRate
            , double initialCost
            , string expectedNPVs
            )
        {
            var npvModel = new NPVModel
            {
                CashFlowsDescription = cashFlowDescription
                ,
                Increment = increment
                ,
                InitialCost = initialCost
                ,
                LowerBoundDiscountRate = lowerBoundDiscountRate
                ,
                UpperBoundDiscountRate = upperBoundDiscountRate
            };
            var listOfExpectedNPVs   = expectedNPVs.Split(",").Select(n => double.Parse(n)).ToList();
            var listOfNPVDetailModel = new List <NPVDetailModel>();

            var discountRate = lowerBoundDiscountRate;

            for (var i = 0; i < listOfExpectedNPVs.Count; i++)
            {
                listOfNPVDetailModel.Add(new NPVDetailModel
                {
                    CashFlowSummary = cashFlowDescription
                    ,
                    DiscountRate  = discountRate
                    , InitialCost = initialCost
                    , NPV         = listOfExpectedNPVs[i]
                });
                discountRate += increment;
            }

            var listOfNPVDetailModelStr = JsonConvert.SerializeObject(listOfNPVDetailModel);

            _httpServiceMock.Setup(h => h.GetAsJson <NPVRequestModel>(It.IsAny <string>(), It.IsAny <NPVRequestModel>()))
            .ReturnsAsync(string.Empty);
            _httpServiceMock.Setup(h => h.PostAsyncReturnAsJson(It.IsAny <string>(), It.IsAny <NPVRequestModel>()))
            .ReturnsAsync(listOfNPVDetailModelStr);
            _configurationMock.SetupGet(c => c[It.IsAny <string>()]).Returns("http://localhost:63620/api/npv");
            var httpContext = new DefaultHttpContext();
            var tempData    = new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>());


            var controller = new VRTestWeb.Razor.Controllers.NPVController(
                _configurationMock.Object
                , _httpServiceMock.Object
                )
            {
                TempData = tempData
            };

            var response = controller.Compute(npvModel).Result;
            var redirectToActionResult = response as RedirectToActionResult;

            Assert.IsNotNull(redirectToActionResult);
            Assert.AreEqual("Index", redirectToActionResult.ActionName);

            _httpServiceMock.VerifyAll();
            _configurationMock.VerifyAll();
        }