public async Task<PCalcResultInfo> Calculate(EnvironmentInfo environment, ProductDescriptionInfo productDescription, ActionResultInfo actionResult)
 {
     var result = new PCalcResultInfo();
     result.ProductDescription = productDescription;
     DescriptionInfo desc = GetRandomDescription();
     result.QueryType = GetQueryType(desc);
     result.QueryDescription = desc.ToTransferDescription();
     return result;
 }
        public void ShouldCalculateNextStep(int milliseconds)
        {
            m_ExpectedMaximum = TimeSpan.FromMilliseconds(milliseconds);

            ProductDescriptionInfo product = new ProductDescriptionInfo { ProductId = 1, WeightClass = 1, Weight = m_Weight };

            var actionResult = new ActionResultInfo { Action = EActionId.ShowMenu, Label = 0, Results = new List<AnyInfo> { new AnyInfo { AnyValue = "0", AnyType = EAnyType.UINT32 } } };
            PCalcResultInfo result = Calculate(m_Environment, product, actionResult);

            Assert.IsNotNull(result);
            Assert.AreEqual(EQueryType.ShowMenu, result.QueryType);
            Assert.Greater(result.ProductDescription.Weight.WeightValue, 0, "Product has no weight");

            Pass(result);
        }
 public async Task<PCalcResultInfo> Calculate(EnvironmentInfo environment, ProductDescriptionInfo productDescription, ActionResultInfo actionResult)
 {
     //lookup correct entry            
     ScenarioResult initResult = await m_ScenarioRunner.RunAsync(InitFileHandler(environment));
     if (initResult.Success == false || m_Handler.IsValid == false)
     {
         return ReturnErrorResult(initResult);
     }
     ScenarioResult<PCalcResultInfo> calcResult = m_ScenarioRunner.Run(() => Calculate(environment, productDescription, actionResult, m_Handler));
     if (calcResult.Success == false)
     {
         return ReturnErrorResult(calcResult);
     }
     return calcResult.Value;
 }
        private async Task<ActionResult> HandleCalculation(ActionResultInfo actionResult, EnvironmentInfo environment)
        {
            PCalcResultInfo lastResult = GetLastPcalcResult();
            if (null == lastResult)
            {
                return HandleGeneralError("Index", ProductCalculationViewModel.Create(EQueryType.None), "Unable to retrieve last product");
            }
            if (null == environment)
            {
                return HandleGeneralError("Index", ProductCalculationViewModel.Create(EQueryType.None), "Unable to retrieve environment information");
            }

            CalculateRequest calc = new CalculateRequest();
            calc.Environment = environment;
            calc.ProductDescription = lastResult.ProductDescription;
            calc.ActionResult = actionResult;
            ApiResponse<PCalcResultInfo> response = await m_Repository.Calculate(calc);
            if (IsApiError(response) || PcalcResultIsValid(response.ApiResult) == false)
            {
                return HandleGeneralPcalcError("Index", ProductCalculationViewModel.Create(EQueryType.None), response.ApiResult);
            }
            AddOrUpdateTempData(response.ApiResult, calc.Environment);
            AddViewData(response.ApiResult, environment);
            return View("Index", ProductCalculationViewModel.Create(response.ApiResult.QueryType));            
        }
 public async Task<ActionResult> Acknowledge()
 {
     var actionResult = new ActionResultInfo()
     {
         Action = EActionId.Display,
         Results = new List<AnyInfo>()
         {
             new AnyInfo() { AnyType = EAnyType.INT32, AnyValue = ((int)EActionDisplayResult.DISPLAYED).ToString()}
         }
     };
     return await HandleCalculation(actionResult, GetEnvironment());
 }
        public async Task<ActionResult> RequestValue(RequestValueViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return HandleGeneralError("Index", ProductCalculationViewModel.Create(EQueryType.None), "Product Calculation Error");
            }
            EnvironmentInfo environment = GetEnvironment();
            if (null == environment)
            {
                return HandleGeneralError("Index", ProductCalculationViewModel.Create(EQueryType.None), "Unable to retrieve environment information");
            }
            CultureInfo culture = new CultureInfo(environment.Culture);
            char decimalSeperator = culture.NumberFormat.CurrencyDecimalSeparator.ToCharArray()[0];

            FormatStringAdapter adapter = new FormatStringAdapter(decimalSeperator);
            adapter.SetFormatString(model.FormatString);

            string formattedString;
            adapter.Format(out formattedString, model.EnteredRawValue);

            var actionResult = new ActionResultInfo()
            {
                Action = model.QueryType == EQueryType.RequestValue ? EActionId.RequestValue :
                model.QueryType == EQueryType.RequestPostage ? EActionId.ManualPostage : EActionId.RequestString,
                Results = new List<AnyInfo>()
            };

            uint numberOfEntries = adapter.GetValueNumber();
            for (uint i = 0; i < numberOfEntries; ++i)
            {
                object valuePart = adapter.GetValue(formattedString, i);
                if (valuePart != null)
                {
                    actionResult.Results.Add(new AnyInfo()
                    {
                        AnyType = model.QueryType == EQueryType.RequestString ? EAnyType.STRING : EAnyType.UINT32,
                        AnyValue = valuePart.ToString()
                    });
                }
            }
            return await HandleCalculation(actionResult, environment);
        }
 public async Task<ActionResult> SelectValue(int entryValue)
 {
     var actionResult = new ActionResultInfo()
     {
         Action = EActionId.SelectValue,
         Results = new List<AnyInfo>()
         {
             new AnyInfo() { AnyType = EAnyType.INT32, AnyValue=entryValue.ToString()}
         }
     };
     return await HandleCalculation(actionResult, GetEnvironment());
 }
 public async Task<ActionResult> SelectMenuIndex(int index)
 {
     var actionResult = new ActionResultInfo()
     {
         Action = EActionId.ShowMenu,
         Results = new List<AnyInfo>()
         {
             new AnyInfo() { AnyType = EAnyType.INT32, AnyValue=index.ToString()}
         }
     };
     return await HandleCalculation(actionResult, GetEnvironment());
 }
        public void ShouldHandleCalculateCompleteProduct(int milliseconds)
        {
            m_ExpectedMaximum = TimeSpan.FromMilliseconds(milliseconds);

            PCalcResultInfo result = Start(m_Environment, m_Weight);

            int steps = 0;

            for (int i = 0; i < MAX_STEPS; i++)
            {
                if (result.ProductDescription.State == EProductDescriptionState.Complete)
                {
                    break;
                }

                steps++;
                switch (result.QueryType)
                {
                    case EQueryType.ShowMenu:
                        var actionResult = new ActionResultInfo
                                               {
                                                   Action = EActionId.ShowMenu,
                                                   Label = 0,
                                                   Results = new List<AnyInfo> { new AnyInfo { AnyValue = (result.QueryDescription.Entries.Count - 1).ToString(), AnyType = EAnyType.UINT32 } }
                                               };
                        result = Calculate(m_Environment, result.ProductDescription, actionResult);
                        break;

                    case EQueryType.None:
                        i = MAX_STEPS - 1;
                        break;

                    default:
                        Assert.Fail();
                        break;
                }

                Assert.IsNotNull(result.ProductDescription);
                Assert.IsTrue(result.ProductDescription.ProductId > 0);
            }

            Assert.IsTrue(steps < MAX_STEPS);
            Assert.IsNotNull(result.ProductDescription);
            Assert.IsNotNull(result.ProductDescription.Postage);

            Assert.IsTrue(result.ProductDescription.State == EProductDescriptionState.Complete);
            Assert.IsTrue(result.ProductDescription.Postage.PostageValue > 0);
            Assert.IsTrue(result.ProductDescription.ProductCode > 0);

            Pass(result);
        }
        private PCalcResultInfo Calculate(EnvironmentInfo info, ProductDescriptionInfo product, ActionResultInfo actionResult)
        {
            using (var context = new PCalcProxyContext(info, m_AmxFile.FullName, m_TableFile.FullName))
            {
                IPCalcProxy proxy = context.Proxy;
                Assert.That(proxy, Is.Not.Null);

                return proxy.Calculate(info, product, actionResult);
            }
        }
        public void ShouldHandleStepBackToPreviousProduct()
        {
            var actionResult = new ActionResultInfo { Action = EActionId.ShowMenu, Label = 0, Results = new List<AnyInfo> { new AnyInfo { AnyValue = "0", AnyType = EAnyType.UINT32 } } };
            PCalcResultInfo result = Start(m_Environment, new WeightInfo());

            result = Calculate(m_Environment, result.ProductDescription, actionResult);
            result = Calculate(m_Environment, result.ProductDescription, actionResult);

            int productID = result.ProductDescription.ProductId;

            result = Calculate(m_Environment, result.ProductDescription, actionResult);
            Assert.AreNotEqual(productID, result.ProductDescription.ProductId);

            result = Back(m_Environment, result.ProductDescription);
            Assert.AreEqual(productID, result.ProductDescription.ProductId);

            Pass(result);
        }
        public void ShouldHandleRequestValue()
        {
            var actionResult = new ActionResultInfo { Action = EActionId.ShowMenu, Label = 0, Results = new List<AnyInfo> { new AnyInfo { AnyValue = "0", AnyType = EAnyType.INT32 } } };
            PCalcResultInfo result = Start(m_Environment, new WeightInfo());

            result = Calculate(m_Environment, result.ProductDescription, actionResult);
            result = Calculate(m_Environment, result.ProductDescription, actionResult);
            result = Calculate(m_Environment, result.ProductDescription, actionResult);

            actionResult.Results.Single().AnyValue = "4";
            actionResult.Action = EActionId.ShowMenu;
            actionResult.Label = 1;

            Assert.DoesNotThrow(() => result = Calculate(m_Environment, result.ProductDescription, actionResult));
            Assert.AreEqual(EQueryType.RequestValue, result.QueryType);

            actionResult.Results.Clear();
            actionResult.Results.Add(new AnyInfo { AnyType = EAnyType.UINT32, AnyValue = "123456" });
            actionResult.Action = EActionId.RequestValue;
            actionResult.Label = result.QueryDescription.Label;

            Assert.DoesNotThrow(() => result = Calculate(m_Environment, result.ProductDescription, actionResult));

            Pass(result);
        }
 private ScenarioResult<PCalcResultInfo> Calculate(EnvironmentInfo environment, ProductDescriptionInfo description, ActionResultInfo actionResult, RateCalculationFileHandler handler)
 {
     using (var context = new PCalcProxyContext(environment, handler.PawnFile, handler.RateTableFile, handler.AdditionalFiles))
     {
         IPCalcProxy proxy = context.Proxy;
         PCalcResultInfo result = proxy.Calculate(environment, description, actionResult);
         HandleCurrencySymbol(result, environment);
         return new ScenarioResult<PCalcResultInfo>() { Value = result };
     }
 }