client_edgex_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. //go:build edgex
  15. // +build edgex
  16. package connection
  17. import (
  18. "github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "testing"
  21. )
  22. func TestEdgex_CfgValidate(t *testing.T) {
  23. type fields struct {
  24. mbconf types.MessageBusConfig
  25. }
  26. type args struct {
  27. props map[string]interface{}
  28. }
  29. tests := []struct {
  30. name string
  31. fields fields
  32. args args
  33. wantErr bool
  34. }{
  35. {
  36. name: "config pass",
  37. fields: fields{},
  38. args: args{props: map[string]interface{}{
  39. "protocol": "tcp",
  40. "server": "127.0.0.1",
  41. "port": int64(1883),
  42. "type": "mqtt",
  43. "optional": map[string]string{
  44. "ClientId": "client1",
  45. "Username": "user1",
  46. },
  47. }},
  48. wantErr: false,
  49. },
  50. {
  51. name: "config not case sensitive",
  52. fields: fields{},
  53. args: args{props: map[string]interface{}{
  54. "Protocol": "tcp",
  55. "server": "127.0.0.1",
  56. "Port": 1883,
  57. "type": "mqtt",
  58. "optional": map[string]string{
  59. "ClientId": "client1",
  60. "Username": "user1",
  61. },
  62. }},
  63. wantErr: false,
  64. },
  65. {
  66. name: "have unwanted config items topic",
  67. fields: fields{},
  68. args: args{props: map[string]interface{}{
  69. "protocol": "tcp",
  70. "server": "127.0.0.1",
  71. "port": 1883,
  72. "type": "mqtt",
  73. "optional": map[string]string{
  74. "ClientId": "client1",
  75. "Username": "user1",
  76. },
  77. "topic": "demo",
  78. }},
  79. wantErr: true,
  80. },
  81. {
  82. name: "config type not in zero/mqtt/redis ",
  83. fields: fields{},
  84. args: args{props: map[string]interface{}{
  85. "protocol": "tcp",
  86. "server": "127.0.0.1",
  87. "port": 1883,
  88. "type": "kafka",
  89. "optional": map[string]string{
  90. "ClientId": "client1",
  91. "Username": "user1",
  92. },
  93. }},
  94. wantErr: true,
  95. },
  96. {
  97. name: "do not have enough config items ",
  98. fields: fields{},
  99. args: args{props: map[string]interface{}{
  100. "protocol": "tcp",
  101. "type": "mqtt",
  102. "optional": map[string]string{
  103. "ClientId": "client1",
  104. "Username": "user1",
  105. },
  106. }},
  107. wantErr: true,
  108. },
  109. }
  110. for _, tt := range tests {
  111. t.Run(tt.name, func(t *testing.T) {
  112. es := &EdgexClient{
  113. selector: &conf.ConSelector{
  114. ConnSelectorStr: "testSelector",
  115. },
  116. mbconf: tt.fields.mbconf,
  117. }
  118. if err := es.CfgValidate(tt.args.props); (err != nil) != tt.wantErr {
  119. t.Errorf("CfgValidate() error = %v, wantErr %v", err, tt.wantErr)
  120. }
  121. })
  122. }
  123. }