source_node_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 node
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/conf"
  17. "github.com/lf-edge/ekuiper/internal/topo/context"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. "github.com/lf-edge/ekuiper/pkg/cast"
  20. "reflect"
  21. "testing"
  22. )
  23. func TestGetConf_Apply(t *testing.T) {
  24. result := map[string]interface{}{
  25. "interval": 1000,
  26. "ashost": "192.168.1.100",
  27. "sysnr": "02",
  28. "client": "900",
  29. "user": "SPERF",
  30. "passwd": "PASSPASS",
  31. "format": "json",
  32. "params": map[string]interface{}{
  33. "QUERY_TABLE": "VBAP",
  34. "ROWCOUNT": 10,
  35. "FIELDS": []interface{}{
  36. map[string]interface{}{"FIELDNAME": "MANDT"},
  37. map[string]interface{}{"FIELDNAME": "VBELN"},
  38. map[string]interface{}{"FIELDNAME": "POSNR"},
  39. },
  40. },
  41. }
  42. n := NewSourceNode("test", ast.TypeStream, &ast.Options{
  43. DATASOURCE: "RFC_READ_TABLE",
  44. TYPE: "test",
  45. })
  46. contextLogger := conf.Log.WithField("rule", "test")
  47. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  48. conf := getSourceConf(ctx, n.sourceType, n.options)
  49. if !reflect.DeepEqual(result, conf) {
  50. t.Errorf("result mismatch:\n\nexp=%s\n\ngot=%s\n\n", result, conf)
  51. }
  52. }
  53. func TestGetConfAndConvert_Apply(t *testing.T) {
  54. result := map[string]interface{}{
  55. "interval": 100,
  56. "seed": 1,
  57. "format": "json",
  58. "pattern": map[string]interface{}{
  59. "count": 50,
  60. },
  61. "deduplicate": 50,
  62. }
  63. n := NewSourceNode("test", ast.TypeStream, &ast.Options{
  64. DATASOURCE: "test",
  65. TYPE: "random",
  66. CONF_KEY: "dedup",
  67. })
  68. contextLogger := conf.Log.WithField("rule", "test")
  69. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  70. conf := getSourceConf(ctx, n.sourceType, n.options)
  71. if !reflect.DeepEqual(result, conf) {
  72. t.Errorf("result mismatch:\n\nexp=%s\n\ngot=%s\n\n", result, conf)
  73. return
  74. }
  75. r := &randomSourceConfig{
  76. Interval: 100,
  77. Seed: 1,
  78. Pattern: map[string]interface{}{
  79. "count": float64(50),
  80. },
  81. Deduplicate: 50,
  82. }
  83. cfg := &randomSourceConfig{}
  84. err := cast.MapToStruct(conf, cfg)
  85. if err != nil {
  86. t.Errorf("map to sturct error %s", err)
  87. return
  88. }
  89. if !reflect.DeepEqual(r, cfg) {
  90. t.Errorf("result mismatch:\n\nexp=%v\n\ngot=%v\n\n", r, cfg)
  91. return
  92. }
  93. }
  94. type randomSourceConfig struct {
  95. Interval int `json:"interval"`
  96. Seed int `json:"seed"`
  97. Pattern map[string]interface{} `json:"pattern"`
  98. Deduplicate int `json:"deduplicate"`
  99. }