ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 1 | // |
| 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 | |
| 18 | package controllers |
| 19 | |
| 20 | import ( |
| 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 | |
| 41 | func TestShutdownHandlerRnibError(t *testing.T) { |
| 42 | log := initLog(t) |
| 43 | config := configuration.ParseConfiguration() |
| 44 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame] | 45 | rmrMessengerMock := &mocks.RmrMessengerMock{} |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 46 | 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 | } |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 54 | |
| 55 | rnibErr := &common.RNibError{} |
| 56 | var nbIdentityList []*entities.NbIdentity |
| 57 | readerMock.On("GetListNodebIds").Return(nbIdentityList, rnibErr) |
| 58 | |
| 59 | writer := httptest.NewRecorder() |
irina | d6deb7f | 2019-08-16 00:14:51 +0300 | [diff] [blame] | 60 | controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 61 | 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 | |
| 69 | func TestHeaderValidationFailed(t *testing.T) { |
| 70 | log := initLog(t) |
| 71 | config := configuration.ParseConfiguration() |
| 72 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame] | 73 | rmrMessengerMock := &mocks.RmrMessengerMock{} |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 74 | 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 | } |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 82 | |
| 83 | writer := httptest.NewRecorder() |
| 84 | |
irina | d6deb7f | 2019-08-16 00:14:51 +0300 | [diff] [blame] | 85 | controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 86 | |
| 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 | |
| 99 | func TestShutdownStatusNoContent(t *testing.T){ |
| 100 | log := initLog(t) |
| 101 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame] | 102 | rmrMessengerMock := &mocks.RmrMessengerMock{} |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 103 | 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() |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 112 | |
| 113 | var rnibError common.IRNibError |
| 114 | nbIdentityList := []*entities.NbIdentity{} |
| 115 | readerMock.On("GetListNodebIds").Return(nbIdentityList, rnibError) |
| 116 | |
| 117 | writer := httptest.NewRecorder() |
irina | d6deb7f | 2019-08-16 00:14:51 +0300 | [diff] [blame] | 118 | controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 119 | controller.ShutdownHandler(writer, tests.GetHttpRequest(), nil) |
| 120 | |
| 121 | assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode) |
| 122 | } |
| 123 | |
| 124 | func TestHandleInternalError(t *testing.T) { |
| 125 | log := initLog(t) |
| 126 | config := configuration.ParseConfiguration() |
| 127 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame] | 128 | rmrMessengerMock := &mocks.RmrMessengerMock{} |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 129 | 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 | } |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 137 | |
| 138 | writer := httptest.NewRecorder() |
irina | d6deb7f | 2019-08-16 00:14:51 +0300 | [diff] [blame] | 139 | controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 140 | 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 | |
| 150 | func TestHandleCommandAlreadyInProgressError(t *testing.T) { |
| 151 | log := initLog(t) |
| 152 | config := configuration.ParseConfiguration() |
| 153 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame] | 154 | rmrMessengerMock := &mocks.RmrMessengerMock{} |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 155 | 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 | } |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 163 | writer := httptest.NewRecorder() |
irina | d6deb7f | 2019-08-16 00:14:51 +0300 | [diff] [blame] | 164 | controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 165 | 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 | |
| 175 | func TestValidateHeaders(t *testing.T){ |
| 176 | log := initLog(t) |
| 177 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame] | 178 | rmrMessengerMock := &mocks.RmrMessengerMock{} |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 179 | 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() |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 188 | |
irina | d6deb7f | 2019-08-16 00:14:51 +0300 | [diff] [blame] | 189 | controller := NewController(log, getRmrService(rmrMessengerMock, log), readerProvider, writerProvider, config) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 190 | |
| 191 | header := http.Header{} |
| 192 | header.Set("Content-Type", "application/json") |
| 193 | result := controller.validateRequestHeader(&header) |
| 194 | |
| 195 | assert.Nil(t, result) |
| 196 | } |
| 197 | |
| 198 | func 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 | |
| 210 | func 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 | } |