yaml_config_ops_test.go 3.4 KB

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