hybridgroup.gobot/platforms/mqtt/mqtt_adaptor_test.go

128 lines
3.3 KiB
Go
Raw Normal View History

2014-11-04 10:30:56 +08:00
package mqtt
import (
"errors"
2014-11-05 00:33:05 +08:00
"fmt"
"strings"
2014-11-04 10:56:33 +08:00
"testing"
multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
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()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "MQTT"), true)
a.SetName("NewName")
gobottest.Assert(t, a.Name(), "NewName")
}
func TestMqttAdaptorPort(t *testing.T) {
2014-11-04 10:56:33 +08:00
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)
2014-11-04 10:30:56 +08:00
}
func TestMqttAdaptorFinalize(t *testing.T) {
2014-11-04 10:56:33 +08:00
a := initTestMqttAdaptor()
gobottest.Assert(t, a.Finalize(), nil)
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")
gobottest.Assert(t, a.Publish("test", data), false)
2014-11-05 00:33:05 +08:00
}
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
a.Connect()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), true)
2014-11-05 00:33:05 +08:00
}
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.On("hola", func(msg Message) {
2014-11-05 00:33:05 +08:00
fmt.Println("hola")
}), false)
}
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
a.Connect()
gobottest.Assert(t, a.On("hola", func(msg Message) {
2014-11-05 00:33:05 +08:00
fmt.Println("hola")
}), true)
}