gobot.io/x/gobot/v2@v2.1.0/platforms/mqtt/mqtt_adaptor_test.go (about)

     1  package mqtt
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  
     9  	multierror "github.com/hashicorp/go-multierror"
    10  	"gobot.io/x/gobot/v2"
    11  	"gobot.io/x/gobot/v2/gobottest"
    12  )
    13  
    14  var _ gobot.Adaptor = (*Adaptor)(nil)
    15  
    16  func initTestMqttAdaptor() *Adaptor {
    17  	return NewAdaptor("tcp://localhost:1883", "client")
    18  }
    19  
    20  func TestMqttAdaptorName(t *testing.T) {
    21  	a := initTestMqttAdaptor()
    22  	gobottest.Assert(t, strings.HasPrefix(a.Name(), "MQTT"), true)
    23  	a.SetName("NewName")
    24  	gobottest.Assert(t, a.Name(), "NewName")
    25  }
    26  
    27  func TestMqttAdaptorPort(t *testing.T) {
    28  	a := initTestMqttAdaptor()
    29  	gobottest.Assert(t, a.Port(), "tcp://localhost:1883")
    30  }
    31  
    32  func TestMqttAdaptorAutoReconnect(t *testing.T) {
    33  	a := initTestMqttAdaptor()
    34  	gobottest.Assert(t, a.AutoReconnect(), false)
    35  	a.SetAutoReconnect(true)
    36  	gobottest.Assert(t, a.AutoReconnect(), true)
    37  }
    38  
    39  func TestMqttAdaptorCleanSession(t *testing.T) {
    40  	a := initTestMqttAdaptor()
    41  	gobottest.Assert(t, a.CleanSession(), true)
    42  	a.SetCleanSession(false)
    43  	gobottest.Assert(t, a.CleanSession(), false)
    44  }
    45  
    46  func TestMqttAdaptorUseSSL(t *testing.T) {
    47  	a := initTestMqttAdaptor()
    48  	gobottest.Assert(t, a.UseSSL(), false)
    49  	a.SetUseSSL(true)
    50  	gobottest.Assert(t, a.UseSSL(), true)
    51  }
    52  
    53  func TestMqttAdaptorUseServerCert(t *testing.T) {
    54  	a := initTestMqttAdaptor()
    55  	gobottest.Assert(t, a.ServerCert(), "")
    56  	a.SetServerCert("/path/to/server.cert")
    57  	gobottest.Assert(t, a.ServerCert(), "/path/to/server.cert")
    58  }
    59  
    60  func TestMqttAdaptorUseClientCert(t *testing.T) {
    61  	a := initTestMqttAdaptor()
    62  	gobottest.Assert(t, a.ClientCert(), "")
    63  	a.SetClientCert("/path/to/client.cert")
    64  	gobottest.Assert(t, a.ClientCert(), "/path/to/client.cert")
    65  }
    66  
    67  func TestMqttAdaptorUseClientKey(t *testing.T) {
    68  	a := initTestMqttAdaptor()
    69  	gobottest.Assert(t, a.ClientKey(), "")
    70  	a.SetClientKey("/path/to/client.key")
    71  	gobottest.Assert(t, a.ClientKey(), "/path/to/client.key")
    72  }
    73  
    74  func TestMqttAdaptorConnectError(t *testing.T) {
    75  	a := NewAdaptor("tcp://localhost:1884", "client")
    76  
    77  	err := a.Connect()
    78  	gobottest.Assert(t, strings.Contains(err.Error(), "connection refused"), true)
    79  }
    80  
    81  func TestMqttAdaptorConnectSSLError(t *testing.T) {
    82  	a := NewAdaptor("tcp://localhost:1884", "client")
    83  	a.SetUseSSL(true)
    84  	err := a.Connect()
    85  	gobottest.Assert(t, strings.Contains(err.Error(), "connection refused"), true)
    86  }
    87  
    88  func TestMqttAdaptorConnectWithAuthError(t *testing.T) {
    89  	a := NewAdaptorWithAuth("xyz://localhost:1883", "client", "user", "pass")
    90  	var expected error
    91  	expected = multierror.Append(expected, errors.New("network Error : unknown protocol"))
    92  
    93  	gobottest.Assert(t, a.Connect(), expected)
    94  }
    95  
    96  func TestMqttAdaptorFinalize(t *testing.T) {
    97  	a := initTestMqttAdaptor()
    98  	gobottest.Assert(t, a.Finalize(), nil)
    99  }
   100  
   101  func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
   102  	a := initTestMqttAdaptor()
   103  	data := []byte("o")
   104  	gobottest.Assert(t, a.Publish("test", data), false)
   105  }
   106  
   107  func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
   108  	a := initTestMqttAdaptor()
   109  	a.Connect()
   110  	data := []byte("o")
   111  	gobottest.Assert(t, a.Publish("test", data), true)
   112  }
   113  
   114  func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
   115  	a := initTestMqttAdaptor()
   116  	gobottest.Assert(t, a.On("hola", func(msg Message) {
   117  		fmt.Println("hola")
   118  	}), false)
   119  }
   120  
   121  func TestMqttAdaptorOnWhenConnected(t *testing.T) {
   122  	a := initTestMqttAdaptor()
   123  	a.Connect()
   124  	gobottest.Assert(t, a.On("hola", func(msg Message) {
   125  		fmt.Println("hola")
   126  	}), true)
   127  }
   128  
   129  func TestMqttAdaptorQoS(t *testing.T) {
   130  	a := initTestMqttAdaptor()
   131  	a.SetQoS(1)
   132  	gobottest.Assert(t, 1, a.qos)
   133  }