client_edgex_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2022-2023 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 edgex
  17. import (
  18. "github.com/edgexfoundry/go-mod-messaging/v3/pkg/types"
  19. "reflect"
  20. "testing"
  21. )
  22. func TestEdgex_CfgValidate(t *testing.T) {
  23. tests := []struct {
  24. name string
  25. expConf types.MessageBusConfig
  26. props map[string]interface{}
  27. wantErr bool
  28. }{
  29. {
  30. name: "config pass",
  31. props: map[string]interface{}{
  32. "protocol": "tcp",
  33. "server": "127.0.0.1",
  34. "port": 1883,
  35. "type": "mqtt",
  36. "optional": map[string]interface{}{
  37. "ClientId": "client1",
  38. "Username": "user1",
  39. "KeepAlive": 500,
  40. },
  41. },
  42. wantErr: false,
  43. expConf: types.MessageBusConfig{
  44. Broker: types.HostInfo{
  45. Host: "127.0.0.1",
  46. Port: 1883,
  47. Protocol: "tcp",
  48. },
  49. Type: "mqtt",
  50. Optional: map[string]string{
  51. "ClientId": "client1",
  52. "Username": "user1",
  53. "KeepAlive": "500",
  54. },
  55. },
  56. },
  57. {
  58. name: "config pass",
  59. props: map[string]interface{}{
  60. "protocol": "redis",
  61. "server": "edgex-redis",
  62. "port": 6379,
  63. "type": "redis",
  64. },
  65. wantErr: false,
  66. expConf: types.MessageBusConfig{
  67. Broker: types.HostInfo{
  68. Host: "edgex-redis",
  69. Port: 6379,
  70. Protocol: "redis",
  71. },
  72. Type: "redis",
  73. },
  74. },
  75. {
  76. name: "config not case sensitive",
  77. props: map[string]interface{}{
  78. "Protocol": "tcp",
  79. "server": "127.0.0.1",
  80. "Port": 1883,
  81. "type": "mqtt",
  82. "optional": map[string]string{
  83. "ClientId": "client1",
  84. "Username": "user1",
  85. },
  86. },
  87. expConf: types.MessageBusConfig{
  88. Broker: types.HostInfo{
  89. Host: "127.0.0.1",
  90. Port: 1883,
  91. Protocol: "tcp",
  92. },
  93. Type: "mqtt",
  94. Optional: map[string]string{
  95. "ClientId": "client1",
  96. "Username": "user1",
  97. },
  98. },
  99. wantErr: false,
  100. },
  101. {
  102. name: "config type not in mqtt/redis ",
  103. props: map[string]interface{}{
  104. "protocol": "tcp",
  105. "server": "127.0.0.1",
  106. "port": 1883,
  107. "type": "kafka",
  108. "optional": map[string]string{
  109. "ClientId": "client1",
  110. "Username": "user1",
  111. },
  112. },
  113. wantErr: true,
  114. },
  115. {
  116. name: "do not have enough config items ",
  117. props: map[string]interface{}{
  118. "optional": map[string]string{
  119. "ClientId": "client1",
  120. "Username": "user1",
  121. },
  122. },
  123. expConf: types.MessageBusConfig{
  124. Broker: types.HostInfo{
  125. Host: "localhost",
  126. Port: 6379,
  127. Protocol: "redis",
  128. },
  129. Type: "redis",
  130. Optional: map[string]string{
  131. "ClientId": "client1",
  132. "Username": "user1",
  133. },
  134. },
  135. wantErr: false,
  136. },
  137. {
  138. name: "type is not right",
  139. props: map[string]interface{}{
  140. "type": 20,
  141. "protocol": "redis",
  142. "host": "edgex-redis",
  143. "port": 6379,
  144. "optional": map[string]string{
  145. "ClientId": "client1",
  146. "Username": "user1",
  147. },
  148. },
  149. wantErr: true,
  150. },
  151. {
  152. name: "port is not right",
  153. props: map[string]interface{}{
  154. "type": "mqtt",
  155. "protocol": "redis",
  156. "host": "edgex-redis",
  157. "port": -1,
  158. "optional": map[string]string{
  159. "ClientId": "client1",
  160. "Username": "user1",
  161. },
  162. },
  163. wantErr: true,
  164. },
  165. {
  166. name: "wrong type value",
  167. props: map[string]interface{}{
  168. "type": "mqt",
  169. "protocol": "redis",
  170. "host": "edgex-redis",
  171. "port": 6379,
  172. "optional": map[string]string{
  173. "ClientId": "client1",
  174. "Username": "user1",
  175. },
  176. },
  177. wantErr: true,
  178. },
  179. }
  180. for _, tt := range tests {
  181. t.Run(tt.name, func(t *testing.T) {
  182. es := &EdgexClient{}
  183. if err := es.CfgValidate(tt.props); (err != nil) != tt.wantErr {
  184. t.Errorf("CfgValidate() error = %v, wantErr %v", err, tt.wantErr)
  185. } else {
  186. if !reflect.DeepEqual(tt.expConf, es.mbconf) {
  187. t.Errorf("CfgValidate() expect = %v, actual %v", tt.expConf, es.mbconf)
  188. }
  189. }
  190. })
  191. }
  192. }