blob: f14d8309912a9f81900e04e0579f2dbff92dde31 [file] [log] [blame]
ss412g07ef76d2019-08-12 17:26:40 +03001//
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
18package controllers
19
20import (
rh362j574c21e2019-08-19 15:25:50 +030021 "bytes"
ss412g07ef76d2019-08-12 17:26:40 +030022 "e2mgr/configuration"
23 "e2mgr/e2managererrors"
irinab7c68cc2019-09-11 14:29:45 +030024 "e2mgr/e2pdus"
ss412g07ef76d2019-08-12 17:26:40 +030025 "e2mgr/logger"
irina33f84e12019-09-10 12:40:37 +030026 "e2mgr/managers"
ss412g07ef76d2019-08-12 17:26:40 +030027 "e2mgr/mocks"
28 "e2mgr/models"
is005q19e72a52019-08-26 17:56:18 +030029 "e2mgr/providers/httpmsghandlerprovider"
ss412g07ef76d2019-08-12 17:26:40 +030030 "e2mgr/rNibWriter"
rh362j574c21e2019-08-19 15:25:50 +030031 "e2mgr/rmrCgo"
ss412g07ef76d2019-08-12 17:26:40 +030032 "e2mgr/tests"
33 "encoding/json"
rh362j574c21e2019-08-19 15:25:50 +030034 "fmt"
ss412g07ef76d2019-08-12 17:26:40 +030035 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
36 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
37 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
Amichai4db397d2019-08-28 19:23:15 +030038 "github.com/gorilla/mux"
ss412g07ef76d2019-08-12 17:26:40 +030039 "github.com/stretchr/testify/assert"
rh362j574c21e2019-08-19 15:25:50 +030040 "github.com/stretchr/testify/mock"
ss412g07ef76d2019-08-12 17:26:40 +030041 "io"
42 "io/ioutil"
43 "net/http"
44 "net/http/httptest"
rh362j574c21e2019-08-19 15:25:50 +030045 "strings"
ss412g07ef76d2019-08-12 17:26:40 +030046 "testing"
47)
48
irinab7c68cc2019-09-11 14:29:45 +030049func TestX2SetupInvalidBody(t *testing.T) {
irina33f84e12019-09-10 12:40:37 +030050
irinab7c68cc2019-09-11 14:29:45 +030051 readerMock, writerMock, rmrMessengerMock, ranSetupManager := initTest(t)
52 log := initLog(t)
53
irina33f84e12019-09-10 12:40:37 +030054 readerProvider := func() reader.RNibReader {
55 return readerMock
56 }
irina33f84e12019-09-10 12:40:37 +030057 writerProvider := func() rNibWriter.RNibWriter {
58 return writerMock
59 }
irina33f84e12019-09-10 12:40:37 +030060
61 header := http.Header{}
62 header.Set("Content-Type", "application/json")
irinab7c68cc2019-09-11 14:29:45 +030063 httpRequest, _ := http.NewRequest("POST", "http://localhost:3800/v1/nodeb/x2-setup", strings.NewReader("{}{}"))
64 httpRequest.Header = header
irina33f84e12019-09-10 12:40:37 +030065
66 writer := httptest.NewRecorder()
irinab7c68cc2019-09-11 14:29:45 +030067 config := configuration.ParseConfiguration()
irina33f84e12019-09-10 12:40:37 +030068 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config, ranSetupManager)
irina33f84e12019-09-10 12:40:37 +030069 controller.X2SetupHandler(writer, httpRequest)
70
irinab7c68cc2019-09-11 14:29:45 +030071 var errorResponse = parseJsonRequest(t, writer.Body)
72
73 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
74 assert.Equal(t, e2managererrors.NewInvalidJsonError().Code, errorResponse.Code)
75}
76
77func TestX2SetupSuccess(t *testing.T) {
78
79 readerMock, writerMock, rmrMessengerMock, ranSetupManager := initTest(t)
80 log := initLog(t)
81
82 readerProvider := func() reader.RNibReader {
83 return readerMock
84 }
85 writerProvider := func() rNibWriter.RNibWriter {
86 return writerMock
87 }
88
89 ranName := "test"
90 nb := &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
91 readerMock.On("GetNodeb", ranName).Return(nb, nil)
92
93 var nbUpdated = &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, ConnectionAttempts: 1}
94 writerMock.On("UpdateNodebInfo", nbUpdated).Return(nil)
95
96 payload := e2pdus.PackedX2setupRequest
97 xaction := []byte(ranName)
98 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xaction)
99
100 rmrMessengerMock.On("SendMsg", mock.Anything, mock.Anything).Return(msg, nil)
101
102 header := http.Header{}
103 header.Set("Content-Type", "application/json")
104 httpRequest := tests.GetHttpRequest()
105 httpRequest.Header = header
106
107 writer := httptest.NewRecorder()
108 config := configuration.ParseConfiguration()
109 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config, ranSetupManager)
110 controller.X2SetupHandler(writer, httpRequest)
111
112 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
113}
114
115func TestEndcSetupSuccess(t *testing.T) {
116
117 readerMock, writerMock, rmrMessengerMock, ranSetupManager := initTest(t)
118 log := initLog(t)
119
120 readerProvider := func() reader.RNibReader {
121 return readerMock
122 }
123 writerProvider := func() rNibWriter.RNibWriter {
124 return writerMock
125 }
126
127 ranName := "test"
128 nb := &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
129 readerMock.On("GetNodeb", ranName).Return(nb, nil)
130
131 var nbUpdated = &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST, ConnectionAttempts: 1}
132 writerMock.On("UpdateNodebInfo", nbUpdated).Return(nil)
133
134 payload := e2pdus.PackedEndcX2setupRequest
135 xaction := []byte(ranName)
136 msg := rmrCgo.NewMBuf(rmrCgo.RIC_ENDC_X2_SETUP_REQ, len(payload), ranName, &payload, &xaction)
137
138 rmrMessengerMock.On("SendMsg", mock.Anything, mock.Anything).Return(msg, nil)
139
140 header := http.Header{}
141 header.Set("Content-Type", "application/json")
142 httpRequest := tests.GetHttpRequest()
143 httpRequest.Header = header
144
145 writer := httptest.NewRecorder()
146 config := configuration.ParseConfiguration()
147 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config, ranSetupManager)
148 controller.EndcSetupHandler(writer, httpRequest)
149
150 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
irina33f84e12019-09-10 12:40:37 +0300151}
152
ss412g07ef76d2019-08-12 17:26:40 +0300153func TestShutdownHandlerRnibError(t *testing.T) {
154 log := initLog(t)
155 config := configuration.ParseConfiguration()
156
rh362jf9e31bb2019-08-15 15:37:32 +0300157 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +0300158 readerMock := &mocks.RnibReaderMock{}
159 readerProvider := func() reader.RNibReader {
160 return readerMock
161 }
162 writerMock := &mocks.RnibWriterMock{}
163 writerProvider := func() rNibWriter.RNibWriter {
164 return writerMock
165 }
ss412g07ef76d2019-08-12 17:26:40 +0300166
irina006fbce2019-09-05 16:11:02 +0300167 rnibErr := &common.ResourceNotFoundError{}
ss412g07ef76d2019-08-12 17:26:40 +0300168 var nbIdentityList []*entities.NbIdentity
169 readerMock.On("GetListNodebIds").Return(nbIdentityList, rnibErr)
170
171 writer := httptest.NewRecorder()
irina33f84e12019-09-10 12:40:37 +0300172 ranSetupManager := managers.NewRanSetupManager(log, getRmrService(rmrMessengerMock, log), rNibWriter.GetRNibWriter)
173 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config, ranSetupManager)
Amichai4db397d2019-08-28 19:23:15 +0300174 controller.ShutdownHandler(writer, tests.GetHttpRequest())
ss412g07ef76d2019-08-12 17:26:40 +0300175
176 var errorResponse = parseJsonRequest(t, writer.Body)
177
178 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
irina2165a762019-09-04 10:54:37 +0300179 assert.Equal(t, errorResponse.Code, e2managererrors.NewRnibDbError().Code)
ss412g07ef76d2019-08-12 17:26:40 +0300180}
181
182func TestHeaderValidationFailed(t *testing.T) {
183 log := initLog(t)
184 config := configuration.ParseConfiguration()
185
rh362jf9e31bb2019-08-15 15:37:32 +0300186 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +0300187 readerMock := &mocks.RnibReaderMock{}
188 readerProvider := func() reader.RNibReader {
189 return readerMock
190 }
191 writerMock := &mocks.RnibWriterMock{}
192 writerProvider := func() rNibWriter.RNibWriter {
193 return writerMock
194 }
ss412g07ef76d2019-08-12 17:26:40 +0300195
196 writer := httptest.NewRecorder()
irina33f84e12019-09-10 12:40:37 +0300197 ranSetupManager := managers.NewRanSetupManager(log, getRmrService(rmrMessengerMock, log), rNibWriter.GetRNibWriter)
198 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config, ranSetupManager)
ss412g07ef76d2019-08-12 17:26:40 +0300199
200 header := &http.Header{}
201
irina33f84e12019-09-10 12:40:37 +0300202 controller.handleRequest(writer, header, httpmsghandlerprovider.ShutdownRequest, nil, true)
ss412g07ef76d2019-08-12 17:26:40 +0300203
204 var errorResponse = parseJsonRequest(t, writer.Body)
205 err := e2managererrors.NewHeaderValidationError()
206
207 assert.Equal(t, http.StatusUnsupportedMediaType, writer.Result().StatusCode)
irina2165a762019-09-04 10:54:37 +0300208 assert.Equal(t, errorResponse.Code, err.Code)
209 assert.Equal(t, errorResponse.Message, err.Message)
ss412g07ef76d2019-08-12 17:26:40 +0300210}
211
is005q19e72a52019-08-26 17:56:18 +0300212func TestShutdownStatusNoContent(t *testing.T) {
ss412g07ef76d2019-08-12 17:26:40 +0300213 log := initLog(t)
214
rh362jf9e31bb2019-08-15 15:37:32 +0300215 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +0300216 readerMock := &mocks.RnibReaderMock{}
217 readerProvider := func() reader.RNibReader {
218 return readerMock
219 }
220 writerMock := &mocks.RnibWriterMock{}
221 writerProvider := func() rNibWriter.RNibWriter {
222 return writerMock
223 }
224 config := configuration.ParseConfiguration()
ss412g07ef76d2019-08-12 17:26:40 +0300225
irina006fbce2019-09-05 16:11:02 +0300226 var rnibError error
ss412g07ef76d2019-08-12 17:26:40 +0300227 nbIdentityList := []*entities.NbIdentity{}
228 readerMock.On("GetListNodebIds").Return(nbIdentityList, rnibError)
229
230 writer := httptest.NewRecorder()
irina33f84e12019-09-10 12:40:37 +0300231 ranSetupManager := managers.NewRanSetupManager(log, getRmrService(rmrMessengerMock, log), rNibWriter.GetRNibWriter)
232 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config, ranSetupManager)
Amichai4db397d2019-08-28 19:23:15 +0300233 controller.ShutdownHandler(writer, tests.GetHttpRequest())
ss412g07ef76d2019-08-12 17:26:40 +0300234
235 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
236}
237
238func TestHandleInternalError(t *testing.T) {
239 log := initLog(t)
240 config := configuration.ParseConfiguration()
241
rh362jf9e31bb2019-08-15 15:37:32 +0300242 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +0300243 readerMock := &mocks.RnibReaderMock{}
244 readerProvider := func() reader.RNibReader {
245 return readerMock
246 }
247 writerMock := &mocks.RnibWriterMock{}
248 writerProvider := func() rNibWriter.RNibWriter {
249 return writerMock
250 }
ss412g07ef76d2019-08-12 17:26:40 +0300251
252 writer := httptest.NewRecorder()
irina33f84e12019-09-10 12:40:37 +0300253 ranSetupManager := managers.NewRanSetupManager(log, getRmrService(rmrMessengerMock, log), rNibWriter.GetRNibWriter)
254 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config, ranSetupManager)
ss412g07ef76d2019-08-12 17:26:40 +0300255 err := e2managererrors.NewInternalError()
256
257 controller.handleErrorResponse(err, writer)
258 var errorResponse = parseJsonRequest(t, writer.Body)
259
260 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
irina2165a762019-09-04 10:54:37 +0300261 assert.Equal(t, errorResponse.Code, err.Code)
262 assert.Equal(t, errorResponse.Message, err.Message)
ss412g07ef76d2019-08-12 17:26:40 +0300263}
264
265func TestHandleCommandAlreadyInProgressError(t *testing.T) {
266 log := initLog(t)
267 config := configuration.ParseConfiguration()
268
rh362jf9e31bb2019-08-15 15:37:32 +0300269 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +0300270 readerMock := &mocks.RnibReaderMock{}
271 readerProvider := func() reader.RNibReader {
272 return readerMock
273 }
274 writerMock := &mocks.RnibWriterMock{}
275 writerProvider := func() rNibWriter.RNibWriter {
276 return writerMock
277 }
ss412g07ef76d2019-08-12 17:26:40 +0300278 writer := httptest.NewRecorder()
irina33f84e12019-09-10 12:40:37 +0300279 ranSetupManager := managers.NewRanSetupManager(log, getRmrService(rmrMessengerMock, log), rNibWriter.GetRNibWriter)
280 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config, ranSetupManager)
ss412g07ef76d2019-08-12 17:26:40 +0300281 err := e2managererrors.NewCommandAlreadyInProgressError()
282
283 controller.handleErrorResponse(err, writer)
284 var errorResponse = parseJsonRequest(t, writer.Body)
285
286 assert.Equal(t, http.StatusMethodNotAllowed, writer.Result().StatusCode)
irina2165a762019-09-04 10:54:37 +0300287 assert.Equal(t, errorResponse.Code, err.Code)
288 assert.Equal(t, errorResponse.Message, err.Message)
ss412g07ef76d2019-08-12 17:26:40 +0300289}
290
is005q19e72a52019-08-26 17:56:18 +0300291func TestValidateHeaders(t *testing.T) {
ss412g07ef76d2019-08-12 17:26:40 +0300292 log := initLog(t)
293
rh362jf9e31bb2019-08-15 15:37:32 +0300294 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +0300295 readerMock := &mocks.RnibReaderMock{}
296 readerProvider := func() reader.RNibReader {
297 return readerMock
298 }
299 writerMock := &mocks.RnibWriterMock{}
300 writerProvider := func() rNibWriter.RNibWriter {
301 return writerMock
302 }
303 config := configuration.ParseConfiguration()
irina33f84e12019-09-10 12:40:37 +0300304 ranSetupManager := managers.NewRanSetupManager(log, getRmrService(rmrMessengerMock, log), rNibWriter.GetRNibWriter)
305 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config, ranSetupManager)
ss412g07ef76d2019-08-12 17:26:40 +0300306
307 header := http.Header{}
308 header.Set("Content-Type", "application/json")
309 result := controller.validateRequestHeader(&header)
310
311 assert.Nil(t, result)
312}
313
314func parseJsonRequest(t *testing.T, r io.Reader) models.ErrorResponse {
315
316 var errorResponse models.ErrorResponse
317 body, err := ioutil.ReadAll(r)
318 if err != nil {
319 t.Errorf("Error cannot deserialize json request")
320 }
321 json.Unmarshal(body, &errorResponse)
322
323 return errorResponse
324}
325
326func initLog(t *testing.T) *logger.Logger {
327 log, err := logger.InitLogger(logger.InfoLevel)
328 if err != nil {
329 t.Errorf("#delete_all_request_handler_test.TestHandleSuccessFlow - failed to initialize logger, error: %s", err)
330 }
331 return log
332}
rh362j574c21e2019-08-19 15:25:50 +0300333
is005q19e72a52019-08-26 17:56:18 +0300334func TestX2ResetHandleSuccessfulRequestedCause(t *testing.T) {
rh362j574c21e2019-08-19 15:25:50 +0300335 log := initLog(t)
336
337 ranName := "test1"
338
339 readerMock := &mocks.RnibReaderMock{}
340 readerProvider := func() reader.RNibReader {
341 return readerMock
342 }
343 writerMock := &mocks.RnibWriterMock{}
344 writerProvider := func() rNibWriter.RNibWriter {
345 return writerMock
346 }
is005q19e72a52019-08-26 17:56:18 +0300347 payload := []byte{0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x40}
rh362j574c21e2019-08-19 15:25:50 +0300348 xaction := []byte(ranName)
is005q19e72a52019-08-26 17:56:18 +0300349 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xaction)
rh362j574c21e2019-08-19 15:25:50 +0300350 rmrMessengerMock := &mocks.RmrMessengerMock{}
is005q19e72a52019-08-26 17:56:18 +0300351 rmrMessengerMock.On("SendMsg", msg, mock.Anything).Return(msg, nil)
rh362j574c21e2019-08-19 15:25:50 +0300352
353 config := configuration.ParseConfiguration()
is005q19e72a52019-08-26 17:56:18 +0300354 rmrService := getRmrService(rmrMessengerMock, log)
rh362j574c21e2019-08-19 15:25:50 +0300355
356 writer := httptest.NewRecorder()
irina33f84e12019-09-10 12:40:37 +0300357 ranSetupManager := managers.NewRanSetupManager(log, getRmrService(rmrMessengerMock, log), rNibWriter.GetRNibWriter)
358 controller := NewController(log, rmrService, readerProvider, writerProvider, config, ranSetupManager)
rh362j574c21e2019-08-19 15:25:50 +0300359
is005q19e72a52019-08-26 17:56:18 +0300360 var nodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED}
361 readerMock.On("GetNodeb", ranName).Return(nodeb, nil)
rh362j574c21e2019-08-19 15:25:50 +0300362
363 data4Req := map[string]interface{}{"cause": "protocol:transfer-syntax-error"}
364 b := new(bytes.Buffer)
365 _ = json.NewEncoder(b).Encode(data4Req)
366 req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
Amichai4db397d2019-08-28 19:23:15 +0300367 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
rh362j574c21e2019-08-19 15:25:50 +0300368
Amichai4db397d2019-08-28 19:23:15 +0300369 controller.X2ResetHandler(writer, req)
rh362j574c21e2019-08-19 15:25:50 +0300370 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
371
rh362j574c21e2019-08-19 15:25:50 +0300372}
373
is005q19e72a52019-08-26 17:56:18 +0300374func TestX2ResetHandleSuccessfulRequestedDefault(t *testing.T) {
rh362j574c21e2019-08-19 15:25:50 +0300375 log := initLog(t)
376
377 ranName := "test1"
378
379 readerMock := &mocks.RnibReaderMock{}
380 readerProvider := func() reader.RNibReader {
381 return readerMock
382 }
383 writerMock := &mocks.RnibWriterMock{}
384 writerProvider := func() rNibWriter.RNibWriter {
385 return writerMock
386 }
387 // o&m intervention
is005q19e72a52019-08-26 17:56:18 +0300388 payload := []byte{0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x64}
rh362j574c21e2019-08-19 15:25:50 +0300389 xaction := []byte(ranName)
is005q19e72a52019-08-26 17:56:18 +0300390 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xaction)
rh362j574c21e2019-08-19 15:25:50 +0300391 rmrMessengerMock := &mocks.RmrMessengerMock{}
is005q19e72a52019-08-26 17:56:18 +0300392 rmrMessengerMock.On("SendMsg", msg, mock.Anything).Return(msg, nil)
rh362j574c21e2019-08-19 15:25:50 +0300393
394 config := configuration.ParseConfiguration()
is005q19e72a52019-08-26 17:56:18 +0300395 rmrService := getRmrService(rmrMessengerMock, log)
rh362j574c21e2019-08-19 15:25:50 +0300396
397 writer := httptest.NewRecorder()
irina33f84e12019-09-10 12:40:37 +0300398 ranSetupManager := managers.NewRanSetupManager(log, getRmrService(rmrMessengerMock, log), rNibWriter.GetRNibWriter)
399 controller := NewController(log, rmrService, readerProvider, writerProvider, config, ranSetupManager)
rh362j574c21e2019-08-19 15:25:50 +0300400
is005q19e72a52019-08-26 17:56:18 +0300401 var nodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED}
402 readerMock.On("GetNodeb", ranName).Return(nodeb, nil)
rh362j574c21e2019-08-19 15:25:50 +0300403
404 // no body
405 b := new(bytes.Buffer)
406 req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
Amichai4db397d2019-08-28 19:23:15 +0300407 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
rh362j574c21e2019-08-19 15:25:50 +0300408
Amichai4db397d2019-08-28 19:23:15 +0300409 controller.X2ResetHandler(writer, req)
rh362j574c21e2019-08-19 15:25:50 +0300410 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
411
rh362j574c21e2019-08-19 15:25:50 +0300412}
413
is005q19e72a52019-08-26 17:56:18 +0300414func TestX2ResetHandleFailureInvalidBody(t *testing.T) {
rh362j574c21e2019-08-19 15:25:50 +0300415 log := initLog(t)
416
417 ranName := "test1"
418
419 readerMock := &mocks.RnibReaderMock{}
420 readerProvider := func() reader.RNibReader {
421 return readerMock
422 }
423 writerMock := &mocks.RnibWriterMock{}
424 writerProvider := func() rNibWriter.RNibWriter {
425 return writerMock
426 }
427 rmrMessengerMock := &mocks.RmrMessengerMock{}
428
429 config := configuration.ParseConfiguration()
is005q19e72a52019-08-26 17:56:18 +0300430 rmrService := getRmrService(rmrMessengerMock, log)
rh362j574c21e2019-08-19 15:25:50 +0300431
432 writer := httptest.NewRecorder()
irina33f84e12019-09-10 12:40:37 +0300433 ranSetupManager := managers.NewRanSetupManager(log, getRmrService(rmrMessengerMock, log), rNibWriter.GetRNibWriter)
434 controller := NewController(log, rmrService, readerProvider, writerProvider, config, ranSetupManager)
rh362j574c21e2019-08-19 15:25:50 +0300435
436 // Invalid json: attribute name without quotes (should be "cause":).
437 b := strings.NewReader("{cause:\"protocol:transfer-syntax-error\"")
438 req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
Amichai4db397d2019-08-28 19:23:15 +0300439 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
rh362j574c21e2019-08-19 15:25:50 +0300440
Amichai4db397d2019-08-28 19:23:15 +0300441 controller.X2ResetHandler(writer, req)
rh362j574c21e2019-08-19 15:25:50 +0300442 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
irinab7c68cc2019-09-11 14:29:45 +0300443
rh362j574c21e2019-08-19 15:25:50 +0300444}
445
is005q19e72a52019-08-26 17:56:18 +0300446func TestHandleErrorResponse(t *testing.T) {
rh362j574c21e2019-08-19 15:25:50 +0300447 log := initLog(t)
448
449 readerMock := &mocks.RnibReaderMock{}
450 readerProvider := func() reader.RNibReader {
451 return readerMock
452 }
453 writerMock := &mocks.RnibWriterMock{}
454 writerProvider := func() rNibWriter.RNibWriter {
455 return writerMock
456 }
457 rmrMessengerMock := &mocks.RmrMessengerMock{}
458
459 config := configuration.ParseConfiguration()
is005q19e72a52019-08-26 17:56:18 +0300460 rmrService := getRmrService(rmrMessengerMock, log)
irina33f84e12019-09-10 12:40:37 +0300461 ranSetupManager := managers.NewRanSetupManager(log, getRmrService(rmrMessengerMock, log), rNibWriter.GetRNibWriter)
462 controller := NewController(log, rmrService, readerProvider, writerProvider, config, ranSetupManager)
rh362j574c21e2019-08-19 15:25:50 +0300463
464 writer := httptest.NewRecorder()
is005q19e72a52019-08-26 17:56:18 +0300465 controller.handleErrorResponse(e2managererrors.NewRnibDbError(), writer)
rh362j574c21e2019-08-19 15:25:50 +0300466 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
467
468 writer = httptest.NewRecorder()
is005q19e72a52019-08-26 17:56:18 +0300469 controller.handleErrorResponse(e2managererrors.NewCommandAlreadyInProgressError(), writer)
rh362j574c21e2019-08-19 15:25:50 +0300470 assert.Equal(t, http.StatusMethodNotAllowed, writer.Result().StatusCode)
471
472 writer = httptest.NewRecorder()
is005q19e72a52019-08-26 17:56:18 +0300473 controller.handleErrorResponse(e2managererrors.NewHeaderValidationError(), writer)
rh362j574c21e2019-08-19 15:25:50 +0300474 assert.Equal(t, http.StatusUnsupportedMediaType, writer.Result().StatusCode)
475
476 writer = httptest.NewRecorder()
is005q19e72a52019-08-26 17:56:18 +0300477 controller.handleErrorResponse(e2managererrors.NewWrongStateError("", ""), writer)
rh362j574c21e2019-08-19 15:25:50 +0300478 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
479
480 writer = httptest.NewRecorder()
is005q19e72a52019-08-26 17:56:18 +0300481 controller.handleErrorResponse(e2managererrors.NewRequestValidationError(), writer)
rh362j574c21e2019-08-19 15:25:50 +0300482 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
483
484 writer = httptest.NewRecorder()
is005q19e72a52019-08-26 17:56:18 +0300485 controller.handleErrorResponse(e2managererrors.NewRmrError(), writer)
rh362j574c21e2019-08-19 15:25:50 +0300486 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
487
488 writer = httptest.NewRecorder()
is005q19e72a52019-08-26 17:56:18 +0300489 controller.handleErrorResponse(e2managererrors.NewResourceNotFoundError(), writer)
rh362j574c21e2019-08-19 15:25:50 +0300490 assert.Equal(t, http.StatusNotFound, writer.Result().StatusCode)
491
492 writer = httptest.NewRecorder()
is005q19e72a52019-08-26 17:56:18 +0300493 controller.handleErrorResponse(fmt.Errorf("ErrorError"), writer)
rh362j574c21e2019-08-19 15:25:50 +0300494 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
495}
irinab7c68cc2019-09-11 14:29:45 +0300496
497func initTest(t *testing.T)(*mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.RmrMessengerMock, *managers.RanSetupManager) {
498 log := initLog(t)
499
500 readerMock := &mocks.RnibReaderMock{}
501 writerMock := &mocks.RnibWriterMock{}
502 writerProvider := func() rNibWriter.RNibWriter {
503 return writerMock
504 }
505
506 rmrMessengerMock := &mocks.RmrMessengerMock{}
507 rmrService := getRmrService(rmrMessengerMock, log)
508
509 ranSetupManager := managers.NewRanSetupManager(log, rmrService, writerProvider)
510
511 return readerMock, writerMock, rmrMessengerMock, ranSetupManager
512}