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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
26 "github.com/gorilla/mux"
27 "github.com/stretchr/testify/assert"
34 func setupRouterAndMocks() (*mux.Router, *mocks.RootControllerMock, *mocks.NodebControllerMock, *mocks.E2TControllerMock) {
35 rootControllerMock := &mocks.RootControllerMock{}
36 rootControllerMock.On("HandleHealthCheckRequest").Return(nil)
38 nodebControllerMock := &mocks.NodebControllerMock{}
39 nodebControllerMock.On("Shutdown").Return(nil)
40 nodebControllerMock.On("GetNodeb").Return(nil)
41 nodebControllerMock.On("GetNodebIdList").Return(nil)
43 e2tControllerMock := &mocks.E2TControllerMock{}
45 e2tControllerMock.On("GetE2TInstances").Return(nil)
47 router := mux.NewRouter()
48 initializeRoutes(router, rootControllerMock, nodebControllerMock, e2tControllerMock)
49 return router, rootControllerMock, nodebControllerMock, e2tControllerMock
52 func TestRouteGetNodebIds(t *testing.T) {
53 router, _, nodebControllerMock, _ := setupRouterAndMocks()
55 req, err := http.NewRequest("GET", "/v1/nodeb/ids", nil)
59 rr := httptest.NewRecorder()
60 router.ServeHTTP(rr, req)
62 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
65 func TestRouteGetNodebRanName(t *testing.T) {
66 router, _, nodebControllerMock, _ := setupRouterAndMocks()
68 req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil)
72 rr := httptest.NewRecorder()
73 router.ServeHTTP(rr, req)
75 assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code")
76 assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body")
77 nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1)
80 func TestRouteGetHealth(t *testing.T) {
81 router, rootControllerMock, _, _ := setupRouterAndMocks()
83 req, err := http.NewRequest("GET", "/v1/health", nil)
87 rr := httptest.NewRecorder()
88 router.ServeHTTP(rr, req)
90 rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
93 func TestRoutePutNodebShutdown(t *testing.T) {
94 router, _, nodebControllerMock, _ := setupRouterAndMocks()
96 req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil)
100 rr := httptest.NewRecorder()
101 router.ServeHTTP(rr, req)
103 nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1)
106 func TestRouteNotFound(t *testing.T) {
107 router, _, _,_ := setupRouterAndMocks()
109 req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
113 rr := httptest.NewRecorder()
114 router.ServeHTTP(rr, req)
116 assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
119 func TestRunError(t *testing.T) {
121 err := Run(log, 1234567, &mocks.RootControllerMock{}, &mocks.NodebControllerMock{}, &mocks.E2TControllerMock{})
122 assert.NotNil(t, err)
125 func TestRun(t *testing.T) {
127 _, rootControllerMock, nodebControllerMock, e2tControllerMock := setupRouterAndMocks()
128 go Run(log, 11223, rootControllerMock, nodebControllerMock, e2tControllerMock)
130 time.Sleep(time.Millisecond * 100)
131 resp, err := http.Get("http://localhost:11223/v1/health")
133 t.Fatalf("failed to perform GET to http://localhost:11223/v1/health")
135 assert.Equal(t, 200, resp.StatusCode)
138 func initLog(t *testing.T) *logger.Logger {
139 log, err := logger.InitLogger(logger.InfoLevel)
141 t.Errorf("#initLog test - failed to initialize logger, error: %s", err)