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 "e2mgr/e2managererrors"
27 "e2mgr/providers/httpmsghandlerprovider"
33 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
34 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
35 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
36 "github.com/gorilla/mux"
37 "github.com/stretchr/testify/assert"
38 "github.com/stretchr/testify/mock"
47 func TestShutdownHandlerRnibError(t *testing.T) {
49 config := configuration.ParseConfiguration()
51 rmrMessengerMock := &mocks.RmrMessengerMock{}
52 readerMock := &mocks.RnibReaderMock{}
53 readerProvider := func() reader.RNibReader {
56 writerMock := &mocks.RnibWriterMock{}
57 writerProvider := func() rNibWriter.RNibWriter {
61 rnibErr := &common.RNibError{}
62 var nbIdentityList []*entities.NbIdentity
63 readerMock.On("GetListNodebIds").Return(nbIdentityList, rnibErr)
65 writer := httptest.NewRecorder()
66 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
67 controller.ShutdownHandler(writer, tests.GetHttpRequest())
69 var errorResponse = parseJsonRequest(t, writer.Body)
71 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
72 assert.Equal(t, errorResponse.Code, e2managererrors.NewRnibDbError().Err.Code)
75 func TestHeaderValidationFailed(t *testing.T) {
77 config := configuration.ParseConfiguration()
79 rmrMessengerMock := &mocks.RmrMessengerMock{}
80 readerMock := &mocks.RnibReaderMock{}
81 readerProvider := func() reader.RNibReader {
84 writerMock := &mocks.RnibWriterMock{}
85 writerProvider := func() rNibWriter.RNibWriter {
89 writer := httptest.NewRecorder()
91 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
93 header := &http.Header{}
95 controller.handleRequest(writer, header, httpmsghandlerprovider.ShutdownRequest, nil, true, http.StatusNoContent)
97 var errorResponse = parseJsonRequest(t, writer.Body)
98 err := e2managererrors.NewHeaderValidationError()
100 assert.Equal(t, http.StatusUnsupportedMediaType, writer.Result().StatusCode)
101 assert.Equal(t, errorResponse.Code, err.Err.Code)
102 assert.Equal(t, errorResponse.Message, err.Err.Message)
105 func TestShutdownStatusNoContent(t *testing.T) {
108 rmrMessengerMock := &mocks.RmrMessengerMock{}
109 readerMock := &mocks.RnibReaderMock{}
110 readerProvider := func() reader.RNibReader {
113 writerMock := &mocks.RnibWriterMock{}
114 writerProvider := func() rNibWriter.RNibWriter {
117 config := configuration.ParseConfiguration()
119 var rnibError common.IRNibError
120 nbIdentityList := []*entities.NbIdentity{}
121 readerMock.On("GetListNodebIds").Return(nbIdentityList, rnibError)
123 writer := httptest.NewRecorder()
124 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
125 controller.ShutdownHandler(writer, tests.GetHttpRequest())
127 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
130 func TestHandleInternalError(t *testing.T) {
132 config := configuration.ParseConfiguration()
134 rmrMessengerMock := &mocks.RmrMessengerMock{}
135 readerMock := &mocks.RnibReaderMock{}
136 readerProvider := func() reader.RNibReader {
139 writerMock := &mocks.RnibWriterMock{}
140 writerProvider := func() rNibWriter.RNibWriter {
144 writer := httptest.NewRecorder()
145 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
146 err := e2managererrors.NewInternalError()
148 controller.handleErrorResponse(err, writer)
149 var errorResponse = parseJsonRequest(t, writer.Body)
151 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
152 assert.Equal(t, errorResponse.Code, err.Err.Code)
153 assert.Equal(t, errorResponse.Message, err.Err.Message)
156 func TestHandleCommandAlreadyInProgressError(t *testing.T) {
158 config := configuration.ParseConfiguration()
160 rmrMessengerMock := &mocks.RmrMessengerMock{}
161 readerMock := &mocks.RnibReaderMock{}
162 readerProvider := func() reader.RNibReader {
165 writerMock := &mocks.RnibWriterMock{}
166 writerProvider := func() rNibWriter.RNibWriter {
169 writer := httptest.NewRecorder()
170 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
171 err := e2managererrors.NewCommandAlreadyInProgressError()
173 controller.handleErrorResponse(err, writer)
174 var errorResponse = parseJsonRequest(t, writer.Body)
176 assert.Equal(t, http.StatusMethodNotAllowed, writer.Result().StatusCode)
177 assert.Equal(t, errorResponse.Code, err.Err.Code)
178 assert.Equal(t, errorResponse.Message, err.Err.Message)
181 func TestValidateHeaders(t *testing.T) {
184 rmrMessengerMock := &mocks.RmrMessengerMock{}
185 readerMock := &mocks.RnibReaderMock{}
186 readerProvider := func() reader.RNibReader {
189 writerMock := &mocks.RnibWriterMock{}
190 writerProvider := func() rNibWriter.RNibWriter {
193 config := configuration.ParseConfiguration()
195 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
197 header := http.Header{}
198 header.Set("Content-Type", "application/json")
199 result := controller.validateRequestHeader(&header)
201 assert.Nil(t, result)
204 func parseJsonRequest(t *testing.T, r io.Reader) models.ErrorResponse {
206 var errorResponse models.ErrorResponse
207 body, err := ioutil.ReadAll(r)
209 t.Errorf("Error cannot deserialize json request")
211 json.Unmarshal(body, &errorResponse)
216 func initLog(t *testing.T) *logger.Logger {
217 log, err := logger.InitLogger(logger.InfoLevel)
219 t.Errorf("#delete_all_request_handler_test.TestHandleSuccessFlow - failed to initialize logger, error: %s", err)
224 func TestX2ResetHandleSuccessfulRequestedCause(t *testing.T) {
229 readerMock := &mocks.RnibReaderMock{}
230 readerProvider := func() reader.RNibReader {
233 writerMock := &mocks.RnibWriterMock{}
234 writerProvider := func() rNibWriter.RNibWriter {
237 payload := []byte{0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x40}
238 xaction := []byte(ranName)
239 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xaction)
240 rmrMessengerMock := &mocks.RmrMessengerMock{}
241 rmrMessengerMock.On("SendMsg", msg, mock.Anything).Return(msg, nil)
243 config := configuration.ParseConfiguration()
244 rmrService := getRmrService(rmrMessengerMock, log)
246 writer := httptest.NewRecorder()
247 controller := NewController(log, rmrService, readerProvider, writerProvider, config)
249 var nodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED}
250 readerMock.On("GetNodeb", ranName).Return(nodeb, nil)
252 data4Req := map[string]interface{}{"cause": "protocol:transfer-syntax-error"}
253 b := new(bytes.Buffer)
254 _ = json.NewEncoder(b).Encode(data4Req)
255 req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
256 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
258 controller.X2ResetHandler(writer, req)
259 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
263 func TestX2ResetHandleSuccessfulRequestedDefault(t *testing.T) {
268 readerMock := &mocks.RnibReaderMock{}
269 readerProvider := func() reader.RNibReader {
272 writerMock := &mocks.RnibWriterMock{}
273 writerProvider := func() rNibWriter.RNibWriter {
277 payload := []byte{0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x64}
278 xaction := []byte(ranName)
279 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xaction)
280 rmrMessengerMock := &mocks.RmrMessengerMock{}
281 rmrMessengerMock.On("SendMsg", msg, mock.Anything).Return(msg, nil)
283 config := configuration.ParseConfiguration()
284 rmrService := getRmrService(rmrMessengerMock, log)
286 writer := httptest.NewRecorder()
287 controller := NewController(log, rmrService, readerProvider, writerProvider, config)
289 var nodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED}
290 readerMock.On("GetNodeb", ranName).Return(nodeb, nil)
293 b := new(bytes.Buffer)
294 req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
295 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
297 controller.X2ResetHandler(writer, req)
298 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
302 func TestX2ResetHandleFailureInvalidBody(t *testing.T) {
307 readerMock := &mocks.RnibReaderMock{}
308 readerProvider := func() reader.RNibReader {
311 writerMock := &mocks.RnibWriterMock{}
312 writerProvider := func() rNibWriter.RNibWriter {
315 rmrMessengerMock := &mocks.RmrMessengerMock{}
317 config := configuration.ParseConfiguration()
318 rmrService := getRmrService(rmrMessengerMock, log)
320 writer := httptest.NewRecorder()
321 controller := NewController(log, rmrService, readerProvider, writerProvider, config)
323 // Invalid json: attribute name without quotes (should be "cause":).
324 b := strings.NewReader("{cause:\"protocol:transfer-syntax-error\"")
325 req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
326 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
328 controller.X2ResetHandler(writer, req)
329 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
331 _, ok := rmrService.E2sessions[ranName]
336 func TestHandleErrorResponse(t *testing.T) {
339 readerMock := &mocks.RnibReaderMock{}
340 readerProvider := func() reader.RNibReader {
343 writerMock := &mocks.RnibWriterMock{}
344 writerProvider := func() rNibWriter.RNibWriter {
347 rmrMessengerMock := &mocks.RmrMessengerMock{}
349 config := configuration.ParseConfiguration()
350 rmrService := getRmrService(rmrMessengerMock, log)
352 controller := NewController(log, rmrService, readerProvider, writerProvider, config)
354 writer := httptest.NewRecorder()
355 controller.handleErrorResponse(e2managererrors.NewRnibDbError(), writer)
356 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
358 writer = httptest.NewRecorder()
359 controller.handleErrorResponse(e2managererrors.NewCommandAlreadyInProgressError(), writer)
360 assert.Equal(t, http.StatusMethodNotAllowed, writer.Result().StatusCode)
362 writer = httptest.NewRecorder()
363 controller.handleErrorResponse(e2managererrors.NewHeaderValidationError(), writer)
364 assert.Equal(t, http.StatusUnsupportedMediaType, writer.Result().StatusCode)
366 writer = httptest.NewRecorder()
367 controller.handleErrorResponse(e2managererrors.NewWrongStateError("", ""), writer)
368 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
370 writer = httptest.NewRecorder()
371 controller.handleErrorResponse(e2managererrors.NewRequestValidationError(), writer)
372 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
374 writer = httptest.NewRecorder()
375 controller.handleErrorResponse(e2managererrors.NewRmrError(), writer)
376 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
378 writer = httptest.NewRecorder()
379 controller.handleErrorResponse(e2managererrors.NewResourceNotFoundError(), writer)
380 assert.Equal(t, http.StatusNotFound, writer.Result().StatusCode)
382 writer = httptest.NewRecorder()
383 controller.handleErrorResponse(fmt.Errorf("ErrorError"), writer)
384 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)