public void GetForecastByZipcode_InvalidRequestUri()
        {
            //Arrange
            var logger = new Mock <ILogger <IForecastProvider> >();
            OpenWeatherForecast openWeatherForecast = new OpenWeatherForecast();

            var weatherApiRetriever = new Mock <IApiDataRetriever <OpenWeatherForecast> >();

            weatherApiRetriever.Setup(m => m.GetData(It.IsAny <Uri>(), It.IsAny <string>()))
            .ReturnsAsync(openWeatherForecast);

            var uriProvider = new Mock <IExternalApiUriProvider>();
            Uri invalidUri  = new Uri("http://invaliduri");

            uriProvider.Setup(m => m.GetForecastByZipCodeUri(It.IsAny <string>(), It.IsAny <string>())).Returns(invalidUri);

            var weatherResponseConverter = new Mock <IExternalApiResponseConverter <OpenWeatherForecast, ForecastModel> >();

            var forecastProvider = new ForecastProvider(
                logger.Object,
                weatherApiRetriever.Object,
                uriProvider.Object,
                weatherResponseConverter.Object);

            //Act
            var result = forecastProvider.GetForecastByZipCode("10247", "de"); //10247 - Berlin Friedrichshain

            //Assert
            Assert.ThrowsAsync <HttpResponseWithStatusCodeException>(
                () => forecastProvider.GetForecastByZipCode("berlin", "de"), "Invalid input parameters.");
        }
        public void GetForecastByCityName_GetData_Returns_EmptyOpenWeatherForecast()
        {
            //Arrange
            var logger = new Mock <ILogger <IForecastProvider> >();
            OpenWeatherForecast openWeatherForecast = new OpenWeatherForecast();

            var uriProvider = new Mock <IExternalApiUriProvider>();
            Uri uri         = new Uri("http://someuri");

            uriProvider.Setup(m => m.GetForecastByCityUri(It.IsAny <string>(), It.IsAny <string>())).Returns(uri);

            var weatherApiRetriever = new Mock <IApiDataRetriever <OpenWeatherForecast> >();

            weatherApiRetriever.Setup(m => m.GetData(It.IsAny <Uri>(), It.IsAny <string>()))
            .ReturnsAsync(openWeatherForecast);

            var weatherResponseConverter = new Mock <IExternalApiResponseConverter <OpenWeatherForecast, ForecastModel> >();

            weatherResponseConverter
            .Setup(m => m.Convert(It.IsAny <OpenWeatherForecast>()))
            .Throws(new ArgumentNullException("Invalid input parameter."));

            var forecastProvider = new ForecastProvider(
                logger.Object,
                weatherApiRetriever.Object,
                uriProvider.Object,
                weatherResponseConverter.Object);

            //Act
            //Assert
            Assert.ThrowsAsync <HttpResponseWithStatusCodeException>(
                () => forecastProvider.GetForecastByCityName("berlin", "de"), "Invalid input parameters.");
        }
Exemple #3
0
        public List <CommTypeCalendarEvent> GetCommTypeCalendarEvents(OpenWeatherForecast forecast)
        {
            var events = new List <CommTypeCalendarEvent>();

            if (forecast.list != null)
            {
                foreach (var projection in forecast.list)
                {
                    var commTypeName = GetCommType(projection);

                    var commTypeCalendarEvent = new CommTypeCalendarEvent
                    {
                        StartUtc     = projection.dt_txt,
                        EndUtc       = projection.dt_txt.AddHours(3),
                        CssClasses   = $"commType commType-{commTypeName}",
                        Temp         = projection.main.temp,
                        WeatherId    = projection.weather[0].id,
                        CommTypeName = commTypeName
                    };

                    events.Add(commTypeCalendarEvent);
                }

                events.OrderBy(x => x.StartUtc);
            }

            return(events);
        }
        public async Task <OpenWeatherForecast> GetForecast(string location)
        {
            var forecast = new OpenWeatherForecast();
            var url      = $"/data/2.5/forecast?q={location}&units=imperial&APPID={ApiKey}";

            var response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();

                forecast = JsonConvert.DeserializeObject <OpenWeatherForecast>(result);
            }


            return(forecast);
        }