Exemple #1
0
        public async Task GetExchangeRateAsync_WhenCurrencySymbolIsPln_ShouldReturnOne()
        {
            // Arrange
            const string plnCurrencySymbol = "PLN";
            var          httpClientMock    = new HttpClientMock();

            var service = new ExchangeRateService(httpClientMock.GetHttpClient(),
                                                  new Mock <ILogger <ExchangeRateService> >().Object);

            // Act
            decimal result = await service.GetExchangeRateAsync(plnCurrencySymbol, _today);

            // Assert
            result.ShouldBe(1m);
        }
Exemple #2
0
        public async Task GetExchangeRateAsync_WhenExceptionIsBeingThrows_ShouldThrowApiException()
        {
            // Arrange
            const string errorMessage   = "error-message";
            var          httpClientMock = new HttpClientMock();

            var service = new ExchangeRateService(httpClientMock.GetHttpClientWhichThrowsException(errorMessage),
                                                  new Mock <ILogger <ExchangeRateService> >().Object);

            // Act
            var exception =
                await Should.ThrowAsync <Exception>(async() =>
                                                    await service.GetExchangeRateAsync(UsdCurrencySymbol, _today));

            // Assert
            exception.Message.ShouldBe(errorMessage);
        }
Exemple #3
0
        public async Task GetExchangeRateAsync_WhenResponseIsValid_ShouldReturnRate()
        {
            // Arrange
            var httpClientMock = new HttpClientMock
            {
                Content =
                    "{\"table\":\"A\",\"currency\":\"jen (Japonia)\",\"code\":\"JPY\",\"rates\":[{\"no\":\"088/A/NBP/2019\",\"effectiveDate\":\"2019-05-08\",\"mid\":0.034793}]}"
            };

            var service = new ExchangeRateService(httpClientMock.GetHttpClient(),
                                                  new Mock <ILogger <ExchangeRateService> >().Object);

            // Act
            decimal result = await service.GetExchangeRateAsync("JPY", new DateTime(2019, 5, 8));

            // Assert
            result.ShouldBe(0.034793m);
        }
Exemple #4
0
        public async Task GetExchangeRateAsync_WhenResponseStatusCodeIsNotFound_ShouldThrowApiException()
        {
            // Arrange
            var httpClientMock = new HttpClientMock {
                StatusCode = HttpStatusCode.NotFound
            };

            var service = new ExchangeRateService(httpClientMock.GetHttpClient(),
                                                  new Mock <ILogger <ExchangeRateService> >().Object);

            // Act
            var exception =
                await Should.ThrowAsync <ApiException>(async() =>
                                                       await service.GetExchangeRateAsync(UsdCurrencySymbol, _today));

            // Assert
            this.ShouldSatisfyAllConditions(
                () =>
            {
                exception.Code.ShouldBe(HttpStatusCode.NotFound);
                exception.Errors.ShouldBe($"Exchange rate not found [{UsdCurrencySymbol}/{_today:yyyy-MM-dd}].");
            });
        }
Exemple #5
0
        public async Task GetExchangeRateAsync_WhenResponseStatusCodeIsInvalid_ShouldThrowApiException(HttpStatusCode statusCode)
        {
            // Arrange
            var httpClientMock = new HttpClientMock {
                StatusCode = statusCode
            };

            var service = new ExchangeRateService(httpClientMock.GetHttpClient(),
                                                  new Mock <ILogger <ExchangeRateService> >().Object);

            // Act
            var exception =
                await Should.ThrowAsync <ApiException>(async() =>
                                                       await service.GetExchangeRateAsync("USD", _today));

            // Assert
            this.ShouldSatisfyAllConditions(
                () =>
            {
                exception.Code.ShouldBe(HttpStatusCode.InternalServerError);
                exception.Errors.ShouldBe(
                    $"An error occured while getting exchange rate [USD/{_today:yyyy-MM-dd}].");
            });
        }