ruleset_test.go 3.4 KB

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