diff --git a/src/main/java/eirb/pg203/City.java b/src/main/java/eirb/pg203/City.java index 40f3158..e89e682 100644 --- a/src/main/java/eirb/pg203/City.java +++ b/src/main/java/eirb/pg203/City.java @@ -9,6 +9,7 @@ import java.net.URL; import java.util.Locale; import eirb.pg203.utils.JSONFetcher; +import eirb.pg203.utils.JSONFetcherInterface; import org.json.JSONArray; import org.json.JSONObject; @@ -21,6 +22,7 @@ import eirb.pg203.utils.Coords; public class City { private String cityName; private Coords cityCoords; + private static final JSONFetcherInterface JSONFetcher = new JSONFetcher(); /** * Fetch data from adresse.data.gouv.fr diff --git a/src/main/java/eirb/pg203/OpenMeteo.java b/src/main/java/eirb/pg203/OpenMeteo.java index bbe5b00..dab51de 100644 --- a/src/main/java/eirb/pg203/OpenMeteo.java +++ b/src/main/java/eirb/pg203/OpenMeteo.java @@ -1,5 +1,6 @@ package eirb.pg203; +import eirb.pg203.utils.JSONFetcherInterface; import org.json.JSONArray; import org.json.JSONObject; @@ -22,6 +23,7 @@ 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(); /** * Default constructor diff --git a/src/main/java/eirb/pg203/OpenWeatherMap.java b/src/main/java/eirb/pg203/OpenWeatherMap.java index c79ffa4..9a54da3 100644 --- a/src/main/java/eirb/pg203/OpenWeatherMap.java +++ b/src/main/java/eirb/pg203/OpenWeatherMap.java @@ -1,5 +1,6 @@ package eirb.pg203; +import eirb.pg203.utils.JSONFetcherInterface; import org.json.JSONObject; import org.json.JSONArray; @@ -22,6 +23,7 @@ import eirb.pg203.WeatherData.Condition; public class OpenWeatherMap implements WeatherDataAPI { private static final String forecastBaseURL = "https://api.openweathermap.org/data/2.5/forecast"; private String APIKey; + private static final JSONFetcherInterface JSONFetcher = new JSONFetcher(); OpenWeatherMap(String APIKey) { this.APIKey = APIKey; diff --git a/src/main/java/eirb/pg203/WeatherAPI.java b/src/main/java/eirb/pg203/WeatherAPI.java index 383a55c..9abd91e 100644 --- a/src/main/java/eirb/pg203/WeatherAPI.java +++ b/src/main/java/eirb/pg203/WeatherAPI.java @@ -1,5 +1,6 @@ package eirb.pg203; +import eirb.pg203.utils.JSONFetcherInterface; import org.json.JSONArray; import org.json.JSONObject; @@ -18,7 +19,8 @@ import java.util.Locale; */ public class WeatherAPI implements WeatherDataAPI{ private final String weatherAPIKey; - private static final String forecastBaseURL = "https://api.weatherapi.com/v1/forecast.json"; + private static final JSONFetcherInterface JSONFetcher = new JSONFetcher(); + private static final String forecastBaseURL = "https://api.weatherapi.com/v1/forecast.json"; WeatherAPI(String weatherAPIKey) { this.weatherAPIKey = weatherAPIKey; diff --git a/src/main/java/eirb/pg203/utils/JSONFetcher.java b/src/main/java/eirb/pg203/utils/JSONFetcher.java index 079a0b8..16484a9 100644 --- a/src/main/java/eirb/pg203/utils/JSONFetcher.java +++ b/src/main/java/eirb/pg203/utils/JSONFetcher.java @@ -12,12 +12,12 @@ import org.json.JSONObject; /** * Util for http calls */ -public class JSONFetcher { +public class JSONFetcher implements JSONFetcherInterface{ /** * No need for constructor */ - private JSONFetcher() {}; + public JSONFetcher() {}; /** * Make the request * @param url url to fetch @@ -44,7 +44,8 @@ public class JSONFetcher { * @return Json object of the response * @throws IOException if the request failed */ - public static JSONObject fetch(URL url) throws IOException { + @Override + public JSONObject fetch(URL url) throws IOException { String result = fetchString(url); return new JSONObject(result); @@ -56,7 +57,8 @@ public class JSONFetcher { * @return JSON array * @throws IOException when request failed */ - public static JSONArray fetchArray(URL url) throws IOException { + @Override + public JSONArray fetchArray(URL url) throws IOException { String result = fetchString(url); return new JSONArray(result); diff --git a/src/main/java/eirb/pg203/utils/JSONFetcherInterface.java b/src/main/java/eirb/pg203/utils/JSONFetcherInterface.java new file mode 100644 index 0000000..0adbd33 --- /dev/null +++ b/src/main/java/eirb/pg203/utils/JSONFetcherInterface.java @@ -0,0 +1,16 @@ +package eirb.pg203.utils; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.IOException; +import java.net.URL; + +/** + * Interface use to be overrided by a fake jsonFetcher during tests + */ +public interface JSONFetcherInterface { + JSONObject fetch(URL url) throws IOException; + + JSONArray fetchArray(URL url) throws IOException; +}