feat: add weather data tests

This commit is contained in:
Martin Eyben 2024-11-24 12:45:45 +01:00
parent eb56bbfe73
commit 8f1e2649e1
4 changed files with 113 additions and 4 deletions

View File

@ -101,7 +101,7 @@ public class WeatherData {
private Condition condition; // cloudly, sunny ... private Condition condition; // cloudly, sunny ...
private float windSpeed; private float windSpeed;
private float windDirectionAngle; private float windDirectionAngle;
private final WindDirection windDirection; private WindDirection windDirection;
/** /**
@ -110,7 +110,7 @@ public class WeatherData {
* @return wind direction representation * @return wind direction representation
*/ */
private WindDirection getWindDirection(float windDirectionAngle) { private WindDirection getWindDirection(float windDirectionAngle) {
if (windDirectionAngle >= 337.5 || windDirectionAngle <= 22.5) if ((windDirectionAngle >= 337.5 && windDirectionAngle <= 360) || (windDirectionAngle >= 0 && windDirectionAngle <= 22.5))
return WindDirection.N; return WindDirection.N;
if (windDirectionAngle > 22.5 && windDirectionAngle <= 67.5) if (windDirectionAngle > 22.5 && windDirectionAngle <= 67.5)
return WindDirection.NE; return WindDirection.NE;
@ -241,6 +241,7 @@ public class WeatherData {
*/ */
public void setWindDirectionAngle(float windDirectionAngle) { public void setWindDirectionAngle(float windDirectionAngle) {
this.windDirectionAngle = windDirectionAngle; this.windDirectionAngle = windDirectionAngle;
this.windDirection = this.getWindDirection(windDirectionAngle);
} }
/** /**

View File

@ -105,7 +105,7 @@ public class WeatherDataAPITest {
float[] openMeteoExpectedTemperatures = {7.6F, 13.2F, 12.3F, 10.80F}; float[] openMeteoExpectedTemperatures = {7.6F, 13.2F, 12.3F, 10.80F};
WeatherData.Condition[] openMeteoExpectedConditions = {WeatherData.Condition.PARTIAL, WeatherData.Condition.CLOUDY, WeatherData.Condition.RAINY, WeatherData.Condition.SUNNY}; WeatherData.Condition[] openMeteoExpectedConditions = {WeatherData.Condition.PARTIAL, WeatherData.Condition.CLOUDY, WeatherData.Condition.RAINY, WeatherData.Condition.SUNNY};
float[] openMeteoExpectedWindSpeed = {17.6F, 20.6F, 21.7F, 9.40F}; float[] openMeteoExpectedWindSpeed = {17.6F, 20.6F, 21.7F, 9.40F};
float[] openMeteoExpectedWindDirection = {157.00F, 149F, 187F, 177F}; float[] openMeteoExpectedWindDirection = {151.00F, 149F, 187F, 177F};
return Stream.of( return Stream.of(
Arguments.arguments(weatherAPI(), 4, weatherAPIExpectedTemperatures, weatherAPIExpectedConditions, weatherAPIExpectedWindSpeed, weatherAPIExpectedWindDirection), Arguments.arguments(weatherAPI(), 4, weatherAPIExpectedTemperatures, weatherAPIExpectedConditions, weatherAPIExpectedWindSpeed, weatherAPIExpectedWindDirection),

View File

@ -0,0 +1,108 @@
package eirb.pg203;
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 java.time.Instant;
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());
}
@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<Arguments> 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());
}
}