diff --git a/src/main/java/eirb/pg203/WeatherAPI.java b/src/main/java/eirb/pg203/WeatherAPI.java index 6e30274..d4f1d83 100644 --- a/src/main/java/eirb/pg203/WeatherAPI.java +++ b/src/main/java/eirb/pg203/WeatherAPI.java @@ -111,4 +111,9 @@ public class WeatherAPI implements WeatherDataAPI{ public String getAPIName() { return "WeatherAPI"; } + + @Override + public String toString() { + return this.getAPIName(); + } } diff --git a/src/test/java/eirb/pg203/WeatherAPITest.java b/src/test/java/eirb/pg203/WeatherAPITest.java index 248870e..0a29db9 100644 --- a/src/test/java/eirb/pg203/WeatherAPITest.java +++ b/src/test/java/eirb/pg203/WeatherAPITest.java @@ -23,35 +23,6 @@ public class WeatherAPITest { this.weatherAPI.JSONFetcher = new FakeJSONFetcherWeatherAPI(); } - /** - * List of args for Temperature testing - * @return Args for testing - */ - private static Stream testGetTemperature(){ - return Stream.of( - Arguments.arguments(0, 8.1F,WeatherData.Condition.PARTIAL, 17.45F, 142.08F), - Arguments.arguments(1, 13F, WeatherData.Condition.SUNNY, 23.03F, 142.58F), - Arguments.arguments(2, 12.7F, WeatherData.Condition.RAINY, 13.19F, 222.92F), - Arguments.arguments(3, 8.1F,WeatherData.Condition.CLOUDY, 17.45F, 142.08F) - ); - } - - @ParameterizedTest(name="Temperature fetch at Bordeaux D+{0}") - @MethodSource - public void testGetTemperature(int day, float expectedTemp, WeatherData.Condition expectedCond, float expectedWindSpeed, float expectedWindAngle) throws IOException { - String city = "Bordeaux"; - WeatherData weatherData; - weatherData = weatherAPI.getTemperature(day, city); - - /* Temperatures */ - Assertions.assertEquals(expectedTemp, weatherData.getTemp()); - /* Condition */ - Assertions.assertEquals(expectedCond, weatherData.getCondition()); - /* Wind */ - Assertions.assertTrue(expectedWindSpeed - weatherData.getWindSpeed() < epsilon); - Assertions.assertTrue(expectedWindAngle - weatherData.getWindDirectionAngle() < epsilon); - } - /** * For coverage (hour not yet implemented) * @throws IOException never @@ -65,34 +36,6 @@ public class WeatherAPITest { } - @Test - @DisplayName("Multiple day temperature fetch") - public void testGetTemperatures() throws IOException { - String city = "Bordeaux"; - int days = 3; - float[] expectedTemperatures = {8.1F, 13F, 12.7F, 8.1F}; - WeatherData.Condition[] expectedConditions = {WeatherData.Condition.PARTIAL, WeatherData.Condition.SUNNY, WeatherData.Condition.RAINY, WeatherData.Condition.CLOUDY}; - float[] expectedWindSpeed = {17.45F, 23.03F, 13.19F, 17.45F}; - float[] expectedWindDirection = {142.08F, 142.58F, 222.92F, 142.08F}; - - ArrayList weatherDatas; - WeatherData weatherData; - weatherDatas = weatherAPI.getTemperatures(days, city); - - for (int index = 0; index < days; index++) { - weatherData = weatherDatas.get(index); - - - /* Temperatures */ - Assertions.assertEquals(expectedTemperatures[index], weatherData.getTemp()); - /* Weather condition */ - Assertions.assertEquals(expectedConditions[index], weatherData.getCondition()); - /* Wind */ - Assertions.assertTrue(expectedWindSpeed[index] - weatherData.getWindSpeed() < epsilon); - Assertions.assertTrue(expectedWindDirection[index] - weatherData.getWindDirectionAngle() < epsilon); - } - } - @Test public void testGetAPIName() { Assertions.assertEquals("WeatherAPI", weatherAPI.getAPIName()); diff --git a/src/test/java/eirb/pg203/WeatherDataAPITest.java b/src/test/java/eirb/pg203/WeatherDataAPITest.java new file mode 100644 index 0000000..8826cc7 --- /dev/null +++ b/src/test/java/eirb/pg203/WeatherDataAPITest.java @@ -0,0 +1,86 @@ +package eirb.pg203; + +import eirb.pg203.fakeJSONFetcher.FakeJSONFetcherWeatherAPI; +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.util.ArrayList; +import java.util.stream.Stream; + +public class WeatherDataAPITest { + private static final float epsilon = 0.01F; + private static final String APIKey = "realKey"; + + private static WeatherAPI weatherAPI(){ + WeatherAPI weatherAPI = new WeatherAPI(APIKey); + weatherAPI.JSONFetcher = new FakeJSONFetcherWeatherAPI(); + return weatherAPI; + } + + /** + * List of args for Temperature testing + * @return Args for testing + */ + private static Stream testGetTemperature(){ + + return Stream.of( + 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) + ); + } + + @ParameterizedTest(name="[{0}] Temperature fetch at Bordeaux D+{1}") + @MethodSource + public void testGetTemperature(WeatherDataAPI weatherDataAPI, int day, float expectedTemp, WeatherData.Condition expectedCond, float expectedWindSpeed, float expectedWindAngle) throws IOException { + String city = "Bordeaux"; + WeatherData weatherData; + weatherData = weatherDataAPI.getTemperature(day, city); + + /* Temperatures */ + Assertions.assertEquals(expectedTemp, weatherData.getTemp()); + /* Condition */ + Assertions.assertEquals(expectedCond, weatherData.getCondition()); + /* Wind */ + Assertions.assertTrue(expectedWindSpeed - weatherData.getWindSpeed() < epsilon); + Assertions.assertTrue(expectedWindAngle - weatherData.getWindDirectionAngle() < epsilon); + } + + private static Stream testGetTemperatures() { + float[] weatherAPIExpectedTemperatures = {8.1F, 13F, 12.7F, 8.1F}; + WeatherData.Condition[] weatherAPIExpectedConditions = {WeatherData.Condition.PARTIAL, WeatherData.Condition.SUNNY, WeatherData.Condition.RAINY, WeatherData.Condition.CLOUDY}; + float[] weatherAPIExpectedWindSpeed = {17.45F, 23.03F, 13.19F, 17.45F}; + float[] weatherAPIExpectedWindDirection = {142.08F, 142.58F, 222.92F, 142.08F}; + return Stream.of( + Arguments.arguments(weatherAPI(), 4, weatherAPIExpectedTemperatures, weatherAPIExpectedConditions, weatherAPIExpectedWindSpeed, weatherAPIExpectedWindDirection) + ); + + } + @ParameterizedTest(name = "[{0}] Fetch temperatures for {1} days") + @MethodSource + public void testGetTemperatures(WeatherDataAPI weatherDataAPI, int days, float[] expectedTemperatures, WeatherData.Condition[] expectedConditions, float[] expectedWindSpeed, float[] expectedWindDirection) throws IOException { + String city = "Bordeaux"; + + ArrayList weatherDatas; + WeatherData weatherData; + weatherDatas = weatherDataAPI.getTemperatures(days, city); + + for (int index = 0; index < days; index++) { + weatherData = weatherDatas.get(index); + + /* Temperatures */ + Assertions.assertEquals(expectedTemperatures[index], weatherData.getTemp()); + /* Weather condition */ + Assertions.assertEquals(expectedConditions[index], weatherData.getCondition()); + /* Wind */ + Assertions.assertTrue(expectedWindSpeed[index] - weatherData.getWindSpeed() < epsilon); + Assertions.assertTrue(expectedWindDirection[index] - weatherData.getWindDirectionAngle() < epsilon); + } + } +}