feat: openMeteo tests

This commit is contained in:
Martin Eyben 2024-11-24 00:50:44 +01:00
parent 68abfe66fa
commit eb56bbfe73
4 changed files with 129 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import eirb.pg203.utils.JSONFetcher;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.time.Clock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Locale;
@ -23,7 +24,8 @@ import eirb.pg203.WeatherData.Condition;
public class OpenMeteo implements WeatherDataAPI {
private static final String forecastBaseURL = "https://api.open-meteo.com/v1/forecast";
private static final String dailyQuery = "weather_code,temperature_2m_max,temperature_2m_min,wind_speed_10m_max,wind_direction_10m_dominant";
private static final JSONFetcherInterface JSONFetcher = new JSONFetcher();
JSONFetcherInterface JSONFetcher = new JSONFetcher();
Clock clock = Clock.systemUTC();
/**
* Default constructor
@ -45,6 +47,9 @@ public class OpenMeteo implements WeatherDataAPI {
}
private static Condition getConditionFromCode(int WMOCode) {
// TODO Wesh Nemo c'est quoi cette merde ?
if (WMOCode == 2 )
return Condition.PARTIAL;
if (WMOCode < 20)
return Condition.SUNNY;
else if (WMOCode < 30)
@ -56,7 +61,7 @@ public class OpenMeteo implements WeatherDataAPI {
}
private static WeatherData getWeatherDataFromForecast(JSONObject response, int day, String city) {
private WeatherData getWeatherDataFromForecast(JSONObject response, int day, String city) {
JSONObject daily = response.getJSONObject("daily");
float max_temp = daily.getJSONArray("temperature_2m_max").getFloat(day);
float min_temp = daily.getJSONArray("temperature_2m_min").getFloat(day);
@ -68,7 +73,7 @@ public class OpenMeteo implements WeatherDataAPI {
return new WeatherData(
new City(city),
Instant.now(),
Instant.now(clock),
temp_c,
windSpeed,
windDirection,
@ -109,4 +114,9 @@ public class OpenMeteo implements WeatherDataAPI {
public String getAPIName() {
return "OpenMeteo";
}
@Override
public String toString() {
return this.getAPIName();
}
}

View File

@ -1,5 +1,6 @@
package eirb.pg203;
import eirb.pg203.fakeJSONFetcher.FakeJSONFetcherOpenMeteo;
import eirb.pg203.fakeJSONFetcher.FakeJSONFetcherOpenWeatherMap;
import eirb.pg203.fakeJSONFetcher.FakeJSONFetcherWeatherAPI;
import org.junit.jupiter.api.Assertions;
@ -37,6 +38,17 @@ public class WeatherDataAPITest {
return openWeatherMap;
}
private static OpenMeteo openMeteo() {
// Fix clock for testing
String instantExpected = "2024-11-24T00:00:00.00Z";
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.systemDefault());
OpenMeteo openMeteo = new OpenMeteo();
openMeteo.clock = clock;
openMeteo.JSONFetcher = new FakeJSONFetcherOpenMeteo();
return openMeteo;
}
/**
* List of args for Temperature testing
@ -45,14 +57,21 @@ public class WeatherDataAPITest {
private static Stream<Arguments> testGetTemperature(){
return Stream.of(
/* WeatherAPI */
Arguments.arguments(weatherAPI(), 0, 8.1F,WeatherData.Condition.PARTIAL, 17.45F, 142.08F),
Arguments.arguments(weatherAPI(), 1, 13F, WeatherData.Condition.SUNNY, 23.03F, 142.58F),
Arguments.arguments(weatherAPI(), 2, 12.7F, WeatherData.Condition.RAINY, 13.19F, 222.92F),
Arguments.arguments(weatherAPI(), 3, 8.1F,WeatherData.Condition.CLOUDY, 17.45F, 142.08F),
/* Open Weather Map */
Arguments.arguments(openWeatherMap(), 0, 13.41F,WeatherData.Condition.PARTIAL, 5.74F, 142.13F),
Arguments.arguments(openWeatherMap(), 1, 13.29F,WeatherData.Condition.CLOUDY, 3.62F, 225.25F),
Arguments.arguments(openWeatherMap(), 2, 10.06F,WeatherData.Condition.RAINY, 2.22F, 191.75F),
Arguments.arguments(openWeatherMap(), 3, 9.88F,WeatherData.Condition.SUNNY, 2.00F, 160.00F)
Arguments.arguments(openWeatherMap(), 3, 9.88F,WeatherData.Condition.SUNNY, 2.00F, 160.00F),
/* Open Meteo */
Arguments.arguments(openMeteo(), 0, 7.6F,WeatherData.Condition.PARTIAL, 17.6F, 151F),
Arguments.arguments(openMeteo(), 1, 13.20F,WeatherData.Condition.CLOUDY, 20.6F, 149F),
Arguments.arguments(openMeteo(), 2, 12.3F,WeatherData.Condition.RAINY, 21.70F, 187F),
Arguments.arguments(openMeteo(), 3, 10.80F,WeatherData.Condition.SUNNY, 9.4F, 177F)
);
}
@ -83,9 +102,15 @@ public class WeatherDataAPITest {
float[] openWeatherMapExpectedWindSpeed = {5.74F, 3.62F, 2.22F, 2.00F};
float[] openWeatherMapExpectedWindDirection = {142.13F, 225.25F, 191.75F, 160F};
float[] openMeteoExpectedTemperatures = {7.6F, 13.2F, 12.3F, 10.80F};
WeatherData.Condition[] openMeteoExpectedConditions = {WeatherData.Condition.PARTIAL, WeatherData.Condition.CLOUDY, WeatherData.Condition.RAINY, WeatherData.Condition.SUNNY};
float[] openMeteoExpectedWindSpeed = {17.6F, 20.6F, 21.7F, 9.40F};
float[] openMeteoExpectedWindDirection = {157.00F, 149F, 187F, 177F};
return Stream.of(
Arguments.arguments(weatherAPI(), 4, weatherAPIExpectedTemperatures, weatherAPIExpectedConditions, weatherAPIExpectedWindSpeed, weatherAPIExpectedWindDirection),
Arguments.arguments(openWeatherMap(), 4, openWeatherMapExpectedTemperatures, openWeatherMapExpectedConditions, openWeatherMapExpectedWindSpeed, openWeatherMapExpectedWindDirection)
Arguments.arguments(openWeatherMap(), 4, openWeatherMapExpectedTemperatures, openWeatherMapExpectedConditions, openWeatherMapExpectedWindSpeed, openWeatherMapExpectedWindDirection),
Arguments.arguments(openMeteo(), 4, openMeteoExpectedTemperatures, openMeteoExpectedConditions, openMeteoExpectedWindSpeed, openMeteoExpectedWindDirection)
);
}
@ -115,9 +140,15 @@ public class WeatherDataAPITest {
return Stream.of(
Arguments.arguments(weatherAPI()),
Arguments.arguments(openWeatherMap())
Arguments.arguments(openWeatherMap()),
Arguments.arguments(openMeteo())
);
}
/**
* For coverage, not yet implemented
* @param weatherDataAPI api to test
*/
@ParameterizedTest(name = "[{0}] Get temperature for a specific hour")
@MethodSource
public void testGetTemperatureByHours(WeatherDataAPI weatherDataAPI) {

View File

@ -0,0 +1,27 @@
package eirb.pg203.fakeJSONFetcher;
import eirb.pg203.utils.FileResourcesUtils;
import eirb.pg203.utils.JSONFetcherInterface;
import eirb.pg203.utils.SplitQueryUrl;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
public class FakeJSONFetcherOpenMeteo implements JSONFetcherInterface {
private JSONObject responseExample() {
return FileResourcesUtils.getFileFromResourceAsJson("OpenMeteo/Bordeaux-partial-cloudy-rain-sunny.json");
}
@Override
public JSONObject fetch(URL url) throws IOException {
return responseExample();
}
@Override
public JSONArray fetchArray(URL url) throws IOException {
return null;
}
}

View File

@ -0,0 +1,55 @@
{
"latitude": 44.84,
"longitude": -0.58000016,
"generationtime_ms": 0.1360177993774414,
"utc_offset_seconds": 0,
"timezone": "GMT",
"timezone_abbreviation": "GMT",
"elevation": 15,
"daily_units": {
"time": "iso8601",
"weather_code": "wmo code",
"temperature_2m_max": "°C",
"temperature_2m_min": "°C",
"wind_speed_10m_max": "km/h",
"wind_direction_10m_dominant": "°"
},
"daily": {
"time": [
"2024-11-23",
"2024-11-24",
"2024-11-25",
"2024-11-26"
],
"weather_code": [
2,
3,
63,
0
],
"temperature_2m_max": [
12.3,
17.2,
15.4,
13.7
],
"temperature_2m_min": [
2.9,
9.2,
9.2,
7.9
],
"wind_speed_10m_max": [
17.6,
20.6,
21.7,
9.4
],
"wind_direction_10m_dominant": [
151,
149,
187,
177
]
}
}