ruleset_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 processor
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/pkg/ast"
  18. "io"
  19. "reflect"
  20. "sort"
  21. "strings"
  22. "testing"
  23. )
  24. func TestIO(t *testing.T) {
  25. expected := `{"streams":{"demo":"CREATE STREAM demo () WITH (DATASOURCE=\"users\", FORMAT=\"JSON\")"},"tables":{},"rules":{"rule1":"{\"id\":\"rule1\",\"sql\": \"SELECT * FROM demo\",\"actions\": [{\"log\": {}}]}","rule2":"{\"id\": \"rule2\",\"sql\": \"SELECT * FROM demo\",\"actions\": [{ \"log\": {}}]}"}}`
  26. expectedCounts := []int{1, 0, 2}
  27. expectedStreams := []string{"demo"}
  28. expectedRules := []string{"rule1", "rule2"}
  29. sp := NewStreamProcessor()
  30. defer sp.db.Clean()
  31. rp := NewRuleProcessor()
  32. defer rp.db.Clean()
  33. rsp := NewRulesetProcessor(rp, sp)
  34. names, counts, err := rsp.Import([]byte(expected))
  35. if err != nil {
  36. t.Errorf("fail to import ruleset: %v", err)
  37. return
  38. }
  39. sort.Strings(names)
  40. if !reflect.DeepEqual(names, expectedRules) {
  41. t.Errorf("fail to return the imported rules, expect %v but got %v", expectedRules, names)
  42. }
  43. if !reflect.DeepEqual(counts, expectedCounts) {
  44. t.Errorf("fail to return the correct counts, expect %v, but got %v", expectedCounts, counts)
  45. }
  46. streams, err := sp.execShow(ast.TypeStream)
  47. if err != nil {
  48. t.Errorf("fail to get all streams: %v", err)
  49. return
  50. }
  51. if !reflect.DeepEqual(streams, expectedStreams) {
  52. t.Errorf("After import, expect streams %v, but got %v", expectedStreams, streams)
  53. return
  54. }
  55. rules, err := rp.GetAllRules()
  56. if err != nil {
  57. t.Errorf("fail to get all rules: %v", err)
  58. return
  59. }
  60. sort.Strings(rules)
  61. if !reflect.DeepEqual(rules, expectedRules) {
  62. t.Errorf("After import, expect rules %v, but got %v", expectedRules, rules)
  63. return
  64. }
  65. exp, exCounts, err := rsp.Export()
  66. if err != nil {
  67. t.Errorf("fail to export ruleset: %v", err)
  68. return
  69. }
  70. buf := new(strings.Builder)
  71. _, err = io.Copy(buf, exp)
  72. if err != nil {
  73. t.Errorf("fail to convert exported ruleset: %v", err)
  74. return
  75. }
  76. actual := buf.String()
  77. if actual != expected {
  78. t.Errorf("Expect\t\n %v but got\t\n %v", expected, actual)
  79. }
  80. if !reflect.DeepEqual(exCounts, expectedCounts) {
  81. t.Errorf("fail to return the correct counts, expect %v, but got %v", expectedCounts, exCounts)
  82. }
  83. }
  84. func TestImportError(t *testing.T) {
  85. contents := []string{
  86. "notjson",
  87. `{INvalid"streams":{"demo":"CREATE STREAM demo () WITH (DATASOURCE=\"users\", FORMAT=\"JSON\")"},"tables":{},"rules":{"rule1":"{\"id\":\"rule1\",\"sql\": \"SELECT * FROM demo\",\"actions\": [{\"log\": {}}]}","rule2":"{\"id\": \"rule2\",\"sql\": \"SELECT * FROM demo\",\"actions\": [{ \"log\": {}}]}"}}`,
  88. }
  89. sp := NewStreamProcessor()
  90. defer sp.db.Clean()
  91. rp := NewRuleProcessor()
  92. defer rp.db.Clean()
  93. rsp := NewRulesetProcessor(rp, sp)
  94. for i, content := range contents {
  95. _, _, err := rsp.Import([]byte(content))
  96. if err == nil {
  97. t.Errorf("%d fail, expect error but pass", i)
  98. } else {
  99. fmt.Println(err)
  100. }
  101. }
  102. }