rule_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2021 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. "github.com/lf-edge/ekuiper/pkg/api"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestRuleActionParse_Apply(t *testing.T) {
  21. var tests = []struct {
  22. ruleStr string
  23. result *api.Rule
  24. }{
  25. {
  26. ruleStr: `{
  27. "id": "ruleTest",
  28. "sql": "SELECT * from demo",
  29. "actions": [
  30. {
  31. "funcName": "RFC_READ_TABLE",
  32. "ashost": "192.168.1.100",
  33. "sysnr": "02",
  34. "client": "900",
  35. "user": "SPERF",
  36. "passwd": "PASSPASS",
  37. "params": {
  38. "QUERY_TABLE": "VBAP",
  39. "ROWCOUNT": 10,
  40. "FIELDS": [
  41. {"FIELDNAME": "MANDT"},
  42. {"FIELDNAME": "VBELN"},
  43. {"FIELDNAME": "POSNR"}
  44. ]
  45. }
  46. }
  47. ]
  48. }`,
  49. result: &api.Rule{
  50. Triggered: false,
  51. Id: "ruleTest",
  52. Sql: "SELECT * from demo",
  53. Actions: []map[string]interface{}{
  54. {
  55. "funcName": "RFC_READ_TABLE",
  56. "ashost": "192.168.1.100",
  57. "sysnr": "02",
  58. "client": "900",
  59. "user": "SPERF",
  60. "passwd": "PASSPASS",
  61. "params": map[string]interface{}{
  62. "QUERY_TABLE": "VBAP",
  63. "ROWCOUNT": float64(10),
  64. "FIELDS": []interface{}{
  65. map[string]interface{}{"FIELDNAME": "MANDT"},
  66. map[string]interface{}{"FIELDNAME": "VBELN"},
  67. map[string]interface{}{"FIELDNAME": "POSNR"},
  68. },
  69. },
  70. },
  71. },
  72. Options: &api.RuleOption{
  73. IsEventTime: false,
  74. LateTol: 1000,
  75. Concurrency: 1,
  76. BufferLength: 1024,
  77. SendMetaToSink: false,
  78. Qos: api.AtMostOnce,
  79. CheckpointInterval: 300000,
  80. SendError: true,
  81. },
  82. },
  83. }, {
  84. ruleStr: `{
  85. "id": "ruleTest2",
  86. "sql": "SELECT * from demo",
  87. "actions": [
  88. {
  89. "log": ""
  90. },
  91. {
  92. "sap": {
  93. "funcName": "RFC_READ_TABLE",
  94. "ashost": "192.168.100.10",
  95. "sysnr": "02",
  96. "client": "900",
  97. "user": "uuu",
  98. "passwd": "ppp."
  99. }
  100. }
  101. ],
  102. "options": {
  103. "isEventTime": true,
  104. "lateTolerance": 1000,
  105. "bufferLength": 10240,
  106. "qos": 2,
  107. "checkpointInterval": 60000
  108. }
  109. }`,
  110. result: &api.Rule{
  111. Triggered: false,
  112. Id: "ruleTest2",
  113. Sql: "SELECT * from demo",
  114. Actions: []map[string]interface{}{
  115. {
  116. "log": "",
  117. }, {
  118. "sap": map[string]interface{}{
  119. "funcName": "RFC_READ_TABLE",
  120. "ashost": "192.168.100.10",
  121. "sysnr": "02",
  122. "client": "900",
  123. "user": "uuu",
  124. "passwd": "ppp.",
  125. },
  126. },
  127. },
  128. Options: &api.RuleOption{
  129. IsEventTime: true,
  130. LateTol: 1000,
  131. Concurrency: 1,
  132. BufferLength: 10240,
  133. SendMetaToSink: false,
  134. Qos: api.ExactlyOnce,
  135. CheckpointInterval: 60000,
  136. SendError: true,
  137. },
  138. },
  139. },
  140. }
  141. p := NewRuleProcessor()
  142. for i, tt := range tests {
  143. r, err := p.getRuleByJson(tt.result.Id, tt.ruleStr)
  144. if err != nil {
  145. t.Errorf("get rule error: %s", err)
  146. }
  147. if !reflect.DeepEqual(tt.result, r) {
  148. t.Errorf("%d \tresult mismatch:\n\nexp=%+v\n\ngot=%+v\n\n", i, tt.result, r)
  149. }
  150. }
  151. }