Commit e35a5cf7 authored by Christopher Bohn's avatar Christopher Bohn 🤔
Browse files

Added Air Pollution wrapper methods

parent e32067f9
package edu.unl.cse.soft160.json_connections;
import edu.unl.cse.soft160.json_connections.connection.FileConnection;
import edu.unl.cse.soft160.json_connections.connection.RestConnection;
import edu.unl.cse.soft160.json_connections.connector.OpenWeatherConnector;
import org.json.simple.parser.ParseException;
import java.io.IOException;
......@@ -16,11 +18,14 @@ public class Main {
System.err.println("Caused by: " + ioException.getCause());
System.err.println("\t" + ioException.getCause().getMessage());
}
/* Using OpenWeatherConnector */
/*
// OpenWeatherConnector weather = new OpenWeatherConnector("weather", apiKey);
OpenWeatherConnector weather = new OpenWeatherConnector("air_pollution/history", apiKey);
String data = null;
try {
data = weather.retrieveData("lat=40.8194698&lon=-96.7066454&start=1606223802&end=1606482999");
data = weather.retrieveData("lat=40.81506358&lon=-96.7048613&start=1606223802&end=1606482999");
// data = weather.retrieveData("website-example.json");
} catch (IOException ioException) {
System.err.println("IO Exception: " + ioException.getClass());
......@@ -33,5 +38,17 @@ public class Main {
System.out.println("Temperature: " + weather.getTemperature() + "K");
System.out.println("Cloud cover: " + weather.getCloudCover() + "%");
System.out.println("Timestamp: " + weather.getTimestamp());
*/
/* Creating file copies of live data */
RestConnection restConnection = new RestConnection("http", "api.openweathermap.org", "/data/2.5/air_pollution/history", apiKey);
FileConnection fileConnection = new FileConnection("air_pollution");
try {
fileConnection.save("air_pollution-unl-historical.json",
restConnection.get("lat=40.81506358&lon=-96.7048613&start=1606223802&end=1606482999"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
package edu.unl.cse.soft160.json_connections;
import edu.unl.cse.soft160.json_connections.connector.OpenWeatherConnector;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class AirPollution_UnlNov24ToNov27 {
private OpenWeatherConnector connector;
private List<Date> timestamps;
@Before
public void setup() throws IOException {
connector = new OpenWeatherConnector("air_pollution/history");
connector.retrieveData("air_pollution-unl-historical.json");
timestamps = connector.getTimeStamps();
}
@Test
public void testLocation() {
double expectedLatitude = 40.8151;
double expectedLongitude = -96.7049;
double actualLatitude = connector.getLatitude();
double actualLongitude = connector.getLongitude();
assertEquals(expectedLatitude, actualLatitude, 0.0001);
assertEquals(expectedLongitude, actualLongitude, 0.0001);
}
@Test
public void testAirQualityIndex() {
// We'll pick one of the hours that has AQI==2
OpenWeatherConnector.AirQuality expectedValue = OpenWeatherConnector.AirQuality.LOW;
OpenWeatherConnector.AirQuality actualValue = connector.getAirQualityIndex(timestamps.get(25));
assertEquals(expectedValue, actualValue);
}
@Test
public void testCO() {
double expectedValue = 240.33;
double actualValue = connector.getCarbonMonoxide(timestamps.get(0));
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testNO() {
// This is interesting. Normally a double, this report has NO as 0 instead of 0.0.
double expectedValue = 0.0;
double actualValue = connector.getNitrogenMonoxide(timestamps.get(1));
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testNO2() {
double expectedValue = 3.64;
double actualValue = connector.getNitrogenDioxide(timestamps.get(2));
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testO3() {
double expectedValue = 55.08;
double actualValue = connector.getOzone(timestamps.get(3));
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testSO2() {
double expectedValue = 0.08;
double actualValue = connector.getSulfurDioxide(timestamps.get(60));
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testPM2_5() {
double expectedValue = 1.32;
double actualValue = connector.getFineParticulateMatter(timestamps.get(59));
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testPM10() {
double expectedValue = 1.78;
double actualValue = connector.getCoarseParticulateMatter(timestamps.get(58));
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testNH3() {
double expectedValue = 1.85;
double actualValue = connector.getAmmonia(timestamps.get(57));
assertEquals(expectedValue, actualValue, 0.0001);
}
}
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.Date;
import static org.junit.Assert.assertEquals;
public class AirPollution_WebsiteExampleTest {
private OpenWeatherConnector connector;
@Before
public void setup() throws IOException {
connector = new OpenWeatherConnector("air_pollution");
connector.retrieveData("website-example.json");
}
@Test
public void testLocation() {
double expectedLatitude = 50.0;
double expectedLongitude = 50.0;
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(1605182400000L);
String expectedValue = "Thu Nov 12 06:00:00 CST 2020";
Date actualValue = connector.getTimestamp();
assertEquals(expectedValue, actualValue.toString());
}
@Test
public void testAirQualityIndex() {
OpenWeatherConnector.AirQuality expectedValue = OpenWeatherConnector.AirQuality.VERY_LOW;
OpenWeatherConnector.AirQuality actualValue = connector.getAirQualityIndex();
assertEquals(expectedValue, actualValue);
}
@Test
public void testCO() {
double expectedValue = 201.94053649902344;
double actualValue = connector.getCarbonMonoxide();
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testNO() {
double expectedValue = 0.01877197064459324;
double actualValue = connector.getNitrogenMonoxide();
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testNO2() {
double expectedValue = 0.7711350917816162;
double actualValue = connector.getNitrogenDioxide();
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testO3() {
double expectedValue = 68.66455078125;
double actualValue = connector.getOzone();
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testSO2() {
double expectedValue = 0.6407499313354492;
double actualValue = connector.getSulfurDioxide();
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testPM2_5() {
double expectedValue = 0.5;
double actualValue = connector.getFineParticulateMatter();
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testPM10() {
double expectedValue = 0.540438711643219;
double actualValue = connector.getCoarseParticulateMatter();
assertEquals(expectedValue, actualValue, 0.0001);
}
@Test
public void testNH3() {
double expectedValue = 0.12369127571582794;
double actualValue = connector.getAmmonia();
assertEquals(expectedValue, actualValue, 0.0001);
}
}
......@@ -11,7 +11,7 @@ import java.util.List;
import static org.junit.Assert.assertEquals;
public class UnlOct24At0917Test {
public class Weather_UnlOct24At0917Test {
private OpenWeatherConnector connector;
/* Demonstrates most fields; gust is distinct from wind speed; 3hr rain defaults to 1hr rain; no snow; multiple
......
......@@ -11,7 +11,7 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class WebsiteExampleTest {
public class Weather_WebsiteExampleTest {
private OpenWeatherConnector connector;
/* Demonstrates most fields; gust defaults to wind speed; no rain or snow; one weather category */
......
{
"_comment" : {
"text1" : "The original coord field in the website example is a list of Longs:",
"coord" : [
50,
50
],
"text2" : "I have modified this to reflect the actual format returned by REST calls"
},
"coord" : {
"lon" : 50.0,
"lat" : 50.0
},
"list" : [
{
"dt" : 1605182400,
"main" : {
"aqi" : 1
},
"components" : {
"co" : 201.94053649902344,
"no" : 0.01877197064459324,
"no2" : 0.7711350917816162,
"o3" : 68.66455078125,
"so2" : 0.6407499313354492,
"pm2_5" : 0.5,
"pm10" : 0.540438711643219,
"nh3" : 0.12369127571582794
}
}
]
}
\ No newline at end of file
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