sourceMeta_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 plugin
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. )
  21. var (
  22. gFile = `httppull.json`
  23. gPlugin = `httppull`
  24. gCf = `{"default":{"url":"http://localhost","method":"post","interval":10000,"timeout":5000,"body":"{}","bodyType":"json","headers":{"Accept":"application/json"}},"ck1":{"url":"127.0.0.1:9527","method":"get","interval":1000,"headers":{"Accept":"application/json"}},"ck2":{"method":"delete","interval":100,"url":"http://localhost:9090/pull"}}`
  25. gTemplate = `{"author":{"name":"Jiyong Huang","email":"huangjy@emqx.io","company":"EMQ Technologies Co., Ltd","website":"https://www.emqx.io"},"libs":[],"helpUrl":{"en_US":"https://github.com/lf-edge/ekuiper/blob/master/docs/en_US/rules/sources/http_pull.md","zh_CN":"https://github.com/lf-edge/ekuiper/blob/master/docs/zh_CN/rules/sources/http_pull.md"},"properties":{"default":[{"name":"url","default":"127.0.0.1:5536","optional":false,"control":"text","type":"string","hint":{"en_US":"The URL where to get the result.","zh_CN":"获取结果的 URL"},"label":{"en_US":"URL","zh_CN":"路径"}},{"name":"method","default":"","optional":false,"control":"text","type":"string","hint":{"en_US":"HTTP method, it could be post, get, put & delete.","zh_CN":"HTTP 方法,它可以是 post、get、put 和 delete。"},"label":{"en_US":"HTTP method","zh_CN":"HTTP 方法"}},{"name":"interval","default":1000,"optional":false,"control":"text","type":"int","hint":{"en_US":"The interval between the requests, time unit is ms.","zh_CN":"请求之间的间隔时间,单位为 ms"},"label":{"en_US":"Interval","zh_CN":"间隔时间"}},{"name":"timeout","default":5000,"optional":false,"control":"text","type":"int","hint":{"en_US":"The timeout for http request, time unit is ms.","zh_CN":"http 请求的超时时间,单位为 ms"},"label":{"en_US":"Timeout","zh_CN":"超时时间"}},{"name":"incremental","default":false,"optional":false,"control":"text","type":"bool","hint":{"en_US":"If it's set to true, then will compare with last result; If response of two requests are the same, then will skip sending out the result.","zh_CN":"如果将其设置为 true,则将与最后的结果进行比较; 如果两个请求的响应相同,则将跳过发送结果。"},"label":{"en_US":"Incremental","zh_CN":"递增"}},{"name":"body","default":"{}","optional":false,"control":"text","type":"string","hint":{"en_US":"The body of request","zh_CN":"请求的正文"},"label":{"en_US":"Body","zh_CN":"正文"}},{"name":"bodyType","default":"json","optional":false,"control":"text","type":"string","hint":{"en_US":"Body type, it could be none|text|json|html|xml|javascript|format.","zh_CN":"正文类型,可以是 none|text|json|html|xml|javascript| 格式"},"label":{"en_US":"Body type","zh_CN":"正文类型"}},{"name":"headers","default":[{"name":"Accept","default":"application/json","optional":false,"control":"text","type":"string","hint":{"en_US":"HTTP headers","zh_CN":"HTTP标头"},"label":{"en_US":"HTTP headers","zh_CN":"HTTP标头"}}],"optional":false,"control":"text","type":"string","hint":{"en_US":"The HTTP request headers that you want to send along with the HTTP request.","zh_CN":"需要与 HTTP 请求一起发送的 HTTP 请求标头。"},"label":{"en_US":"HTTP headers","zh_CN":"HTTP标头"}}]}}`
  26. )
  27. func TestGetSourceMeta(t *testing.T) {
  28. source := new(sourceProperty)
  29. var cf map[string]map[string]interface{}
  30. if err := json.Unmarshal([]byte(gCf), &cf); nil != err {
  31. t.Error(err)
  32. }
  33. var fileMeta = new(fileSource)
  34. if err := json.Unmarshal([]byte(gTemplate), fileMeta); nil != err {
  35. t.Error(err)
  36. }
  37. meta, err := newUiSource(fileMeta)
  38. if nil != err {
  39. t.Error(err)
  40. }
  41. source.cf = cf
  42. source.meta = meta
  43. gSourceproperty = make(map[string]*sourceProperty)
  44. gSourceproperty[gFile] = source
  45. showMeta, err := GetSourceMeta(gPlugin, "zh_CN")
  46. if nil != err {
  47. t.Error(err)
  48. }
  49. if err := compare(source, showMeta); nil != err {
  50. t.Error(err)
  51. }
  52. addData := `{"url":"127.0.0.1","method":"post","headers":{"Accept":"json"}}`
  53. delData := `{"method":"","headers":{"Accept":""}}`
  54. if err := AddSourceConfKey(gPlugin, "new", "zh_CN", []byte(addData)); nil != err {
  55. t.Error(err)
  56. }
  57. if err := isAddData(addData, cf[`new`]); nil != err {
  58. t.Error(err)
  59. }
  60. if err := DelSourceConfKeyField(gPlugin, "new", "zh_CN", []byte(delData)); nil != err {
  61. t.Error(err)
  62. }
  63. if err := isDelData(delData, cf[`new`]); nil != err {
  64. t.Error(err)
  65. }
  66. }
  67. func isDelData(js string, cf map[string]interface{}) error {
  68. var delNode map[string]interface{}
  69. if err := json.Unmarshal([]byte(js), &delNode); nil != err {
  70. return err
  71. }
  72. for delk, delv := range delNode {
  73. if nil == delv {
  74. if _, ok := cf[delk]; ok {
  75. return fmt.Errorf("%s still exists", delk)
  76. }
  77. }
  78. switch t := delv.(type) {
  79. case string:
  80. if 0 == len(t) {
  81. if _, ok := cf[delk]; ok {
  82. return fmt.Errorf("%s still exists", delk)
  83. }
  84. }
  85. case map[string]interface{}:
  86. if b, err := json.Marshal(t); nil != err {
  87. return fmt.Errorf("request format error")
  88. } else {
  89. var auxCf map[string]interface{}
  90. if err := marshalUn(cf[delk], &auxCf); nil == err {
  91. if err := isDelData(string(b), auxCf); nil != err {
  92. return err
  93. }
  94. }
  95. }
  96. }
  97. }
  98. return nil
  99. }
  100. func isAddData(js string, cf map[string]interface{}) error {
  101. var addNode map[string]interface{}
  102. if err := json.Unmarshal([]byte(js), &addNode); nil != err {
  103. return err
  104. }
  105. for addk := range addNode {
  106. if _, ok := cf[addk]; !ok {
  107. return fmt.Errorf("not found key:%s", addk)
  108. }
  109. }
  110. return nil
  111. }
  112. func marshalUn(input, output interface{}) error {
  113. jsonString, err := json.Marshal(input)
  114. if err != nil {
  115. return err
  116. }
  117. return json.Unmarshal(jsonString, output)
  118. }
  119. func compareUiCf(ui []field, cf map[string]interface{}) (err error) {
  120. for i := 0; i < len(ui); i++ {
  121. if !ui[i].Exist {
  122. continue
  123. }
  124. if v, ok := cf[ui[i].Name]; ok {
  125. if v == ui[i].Default {
  126. continue
  127. }
  128. if nil == v || nil == ui[i].Default {
  129. return fmt.Errorf("default of %s is nil", ui[i].Name)
  130. }
  131. if reflect.Map == reflect.TypeOf(v).Kind() {
  132. var auxUi []field
  133. if err = marshalUn(ui[i].Default, &auxUi); nil != err {
  134. return err
  135. }
  136. var auxCf map[string]interface{}
  137. if err = marshalUn(v, &auxCf); nil != err {
  138. return err
  139. }
  140. if err = compareUiCf(auxUi, auxCf); nil != err {
  141. return err
  142. }
  143. } else if ui[i].Default != v {
  144. return fmt.Errorf("not equal->%s:{cf:%v,ui:%v}", ui[i].Name, v, ui[i])
  145. }
  146. } else {
  147. return fmt.Errorf("%s is not in the configuration file", ui[i].Name)
  148. }
  149. }
  150. return nil
  151. }
  152. func compareUiTp(ui, tp []field) (err error) {
  153. for i := 0; i < len(ui); i++ {
  154. j := 0
  155. for ; j < len(tp); j++ {
  156. if ui[i].Name != tp[j].Name {
  157. continue
  158. }
  159. if ui[i].Type != tp[j].Type {
  160. return fmt.Errorf("not equal->%s type:{tp:%v,ui:%v}", ui[i].Name, tp[j].Type, ui[i].Type)
  161. }
  162. if ui[i].Control != tp[j].Control {
  163. return fmt.Errorf("not equal->%s control:{tp:%v,ui:%v}", ui[i].Name, tp[j].Control, ui[i].Control)
  164. }
  165. if ui[i].Optional != tp[j].Optional {
  166. return fmt.Errorf("not equal->%s optional:{tp:%v,ui:%v}", ui[i].Name, tp[j].Optional, ui[i].Optional)
  167. }
  168. if ui[i].Values != tp[j].Values {
  169. return fmt.Errorf("not equal->%s values:{tp:%v,ui:%v}", ui[i].Name, tp[j].Values, ui[i].Values)
  170. }
  171. if ui[i].Hint != tp[j].Hint {
  172. if nil == ui[i].Hint || nil == tp[j].Hint {
  173. return fmt.Errorf("hint of %s is nil", ui[i].Name)
  174. }
  175. if ui[i].Hint.English != tp[j].Hint.English {
  176. return fmt.Errorf("not equal->%s hint.en_US:{tp:%v,ui:%v}", ui[i].Name, tp[j].Hint.English, ui[i].Hint.English)
  177. }
  178. if ui[i].Hint.Chinese != tp[j].Hint.Chinese {
  179. return fmt.Errorf("not equal->%s hint.zh_CN:{tp:%v,ui:%v}", ui[i].Name, tp[j].Hint.Chinese, ui[i].Hint.Chinese)
  180. }
  181. }
  182. if ui[i].Label != tp[j].Label {
  183. if nil == ui[i].Label || nil == tp[j].Label {
  184. return fmt.Errorf("label of %s is nil", ui[i].Name)
  185. }
  186. if ui[i].Label.English != tp[j].Label.English {
  187. return fmt.Errorf("not equal->%s label.en_US:{tp:%v,ui:%v}", ui[i].Name, tp[j].Label.English, ui[i].Label.English)
  188. }
  189. if ui[i].Label.Chinese != tp[j].Label.Chinese {
  190. return fmt.Errorf("not equal->%s label.zh_CN:{tp:%v,ui:%v}", ui[i].Name, tp[j].Label.Chinese, ui[i].Label.Chinese)
  191. }
  192. }
  193. if !ui[i].Exist {
  194. if nil == ui[i].Default || nil == tp[j].Default {
  195. return fmt.Errorf("The default of %s is nil", ui[i].Name)
  196. }
  197. }
  198. break
  199. }
  200. if len(tp) == j {
  201. return fmt.Errorf("%s is not in the template file", ui[i].Name)
  202. }
  203. }
  204. return nil
  205. }
  206. func compare(source *sourceProperty, uiMeta *uiSource) (err error) {
  207. tp := source.meta.ConfKeys["default"]
  208. for k, v := range source.cf {
  209. ui := uiMeta.ConfKeys[k]
  210. if err = compareUiCf(ui, v); nil != err {
  211. return err
  212. }
  213. if err = compareUiTp(ui, tp); nil != err {
  214. return err
  215. }
  216. }
  217. return nil
  218. }