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)
44 assert.Equal(t, true, config.ResourceStatusParams.EnableResourceStatus)
45 assert.Equal(t, true, config.ResourceStatusParams.PrbPeriodic)
46 assert.Equal(t, true, config.ResourceStatusParams.TnlLoadIndPeriodic)
47 assert.Equal(t, true, config.ResourceStatusParams.HwLoadIndPeriodic)
48 assert.Equal(t, true, config.ResourceStatusParams.AbsStatusPeriodic)
49 assert.Equal(t, true, config.ResourceStatusParams.RsrpMeasurementPeriodic)
50 assert.Equal(t, true, config.ResourceStatusParams.CsiPeriodic)
52 /*assert.Equal(t, 1, config.ResourceStatusParams.PeriodicityMs)
53 assert.Equal(t, 120, config.ResourceStatusParams.PeriodicityRsrpMeasurementMs)
54 assert.Equal(t, 5, config.ResourceStatusParams.PeriodicityCsiMs)*/
58 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
59 configPath := "../resources/configuration.yaml"
60 configPathTmp := "../resources/configuration.yaml_tmp"
61 err := os.Rename(configPath, configPathTmp)
63 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
66 err = os.Rename(configPathTmp, configPath)
68 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
72 _, cErr := ParseConfiguration()
76 func TestRmrConfigNotFoundFailure(t *testing.T) {
77 yamlMap := map[string]interface{}{
78 "logging": map[string]interface{}{"logLevel": "info"},
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.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n")
90 func TestLoggingConfigNotFoundFailure(t *testing.T) {
91 yamlMap := map[string]interface{}{
92 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
93 "http": map[string]interface{}{"port": 631},
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.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n")
104 func TestHttpConfigNotFoundFailure(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 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
109 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
111 cleanUp := prepareTempConfigForTest(t, yamlMap)
114 _, cErr := ParseConfiguration()
115 assert.EqualError(t, cErr, "#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n")
118 func TestRnibConfigNotFoundFailure(t *testing.T) {
119 yamlMap := map[string]interface{}{
120 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
121 "logging": map[string]interface{}{"logLevel": "info"},
122 "http": map[string]interface{}{"port": 631},
123 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
125 cleanUp := prepareTempConfigForTest(t, yamlMap)
128 _, cErr := ParseConfiguration()
129 assert.EqualError(t, cErr, "#configuration.fillRnibConfig - failed to fill RNib configuration: The entry 'rnib' not found\n")
132 func TestResourceStatusParamsConfigNotFoundFailure(t *testing.T) {
134 yamlMap := map[string]interface{}{
135 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
136 "logging": map[string]interface{}{"logLevel": "info"},
137 "http": map[string]interface{}{"port": 631},
138 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
140 cleanUp := prepareTempConfigForTest(t, yamlMap)
143 _, cErr := ParseConfiguration()
144 assert.EqualError(t, cErr, "#configuration.fillResourceStatusParamsConfig - failed to fill resourceStatusParams configuration: The entry 'resourceStatusParams' not found\n")
147 func TestCharacteristicsConfigInvalidPeriodicityMs(t *testing.T) {
149 yamlMap := map[string]interface{}{
150 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
151 "logging": map[string]interface{}{"logLevel": "info"},
152 "http": map[string]interface{}{"port": 631},
153 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
154 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityMs": 50, "periodicityRsrpMeasurementMs": 480, "periodicityCsiMs": 20},
156 cleanUp := prepareTempConfigForTest(t, yamlMap)
159 _, cErr := ParseConfiguration()
160 assert.Error(t, cErr)
163 func TestResourceStatusParamsConfigInvalidPeriodicityRsrpMeasurementMs(t *testing.T) {
165 yamlMap := map[string]interface{}{
166 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
167 "logging": map[string]interface{}{"logLevel": "info"},
168 "http": map[string]interface{}{"port": 631},
169 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
170 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityMs": 1000, "periodicityRsrpMeasurementMs": 50, "periodicityCsiMs": 20},
172 cleanUp := prepareTempConfigForTest(t, yamlMap)
175 _, cErr := ParseConfiguration()
176 assert.Error(t, cErr)
179 func TestResourceStatusParamsConfigInvalidPeriodicityCsiMs(t *testing.T) {
181 yamlMap := map[string]interface{}{
182 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
183 "logging": map[string]interface{}{"logLevel": "info"},
184 "http": map[string]interface{}{"port": 631},
185 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
186 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityMs": 1000, "periodicityRsrpMeasurementMs": 480, "periodicityCsiMs": 50},
188 cleanUp := prepareTempConfigForTest(t, yamlMap)
191 _, cErr := ParseConfiguration()
192 assert.Error(t, cErr)
195 func TestConfigurationString(t *testing.T) {
196 config, err := ParseConfiguration()
198 t.Errorf("failed to parse configuration. error: %s", err)
200 str := config.String()
201 assert.NotEmpty(t, str)
202 assert.Contains(t, str, "logging")
203 assert.Contains(t, str, "http")
204 assert.Contains(t, str, "rmr")
205 assert.Contains(t, str, "rnib")
206 assert.Contains(t, str, "resourceStatusParams")
209 func prepareTempConfigForTest(t *testing.T, yamlMap map[string]interface{}) func() {
210 configPath := "../resources/configuration.yaml"
211 configPathTmp := "../resources/configuration.yaml_tmp"
212 err := os.Rename(configPath, configPathTmp)
214 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
216 buf, err := yaml.Marshal(yamlMap)
218 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to marshal configuration map\n")
220 err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
222 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
226 err = os.Rename(configPathTmp, configPath)
228 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)