public IActionResult Get([FromQuery] Models.CurrencyConversionRequest request)
 {
     if (ModelState.IsValid)
     {
         try
         {
             return(Ok(_converter.GetConvertedAmount(request.From, request.To, request.Amount)));
         }
         catch (InvalidCurrencyException e)
         {
             return(BadRequest(e.Message));
         }
     }
     return(BadRequest("Invalid request."));
 }
Ejemplo n.º 2
0
        public void TestAllConversionCombinations()
        {
            var amount       = 1.0M;
            var conversions  = _repository.GetConversions().ToArray();
            var combinations = new List <(string, string)>();

            for (int i = 0; i < conversions.Count(); i++)
            {
                for (int j = i; j < conversions.Count(); j++)
                {
                    combinations.Add((conversions[i].CurrencyCode, conversions[j].CurrencyCode));
                }
            }

            foreach (var combination in combinations)
            {
                var fromConversion = _repository.GetConversions().SingleOrDefault(x => x.CurrencyCode == combination.Item1);
                var toConversion   = _repository.GetConversions().SingleOrDefault(x => x.CurrencyCode == combination.Item2);

                //Filter out the currencies that I expect to fail.  A better way to handle this would be to setup different mock repositories
                //for each test case.
                if (fromConversion.RateFromUSDToCurrency > 0 && toConversion.RateFromUSDToCurrency > 0 && fromConversion.CurrencyCode != "MAX" && toConversion.CurrencyCode != "MAX")
                {
                    var actualResult   = _converter.GetConvertedAmount(combination.Item1, combination.Item2, amount);
                    var expectedResult = amount * (toConversion.RateFromUSDToCurrency / fromConversion.RateFromUSDToCurrency);
                    Assert.AreEqual(expectedResult, actualResult, $"{amount}{combination.Item1}->{combination.Item2}");
                }
            }
        }