-
Notifications
You must be signed in to change notification settings - Fork 25
/
alarms_test.go
140 lines (129 loc) · 2.88 KB
/
alarms_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
package unifi
import (
"bytes"
"errors"
"fmt"
"net"
"net/http"
"reflect"
"strings"
"testing"
"time"
)
func TestClientAlarms(t *testing.T) {
const (
wantSite = "default"
wantID = "abcdef123457890"
wantAPName = "ap001"
wantMessage = "ap001 was disconnected"
)
var (
wantAPMAC = net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad}
wantDateTime = time.Date(2016, time.January, 01, 0, 0, 0, 0, time.UTC)
)
wantAlarm := &Alarm{
ID: wantID,
APMAC: wantAPMAC,
APName: wantAPName,
DateTime: wantDateTime,
Message: wantMessage,
}
v := struct {
Alarms []alarm `json:"data"`
}{
Alarms: []alarm{{
ID: wantID,
AP: wantAPMAC.String(),
APName: wantAPName,
DateTime: wantDateTime.Format(time.RFC3339),
Msg: wantMessage,
}},
}
c, done := testClient(t, testHandler(
t,
http.MethodGet,
fmt.Sprintf("/api/s/%s/list/alarm", wantSite),
nil,
v,
))
defer done()
alarms, err := c.Alarms(wantSite)
if err != nil {
t.Fatalf("unexpected error from Client.Alarms: %v", err)
}
if want, got := 1, len(alarms); want != got {
t.Fatalf("unexpected number of Alarms:\n- want: %d\n- got: %d",
want, got)
}
if want, got := wantAlarm, alarms[0]; !reflect.DeepEqual(want, got) {
t.Fatalf("unexpected Alarm:\n- want: %#v\n- got: %#v",
want, got)
}
}
func TestAlarmUnmarshalJSON(t *testing.T) {
var tests = []struct {
desc string
b []byte
a *Alarm
err error
}{
{
desc: "invalid JSON",
b: []byte(`<>`),
err: errors.New("invalid character"),
},
{
desc: "invalid APMAC",
b: []byte(`{"ap":"foo"}`),
err: errors.New("invalid MAC address"),
},
{
desc: "invalid DateTime",
b: []byte(`{"ap":"de:ad:be:ef:de:ad","datetime":"foo"}`),
err: errors.New("parsing time"),
},
{
desc: "OK",
b: bytes.TrimSpace([]byte(`
{
"_id": "abcdef1234567890",
"ap": "de:ad:be:ef:de:ad",
"ap_name": "ap001",
"archived": false,
"datetime": "2016-01-01T00:00:00Z"
"key": "EVT AP Lost Contact",
"msg": "ap001 was disconnected"
"site_id": "default",
"subsystem": "wlan"
}
`)),
a: &Alarm{
ID: "abcdef1234567890",
APMAC: net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad},
APName: "ap001",
DateTime: time.Date(2016, time.January, 01, 0, 0, 0, 0, time.UTC),
Key: "EVT AP Lost Contact",
Message: "ap001 was disconnected",
SiteID: "default",
Subsystem: "wlan",
},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
a := new(Alarm)
err := a.UnmarshalJSON(tt.b)
if want, got := errStr(tt.err), errStr(err); !strings.Contains(got, want) {
t.Fatalf("unexpected error:\n- want: %v\n- got: %v",
want, got)
}
if err != nil {
return
}
if want, got := tt.a, a; !reflect.DeepEqual(got, want) {
t.Fatalf("unexpected Alarm:\n- want: %+v\n- got: %+v",
want, got)
}
})
}
}