Merge "upgrade RMR version to 1.13.0" into PI3
[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         }
73         buf, err := yaml.Marshal(yamlMap)
74         if err != nil {
75                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
76         }
77         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
78         if err != nil {
79                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
80         }
81         assert.PanicsWithValue(t, "#configuration.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n", func() { ParseConfiguration() })
82 }
83
84 func TestLoggingConfigNotFoundFailure(t *testing.T) {
85         configPath := "../resources/configuration.yaml"
86         configPathTmp := "../resources/configuration.yaml_tmp"
87         err := os.Rename(configPath, configPathTmp)
88         if err != nil {
89                 t.Errorf("#TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
90         }
91         defer func() {
92                 err = os.Rename(configPathTmp, configPath)
93                 if err != nil {
94                         t.Errorf("#TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
95                 }
96         }()
97         yamlMap := map[string]interface{}{
98                 "rmr":  map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
99                 "http": map[string]interface{}{"port": 3800},
100         }
101         buf, err := yaml.Marshal(yamlMap)
102         if err != nil {
103                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
104         }
105         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
106         if err != nil {
107                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
108         }
109         assert.PanicsWithValue(t, "#configuration.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n",
110                 func() { ParseConfiguration() })
111 }
112
113 func TestHttpConfigNotFoundFailure(t *testing.T) {
114         configPath := "../resources/configuration.yaml"
115         configPathTmp := "../resources/configuration.yaml_tmp"
116         err := os.Rename(configPath, configPathTmp)
117         if err != nil {
118                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
119         }
120         defer func() {
121                 err = os.Rename(configPathTmp, configPath)
122                 if err != nil {
123                         t.Errorf("#TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
124                 }
125         }()
126         yamlMap := map[string]interface{}{
127                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
128                 "logging": map[string]interface{}{"logLevel": "info"},
129         }
130         buf, err := yaml.Marshal(yamlMap)
131         if err != nil {
132                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to marshal configuration map\n")
133         }
134         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
135         if err != nil {
136                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
137         }
138         assert.PanicsWithValue(t, "#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n",
139                 func() { ParseConfiguration() })
140 }