forked from CyCoreSystems/ari-proxy
/
config.go
156 lines (123 loc) · 3.88 KB
/
config.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
package integration
import (
"errors"
"testing"
"github.com/CyCoreSystems/ari"
"github.com/CyCoreSystems/ari/client/arimocks"
tmock "github.com/stretchr/testify/mock"
)
var _ = tmock.Anything
func TestConfigData(t *testing.T, s Server) {
var key = ari.NewKey("config", ari.ConfigID("c1", "o1", "id1"))
runTest("ok", t, s, func(t *testing.T, m *mock, cl ari.Client) {
var expected ari.ConfigData
expected.Class = "c1"
expected.Key = key
expected.Type = "o1"
expected.Fields = []ari.ConfigTuple{
ari.ConfigTuple{
Value: "v1",
Attribute: "a1",
},
}
cfg := arimocks.Config{}
m.Asterisk.On("Config").Return(&cfg)
cfg.On("Data", key).Return(&expected, nil)
data, err := cl.Asterisk().Config().Data(key)
if err != nil {
t.Errorf("Unexpected error in remove config data call: %s", err)
}
if data == nil {
t.Errorf("Expected non-nil data")
} else {
failed := false
failed = failed || data.Class != expected.Class
failed = failed || data.ID() != expected.ID()
failed = failed || data.Type != expected.Type
failed = failed || len(data.Fields) != len(expected.Fields)
for idx := range data.Fields {
failed = failed || data.Fields[idx].Attribute != expected.Fields[idx].Attribute
failed = failed || data.Fields[idx].Value != expected.Fields[idx].Value
}
if failed {
t.Errorf("Expected config data '%v', got '%v'", expected, data)
}
}
m.Asterisk.AssertCalled(t, "Config")
cfg.AssertCalled(t, "Data", key)
})
runTest("err", t, s, func(t *testing.T, m *mock, cl ari.Client) {
cfg := arimocks.Config{}
m.Asterisk.On("Config").Return(&cfg)
cfg.On("Data", key).Return(nil, errors.New("error"))
data, err := cl.Asterisk().Config().Data(key)
if err == nil {
t.Errorf("Expected error in remove config data call")
}
if data != nil {
t.Errorf("Expected nil data")
}
m.Asterisk.AssertCalled(t, "Config")
cfg.AssertCalled(t, "Data", key)
})
}
func TestConfigDelete(t *testing.T, s Server) {
var key = ari.NewKey("config", ari.ConfigID("c1", "o1", "id1"))
runTest("ok", t, s, func(t *testing.T, m *mock, cl ari.Client) {
cfg := arimocks.Config{}
m.Asterisk.On("Config").Return(&cfg)
cfg.On("Delete", key).Return(nil)
err := cl.Asterisk().Config().Delete(key)
if err != nil {
t.Errorf("Unexpected error in remove config delete call: %s", err)
}
m.Asterisk.AssertCalled(t, "Config")
cfg.AssertCalled(t, "Delete", key)
})
runTest("err", t, s, func(t *testing.T, m *mock, cl ari.Client) {
cfg := arimocks.Config{}
m.Asterisk.On("Config").Return(&cfg)
cfg.On("Delete", key).Return(errors.New("error"))
err := cl.Asterisk().Config().Delete(key)
if err == nil {
t.Errorf("Expected error in remove config delete call")
}
m.Asterisk.AssertCalled(t, "Config")
cfg.AssertCalled(t, "Delete", key)
})
}
func TestConfigUpdate(t *testing.T, s Server) {
var key = ari.NewKey("config", ari.ConfigID("c1", "o1", "id1"))
tuples := []ari.ConfigTuple{
ari.ConfigTuple{
Value: "v1",
Attribute: "a1",
},
ari.ConfigTuple{
Value: "v2",
Attribute: "a2",
},
}
runTest("ok", t, s, func(t *testing.T, m *mock, cl ari.Client) {
cfg := arimocks.Config{}
m.Asterisk.On("Config").Return(&cfg)
cfg.On("Update", key, tuples).Return(nil)
err := cl.Asterisk().Config().Update(key, tuples)
if err != nil {
t.Errorf("Unexpected error in remove config Update call: %s", err)
}
m.Asterisk.AssertCalled(t, "Config")
cfg.AssertCalled(t, "Update", key, tuples)
})
runTest("err", t, s, func(t *testing.T, m *mock, cl ari.Client) {
cfg := arimocks.Config{}
m.Asterisk.On("Config").Return(&cfg)
cfg.On("Update", key, tuples).Return(errors.New("error"))
err := cl.Asterisk().Config().Update(key, tuples)
if err == nil {
t.Errorf("Expected error in remove config Update call")
}
m.Asterisk.AssertCalled(t, "Config")
cfg.AssertCalled(t, "Update", key, tuples)
})
}