private Forecast GetTestForecast()
 {
     // Available values:
     // 30.07.2019 21:00
     // 31.07.2019 00:00
     return(ForecastResponse.FromJson(TestData.Forecast).Forecasts.Where(forecast =>
     {
         return forecast.MeasureTime.Equals(new DateTime(2019, 7, 30, 21, 0, 0));
     })
            .Single());
 }
        public async Task ReturnEmptyIEnumerableWhenNoMatchFound()
        {
            string location   = "Karlsruhe,de";
            var    urlBuilder = CreateFluentUrlProviderWithReturnUrl();
            var    client     = CreateClient(new List <Helper.Maybe <ForecastResponse> >()
            {
                new Helper.Maybe <ForecastResponse>(ForecastResponse.FromJson(TestData.ForecastResponseKarlsruhe))
            });
            var mockQuery = CreateQuery(location);

            mockQuery.Setup(query => query.IsSatisfiedBy(It.IsAny <Forecast>()))
            .Returns((Forecast forecast) => false);

            var serviceUnderTest = new OpenWeatherService(client.Object, urlBuilder.Object);

            var result = await serviceUnderTest.GetForecasts(mockQuery.Object);

            Assert.False(result.Any());
        }
        public async Task ReturnsIEnumerableForGetForecast()
        {
            string location   = "Karlsruhe,de";
            var    urlBuilder = CreateFluentUrlProviderWithReturnUrl();
            var    client     = CreateClient(new List <Helper.Maybe <ForecastResponse> >()
            {
                new Helper.Maybe <ForecastResponse>(ForecastResponse.FromJson(TestData.ForecastResponseKarlsruhe))
            });
            var mockQuery = CreateQuery(location);

            mockQuery.Setup(query => query.IsSatisfiedBy(It.IsAny <Forecast>()))
            .Returns((Forecast forecast) => forecast.MeasureTime.Equals(new DateTimeOffset(2019, 7, 30, 21, 0, 0, new TimeSpan())));

            var serviceUnderTest = new OpenWeatherService(client.Object, urlBuilder.Object);

            var result = await serviceUnderTest.GetForecasts(mockQuery.Object);

            Assert.IsAssignableFrom <IEnumerable <ForecastResult> >(result);
        }
        public async Task OneQueryWithOneLocationAndTwoHitsReturnsOneForecastResultWithTwoElements()
        {
            string location   = "Karlsruhe,de";
            var    urlBuilder = CreateFluentUrlProviderWithReturnUrl();
            var    client     = CreateClient(new List <Helper.Maybe <ForecastResponse> >()
            {
                new Helper.Maybe <ForecastResponse>(ForecastResponse.FromJson(TestData.ForecastResponseKarlsruhe))
            });
            var mockQuery = CreateQuery(location);

            mockQuery.Setup(query => query.IsSatisfiedBy(It.IsAny <Forecast>()))
            .Returns((Forecast forecast) =>
            {
                return(forecast.MeasureTime.Equals(new DateTime(2019, 7, 30, 21, 0, 0)) ||
                       forecast.MeasureTime.Equals(new DateTime(2019, 7, 31, 9, 0, 0)));
            });

            var serviceUnderTest = new OpenWeatherService(client.Object, urlBuilder.Object);

            var result = await serviceUnderTest.GetForecasts(mockQuery.Object);

            Assert.True(result.First().ForecastData.Count == 2);
        }
        async Task <Maybe <ForecastResponse> > IClient.GetForecastAsync(string url)
        {
            try
            {
                var response = await base.GetAsync(url).ConfigureAwait(false);

                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    return(new Maybe <ForecastResponse>());
                }

                var deserializedResponse = ForecastResponse.FromJson(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
                return(new Maybe <ForecastResponse>(deserializedResponse));
            }
            catch (HttpRequestException ex)
            {
                return(new Maybe <ForecastResponse>()
                {
                    ErrorMessage = $"{ex.GetType().Name}: {ex.Message}"
                });
            }
            catch (InvalidOperationException ex)
            {
                return(new Maybe <ForecastResponse>()
                {
                    ErrorMessage = $"{ex.GetType().Name}: {ex.Message}"
                });
            }
            catch (Newtonsoft.Json.JsonSerializationException ex)
            {
                return(new Maybe <ForecastResponse>()
                {
                    ErrorMessage = $"{ex.GetType().Name}: {ex.Message}"
                });
            }
        }
        public async Task ReturnTwoForecastResultsWhenGivenTwoLocationsInQuery()
        {
            string firstLocation  = "Karlsruhe,de";
            string secondLocation = "Stuttgart,de";
            var    urlBuilder     = CreateFluentUrlProviderWithReturnUrl();
            var    client         = CreateClient(new List <Helper.Maybe <ForecastResponse> >()
            {
                new Helper.Maybe <ForecastResponse>(ForecastResponse.FromJson(TestData.ForecastResponseKarlsruhe)),
                new Helper.Maybe <ForecastResponse>(ForecastResponse.FromJson(TestData.ForecastResponseStuttgart))
            });
            var mockQuery = CreateQuery(new List <string>()
            {
                firstLocation, secondLocation
            });

            mockQuery.Setup(query => query.IsSatisfiedBy(It.IsAny <Forecast>()))
            .Returns((Forecast forecast) => forecast.MeasureTime.Equals(new DateTime(2019, 7, 30, 21, 0, 0)));

            var serviceUnderTest = new OpenWeatherService(client.Object, urlBuilder.Object);

            var result = await serviceUnderTest.GetForecasts(mockQuery.Object);

            Assert.True(result.Count() == 2);
        }