コード例 #1
0
        public async Task ConvertSuccess()
        {
            var service = new Mock <IDataFeedService>(MockBehavior.Strict);

            var dataFeed = new DataFeed()
            {
                Base      = "EUR",
                Success   = true,
                Timestamp = 1541819346,
                Rates     = new Dictionary <string, decimal>()
                {
                    { "ETB", 31.813406m },
                    { "EUR", 1m },
                    { "GBP", 0.873592m }
                }
            };

            service.Setup(s => s.LoadAsync()).ReturnsAsync(dataFeed);
            var controller = new ExchangeRateController(service.Object);
            var result     = await controller.Convert("GBP", "EUR");

            Assert.IsInstanceOf <ActionResult <ExchangeRatePair> >(result);

            Assert.IsNotNull(result.Value, "ExchangeRatePair");
            Assert.AreEqual("GBP", result.Value.BaseCurrency);
            Assert.AreEqual("EUR", result.Value.TargetCurrency);
            Assert.AreEqual(0.87359m, result.Value.ExchangeRate);
            Assert.AreEqual("2018-11-10T02:09:06.00Z", result.Value.Timestamp);
        }
コード例 #2
0
        public void GetExchangeRateForMultipleDates_BadResultWithEmptyEndDate()
        {
            ExchangeRateSelectedDatesInput exchangeRateInput = GetExchangeRateForMultipleDatesInputModel();

            exchangeRateInput.Dates = string.Empty;
            var exchangeRateController = new ExchangeRateController();
            var result        = exchangeRateController.GetExchangeRateForMultipleDates(exchangeRateInput);
            var contentResult = result as BadRequestResult;

            Assert.IsNotNull(result);
        }
コード例 #3
0
        public void GetExchangeRateForMultipleDates_ValidResult()
        {
            ExchangeRateSelectedDatesInput exchangeRateInput = GetExchangeRateForMultipleDatesInputModel();

            var exchangeRateController = new ExchangeRateController();
            var result        = exchangeRateController.GetExchangeRateForMultipleDates(exchangeRateInput);
            var contentResult = ((OkNegotiatedContentResult <ExchangeRateOutputModel>)(result));

            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);
        }
コード例 #4
0
        public void GetExchangeRateHistory_BadResultWithEmptyEndDate()
        {
            ExchangeRateInput exchangeRateInput = GetExchangeRateHistoryInputModel();

            exchangeRateInput.EndDate = string.Empty;
            var exchangeRateController = new ExchangeRateController();
            var result        = exchangeRateController.GetExchangeRateHistory(exchangeRateInput);
            var contentResult = result as BadRequestResult;

            Assert.IsNotNull(result);
        }
コード例 #5
0
        public void GetExchangeRateHistory_ValidResult()
        {
            ExchangeRateInput exchangeRateInput = GetExchangeRateHistoryInputModel();

            var exchangeRateController = new ExchangeRateController();
            var result        = exchangeRateController.GetExchangeRateHistory(exchangeRateInput);
            var contentResult = ((OkNegotiatedContentResult <ExchangeRateOutputModel>)(result));

            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);
        }
コード例 #6
0
        public async Task GetTest(Currency currency)
        {
            var rate = _fixture.Build <ExchangeRate>().With(x => x.Currency, currency).Create();

            var repo = new Mock <IExchangeRepository>();

            repo.Setup(r => r.GetLatest(currency)).Returns(Task.FromResult(rate));
            var controller = new ExchangeRateController(repo.Object);

            var result = await controller.Get(currency.ToString()).ConfigureAwait(false);

            Assert.Equal(Mapper.Map <ExchangeRateDto>(rate), result.Value, new ObjectComparer <ExchangeRateDto>());
        }
コード例 #7
0
        public async Task ConvertErrorCurrencyIsNot3SymbolsInLength()
        {
            var service = new Mock <IDataFeedService>(MockBehavior.Strict);

            var controller = new ExchangeRateController(service.Object);
            var result     = await controller.Convert("GB", "EU");

            Assert.IsInstanceOf <ActionResult <ExchangeRatePair> >(result);
            Assert.IsInstanceOf <BadRequestObjectResult>(result.Result);

            Assert.AreEqual((int)HttpStatusCode.BadRequest, ((BadRequestObjectResult)result.Result).StatusCode);

            Assert.IsNull(result.Value);
        }
コード例 #8
0
        public async Task ConvertErrorBaseAndTargetCurrencyIsTheSame()
        {
            var service = new Mock <IDataFeedService>(MockBehavior.Strict);

            var controller = new ExchangeRateController(service.Object);
            var result     = await controller.Convert("GBP", "GBP");

            Assert.IsInstanceOf <ActionResult <ExchangeRatePair> >(result);
            Assert.IsInstanceOf <BadRequestObjectResult>(result.Result);

            Assert.AreEqual((int)HttpStatusCode.BadRequest, ((BadRequestObjectResult)result.Result).StatusCode);
            Assert.AreEqual("BaseCurrency should be different than TargetCurrency.", ((BadRequestObjectResult)result.Result).Value);

            Assert.IsNull(result.Value);
        }
コード例 #9
0
        public async Task GetQuoteForeignCurrencyWhenResponseIsSuccessTest()
        {
            var request = new QuoteForeignCurrencyRequest
            {
                Amount   = 10,
                Currency = Currency.USD
            };

            _exchangeRateServiceMock.Setup(setup => setup.GetQuoteForeignCurrencyAsync(request))
            .ReturnsAsync(ExchangeRateResponseBuilder.ExchangeRateResponse);

            var controller = new ExchangeRateController(_exchangeRateServiceMock.Object);
            var service    = await controller.GetQuoteForeignCurrencyAsync(request);

            Assert.IsType <OkObjectResult>(service.Result);
        }
コード例 #10
0
        public void ExchangeRateControllerTest()
        {
            var projectRoot = Path.GetDirectoryName(Assembly.GetExecutingAssembly()
                                                    .Location.Replace("XUnitTests\\bin\\Debug\\netcoreapp2.2", "TestSolution"));
            var mockEnvironment = new Mock <IHostingEnvironment>();

            mockEnvironment.SetupGet(m => m.ContentRootPath).Returns(projectRoot);

            var rateService    = new ExchangeRateService(mockEnvironment.Object);
            var rateController = new ExchangeRateController(rateService);

            var testRate = File.ReadAllText($"{projectRoot}\\{rateService.RateFileName}");

            var         okResultsCount  = 0;
            var         badResultsCount = 0;
            var         maxTasks        = 100;
            List <Task> tasks           = new List <Task>();

            for (var i = 0; i < maxTasks; i++)
            {
                tasks.Add(Task.Run(async() =>
                {
                    var response = await rateController.Get();
                    var result   = response.Result;

                    if (result is OkObjectResult okResult)
                    {
                        Assert.NotNull(okResult.Value);
                        Assert.Equal(testRate, okResult.Value);
                        Assert.InRange(rateService.CurrentNumberOfThreads, 0, 9);
                        Interlocked.Increment(ref okResultsCount);
                    }

                    if (result is BadRequestObjectResult badResult)
                    {
                        Assert.NotNull(badResult.Value);
                        Assert.Equal(rateService.OverThreadExceptionMessage, badResult.Value);
                        Assert.Equal(10, rateService.CurrentNumberOfThreads);
                        Interlocked.Increment(ref badResultsCount);
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());

            Assert.Equal(10, okResultsCount);
            Assert.Equal(maxTasks - 10, badResultsCount);
        }
コード例 #11
0
        public async Task ConvertErrorDataFeedNotLoaded()
        {
            var service = new Mock <IDataFeedService>(MockBehavior.Strict);

            service.Setup(s => s.LoadAsync()).ReturnsAsync((DataFeed)null);

            var controller = new ExchangeRateController(service.Object);
            var result     = await controller.Convert("GBP", "EUR");

            Assert.IsInstanceOf <ActionResult <ExchangeRatePair> >(result);
            Assert.IsInstanceOf <ObjectResult>(result.Result);

            Assert.AreEqual((int)HttpStatusCode.PreconditionFailed, ((ObjectResult)result.Result).StatusCode);
            Assert.AreEqual("DataFeed is empty", ((ObjectResult)result.Result).Value);

            Assert.IsNull(result.Value);
        }
コード例 #12
0
        public async Task GetForeignCurrencyConversionWhenResponseIsSuccessTest()
        {
            var request = new ForeignCurrencyConversionRequest
            {
                Amount          = 10,
                Currency        = Currency.USD,
                CustomerSegment = CustomerSegment.RETAIL
            };

            _exchangeRateServiceMock.Setup(setup => setup.GetForeignCurrencyConversionAsync(request))
            .ReturnsAsync(ExchangeRateResponseBuilder.ExchangeRateResponse);

            var controller = new ExchangeRateController(_exchangeRateServiceMock.Object);
            var service    = await controller.GetForeignCurrencyConversionAsync(request);

            Assert.IsType <OkObjectResult>(service.Result);
        }
コード例 #13
0
        /// <summary>
        /// Get exchange rates based on a single currency
        /// </summary>
        /// <param name="currency"></param>
        /// <returns>ExchangeRate</returns>
        public ExchangeRate GetExchangeRate(string currency)
        {
            var          abbreviation = currency.Length == 3 ? currency : GetCurrencyAbbreviation(currency);
            var          rateJson     = new ExchangeRateController().GetRates(abbreviation);
            ExchangeRate exchangeRate;

            try
            {
                exchangeRate = JsonConvert.DeserializeObject <ExchangeRate>(rateJson);
            }
            catch (JsonException e)
            {
                Console.WriteLine(e);
                return(null);
            }

            return(exchangeRate);
        }
コード例 #14
0
        /// <summary>
        /// Get exchange rate for two specific currencies, ignoring all other currencies
        /// </summary>
        /// <param name="firstCurrency"></param>
        /// <param name="secondCurrency"></param>
        /// <returns>ExchangeRate</returns>
        public ExchangeRate GetExchangeRate(string firstCurrency, string secondCurrency)
        {
            var firstAbbr  = firstCurrency.Length == 3 ? firstCurrency : GetCurrencyAbbreviation(firstCurrency);
            var secondAbbr = secondCurrency.Length == 3 ? secondCurrency : GetCurrencyAbbreviation(secondCurrency);

            var          rateJson = new ExchangeRateController().GetRates(firstAbbr, secondAbbr);
            ExchangeRate exchangeRate;

            try
            {
                exchangeRate = JsonConvert.DeserializeObject <ExchangeRate>(rateJson);
            }
            catch (JsonException e)
            {
                Console.WriteLine(e);
                return(null);
            }

            return(exchangeRate);
        }
コード例 #15
0
        public static ExchangeRateController Fixture()
        {
            ExchangeRateController controller = new ExchangeRateController(new ExchangeRateRepository(), "", new LoginView());

            return(controller);
        }
コード例 #16
0
 public static ExchangeRateController Fixture()
 {
     ExchangeRateController controller = new ExchangeRateController(new ExchangeRateRepository(), "", new LoginView());
     return controller;
 }