示例#1
0
        public async Task ShouldThrowModelValidationErrorForInvalidRequest()
        {
            var priceConversionService = new PriceConversionService(httpWrapper.Object, configuration.Object, _priceConversionRequestValidator, _loggerMock.Object);
            await Assert.ThrowsAsync <ArgumentNullException>(() => priceConversionService.ConvertCurrencyAsync(null));

            await Assert.ThrowsAsync <ValidationException>(() => priceConversionService.ConvertCurrencyAsync(new PriceConversionRequest()));
        }
示例#2
0
        public async Task ShouldReturnNullForNotSupportedCurrencies()
        {
            var apiResponse = new HttpWrappedResponse <ExchangeRatesResponse>(null, new HttpResponseMessage(HttpStatusCode.NotFound));

            httpWrapper.Setup(x => x.GetAsync <ExchangeRatesResponse>(It.IsAny <string>(), It.IsAny <CancellationToken>(), null)).ReturnsAsync(apiResponse);

            var priceConversionService = new PriceConversionService(httpWrapper.Object, configuration.Object, _priceConversionRequestValidator, _loggerMock.Object);
            var result = await priceConversionService.ConvertCurrencyAsync(new PriceConversionRequest
                                                                           { Price = 10, SourceCurrency = "Source", TargetCurrency = "Target" }).ConfigureAwait(false);

            Assert.Null(result);
        }
示例#3
0
        public async Task ShouldThrowErrorIfExchangeRateApiNotConfigured()
        {
            var apiResponse = new HttpWrappedResponse <ExchangeRatesResponse>(null, new HttpResponseMessage(HttpStatusCode.NotFound));

            configuration.Setup(x => x.GetSection(It.IsAny <string>())).Returns(default(ConfigurationSection));
            httpWrapper.Setup(x => x.GetAsync <ExchangeRatesResponse>(It.IsAny <string>(), It.IsAny <CancellationToken>(), null)).ReturnsAsync(apiResponse);

            var priceConversionService = new PriceConversionService(httpWrapper.Object, configuration.Object, _priceConversionRequestValidator, _loggerMock.Object);
            var error = await Assert.ThrowsAsync <Exception>(() => priceConversionService.ConvertCurrencyAsync(new PriceConversionRequest {
                Price = 10, SourceCurrency = "Source", TargetCurrency = "Target"
            })).ConfigureAwait(false);

            Assert.Equal("Exchange rates API not configured", error.Message);
            _loggerMock.Verify(x => x.LogError(It.IsAny <string>()), Times.Once);
        }
示例#4
0
        public async Task ShouldConvertCurrencyForSupportedCurrencies(double price, double exchangeRate, string targetCurrency, string sourceCurrency)
        {
            var apiResponse = new HttpWrappedResponse <ExchangeRatesResponse>(new ExchangeRatesResponse
            {
                Date            = DateTime.Now.AddDays(-1).ToString("dd/MM/yyyy"),
                TimeLastUpdated = DateTime.Now.AddDays(-1).Ticks,
                Rates           = new Rates {
                    EUR = exchangeRate, GBP = exchangeRate, USD = exchangeRate
                }
            },
                                                                              new HttpResponseMessage(HttpStatusCode.OK));


            httpWrapper.Setup(x => x.GetAsync <ExchangeRatesResponse>(It.IsAny <string>(), It.IsAny <CancellationToken>(), null)).ReturnsAsync(apiResponse);

            var priceConversionService = new PriceConversionService(httpWrapper.Object, configuration.Object, _priceConversionRequestValidator, _loggerMock.Object);
            var result = await priceConversionService.ConvertCurrencyAsync(new PriceConversionRequest
                                                                           { Price = price, SourceCurrency = sourceCurrency, TargetCurrency = targetCurrency }).ConfigureAwait(false);

            var expectedPrice = price * exchangeRate;

            Assert.Equal(expectedPrice, result.NewPrice);
        }