[RICPLT-2727] Update RoutingMangaerClient UTs + others....
[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
18 package configuration
19
20 import (
21         "github.com/stretchr/testify/assert"
22         "gopkg.in/yaml.v2"
23         "io/ioutil"
24         "os"
25         "testing"
26 )
27
28 func TestParseConfigurationSuccess(t *testing.T) {
29         config := ParseConfiguration()
30         assert.Equal(t, 3800, config.Http.Port)
31         assert.Equal(t, 3801, config.Rmr.Port)
32         assert.Equal(t, 65536, config.Rmr.MaxMsgSize)
33         assert.Equal(t, "info", config.Logging.LogLevel)
34         assert.Equal(t, 100, config.NotificationResponseBuffer)
35         assert.Equal(t, 5, config.BigRedButtonTimeoutSec)
36         assert.Equal(t, 1500, config.KeepAliveResponseTimeoutMs)
37         assert.Equal(t, 500, config.KeepAliveDelayMs)
38 }
39
40 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
41         configPath := "../resources/configuration.yaml"
42         configPathTmp := "../resources/configuration.yaml_tmp"
43         err := os.Rename(configPath, configPathTmp)
44         if err != nil {
45                 t.Errorf("#TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
46         }
47         defer func() {
48                 err = os.Rename(configPathTmp, configPath)
49                 if err != nil {
50                         t.Errorf("#TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
51                 }
52         }()
53         assert.Panics(t, func() { ParseConfiguration() })
54 }
55
56 func TestRmrConfigNotFoundFailure(t *testing.T) {
57         configPath := "../resources/configuration.yaml"
58         configPathTmp := "../resources/configuration.yaml_tmp"
59         err := os.Rename(configPath, configPathTmp)
60         if err != nil {
61                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
62         }
63         defer func() {
64                 err = os.Rename(configPathTmp, configPath)
65                 if err != nil {
66                         t.Errorf("#TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
67                 }
68         }()
69         yamlMap := map[string]interface{}{
70                 "logging": map[string]interface{}{"logLevel": "info"},
71                 "http":    map[string]interface{}{"port": 3800},
72                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
73         }
74         buf, err := yaml.Marshal(yamlMap)
75         if err != nil {
76                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
77         }
78         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
79         if err != nil {
80                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
81         }
82         assert.PanicsWithValue(t, "#configuration.populateRmrConfig - failed to populate RMR configuration: The entry 'rmr' not found\n", func() { ParseConfiguration() })
83 }
84
85 func TestLoggingConfigNotFoundFailure(t *testing.T) {
86         configPath := "../resources/configuration.yaml"
87         configPathTmp := "../resources/configuration.yaml_tmp"
88         err := os.Rename(configPath, configPathTmp)
89         if err != nil {
90                 t.Errorf("#TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
91         }
92         defer func() {
93                 err = os.Rename(configPathTmp, configPath)
94                 if err != nil {
95                         t.Errorf("#TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
96                 }
97         }()
98         yamlMap := map[string]interface{}{
99                 "rmr":  map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
100                 "http": map[string]interface{}{"port": 3800},
101                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
102         }
103         buf, err := yaml.Marshal(yamlMap)
104         if err != nil {
105                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
106         }
107         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
108         if err != nil {
109                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
110         }
111         assert.PanicsWithValue(t, "#configuration.populateLoggingConfig - failed to populate logging configuration: The entry 'logging' not found\n",
112                 func() { ParseConfiguration() })
113 }
114
115 func TestHttpConfigNotFoundFailure(t *testing.T) {
116         configPath := "../resources/configuration.yaml"
117         configPathTmp := "../resources/configuration.yaml_tmp"
118         err := os.Rename(configPath, configPathTmp)
119         if err != nil {
120                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
121         }
122         defer func() {
123                 err = os.Rename(configPathTmp, configPath)
124                 if err != nil {
125                         t.Errorf("#TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
126                 }
127         }()
128         yamlMap := map[string]interface{}{
129                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
130                 "logging": map[string]interface{}{"logLevel": "info"},
131                 "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
132         }
133         buf, err := yaml.Marshal(yamlMap)
134         if err != nil {
135                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to marshal configuration map\n")
136         }
137         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
138         if err != nil {
139                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
140         }
141         assert.PanicsWithValue(t, "#configuration.populateHttpConfig - failed to populate HTTP configuration: The entry 'http' not found\n",
142                 func() { ParseConfiguration() })
143 }
144
145 func TestRoutingManagerConfigNotFoundFailure(t *testing.T) {
146         configPath := "../resources/configuration.yaml"
147         configPathTmp := "../resources/configuration.yaml_tmp"
148         err := os.Rename(configPath, configPathTmp)
149         if err != nil {
150                 t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
151         }
152         defer func() {
153                 err = os.Rename(configPathTmp, configPath)
154                 if err != nil {
155                         t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
156                 }
157         }()
158         yamlMap := map[string]interface{}{
159                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
160                 "logging": map[string]interface{}{"logLevel": "info"},
161                 "http": map[string]interface{}{"port": 3800},
162         }
163         buf, err := yaml.Marshal(yamlMap)
164         if err != nil {
165                 t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to marshal configuration map\n")
166         }
167         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
168         if err != nil {
169                 t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
170         }
171         assert.PanicsWithValue(t, "#configuration.populateRoutingManagerConfig - failed to populate Routing Manager configuration: The entry 'routingManager' not found\n",
172                 func() { ParseConfiguration() })
173 }