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.
21 "github.com/stretchr/testify/assert"
28 func TestParseConfigurationSuccess(t *testing.T) {
29 config, err := ParseConfiguration()
31 t.Errorf("failed to parse configuration: %s", err)
33 assert.Equal(t, 4800, config.Http.Port)
34 assert.Equal(t, 4801, config.Rmr.Port)
35 assert.Equal(t, 65536, config.Rmr.MaxMsgSize)
36 assert.Equal(t, "info", config.Logging.LogLevel)
38 assert.Equal(t, 3, config.Rnib.MaxRnibConnectionAttempts)
39 assert.Equal(t, 10, config.Rnib.RnibRetryIntervalMs)
41 assert.Equal(t, true, config.ResourceStatusParams.EnableResourceStatus)
42 assert.Equal(t, true, config.ResourceStatusParams.PrbPeriodic)
43 assert.Equal(t, true, config.ResourceStatusParams.TnlLoadIndPeriodic)
44 assert.Equal(t, true, config.ResourceStatusParams.HwLoadIndPeriodic)
45 assert.Equal(t, true, config.ResourceStatusParams.AbsStatusPeriodic)
46 assert.Equal(t, true, config.ResourceStatusParams.RsrpMeasurementPeriodic)
47 assert.Equal(t, true, config.ResourceStatusParams.CsiPeriodic)
49 /*assert.Equal(t, 1, config.ResourceStatusParams.PeriodicityMs)
50 assert.Equal(t, 120, config.ResourceStatusParams.PeriodicityRsrpMeasurementMs)
51 assert.Equal(t, 5, config.ResourceStatusParams.PeriodicityCsiMs)*/
55 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
56 configPath := "../resources/configuration.yaml"
57 configPathTmp := "../resources/configuration.yaml_tmp"
58 err := os.Rename(configPath, configPathTmp)
60 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
63 err = os.Rename(configPathTmp, configPath)
65 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
69 _, cErr := ParseConfiguration()
73 func TestRmrConfigNotFoundFailure(t *testing.T) {
74 yamlMap := map[string]interface{}{
75 "logging": map[string]interface{}{"logLevel": "info"},
76 "http": map[string]interface{}{"port": 631},
77 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
78 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
80 cleanUp := prepareTempConfigForTest(t, yamlMap)
83 _, cErr := ParseConfiguration()
84 assert.EqualError(t, cErr, "#configuration.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n")
87 func TestLoggingConfigNotFoundFailure(t *testing.T) {
88 yamlMap := map[string]interface{}{
89 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
90 "http": map[string]interface{}{"port": 631},
91 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
92 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
94 cleanUp := prepareTempConfigForTest(t, yamlMap)
97 _, cErr := ParseConfiguration()
98 assert.EqualError(t, cErr, "#configuration.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n")
101 func TestHttpConfigNotFoundFailure(t *testing.T) {
102 yamlMap := map[string]interface{}{
103 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
104 "logging": map[string]interface{}{"logLevel": "info"},
105 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
106 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
108 cleanUp := prepareTempConfigForTest(t, yamlMap)
111 _, cErr := ParseConfiguration()
112 assert.EqualError(t, cErr, "#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n")
115 func TestRnibConfigNotFoundFailure(t *testing.T) {
116 yamlMap := map[string]interface{}{
117 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
118 "logging": map[string]interface{}{"logLevel": "info"},
119 "http": map[string]interface{}{"port": 631},
120 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityCsiMs": 5},
122 cleanUp := prepareTempConfigForTest(t, yamlMap)
125 _, cErr := ParseConfiguration()
126 assert.EqualError(t, cErr, "#configuration.fillRnibConfig - failed to fill RNib configuration: The entry 'rnib' not found\n")
129 func TestResourceStatusParamsConfigNotFoundFailure(t *testing.T) {
131 yamlMap := map[string]interface{}{
132 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
133 "logging": map[string]interface{}{"logLevel": "info"},
134 "http": map[string]interface{}{"port": 631},
135 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
137 cleanUp := prepareTempConfigForTest(t, yamlMap)
140 _, cErr := ParseConfiguration()
141 assert.EqualError(t, cErr, "#configuration.fillResourceStatusParamsConfig - failed to fill resourceStatusParams configuration: The entry 'resourceStatusParams' not found\n")
144 func TestCharacteristicsConfigInvalidPeriodicityMs(t *testing.T) {
146 yamlMap := map[string]interface{}{
147 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
148 "logging": map[string]interface{}{"logLevel": "info"},
149 "http": map[string]interface{}{"port": 631},
150 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
151 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityMs": 50, "periodicityRsrpMeasurementMs": 480, "periodicityCsiMs": 20},
153 cleanUp := prepareTempConfigForTest(t, yamlMap)
156 _, cErr := ParseConfiguration()
157 assert.Error(t, cErr)
160 func TestResourceStatusParamsConfigInvalidPeriodicityRsrpMeasurementMs(t *testing.T) {
162 yamlMap := map[string]interface{}{
163 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
164 "logging": map[string]interface{}{"logLevel": "info"},
165 "http": map[string]interface{}{"port": 631},
166 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
167 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityMs": 1000, "periodicityRsrpMeasurementMs": 50, "periodicityCsiMs": 20},
169 cleanUp := prepareTempConfigForTest(t, yamlMap)
172 _, cErr := ParseConfiguration()
173 assert.Error(t, cErr)
176 func TestResourceStatusParamsConfigInvalidPeriodicityCsiMs(t *testing.T) {
178 yamlMap := map[string]interface{}{
179 "rmr": map[string]interface{}{"port": 6942, "maxMsgSize": 4096},
180 "logging": map[string]interface{}{"logLevel": "info"},
181 "http": map[string]interface{}{"port": 631},
182 "rnib": map[string]interface{}{"maxRnibConnectionAttempts": 3, "rnibRetryIntervalMs": 10},
183 "resourceStatusParams": map[string]interface{}{"enableResourceStatus": true, "periodicityMs": 1000, "periodicityRsrpMeasurementMs": 480, "periodicityCsiMs": 50},
185 cleanUp := prepareTempConfigForTest(t, yamlMap)
188 _, cErr := ParseConfiguration()
189 assert.Error(t, cErr)
192 func TestConfigurationString(t *testing.T) {
193 config, err := ParseConfiguration()
195 t.Errorf("failed to parse configuration. error: %s", err)
197 str := config.String()
198 assert.NotEmpty(t, str)
199 assert.Contains(t, str, "logging")
200 assert.Contains(t, str, "http")
201 assert.Contains(t, str, "rmr")
202 assert.Contains(t, str, "rnib")
203 assert.Contains(t, str, "resourceStatusParams")
206 func prepareTempConfigForTest(t *testing.T, yamlMap map[string]interface{}) func() {
207 configPath := "../resources/configuration.yaml"
208 configPathTmp := "../resources/configuration.yaml_tmp"
209 err := os.Rename(configPath, configPathTmp)
211 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
213 buf, err := yaml.Marshal(yamlMap)
215 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to marshal configuration map\n")
217 err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
219 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
223 err = os.Rename(configPathTmp, configPath)
225 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)