370e79e344606ad7c60c7e80fe12ba9bff65fdcb
[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 }
37
38 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
39         configPath := "../resources/configuration.yaml"
40         configPathTmp := "../resources/configuration.yaml_tmp"
41         err := os.Rename(configPath, configPathTmp)
42         if err != nil {
43                 t.Errorf("#TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
44         }
45         defer func() {
46                 err = os.Rename(configPathTmp, configPath)
47                 if err != nil {
48                         t.Errorf("#TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
49                 }
50         }()
51         assert.Panics(t, func() { ParseConfiguration() })
52 }
53
54 func TestRmrConfigNotFoundFailure(t *testing.T) {
55         configPath := "../resources/configuration.yaml"
56         configPathTmp := "../resources/configuration.yaml_tmp"
57         err := os.Rename(configPath, configPathTmp)
58         if err != nil {
59                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
60         }
61         defer func() {
62                 err = os.Rename(configPathTmp, configPath)
63                 if err != nil {
64                         t.Errorf("#TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
65                 }
66         }()
67         yamlMap := map[string]interface{}{
68                 "logging": map[string]interface{}{"logLevel": "info"},
69                 "http":    map[string]interface{}{"port": 3800},
70         }
71         buf, err := yaml.Marshal(yamlMap)
72         if err != nil {
73                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
74         }
75         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
76         if err != nil {
77                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
78         }
79         assert.PanicsWithValue(t, "#configuration.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n", func() { ParseConfiguration() })
80 }
81
82 func TestLoggingConfigNotFoundFailure(t *testing.T) {
83         configPath := "../resources/configuration.yaml"
84         configPathTmp := "../resources/configuration.yaml_tmp"
85         err := os.Rename(configPath, configPathTmp)
86         if err != nil {
87                 t.Errorf("#TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
88         }
89         defer func() {
90                 err = os.Rename(configPathTmp, configPath)
91                 if err != nil {
92                         t.Errorf("#TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
93                 }
94         }()
95         yamlMap := map[string]interface{}{
96                 "rmr":  map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
97                 "http": map[string]interface{}{"port": 3800},
98         }
99         buf, err := yaml.Marshal(yamlMap)
100         if err != nil {
101                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
102         }
103         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
104         if err != nil {
105                 t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
106         }
107         assert.PanicsWithValue(t, "#configuration.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n",
108                 func() { ParseConfiguration() })
109 }
110
111 func TestHttpConfigNotFoundFailure(t *testing.T) {
112         configPath := "../resources/configuration.yaml"
113         configPathTmp := "../resources/configuration.yaml_tmp"
114         err := os.Rename(configPath, configPathTmp)
115         if err != nil {
116                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
117         }
118         defer func() {
119                 err = os.Rename(configPathTmp, configPath)
120                 if err != nil {
121                         t.Errorf("#TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
122                 }
123         }()
124         yamlMap := map[string]interface{}{
125                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
126                 "logging": map[string]interface{}{"logLevel": "info"},
127         }
128         buf, err := yaml.Marshal(yamlMap)
129         if err != nil {
130                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to marshal configuration map\n")
131         }
132         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
133         if err != nil {
134                 t.Errorf("#TestHttpConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
135         }
136         assert.PanicsWithValue(t, "#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n",
137                 func() { ParseConfiguration() })
138 }