c59403e635e94c1162b201e788a88eb93836de0b
[ric-plt/e2mgr.git] / E2Manager / main / http_server_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 main
19
20 import (
21         "e2mgr/configuration"
22         "github.com/stretchr/testify/assert"
23         "gopkg.in/yaml.v2"
24         "io/ioutil"
25         "os"
26         "testing"
27 )
28
29 func TestRunServer(t *testing.T) {
30
31 }
32
33 func TestParseConfigurationSuccess(t *testing.T) {
34         config := configuration.ParseConfiguration()
35         assert.Equal(t, 3800, config.Http.Port)
36         assert.Equal(t, 3801, config.Rmr.Port)
37         assert.Equal(t, 4096, config.Rmr.MaxMsgSize)
38         assert.Equal(t, "info", config.Logging.LogLevel)
39         assert.Equal(t, 100, config.NotificationResponseBuffer)
40         assert.Equal(t,5, config.BigRedButtonTimeoutSec)
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("#http_server_test.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("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
54                 }
55         }()
56         assert.Panics(t, func (){configuration.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("#http_server_test.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("#http_server_test.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         }
76         buf, err := yaml.Marshal(yamlMap)
77         if err != nil{
78                 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
79         }
80         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
81         if err != nil{
82                 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
83         }
84         assert.PanicsWithValue(t, "#http_server.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n", func (){configuration.ParseConfiguration()})
85 }
86
87 func TestLoggingConfigNotFoundFailure(t *testing.T) {
88         configPath := "../resources/configuration.yaml"
89         configPathTmp := "../resources/configuration.yaml_tmp"
90         err := os.Rename(configPath, configPathTmp)
91         if err != nil{
92                 t.Errorf("#http_server_test.TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
93         }
94         defer func() {
95                 err = os.Rename(configPathTmp, configPath)
96                 if err != nil{
97                         t.Errorf("#http_server_test.TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
98                 }
99         }()
100         yamlMap := map[string]interface{}{
101                 "rmr":map[string]interface{}{"port":3801, "maxMsgSize":4096},
102                 "http":map[string]interface{}{"port":3800},
103         }
104         buf, err := yaml.Marshal(yamlMap)
105         if err != nil{
106                 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
107         }
108         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
109         if err != nil{
110                 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
111         }
112         assert.PanicsWithValue(t, "#http_server.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n",
113                 func (){configuration.ParseConfiguration()})
114 }
115
116 func TestHttpConfigNotFoundFailure(t *testing.T) {
117         configPath := "../resources/configuration.yaml"
118         configPathTmp := "../resources/configuration.yaml_tmp"
119         err := os.Rename(configPath, configPathTmp)
120         if err != nil{
121                 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
122         }
123         defer func() {
124                 err = os.Rename(configPathTmp, configPath)
125                 if err != nil{
126                         t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
127                 }
128         }()
129         yamlMap := map[string]interface{}{
130                 "rmr":map[string]interface{}{"port":3801, "maxMsgSize":4096},
131                 "logging":map[string]interface{}{"logLevel":"info"},
132         }
133         buf, err := yaml.Marshal(yamlMap)
134         if err != nil{
135                 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to marshal configuration map\n")
136         }
137         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
138         if err != nil{
139                 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
140         }
141         assert.PanicsWithValue(t, "#http_server.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n",
142                 func (){configuration.ParseConfiguration()})
143 }