client_edgex_test.go 2.9 KB

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