-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers_test.go
388 lines (318 loc) · 12.1 KB
/
handlers_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
package main
import (
"bytes"
"github.com/julienschmidt/httprouter"
"net/http"
"net/http/httptest"
"testing"
"time"
//"fmt"
)
func Test_checkForFaults(t *testing.T) {
utc, err := time.LoadLocation("UTC")
if err != nil {
t.Fatalf("Could not create time location: %v", err)
}
dt := time.Date(2016, 12, 20, 10, 28, 50, 0, utc) //year int, month Month, day, hour, min, sec, nsec int, loc *Location
sd := SensorData{Location: "IIT Tower Vault 1 SW Corner", Serial_Number: 1,
Temperature: 120.1, Pressure: 760.2, Humidity: 80.2,
Date_Time: dt, Water_Level: 20}
ml := MetricLimits{100.0, -20.0, 0.7, 0.0, 160, 10}
f := checkForFaults(sd, ml)
available_sensors := [4]string{"Temperature", "Humidity", "Pressure", "Water level"}
for idx, val := range f.FaultMessages {
if val != available_sensors[idx]+" exceeded threshold!" {
t.Fatalf("First message is not abbout %s its: %v", available_sensors[idx],
f.FaultMessages[0])
}
}
}
func Test_checkForFaults_Nil(t *testing.T) {
utc, err := time.LoadLocation("UTC")
if err != nil {
t.Fatalf("Could not create time location: %v", err)
}
dt := time.Date(2016, 12, 20, 10, 28, 50, 0, utc) //year int, month Month, day, hour, min, sec, nsec int, loc *Location
sd := SensorData{Location: "IIT Tower Vault 1 SW Corner", Serial_Number: 1,
Temperature: 20.1, Pressure: 120.2, Humidity: .2,
Date_Time: dt, Water_Level: 2}
ml := MetricLimits{100.0, -20.0, 0.7, 0.0, 160, 10}
f := checkForFaults(sd, ml)
if f != nil {
t.Fatalf("All sensor data were within range, still returned exceeds threshold: %v", err)
}
}
func Test_GET_Sensor_Data_Empty(t *testing.T) {
i := InitGlobal()
req, err := http.NewRequest(
http.MethodGet,
"http://localhost:8082/api/sensor_data",
nil,
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec := httptest.NewRecorder()
i.GET_SensorData(rec, req, nil)
if rec.Code != http.StatusOK {
t.Errorf("expected status 200 got %d", rec.Code)
}
if rec.Header()["Content-Type"][0] != "application/json" {
t.Errorf("expected header of application/json but got %v", rec.Header()["Content-Type"])
}
if rec.Body.String() != "{}" {
t.Errorf("expected empty json body but got %v", rec.Body)
}
}
func Test_GET_Sensor_Data(t *testing.T) {
i := InitGlobal()
utc, err := time.LoadLocation("UTC")
if err != nil {
t.Fatalf("Could not create time location: %v", err)
}
dt := time.Date(2016, 12, 20, 10, 28, 50, 0, utc) //year int, month Month, day, hour, min, sec, nsec int, loc *Location
sd1 := SensorData{Location: "IIT Tower Vault 1 SW Corner", Serial_Number: 1,
Temperature: 120.1, Pressure: 760.2, Humidity: 80.2,
Date_Time: dt, Water_Level: 20}
i.sd_table["1"] = sd1
sd2 := SensorData{Location: "IIT Tower Vault 2 NW Corner", Serial_Number: 2,
Temperature: 40.1, Pressure: 760.2, Humidity: 40.2,
Date_Time: dt, Water_Level: 2}
i.sd_table["2"] = sd2
req, err := http.NewRequest(
http.MethodGet,
"http://localhost:8082/api/sensor_data",
nil,
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec := httptest.NewRecorder()
i.GET_SensorData(rec, req, nil)
if rec.Code != http.StatusOK {
t.Errorf("expected status 200 got %d", rec.Code)
}
if rec.Header()["Content-Type"][0] != "application/json" {
t.Errorf("expected header of application/json but got %v", rec.Header()["Content-Type"])
}
expected := "{\"1\":{\"location\":\"IIT Tower Vault 1 SW Corner\",\"serial_number\":1,\"temperature\":120.1,\"pressure\":760.2,\"humidity\":80.2,\"datetime\":\"2016-12-20T10:28:50Z\",\"water_level\":20},\"2\":{\"location\":\"IIT Tower Vault 2 NW Corner\",\"serial_number\":2,\"temperature\":40.1,\"pressure\":760.2,\"humidity\":40.2,\"datetime\":\"2016-12-20T10:28:50Z\",\"water_level\":2}}"
if rec.Body.String() != expected {
t.Errorf("expected empty json body but got %v", rec.Body)
}
req, err = http.NewRequest(
http.MethodGet,
"http://localhost:8082/api/sensor_data",
nil,
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec = httptest.NewRecorder()
i.GET_SensorData(rec, req, nil)
if rec.Code != http.StatusOK {
t.Errorf("expected status 200 got %d", rec.Code)
}
if rec.Header()["Content-Type"][0] != "application/json" {
t.Errorf("expected header of application/json but got %v", rec.Header()["Content-Type"])
}
expected = "{\"1\":{\"location\":\"IIT Tower Vault 1 SW Corner\",\"serial_number\":1,\"temperature\":120.1,\"pressure\":760.2,\"humidity\":80.2,\"datetime\":\"2016-12-20T10:28:50Z\",\"water_level\":20},\"2\":{\"location\":\"IIT Tower Vault 2 NW Corner\",\"serial_number\":2,\"temperature\":40.1,\"pressure\":760.2,\"humidity\":40.2,\"datetime\":\"2016-12-20T10:28:50Z\",\"water_level\":2}}"
if rec.Body.String() != expected {
t.Errorf("expected empty json body but got %v", rec.Body)
}
req, err = http.NewRequest(
http.MethodGet,
"http://localhost:8082/api/sensor_data/999",
nil,
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec = httptest.NewRecorder()
ps := httprouter.Params{httprouter.Param{"sensor_id", "999"}}
i.GET_SensorData_ID(rec, req, ps)
if rec.Code != http.StatusNotFound {
t.Errorf("expected status 404 got %d", rec.Code)
}
req, err = http.NewRequest(
http.MethodGet,
"http://localhost:8082/api/sensor_data/1",
nil,
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec = httptest.NewRecorder()
ps = httprouter.Params{httprouter.Param{"sensor_id", "1"}}
i.GET_SensorData_ID(rec, req, ps)
if rec.Code != http.StatusOK {
t.Errorf("expected status 200 got %d", rec.Code)
}
if rec.Header()["Content-Type"][0] != "application/json" {
t.Errorf("expected header of application/json but got %v", rec.Header()["Content-Type"])
}
expected = "{\"location\":\"IIT Tower Vault 1 SW Corner\",\"serial_number\":1,\"temperature\":120.1,\"pressure\":760.2,\"humidity\":80.2,\"datetime\":\"2016-12-20T10:28:50Z\",\"water_level\":20}"
if rec.Body.String() != expected {
t.Errorf("expected sd1 json body but got %v", rec.Body)
}
}
func Test_POST_Sensor_Data(t *testing.T) {
i := InitGlobal()
payload := []byte(`{
"serial_number": 1,
"location": "IIT Tower Vault 1 SW Corner",
"temperature": 20.1,
"pressure": 760.2,
"humidity": 80.2,
"water_level": 2,
"datetime": "2016-10-21T23:25:40.573559+00:00"
}`)
req, err := http.NewRequest(
http.MethodPost,
"http://localhost:8082/api/sensor_data/0",
bytes.NewReader(payload),
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec := httptest.NewRecorder()
ps := httprouter.Params{httprouter.Param{"sensor_id", "1"}}
i.POST_SensorData_ID(rec, req, ps)
if rec.Code != http.StatusOK {
t.Errorf("expected status 200 got %d", rec.Code)
}
// Make sure the sd_table has the data
if i.sd_table["1"].Serial_Number != 1 {
t.Errorf("expected sensor_number to be 1 but got %d", i.sd_table["1"].Serial_Number)
}
// The json sent is faulty verify if its inserted
if *i.faults["1"][0].DataEntry != i.sd_table["1"] {
t.Errorf("fault dataentry and sd_table should be equal, dataentry: %v sd_table %v", *i.faults["1"][0].DataEntry, i.sd_table["1"])
}
}
func Test_GET_DELETE_Faults_ID(t *testing.T) {
i := InitGlobal()
utc, err := time.LoadLocation("UTC")
if err != nil {
t.Fatalf("Could not create time location: %v", err)
}
dt := time.Date(2016, 12, 20, 10, 28, 50, 0, utc) //year int, month Month, day, hour, min, sec, nsec int, loc *Location
sd1 := SensorData{Location: "IIT Tower Vault 1 SW Corner", Serial_Number: 1,
Temperature: 120.1, Pressure: 760.2, Humidity: 80.2,
Date_Time: dt, Water_Level: 20}
i.sd_table["1"] = sd1
fe := FaultEntry{&sd1, []string{"Temperature exceeded threshold!"}}
i.faults["1"] = append(i.faults["1"], &fe)
req, err := http.NewRequest(
http.MethodGet,
"http://localhost:8082/api/faults/1",
nil,
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec := httptest.NewRecorder()
ps := httprouter.Params{httprouter.Param{"sensor_id", "1"}}
i.GET_Faults_ID(rec, req, ps)
if rec.Code != http.StatusOK {
t.Errorf("expected status 200 got %d", rec.Code)
}
expected := "{\"DataEntry\":{\"location\":\"IIT Tower Vault 1 SW Corner\",\"serial_number\":1,\"temperature\":120.1,\"pressure\":760.2,\"humidity\":80.2,\"datetime\":\"2016-12-20T10:28:50Z\",\"water_level\":20},\"FaultMessages\":[\"Temperature exceeded threshold!\"]}"
if expected != rec.Body.String() {
t.Errorf("expected string not equal to response body, got %s", rec.Body.String())
}
//Delete the faults for sensor id 1
req, err = http.NewRequest(
http.MethodDelete,
"http://localhost:8082/api/faults/1",
nil,
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec = httptest.NewRecorder()
ps = httprouter.Params{httprouter.Param{"sensor_id", "1"}}
i.DELETE_Faults_ID(rec, req, ps)
if rec.Code != http.StatusOK {
t.Errorf("expected status 200 got %d", rec.Code)
}
//Delete the faults for sensor id 999 which doesnt exist
req, err = http.NewRequest(
http.MethodDelete,
"http://localhost:8082/api/faults/999",
nil,
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec = httptest.NewRecorder()
ps = httprouter.Params{httprouter.Param{"sensor_id", "999"}}
i.DELETE_Faults_ID(rec, req, ps)
if rec.Code != http.StatusNotFound {
t.Errorf("expected status 404 got %d", rec.Code)
}
//Try to get faults for sensor id 1 which was just deleted
req, err = http.NewRequest(
http.MethodGet,
"http://localhost:8082/api/faults/1",
nil,
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec = httptest.NewRecorder()
ps = httprouter.Params{httprouter.Param{"sensor_id", "1"}}
i.GET_Faults_ID(rec, req, ps)
if rec.Code != http.StatusNotFound {
t.Errorf("expected status 404 got %d", rec.Code)
}
expected = "Can't find fault data for the id specified"
if expected != rec.Body.String() {
t.Errorf("expected string not equal to response body, got %s", rec.Body.String())
}
}
func Test_GET_Faults(t *testing.T) {
i := InitGlobal()
utc, err := time.LoadLocation("UTC")
if err != nil {
t.Fatalf("Could not create time location: %v", err)
}
dt := time.Date(2016, 12, 20, 10, 28, 50, 0, utc) //year int, month Month, day, hour, min, sec, nsec int, loc *Location
sd1 := SensorData{Location: "IIT Tower Vault 1 SW Corner", Serial_Number: 1,
Temperature: 120.1, Pressure: 760.2, Humidity: 80.2,
Date_Time: dt, Water_Level: 20}
i.sd_table["1"] = sd1
fe := FaultEntry{&sd1, []string{"Temperature exceeded threshold!"}}
i.faults["1"] = append(i.faults["1"], &fe)
sd2 := SensorData{Location: "IIT Tower Vault 2 NW Corner", Serial_Number: 2,
Temperature: 40.1, Pressure: 760.2, Humidity: 40.2,
Date_Time: dt, Water_Level: 2}
i.sd_table["2"] = sd2
fe2 := FaultEntry{&sd2, []string{"Temperature exceeded threshold!"}}
i.faults["2"] = append(i.faults["2"], &fe2)
sd3 := SensorData{Location: "NEW - IIT Tower Vault 1 NW Corner", Serial_Number: 2,
Temperature: 140.1, Pressure: 160.2, Humidity: 140.2,
Date_Time: dt, Water_Level: 12}
i.sd_table["2"] = sd2
fe3 := FaultEntry{&sd3, []string{"Temperature exceeded threshold!"}}
i.faults["2"] = append(i.faults["2"], &fe3)
req, err := http.NewRequest(
http.MethodGet,
"http://localhost:8082/api/faults",
nil,
)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
rec := httptest.NewRecorder()
i.GET_Faults(rec, req, nil)
if rec.Code != http.StatusOK {
t.Errorf("expected status 200 got %d", rec.Code)
}
if rec.Header()["Content-Type"][0] != "application/json" {
t.Errorf("expected header of application/json but got %v", rec.Header()["Content-Type"])
}
expected := "{\"1\":{\"DataEntry\":{\"location\":\"IIT Tower Vault 1 SW Corner\",\"serial_number\":1,\"temperature\":120.1,\"pressure\":760.2,\"humidity\":80.2,\"datetime\":\"2016-12-20T10:28:50Z\",\"water_level\":20},\"FaultMessages\":[\"Temperature exceeded threshold!\"]},\"2\":{\"DataEntry\":{\"location\":\"NEW - IIT Tower Vault 1 NW Corner\",\"serial_number\":2,\"temperature\":140.1,\"pressure\":160.2,\"humidity\":140.2,\"datetime\":\"2016-12-20T10:28:50Z\",\"water_level\":12},\"FaultMessages\":[\"Temperature exceeded threshold!\"]}}"
if rec.Body.String() != expected {
t.Errorf("expected string not equat to json body: %v", rec.Body)
}
}