forked from CyCoreSystems/ari-proxy
/
device.go
130 lines (98 loc) · 3.47 KB
/
device.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
package integration
import (
"errors"
"testing"
"github.com/CyCoreSystems/ari"
)
func TestDeviceData(t *testing.T, s Server) {
key := ari.NewKey(ari.DeviceStateKey, "d1")
runTest("ok", t, s, func(t *testing.T, m *mock, cl ari.Client) {
var expected ari.DeviceStateData
expected.State = "deviceData1"
expected.Key = ari.NewKey(ari.DeviceStateKey, "d1")
m.DeviceState.On("Data", key).Return(&expected, nil)
data, err := cl.DeviceState().Data(key)
if err != nil {
t.Errorf("Error in remote device state data call: %s", err)
}
if data == nil || data.State != expected.State {
t.Errorf("Expected data '%s', got '%v'", expected, data)
}
m.DeviceState.AssertCalled(t, "Data", key)
})
runTest("err", t, s, func(t *testing.T, m *mock, cl ari.Client) {
m.DeviceState.On("Data", key).Return(nil, errors.New("err"))
data, err := cl.DeviceState().Data(key)
if err == nil {
t.Errorf("Expected error in remote device state data call: %s", err)
}
if data != nil {
t.Errorf("Expected data to be nil, got '%v'", *data)
}
m.DeviceState.AssertCalled(t, "Data", key)
})
}
func TestDeviceDelete(t *testing.T, s Server) {
key := ari.NewKey(ari.DeviceStateKey, "d1")
runTest("ok", t, s, func(t *testing.T, m *mock, cl ari.Client) {
m.DeviceState.On("Delete", key).Return(nil)
err := cl.DeviceState().Delete(key)
if err != nil {
t.Errorf("Error in remote device state Delete call: %s", err)
}
m.DeviceState.AssertCalled(t, "Delete", key)
})
runTest("err", t, s, func(t *testing.T, m *mock, cl ari.Client) {
m.DeviceState.On("Delete", key).Return(errors.New("err"))
err := cl.DeviceState().Delete(key)
if err == nil {
t.Errorf("Expected error in remote device state Delete call: %s", err)
}
m.DeviceState.AssertCalled(t, "Delete", key)
})
}
func TestDeviceUpdate(t *testing.T, s Server) {
key := ari.NewKey(ari.DeviceStateKey, "d1")
runTest("ok", t, s, func(t *testing.T, m *mock, cl ari.Client) {
m.DeviceState.On("Update", key, "st1").Return(nil)
err := cl.DeviceState().Update(key, "st1")
if err != nil {
t.Errorf("Error in remote device state Update call: %s", err)
}
m.DeviceState.AssertCalled(t, "Update", key, "st1")
})
runTest("err", t, s, func(t *testing.T, m *mock, cl ari.Client) {
m.DeviceState.On("Update", key, "st1").Return(errors.New("err"))
err := cl.DeviceState().Update(key, "st1")
if err == nil {
t.Errorf("Expected error in remote device state Update call: %s", err)
}
m.DeviceState.AssertCalled(t, "Update", key, "st1")
})
}
func TestDeviceList(t *testing.T, s Server) {
runTest("ok", t, s, func(t *testing.T, m *mock, cl ari.Client) {
h1 := ari.NewKey(ari.DeviceStateKey, "h1")
h2 := ari.NewKey(ari.DeviceStateKey, "h2")
m.DeviceState.On("List", (*ari.Key)(nil)).Return([]*ari.Key{h1, h2}, nil)
list, err := cl.DeviceState().List(nil)
if err != nil {
t.Errorf("Error in remote device state List call: %s", err)
}
if len(list) != 2 {
t.Errorf("Expected list of length 2, got %d", len(list))
}
m.DeviceState.AssertCalled(t, "List", (*ari.Key)(nil))
})
runTest("err", t, s, func(t *testing.T, m *mock, cl ari.Client) {
m.DeviceState.On("List", (*ari.Key)(nil)).Return([]*ari.Key{}, errors.New("error"))
list, err := cl.DeviceState().List(nil)
if err == nil {
t.Errorf("Expected error in remote device state List call")
}
if len(list) != 0 {
t.Errorf("Expected list of length 0, got %d", len(list))
}
m.DeviceState.AssertCalled(t, "List", (*ari.Key)(nil))
})
}