Adding new comments for Oran in all files with licenses
[ric-plt/resource-status-manager.git] / RSM / configuration / configuration_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
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
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16
17 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20
21 package configuration
22
23 import (
24         "github.com/stretchr/testify/assert"
25         "gopkg.in/yaml.v2"
26         "io/ioutil"
27         "os"
28         "testing"
29 )
30
31 func TestParseConfigurationSuccess(t *testing.T) {
32         config, err := ParseConfiguration()
33         if err != nil {
34                 t.Errorf("failed to parse configuration: %s", err)
35         }
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)
40
41         assert.Equal(t, 3, config.Rnib.MaxRnibConnectionAttempts)
42         assert.Equal(t, 10, config.Rnib.RnibRetryIntervalMs)
43
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)
51
52         /*assert.Equal(t, 1, config.ResourceStatusParams.PeriodicityMs)
53         assert.Equal(t, 120, config.ResourceStatusParams.PeriodicityRsrpMeasurementMs)
54         assert.Equal(t, 5, config.ResourceStatusParams.PeriodicityCsiMs)*/
55
56 }
57
58 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
59         configPath := "../resources/configuration.yaml"
60         configPathTmp := "../resources/configuration.yaml_tmp"
61         err := os.Rename(configPath, configPathTmp)
62         if err != nil {
63                 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
64         }
65         defer func() {
66                 err = os.Rename(configPathTmp, configPath)
67                 if err != nil {
68                         t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
69                 }
70         }()
71
72         _, cErr := ParseConfiguration()
73         assert.Error(t, cErr)
74 }
75
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},
82         }
83         cleanUp := prepareTempConfigForTest(t, yamlMap)
84         defer cleanUp()
85
86         _, cErr := ParseConfiguration()
87         assert.EqualError(t, cErr, "#configuration.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n")
88 }
89
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},
96         }
97         cleanUp := prepareTempConfigForTest(t, yamlMap)
98         defer cleanUp()
99
100         _, cErr := ParseConfiguration()
101         assert.EqualError(t, cErr, "#configuration.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n")
102 }
103
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},
110         }
111         cleanUp := prepareTempConfigForTest(t, yamlMap)
112         defer cleanUp()
113
114         _, cErr := ParseConfiguration()
115         assert.EqualError(t, cErr, "#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n")
116 }
117
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},
124         }
125         cleanUp := prepareTempConfigForTest(t, yamlMap)
126         defer cleanUp()
127
128         _, cErr := ParseConfiguration()
129         assert.EqualError(t, cErr, "#configuration.fillRnibConfig - failed to fill RNib configuration: The entry 'rnib' not found\n")
130 }
131
132 func TestResourceStatusParamsConfigNotFoundFailure(t *testing.T) {
133
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},
139         }
140         cleanUp := prepareTempConfigForTest(t, yamlMap)
141         defer cleanUp()
142
143         _, cErr := ParseConfiguration()
144         assert.EqualError(t, cErr, "#configuration.fillResourceStatusParamsConfig - failed to fill resourceStatusParams configuration: The entry 'resourceStatusParams' not found\n")
145 }
146
147 func TestCharacteristicsConfigInvalidPeriodicityMs(t *testing.T) {
148
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},
155         }
156         cleanUp := prepareTempConfigForTest(t, yamlMap)
157         defer cleanUp()
158
159         _, cErr := ParseConfiguration()
160         assert.Error(t, cErr)
161 }
162
163 func TestResourceStatusParamsConfigInvalidPeriodicityRsrpMeasurementMs(t *testing.T) {
164
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},
171         }
172         cleanUp := prepareTempConfigForTest(t, yamlMap)
173         defer cleanUp()
174
175         _, cErr := ParseConfiguration()
176         assert.Error(t, cErr)
177 }
178
179 func TestResourceStatusParamsConfigInvalidPeriodicityCsiMs(t *testing.T) {
180
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},
187         }
188         cleanUp := prepareTempConfigForTest(t, yamlMap)
189         defer cleanUp()
190
191         _, cErr := ParseConfiguration()
192         assert.Error(t, cErr)
193 }
194
195 func TestConfigurationString(t *testing.T) {
196         config, err := ParseConfiguration()
197         if err != nil {
198                 t.Errorf("failed to parse configuration. error: %s", err)
199         }
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")
207 }
208
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)
213         if err != nil {
214                 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
215         }
216         buf, err := yaml.Marshal(yamlMap)
217         if err != nil {
218                 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to marshal configuration map\n")
219         }
220         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
221         if err != nil {
222                 t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
223         }
224
225         return func() {
226                 err = os.Rename(configPathTmp, configPath)
227                 if err != nil {
228                         t.Errorf("#http_server_test.TestRnibConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
229                 }
230         }
231 }