Commit 317cc4c8 authored by Christopher Bohn's avatar Christopher Bohn 🤔
Browse files

Another test case for current weather, and bug fixes that it revealed

parent 6ea5196d
......@@ -17,9 +17,11 @@ public class Main {
System.err.println("\t" + ioException.getCause().getMessage());
}
OpenWeatherConnector weather = new OpenWeatherConnector("weather", apiKey);
// OpenWeatherConnector weather = new OpenWeatherConnector("weather");
String data = null;
try {
data = weather.retrieveData("zip=68588");
// data = weather.retrieveData("website-example.json");
} catch (IOException ioException) {
System.err.println("IO Exception: " + ioException.getClass());
System.err.println("\t" + ioException.getMessage());
......
......@@ -46,7 +46,7 @@ public class FileConnection implements Connection {
FileConnection.class.getClassLoader().getResourceAsStream(directory + filename)))) {
data = (JSONObject)new JSONParser().parse(inputStreamReader);
} catch (NullPointerException originalException) {
throw new IOException("Could not find " + filename + ".", originalException);
throw new IOException("Could not find " + directory + filename + ".", originalException);
}
return data;
}
......
......@@ -30,7 +30,23 @@ public class OpenWeatherConnector {
* @see <a href="https://openweathermap.org/weather-conditions">OpenWeathermap Weather Conditions</a>
*/
public enum WeatherCategory {
THUNDERSTORM, DRIZZLE, RAIN, SNOW, ATMOSPHERE, CLEAR, CLOUDS, UNKNOWN_CATEGORY,
THUNDERSTORM, // Group 2xx
DRIZZLE, // Group 3xx
RAIN, // Group 5xx
SNOW, // Group 6xx
// Group 7xx (Atmosphere)
MIST, // 701
SMOKE, // 711
HAZE, // 721
DUST, // 731, 761
FOG, // 741
SAND, // 751
ASH, // 762
SQUALL, // 771
TORNADO, // 781
CLEAR, // Group 800
CLOUDS, // Group 80x
UNKNOWN_CATEGORY,
}
/**
......@@ -413,7 +429,7 @@ public class OpenWeatherConnector {
* @throws IllegalStateException if no data has been retrieved using {@link #retrieveData(String)}
*/
public double getThreeHourRainfall() throws IllegalStateException {
return extractDoubleValue("rain", "3h", 0.0);
return extractDoubleValue("rain", "3h", getOneHourRainfall());
}
/**
......@@ -433,6 +449,6 @@ public class OpenWeatherConnector {
* @throws IllegalStateException if no data has been retrieved using {@link #retrieveData(String)}
*/
public double getThreeHourSnowfall() throws IllegalStateException {
return extractDoubleValue("snow", "3h", 0.0);
return extractDoubleValue("snow", "3h", getOneHourSnowfall());
}
}
package edu.unl.cse.soft160.json_connections;
import edu.unl.cse.soft160.json_connections.connector.OpenWeatherConnector;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class UnlOct24At0917Test {
private OpenWeatherConnector connector;
/* Demonstrates most fields; gust is distinct from wind speed; 3hr rain defaults to 1hr rain; no snow; multiple
weather categories */
@Before
public void setup() throws IOException {
connector = new OpenWeatherConnector("weather");
connector.retrieveData("unl-oct24-0917.json");
}
@Test
public void testLocation() {
double expectedLatitude = 40.8206;
double expectedLongitude = -96.6928;
double actualLatitude = connector.getLatitude();
double actualLongitude = connector.getLongitude();
assertEquals(expectedLatitude, actualLatitude, 0.0001);
assertEquals(expectedLongitude, actualLongitude, 0.0001);
}
@Test
public void testDateTime() {
// Date expectedValue = new Date(1635085053000L);
String expectedValue = "Sun Oct 24 09:17:33 CDT 2021";
Date actualValue = connector.getTimestamp();
assertEquals(expectedValue, actualValue.toString());
}
@Test
public void testWeather() {
List<OpenWeatherConnector.WeatherCategory> expectedCategories = Arrays.asList(
OpenWeatherConnector.WeatherCategory.MIST,
OpenWeatherConnector.WeatherCategory.RAIN,
OpenWeatherConnector.WeatherCategory.THUNDERSTORM
);
List<String> expectedDescriptions = Arrays.asList("mist", "light rain", "thunderstorm with light rain");
int expectedListSize = 3;
List<OpenWeatherConnector.WeatherCategory> actualCategories = connector.getWeatherCategories();
List<String> actualDescriptions = connector.getWeatherDescriptions();
assertEquals(expectedListSize, actualCategories.size());
assertEquals(expectedListSize, actualDescriptions.size());
assertEquals(expectedCategories, actualCategories);
assertEquals(expectedDescriptions, actualDescriptions);
}
@Test
public void testMainObservations() {
long expectedVisibility = 8047;
double expectedAmbientTemperature = 281.25;
long expectedHumidity = 96;
double expectedFeelsLikeTemperature = 279.92;
long expectedPressure = 1003;
long actualVisibility = connector.getVisibility();
double actualAmbientTemperature = connector.getTemperature();
long actualHumidity = connector.getHumidity();
double actualFeelsLikeTemperature = connector.getFeelsLike();
long actualPressure = connector.getPressure();
assertEquals(expectedVisibility, actualVisibility);
assertEquals(expectedAmbientTemperature, actualAmbientTemperature, 0.0001);
assertEquals(expectedHumidity, actualHumidity);
assertEquals(expectedFeelsLikeTemperature, actualFeelsLikeTemperature, 0.0001);
assertEquals(expectedPressure, actualPressure);
}
@Test
public void testWinds() {
long expectedDirection = 43;
double expectedSpeed = 2.24;
double expectedGust = 4.92;
long actualDirection = connector.getWindDirection();
double actualSpeed = connector.getWindSpeed();
double actualGust = connector.getWindGust();
assertEquals(expectedDirection, actualDirection);
assertEquals(expectedSpeed, actualSpeed, 0.0001);
assertEquals(expectedGust, actualGust, 0.0001);
}
@Test
public void testClouds() {
long expectedValue = 90;
long actualValue = connector.getCloudCover();
assertEquals(expectedValue, actualValue);
}
@Test
public void testPrecipitation() {
double expected1HourRain = 0.11;
double expected3HourRain = 0.11;
double expected1HourSnow = 0.0;
double expected3HourSnow = 0.0;
double actual1HourRain = connector.getOneHourRainfall();
double actual3HourRain = connector.getThreeHourRainfall();
double actual1HourSnow = connector.getOneHourSnowfall();
double actual3HourSnow = connector.getThreeHourSnowfall();
assertEquals(expected1HourRain, actual1HourRain, 0.0001);
assertEquals(expected3HourRain, actual3HourRain, 0.0001);
assertEquals(expected1HourSnow, actual1HourSnow, 0.0001);
assertEquals(expected3HourSnow, actual3HourSnow, 0.0001);
}
}
......@@ -11,9 +11,11 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class OfficialExampleTest {
public class WebsiteExampleTest {
private OpenWeatherConnector connector;
/* Demonstrates most fields; gust defaults to wind speed; no rain or snow; one weather category */
@Before
public void setup() throws IOException {
connector = new OpenWeatherConnector("weather");
......@@ -32,9 +34,10 @@ public class OfficialExampleTest {
@Test
public void testDateTime() {
Date expectedValue = new Date(1560350645000L);
// Date expectedValue = new Date(1560350645000L);
String expectedValue = "Wed Jun 12 09:44:05 CDT 2019";
Date actualValue = connector.getTimestamp();
assertEquals(expectedValue, actualValue);
assertEquals(expectedValue, actualValue.toString());
}
@Test
......@@ -85,7 +88,7 @@ public class OfficialExampleTest {
long actualValue = connector.getCloudCover();
assertEquals(expectedValue, actualValue);
}
@Test
public void testPrecipitation() {
double expected1HourRain = 0.0;
......
{
"rain" : {
"1h" : 0.11
},
"visibility" : 8047,
"timezone" : -18000,
"main" : {
"temp" : 281.25,
"temp_min" : 279.77,
"humidity" : 96,
"pressure" : 1003,
"feels_like" : 279.92,
"temp_max" : 282.33
},
"clouds" : {
"all" : 90
},
"sys" : {
"country" : "US",
"sunrise" : 1635079694,
"sunset" : 1635118400,
"id" : 2017531,
"type" : 2
},
"dt" : 1635085053,
"coord" : {
"lon" : -96.6928,
"lat" : 40.8206
},
"weather" : [
{
"icon" : "50d",
"description" : "mist",
"main" : "Mist",
"id" : 701
},
{
"icon" : "10d",
"description" : "light rain",
"main" : "Rain",
"id" : 500
},
{
"icon" : "11d",
"description" : "thunderstorm with light rain",
"main" : "Thunderstorm",
"id" : 200
}
],
"name" : "Lincoln",
"cod" : 200,
"id" : 0,
"base" : "stations",
"wind" : {
"deg" : 43,
"speed" : 2.24,
"gust" : 4.92
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment