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