forked from openskynetwork/opensky-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestOpenSkyApi.java
More file actions
122 lines (105 loc) · 4.14 KB
/
TestOpenSkyApi.java
File metadata and controls
122 lines (105 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import org.junit.Test;
import org.opensky.api.OpenSkyApi;
import org.opensky.model.OpenSkyStates;
import org.opensky.model.StateVector;
import java.io.IOException;
import static org.junit.Assert.*;
/**
* @author Markus Fuchs, fuchs@opensky-network.org
*/
public class TestOpenSkyApi {
/** credentials to test authenticated API calls **/
static final String USERNAME = null;
static final String PASSWORD = null;
// serials which belong to the given account
static final Integer[] SERIALS = null;
@Test
public void testAnonGetStates() throws IOException, InterruptedException {
OpenSkyApi api = new OpenSkyApi();
long t0 = System.nanoTime();
OpenSkyStates os = api.getStates(0, null);
long t1 = System.nanoTime();
System.out.println("Request anonStates time = " + ((t1 - t0) / 1000000) + "ms");
assertTrue("More than 1 state vector", os.getStates().size() > 1);
int time = os.getTime();
// more than two requests withing ten seconds
os = api.getStates(0, null);
assertNull("No new data", os);
// wait ten seconds
Thread.sleep(10000);
// now we can retrieve states again
t0 = System.nanoTime();
os = api.getStates(0, null);
t1 = System.nanoTime();
System.out.println("Request anonStates time = " + ((t1 - t0) / 1000000) + "ms");
assertNotNull(os);
assertTrue("More than 1 state vector for second valid request", os.getStates().size() > 1);
assertNotEquals(time, os.getTime());
}
// can only be tested with a valid account
@Test
public void testAuthGetStates() throws IOException, InterruptedException {
if (USERNAME == null || PASSWORD == null) {
System.out.println("WARNING: testAuthGetStates needs valid credentials and did not run");
return;
}
OpenSkyApi api = new OpenSkyApi(USERNAME, PASSWORD);
OpenSkyStates os = api.getStates(0, null);
assertTrue("More than 1 state vector", os.getStates().size() > 1);
int time = os.getTime();
// more than two requests withing ten seconds
os = api.getStates(0, null);
assertNull("No new data", os);
// wait five seconds
Thread.sleep(5000);
// now we can retrieve states again
long t0 = System.nanoTime();
os = api.getStates(0, null);
long t1 = System.nanoTime();
System.out.println("Request authStates time = " + ((t1 - t0) / 1000000) + "ms");
assertNotNull(os);
assertTrue("More than 1 state vector for second valid request", os.getStates().size() > 1);
assertNotEquals(time, os.getTime());
}
@Test
public void testAnonGetMyStates() {
OpenSkyApi api = new OpenSkyApi();
try {
api.getMyStates(0, null, null);
fail("Anonymous access of 'myStates' expected");
} catch (IllegalAccessError iae) {
// like expected
assertTrue("Mismatched exception message",
iae.getMessage().equals("Anonymous access of 'myStates' not allowed"));
} catch (IOException e) {
fail("Request should not be submitted");
}
}
@Test
public void testAuthGetMyStates() throws IOException {
/* DEBUG output:
System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.impl.conn", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.impl.client", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.client", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "DEBUG");
*/
if (USERNAME == null || PASSWORD == null) {
System.out.println("WARNING: testAuthGetMyStates needs valid credentials and did not run");
return;
}
OpenSkyApi api = new OpenSkyApi(USERNAME, PASSWORD);
OpenSkyStates os = api.getMyStates(0, null, SERIALS);
assertTrue("More than 1 state vector", os.getStates().size() > 1);
for (StateVector sv : os.getStates()) {
// all states contain at least one of the user's sensors
boolean gotOne = false;
for (Integer ser : SERIALS) {
gotOne = gotOne || sv.getSerials().contains(ser);
}
assertTrue(gotOne);
}
}
}