mqtt_source_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "bytes"
  17. "compress/zlib"
  18. "reflect"
  19. "testing"
  20. "github.com/lf-edge/ekuiper/internal/compressor"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "github.com/lf-edge/ekuiper/internal/converter"
  23. "github.com/lf-edge/ekuiper/internal/topo/context"
  24. "github.com/lf-edge/ekuiper/pkg/api"
  25. "github.com/lf-edge/ekuiper/pkg/ast"
  26. )
  27. // TestGetTupleWithZlibCompressor is a unit test for getTuple with zlib compressor
  28. func TestGetTupleWithZlibCompressor(t *testing.T) {
  29. contextLogger := conf.Log.WithField("rule", "TestTupleZlib_Apply")
  30. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  31. cv, _ := converter.GetOrCreateConverter(&ast.Options{FORMAT: "json"})
  32. ctx = context.WithValue(ctx, context.DecodeKey, cv)
  33. dc, _ := compressor.GetDecompressor("zlib")
  34. ms := &MQTTSource{
  35. decompressor: dc,
  36. }
  37. // Prepare a compressed payload
  38. originalPayload := []byte(`{"key": "value"}`)
  39. var buf bytes.Buffer
  40. zlibWriter := zlib.NewWriter(&buf)
  41. _, _ = zlibWriter.Write(originalPayload)
  42. _ = zlibWriter.Close()
  43. compressedPayload := buf.Bytes()
  44. // Create a mock MQTT message with the compressed payload
  45. msg := MockMessage{
  46. payload: compressedPayload,
  47. topic: "test/topic",
  48. }
  49. // Call getTuple with the mock MQTT message
  50. results := getTuples(ctx, ms, msg)
  51. for _, result := range results {
  52. // Check if the result is a valid SourceTuple and has the correct content
  53. if st, ok := result.(api.SourceTuple); ok {
  54. if !reflect.DeepEqual(st.Message(), map[string]interface{}{"key": "value"}) {
  55. t.Errorf("Expected message to be %v, but got %v", map[string]interface{}{"key": "value"}, st.Message())
  56. }
  57. if !reflect.DeepEqual(st.Meta(), map[string]interface{}{"topic": "test/topic", "messageid": "1"}) {
  58. t.Errorf("Expected metadata to be %v, but got %v", map[string]interface{}{"topic": "test/topic", "messageid": "1"}, st.Meta())
  59. }
  60. } else {
  61. t.Errorf("Expected result to be a SourceTuple, but got %T", result)
  62. }
  63. }
  64. }
  65. type MockMessage struct {
  66. payload []byte
  67. topic string
  68. }
  69. func (mm MockMessage) Payload() []byte {
  70. return mm.payload
  71. }
  72. func (MockMessage) Duplicate() bool {
  73. panic("function not expected to be invoked")
  74. }
  75. func (MockMessage) Qos() byte {
  76. panic("function not expected to be invoked")
  77. }
  78. func (MockMessage) Retained() bool {
  79. panic("function not expected to be invoked")
  80. }
  81. func (mm MockMessage) Topic() string {
  82. return mm.topic
  83. }
  84. func (MockMessage) MessageID() uint16 {
  85. return 1
  86. }
  87. func (MockMessage) Ack() {
  88. panic("function not expected to be invoked")
  89. }