load_test.go 4.0 KB

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