weather-aggregator/src/test/java/eirb/pg203/weather/fakeJSONFetcher/FakeJSONFetcherWeatherAPI.java
2024-12-02 10:20:29 +00:00

38 lines
1.8 KiB
Java

package eirb.pg203.weather.fakeJSONFetcher;
import eirb.pg203.weather.utils.FileResourcesUtils;
import eirb.pg203.weather.utils.SplitQueryUrl;
import eirb.pg203.weather.utils.JSONFetcher;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
public class FakeJSONFetcherWeatherAPI extends JSONFetcher {
private final static String baseUrlFormat = "https://api.weatherapi.com/v1/forecast.json?key=%s&q=%s&days=%d";
private final String apiKey = "realKey";
private static final JSONObject wrongKeyRequest = FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/wrong-apikey.json");
private static ArrayList<JSONObject> bordeauxRequests() {
ArrayList<JSONObject> bordeauxRequest = new ArrayList<>();
bordeauxRequest.add(eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-1-partial.json"));
bordeauxRequest.add(eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-2-partial-sunny.json"));
bordeauxRequest.add(eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-3-partial-sunny-rain.json"));
bordeauxRequest.add(eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-4-partial-sunny-rain-cloudy.json"));
return bordeauxRequest;
}
@Override
public JSONObject fetch(URL url) throws IOException {
Map<String, String> params = SplitQueryUrl.splitQuery(url);
int days = Integer.parseInt(params.get("days"));
if (!params.get("key").contentEquals(apiKey))
return wrongKeyRequest;
return bordeauxRequests().get(days - 1);
}
}