sink_node_test.go 8.2 KB

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