Sync from Azure to LF
[ric-plt/resource-status-manager.git] / RSM / controllers / root_controller_test.go
diff --git a/RSM/controllers/root_controller_test.go b/RSM/controllers/root_controller_test.go
new file mode 100644 (file)
index 0000000..e546260
--- /dev/null
@@ -0,0 +1,94 @@
+//
+// Copyright 2019 AT&T Intellectual Property
+// Copyright 2019 Nokia
+//
+// Licensed under the Apache License, Version 2.0 (the "License")
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package controllers
+
+import (
+       "fmt"
+       "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
+       "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
+       "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
+       "github.com/stretchr/testify/assert"
+       "net"
+       "net/http"
+       "net/http/httptest"
+       "rsm/configuration"
+       "rsm/logger"
+       "rsm/mocks"
+       "rsm/services"
+       "testing"
+)
+
+func setupRootControllerTest(t *testing.T) (services.RNibDataService, *mocks.RnibReaderMock) {
+       logger, err := logger.InitLogger(logger.DebugLevel)
+       if err != nil {
+               t.Errorf("#... - failed to initialize logger, error: %s", err)
+       }
+       config, err := configuration.ParseConfiguration()
+       if err != nil {
+               t.Errorf("#... - failed to parse configuration error: %s", err)
+       }
+       readerMock := &mocks.RnibReaderMock{}
+       rnibReaderProvider := func() reader.RNibReader {
+               return readerMock
+       }
+       rnibDataService := services.NewRnibDataService(logger, config, rnibReaderProvider)
+       return rnibDataService, readerMock
+}
+
+func TestNewRequestController(t *testing.T) {
+       rnibDataService, _ := setupRootControllerTest(t)
+       assert.NotNil(t, NewRootController(rnibDataService))
+}
+
+func TestHandleHealthCheckRequestGood(t *testing.T) {
+       rnibDataService, rnibReaderMock := setupRootControllerTest(t)
+
+       var nbList []*entities.NbIdentity
+       rnibReaderMock.On("GetListNodebIds").Return(nbList, nil)
+
+       rc := NewRootController(rnibDataService)
+       writer := httptest.NewRecorder()
+       rc.HandleHealthCheckRequest(writer, nil)
+       assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
+}
+
+func TestHandleHealthCheckRequestOtherError(t *testing.T) {
+       rnibDataService, rnibReaderMock := setupRootControllerTest(t)
+
+       mockOtherErr := &common.InternalError{Err: fmt.Errorf("non connection error")}
+       var nbList []*entities.NbIdentity
+       rnibReaderMock.On("GetListNodebIds").Return(nbList, mockOtherErr)
+
+       rc := NewRootController(rnibDataService)
+       writer := httptest.NewRecorder()
+       rc.HandleHealthCheckRequest(writer, nil)
+       assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
+}
+
+func TestHandleHealthCheckRequestConnError(t *testing.T) {
+       rnibDataService, rnibReaderMock := setupRootControllerTest(t)
+
+       mockConnErr := &common.InternalError{Err: &net.OpError{Err: fmt.Errorf("connection error")}}
+       var nbList []*entities.NbIdentity
+       rnibReaderMock.On("GetListNodebIds").Return(nbList, mockConnErr)
+
+       rc := NewRootController(rnibDataService)
+       writer := httptest.NewRecorder()
+       rc.HandleHealthCheckRequest(writer, nil)
+       assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
+}