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)
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)
44 router := mux.NewRouter();
45 initializeRoutes(router, nodebControllerMock, controllerMock)
46 return router, controllerMock, nodebControllerMock
49 func TestRoutePostNodebMessageType(t *testing.T) {
50 router, _, nodebControllerMock := setupRouterAndMocks()
52 req, err := http.NewRequest("POST", "/v1/nodeb/messageType", nil)
56 rr := httptest.NewRecorder()
57 router.ServeHTTP(rr, req)
59 nodebControllerMock.AssertNumberOfCalls(t, "HandleRequest", 1)
62 func TestRouteGetNodebIds(t *testing.T) {
63 router, _, nodebControllerMock := setupRouterAndMocks()
65 req, err := http.NewRequest("GET", "/v1/nodeb/ids", nil)
69 rr := httptest.NewRecorder()
70 router.ServeHTTP(rr, req)
72 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
75 func TestRouteGetNodebRanName(t *testing.T) {
76 router, _, nodebControllerMock := setupRouterAndMocks()
78 req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil)
82 rr := httptest.NewRecorder()
83 router.ServeHTTP(rr, req)
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)
90 func TestRouteGetHealth(t *testing.T) {
91 router, _, nodebControllerMock := setupRouterAndMocks()
93 req, err := http.NewRequest("GET", "/v1/health", nil)
97 rr := httptest.NewRecorder()
98 router.ServeHTTP(rr, req)
100 nodebControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
103 func TestRoutePutNodebShutdown(t *testing.T) {
104 router, controllerMock, _ := setupRouterAndMocks()
106 req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil)
110 rr := httptest.NewRecorder()
111 router.ServeHTTP(rr, req)
113 controllerMock.AssertNumberOfCalls(t, "ShutdownHandler", 1)
116 func TestRoutePutNodebResetRanName(t *testing.T) {
117 router, controllerMock, _ := setupRouterAndMocks()
119 req, err := http.NewRequest("PUT", "/v1/nodeb/ran1/reset", nil)
123 rr := httptest.NewRecorder()
124 router.ServeHTTP(rr, req)
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)
131 func TestRouteNotFound(t *testing.T) {
132 router, _, _ := setupRouterAndMocks()
134 req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
138 rr := httptest.NewRecorder()
139 router.ServeHTTP(rr, req)
141 assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
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)
154 func TestParseConfigurationFileNotFoundFailure(t *testing.T) {
155 configPath := "../resources/configuration.yaml"
156 configPathTmp := "../resources/configuration.yaml_tmp"
157 err := os.Rename(configPath, configPathTmp)
159 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
162 err = os.Rename(configPathTmp, configPath)
164 t.Errorf("#http_server_test.TestParseConfigurationFileNotFoundFailure - failed to rename configuration file: %s\n", configPath)
167 assert.Panics(t, func() { configuration.ParseConfiguration() })
170 func TestRmrConfigNotFoundFailure(t *testing.T) {
171 configPath := "../resources/configuration.yaml"
172 configPathTmp := "../resources/configuration.yaml_tmp"
173 err := os.Rename(configPath, configPathTmp)
175 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
178 err = os.Rename(configPathTmp, configPath)
180 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
183 yamlMap := map[string]interface{}{
184 "logging": map[string]interface{}{"logLevel": "info"},
185 "http": map[string]interface{}{"port": 3800},
187 buf, err := yaml.Marshal(yamlMap)
189 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
191 err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
193 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
195 assert.PanicsWithValue(t, "#http_server.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n", func() { configuration.ParseConfiguration() })
198 func TestLoggingConfigNotFoundFailure(t *testing.T) {
199 configPath := "../resources/configuration.yaml"
200 configPathTmp := "../resources/configuration.yaml_tmp"
201 err := os.Rename(configPath, configPathTmp)
203 t.Errorf("#http_server_test.TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
206 err = os.Rename(configPathTmp, configPath)
208 t.Errorf("#http_server_test.TestLoggingConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
211 yamlMap := map[string]interface{}{
212 "rmr": map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
213 "http": map[string]interface{}{"port": 3800},
215 buf, err := yaml.Marshal(yamlMap)
217 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to marshal configuration map\n")
219 err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
221 t.Errorf("#http_server_test.TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
223 assert.PanicsWithValue(t, "#http_server.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n",
224 func() { configuration.ParseConfiguration() })
227 func TestHttpConfigNotFoundFailure(t *testing.T) {
228 configPath := "../resources/configuration.yaml"
229 configPathTmp := "../resources/configuration.yaml_tmp"
230 err := os.Rename(configPath, configPathTmp)
232 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
235 err = os.Rename(configPathTmp, configPath)
237 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
240 yamlMap := map[string]interface{}{
241 "rmr": map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
242 "logging": map[string]interface{}{"logLevel": "info"},
244 buf, err := yaml.Marshal(yamlMap)
246 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to marshal configuration map\n")
248 err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
250 t.Errorf("#http_server_test.TestHttpConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
252 assert.PanicsWithValue(t, "#http_server.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n",
253 func() { configuration.ParseConfiguration() })