sink_node_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2022 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 node
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/topo/context"
  19. "github.com/lf-edge/ekuiper/internal/topo/topotest/mocknode"
  20. "github.com/lf-edge/ekuiper/internal/xsql"
  21. "reflect"
  22. "testing"
  23. "time"
  24. )
  25. func TestSinkTemplate_Apply(t *testing.T) {
  26. conf.InitConf()
  27. var tests = []struct {
  28. config map[string]interface{}
  29. data []map[string]interface{}
  30. result [][]byte
  31. }{
  32. {
  33. config: map[string]interface{}{
  34. "sendSingle": true,
  35. "dataTemplate": `{"wrapper":"w1","content":{{toJson .}},"ab":"{{.ab}}"}`,
  36. },
  37. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  38. result: [][]byte{[]byte(`{"wrapper":"w1","content":{"ab":"hello1"},"ab":"hello1"}`), []byte(`{"wrapper":"w1","content":{"ab":"hello2"},"ab":"hello2"}`)},
  39. }, {
  40. config: map[string]interface{}{
  41. "dataTemplate": `{"wrapper":"arr","content":{{json .}},"content0":{{json (index . 0)}},ab0":"{{index . 0 "ab"}}"}`,
  42. },
  43. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  44. result: [][]byte{[]byte(`{"wrapper":"arr","content":[{"ab":"hello1"},{"ab":"hello2"}],"content0":{"ab":"hello1"},ab0":"hello1"}`)},
  45. }, {
  46. config: map[string]interface{}{
  47. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.ab}}</li>{{end}}</ul>`,
  48. },
  49. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  50. result: [][]byte{[]byte(`<div>results</div><ul><li>hello1</li><li>hello2</li></ul>`)},
  51. }, {
  52. config: map[string]interface{}{
  53. "dataTemplate": `{"content":{{toJson .}}}`,
  54. },
  55. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  56. result: [][]byte{[]byte(`{"content":[{"ab":"hello1"},{"ab":"hello2"}]}`)},
  57. }, {
  58. config: map[string]interface{}{
  59. "sendSingle": true,
  60. "dataTemplate": `{"newab":"{{.ab}}"}`,
  61. },
  62. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  63. result: [][]byte{[]byte(`{"newab":"hello1"}`), []byte(`{"newab":"hello2"}`)},
  64. }, {
  65. config: map[string]interface{}{
  66. "sendSingle": true,
  67. "dataTemplate": `{"newab":"{{.ab}}"}`,
  68. },
  69. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  70. result: [][]byte{[]byte(`{"newab":"hello1"}`), []byte(`{"newab":"hello2"}`)},
  71. }, {
  72. config: map[string]interface{}{
  73. "sendSingle": true,
  74. "dataTemplate": `{"__meta":{{toJson .__meta}},"temp":{{.temperature}}}`,
  75. },
  76. data: []map[string]interface{}{{"temperature": 33, "humidity": 70, "__meta": xsql.Metadata{"messageid": 45, "other": "mock"}}},
  77. result: [][]byte{[]byte(`{"__meta":{"messageid":45,"other":"mock"},"temp":33}`)},
  78. }, {
  79. config: map[string]interface{}{
  80. "dataTemplate": `[{"__meta":{{toJson (index . 0 "__meta")}},"temp":{{index . 0 "temperature"}}}]`,
  81. },
  82. data: []map[string]interface{}{{"temperature": 33, "humidity": 70, "__meta": xsql.Metadata{"messageid": 45, "other": "mock"}}},
  83. result: [][]byte{[]byte(`[{"__meta":{"messageid":45,"other":"mock"},"temp":33}]`)},
  84. }, {
  85. config: map[string]interface{}{
  86. "dataTemplate": `[{{range $index, $ele := .}}{{if $index}},{{end}}{"result":{{add $ele.temperature $ele.humidity}}}{{end}}]`,
  87. },
  88. data: []map[string]interface{}{{"temperature": 33, "humidity": 70}, {"temperature": 22.0, "humidity": 50}, {"temperature": 11, "humidity": 90}},
  89. result: [][]byte{[]byte(`[{"result":103},{"result":72},{"result":101}]`)},
  90. }, {
  91. config: map[string]interface{}{
  92. "dataTemplate": `{{$counter := 0}}{{range $index, $ele := .}}{{if ne 90 $ele.humidity}}{{$counter = add $counter 1}}{{end}}{{end}}{"result":{{$counter}}}`,
  93. },
  94. data: []map[string]interface{}{{"temperature": 33, "humidity": 70}, {"temperature": 22.0, "humidity": 50}, {"temperature": 11, "humidity": 90}},
  95. result: [][]byte{[]byte(`{"result":2}`)},
  96. }, {
  97. config: map[string]interface{}{
  98. "dataTemplate": `{"a":"{{base64 .a}}","b":"{{base64 .b}}","c":"{{b64enc .c}}","d":"{{b64enc .d}}","e":"{{base64 .e}}"}`,
  99. "sendSingle": true,
  100. },
  101. data: []map[string]interface{}{{"a": 1, "b": 3.1415, "c": "hello", "d": "{\"hello\" : 3}", "e": map[string]interface{}{"humidity": 20, "temperature": 30}}},
  102. result: [][]byte{[]byte(`{"a":"MQ==","b":"My4xNDE1","c":"aGVsbG8=","d":"eyJoZWxsbyIgOiAzfQ==","e":"eyJodW1pZGl0eSI6MjAsInRlbXBlcmF0dXJlIjozMH0="}`)},
  103. },
  104. }
  105. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  106. contextLogger := conf.Log.WithField("rule", "TestSinkTemplate_Apply")
  107. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  108. for i, tt := range tests {
  109. mockSink := mocknode.NewMockSink()
  110. s := NewSinkNodeWithSink("mockSink", mockSink, tt.config)
  111. s.Open(ctx, make(chan error))
  112. s.input <- tt.data
  113. time.Sleep(1 * time.Second)
  114. s.close(ctx, contextLogger)
  115. results := mockSink.GetResults()
  116. if !reflect.DeepEqual(tt.result, results) {
  117. t.Errorf("%d \tresult mismatch:\n\nexp=%s\n\ngot=%s\n\n", i, tt.result, results)
  118. }
  119. }
  120. }
  121. func TestOmitEmpty_Apply(t *testing.T) {
  122. conf.InitConf()
  123. var tests = []struct {
  124. config map[string]interface{}
  125. data []map[string]interface{}
  126. result [][]byte
  127. }{
  128. { // 0
  129. config: map[string]interface{}{
  130. "sendSingle": true,
  131. "omitIfEmpty": true,
  132. },
  133. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  134. result: [][]byte{[]byte(`{"ab":"hello1"}`), []byte(`{"ab":"hello2"}`)},
  135. }, { // 1
  136. config: map[string]interface{}{
  137. "sendSingle": false,
  138. "omitIfEmpty": true,
  139. },
  140. data: []map[string]interface{}{{"ab": "hello1"}, {"ab": "hello2"}},
  141. result: [][]byte{[]byte(`[{"ab":"hello1"},{"ab":"hello2"}]`)},
  142. }, { // 2
  143. config: map[string]interface{}{
  144. "sendSingle": false,
  145. "omitIfEmpty": false,
  146. },
  147. data: []map[string]interface{}{},
  148. result: [][]byte{[]byte(`[]`)},
  149. }, { // 3
  150. config: map[string]interface{}{
  151. "sendSingle": false,
  152. "omitIfEmpty": false,
  153. },
  154. data: nil,
  155. result: [][]byte{[]byte(`null`)},
  156. }, { // 4
  157. config: map[string]interface{}{
  158. "sendSingle": true,
  159. "omitIfEmpty": false,
  160. },
  161. data: []map[string]interface{}{},
  162. result: nil,
  163. }, { // 5
  164. config: map[string]interface{}{
  165. "sendSingle": false,
  166. "omitIfEmpty": true,
  167. },
  168. data: []map[string]interface{}{},
  169. result: nil,
  170. }, { // 6
  171. config: map[string]interface{}{
  172. "sendSingle": false,
  173. "omitIfEmpty": true,
  174. },
  175. data: nil,
  176. result: nil,
  177. }, { // 7
  178. config: map[string]interface{}{
  179. "sendSingle": true,
  180. "omitIfEmpty": false,
  181. },
  182. data: []map[string]interface{}{},
  183. result: nil,
  184. }, { // 8
  185. config: map[string]interface{}{
  186. "sendSingle": true,
  187. "omitIfEmpty": true,
  188. },
  189. data: []map[string]interface{}{{"ab": "hello1"}, {}},
  190. result: [][]byte{[]byte(`{"ab":"hello1"}`)},
  191. }, { // 9
  192. config: map[string]interface{}{
  193. "sendSingle": true,
  194. "omitIfEmpty": false,
  195. },
  196. data: []map[string]interface{}{{"ab": "hello1"}, {}},
  197. result: [][]byte{[]byte(`{"ab":"hello1"}`), []byte(`{}`)},
  198. },
  199. }
  200. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  201. contextLogger := conf.Log.WithField("rule", "TestOmitEmpty_Apply")
  202. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  203. for i, tt := range tests {
  204. mockSink := mocknode.NewMockSink()
  205. s := NewSinkNodeWithSink("mockSink", mockSink, tt.config)
  206. s.Open(ctx, make(chan error))
  207. s.input <- tt.data
  208. time.Sleep(100 * time.Millisecond)
  209. s.close(ctx, contextLogger)
  210. results := mockSink.GetResults()
  211. if !reflect.DeepEqual(tt.result, results) {
  212. t.Errorf("%d \tresult mismatch:\n\nexp=%s\n\ngot=%s\n\n", i, tt.result, results)
  213. }
  214. }
  215. }