client_mqtt_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package connection
  2. import (
  3. "github.com/lf-edge/ekuiper/internal/conf"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestMQTTClient_CfgValidate(t *testing.T) {
  8. type args struct {
  9. props map[string]interface{}
  10. }
  11. tests := []struct {
  12. name string
  13. args args
  14. wantErr bool
  15. }{
  16. {
  17. name: "config pass",
  18. args: args{
  19. props: map[string]interface{}{
  20. "servers": []string{"tcp:127.0.0.1"},
  21. },
  22. },
  23. wantErr: false,
  24. },
  25. {
  26. name: "config are not case sensitive",
  27. args: args{
  28. props: map[string]interface{}{
  29. "SERVERS": []string{"tcp:127.0.0.1"},
  30. },
  31. },
  32. wantErr: false,
  33. },
  34. {
  35. name: "config server addr key error",
  36. args: args{
  37. props: map[string]interface{}{
  38. "server": []string{"tcp:127.0.0.1"},
  39. },
  40. },
  41. wantErr: true,
  42. },
  43. {
  44. name: "config have unwanted topic fields",
  45. args: args{
  46. props: map[string]interface{}{
  47. "servers": []string{"tcp:127.0.0.1"},
  48. "topic": "demo",
  49. },
  50. },
  51. wantErr: true,
  52. },
  53. {
  54. name: "config no server addr",
  55. args: args{
  56. props: map[string]interface{}{
  57. "username": "user1",
  58. },
  59. },
  60. wantErr: true,
  61. },
  62. {
  63. name: "config no server addr",
  64. args: args{
  65. props: map[string]interface{}{
  66. "servers": []string{},
  67. },
  68. },
  69. wantErr: true,
  70. },
  71. {
  72. name: "config miss cert key file",
  73. args: args{
  74. props: map[string]interface{}{
  75. "servers": []string{"tcp:127.0.0.1"},
  76. "certificationPath": "./not_exist.crt",
  77. "privateKeyPath": "./not_exist.key",
  78. },
  79. },
  80. wantErr: true,
  81. },
  82. }
  83. for _, tt := range tests {
  84. t.Run(tt.name, func(t *testing.T) {
  85. ms := &MQTTClient{
  86. selector: &conf.ConSelector{
  87. ConnSelectorStr: "testSelector",
  88. },
  89. }
  90. err := ms.CfgValidate(tt.args.props)
  91. if (err != nil) != tt.wantErr {
  92. t.Errorf("CfgValidate() error = %v, wantErr %v", err, tt.wantErr)
  93. }
  94. })
  95. }
  96. }
  97. func TestMQTTClient_CfgResult(t *testing.T) {
  98. props := map[string]interface{}{
  99. "servers": []string{"tcp:127.0.0.1:1883"},
  100. "USERNAME": "demo",
  101. "Password": "password",
  102. "clientID": "clientid",
  103. }
  104. ms := &MQTTClient{
  105. selector: &conf.ConSelector{
  106. ConnSelectorStr: "testSelector",
  107. },
  108. }
  109. _ = ms.CfgValidate(props)
  110. if !reflect.DeepEqual("tcp:127.0.0.1:1883", ms.srv) {
  111. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.srv)
  112. }
  113. if !reflect.DeepEqual("demo", ms.uName) {
  114. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.uName)
  115. }
  116. if !reflect.DeepEqual("password", ms.password) {
  117. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.password)
  118. }
  119. if !reflect.DeepEqual("clientid", ms.clientid) {
  120. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.clientid)
  121. }
  122. if !reflect.DeepEqual(uint(3), ms.pVersion) {
  123. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.pVersion)
  124. }
  125. }