mqtt_sink_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. },
  61. },
  62. {
  63. name: "Valid configuration with QoS 1 and no retained",
  64. input: map[string]interface{}{
  65. "topic": "testTopic4",
  66. "qos": 1,
  67. "retained": false,
  68. "compression": "zlib",
  69. },
  70. expectedErr: nil,
  71. expectedAdConf: &AdConf{
  72. Tpc: "testTopic4",
  73. Qos: 1,
  74. Retained: false,
  75. Compression: "zlib",
  76. },
  77. },
  78. }
  79. for _, tt := range tests {
  80. t.Run(tt.name, func(t *testing.T) {
  81. ms := &MQTTSink{}
  82. err := ms.Configure(tt.input)
  83. if !reflect.DeepEqual(err, tt.expectedErr) {
  84. t.Errorf("\n Expected error: \t%v\n \t\t\tgot: \t%v", tt.expectedErr, err)
  85. return
  86. }
  87. if !reflect.DeepEqual(ms.adconf, tt.expectedAdConf) {
  88. t.Errorf("\n Expected adConf: \t%v\n \t\t\tgot: \t%v", tt.expectedAdConf, ms.adconf)
  89. return
  90. }
  91. })
  92. }
  93. }