2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
23 "github.com/gorilla/mux"
24 "github.com/stretchr/testify/assert"
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)
40 nodebControllerMock := &mocks.NodebControllerMock{}
41 nodebControllerMock.On("GetNodebIdList").Return(nil)
42 nodebControllerMock.On("GetNodeb").Return(nil)
43 nodebControllerMock.On("HandleHealthCheckRequest").Return(nil)
45 router := mux.NewRouter()
46 initializeRoutes(router, nodebControllerMock, controllerMock)
47 return router, controllerMock, nodebControllerMock
50 func TestRoutePostEndcSetup(t *testing.T) {
51 router, controllerMock, _ := setupRouterAndMocks()
53 req, err := http.NewRequest("POST", "/v1/nodeb/endc-setup", nil)
57 rr := httptest.NewRecorder()
58 router.ServeHTTP(rr, req)
60 controllerMock.AssertNumberOfCalls(t,"EndcSetupHandler", 1)
63 func TestRoutePostX2Setup(t *testing.T) {
64 router, controllerMock, _ := setupRouterAndMocks()
66 req, err := http.NewRequest("POST", "/v1/nodeb/x2-setup", nil)
70 rr := httptest.NewRecorder()
71 router.ServeHTTP(rr, req)
73 controllerMock.AssertNumberOfCalls(t,"X2SetupHandler", 1)
76 func TestRouteGetNodebIds(t *testing.T) {
77 router, _, nodebControllerMock := setupRouterAndMocks()
79 req, err := http.NewRequest("GET", "/v1/nodeb/ids", nil)
83 rr := httptest.NewRecorder()
84 router.ServeHTTP(rr, req)
86 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
89 func TestRouteGetNodebRanName(t *testing.T) {
90 router, _, nodebControllerMock := setupRouterAndMocks()
92 req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil)
96 rr := httptest.NewRecorder()
97 router.ServeHTTP(rr, req)
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)
104 func TestRouteGetHealth(t *testing.T) {
105 router, _, nodebControllerMock := setupRouterAndMocks()
107 req, err := http.NewRequest("GET", "/v1/health", nil)
111 rr := httptest.NewRecorder()
112 router.ServeHTTP(rr, req)
114 nodebControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
117 func TestRoutePutNodebShutdown(t *testing.T) {
118 router, controllerMock, _ := setupRouterAndMocks()
120 req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil)
124 rr := httptest.NewRecorder()
125 router.ServeHTTP(rr, req)
127 controllerMock.AssertNumberOfCalls(t, "ShutdownHandler", 1)
130 func TestRoutePutNodebResetRanName(t *testing.T) {
131 router, controllerMock, _ := setupRouterAndMocks()
133 req, err := http.NewRequest("PUT", "/v1/nodeb/ran1/reset", nil)
137 rr := httptest.NewRecorder()
138 router.ServeHTTP(rr, req)
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)
145 func TestRouteNotFound(t *testing.T) {
146 router, _, _ := setupRouterAndMocks()
148 req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
152 rr := httptest.NewRecorder()
153 router.ServeHTTP(rr, req)
155 assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
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)
168 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
169 configPath := "../resources/configuration.yaml"
170 configPathTmp := "../resources/configuration.yaml_tmp"
171 err := os.Rename(configPath, configPathTmp)
173 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
176 err = os.Rename(configPathTmp, configPath)
178 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
181 assert.Panics(t, func() { configuration.ParseConfiguration() })
184 func TestRmrConfigNotFoundFailure(t *testing.T) {
185 configPath := "../resources/configuration.yaml"
186 configPathTmp := "../resources/configuration.yaml_tmp"
187 err := os.Rename(configPath, configPathTmp)
189 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
192 err = os.Rename(configPathTmp, configPath)
194 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
197 yamlMap := map[string]interface{}{
198 "logging": map[string]interface{}{"logLevel": "info"},
199 "http": map[string]interface{}{"port": 3800},
201 buf, err := yaml.Marshal(yamlMap)
203 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
205 err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
207 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
209 assert.PanicsWithValue(t, "#http_server.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n", func() { configuration.ParseConfiguration() })
212 func TestLoggingConfigNotFoundFailure(t *testing.T) {
213 configPath := "../resources/configuration.yaml"
214 configPathTmp := "../resources/configuration.yaml_tmp"
215 err := os.Rename(configPath, configPathTmp)
217 t.Errorf("#http_server_test.TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
220 err = os.Rename(configPathTmp, configPath)
222 t.Errorf("#http_server_test.TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
225 yamlMap := map[string]interface{}{
226 "rmr": map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
227 "http": map[string]interface{}{"port": 3800},
229 buf, err := yaml.Marshal(yamlMap)
231 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
233 err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
235 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
237 assert.PanicsWithValue(t, "#http_server.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n",
238 func() { configuration.ParseConfiguration() })
241 func TestHttpConfigNotFoundFailure(t *testing.T) {
242 configPath := "../resources/configuration.yaml"
243 configPathTmp := "../resources/configuration.yaml_tmp"
244 err := os.Rename(configPath, configPathTmp)
246 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
249 err = os.Rename(configPathTmp, configPath)
251 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
254 yamlMap := map[string]interface{}{
255 "rmr": map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
256 "logging": map[string]interface{}{"logLevel": "info"},
258 buf, err := yaml.Marshal(yamlMap)
260 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to marshal configuration map\n")
262 err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
264 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
266 assert.PanicsWithValue(t, "#http_server.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n",
267 func() { configuration.ParseConfiguration() })