feat: package refactor

This commit is contained in:
Martin Eyben 2024-12-02 10:07:16 +00:00
parent 4d96191d6a
commit f89bc7ae98
28 changed files with 119 additions and 123 deletions

View File

@ -1,5 +1,11 @@
package eirb.pg203;
import eirb.pg203.weather.display.WeatherDisplay;
import eirb.pg203.weather.display.WeatherDisplayBasic;
import eirb.pg203.weather.data.api.OpenMeteo;
import eirb.pg203.weather.data.api.OpenWeatherMap;
import eirb.pg203.weather.data.api.WeatherAPI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

View File

@ -1,7 +0,0 @@
package eirb.pg203.exceptions;
public class WeatherFetchingExceptionApi extends WeatherFetchingException{
public WeatherFetchingExceptionApi() {
super("An error occurred during API fetching");
}
}

View File

@ -1,7 +0,0 @@
package eirb.pg203.exceptions;
public class WeatherFetchingExceptionCityCoords extends WeatherFetchingException{
public WeatherFetchingExceptionCityCoords() {
super("Impossible to get city coords");
}
}

View File

@ -1,8 +1,9 @@
package eirb.pg203;
package eirb.pg203.weather.data;
import eirb.pg203.weather.utils.City;
import java.time.Instant;
import java.util.Locale;
import java.util.concurrent.locks.Condition;
/**
* Weather Data representation
@ -131,7 +132,7 @@ public class WeatherData {
}
WeatherData(City city, Instant date, float temp, float windSpeed, float windDirectionAngle, Condition condition) {
public WeatherData(City city, Instant date, float temp, float windSpeed, float windDirectionAngle, Condition condition) {
this.city = city;
this.date = date;
this.temp = temp;

View File

@ -1,10 +1,12 @@
package eirb.pg203;
package eirb.pg203.weather.data.api;
import eirb.pg203.exceptions.WeatherFetchingException;
import eirb.pg203.exceptions.WeatherFetchingExceptionApi;
import eirb.pg203.weather.utils.City;
import eirb.pg203.weather.exceptions.WeatherFetchingException;
import eirb.pg203.weather.exceptions.WeatherFetchingExceptionApi;
import eirb.pg203.weather.data.WeatherData;
import org.json.JSONObject;
import eirb.pg203.utils.JSONFetcher;
import eirb.pg203.weather.utils.JSONFetcher;
import java.io.IOException;
import java.net.URI;
@ -14,7 +16,7 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.Locale;
import eirb.pg203.WeatherData.Condition;
import eirb.pg203.weather.data.WeatherData.Condition;
// https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m

View File

@ -1,12 +1,14 @@
package eirb.pg203;
package eirb.pg203.weather.data.api;
import eirb.pg203.exceptions.WeatherFetchingException;
import eirb.pg203.exceptions.WeatherFetchingExceptionApi;
import eirb.pg203.exceptions.WeatherFetchingExceptionCityCoords;
import eirb.pg203.weather.utils.City;
import eirb.pg203.weather.exceptions.WeatherFetchingException;
import eirb.pg203.weather.exceptions.WeatherFetchingExceptionApi;
import eirb.pg203.weather.exceptions.WeatherFetchingExceptionCityCoords;
import eirb.pg203.weather.data.WeatherData;
import org.json.JSONObject;
import org.json.JSONArray;
import eirb.pg203.utils.JSONFetcher;
import eirb.pg203.weather.utils.JSONFetcher;
import java.io.IOException;
import java.net.URI;
@ -18,7 +20,7 @@ import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Locale;
import eirb.pg203.WeatherData.Condition;
import eirb.pg203.weather.data.WeatherData.Condition;
/**
* OpenWeatherMap api implementation
@ -29,7 +31,7 @@ public class OpenWeatherMap implements WeatherDataAPI {
Clock clock = Clock.systemUTC();
JSONFetcher JSONFetcher = new JSONFetcher();
OpenWeatherMap(String APIKey) {
public OpenWeatherMap(String APIKey) {
this.APIKey = APIKey;
}

View File

@ -1,12 +1,14 @@
package eirb.pg203;
package eirb.pg203.weather.data.api;
import eirb.pg203.exceptions.WeatherFetchingException;
import eirb.pg203.exceptions.WeatherFetchingExceptionApi;
import eirb.pg203.weather.utils.City;
import eirb.pg203.weather.exceptions.WeatherFetchingException;
import eirb.pg203.weather.exceptions.WeatherFetchingExceptionApi;
import eirb.pg203.weather.data.WeatherData;
import org.json.JSONArray;
import org.json.JSONObject;
import eirb.pg203.WeatherData.Condition;
import eirb.pg203.utils.JSONFetcher;
import eirb.pg203.weather.data.WeatherData.Condition;
import eirb.pg203.weather.utils.JSONFetcher;
import java.io.IOException;
import java.net.MalformedURLException;
@ -24,7 +26,7 @@ public class WeatherAPI implements WeatherDataAPI{
JSONFetcher JSONFetcher = new JSONFetcher();
private static final String forecastBaseURL = "https://api.weatherapi.com/v1/forecast.json";
WeatherAPI(String weatherAPIKey) {
public WeatherAPI(String weatherAPIKey) {
this.weatherAPIKey = weatherAPIKey;
}
@ -49,7 +51,7 @@ public class WeatherAPI implements WeatherDataAPI{
}
}
private static WeatherData.Condition getConditionFromString(String str) {
private static Condition getConditionFromString(String str) {
if (str.toLowerCase().contains("rain"))
return Condition.RAINY;

View File

@ -1,8 +1,8 @@
package eirb.pg203;
package eirb.pg203.weather.data.api;
import eirb.pg203.exceptions.WeatherFetchingException;
import eirb.pg203.weather.exceptions.WeatherFetchingException;
import eirb.pg203.weather.data.WeatherData;
import java.io.IOException;
import java.util.ArrayList;
/**

View File

@ -1,4 +1,6 @@
package eirb.pg203;
package eirb.pg203.weather.display;
import eirb.pg203.weather.data.api.WeatherDataAPI;
/**
* How to display weather information, make the API calls based on the collection of WheatherDataAPI

View File

@ -1,11 +1,13 @@
package eirb.pg203;
package eirb.pg203.weather.display;
import eirb.pg203.exceptions.WeatherFetchingException;
import eirb.pg203.weather.exceptions.WeatherFetchingException;
import eirb.pg203.weather.data.WeatherData;
import eirb.pg203.weather.data.api.WeatherDataAPI;
import java.util.ArrayList;
import java.util.HashMap;
class WeatherDisplayBasic implements WeatherDisplay {
public class WeatherDisplayBasic implements eirb.pg203.weather.display.WeatherDisplay {
/**
* List of apis
*/

View File

@ -1,4 +1,4 @@
package eirb.pg203.exceptions;
package eirb.pg203.weather.exceptions;
/**
* Exception when an error during the api call

View File

@ -0,0 +1,7 @@
package eirb.pg203.weather.exceptions;
public class WeatherFetchingExceptionApi extends eirb.pg203.weather.exceptions.WeatherFetchingException {
public WeatherFetchingExceptionApi() {
super("An error occurred during API fetching");
}
}

View File

@ -0,0 +1,7 @@
package eirb.pg203.weather.exceptions;
public class WeatherFetchingExceptionCityCoords extends eirb.pg203.weather.exceptions.WeatherFetchingException {
public WeatherFetchingExceptionCityCoords() {
super("Impossible to get city coords");
}
}

View File

@ -1,17 +1,16 @@
package eirb.pg203;
package eirb.pg203.weather.utils;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Locale;
import eirb.pg203.utils.JSONFetcher;
import eirb.pg203.weather.utils.Coords;
import eirb.pg203.weather.utils.JSONFetcher;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import eirb.pg203.utils.Coords;
/**
* Representation of a city
* Possibility to get city coordinates based on the name
@ -54,7 +53,7 @@ public class City {
return new Coords(lat, lon);
}
City (String cityName) {
public City (String cityName) {
this.cityName = cityName;
}

View File

@ -1,4 +1,4 @@
package eirb.pg203.utils;
package eirb.pg203.weather.utils;
/**
* Coordinates representation

View File

@ -1,4 +1,4 @@
package eirb.pg203.utils;
package eirb.pg203.weather.utils;
import java.io.BufferedReader;
import java.io.IOException;
@ -49,16 +49,4 @@ public class JSONFetcher {
return new JSONObject(result);
}
/**
* Fetch a Json array from an url
* @param url url
* @return JSON array
* @throws IOException when request failed
*/
public JSONArray fetchArray(URL url) throws IOException {
String result = fetchString(url);
return new JSONArray(result);
}
}

View File

@ -1,17 +1,19 @@
package eirb.pg203;
package eirb.pg203.weather;
import eirb.pg203.weather.display.WeatherDisplayBasic;
import eirb.pg203.weather.data.api.OpenMeteo;
import eirb.pg203.weather.data.api.OpenWeatherMap;
import eirb.pg203.weather.data.api.WeatherAPI;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.stream.Stream;
import static eirb.pg203.WeatherDataAPITest.*;
import static eirb.pg203.weather.data.api.WeatherDataAPITest.*;
public class WeatherDisplayBasicTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

View File

@ -1,5 +1,6 @@
package eirb.pg203;
package eirb.pg203.weather.data;
import eirb.pg203.weather.utils.City;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

View File

@ -1,16 +1,9 @@
package eirb.pg203;
package eirb.pg203.weather.data.api;
import eirb.pg203.fakeJSONFetcher.FakeJSONFetcherWeatherAPI;
import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherWeatherAPI;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.stream.Stream;
public class WeatherAPITest {
private static final String APIKey = "realKey";

View File

@ -1,17 +1,15 @@
package eirb.pg203;
package eirb.pg203.weather.data.api;
import eirb.pg203.exceptions.WeatherFetchingException;
import eirb.pg203.fakeJSONFetcher.FakeJSONFetcherOpenMeteo;
import eirb.pg203.fakeJSONFetcher.FakeJSONFetcherOpenWeatherMap;
import eirb.pg203.fakeJSONFetcher.FakeJSONFetcherWeatherAPI;
import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherOpenMeteo;
import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherOpenWeatherMap;
import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherWeatherAPI;
import eirb.pg203.weather.exceptions.WeatherFetchingException;
import eirb.pg203.weather.data.WeatherData;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.IOException;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
@ -22,13 +20,13 @@ public class WeatherDataAPITest {
private static final float epsilon = 0.01F;
private static final String APIKey = "realKey";
static WeatherAPI weatherAPI(){
public static WeatherAPI weatherAPI(){
WeatherAPI weatherAPI = new WeatherAPI(APIKey);
weatherAPI.JSONFetcher = new FakeJSONFetcherWeatherAPI();
return weatherAPI;
}
static OpenWeatherMap openWeatherMap(){
public static OpenWeatherMap openWeatherMap(){
// Fix clock for testing
String instantExpected = "2024-11-24T00:00:00.00Z";
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.systemDefault());
@ -39,7 +37,7 @@ public class WeatherDataAPITest {
return openWeatherMap;
}
static OpenMeteo openMeteo() {
public static OpenMeteo openMeteo() {
// Fix clock for testing
String instantExpected = "2024-11-24T00:00:00.00Z";
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.systemDefault());
@ -59,7 +57,7 @@ public class WeatherDataAPITest {
return Stream.of(
/* WeatherAPI */
Arguments.arguments(weatherAPI(), 0, 8.1F,WeatherData.Condition.PARTIAL, 17.45F, 142.08F),
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),

View File

@ -1,8 +1,8 @@
package eirb.pg203.fakeJSONFetcher;
package eirb.pg203.weather.fakeJSONFetcher;
import eirb.pg203.utils.FileResourcesUtils;
import eirb.pg203.utils.JSONFetcher;
import eirb.pg203.utils.SplitQueryUrl;
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;
@ -17,9 +17,9 @@ public class FakeJSONFetcherCity extends JSONFetcher{
private static HashMap<String, JSONObject> cities(){
HashMap<String, JSONObject> cities = new HashMap<>();
cities.put("bordeaux", FileResourcesUtils.getFileFromResourceAsJson("City/bordeaux.json"));
cities.put("paris", FileResourcesUtils.getFileFromResourceAsJson("City/paris.json"));
cities.put("unknown", FileResourcesUtils.getFileFromResourceAsJson("City/fakeCity.json"));
cities.put("bordeaux", eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("City/bordeaux.json"));
cities.put("paris", eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("City/paris.json"));
cities.put("unknown", eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("City/fakeCity.json"));
return cities;
}

View File

@ -1,7 +1,7 @@
package eirb.pg203.fakeJSONFetcher;
package eirb.pg203.weather.fakeJSONFetcher;
import eirb.pg203.utils.FileResourcesUtils;
import eirb.pg203.utils.JSONFetcher;
import eirb.pg203.weather.utils.FileResourcesUtils;
import eirb.pg203.weather.utils.JSONFetcher;
import org.json.JSONArray;
import org.json.JSONObject;

View File

@ -1,8 +1,8 @@
package eirb.pg203.fakeJSONFetcher;
package eirb.pg203.weather.fakeJSONFetcher;
import eirb.pg203.utils.FileResourcesUtils;
import eirb.pg203.utils.JSONFetcher;
import eirb.pg203.utils.SplitQueryUrl;
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;
@ -15,7 +15,7 @@ public class FakeJSONFetcherOpenWeatherMap extends JSONFetcher {
private final JSONObject wrongKeyResponse = FileResourcesUtils.getFileFromResourceAsJson("OpenWeatherMap/wrong-apikey.json");
private JSONObject responseExample() {
return FileResourcesUtils.getFileFromResourceAsJson("OpenWeatherMap/Bordeaux-partial-cloudy-rain-sunny.json");
return eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("OpenWeatherMap/Bordeaux-partial-cloudy-rain-sunny.json");
}
@Override
public JSONObject fetch(URL url) throws IOException {

View File

@ -1,8 +1,8 @@
package eirb.pg203.fakeJSONFetcher;
package eirb.pg203.weather.fakeJSONFetcher;
import eirb.pg203.utils.FileResourcesUtils;
import eirb.pg203.utils.JSONFetcher;
import eirb.pg203.utils.SplitQueryUrl;
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;
@ -17,10 +17,10 @@ public class FakeJSONFetcherWeatherAPI extends JSONFetcher {
private static final JSONObject wrongKeyRequest = FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/wrong-apikey.json");
private static ArrayList<JSONObject> bordeauxRequests() {
ArrayList<JSONObject> bordeauxRequest = new ArrayList<>();
bordeauxRequest.add(FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-1-partial.json"));
bordeauxRequest.add(FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-2-partial-sunny.json"));
bordeauxRequest.add(FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-3-partial-sunny-rain.json"));
bordeauxRequest.add(FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-4-partial-sunny-rain-cloudy.json"));
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;
}

View File

@ -1,7 +1,6 @@
package eirb.pg203;
package eirb.pg203.weather.utils;
import eirb.pg203.fakeJSONFetcher.FakeJSONFetcherCity;
import eirb.pg203.utils.Coords;
import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherCity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;

View File

@ -1,6 +1,5 @@
package eirb.pg203.utils;
package eirb.pg203.weather.utils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@ -14,7 +13,7 @@ class CoordsTest {
float lon = 2f;
Coords coords = new Coords(lat, lon);
Assertions.assertEquals(lat, coords.getLat(), epsilon);
assertEquals(lat, coords.getLat(), epsilon);
}
@Test
@ -23,7 +22,7 @@ class CoordsTest {
float lon = 2f;
Coords coords = new Coords(lat, lon);
Assertions.assertEquals(lon, coords.getLon(), epsilon);
assertEquals(lon, coords.getLon(), epsilon);
}
@Test
@ -35,7 +34,7 @@ class CoordsTest {
float sndLat = 3f;
coords.setLat(sndLat);
Assertions.assertEquals(sndLat, coords.getLat(), epsilon);
assertEquals(sndLat, coords.getLat(), epsilon);
}
@ -48,7 +47,7 @@ class CoordsTest {
float sndLon = 4f;
coords.setLon(sndLon);
Assertions.assertEquals(sndLon, coords.getLon(), epsilon);
assertEquals(sndLon, coords.getLon(), epsilon);
}
}

View File

@ -1,4 +1,4 @@
package eirb.pg203.utils;
package eirb.pg203.weather.utils;
import org.json.JSONObject;

View File

@ -1,4 +1,4 @@
package eirb.pg203.utils;
package eirb.pg203.weather.utils;
import java.io.UnsupportedEncodingException;
import java.net.URL;