7001eef3c5503339681512819711fa00627f5d17
[ric-plt/e2mgr.git] / E2Manager / controllers / root_controller_test.go
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/logger"
23         "e2mgr/mocks"
24         "e2mgr/rNibWriter"
25         "e2mgr/services"
26         "fmt"
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"
31         "net"
32         "net/http"
33         "net/http/httptest"
34         "testing"
35 )
36
37 func setupNodebControllerTest(t *testing.T) (services.RNibDataService, *mocks.RnibReaderMock){
38         logger, err := logger.InitLogger(logger.DebugLevel)
39         if err != nil {
40                 t.Errorf("#... - failed to initialize logger, error: %s", err)
41         }
42         config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
43         readerMock := &mocks.RnibReaderMock{}
44         rnibReaderProvider := func() reader.RNibReader {
45                 return readerMock
46         }
47         rnibWriterProvider := func() rNibWriter.RNibWriter {
48                 return &mocks.RnibWriterMock{}
49         }
50         rnibDataService := services.NewRnibDataService(logger, config, rnibReaderProvider, rnibWriterProvider)
51         return rnibDataService, readerMock
52 }
53
54 func TestNewRequestController(t *testing.T) {
55         rnibDataService, _ := setupNodebControllerTest(t)
56         assert.NotNil(t, NewRootController(rnibDataService))
57 }
58
59 func TestHandleHealthCheckRequestGood(t *testing.T) {
60         rnibDataService, rnibReaderMock := setupNodebControllerTest(t)
61
62         var nbList []*entities.NbIdentity
63         rnibReaderMock.On("GetListNodebIds").Return(nbList, nil)
64
65         rc := NewRootController(rnibDataService)
66         writer := httptest.NewRecorder()
67         rc.HandleHealthCheckRequest(writer, nil)
68         assert.Equal(t, http.StatusOK, writer.Result().StatusCode)
69 }
70
71 func TestHandleHealthCheckRequestOtherError(t *testing.T) {
72         rnibDataService, rnibReaderMock := setupNodebControllerTest(t)
73
74         mockOtherErr := &common.InternalError{Err: fmt.Errorf("non connection error")}
75         var nbList []*entities.NbIdentity
76         rnibReaderMock.On("GetListNodebIds").Return(nbList, mockOtherErr)
77
78         rc := NewRootController(rnibDataService)
79         writer := httptest.NewRecorder()
80         rc.HandleHealthCheckRequest(writer, nil)
81         assert.Equal(t, http.StatusOK, writer.Result().StatusCode)
82 }
83
84 func TestHandleHealthCheckRequestConnError(t *testing.T) {
85         rnibDataService, rnibReaderMock := setupNodebControllerTest(t)
86
87         mockConnErr := &common.InternalError{Err: &net.OpError{Err: fmt.Errorf("connection error")}}
88         var nbList []*entities.NbIdentity
89         rnibReaderMock.On("GetListNodebIds").Return(nbList, mockConnErr)
90
91
92         rc := NewRootController(rnibDataService)
93         writer := httptest.NewRecorder()
94         rc.HandleHealthCheckRequest(writer, nil)
95         assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
96 }