Ejemplo n.º 1
0
        public void TestForecastManager()
        {
            string testOpenWeatherResult = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Examples\\OpenWeatherExample.json").Replace("\r\n", "");
            string str  = string.Empty;
            var    city = "London,GB";

            // Given a forecast manager and openweather instance
            // When Calling GetForecastByCity in the manager
            // Expect the GetForecastByCityJson method to be called in the ForecastManager instance
            var fm2 = new Mock <IForecastManager>();

            fm2.Setup(mgr => mgr.GetForecastByCity(It.IsAny <string>(), It.IsAny <string>())).Returns(getFakeForecastObject());
            fm2.Setup(mgr => mgr.GetForecastByCityJson(city, out str)).Returns(testOpenWeatherResult);
            var strJson = fm2.Object.GetForecastByCityJson(city, out str);

            fm2.Verify(fmgr => fmgr.GetForecastByCityJson(city, out str), Times.Once(), "GetForecastByCityJson was not called");
            Assert.AreEqual(strJson, testOpenWeatherResult);
            Assert.AreEqual(strJson, testOpenWeatherResult, "The expected JSON string was not returned");

            // Verify that manager calls return correctly
            var ow = new Mock <IOpenWeather>();

            ow.Setup(d => d.GetForecastJson(It.IsAny <string>(), out str)).Returns(testOpenWeatherResult);

            // Expect the GetForecastJson method in OpenWeather called
            // Expect the return value to equal what was returned by the OpenWeather instance
            var fm    = new ForecastManager(ow.Object);
            var fjson = fm.GetForecastByCityJson(city, out str);

            ow.Verify(d => d.GetForecastJson(city, out str), Times.Once(), "GetForecastJson method in OpenWeather was not called");
            Assert.AreEqual(fjson, testOpenWeatherResult);
        }
Ejemplo n.º 2
0
 private Task <bool> SaveForecastsAsync()
 {
     return(ExecuteSafe(async() =>
     {
         var json = JsonConvert.SerializeObject(ForecastManager.GetForecastCache());
         return await _storageService.SetCachedTextFileAsync(ReflectionHelper.GetAttributeOfEnum <DescriptionAttribute, FileKeys>(FileKeys.WeatherCache).Description, json);
     }));
 }
Ejemplo n.º 3
0
        private Task Initialize()
        {
            return(ExecuteSafe(async() =>
            {
                if (_isInitialized)
                {
                    return;
                }

                using (await _initializeAsyncLock.LockAsync())
                {
                    var fontJson = await _storageService.GetAssetTextFileAsync(ReflectionHelper.GetAttributeOfEnum <DescriptionAttribute, FileKeys>(FileKeys.WeatherFontInformations).Description);
                    _weatherFontMapping = JsonConvert.DeserializeObject <Dictionary <string, string> >(fontJson);

                    WeatherCacheModel cache = new WeatherCacheModel();

                    await ExecuteSafe(async() =>
                    {
                        var json = await _storageService.GetCachedTextFileAsync(
                            ReflectionHelper.GetAttributeOfEnum <DescriptionAttribute, FileKeys>(
                                FileKeys.WeatherCache).Description);

                        if (!string.IsNullOrEmpty(json))
                        {
                            cache = JsonConvert.DeserializeObject <WeatherCacheModel>(json);
                        }
                    });

                    var cities = await GetCities();
                    foreach (var city in cities)
                    {
                        var oldOne = cache.Forecasts.First(f => f.City == city);
                        if (oldOne != null)
                        {
                            ForecastManager.AddForecast(oldOne);
                        }
                        else
                        {
                            ForecastManager.AddForecast(new Forecast()
                            {
                                City = city
                            });
                        }
                    }

                    _isInitialized = true;
                }
            }));
        }
Ejemplo n.º 4
0
 public Task <bool> ActualizeAsync()
 {
     return(ExecuteSafe(async() =>
     {
         await Initialize();
         if (await _permissionsService.CanDownload())
         {
             foreach (var forecast in ForecastManager.GetForecasts())
             {
                 Uri url = GetApiUrl(forecast);
                 var service = new HttpService();
                 var feedresult = await service.DownloadAsync(url);
                 if (feedresult.IsRequestSuccessfull)
                 {
                     OpenWeatherMapHelper.EvaluateFeed(await feedresult.GetResponseAsStringAsync(),
                                                       _weatherFontMapping, forecast);
                 }
             }
             await SaveForecastsAsync();
         }
         return true;
     }));
 }
Ejemplo n.º 5
0
 public ObservableCollection <Forecast> GetForecasts()
 {
     Initialize();
     return(ForecastManager.GetForecasts());
 }
Ejemplo n.º 6
0
 public void SetUp()
 {
     _forecastManager = new ForecastManager(null);
 }