Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
rlanning2
OpenWeather REST and file connector
Commits
6ea5196d
Commit
6ea5196d
authored
Oct 23, 2021
by
Christopher Bohn
🤔
Browse files
Added tests for example weather response provided on openweathermap.org
parent
6b4add03
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/edu/unl/cse/soft160/json_connections/Main.java
View file @
6ea5196d
...
...
@@ -20,8 +20,11 @@ public class Main {
String
data
=
null
;
try
{
data
=
weather
.
retrieveData
(
"zip=68588"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
ioException
)
{
System
.
err
.
println
(
"IO Exception: "
+
ioException
.
getClass
());
System
.
err
.
println
(
"\t"
+
ioException
.
getMessage
());
System
.
err
.
println
(
"Caused by: "
+
ioException
.
getCause
());
System
.
err
.
println
(
"\t"
+
ioException
.
getCause
().
getMessage
());
}
System
.
out
.
println
(
data
);
System
.
out
.
println
(
weather
.
getWeatherCategories
());
...
...
src/main/java/edu/unl/cse/soft160/json_connections/connector/OpenWeatherConnector.java
View file @
6ea5196d
...
...
@@ -70,11 +70,11 @@ public class OpenWeatherConnector {
case
ONECALL:
case
FORECAST:
case
AIR_POLLUTION:
REST_PATH
=
"/data/2.5"
;
REST_PATH
=
"/data/2.5
/
"
;
break
;
case
DIRECT:
case
REVERSE:
REST_PATH
=
"geo/1.0"
;
REST_PATH
=
"
/
geo/1.0
/
"
;
break
;
default
:
REST_PATH
=
"unknown"
;
...
...
@@ -102,11 +102,11 @@ public class OpenWeatherConnector {
case
ONECALL:
case
FORECAST:
case
AIR_POLLUTION:
REST_PATH
=
"/data/2.5"
;
REST_PATH
=
"/data/2.5
/
"
;
break
;
case
DIRECT:
case
REVERSE:
REST_PATH
=
"geo/1.0"
;
REST_PATH
=
"
/
geo/1.0
/
"
;
break
;
default
:
REST_PATH
=
"unknown"
;
...
...
@@ -163,7 +163,7 @@ public class OpenWeatherConnector {
private
void
checkForDataPresence
()
throws
IllegalStateException
{
if
(
data
==
null
)
{
throw
new
IllegalStateException
(
"No data has been retrieved from the "
+
dataSet
.
toString
().
toLowerCase
()
+
"dataset."
);
+
dataSet
.
toString
().
toLowerCase
()
+
"
dataset."
);
}
}
...
...
@@ -182,7 +182,7 @@ public class OpenWeatherConnector {
double
returnValue
=
defaultValue
;
if
(
data
.
containsKey
(
topLevelField
))
{
JSONObject
observations
=
(
JSONObject
)
data
.
get
(
topLevelField
);
if
(
data
.
containsKey
(
subField
))
{
if
(
observations
.
containsKey
(
subField
))
{
Double
value
=
(
Double
)
observations
.
get
(
subField
);
returnValue
=
value
==
null
?
defaultValue
:
value
;
}
...
...
@@ -205,7 +205,7 @@ public class OpenWeatherConnector {
long
returnValue
=
defaultValue
;
if
(
data
.
containsKey
(
topLevelField
))
{
JSONObject
observations
=
(
JSONObject
)
data
.
get
(
topLevelField
);
if
(
data
.
containsKey
(
subField
))
{
if
(
observations
.
containsKey
(
subField
))
{
Long
value
=
(
Long
)
observations
.
get
(
subField
);
returnValue
=
value
==
null
?
defaultValue
:
value
;
}
...
...
@@ -238,7 +238,7 @@ public class OpenWeatherConnector {
* @see #getTimestamp()
*/
public
double
getLongitude
()
throws
IllegalStateException
{
return
extractDoubleValue
(
"coord"
,
"lon
g
"
,
Double
.
NaN
);
return
extractDoubleValue
(
"coord"
,
"lon"
,
Double
.
NaN
);
}
/**
...
...
src/test/java/edu/unl/cse/soft160/json_connections/OfficialExampleTest.java
0 → 100644
View file @
6ea5196d
package
edu.unl.cse.soft160.json_connections
;
import
edu.unl.cse.soft160.json_connections.connector.OpenWeatherConnector
;
import
java.io.IOException
;
import
java.util.Date
;
import
java.util.List
;
import
org.junit.Before
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.*;
public
class
OfficialExampleTest
{
private
OpenWeatherConnector
connector
;
@Before
public
void
setup
()
throws
IOException
{
connector
=
new
OpenWeatherConnector
(
"weather"
);
connector
.
retrieveData
(
"website-example.json"
);
}
@Test
public
void
testLocation
()
{
double
expectedLatitude
=
37.39
;
double
expectedLongitude
=
-
122.08
;
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
(
1560350645000L
);
Date
actualValue
=
connector
.
getTimestamp
();
assertEquals
(
expectedValue
,
actualValue
);
}
@Test
public
void
testWeather
()
{
OpenWeatherConnector
.
WeatherCategory
expectedCategory
=
OpenWeatherConnector
.
WeatherCategory
.
CLEAR
;
String
expectedDescription
=
"clear sky"
;
int
expectedListSize
=
1
;
List
<
OpenWeatherConnector
.
WeatherCategory
>
actualCategories
=
connector
.
getWeatherCategories
();
List
<
String
>
actualDescriptions
=
connector
.
getWeatherDescriptions
();
assertEquals
(
expectedListSize
,
actualCategories
.
size
());
assertEquals
(
expectedListSize
,
actualDescriptions
.
size
());
assertEquals
(
expectedCategory
,
actualCategories
.
get
(
0
));
assertEquals
(
expectedDescription
,
actualDescriptions
.
get
(
0
));
}
@Test
public
void
testMainObservations
()
{
long
expectedVisibility
=
16093
;
double
expectedAmbientTemperature
=
282.55
;
long
expectedHumidity
=
100
;
double
expectedFeelsLikeTemperature
=
281.86
;
long
actualVisibility
=
connector
.
getVisibility
();
double
actualAmbientTemperature
=
connector
.
getTemperature
();
long
actualHumidity
=
connector
.
getHumidity
();
double
actualFeelsLikeTemperature
=
connector
.
getFeelsLike
();
assertEquals
(
expectedVisibility
,
actualVisibility
);
assertEquals
(
expectedAmbientTemperature
,
actualAmbientTemperature
,
0.0001
);
assertEquals
(
expectedHumidity
,
actualHumidity
);
assertEquals
(
expectedFeelsLikeTemperature
,
actualFeelsLikeTemperature
,
0.0001
);
}
@Test
public
void
testWinds
()
{
long
expectedDirection
=
350
;
double
expectedSpeed
=
1.5
;
double
expectedGust
=
1.5
;
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
=
1
;
long
actualValue
=
connector
.
getCloudCover
();
assertEquals
(
expectedValue
,
actualValue
);
}
@Test
public
void
testPrecipitation
()
{
double
expected1HourRain
=
0.0
;
double
expected3HourRain
=
0.0
;
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
);
}
}
src/test/resources/weather/website-example.json
0 → 100644
View file @
6ea5196d
{
"coord"
:
{
"lon"
:
-122.08
,
"lat"
:
37.39
},
"weather"
:
[
{
"id"
:
800
,
"main"
:
"Clear"
,
"description"
:
"clear sky"
,
"icon"
:
"01d"
}
],
"base"
:
"stations"
,
"main"
:
{
"temp"
:
282.55
,
"feels_like"
:
281.86
,
"temp_min"
:
280.37
,
"temp_max"
:
284.26
,
"pressure"
:
1023
,
"humidity"
:
100
},
"visibility"
:
16093
,
"wind"
:
{
"speed"
:
1.5
,
"deg"
:
350
},
"clouds"
:
{
"all"
:
1
},
"dt"
:
1560350645
,
"sys"
:
{
"type"
:
1
,
"id"
:
5122
,
"message"
:
0.0139
,
"country"
:
"US"
,
"sunrise"
:
1560343627
,
"sunset"
:
1560396563
},
"timezone"
:
-25200
,
"id"
:
420006353
,
"name"
:
"Mountain View"
,
"cod"
:
200
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment