forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_adaptor_test.go
127 lines (105 loc) · 3.27 KB
/
mqtt_adaptor_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
package mqtt
import (
"errors"
"fmt"
"strings"
"testing"
multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Adaptor = (*Adaptor)(nil)
func initTestMqttAdaptor() *Adaptor {
return NewAdaptor("tcp://localhost:1883", "client")
}
func TestMqttAdaptorName(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "MQTT"), true)
a.SetName("NewName")
gobottest.Assert(t, a.Name(), "NewName")
}
func TestMqttAdaptorPort(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.Port(), "tcp://localhost:1883")
}
func TestMqttAdaptorAutoReconnect(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.AutoReconnect(), false)
a.SetAutoReconnect(true)
gobottest.Assert(t, a.AutoReconnect(), true)
}
func TestMqttAdaptorCleanSession(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.CleanSession(), true)
a.SetCleanSession(false)
gobottest.Assert(t, a.CleanSession(), false)
}
func TestMqttAdaptorUseSSL(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.UseSSL(), false)
a.SetUseSSL(true)
gobottest.Assert(t, a.UseSSL(), true)
}
func TestMqttAdaptorUseServerCert(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.ServerCert(), "")
a.SetServerCert("/path/to/server.cert")
gobottest.Assert(t, a.ServerCert(), "/path/to/server.cert")
}
func TestMqttAdaptorUseClientCert(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.ClientCert(), "")
a.SetClientCert("/path/to/client.cert")
gobottest.Assert(t, a.ClientCert(), "/path/to/client.cert")
}
func TestMqttAdaptorUseClientKey(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.ClientKey(), "")
a.SetClientKey("/path/to/client.key")
gobottest.Assert(t, a.ClientKey(), "/path/to/client.key")
}
func TestMqttAdaptorConnectError(t *testing.T) {
a := initTestMqttAdaptor()
err := a.Connect()
gobottest.Assert(t, strings.Contains(err.Error(), "connection refused"), true)
}
func TestMqttAdaptorConnectSSLError(t *testing.T) {
a := initTestMqttAdaptor()
a.SetUseSSL(true)
err := a.Connect()
gobottest.Assert(t, strings.Contains(err.Error(), "connection refused"), true)
}
func TestMqttAdaptorConnectWithAuthError(t *testing.T) {
a := NewAdaptorWithAuth("localhost:1883", "client", "user", "pass")
var expected error
expected = multierror.Append(expected, errors.New("Network Error : Unknown protocol"))
gobottest.Assert(t, a.Connect(), expected)
}
func TestMqttAdaptorFinalize(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.Finalize(), nil)
}
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), false)
}
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
a.Connect()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), true)
}
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.On("hola", func(msg Message) {
fmt.Println("hola")
}), false)
}
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
a.Connect()
gobottest.Assert(t, a.On("hola", func(msg Message) {
fmt.Println("hola")
}), true)
}