test: add tests for WeatherAPI

This commit is contained in:
Nemo D'ACREMONT 2024-11-09 16:45:00 +01:00
parent e1eb03c622
commit dc6fbd8c4e
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276
2 changed files with 71 additions and 2 deletions

View File

@ -10,8 +10,6 @@ public class SampleTest {
@Test @Test
public void testFetchChuckNorrisJoke() throws IOException { public void testFetchChuckNorrisJoke() throws IOException {
JSONObject res = Main.fetchChuckNorrisJoke();
Assertions.assertTrue(res.has("value"));
} }
} }

View File

@ -0,0 +1,71 @@
package eirb.pg203;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class WeatherAPITest {
private static String APIKey = "cef8e1b6ea364994b5072423240111";
@Test
public void testRightAPIKey() {
WeatherAPI weatherAPI = new WeatherAPI(WeatherAPITest.APIKey);
int day = 0;
// int hour = 10;
int days = 7;
String city = "Bordeaux";
Assertions.assertAll(
() -> weatherAPI.getTemperature(day, city),
// () -> weatherAPI.getTemperature(day, hour, city),
() -> weatherAPI.getTemperatures(days, city)
);
}
@Test
public void testWrongAPIKey() {
WeatherAPI weatherAPI = new WeatherAPI("");
int day = 0;
// int hour = 10;
int days = 7;
String city = "Bordeaux";
Assertions.assertThrows(IOException.class, () -> weatherAPI.getTemperature(day, city));
Assertions.assertThrows(IOException.class, () -> weatherAPI.getTemperatures(days, city));
}
@Test
public void testWrongDay() {
WeatherAPI weatherAPI = new WeatherAPI(WeatherAPITest.APIKey);
String city = "Bordeaux";
Assertions.assertThrows(IOException.class, () -> weatherAPI.getTemperature(-1, city));
Assertions.assertThrows(IOException.class, () -> weatherAPI.getTemperature(15, city));
Assertions.assertThrows(IOException.class, () -> weatherAPI.getTemperatures(15, city));
Assertions.assertThrows(IOException.class, () -> weatherAPI.getTemperatures(-1, city));
}
@Test
public void testRightDay() {
WeatherAPI weatherAPI = new WeatherAPI(WeatherAPITest.APIKey);
String city = "Bordeaux";
Assertions.assertAll(
() -> weatherAPI.getTemperature(0, city),
() -> weatherAPI.getTemperature(5, city),
() -> weatherAPI.getTemperature(14, city),
() -> weatherAPI.getTemperatures(0, city),
() -> weatherAPI.getTemperatures(8, city),
() -> weatherAPI.getTemperatures(14, city)
);
}
@Test
public void testGetAPIName() {
WeatherAPI weatherAPI = new WeatherAPI(WeatherAPITest.APIKey);
Assertions.assertTrue(weatherAPI.getAPIName().equals("WeatherAPI"));
}
}