-
Notifications
You must be signed in to change notification settings - Fork 0
/
states_test.go
509 lines (426 loc) · 14.8 KB
/
states_test.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package gopensky_test
import (
"context"
"errors"
"fmt"
"strings"
"github.com/h2non/gock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/navidys/gopensky"
)
var _ = Describe("States", func() {
Describe("GetStates", func() {
It("retrieve state vectors for a given time", func() {
conn, err := gopensky.NewConnection(context.Background(), "", "")
Expect(err).NotTo(HaveOccurred())
defer gock.Off()
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/all_states.json")
gclient, err := gopensky.GetClient(conn)
Expect(err).NotTo(HaveOccurred())
gock.InterceptClient(gclient)
states, err := gopensky.GetStates(conn, 0, nil, nil, false)
Expect(err).NotTo(HaveOccurred())
Expect(len(states.States)).To(Equal(6))
firstState := states.States[0]
Expect(firstState.Icao24).To(Equal("ac96b8"))
Expect(strings.TrimSpace(*firstState.Callsign)).To(Equal("AAL2423"))
Expect(firstState.OriginCountry).To(Equal("United States"))
Expect(*firstState.TimePosition).To(Equal(int64(1518552809)))
Expect(firstState.LastContact).To(Equal(int64(1518552809)))
Expect(*firstState.Longitude).To(Equal(-93.4581))
Expect(*firstState.Latitude).To(Equal(44.9529))
Expect(*firstState.BaroAltitude).To(Equal(1150.62))
Expect(firstState.OnGround).To(Equal(false))
Expect(*firstState.Velocity).To(Equal(116.59))
Expect(*firstState.TrueTrack).To(Equal(94.3))
Expect(*firstState.VerticalRate).To(Equal(float64(0)))
Expect(firstState.Sensors).To(BeNil())
Expect(*firstState.GeoAltitude).To(Equal(float64(1143)))
Expect(*firstState.Squawk).To(Equal("2236"))
Expect(firstState.Spi).To(Equal(false))
Expect(firstState.PositionSource).To(Equal(0))
})
})
Describe("GetStates - errors", func() {
It("tests getstates errors", func() {
conn, err := gopensky.NewConnection(context.Background(), "", "")
Expect(err).NotTo(HaveOccurred())
gclient, err := gopensky.GetClient(conn)
Expect(err).NotTo(HaveOccurred())
gock.InterceptClient(gclient)
_, err = gopensky.GetStates(conn, -1, nil, nil, false)
Expect(err).To(Equal(gopensky.ErrInvalidUnixTime))
_, err = gopensky.GetStates(context.Background(), 0, nil, nil, false)
Expect(err.Error()).To(ContainSubstring("invalid context key"))
defer gock.Off()
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/invalid").
Reply(200).
BodyString("")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(err.Error()).To(ContainSubstring("do request: Get"))
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
BodyString("[{}]")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("json: cannot unmarshal"))
// data count error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states01.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(Equal("invalid state vector data count"))
// icao24 assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states02.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("icao24 assertion"))
// callsign assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states03.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("callsign assertion"))
// country assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states04.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("origin country assertion"))
// time position assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states05.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("time position assertion"))
// last contact assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states06.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("last contact assertion"))
// longitude assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states07.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("longitude assertion"))
// latitude assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states08.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("latitude assertion"))
// baro altitude assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states09.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("baro altitude assertion"))
// velocity assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states10.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("velocity assertion"))
// true track assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states11.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("true track assertion"))
// vertical rate assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states12.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("vertical rate assertion"))
// geo altitude assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states13.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("geo altitude assertion"))
// squawk assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states14.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("squawk assertion"))
// spi assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states15.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("spi assertion"))
// position source assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states16.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("position source assertion"))
// on ground assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states17.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("on ground assertion"))
// sensors assertion error
gock.New(gopensky.OpenSkyAPIURL).
Get("/states/all").
Reply(200).
File("mock_data/errors/states18.json")
_, err = gopensky.GetStates(conn, 0, nil, nil, false)
Expect(errors.Unwrap(err).Error()).To(ContainSubstring("sensors assertion"))
})
})
Describe("getStateRequestParams", func() {
It("generate state request parameters", func() {
tests := []struct {
time int64
icao24 []string
bBox *gopensky.BoundingBoxOptions
extended bool
}{
{time: -1, icao24: nil, bBox: nil, extended: false},
{time: 0, icao24: nil, bBox: nil, extended: false},
{time: 2, icao24: []string{"icao24_a", "icao24_b"}, bBox: gopensky.NewBoundingBox(1.1, 1.2, 1, 1), extended: false},
{time: 2, icao24: []string{"icao24_a", "icao24_b"}, bBox: gopensky.NewBoundingBox(2.2111, 2.1, 0, 1), extended: true},
}
for _, reqParams := range tests {
urlVal := gopensky.GetStateRequestParams(reqParams.time, reqParams.icao24, reqParams.bBox, reqParams.extended)
if reqParams.time > 0 {
reqtime := fmt.Sprintf("%d", reqParams.time)
Expect(urlVal.Get("time")).To(Equal(reqtime))
}
if len(reqParams.icao24) > 0 {
for _, iaco24 := range reqParams.icao24 {
Expect(urlVal["icao24"]).Should(ContainElement(ContainSubstring(iaco24)))
}
}
if reqParams.bBox != nil {
lamax := gopensky.FloatToString(reqParams.bBox.Lamax)
lamin := gopensky.FloatToString(reqParams.bBox.Lamin)
lomax := gopensky.FloatToString(reqParams.bBox.Lomax)
lomin := gopensky.FloatToString(reqParams.bBox.Lomin)
Expect(urlVal.Get("lamax")).To(Equal(lamax))
Expect(urlVal.Get("lamin")).To(Equal(lamin))
Expect(urlVal.Get("lomax")).To(Equal(lomax))
Expect(urlVal.Get("lomin")).To(Equal(lomin))
}
if reqParams.extended {
Expect(urlVal.Get("extended")).To(Equal("1"))
}
}
})
})
Describe("decodeRawStateVector", func() {
It("decode state vector array interface to struct", func() {
testStringValue := "test_str"
testIntValue := 1
testTimeValue := int64(1)
testFloatValue := -46.509700
sensors := []int{1, 2}
tests := []struct {
have []interface{}
wants gopensky.StateVector
}{
{
have: []interface{}{testStringValue},
wants: gopensky.StateVector{}},
{
have: []interface{}{
testStringValue,
testStringValue,
testStringValue,
float64(testTimeValue),
float64(testTimeValue),
testFloatValue,
testFloatValue,
testFloatValue,
false,
testFloatValue,
testFloatValue,
testFloatValue,
sensors,
testFloatValue,
testStringValue,
false,
float64(testIntValue),
float64(testIntValue),
},
wants: gopensky.StateVector{
Icao24: testStringValue,
Callsign: &testStringValue,
OriginCountry: testStringValue,
TimePosition: &testTimeValue,
LastContact: testTimeValue,
Longitude: &testFloatValue,
Latitude: &testFloatValue,
BaroAltitude: &testFloatValue,
OnGround: false,
Velocity: &testFloatValue,
TrueTrack: &testFloatValue,
VerticalRate: &testFloatValue,
Sensors: sensors,
GeoAltitude: &testFloatValue,
Squawk: &testStringValue,
Spi: false,
PositionSource: testIntValue,
Category: testIntValue,
},
},
{
have: []interface{}{
testStringValue,
nil,
testStringValue,
nil,
float64(testTimeValue),
nil,
nil,
nil,
false,
nil,
nil,
nil,
sensors,
nil,
nil,
false,
float64(testIntValue),
float64(testIntValue),
},
wants: gopensky.StateVector{
Icao24: testStringValue,
Callsign: nil,
OriginCountry: testStringValue,
TimePosition: nil,
LastContact: testTimeValue,
Longitude: nil,
Latitude: nil,
BaroAltitude: nil,
OnGround: false,
Velocity: nil,
TrueTrack: nil,
VerticalRate: nil,
Sensors: sensors,
GeoAltitude: nil,
Squawk: nil,
Spi: false,
PositionSource: testIntValue,
Category: testIntValue,
},
},
}
for _, decodeTest := range tests {
stvec, err := gopensky.DecodeRawStateVector(decodeTest.have)
if len(decodeTest.have) < 18 {
Expect(err).To(HaveOccurred())
Expect(stvec).To(BeNil())
continue
}
Expect(err).ToNot(HaveOccurred())
Expect(stvec).ToNot(BeNil())
decodeData := *(stvec)
// icao24
Expect(decodeData.Icao24).To(Equal(decodeTest.wants.Icao24))
// Callsign
if decodeTest.wants.Callsign != nil {
Expect(*(decodeData.Callsign)).To(Equal(testStringValue))
} else {
Expect(decodeData.Callsign).To(BeNil())
}
// OriginCountry
Expect(decodeData.OriginCountry).To(Equal(decodeTest.wants.OriginCountry))
// TimePosition
if decodeTest.wants.TimePosition != nil {
Expect(*(decodeData.TimePosition)).To(Equal(testTimeValue))
} else {
Expect(decodeData.TimePosition).To(BeNil())
}
// LastContact
Expect(decodeData.LastContact).To(Equal(decodeTest.wants.LastContact))
// Longitude
if decodeTest.wants.Longitude != nil {
Expect(*(decodeData.Longitude)).To(Equal(testFloatValue))
} else {
Expect(decodeData.Longitude).To(BeNil())
}
// Latitude
if decodeTest.wants.Latitude != nil {
Expect(*(decodeData.Latitude)).To(Equal(testFloatValue))
} else {
Expect(decodeData.Latitude).To(BeNil())
}
// BaroAltitude
if decodeTest.wants.BaroAltitude != nil {
Expect(*(decodeData.BaroAltitude)).To(Equal(testFloatValue))
} else {
Expect(decodeData.BaroAltitude).To(BeNil())
}
// OnGround
Expect(decodeData.OnGround).To(Equal(decodeTest.wants.OnGround))
// Velocity
if decodeTest.wants.Velocity != nil {
Expect(*(decodeData.Velocity)).To(Equal(testFloatValue))
} else {
Expect(decodeData.Velocity).To(BeNil())
}
// TrueTrack
if decodeTest.wants.TrueTrack != nil {
Expect(*(decodeData.TrueTrack)).To(Equal(testFloatValue))
} else {
Expect(decodeData.TrueTrack).To(BeNil())
}
// VerticalRate
if decodeTest.wants.VerticalRate != nil {
Expect(*(decodeData.VerticalRate)).To(Equal(testFloatValue))
} else {
Expect(decodeData.VerticalRate).To(BeNil())
}
// GeoAltitude
if decodeTest.wants.GeoAltitude != nil {
Expect(*(decodeData.GeoAltitude)).To(Equal(testFloatValue))
} else {
Expect(decodeData.GeoAltitude).To(BeNil())
}
// Squawk
if decodeTest.wants.Squawk != nil {
Expect(*(decodeData.Squawk)).To(Equal(testStringValue))
} else {
Expect(decodeData.Squawk).To(BeNil())
}
// Spi
Expect(decodeData.Spi).To(Equal(decodeTest.wants.Spi))
// PositionSource
Expect(decodeData.PositionSource).To(Equal(decodeTest.wants.PositionSource))
// Category
Expect(decodeData.Category).To(Equal(decodeTest.wants.Category))
}
})
})
})