[RICPLT-213] Print configurations to log on startup
[ric-plt/e2mgr.git] / E2Manager / 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 := ParseConfiguration()
33         assert.Equal(t, 3800, config.Http.Port)
34         assert.Equal(t, 3801, config.Rmr.Port)
35         assert.Equal(t, 65536, config.Rmr.MaxMsgSize)
36         assert.Equal(t, "info", config.Logging.LogLevel)
37         assert.Equal(t, 100, config.NotificationResponseBuffer)
38         assert.Equal(t, 5, config.BigRedButtonTimeoutSec)
39         assert.Equal(t, 4500, config.KeepAliveResponseTimeoutMs)
40         assert.Equal(t, 1500, config.KeepAliveDelayMs)
41         assert.Equal(t, 15000, config.E2TInstanceDeletionTimeoutMs)
42         assert.NotNil(t, config.GlobalRicId)
43         assert.NotEmpty(t, config.GlobalRicId.PlmnId)
44         assert.NotEmpty(t, config.GlobalRicId.RicNearRtId)
45 }
46
47 func TestStringer(t *testing.T) {
48         config := ParseConfiguration().String()
49         assert.NotEmpty(t, config)
50 }
51
52 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
53         configPath := "../resources/configuration.yaml"
54         configPathTmp := "../resources/configuration.yaml_tmp"
55         err := os.Rename(configPath, configPathTmp)
56         if err != nil {
57                 t.Errorf("#TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
58         }
59         defer func() {
60                 err = os.Rename(configPathTmp, configPath)
61                 if err != nil {
62                         t.Errorf("#TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
63                 }
64         }()
65         assert.Panics(t, func() { ParseConfiguration() })
66 }
67
68 func TestRmrConfigNotFoundFailure(t *testing.T) {
69         configPath := "../resources/configuration.yaml"
70         configPathTmp := "../resources/configuration.yaml_tmp"
71         err := os.Rename(configPath, configPathTmp)
72         if err != nil {
73                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
74         }
75         defer func() {
76                 err = os.Rename(configPathTmp, configPath)
77                 if err != nil {
78                         t.Errorf("#TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
79                 }
80         }()
81         yamlMap := map[string]interface{}{
82                 "logging": map[string]interface{}{"logLevel": "info"},
83                 "http":    map[string]interface{}{"port": 3800},
84                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
85                 "globalRicId":    map[string]interface{}{"plmnId": "131014", "ricNearRtId": "556670"},
86         }
87         buf, err := yaml.Marshal(yamlMap)
88         if err != nil {
89                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
90         }
91         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
92         if err != nil {
93                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
94         }
95         assert.PanicsWithValue(t, "#configuration.populateRmrConfig - failed to populate RMR configuration: The entry 'rmr' not found\n", func() { ParseConfiguration() })
96 }
97
98 func TestLoggingConfigNotFoundFailure(t *testing.T) {
99         configPath := "../resources/configuration.yaml"
100         configPathTmp := "../resources/configuration.yaml_tmp"
101         err := os.Rename(configPath, configPathTmp)
102         if err != nil {
103                 t.Errorf("#TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
104         }
105         defer func() {
106                 err = os.Rename(configPathTmp, configPath)
107                 if err != nil {
108                         t.Errorf("#TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
109                 }
110         }()
111         yamlMap := map[string]interface{}{
112                 "rmr":  map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
113                 "http": map[string]interface{}{"port": 3800},
114                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
115                 "globalRicId":    map[string]interface{}{"plmnId": "131014", "ricNearRtId": "556670"},
116         }
117         buf, err := yaml.Marshal(yamlMap)
118         if err != nil {
119                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
120         }
121         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
122         if err != nil {
123                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
124         }
125         assert.PanicsWithValue(t, "#configuration.populateLoggingConfig - failed to populate logging configuration: The entry 'logging' not found\n",
126                 func() { ParseConfiguration() })
127 }
128
129 func TestHttpConfigNotFoundFailure(t *testing.T) {
130         configPath := "../resources/configuration.yaml"
131         configPathTmp := "../resources/configuration.yaml_tmp"
132         err := os.Rename(configPath, configPathTmp)
133         if err != nil {
134                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
135         }
136         defer func() {
137                 err = os.Rename(configPathTmp, configPath)
138                 if err != nil {
139                         t.Errorf("#TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
140                 }
141         }()
142         yamlMap := map[string]interface{}{
143                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
144                 "logging": map[string]interface{}{"logLevel": "info"},
145                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
146                 "globalRicId":    map[string]interface{}{"plmnId": "131014", "ricNearRtId": "556670"},
147         }
148         buf, err := yaml.Marshal(yamlMap)
149         if err != nil {
150                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to marshal configuration map\n")
151         }
152         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
153         if err != nil {
154                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
155         }
156         assert.PanicsWithValue(t, "#configuration.populateHttpConfig - failed to populate HTTP configuration: The entry 'http' not found\n",
157                 func() { ParseConfiguration() })
158 }
159
160 func TestRoutingManagerConfigNotFoundFailure(t *testing.T) {
161         configPath := "../resources/configuration.yaml"
162         configPathTmp := "../resources/configuration.yaml_tmp"
163         err := os.Rename(configPath, configPathTmp)
164         if err != nil {
165                 t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
166         }
167         defer func() {
168                 err = os.Rename(configPathTmp, configPath)
169                 if err != nil {
170                         t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
171                 }
172         }()
173         yamlMap := map[string]interface{}{
174                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
175                 "logging": map[string]interface{}{"logLevel": "info"},
176                 "http": map[string]interface{}{"port": 3800},
177                 "globalRicId":    map[string]interface{}{"plmnId": "131014", "ricNearRtId": "556670"},
178         }
179         buf, err := yaml.Marshal(yamlMap)
180         if err != nil {
181                 t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to marshal configuration map\n")
182         }
183         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
184         if err != nil {
185                 t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
186         }
187         assert.PanicsWithValue(t, "#configuration.populateRoutingManagerConfig - failed to populate Routing Manager configuration: The entry 'routingManager' not found\n",
188                 func() { ParseConfiguration() })
189 }
190
191 func TestGlobalRicIdConfigNotFoundFailure(t *testing.T) {
192         configPath := "../resources/configuration.yaml"
193         configPathTmp := "../resources/configuration.yaml_tmp"
194         err := os.Rename(configPath, configPathTmp)
195         if err != nil {
196                 t.Errorf("#TestGlobalRicIdConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
197         }
198         defer func() {
199                 err = os.Rename(configPathTmp, configPath)
200                 if err != nil {
201                         t.Errorf("#TestGlobalRicIdConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
202                 }
203         }()
204         yamlMap := map[string]interface{}{
205                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
206                 "logging": map[string]interface{}{"logLevel": "info"},
207                 "http": map[string]interface{}{"port": 3800},
208                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
209         }
210         buf, err := yaml.Marshal(yamlMap)
211         if err != nil {
212                 t.Errorf("#TestGlobalRicIdConfigNotFoundFailure - failed to marshal configuration map\n")
213         }
214         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
215         if err != nil {
216                 t.Errorf("#TestGlobalRicIdConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
217         }
218         assert.PanicsWithValue(t, "#configuration.populateGlobalRicIdConfig - failed to populate Global RicId configuration: The entry 'globalRicId' not found\n",
219                 func() { ParseConfiguration() })
220 }