yamlConfigMeta_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 overwrite exist config key")
  49. }
  50. }