load_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // INTECH Process Automation 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. "os"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestEnv(t *testing.T) {
  21. key := "KUIPER__BASIC__CONSOLELOG"
  22. value := "true"
  23. err := os.Setenv(key, value)
  24. if err != nil {
  25. t.Error(err)
  26. }
  27. c := KuiperConf{}
  28. err = LoadConfig(&c)
  29. if err != nil {
  30. t.Error(err)
  31. }
  32. if c.Basic.ConsoleLog != true {
  33. t.Errorf("env variable should set it to true")
  34. }
  35. }
  36. func TestJsonCamelCase(t *testing.T) {
  37. key := "HTTPPULL__DEFAULT__BODYTYPE"
  38. value := "event"
  39. err := os.Setenv(key, value)
  40. if err != nil {
  41. t.Error(err)
  42. }
  43. const ConfigName = "sources/httppull.yaml"
  44. c := make(map[string]interface{})
  45. err = LoadConfigByName(ConfigName, &c)
  46. if err != nil {
  47. t.Error(err)
  48. }
  49. if casted, success := c["default"].(map[string]interface{}); success {
  50. if casted["bodyType"] != "event" {
  51. t.Errorf("env variable should set it to event")
  52. }
  53. } else {
  54. t.Errorf("returned value does not contains map under 'Basic' key")
  55. }
  56. }
  57. func TestNestedFields(t *testing.T) {
  58. key := "EDGEX__DEFAULT__OPTIONAL__PASSWORD"
  59. value := "password"
  60. err := os.Setenv(key, value)
  61. if err != nil {
  62. t.Error(err)
  63. }
  64. const ConfigName = "sources/edgex.yaml"
  65. c := make(map[string]interface{})
  66. err = LoadConfigByName(ConfigName, &c)
  67. if err != nil {
  68. t.Error(err)
  69. }
  70. if casted, success := c["default"].(map[string]interface{}); success {
  71. if optional, ok := casted["optional"].(map[string]interface{}); ok {
  72. if optional["Password"] != "password" {
  73. t.Errorf("Password variable should set it to password")
  74. }
  75. } else {
  76. t.Errorf("returned value does not contains map under 'optional' key")
  77. }
  78. } else {
  79. t.Errorf("returned value does not contains map under 'Basic' key")
  80. }
  81. }
  82. func TestKeysReplacement(t *testing.T) {
  83. input := createRandomConfigMap()
  84. expected := createExpectedRandomConfigMap()
  85. list := []string{"interval", "Seed", "deduplicate", "pattern"}
  86. applyKeys(input, list)
  87. if !reflect.DeepEqual(input, expected) {
  88. t.Errorf("key names within list should be applied \nexpected - %s\n input - %s", expected, input)
  89. }
  90. }
  91. func TestKeyReplacement(t *testing.T) {
  92. m := createRandomConfigMap()
  93. expected := createExpectedRandomConfigMap()
  94. applyKey(m, "Seed")
  95. applyKey(m, "interval")
  96. if !reflect.DeepEqual(m, expected) {
  97. t.Errorf("key names within list should be applied \nexpected - %s\nmap - %s", expected, m)
  98. }
  99. }
  100. func createRandomConfigMap() map[string]interface{} {
  101. pattern := make(map[string]interface{})
  102. pattern["count"] = 50
  103. defaultM := make(map[string]interface{})
  104. defaultM["interval"] = 1000
  105. defaultM["seed"] = 1
  106. defaultM["pattern"] = pattern
  107. defaultM["deduplicate"] = 0
  108. ext := make(map[string]interface{})
  109. ext["interval"] = 100
  110. dedup := make(map[string]interface{})
  111. dedup["interval"] = 100
  112. dedup["deduplicated"] = 50
  113. input := make(map[string]interface{})
  114. input["default"] = defaultM
  115. input["ext"] = ext
  116. input["dedup"] = dedup
  117. return input
  118. }
  119. func createExpectedRandomConfigMap() map[string]interface{} {
  120. input := createRandomConfigMap()
  121. def := input["default"]
  122. if defMap, ok := def.(map[string]interface{}); ok {
  123. tmp := defMap["seed"]
  124. delete(defMap, "seed")
  125. defMap["Seed"] = tmp
  126. }
  127. return input
  128. }