2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
24 "github.com/stretchr/testify/assert"
31 func TestParseConfigurationSuccess(t *testing.T) {
32 config, err := ParseConfiguration()
34 t.Errorf("failed to parse configuration: %s", err)
36 assert.Equal(t, 4800, config.Http.Port)
37 assert.Equal(t, 4801, config.Rmr.Port)
38 assert.Equal(t, 65536, config.Rmr.MaxMsgSize)
39 assert.Equal(t, "info", config.Logging.LogLevel)
41 assert.Equal(t, 3, config.Rnib.MaxRnibConnectionAttempts)
42 assert.Equal(t, 10, config.Rnib.RnibRetryIntervalMs)
45 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
46 configPath := "../resources/configuration.yaml"
47 configPathTmp := "../resources/configuration.yaml_tmp"
48 err := os.Rename(configPath, configPathTmp)
50 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
53 err = os.Rename(configPathTmp, configPath)
55 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
59 _, cErr := ParseConfiguration()
63 func TestRmrConfigNotFoundFailure(t *testing.T) {
64 yamlMap := map[string]interface{}{
65 "logging": map[string]interface{}{"logLevel": "info"},
66 "http": map[string]interface{}{"port": 631},
67 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
69 cleanUp := prepareTempConfigForTest(t, yamlMap)
72 _, cErr := ParseConfiguration()
73 assert.EqualError(t, cErr, "#configuration.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n")
76 func TestLoggingConfigNotFoundFailure(t *testing.T) {
77 yamlMap := map[string]interface{}{
78 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
79 "http": map[string]interface{}{"port": 631},
80 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
81 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
83 cleanUp := prepareTempConfigForTest(t, yamlMap)
86 _, cErr := ParseConfiguration()
87 assert.EqualError(t, cErr, "#configuration.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n")
90 func TestHttpConfigNotFoundFailure(t *testing.T) {
91 yamlMap := map[string]interface{}{
92 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
93 "logging": map[string]interface{}{"logLevel": "info"},
94 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
95 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
97 cleanUp := prepareTempConfigForTest(t, yamlMap)
100 _, cErr := ParseConfiguration()
101 assert.EqualError(t, cErr, "#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n")
104 func TestRnibConfigNotFoundFailure(t *testing.T) {
105 yamlMap := map[string]interface{}{
106 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
107 "logging": map[string]interface{}{"logLevel": "info"},
108 "http": map[string]interface{}{"port": 631},
110 cleanUp := prepareTempConfigForTest(t, yamlMap)
113 _, cErr := ParseConfiguration()
114 assert.EqualError(t, cErr, "#configuration.fillRnibConfig - failed to fill RNib configuration: The entry 'rnib' not found\n")
117 func TestConfigurationString(t *testing.T) {
118 config, err := ParseConfiguration()
120 t.Errorf("failed to parse configuration. error: %s", err)
122 str := config.String()
123 assert.NotEmpty(t, str)
124 assert.Contains(t, str, "logging")
125 assert.Contains(t, str, "http")
126 assert.Contains(t, str, "rmr")
127 assert.Contains(t, str, "rnib")
130 func prepareTempConfigForTest(t *testing.T, yamlMap map[string]interface{}) func() {
131 configPath := "../resources/configuration.yaml"
132 configPathTmp := "../resources/configuration.yaml_tmp"
133 err := os.Rename(configPath, configPathTmp)
135 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
137 buf, err := yaml.Marshal(yamlMap)
139 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to marshal configuration map\n")
141 err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
143 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
147 err = os.Rename(configPathTmp, configPath)
149 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)