2014-11-03 18:30:56 -08:00
|
|
|
package mqtt
|
|
|
|
|
|
|
|
import (
|
2014-11-04 08:33:05 -08:00
|
|
|
"fmt"
|
2017-04-06 10:55:06 +02:00
|
|
|
"strings"
|
2014-11-03 18:56:33 -08:00
|
|
|
"testing"
|
2014-11-06 12:28:04 -08:00
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2014-11-03 18:30:56 -08:00
|
|
|
)
|
|
|
|
|
2016-09-26 10:13:37 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-07-13 11:01:36 -06:00
|
|
|
|
2016-09-26 10:13:37 +02:00
|
|
|
func initTestMqttAdaptor() *Adaptor {
|
2017-04-19 10:46:28 +02:00
|
|
|
return NewAdaptor("tcp://localhost:1883", "client")
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
2017-04-06 10:55:06 +02:00
|
|
|
func TestMqttAdaptorName(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, strings.HasPrefix(a.Name(), "MQTT"))
|
2017-04-06 10:55:06 +02:00
|
|
|
a.SetName("NewName")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "NewName", a.Name())
|
2017-04-06 10:55:06 +02:00
|
|
|
}
|
|
|
|
|
2017-04-19 10:30:30 +02:00
|
|
|
func TestMqttAdaptorPort(t *testing.T) {
|
2014-11-03 18:56:33 -08:00
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "tcp://localhost:1883", a.Port())
|
2017-04-19 10:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorAutoReconnect(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.False(t, a.AutoReconnect())
|
2017-04-19 10:30:30 +02:00
|
|
|
a.SetAutoReconnect(true)
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, a.AutoReconnect())
|
2017-04-19 10:30:30 +02:00
|
|
|
}
|
|
|
|
|
2017-09-24 13:45:47 +02:00
|
|
|
func TestMqttAdaptorCleanSession(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, a.CleanSession())
|
2017-09-24 13:45:47 +02:00
|
|
|
a.SetCleanSession(false)
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.False(t, a.CleanSession())
|
2017-09-24 13:45:47 +02:00
|
|
|
}
|
|
|
|
|
2017-04-19 10:30:30 +02:00
|
|
|
func TestMqttAdaptorUseSSL(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.False(t, a.UseSSL())
|
2017-04-19 10:30:30 +02:00
|
|
|
a.SetUseSSL(true)
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, a.UseSSL())
|
2017-04-19 10:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorUseServerCert(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "", a.ServerCert())
|
2017-04-19 10:30:30 +02:00
|
|
|
a.SetServerCert("/path/to/server.cert")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "/path/to/server.cert", a.ServerCert())
|
2017-04-19 10:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorUseClientCert(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "", a.ClientCert())
|
2017-04-19 10:30:30 +02:00
|
|
|
a.SetClientCert("/path/to/client.cert")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "/path/to/client.cert", a.ClientCert())
|
2017-04-19 10:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorUseClientKey(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "", a.ClientKey())
|
2017-04-19 10:30:30 +02:00
|
|
|
a.SetClientKey("/path/to/client.key")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "/path/to/client.key", a.ClientKey())
|
2017-04-19 10:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnectError(t *testing.T) {
|
2019-05-21 14:29:06 +02:00
|
|
|
a := NewAdaptor("tcp://localhost:1884", "client")
|
2017-04-19 10:46:28 +02:00
|
|
|
|
2017-05-02 20:34:46 +02:00
|
|
|
err := a.Connect()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Contains(t, err.Error(), "connection refused")
|
2017-04-19 10:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnectSSLError(t *testing.T) {
|
2019-05-21 14:29:06 +02:00
|
|
|
a := NewAdaptor("tcp://localhost:1884", "client")
|
2017-04-19 10:46:28 +02:00
|
|
|
a.SetUseSSL(true)
|
2017-05-02 20:34:46 +02:00
|
|
|
err := a.Connect()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Contains(t, err.Error(), "connection refused")
|
2017-04-19 10:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnectWithAuthError(t *testing.T) {
|
2019-05-21 14:29:06 +02:00
|
|
|
a := NewAdaptorWithAuth("xyz://localhost:1883", "client", "user", "pass")
|
2023-10-25 20:21:18 +02:00
|
|
|
assert.ErrorContains(t, a.Connect(), "network Error : unknown protocol")
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorFinalize(t *testing.T) {
|
2014-11-03 18:56:33 -08:00
|
|
|
a := initTestMqttAdaptor()
|
2023-10-26 20:34:19 +02:00
|
|
|
assert.NoError(t, a.Finalize())
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
2014-11-04 08:33:05 -08:00
|
|
|
|
|
|
|
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
data := []byte("o")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.False(t, a.Publish("test", data))
|
2014-11-04 08:33:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-06-12 19:51:25 +02:00
|
|
|
_ = a.Connect()
|
2014-11-04 08:33:05 -08:00
|
|
|
data := []byte("o")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, a.Publish("test", data))
|
2014-11-04 08:33:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.False(t, a.On("hola", func(msg Message) {
|
2014-11-04 08:33:05 -08:00
|
|
|
fmt.Println("hola")
|
2023-10-20 10:27:09 +02:00
|
|
|
}))
|
2014-11-04 08:33:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-06-12 19:51:25 +02:00
|
|
|
_ = a.Connect()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, a.On("hola", func(msg Message) {
|
2014-11-04 08:33:05 -08:00
|
|
|
fmt.Println("hola")
|
2023-10-20 10:27:09 +02:00
|
|
|
}))
|
2014-11-04 08:33:05 -08:00
|
|
|
}
|
2018-12-17 11:06:34 -06:00
|
|
|
|
|
|
|
func TestMqttAdaptorQoS(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
a.SetQoS(1)
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, a.qos, 1)
|
2018-12-17 11:06:34 -06:00
|
|
|
}
|