mqtt_sink_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mqtt
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestSinkConfigure(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. input map[string]interface{}
  24. expectedErr error
  25. expectedAdConf *AdConf
  26. }{
  27. {
  28. name: "Missing topic",
  29. input: map[string]interface{}{
  30. "qos": 1,
  31. "retained": false,
  32. "compression": "zlib",
  33. },
  34. expectedErr: fmt.Errorf("mqtt sink is missing property topic"),
  35. },
  36. {
  37. name: "Invalid QoS",
  38. input: map[string]interface{}{
  39. "topic": "testTopic",
  40. "qos": 3,
  41. "retained": false,
  42. "compression": "gzip",
  43. },
  44. expectedErr: fmt.Errorf("invalid qos value %v, the value could be only int 0 or 1 or 2", 3),
  45. },
  46. {
  47. name: "Valid configuration with QoS 0 and no compression",
  48. input: map[string]interface{}{
  49. "topic": "testTopic3",
  50. "qos": 0,
  51. "retained": false,
  52. "compression": "",
  53. },
  54. expectedErr: nil,
  55. expectedAdConf: &AdConf{
  56. Tpc: "testTopic3",
  57. Qos: 0,
  58. Retained: false,
  59. Compression: "",
  60. ResendTopic: "testTopic3",
  61. },
  62. },
  63. {
  64. name: "Valid configuration with QoS 1 and no retained",
  65. input: map[string]interface{}{
  66. "topic": "testTopic4",
  67. "qos": 1,
  68. "retained": false,
  69. "compression": "zlib",
  70. },
  71. expectedErr: nil,
  72. expectedAdConf: &AdConf{
  73. Tpc: "testTopic4",
  74. Qos: 1,
  75. Retained: false,
  76. Compression: "zlib",
  77. ResendTopic: "testTopic4",
  78. },
  79. },
  80. }
  81. for _, tt := range tests {
  82. t.Run(tt.name, func(t *testing.T) {
  83. ms := &MQTTSink{}
  84. err := ms.Configure(tt.input)
  85. if !reflect.DeepEqual(err, tt.expectedErr) {
  86. t.Errorf("\n Expected error: \t%v\n \t\t\tgot: \t%v", tt.expectedErr, err)
  87. return
  88. }
  89. if !reflect.DeepEqual(ms.adconf, tt.expectedAdConf) {
  90. t.Errorf("\n Expected adConf: \t%v\n \t\t\tgot: \t%v", tt.expectedAdConf, ms.adconf)
  91. return
  92. }
  93. })
  94. }
  95. }