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