yaml_config_ops_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. package conf
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "testing"
  19. )
  20. func TestConfigKeys_LoadSourceFile(t *testing.T) {
  21. _, err := NewConfigOperatorFromSourceYaml("mqtt")
  22. if err != nil {
  23. t.Error(err)
  24. }
  25. }
  26. func TestConfigKeys_LoadConnectionMqtt(t *testing.T) {
  27. _, err := NewConfigOperatorFromConnectionYaml("mqtt")
  28. if err != nil {
  29. t.Error(err)
  30. }
  31. }
  32. func TestConfigKeys_LoadConnectionEdgex(t *testing.T) {
  33. _, err := NewConfigOperatorFromConnectionYaml("edgex")
  34. if err != nil {
  35. t.Error(err)
  36. }
  37. }
  38. func TestConfigKeys_Ops(t *testing.T) {
  39. httpCfg, err := NewConfigOperatorFromSourceYaml("httppull")
  40. if err != nil {
  41. t.Error(err)
  42. }
  43. addData := `{"url":"127.0.0.1","method":"post","headers":{"Accept":"json"}}`
  44. delData := `{"method":"","headers":{"Accept":""}}`
  45. reqField := make(map[string]interface{})
  46. _ = json.Unmarshal([]byte(addData), &reqField)
  47. err = httpCfg.AddConfKey("new", reqField)
  48. if err != nil {
  49. t.Error(err)
  50. }
  51. if err := isAddData(addData, httpCfg.CopyConfContent()[`new`]); nil != err {
  52. t.Error(err)
  53. }
  54. delField := make(map[string]interface{})
  55. _ = json.Unmarshal([]byte(delData), &delField)
  56. err = httpCfg.DeleteConfKeyField("new", delField)
  57. if err != nil {
  58. t.Error(err)
  59. }
  60. if err := isDelData(delData, httpCfg.CopyConfContent()[`new`]); nil != err {
  61. t.Error(err)
  62. }
  63. }
  64. func marshalUn(input, output interface{}) error {
  65. jsonString, err := json.Marshal(input)
  66. if err != nil {
  67. return err
  68. }
  69. return json.Unmarshal(jsonString, output)
  70. }
  71. func isDelData(js string, cf map[string]interface{}) error {
  72. var delNode map[string]interface{}
  73. if err := json.Unmarshal([]byte(js), &delNode); nil != err {
  74. return err
  75. }
  76. for delk, delv := range delNode {
  77. if nil == delv {
  78. if _, ok := cf[delk]; ok {
  79. return fmt.Errorf("%s still exists", delk)
  80. }
  81. }
  82. switch t := delv.(type) {
  83. case string:
  84. if 0 == len(t) {
  85. if _, ok := cf[delk]; ok {
  86. return fmt.Errorf("%s still exists", delk)
  87. }
  88. }
  89. case map[string]interface{}:
  90. if b, err := json.Marshal(t); nil != err {
  91. return fmt.Errorf("request format error")
  92. } else {
  93. var auxCf map[string]interface{}
  94. if err := marshalUn(cf[delk], &auxCf); nil == err {
  95. if err := isDelData(string(b), auxCf); nil != err {
  96. return err
  97. }
  98. }
  99. }
  100. }
  101. }
  102. return nil
  103. }
  104. func isAddData(js string, cf map[string]interface{}) error {
  105. var addNode map[string]interface{}
  106. if err := json.Unmarshal([]byte(js), &addNode); nil != err {
  107. return err
  108. }
  109. for addk := range addNode {
  110. if _, ok := cf[addk]; !ok {
  111. return fmt.Errorf("not found key:%s", addk)
  112. }
  113. }
  114. return nil
  115. }