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