feat: enhance tests

This commit is contained in:
Martin Eyben 2024-12-02 11:27:17 +00:00
parent 43425a742b
commit 138d65ae42
4 changed files with 57 additions and 3 deletions

View File

@ -1,5 +1,6 @@
package eirb.pg203.weather.data.api; package eirb.pg203.weather.data.api;
import eirb.pg203.weather.exceptions.WeatherFetchingExceptionCityCoords;
import eirb.pg203.weather.utils.City; import eirb.pg203.weather.utils.City;
import eirb.pg203.weather.exceptions.WeatherFetchingException; import eirb.pg203.weather.exceptions.WeatherFetchingException;
import eirb.pg203.weather.exceptions.WeatherFetchingExceptionApi; import eirb.pg203.weather.exceptions.WeatherFetchingExceptionApi;
@ -47,7 +48,7 @@ public class OpenMeteo implements WeatherDataAPI {
) )
).toURL(); ).toURL();
} catch (IOException e) { } catch (IOException e) {
throw new WeatherFetchingException("Impossible to get city coords"); throw new WeatherFetchingExceptionCityCoords();
} }
try { try {

View File

@ -1,5 +1,7 @@
package eirb.pg203.weather.data.api; package eirb.pg203.weather.data.api;
import eirb.pg203.weather.exceptions.WeatherFetchingExceptionApi;
import eirb.pg203.weather.exceptions.WeatherFetchingExceptionCityCoords;
import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherOpenMeteo; import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherOpenMeteo;
import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherOpenWeatherMap; import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherOpenWeatherMap;
import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherWeatherAPI; import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherWeatherAPI;
@ -19,6 +21,18 @@ import java.util.stream.Stream;
public class WeatherDataAPITest { public class WeatherDataAPITest {
private static final float epsilon = 0.01F; private static final float epsilon = 0.01F;
private static final String APIKey = "realKey"; private static final String APIKey = "realKey";
private static final String wrongAPIKey = "wrongKey";
private static WeatherAPI wrongWeatherAPI() {
WeatherAPI weatherAPI = new WeatherAPI(wrongAPIKey);
weatherAPI.JSONFetcher = new FakeJSONFetcherWeatherAPI();
return weatherAPI;
}
private static OpenWeatherMap wrongOpenWeatherMap() {
OpenWeatherMap weatherAPI = new OpenWeatherMap(wrongAPIKey);
weatherAPI.JSONFetcher = new FakeJSONFetcherOpenWeatherMap();
return weatherAPI;
}
public static WeatherAPI weatherAPI(){ public static WeatherAPI weatherAPI(){
WeatherAPI weatherAPI = new WeatherAPI(APIKey); WeatherAPI weatherAPI = new WeatherAPI(APIKey);
@ -155,6 +169,45 @@ public class WeatherDataAPITest {
Assertions.assertAll( Assertions.assertAll(
() -> weatherDataAPI.getTemperature(0,1, city) () -> weatherDataAPI.getTemperature(0,1, city)
); );
}
private static Stream<Arguments> testUnkowncity() {
return Stream.of(
Arguments.arguments(openWeatherMap()),
Arguments.arguments(openMeteo())
);
}
@ParameterizedTest(name = "[{0}] Get temperature from missing city")
@MethodSource
public void testUnkowncity(WeatherDataAPI weatherDataAPI) {
String unknownCity = "jlkfreajflkj";
Assertions.assertThrows(WeatherFetchingExceptionCityCoords.class,
() -> weatherDataAPI.getTemperature(3, unknownCity)
);
Assertions.assertThrows(WeatherFetchingExceptionCityCoords.class,
() -> weatherDataAPI.getTemperatures(3, unknownCity)
);
}
private static Stream<Arguments> testWrongApiKey() {
return Stream.of(
Arguments.arguments(wrongWeatherAPI()),
Arguments.arguments(wrongOpenWeatherMap())
);
}
@ParameterizedTest(name = "[{0}] Wrong API Key throws exception")
@MethodSource
public void testWrongApiKey(WeatherDataAPI weatherDataAPI) {
String city = "Bordeaux";
Assertions.assertThrows(WeatherFetchingExceptionApi.class,
() -> weatherDataAPI.getTemperatures(3, city)
);
Assertions.assertThrows(WeatherFetchingExceptionApi.class,
() -> weatherDataAPI.getTemperature(3, city)
);
} }
} }

View File

@ -21,7 +21,7 @@ public class FakeJSONFetcherOpenWeatherMap extends JSONFetcher {
public JSONObject fetch(URL url) throws IOException { public JSONObject fetch(URL url) throws IOException {
Map<String, String> params = SplitQueryUrl.splitQuery(url); Map<String, String> params = SplitQueryUrl.splitQuery(url);
if (!params.getOrDefault("appid", "").contentEquals(apiKey)) if (!params.getOrDefault("appid", "").contentEquals(apiKey))
return wrongKeyResponse; throw new IOException();
return responseExample(); return responseExample();
} }
} }

View File

@ -30,7 +30,7 @@ public class FakeJSONFetcherWeatherAPI extends JSONFetcher {
int days = Integer.parseInt(params.get("days")); int days = Integer.parseInt(params.get("days"));
if (!params.get("key").contentEquals(apiKey)) if (!params.get("key").contentEquals(apiKey))
return wrongKeyRequest; throw new IOException();
return bordeauxRequests().get(days - 1); return bordeauxRequests().get(days - 1);
} }