Exemplo n.º 1
0
        public async Task DopplerCurrencyService_ShouldBeHttpStatusCodeOk_WhenCurrencyCodeIsCorrectAndDateIsNotHoliday()
        {
            _httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(),
                                                 ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(@"{'cotizationAvailable':true, 'date':'2020-03-18','saleValue':65.0000,
                       'buyValue':'20.30','currencyName':'Peso Argentino', 'currencyCode':'ARS'}")
            });

            _httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>()))
            .Returns(_httpClient);

            var service = CreateSutCurrencyServiceTests.CreateSut(
                _httpClientFactoryMock.Object,
                _dopplerCurrencyServiceSettingsMock.Object,
                jwtOptions: _jwtOptionMock.Object,
                signingCredentials: _signingCredentials);

            var result = await service.GetCurrencyByCode();

            Assert.NotEmpty(result);

            var currency = result[0];

            Assert.Equal("2020-03-18", currency.Date);
            Assert.Equal(65.0000M, currency.SaleValue);
            Assert.Equal(20.30M, currency.BuyValue);
            Assert.Equal("Peso Argentino", currency.CurrencyName);
            Assert.Equal("ARS", currency.CurrencyCode);
        }
Exemplo n.º 2
0
        public async Task DopplerCurrencyService_ShouldBeEmptyList_WhenDateIsHolidayAndCotizationAvailableIsFalseAfterFiveRetries()
        {
            _httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(),
                                                 ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(@"{'cotizationAvailable':false, 'date':'2020-03-18','currencyName':'Peso Argentino', 'currencyCode':'ARS'}")
            });

            _httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>()))
            .Returns(_httpClient);

            var service = CreateSutCurrencyServiceTests.CreateSut(
                _httpClientFactoryMock.Object,
                _dopplerCurrencyServiceSettingsMock.Object,
                jwtOptions: _jwtOptionMock.Object,
                signingCredentials: _signingCredentials);

            var result = await service.GetCurrencyByCode();

            Assert.Empty(result);
            _httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Exactly(6), ItExpr.IsAny <HttpRequestMessage>(),
                                                                                     ItExpr.IsAny <CancellationToken>());
        }
Exemplo n.º 3
0
        public async Task DopplerCurrencyService_ShouldBeException_WhenJsonContentIsInvalid()
        {
            _httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(),
                                                 ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("test")
            });
            _httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>()))
            .Returns(_httpClient);

            var loggerMock = new Mock <ILogger <DopplerCurrencyService> >();

            var service = CreateSutCurrencyServiceTests.CreateSut(
                _httpClientFactoryMock.Object,
                _dopplerCurrencyServiceSettingsMock.Object,
                jwtOptions: _jwtOptionMock.Object,
                loggerCurrencyService: loggerMock.Object,
                signingCredentials: _signingCredentials);

            var exception = await Assert.ThrowsAnyAsync <Exception>(() => service.GetCurrencyByCode());

            Assert.Equal("Error parsing boolean value. Path '', line 1, position 1.", exception.Message);
        }
Exemplo n.º 4
0
        public async Task DopplerCurrencyService__ShouldBeHttpStatusCodeOk_WhenDateIsHolidayAndCotizationAvailableIsTrueAfterRetry()
        {
            var failedUrlSegment = $"{DateTime.UtcNow.Year}-{DateTime.UtcNow.Month}-{DateTime.UtcNow.Day}";

            var previousDay          = DateTime.UtcNow.DayOfWeek == DayOfWeek.Monday ? DateTime.UtcNow.AddDays(-3) : DateTime.UtcNow.AddDays(-1);
            var successfulUrlSegment = $"{previousDay.Year}-{previousDay.Month}-{previousDay.Day}";

            _httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.Is <HttpRequestMessage>(x => x.RequestUri.AbsolutePath.EndsWith(failedUrlSegment)),
                                                 ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(@"{'cotizationAvailable':false, 'date':'" + $"{DateTime.UtcNow:yyyy-MM-dd}" + "','currencyName':'Peso Argentino', 'currencyCode':'ARS'}")
            });

            _httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.Is <HttpRequestMessage>(x => x.RequestUri.AbsolutePath.EndsWith(successfulUrlSegment)),
                                                 ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(@"{'cotizationAvailable':true, 'date':'" + $"{previousDay:yyyy-MM-dd}" + "', 'saleValue':65.0000, 'buyValue':'20.30','currencyName':'Peso Argentino', 'currencyCode':'ARS'}")
            });

            _httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>()))
            .Returns(_httpClient);

            var service = CreateSutCurrencyServiceTests.CreateSut(
                _httpClientFactoryMock.Object,
                _dopplerCurrencyServiceSettingsMock.Object,
                jwtOptions: _jwtOptionMock.Object,
                signingCredentials: _signingCredentials);

            var result = await service.GetCurrencyByCode();

            Assert.NotEmpty(result);

            var currency = result[0];

            Assert.Equal($"{DateTime.UtcNow:yyyy-MM-dd}", currency.Date);
            Assert.Equal(65.0000M, currency.SaleValue);
            Assert.Equal(20.30M, currency.BuyValue);
            Assert.Equal("Peso Argentino", currency.CurrencyName);
            Assert.Equal("ARS", currency.CurrencyCode);

            _httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Exactly(2), ItExpr.IsAny <HttpRequestMessage>(),
                                                                                     ItExpr.IsAny <CancellationToken>());
        }
Exemplo n.º 5
0
        public async Task DopplerCurrencyService_ShouldBeException_WhenUrlIsInvalid()
        {
            var loggerMock = new Mock <ILogger <DopplerCurrencyService> >();

            _dopplerCurrencyServiceSettingsMock.Setup(x => x.CurrentValue)
            .Returns(new DopplerCurrencyServiceSettings
            {
                CurrencyCodeList = new List <string> {
                    "ARS"
                }
            });

            var service = CreateSutCurrencyServiceTests.CreateSut(
                _httpClientFactoryMock.Object,
                _dopplerCurrencyServiceSettingsMock.Object,
                loggerMock.Object,
                jwtOptions: _jwtOptionMock.Object,
                signingCredentials: _signingCredentials);

            var exception = await Assert.ThrowsAnyAsync <Exception>(() => service.GetCurrencyByCode());

            loggerMock.VerifyLogger(LogLevel.Error, "Unexpected error getting currency for ARS.", Times.Once());
            Assert.Equal("Invalid URI: The format of the URI could not be determined.", exception.Message);
        }
Exemplo n.º 6
0
        public async Task DopplerCurrencyService_ShouldBeEmptyList_WhenCurrencyCodeIsCorrectAndDateIsNotHoliday()
        {
            _httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(),
                                                 ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.BadRequest,
                Content    = new StringContent("")
            });

            _httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>()))
            .Returns(_httpClient);

            var service = CreateSutCurrencyServiceTests.CreateSut(
                _httpClientFactoryMock.Object,
                _dopplerCurrencyServiceSettingsMock.Object,
                jwtOptions: _jwtOptionMock.Object,
                signingCredentials: _signingCredentials);

            var result = await service.GetCurrencyByCode();

            Assert.Empty(result);
        }