mirror of
https://github.com/Prominence/openweathermap-java-api.git
synced 2026-07-04 03:36:44 +03:00
Implemented climatic forecast for 30 days. Some refactoring.
This commit is contained in:
+126
@@ -0,0 +1,126 @@
|
||||
package com.github.prominence.openweathermap.api.mapper;
|
||||
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.model.Wind;
|
||||
import com.github.prominence.openweathermap.api.model.*;
|
||||
import com.github.prominence.openweathermap.api.model.forecast.climatic.AtmosphericPressure;
|
||||
import com.github.prominence.openweathermap.api.model.forecast.climatic.Temperature;
|
||||
import com.github.prominence.openweathermap.api.model.forecast.climatic.*;
|
||||
import com.github.prominence.openweathermap.api.utils.TestMappingUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ClimaticForecastResponseMapperTest {
|
||||
|
||||
@Test
|
||||
public void mapToForecast() {
|
||||
final String jsonResponse = """
|
||||
{
|
||||
"cod": "200",
|
||||
"city": {
|
||||
"id": 2643743,
|
||||
"name": "London",
|
||||
"coord": {
|
||||
"lon": -0.1277,
|
||||
"lat": 51.5073
|
||||
},
|
||||
"country": "GB"
|
||||
},
|
||||
"message": 0.353472054,
|
||||
"list": [
|
||||
{
|
||||
"dt": 1594382400,
|
||||
"sunrise": 1594353335,
|
||||
"sunset": 1594412149,
|
||||
"temp": {
|
||||
"day": 286.98,
|
||||
"min": 285.22,
|
||||
"max": 287.97,
|
||||
"night": 285.22,
|
||||
"eve": 287.97,
|
||||
"morn": 287.29
|
||||
},
|
||||
"feels_like": {
|
||||
"day": 282.61,
|
||||
"night": 283.19,
|
||||
"eve": 284.98,
|
||||
"morn": 282.68
|
||||
},
|
||||
"pressure": 1016,
|
||||
"humidity": 84,
|
||||
"weather": [
|
||||
{
|
||||
"id": 500,
|
||||
"main": "Rain",
|
||||
"description": "light rain",
|
||||
"icon": "10d"
|
||||
}
|
||||
],
|
||||
"speed": 6.78,
|
||||
"deg": 320,
|
||||
"clouds": 81,
|
||||
"rain": 1.96,
|
||||
"snow": 2.21
|
||||
}
|
||||
]
|
||||
}
|
||||
""";
|
||||
|
||||
final Forecast forecast = new ClimaticForecastResponseMapper(UnitSystem.METRIC).mapToForecast(jsonResponse);
|
||||
assertNotNull(forecast);
|
||||
|
||||
final Location location = forecast.getLocation();
|
||||
assertNotNull(location);
|
||||
assertEquals(Coordinates.of(51.5073, -0.1277), location.getCoordinate());
|
||||
assertEquals(2643743, location.getId());
|
||||
assertEquals("London", location.getName());
|
||||
assertEquals("GB", location.getCountryCode());
|
||||
assertNull(location.getPopulation());
|
||||
assertNull(location.getZoneOffset());
|
||||
|
||||
assertEquals(1, forecast.getWeatherForecasts().size());
|
||||
final WeatherForecast weatherForecast = forecast.getWeatherForecasts().get(0);
|
||||
assertEquals(TestMappingUtils.parseDateTime(1594382400), weatherForecast.getForecastTime());
|
||||
assertEquals(TestMappingUtils.parseDateTime(1594353335), weatherForecast.getSunriseTime());
|
||||
assertEquals(TestMappingUtils.parseDateTime(1594412149), weatherForecast.getSunsetTime());
|
||||
|
||||
final Temperature temperature = weatherForecast.getTemperature();
|
||||
assertEquals(286.98, temperature.getDay());
|
||||
assertEquals(285.22, temperature.getMin());
|
||||
assertEquals(287.97, temperature.getMax());
|
||||
assertEquals(285.22, temperature.getNight());
|
||||
assertEquals(287.97, temperature.getEve());
|
||||
assertEquals(287.29, temperature.getMorning());
|
||||
assertEquals(282.61, temperature.getDayFeelsLike());
|
||||
assertEquals(283.19, temperature.getNightFeelsLike());
|
||||
assertEquals(284.98, temperature.getEveFeelsLike());
|
||||
assertEquals(282.68, temperature.getMorningFeelsLike());
|
||||
|
||||
final AtmosphericPressure pressure = weatherForecast.getAtmosphericPressure();
|
||||
assertEquals(1016, pressure.getSeaLevelValue());
|
||||
|
||||
final Humidity humidity = weatherForecast.getHumidity();
|
||||
assertEquals(84, humidity.getValue());
|
||||
|
||||
final Wind wind = weatherForecast.getWind();
|
||||
assertEquals(6.78, wind.getSpeed());
|
||||
assertEquals(320, wind.getDegrees());
|
||||
|
||||
final Clouds clouds = weatherForecast.getClouds();
|
||||
assertEquals(81, clouds.getValue());
|
||||
|
||||
assertEquals(1, weatherForecast.getWeatherStates().size());
|
||||
final WeatherState weatherState = weatherForecast.getWeatherStates().get(0);
|
||||
assertEquals(500, weatherState.getId());
|
||||
assertEquals("Rain", weatherState.getName());
|
||||
assertEquals("light rain", weatherState.getDescription());
|
||||
assertEquals("10d", weatherState.getIconId());
|
||||
|
||||
final Rain rain = weatherForecast.getRain();
|
||||
assertEquals(1.96, rain.getLevel());
|
||||
|
||||
final Snow snow = weatherForecast.getSnow();
|
||||
assertEquals(2.21, snow.getLevel());
|
||||
}
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
package com.github.prominence.openweathermap.api.mapper;
|
||||
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.model.*;
|
||||
import com.github.prominence.openweathermap.api.model.forecast.daily.AtmosphericPressure;
|
||||
import com.github.prominence.openweathermap.api.model.forecast.daily.Temperature;
|
||||
import com.github.prominence.openweathermap.api.model.forecast.daily.*;
|
||||
import com.github.prominence.openweathermap.api.utils.TestMappingUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
class DailyForecastResponseMapperTest {
|
||||
|
||||
@Test
|
||||
public void mapToForecast() {
|
||||
final String jsonResponse = """
|
||||
{
|
||||
"city": {
|
||||
"id": 2643743,
|
||||
"name": "London",
|
||||
"coord": {
|
||||
"lon": -0.1258,
|
||||
"lat": 51.5085
|
||||
},
|
||||
"country": "GB",
|
||||
"population": 0,
|
||||
"timezone": 3600
|
||||
},
|
||||
"cod": "200",
|
||||
"message": 0.7809187,
|
||||
"cnt": 1,
|
||||
"list": [
|
||||
{
|
||||
"dt": 1568977200,
|
||||
"sunrise": 1568958164,
|
||||
"sunset": 1569002733,
|
||||
"temp": {
|
||||
"day": 293.79,
|
||||
"min": 288.85,
|
||||
"max": 294.47,
|
||||
"night": 288.85,
|
||||
"eve": 290.44,
|
||||
"morn": 293.79
|
||||
},
|
||||
"feels_like": {
|
||||
"day": 278.87,
|
||||
"night": 282.73,
|
||||
"eve": 281.92,
|
||||
"morn": 278.87
|
||||
},
|
||||
"pressure": 1025.04,
|
||||
"humidity": 42,
|
||||
"weather": [
|
||||
{
|
||||
"id": 800,
|
||||
"main": "Clear",
|
||||
"description": "sky is clear",
|
||||
"icon": "01d"
|
||||
}
|
||||
],
|
||||
"speed": 4.66,
|
||||
"deg": 102,
|
||||
"gust": 5.3,
|
||||
"clouds": 0,
|
||||
"pop": 0.24,
|
||||
"rain": 22.2,
|
||||
"snow": 24.2
|
||||
}
|
||||
]
|
||||
}
|
||||
""";
|
||||
|
||||
final Forecast forecast = new DailyForecastResponseMapper(UnitSystem.METRIC).mapToForecast(jsonResponse);
|
||||
assertNotNull(forecast);
|
||||
|
||||
final Location location = forecast.getLocation();
|
||||
assertNotNull(location);
|
||||
assertEquals(Coordinates.of(51.5085, -0.1258), location.getCoordinate());
|
||||
assertEquals(2643743, location.getId());
|
||||
assertEquals("London", location.getName());
|
||||
assertEquals("GB", location.getCountryCode());
|
||||
assertEquals(0, location.getPopulation());
|
||||
assertEquals(ZoneOffset.ofTotalSeconds(3600), location.getZoneOffset());
|
||||
|
||||
assertEquals(1, forecast.getWeatherForecasts().size());
|
||||
final WeatherForecast weatherForecast = forecast.getWeatherForecasts().get(0);
|
||||
assertEquals(TestMappingUtils.parseDateTime(1568977200), weatherForecast.getForecastTime());
|
||||
// TODO: Does the API provide the sunrise and sunset info??? It is not officially described in the API but present in the example.
|
||||
assertEquals(TestMappingUtils.parseDateTime(1568958164), weatherForecast.getSunriseTime());
|
||||
assertEquals(TestMappingUtils.parseDateTime(1569002733), weatherForecast.getSunsetTime());
|
||||
|
||||
final Temperature temperature = weatherForecast.getTemperature();
|
||||
assertEquals(293.79, temperature.getDay());
|
||||
assertEquals(288.85, temperature.getMin());
|
||||
assertEquals(294.47, temperature.getMax());
|
||||
assertEquals(288.85, temperature.getNight());
|
||||
assertEquals(290.44, temperature.getEve());
|
||||
assertEquals(293.79, temperature.getMorning());
|
||||
assertEquals(278.87, temperature.getDayFeelsLike());
|
||||
assertEquals(282.73, temperature.getNightFeelsLike());
|
||||
assertEquals(281.92, temperature.getEveFeelsLike());
|
||||
assertEquals(278.87, temperature.getMorningFeelsLike());
|
||||
|
||||
final AtmosphericPressure pressure = weatherForecast.getAtmosphericPressure();
|
||||
assertEquals(1025.04, pressure.getSeaLevelValue());
|
||||
|
||||
final Humidity humidity = weatherForecast.getHumidity();
|
||||
assertEquals(42, humidity.getValue());
|
||||
|
||||
final Wind wind = weatherForecast.getWind();
|
||||
assertEquals(4.66, wind.getSpeed());
|
||||
assertEquals(102, wind.getDegrees());
|
||||
assertEquals(5.3, wind.getGust());
|
||||
|
||||
final Clouds clouds = weatherForecast.getClouds();
|
||||
assertEquals(0, clouds.getValue());
|
||||
|
||||
assertEquals(1, weatherForecast.getWeatherStates().size());
|
||||
final WeatherState weatherState = weatherForecast.getWeatherStates().get(0);
|
||||
assertEquals(800, weatherState.getId());
|
||||
assertEquals("Clear", weatherState.getName());
|
||||
assertEquals("sky is clear", weatherState.getDescription());
|
||||
assertEquals("01d", weatherState.getIconId());
|
||||
|
||||
final Rain rain = weatherForecast.getRain();
|
||||
assertEquals(22.2, rain.getLevel());
|
||||
|
||||
final Snow snow = weatherForecast.getSnow();
|
||||
assertEquals(24.2, snow.getLevel());
|
||||
|
||||
assertEquals(0.24, weatherForecast.getProbabilityOfPrecipitation());
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -29,7 +29,8 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
class GeocodingResponseMapperTest {
|
||||
|
||||
|
||||
+2
-1
@@ -32,7 +32,8 @@ import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
class HourlyForecastResponseMapperTest {
|
||||
|
||||
|
||||
+2
-1
@@ -30,7 +30,8 @@ import org.junit.jupiter.api.Test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
public class AirPollutionDetailsUnitTest {
|
||||
@Test
|
||||
|
||||
+1
-1
@@ -22,8 +22,8 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.model.onecall;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import com.github.prominence.openweathermap.api.model.Wind;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
+2
-1
@@ -24,7 +24,8 @@ package com.github.prominence.openweathermap.api.model.onecall.current;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
public class DailyTemperatureUnitTest {
|
||||
|
||||
|
||||
+1
-1
@@ -25,8 +25,8 @@ package com.github.prominence.openweathermap.api.model.onecall.current;
|
||||
import com.github.prominence.openweathermap.api.model.Clouds;
|
||||
import com.github.prominence.openweathermap.api.model.Humidity;
|
||||
import com.github.prominence.openweathermap.api.model.WeatherState;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.AtmosphericPressure;
|
||||
import com.github.prominence.openweathermap.api.model.Wind;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.AtmosphericPressure;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
+4
-1
@@ -26,7 +26,10 @@ import com.github.prominence.openweathermap.api.model.Clouds;
|
||||
import com.github.prominence.openweathermap.api.model.Humidity;
|
||||
import com.github.prominence.openweathermap.api.model.WeatherState;
|
||||
import com.github.prominence.openweathermap.api.model.Wind;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.*;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.AtmosphericPressure;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.Rain;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.Snow;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.Temperature;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
+4
-1
@@ -26,7 +26,10 @@ import com.github.prominence.openweathermap.api.model.Clouds;
|
||||
import com.github.prominence.openweathermap.api.model.Humidity;
|
||||
import com.github.prominence.openweathermap.api.model.WeatherState;
|
||||
import com.github.prominence.openweathermap.api.model.Wind;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.*;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.AtmosphericPressure;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.Rain;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.Snow;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.Temperature;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
+4
-1
@@ -26,7 +26,10 @@ import com.github.prominence.openweathermap.api.model.Clouds;
|
||||
import com.github.prominence.openweathermap.api.model.Humidity;
|
||||
import com.github.prominence.openweathermap.api.model.WeatherState;
|
||||
import com.github.prominence.openweathermap.api.model.Wind;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.*;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.AtmosphericPressure;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.Rain;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.Snow;
|
||||
import com.github.prominence.openweathermap.api.model.onecall.Temperature;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
-1
@@ -27,7 +27,6 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.github.prominence.openweathermap.api.utils;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class TestMappingUtils {
|
||||
|
||||
public static LocalDateTime parseDateTime(int seconds) {
|
||||
return LocalDateTime.ofInstant(Instant.ofEpochSecond(seconds), TimeZone.getDefault().toZoneId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user