yamlConfigMeta_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "fmt"
  17. "sync"
  18. "testing"
  19. )
  20. type MockSourcesConfigOps struct {
  21. *ConfigKeys
  22. }
  23. func (m MockSourcesConfigOps) IsSource() bool {
  24. return true
  25. }
  26. func (m MockSourcesConfigOps) SaveCfgToFile() error {
  27. return nil
  28. }
  29. func TestYamlConfigMeta_Ops(t *testing.T) {
  30. plgName := "mocksource"
  31. yamlKey := fmt.Sprintf(SourceCfgOperatorKeyTemplate, plgName)
  32. addData := `{"url":"127.0.0.1","method":"post","headers":{"Accept":"json"}}`
  33. ConfigManager.cfgOperators[yamlKey] = MockSourcesConfigOps{
  34. &ConfigKeys{
  35. sync.RWMutex{},
  36. plgName,
  37. make(map[string]map[string]interface{}),
  38. },
  39. }
  40. // init new ConfigOperator, success
  41. err := AddSourceConfKey(plgName, "new", "en_US", []byte(addData))
  42. if err != nil {
  43. t.Error(err)
  44. }
  45. //Exist ConfigKey , fail
  46. err = AddSourceConfKey(plgName, "new", "en_US", []byte(addData))
  47. if err == nil {
  48. t.Error("should return error when overwrite exist config key")
  49. }
  50. addData1 := `{"interval":10000, "timeout":200}`
  51. // no exist key, fail
  52. noExistKey := "noexist"
  53. err = AddSourceConfKeyField(noExistKey, "new", "en_US", []byte(addData1))
  54. if err == nil {
  55. t.Error("should return error when no exist key")
  56. }
  57. // exist key, success
  58. ExistKey := plgName
  59. err = AddSourceConfKeyField(ExistKey, "new", "en_US", []byte(addData1))
  60. if err != nil {
  61. t.Error(err)
  62. }
  63. // exist key, success
  64. err = DelSourceConfKeyField(ExistKey, "new", "en_US", []byte(addData1))
  65. if err != nil {
  66. t.Error(err)
  67. }
  68. // exist key, success
  69. err = DelSourceConfKey(ExistKey, "new", "en_US")
  70. if err != nil {
  71. t.Error(err)
  72. }
  73. }