-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathmqtt_adaptor_test.go
130 lines (107 loc) · 3.14 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
128
129
130
package mqtt
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
)
var _ gobot.Adaptor = (*Adaptor)(nil)
func initTestMqttAdaptor() *Adaptor {
return NewAdaptor("tcp://localhost:1883", "client")
}
func TestMqttAdaptorName(t *testing.T) {
a := initTestMqttAdaptor()
assert.True(t, strings.HasPrefix(a.Name(), "MQTT"))
a.SetName("NewName")
assert.Equal(t, "NewName", a.Name())
}
func TestMqttAdaptorPort(t *testing.T) {
a := initTestMqttAdaptor()
assert.Equal(t, "tcp://localhost:1883", a.Port())
}
func TestMqttAdaptorAutoReconnect(t *testing.T) {
a := initTestMqttAdaptor()
assert.False(t, a.AutoReconnect())
a.SetAutoReconnect(true)
assert.True(t, a.AutoReconnect())
}
func TestMqttAdaptorCleanSession(t *testing.T) {
a := initTestMqttAdaptor()
assert.True(t, a.CleanSession())
a.SetCleanSession(false)
assert.False(t, a.CleanSession())
}
func TestMqttAdaptorUseSSL(t *testing.T) {
a := initTestMqttAdaptor()
assert.False(t, a.UseSSL())
a.SetUseSSL(true)
assert.True(t, a.UseSSL())
}
func TestMqttAdaptorUseServerCert(t *testing.T) {
a := initTestMqttAdaptor()
assert.Equal(t, "", a.ServerCert())
a.SetServerCert("/path/to/server.cert")
assert.Equal(t, "/path/to/server.cert", a.ServerCert())
}
func TestMqttAdaptorUseClientCert(t *testing.T) {
a := initTestMqttAdaptor()
assert.Equal(t, "", a.ClientCert())
a.SetClientCert("/path/to/client.cert")
assert.Equal(t, "/path/to/client.cert", a.ClientCert())
}
func TestMqttAdaptorUseClientKey(t *testing.T) {
a := initTestMqttAdaptor()
assert.Equal(t, "", a.ClientKey())
a.SetClientKey("/path/to/client.key")
assert.Equal(t, "/path/to/client.key", a.ClientKey())
}
func TestMqttAdaptorConnectError(t *testing.T) {
a := NewAdaptor("tcp://localhost:1884", "client")
err := a.Connect()
require.ErrorContains(t, err, "connection refused")
}
func TestMqttAdaptorConnectSSLError(t *testing.T) {
a := NewAdaptor("tcp://localhost:1884", "client")
a.SetUseSSL(true)
err := a.Connect()
require.ErrorContains(t, err, "connection refused")
}
func TestMqttAdaptorConnectWithAuthError(t *testing.T) {
a := NewAdaptorWithAuth("xyz://localhost:1883", "client", "user", "pass")
require.ErrorContains(t, a.Connect(), "network Error : unknown protocol")
}
func TestMqttAdaptorFinalize(t *testing.T) {
a := initTestMqttAdaptor()
require.NoError(t, a.Finalize())
}
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
data := []byte("o")
assert.False(t, a.Publish("test", data))
}
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
_ = a.Connect()
data := []byte("o")
assert.True(t, a.Publish("test", data))
}
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
assert.False(t, a.On("hola", func(msg Message) {
fmt.Println("hola")
}))
}
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
_ = a.Connect()
assert.True(t, a.On("hola", func(msg Message) {
fmt.Println("hola")
}))
}
func TestMqttAdaptorQoS(t *testing.T) {
a := initTestMqttAdaptor()
a.SetQoS(1)
assert.Equal(t, 1, a.qos)
}