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.
27 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
29 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
30 "github.com/stretchr/testify/assert"
37 func setupNodebControllerTest(t *testing.T) (services.RNibDataService, *mocks.RnibReaderMock){
38 logger, err := logger.InitLogger(logger.DebugLevel)
40 t.Errorf("#... - failed to initialize logger, error: %s", err)
42 config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
43 readerMock := &mocks.RnibReaderMock{}
44 rnibReaderProvider := func() reader.RNibReader {
47 rnibWriterProvider := func() rNibWriter.RNibWriter {
48 return &mocks.RnibWriterMock{}
50 rnibDataService := services.NewRnibDataService(logger, config, rnibReaderProvider, rnibWriterProvider)
51 return rnibDataService, readerMock
54 func TestNewRequestController(t *testing.T) {
55 rnibDataService, _ := setupNodebControllerTest(t)
56 assert.NotNil(t, NewRootController(rnibDataService))
59 func TestHandleHealthCheckRequestGood(t *testing.T) {
60 rnibDataService, rnibReaderMock := setupNodebControllerTest(t)
62 var nbList []*entities.NbIdentity
63 rnibReaderMock.On("GetListNodebIds").Return(nbList, nil)
65 rc := NewRootController(rnibDataService)
66 writer := httptest.NewRecorder()
67 rc.HandleHealthCheckRequest(writer, nil)
68 assert.Equal(t, http.StatusOK, writer.Result().StatusCode)
71 func TestHandleHealthCheckRequestOtherError(t *testing.T) {
72 rnibDataService, rnibReaderMock := setupNodebControllerTest(t)
74 mockOtherErr := &common.InternalError{Err: fmt.Errorf("non connection error")}
75 var nbList []*entities.NbIdentity
76 rnibReaderMock.On("GetListNodebIds").Return(nbList, mockOtherErr)
78 rc := NewRootController(rnibDataService)
79 writer := httptest.NewRecorder()
80 rc.HandleHealthCheckRequest(writer, nil)
81 assert.Equal(t, http.StatusOK, writer.Result().StatusCode)
84 func TestHandleHealthCheckRequestConnError(t *testing.T) {
85 rnibDataService, rnibReaderMock := setupNodebControllerTest(t)
87 mockConnErr := &common.InternalError{Err: &net.OpError{Err: fmt.Errorf("connection error")}}
88 var nbList []*entities.NbIdentity
89 rnibReaderMock.On("GetListNodebIds").Return(nbList, mockConnErr)
92 rc := NewRootController(rnibDataService)
93 writer := httptest.NewRecorder()
94 rc.HandleHealthCheckRequest(writer, nil)
95 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)