package eirb.pg203.weather.data; import eirb.pg203.weather.utils.City; import org.json.JSONObject; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; 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.EnumSource; import org.junit.jupiter.params.provider.MethodSource; import static org.junit.jupiter.api.Assertions.assertTrue; import java.time.Instant; import java.util.ArrayList; import java.util.stream.Stream; public class WeatherDataTest { private static final float epsilon = 0.01F; private WeatherData weatherData; @BeforeEach public void setWeatherData() { this.weatherData = new WeatherData(new City("f"), Instant.now(), 0f, 0, 0, WeatherData.Condition.SUNNY); } @Test void city() { String cityName = "Paris"; City city = new City(cityName); this.weatherData.setCity(city); /* check if the setter works */ Assertions.assertEquals(weatherData.getCity(), city); } @Test void temp() { float temp = 69.69f; this.weatherData.setTemp(temp); /* check if the setter works */ Assertions.assertEquals(temp, weatherData.getTemp(), epsilon); } @ParameterizedTest(name = "Set {0}") @EnumSource void condition(WeatherData.Condition condition) { this.weatherData.setCondition(condition); /* check if the setter works */ Assertions.assertEquals(condition, weatherData.getCondition()); } private static Stream conditionFromString() { return Stream.of( Arguments.arguments("☀️", WeatherData.Condition.SUNNY), Arguments.arguments("☁️", WeatherData.Condition.CLOUDY), Arguments.arguments("🌤", WeatherData.Condition.PARTIAL), Arguments.arguments("🌧", WeatherData.Condition.RAINY), Arguments.arguments("E", WeatherData.Condition.ERROR) ); } @ParameterizedTest(name = "Condition {1} from string {0}") @MethodSource void conditionFromString(String conditionString, WeatherData.Condition expectedCondition) { WeatherData.Condition condition = WeatherData.Condition.fromString(conditionString); Assertions.assertEquals(expectedCondition, condition); } private static Stream windDirectionFromString() { return Stream.of( Arguments.arguments("🡩", WeatherData.WindDirection.N), Arguments.arguments("🡭", WeatherData.WindDirection.NE), Arguments.arguments("🡪", WeatherData.WindDirection.E), Arguments.arguments("🡮", WeatherData.WindDirection.SE), Arguments.arguments("🡫", WeatherData.WindDirection.S), Arguments.arguments("🡯", WeatherData.WindDirection.SW), Arguments.arguments("🡨", WeatherData.WindDirection.W), Arguments.arguments("🡬", WeatherData.WindDirection.NW), Arguments.arguments("E", WeatherData.WindDirection.ERROR), Arguments.arguments("fjlkre", WeatherData.WindDirection.ERROR) ); } @ParameterizedTest(name = "Wind direction {1} from string {0}") @MethodSource void windDirectionFromString(String windDirectionString, WeatherData.WindDirection expectedWindDirection) { WeatherData.WindDirection windDirection = WeatherData.WindDirection.fromString(windDirectionString); Assertions.assertEquals(expectedWindDirection, windDirection); } @Test void windSpeed() { float windSpeed = 69.69f; this.weatherData.setWindSpeed(windSpeed); /* check if the setter works */ Assertions.assertEquals(windSpeed, weatherData.getWindSpeed()); } @Test void windAngle() { float windAngle = 69.69f; this.weatherData.setWindDirectionAngle(windAngle); /* check if the setter works */ Assertions.assertEquals(windAngle, weatherData.getWindDirectionAngle()); } @Test void date() { Instant instant = Instant.now(); this.weatherData.setDate(instant); /* check if the setter works */ Assertions.assertEquals(instant, weatherData.getDate()); } @Test void stringRepresentation() { this.weatherData = new WeatherData(new City("f"), Instant.now(), 0f, 0, 0, WeatherData.Condition.SUNNY); Assertions.assertEquals("00.00° ☀️ 00.00km/h 000.00° 🡩", weatherData.toString()); } private static Stream windDirectionRepresentation() { return Stream.of( Arguments.arguments(0f, WeatherData.WindDirection.N), Arguments.arguments(45f, WeatherData.WindDirection.NE), Arguments.arguments(90f, WeatherData.WindDirection.E), Arguments.arguments(135f, WeatherData.WindDirection.SE), Arguments.arguments(180f, WeatherData.WindDirection.S), Arguments.arguments(225f, WeatherData.WindDirection.SW), Arguments.arguments(270f, WeatherData.WindDirection.W), Arguments.arguments(315f, WeatherData.WindDirection.NW), Arguments.arguments(999f, WeatherData.WindDirection.ERROR) ); } @ParameterizedTest @MethodSource void windDirectionRepresentation(float windDirection, WeatherData.WindDirection expectedWindDirection){ weatherData.setWindDirectionAngle(windDirection); Assertions.assertEquals(expectedWindDirection, weatherData.getWindDirection()); } @Test void toJSONTest() { Instant now = Instant.now(); float temp = 0f; WeatherData.Condition condition = WeatherData.Condition.SUNNY; float windSpeed = 0f; float windDirectionAngle = 0f; WeatherData weatherData = new WeatherData(new City("aCity"), now, temp, windSpeed, windDirectionAngle, condition); JSONObject equalsTo = new JSONObject(); equalsTo.put("city", "aCity"); equalsTo.put("date", now.toEpochMilli()); equalsTo.put("temp", temp); equalsTo.put("condition", condition.toString()); equalsTo.put("windSpeed", windSpeed); equalsTo.put("windDirectionAngle", windDirectionAngle); Assertions.assertEquals(equalsTo.toString(), weatherData.toJSON().toString()); WeatherData weatherData2 = WeatherData.fromJSON(equalsTo); Assertions.assertEquals(weatherData.toString(), weatherData2.toString()); } }