public void Json_Object_To_Json_Test()
        {
            var testObject = new
            {
                City    = "London",
                Country = "GB",
                Weather = "Always Changing",
                MaxTemp = 10,
                MinTemp = 6
            };
            var result = HelperClasses.Json(testObject);

            Assert.AreEqual(result, "{\r\n  \"city\": \"London\",\r\n  \"country\": \"GB\",\r\n  \"weather\": \"Always Changing\",\r\n  \"maxTemp\": 10,\r\n  \"minTemp\": 6\r\n}");
        }
        public void Json_Dto_To_Json_Test()
        {
            var testDto = new WeatherDataDTO
            {
                CityName     = "Basingstoke",
                ErrorMessage = "",
                ForecastList = new List <ForecastListDTO>()
                {
                    new ForecastListDTO {
                        Day = "Monday", Humidity = 70, MainTemp = 10, WindSpeed = 3.8m, TempMax = 12.4m, TempMin = 6.5m
                    }
                }
            };
            var result = HelperClasses.Json(testDto);

            Assert.AreEqual(result, "{\r\n  \"errorMessage\": \"\",\r\n  \"cityName\": \"Basingstoke\",\r\n  \"forecastList\": [\r\n    {\r\n      \"day\": \"Monday\",\r\n      \"mainTemp\": 10.0,\r\n      \"tempMin\": 6.5,\r\n      \"tempMax\": 12.4,\r\n      \"pressure\": 0.0,\r\n      \"humidity\": 70,\r\n      \"windSpeed\": 3.8\r\n    }\r\n  ]\r\n}");
        }
Exemplo n.º 3
0
        public async Task <ActionResult> GetWeatherReport(string cityName)
        {
            WeatherDataModel weatherData   = new WeatherDataModel();
            bool             dataRetrieved = false;

            weatherData.ErrorMessage = "Please specify city name to get the weather forecast.";
            // check if cityName is not empty
            if (!string.IsNullOrWhiteSpace(cityName))
            {
                try
                {
                    string result;
                    // get json string result from the service
                    result = await _weatherService.GetWeatherByCity(cityName);

                    if (!string.IsNullOrWhiteSpace(result))
                    {
                        // map json data to our model class
                        // this will be returned as json object to front end
                        weatherData = JsonConvert.DeserializeObject <WeatherDataModel>(result);
                        weatherData.ErrorMessage = "Please specify a valid city name to get the weather forecast. Or the service is currently unavailable.";
                        // check if the correct city weather is returned
                        if (weatherData.City != null)
                        {
                            if (weatherData.City.Name.Equals(cityName, OrdinalIgnoreCase))
                            {
                                weatherData.ErrorMessage = "";
                                dataRetrieved            = true;
                            }
                        }
                    }
                    if (!dataRetrieved && string.IsNullOrWhiteSpace(weatherData.ErrorMessage))
                    {
                        weatherData.ErrorMessage = "There was some technical problem getting the weather report. Please try again after sometime.";
                    }
                }
                catch (Exception ex)
                {
                    weatherData.ErrorMessage = $"There was some problem getting the weather report: {ex.Message}";
                }
            }

            return(Json(HelperClasses.Json(Mapper.Map <WeatherDataDTO>(weatherData)), JsonRequestBehavior.AllowGet));
        }
        public void Json_Empty_Object_test()
        {
            var result = HelperClasses.Json(null);

            Assert.AreEqual(result, String.Empty);
        }