2024-12-02 10:07:16 +00:00

51 lines
1.8 KiB
Java

package eirb.pg203.weather.utils;
import eirb.pg203.weather.fakeJSONFetcher.FakeJSONFetcherCity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.io.IOException;
public class CityTest {
private final static float epsilon = 0.001F;
@ParameterizedTest(name = "{0} is located at [lat:{1}, lon:{2}]")
@CsvSource({
"Paris,48.859F,2.347F,'City(Paris, lat: 48.859001, lon: 2.347000)'",
"Bordeaux,44.851895F,-0.587877F,'City(Bordeaux, lat: 44.851894, lon: -0.587877)'"
})
void testRealCity(String cityName, float expectedLat, float expectedLon, String expectedString) throws IOException {
City city = new City(cityName);
city.JSONFetcher = new FakeJSONFetcherCity();
/* Name coherence */
Assertions.assertEquals(cityName, city.getCityName());
/* Localisation */
Coords coords = city.getCityCoords();
float lat = coords.getLat();
float lon = coords.getLon();
Assertions.assertTrue(Math.abs(lat - expectedLat) < epsilon);
Assertions.assertTrue(Math.abs(lon - expectedLon) < epsilon);
/* String representation */
Assertions.assertEquals(expectedString, city.toString());
}
@Test
void testFakeCity(){
String fakeCity = "farlmjmjfkl";
String fakeCityRepresentation = "City("+ fakeCity +", lat: Request failed, lon: Request Failed)";
City city = new City(fakeCity);
/* String representation */
Assertions.assertEquals(fakeCityRepresentation, city.toString());
/* Throw exception */
Assertions.assertThrows(IOException.class, city::getCityCoords);
}
}