-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmessages_test.go
92 lines (77 loc) · 1.93 KB
/
messages_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
package ari
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestParseMsg(t *testing.T) {
assert := assert.New(t)
bytes := readFile("ChannelConnectedLine.json")
msg, err := parseMsg(bytes)
assert.Nil(err)
assert.IsType(&ChannelConnectedLine{}, msg)
line := msg.(*ChannelConnectedLine)
assert.Equal("demo", line.Application)
}
func TestParseMsgUnknown(t *testing.T) {
assert := assert.New(t)
bytes := readFile("Unknown.json")
msg, err := parseMsg(bytes)
assert.Nil(err)
assert.IsType(&Event{}, msg)
assert.Equal("Unknown", msg.GetType())
}
func TestChannelConnectedLine(t *testing.T) {
actual := ChannelConnectedLine{}
parseJSON("ChannelConnectedLine.json", &actual)
expected := ChannelConnectedLine{
Event: Event{
Message: Message{
Type: "ChannelConnectedLine",
},
Application: "demo",
Timestamp: parseTime("2017-08-26T15:20:12.596+0200"),
},
Channel: &Channel{
ID: "1503753612.1674",
AccountCode: "12",
CreationTime: parseTime("2017-08-26T15:20:12.595+0200"),
Name: "Local/2103@spaeter-00000072;1",
State: "Down",
Connected: &CallerID{Name: "Bob", Number: "012345678"},
Caller: &CallerID{Name: "Alice", Number: "123456e0"},
Dialplan: &DialplanCEP{Context: "spaeter", Exten: "123456e0", Priority: 1},
},
}
assert.EqualValues(t, expected, actual)
}
func parseTime(str string) *Time {
timestamp, err := time.Parse(timeFormat, str)
if err != nil {
panic(err)
}
ts := Time(timestamp)
return &ts
}
func readFile(filename string) []byte {
bytes, err := ioutil.ReadFile("testdata/" + filename)
if err != nil {
panic(err)
}
return bytes
}
func parseJSON(filename string, v interface{}) {
file, err := os.Open("testdata/" + filename)
if err != nil {
panic(err)
}
defer file.Close()
jsonParser := json.NewDecoder(file)
err = jsonParser.Decode(&v)
if err != nil {
panic(err)
}
}