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