hybridgroup.gobot/platforms/mqtt/mqtt_adaptor_test.go

131 lines
3.1 KiB
Go
Raw Normal View History

2014-11-04 10:30:56 +08:00
package mqtt
import (
2014-11-05 00:33:05 +08:00
"fmt"
"strings"
2014-11-04 10:56:33 +08:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
2014-11-04 10:30:56 +08:00
)
var _ gobot.Adaptor = (*Adaptor)(nil)
func initTestMqttAdaptor() *Adaptor {
return NewAdaptor("tcp://localhost:1883", "client")
2014-11-04 10:30:56 +08:00
}
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) {
2014-11-04 10:56:33 +08:00
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")
2014-11-04 10:30:56 +08:00
}
func TestMqttAdaptorFinalize(t *testing.T) {
2014-11-04 10:56:33 +08:00
a := initTestMqttAdaptor()
require.NoError(t, a.Finalize())
2014-11-04 10:30:56 +08:00
}
2014-11-05 00:33:05 +08:00
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
data := []byte("o")
assert.False(t, a.Publish("test", data))
2014-11-05 00:33:05 +08:00
}
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
_ = a.Connect()
2014-11-05 00:33:05 +08:00
data := []byte("o")
assert.True(t, a.Publish("test", data))
2014-11-05 00:33:05 +08:00
}
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
assert.False(t, a.On("hola", func(msg Message) {
2014-11-05 00:33:05 +08:00
fmt.Println("hola")
}))
2014-11-05 00:33:05 +08:00
}
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
_ = a.Connect()
assert.True(t, a.On("hola", func(msg Message) {
2014-11-05 00:33:05 +08:00
fmt.Println("hola")
}))
2014-11-05 00:33:05 +08:00
}
func TestMqttAdaptorQoS(t *testing.T) {
a := initTestMqttAdaptor()
a.SetQoS(1)
assert.Equal(t, 1, a.qos)
}