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())); }
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); }
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); }
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); }