aaac4db4d157ecf1a88cd5986dcbf828a27d5c75
[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         "e2mgr/mocks"
23         "github.com/gorilla/mux"
24         "github.com/stretchr/testify/assert"
25         "gopkg.in/yaml.v2"
26         "io/ioutil"
27         "net/http"
28         "net/http/httptest"
29         "os"
30         "testing"
31 )
32
33 func setupRouterAndMocks() (*mux.Router, *mocks.ControllerMock, *mocks.NodebControllerMock) {
34         controllerMock := &mocks.ControllerMock{}
35         controllerMock.On("ShutdownHandler").Return(nil)
36         controllerMock.On("X2ResetHandler").Return(nil)
37
38         nodebControllerMock := &mocks.NodebControllerMock{}
39         nodebControllerMock.On("HandleRequest").Return(nil)
40         nodebControllerMock.On("GetNodebIdList").Return(nil)
41         nodebControllerMock.On("GetNodeb").Return(nil)
42         nodebControllerMock.On("HandleHealthCheckRequest").Return(nil)
43
44         router := mux.NewRouter();
45         initializeRoutes(router, nodebControllerMock, controllerMock)
46         return router, controllerMock, nodebControllerMock
47 }
48
49 func TestRoutePostNodebMessageType(t *testing.T) {
50         router, _, nodebControllerMock := setupRouterAndMocks()
51
52         req, err := http.NewRequest("POST", "/v1/nodeb/messageType", nil)
53         if err != nil {
54                 t.Fatal(err)
55         }
56         rr := httptest.NewRecorder()
57         router.ServeHTTP(rr, req)
58
59         nodebControllerMock.AssertNumberOfCalls(t, "HandleRequest", 1)
60 }
61
62 func TestRouteGetNodebIds(t *testing.T) {
63         router, _, nodebControllerMock := setupRouterAndMocks()
64
65         req, err := http.NewRequest("GET", "/v1/nodeb/ids", nil)
66         if err != nil {
67                 t.Fatal(err)
68         }
69         rr := httptest.NewRecorder()
70         router.ServeHTTP(rr, req)
71
72         nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
73 }
74
75 func TestRouteGetNodebRanName(t *testing.T) {
76         router, _, nodebControllerMock := setupRouterAndMocks()
77
78         req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil)
79         if err != nil {
80                 t.Fatal(err)
81         }
82         rr := httptest.NewRecorder()
83         router.ServeHTTP(rr, req)
84
85         assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code")
86         assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body")
87         nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1)
88 }
89
90 func TestRouteGetHealth(t *testing.T) {
91         router, _, nodebControllerMock := setupRouterAndMocks()
92
93         req, err := http.NewRequest("GET", "/v1/health", nil)
94         if err != nil {
95                 t.Fatal(err)
96         }
97         rr := httptest.NewRecorder()
98         router.ServeHTTP(rr, req)
99
100         nodebControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
101 }
102
103 func TestRoutePutNodebShutdown(t *testing.T) {
104         router, controllerMock, _ := setupRouterAndMocks()
105
106         req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil)
107         if err != nil {
108                 t.Fatal(err)
109         }
110         rr := httptest.NewRecorder()
111         router.ServeHTTP(rr, req)
112
113         controllerMock.AssertNumberOfCalls(t, "ShutdownHandler", 1)
114 }
115
116 func TestRoutePutNodebResetRanName(t *testing.T) {
117         router, controllerMock, _ := setupRouterAndMocks()
118
119         req, err := http.NewRequest("PUT", "/v1/nodeb/ran1/reset", nil)
120         if err != nil {
121                 t.Fatal(err)
122         }
123         rr := httptest.NewRecorder()
124         router.ServeHTTP(rr, req)
125
126         assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code")
127         assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body")
128         controllerMock.AssertNumberOfCalls(t, "X2ResetHandler", 1)
129 }
130
131 func TestRouteNotFound(t *testing.T) {
132         router, _, _ := setupRouterAndMocks()
133
134         req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
135         if err != nil {
136                 t.Fatal(err)
137         }
138         rr := httptest.NewRecorder()
139         router.ServeHTTP(rr, req)
140
141         assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
142 }
143
144 func TestParseConfigurationSuccess(t *testing.T) {
145         config := configuration.ParseConfiguration()
146         assert.Equal(t, 3800, config.Http.Port)
147         assert.Equal(t, 3801, config.Rmr.Port)
148         assert.Equal(t, 4096, config.Rmr.MaxMsgSize)
149         assert.Equal(t, "info", config.Logging.LogLevel)
150         assert.Equal(t, 100, config.NotificationResponseBuffer)
151         assert.Equal(t, 5, config.BigRedButtonTimeoutSec)
152 }
153
154 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
155         configPath := "../resources/configuration.yaml"
156         configPathTmp := "../resources/configuration.yaml_tmp"
157         err := os.Rename(configPath, configPathTmp)
158         if err != nil {
159                 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
160         }
161         defer func() {
162                 err = os.Rename(configPathTmp, configPath)
163                 if err != nil {
164                         t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
165                 }
166         }()
167         assert.Panics(t, func() { configuration.ParseConfiguration() })
168 }
169
170 func TestRmrConfigNotFoundFailure(t *testing.T) {
171         configPath := "../resources/configuration.yaml"
172         configPathTmp := "../resources/configuration.yaml_tmp"
173         err := os.Rename(configPath, configPathTmp)
174         if err != nil {
175                 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
176         }
177         defer func() {
178                 err = os.Rename(configPathTmp, configPath)
179                 if err != nil {
180                         t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
181                 }
182         }()
183         yamlMap := map[string]interface{}{
184                 "logging": map[string]interface{}{"logLevel": "info"},
185                 "http":    map[string]interface{}{"port": 3800},
186         }
187         buf, err := yaml.Marshal(yamlMap)
188         if err != nil {
189                 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
190         }
191         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
192         if err != nil {
193                 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
194         }
195         assert.PanicsWithValue(t, "#http_server.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n", func() { configuration.ParseConfiguration() })
196 }
197
198 func TestLoggingConfigNotFoundFailure(t *testing.T) {
199         configPath := "../resources/configuration.yaml"
200         configPathTmp := "../resources/configuration.yaml_tmp"
201         err := os.Rename(configPath, configPathTmp)
202         if err != nil {
203                 t.Errorf("#http_server_test.TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
204         }
205         defer func() {
206                 err = os.Rename(configPathTmp, configPath)
207                 if err != nil {
208                         t.Errorf("#http_server_test.TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
209                 }
210         }()
211         yamlMap := map[string]interface{}{
212                 "rmr":  map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
213                 "http": map[string]interface{}{"port": 3800},
214         }
215         buf, err := yaml.Marshal(yamlMap)
216         if err != nil {
217                 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
218         }
219         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
220         if err != nil {
221                 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
222         }
223         assert.PanicsWithValue(t, "#http_server.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n",
224                 func() { configuration.ParseConfiguration() })
225 }
226
227 func TestHttpConfigNotFoundFailure(t *testing.T) {
228         configPath := "../resources/configuration.yaml"
229         configPathTmp := "../resources/configuration.yaml_tmp"
230         err := os.Rename(configPath, configPathTmp)
231         if err != nil {
232                 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
233         }
234         defer func() {
235                 err = os.Rename(configPathTmp, configPath)
236                 if err != nil {
237                         t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
238                 }
239         }()
240         yamlMap := map[string]interface{}{
241                 "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
242                 "logging": map[string]interface{}{"logLevel": "info"},
243         }
244         buf, err := yaml.Marshal(yamlMap)
245         if err != nil {
246                 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to marshal configuration map\n")
247         }
248         err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
249         if err != nil {
250                 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
251         }
252         assert.PanicsWithValue(t, "#http_server.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n",
253                 func() { configuration.ParseConfiguration() })
254 }