forked from Marquis42/ari-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
asterisk.go
141 lines (103 loc) · 3.6 KB
/
asterisk.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
package integration
import (
"testing"
"github.com/CyCoreSystems/ari"
"github.com/CyCoreSystems/ari/client/arimocks"
"github.com/pkg/errors"
)
func TestAsteriskInfo(t *testing.T, s Server) {
runTest("noFilter", t, s, func(t *testing.T, m *mock, cl ari.Client) {
var ai ari.AsteriskInfo
ai.SystemInfo.EntityID = "1"
m.Asterisk.On("Info", ari.NodeKey("asdf", "1")).Return(&ai, nil)
ret, err := cl.Asterisk().Info(ari.NodeKey("asdf", "1"))
if err != nil {
t.Errorf("Unexpected error in remote Info call: %v", err)
}
if ret == nil || ret.SystemInfo.EntityID != ai.SystemInfo.EntityID {
t.Errorf("Expected asterisk info %v, got %v", ai, ret)
}
m.Shutdown()
m.Asterisk.AssertCalled(t, "Info", ari.NodeKey("asdf", "1"))
})
runTest("noFilterError", t, s, func(t *testing.T, m *mock, cl ari.Client) {
expected := errors.New("unknown error")
m.Asterisk.On("Info", ari.NodeKey("asdf", "1")).Return(nil, expected)
ret, err := cl.Asterisk().Info(ari.NodeKey("asdf", "1"))
if err == nil || errors.Cause(err).Error() != expected.Error() {
t.Errorf("Expected error '%v', got '%v'", expected, err)
}
if ret != nil {
t.Errorf("Expected nil ret, got '%v'", ret)
}
m.Shutdown()
m.Asterisk.AssertCalled(t, "Info", ari.NodeKey("asdf", "1"))
})
}
func TestAsteriskVariablesGet(t *testing.T, s Server) {
key := ari.NewKey(ari.VariableKey, "s")
runTest("simple", t, s, func(t *testing.T, m *mock, cl ari.Client) {
var ai ari.AsteriskInfo
ai.SystemInfo.EntityID = "1"
mv := arimocks.AsteriskVariables{}
mv.On("Get", key).Return("hello", nil)
m.Asterisk.On("Variables").Return(&mv)
ret, err := cl.Asterisk().Variables().Get(key)
if err != nil {
t.Errorf("Unexpected error in remote Variables Get call: %v", err)
}
if ret != "hello" {
t.Errorf("Expected Variables Get return %v, got %v", "hello", ret)
}
m.Shutdown()
m.Asterisk.AssertCalled(t, "Variables")
mv.AssertCalled(t, "Get", key)
})
runTest("error", t, s, func(t *testing.T, m *mock, cl ari.Client) {
var expected = errors.New("unknown error")
mv := arimocks.AsteriskVariables{}
mv.On("Get", key).Return("", expected)
m.Asterisk.On("Variables").Return(&mv)
ret, err := cl.Asterisk().Variables().Get(key)
if err == nil || errors.Cause(err).Error() != expected.Error() {
t.Errorf("Expected error '%v', got '%v'", expected, err)
}
if ret != "" {
t.Errorf("Expected Variables Get return %v, got %v", "", ret)
}
m.Shutdown()
m.Asterisk.AssertCalled(t, "Variables")
mv.AssertCalled(t, "Get", key)
})
}
func TestAsteriskVariablesSet(t *testing.T, s Server) {
key := ari.NewKey(ari.VariableKey, "s")
runTest("simple", t, s, func(t *testing.T, m *mock, cl ari.Client) {
var ai ari.AsteriskInfo
ai.SystemInfo.EntityID = "1"
mv := arimocks.AsteriskVariables{}
m.Asterisk.On("Variables").Return(&mv)
mv.On("Set", key, "hello").Return(nil)
err := cl.Asterisk().Variables().Set(key, "hello")
if err != nil {
t.Errorf("Unexpected error in remote Variables Set call: %v", err)
}
m.Shutdown()
m.Asterisk.AssertCalled(t, "Variables")
mv.AssertCalled(t, "Set", key, "hello")
})
runTest("err", t, s, func(t *testing.T, m *mock, cl ari.Client) {
var ai ari.AsteriskInfo
ai.SystemInfo.EntityID = "1"
mv := arimocks.AsteriskVariables{}
m.Asterisk.On("Variables").Return(&mv)
mv.On("Set", key, "hello").Return(errors.New("error"))
err := cl.Asterisk().Variables().Set(key, "hello")
if err == nil {
t.Errorf("Expected error in remote Variables Set call: %v", err)
}
m.Shutdown()
m.Asterisk.AssertCalled(t, "Variables")
mv.AssertCalled(t, "Set", key, "hello")
})
}