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)
42 nodebControllerMock.On("SetGeneralConfiguration").Return(nil)
43 nodebControllerMock.On("DeleteEnb").Return(nil)
44 nodebControllerMock.On("AddEnb").Return(nil)
46 e2tControllerMock := &mocks.E2TControllerMock{}
48 e2tControllerMock.On("GetE2TInstances").Return(nil)
50 router := mux.NewRouter()
51 initializeRoutes(router, rootControllerMock, nodebControllerMock, e2tControllerMock)
52 return router, rootControllerMock, nodebControllerMock, e2tControllerMock
55 func TestRouteGetNodebIds(t *testing.T) {
56 router, _, nodebControllerMock, _ := setupRouterAndMocks()
58 req, err := http.NewRequest("GET", "/v1/nodeb/ids", nil)
62 rr := httptest.NewRecorder()
63 router.ServeHTTP(rr, req)
65 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
68 func TestRouteGetNodebRanName(t *testing.T) {
69 router, _, nodebControllerMock, _ := setupRouterAndMocks()
71 req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil)
75 rr := httptest.NewRecorder()
76 router.ServeHTTP(rr, req)
78 assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code")
79 assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body")
80 nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1)
83 func TestRouteGetHealth(t *testing.T) {
84 router, rootControllerMock, _, _ := setupRouterAndMocks()
86 req, err := http.NewRequest("GET", "/v1/health", nil)
90 rr := httptest.NewRecorder()
91 router.ServeHTTP(rr, req)
93 rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
96 func TestRoutePutNodebShutdown(t *testing.T) {
97 router, _, nodebControllerMock, _ := setupRouterAndMocks()
99 req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil)
103 rr := httptest.NewRecorder()
104 router.ServeHTTP(rr, req)
106 nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1)
109 func TestRoutePutNodebSetGeneralConfiguration(t *testing.T) {
110 router, _, nodebControllerMock, _ := setupRouterAndMocks()
112 req, err := http.NewRequest("PUT", "/v1/nodeb/parameters", nil)
116 rr := httptest.NewRecorder()
117 router.ServeHTTP(rr, req)
119 nodebControllerMock.AssertNumberOfCalls(t, "SetGeneralConfiguration", 1)
122 func TestRouteNotFound(t *testing.T) {
123 router, _, _,_ := setupRouterAndMocks()
125 req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
129 rr := httptest.NewRecorder()
130 router.ServeHTTP(rr, req)
132 assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
135 func TestRunError(t *testing.T) {
137 err := Run(log, 1234567, &mocks.RootControllerMock{}, &mocks.NodebControllerMock{}, &mocks.E2TControllerMock{})
138 assert.NotNil(t, err)
141 func TestRun(t *testing.T) {
143 _, rootControllerMock, nodebControllerMock, e2tControllerMock := setupRouterAndMocks()
144 go Run(log, 11223, rootControllerMock, nodebControllerMock, e2tControllerMock)
146 time.Sleep(time.Millisecond * 100)
147 resp, err := http.Get("http://localhost:11223/v1/health")
149 t.Fatalf("failed to perform GET to http://localhost:11223/v1/health")
151 assert.Equal(t, 200, resp.StatusCode)
154 func TestRouteAddEnb(t *testing.T) {
155 router, _, nodebControllerMock, _ := setupRouterAndMocks()
157 req, err := http.NewRequest("POST", "/v1/nodeb/enb", nil)
161 rr := httptest.NewRecorder()
162 router.ServeHTTP(rr, req)
164 assert.Equal(t, http.StatusCreated, rr.Code, "handler returned wrong status code")
165 nodebControllerMock.AssertNumberOfCalls(t, "AddEnb", 1)
168 func TestRouteDeleteEnb(t *testing.T) {
169 router, _, nodebControllerMock, _ := setupRouterAndMocks()
171 req, err := http.NewRequest("DELETE", "/v1/nodeb/enb/ran1", nil)
175 rr := httptest.NewRecorder()
176 router.ServeHTTP(rr, req)
178 assert.Equal(t, http.StatusNoContent, rr.Code, "handler returned wrong status code")
179 nodebControllerMock.AssertNumberOfCalls(t, "DeleteEnb", 1)
182 func initLog(t *testing.T) *logger.Logger {
183 log, err := logger.InitLogger(logger.InfoLevel)
185 t.Errorf("#initLog test - failed to initialize logger, error: %s", err)