blob: 6bc16136fd1165c6c521e4dca314d7d07da136b6 [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 (
21 "e2mgr/configuration"
22 "e2mgr/e2managererrors"
23 "e2mgr/logger"
24 "e2mgr/mocks"
25 "e2mgr/models"
26 "e2mgr/providers"
27 "e2mgr/rNibWriter"
28 "e2mgr/tests"
29 "encoding/json"
30 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
31 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
33 "github.com/stretchr/testify/assert"
34 "io"
35 "io/ioutil"
36 "net/http"
37 "net/http/httptest"
38 "testing"
39)
40
41func TestShutdownHandlerRnibError(t *testing.T) {
42 log := initLog(t)
43 config := configuration.ParseConfiguration()
44
rh362jf9e31bb2019-08-15 15:37:32 +030045 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +030046 readerMock := &mocks.RnibReaderMock{}
47 readerProvider := func() reader.RNibReader {
48 return readerMock
49 }
50 writerMock := &mocks.RnibWriterMock{}
51 writerProvider := func() rNibWriter.RNibWriter {
52 return writerMock
53 }
ss412g07ef76d2019-08-12 17:26:40 +030054
55 rnibErr := &common.RNibError{}
56 var nbIdentityList []*entities.NbIdentity
57 readerMock.On("GetListNodebIds").Return(nbIdentityList, rnibErr)
58
59 writer := httptest.NewRecorder()
irinad6deb7f2019-08-16 00:14:51 +030060 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
ss412g07ef76d2019-08-12 17:26:40 +030061 controller.ShutdownHandler(writer, tests.GetHttpRequest(), nil)
62
63 var errorResponse = parseJsonRequest(t, writer.Body)
64
65 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
66 assert.Equal(t, errorResponse.Code, e2managererrors.NewRnibDbError().Err.Code)
67}
68
69func TestHeaderValidationFailed(t *testing.T) {
70 log := initLog(t)
71 config := configuration.ParseConfiguration()
72
rh362jf9e31bb2019-08-15 15:37:32 +030073 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +030074 readerMock := &mocks.RnibReaderMock{}
75 readerProvider := func() reader.RNibReader {
76 return readerMock
77 }
78 writerMock := &mocks.RnibWriterMock{}
79 writerProvider := func() rNibWriter.RNibWriter {
80 return writerMock
81 }
ss412g07ef76d2019-08-12 17:26:40 +030082
83 writer := httptest.NewRecorder()
84
irinad6deb7f2019-08-16 00:14:51 +030085 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
ss412g07ef76d2019-08-12 17:26:40 +030086
87 header := &http.Header{}
88
89 controller.handleRequest(writer, header, providers.ShutdownRequest, nil, true, http.StatusNoContent)
90
91 var errorResponse = parseJsonRequest(t, writer.Body)
92 err := e2managererrors.NewHeaderValidationError()
93
94 assert.Equal(t, http.StatusUnsupportedMediaType, writer.Result().StatusCode)
95 assert.Equal(t, errorResponse.Code, err.Err.Code)
96 assert.Equal(t, errorResponse.Message, err.Err.Message)
97}
98
99func TestShutdownStatusNoContent(t *testing.T){
100 log := initLog(t)
101
rh362jf9e31bb2019-08-15 15:37:32 +0300102 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +0300103 readerMock := &mocks.RnibReaderMock{}
104 readerProvider := func() reader.RNibReader {
105 return readerMock
106 }
107 writerMock := &mocks.RnibWriterMock{}
108 writerProvider := func() rNibWriter.RNibWriter {
109 return writerMock
110 }
111 config := configuration.ParseConfiguration()
ss412g07ef76d2019-08-12 17:26:40 +0300112
113 var rnibError common.IRNibError
114 nbIdentityList := []*entities.NbIdentity{}
115 readerMock.On("GetListNodebIds").Return(nbIdentityList, rnibError)
116
117 writer := httptest.NewRecorder()
irinad6deb7f2019-08-16 00:14:51 +0300118 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
ss412g07ef76d2019-08-12 17:26:40 +0300119 controller.ShutdownHandler(writer, tests.GetHttpRequest(), nil)
120
121 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
122}
123
124func TestHandleInternalError(t *testing.T) {
125 log := initLog(t)
126 config := configuration.ParseConfiguration()
127
rh362jf9e31bb2019-08-15 15:37:32 +0300128 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +0300129 readerMock := &mocks.RnibReaderMock{}
130 readerProvider := func() reader.RNibReader {
131 return readerMock
132 }
133 writerMock := &mocks.RnibWriterMock{}
134 writerProvider := func() rNibWriter.RNibWriter {
135 return writerMock
136 }
ss412g07ef76d2019-08-12 17:26:40 +0300137
138 writer := httptest.NewRecorder()
irinad6deb7f2019-08-16 00:14:51 +0300139 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
ss412g07ef76d2019-08-12 17:26:40 +0300140 err := e2managererrors.NewInternalError()
141
142 controller.handleErrorResponse(err, writer)
143 var errorResponse = parseJsonRequest(t, writer.Body)
144
145 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
146 assert.Equal(t, errorResponse.Code, err.Err.Code)
147 assert.Equal(t, errorResponse.Message, err.Err.Message)
148}
149
150func TestHandleCommandAlreadyInProgressError(t *testing.T) {
151 log := initLog(t)
152 config := configuration.ParseConfiguration()
153
rh362jf9e31bb2019-08-15 15:37:32 +0300154 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +0300155 readerMock := &mocks.RnibReaderMock{}
156 readerProvider := func() reader.RNibReader {
157 return readerMock
158 }
159 writerMock := &mocks.RnibWriterMock{}
160 writerProvider := func() rNibWriter.RNibWriter {
161 return writerMock
162 }
ss412g07ef76d2019-08-12 17:26:40 +0300163 writer := httptest.NewRecorder()
irinad6deb7f2019-08-16 00:14:51 +0300164 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
ss412g07ef76d2019-08-12 17:26:40 +0300165 err := e2managererrors.NewCommandAlreadyInProgressError()
166
167 controller.handleErrorResponse(err, writer)
168 var errorResponse = parseJsonRequest(t, writer.Body)
169
170 assert.Equal(t, http.StatusMethodNotAllowed, writer.Result().StatusCode)
171 assert.Equal(t, errorResponse.Code, err.Err.Code)
172 assert.Equal(t, errorResponse.Message, err.Err.Message)
173}
174
175func TestValidateHeaders(t *testing.T){
176 log := initLog(t)
177
rh362jf9e31bb2019-08-15 15:37:32 +0300178 rmrMessengerMock := &mocks.RmrMessengerMock{}
ss412g07ef76d2019-08-12 17:26:40 +0300179 readerMock := &mocks.RnibReaderMock{}
180 readerProvider := func() reader.RNibReader {
181 return readerMock
182 }
183 writerMock := &mocks.RnibWriterMock{}
184 writerProvider := func() rNibWriter.RNibWriter {
185 return writerMock
186 }
187 config := configuration.ParseConfiguration()
ss412g07ef76d2019-08-12 17:26:40 +0300188
irinad6deb7f2019-08-16 00:14:51 +0300189 controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config)
ss412g07ef76d2019-08-12 17:26:40 +0300190
191 header := http.Header{}
192 header.Set("Content-Type", "application/json")
193 result := controller.validateRequestHeader(&header)
194
195 assert.Nil(t, result)
196}
197
198func parseJsonRequest(t *testing.T, r io.Reader) models.ErrorResponse {
199
200 var errorResponse models.ErrorResponse
201 body, err := ioutil.ReadAll(r)
202 if err != nil {
203 t.Errorf("Error cannot deserialize json request")
204 }
205 json.Unmarshal(body, &errorResponse)
206
207 return errorResponse
208}
209
210func initLog(t *testing.T) *logger.Logger {
211 log, err := logger.InitLogger(logger.InfoLevel)
212 if err != nil {
213 t.Errorf("#delete_all_request_handler_test.TestHandleSuccessFlow - failed to initialize logger, error: %s", err)
214 }
215 return log
216}