client_edgex_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "reflect"
  19. "testing"
  20. )
  21. func TestEdgex_CfgValidate(t *testing.T) {
  22. type fields struct {
  23. mbconf types.MessageBusConfig
  24. }
  25. type args struct {
  26. props map[string]interface{}
  27. }
  28. tests := []struct {
  29. name string
  30. fields fields
  31. args args
  32. wantErr bool
  33. }{
  34. {
  35. name: "config pass",
  36. fields: fields{},
  37. args: args{props: map[string]interface{}{
  38. "protocol": "tcp",
  39. "server": "127.0.0.1",
  40. "port": int64(1883),
  41. "type": "mqtt",
  42. "optional": map[string]string{
  43. "ClientId": "client1",
  44. "Username": "user1",
  45. },
  46. }},
  47. wantErr: false,
  48. },
  49. {
  50. name: "config not case sensitive",
  51. fields: fields{},
  52. args: args{props: map[string]interface{}{
  53. "Protocol": "tcp",
  54. "server": "127.0.0.1",
  55. "Port": 1883,
  56. "type": "mqtt",
  57. "optional": map[string]string{
  58. "ClientId": "client1",
  59. "Username": "user1",
  60. },
  61. }},
  62. wantErr: false,
  63. },
  64. {
  65. name: "have unwanted config items topic",
  66. fields: fields{},
  67. args: args{props: map[string]interface{}{
  68. "protocol": "tcp",
  69. "server": "127.0.0.1",
  70. "port": 1883,
  71. "type": "mqtt",
  72. "optional": map[string]string{
  73. "ClientId": "client1",
  74. "Username": "user1",
  75. },
  76. "topic": "demo",
  77. }},
  78. wantErr: true,
  79. },
  80. {
  81. name: "config type not in zero/mqtt/redis ",
  82. fields: fields{},
  83. args: args{props: map[string]interface{}{
  84. "protocol": "tcp",
  85. "server": "127.0.0.1",
  86. "port": 1883,
  87. "type": "kafka",
  88. "optional": map[string]string{
  89. "ClientId": "client1",
  90. "Username": "user1",
  91. },
  92. }},
  93. wantErr: true,
  94. },
  95. {
  96. name: "do not have enough config items ",
  97. fields: fields{},
  98. args: args{props: map[string]interface{}{
  99. "protocol": "tcp",
  100. "type": "mqtt",
  101. "optional": map[string]string{
  102. "ClientId": "client1",
  103. "Username": "user1",
  104. },
  105. }},
  106. wantErr: true,
  107. },
  108. }
  109. for _, tt := range tests {
  110. t.Run(tt.name, func(t *testing.T) {
  111. es := &EdgexClient{
  112. selector: &ConSelector{
  113. ConnSelectorCfg: "testSelector",
  114. },
  115. mbconf: tt.fields.mbconf,
  116. }
  117. if err := es.CfgValidate(tt.args.props); (err != nil) != tt.wantErr {
  118. t.Errorf("CfgValidate() error = %v, wantErr %v", err, tt.wantErr)
  119. }
  120. })
  121. }
  122. }
  123. func TestEdgex_CorrectsConfigKey(t *testing.T) {
  124. var props = map[string]interface{}{
  125. "protocol": "tcp",
  126. "server": "127.0.0.1",
  127. "port": int64(1883),
  128. "type": "mqtt",
  129. "optional": map[string]interface{}{
  130. "clientid": "client1",
  131. "username": "user1",
  132. "password": "password",
  133. },
  134. }
  135. es := &EdgexClient{
  136. selector: &ConSelector{
  137. ConnSelectorCfg: "testSelector",
  138. },
  139. }
  140. err := es.CfgValidate(props)
  141. if err != nil {
  142. t.Errorf("Error %v", err)
  143. }
  144. expectOps := map[string]string{
  145. "ClientId": "client1",
  146. "Username": "user1",
  147. "Password": "password",
  148. }
  149. if !reflect.DeepEqual(es.mbconf.Optional, expectOps) {
  150. t.Errorf("CfgValidate() expect = %+v, actual %+v", expectOps, es.mbconf.Optional)
  151. }
  152. }