client_edgex_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright 2022 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/v2/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. PublishHost: types.HostInfo{
  45. Host: "127.0.0.1",
  46. Port: 1883,
  47. Protocol: "tcp",
  48. },
  49. SubscribeHost: types.HostInfo{
  50. Host: "127.0.0.1",
  51. Port: 1883,
  52. Protocol: "tcp",
  53. },
  54. Type: "mqtt",
  55. Optional: map[string]string{
  56. "ClientId": "client1",
  57. "Username": "user1",
  58. "KeepAlive": "500",
  59. },
  60. },
  61. },
  62. {
  63. name: "config pass",
  64. props: map[string]interface{}{
  65. "protocol": "redis",
  66. "server": "edgex-redis",
  67. "port": 6379,
  68. "type": "redis",
  69. },
  70. wantErr: false,
  71. expConf: types.MessageBusConfig{
  72. PublishHost: types.HostInfo{
  73. Host: "edgex-redis",
  74. Port: 6379,
  75. Protocol: "redis",
  76. },
  77. SubscribeHost: types.HostInfo{
  78. Host: "edgex-redis",
  79. Port: 6379,
  80. Protocol: "redis",
  81. },
  82. Type: "redis",
  83. },
  84. },
  85. {
  86. name: "config not case sensitive",
  87. props: map[string]interface{}{
  88. "Protocol": "tcp",
  89. "server": "127.0.0.1",
  90. "Port": 1883,
  91. "type": "mqtt",
  92. "optional": map[string]string{
  93. "ClientId": "client1",
  94. "Username": "user1",
  95. },
  96. },
  97. expConf: types.MessageBusConfig{
  98. PublishHost: types.HostInfo{
  99. Host: "127.0.0.1",
  100. Port: 1883,
  101. Protocol: "tcp",
  102. },
  103. SubscribeHost: types.HostInfo{
  104. Host: "127.0.0.1",
  105. Port: 1883,
  106. Protocol: "tcp",
  107. },
  108. Type: "mqtt",
  109. Optional: map[string]string{
  110. "ClientId": "client1",
  111. "Username": "user1",
  112. },
  113. },
  114. wantErr: false,
  115. },
  116. {
  117. name: "config type not in zero/mqtt/redis ",
  118. props: map[string]interface{}{
  119. "protocol": "tcp",
  120. "server": "127.0.0.1",
  121. "port": 1883,
  122. "type": "kafka",
  123. "optional": map[string]string{
  124. "ClientId": "client1",
  125. "Username": "user1",
  126. },
  127. },
  128. wantErr: true,
  129. },
  130. {
  131. name: "do not have enough config items ",
  132. props: map[string]interface{}{
  133. "optional": map[string]string{
  134. "ClientId": "client1",
  135. "Username": "user1",
  136. },
  137. },
  138. expConf: types.MessageBusConfig{
  139. PublishHost: types.HostInfo{
  140. Host: "localhost",
  141. Port: 6379,
  142. Protocol: "redis",
  143. },
  144. SubscribeHost: types.HostInfo{
  145. Host: "localhost",
  146. Port: 6379,
  147. Protocol: "redis",
  148. },
  149. Type: "redis",
  150. Optional: map[string]string{
  151. "ClientId": "client1",
  152. "Username": "user1",
  153. },
  154. },
  155. wantErr: false,
  156. },
  157. {
  158. name: "type is not right",
  159. props: map[string]interface{}{
  160. "type": 20,
  161. "protocol": "redis",
  162. "host": "edgex-redis",
  163. "port": 6379,
  164. "optional": map[string]string{
  165. "ClientId": "client1",
  166. "Username": "user1",
  167. },
  168. },
  169. wantErr: true,
  170. },
  171. {
  172. name: "port is not right",
  173. props: map[string]interface{}{
  174. "type": "mqtt",
  175. "protocol": "redis",
  176. "host": "edgex-redis",
  177. "port": -1,
  178. "optional": map[string]string{
  179. "ClientId": "client1",
  180. "Username": "user1",
  181. },
  182. },
  183. wantErr: true,
  184. },
  185. {
  186. name: "wrong type value",
  187. props: map[string]interface{}{
  188. "type": "zmq",
  189. "protocol": "redis",
  190. "host": "edgex-redis",
  191. "port": 6379,
  192. "optional": map[string]string{
  193. "ClientId": "client1",
  194. "Username": "user1",
  195. },
  196. },
  197. wantErr: true,
  198. },
  199. }
  200. for _, tt := range tests {
  201. t.Run(tt.name, func(t *testing.T) {
  202. es := &EdgexClient{}
  203. if err := es.CfgValidate(tt.props); (err != nil) != tt.wantErr {
  204. t.Errorf("CfgValidate() error = %v, wantErr %v", err, tt.wantErr)
  205. } else {
  206. if !reflect.DeepEqual(tt.expConf, es.mbconf) {
  207. t.Errorf("CfgValidate() expect = %v, actual %v", tt.expConf, es.mbconf)
  208. }
  209. }
  210. })
  211. }
  212. }