blob: f39a2590f84fcf22fad7691a6cd4fbf486f3c4bc [file] [log] [blame]
ss412gde190682019-10-24 09:29:26 +03001//
2// Copyright 2019 AT&T Intellectual Property
3// Copyright 2019 Nokia
Rahul Banerji5f208b12020-10-01 17:00:32 +05304// Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
ss412gde190682019-10-24 09:29:26 +03005//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
nm755n15d39822019-11-28 16:56:00 +000017
18// This source code is part of the near-RT RIC (RAN Intelligent Controller)
19// platform project (RICP).
20
ss412gde190682019-10-24 09:29:26 +030021package httpserver
22
23import (
ss412gefcb4522019-12-02 16:59:19 +020024 "e2mgr/logger"
ss412gde190682019-10-24 09:29:26 +030025 "e2mgr/mocks"
26 "github.com/gorilla/mux"
27 "github.com/stretchr/testify/assert"
28 "net/http"
29 "net/http/httptest"
30 "testing"
ss412gefcb4522019-12-02 16:59:19 +020031 "time"
ss412gde190682019-10-24 09:29:26 +030032)
33
ss412gefcb4522019-12-02 16:59:19 +020034func setupRouterAndMocks() (*mux.Router, *mocks.RootControllerMock, *mocks.NodebControllerMock, *mocks.E2TControllerMock) {
35 rootControllerMock := &mocks.RootControllerMock{}
36 rootControllerMock.On("HandleHealthCheckRequest").Return(nil)
ss412gde190682019-10-24 09:29:26 +030037
38 nodebControllerMock := &mocks.NodebControllerMock{}
ss412gefcb4522019-12-02 16:59:19 +020039 nodebControllerMock.On("Shutdown").Return(nil)
ss412gefcb4522019-12-02 16:59:19 +020040 nodebControllerMock.On("GetNodeb").Return(nil)
ss412gde190682019-10-24 09:29:26 +030041 nodebControllerMock.On("GetNodebIdList").Return(nil)
Rahul Banerji25cf7602020-10-06 11:26:30 +053042 nodebControllerMock.On("GetNodebId").Return(nil)
ss412ga07b8592020-06-16 21:51:33 +030043 nodebControllerMock.On("SetGeneralConfiguration").Return(nil)
Amichai7e2e4932020-07-07 15:49:07 +000044 nodebControllerMock.On("DeleteEnb").Return(nil)
45 nodebControllerMock.On("AddEnb").Return(nil)
Irina5830d4d2020-07-08 18:11:04 +000046 nodebControllerMock.On("UpdateEnb").Return(nil)
Rahul Banerjife8d4812020-08-24 14:26:41 +053047 nodebControllerMock.On("HealthCheckRequest").Return(nil)
ss412gefcb4522019-12-02 16:59:19 +020048
49 e2tControllerMock := &mocks.E2TControllerMock{}
50
51 e2tControllerMock.On("GetE2TInstances").Return(nil)
ss412gde190682019-10-24 09:29:26 +030052
53 router := mux.NewRouter()
ss412gefcb4522019-12-02 16:59:19 +020054 initializeRoutes(router, rootControllerMock, nodebControllerMock, e2tControllerMock)
55 return router, rootControllerMock, nodebControllerMock, e2tControllerMock
ss412gde190682019-10-24 09:29:26 +030056}
57
Rahul Banerji25cf7602020-10-06 11:26:30 +053058func TestRouteGetNodebIdList(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020059 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030060
Irinaa5904172020-07-16 21:21:51 +030061 req, err := http.NewRequest("GET", "/v1/nodeb/states", nil)
ss412gde190682019-10-24 09:29:26 +030062 if err != nil {
63 t.Fatal(err)
64 }
65 rr := httptest.NewRecorder()
66 router.ServeHTTP(rr, req)
67
ss412gefcb4522019-12-02 16:59:19 +020068 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
ss412gde190682019-10-24 09:29:26 +030069}
70
Rahul Banerji25cf7602020-10-06 11:26:30 +053071func TestRouteGetNodebId(t *testing.T) {
72 router, _, nodebControllerMock, _ := setupRouterAndMocks()
73
74 req, err := http.NewRequest("GET", "/v1/nodeb/states/ran1", nil)
75 if err != nil {
76 t.Fatal(err)
77 }
78 rr := httptest.NewRecorder()
79 router.ServeHTTP(rr, req)
80
81 assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code")
82 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebId", 1)
83}
84
ss412gde190682019-10-24 09:29:26 +030085func TestRouteGetNodebRanName(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020086 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030087
88 req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil)
89 if err != nil {
90 t.Fatal(err)
91 }
92 rr := httptest.NewRecorder()
93 router.ServeHTTP(rr, req)
94
95 assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code")
96 assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body")
ss412gefcb4522019-12-02 16:59:19 +020097 nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1)
ss412gde190682019-10-24 09:29:26 +030098}
99
100func TestRouteGetHealth(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +0200101 router, rootControllerMock, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300102
103 req, err := http.NewRequest("GET", "/v1/health", nil)
104 if err != nil {
105 t.Fatal(err)
106 }
107 rr := httptest.NewRecorder()
108 router.ServeHTTP(rr, req)
109
ss412gefcb4522019-12-02 16:59:19 +0200110 rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
ss412gde190682019-10-24 09:29:26 +0300111}
112
113func TestRoutePutNodebShutdown(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +0200114 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300115
116 req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil)
117 if err != nil {
118 t.Fatal(err)
119 }
120 rr := httptest.NewRecorder()
121 router.ServeHTTP(rr, req)
122
ss412gefcb4522019-12-02 16:59:19 +0200123 nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1)
ss412gde190682019-10-24 09:29:26 +0300124}
125
Rahul Banerjife8d4812020-08-24 14:26:41 +0530126func TestHealthCheckRequest(t *testing.T) {
127 router, _, nodebControllerMock, _ := setupRouterAndMocks()
128
129 req, err := http.NewRequest("PUT", "/v1/nodeb/health", nil)
130 if err != nil {
131 t.Fatal(err)
132 }
133 rr := httptest.NewRecorder()
134 router.ServeHTTP(rr, req)
135
Rahul Banerji25cf7602020-10-06 11:26:30 +0530136 assert.Equal(t, http.StatusAccepted, rr.Code, "handler returned wrong status code")
Rahul Banerjife8d4812020-08-24 14:26:41 +0530137 nodebControllerMock.AssertNumberOfCalls(t, "HealthCheckRequest", 1)
138}
139
ss412ga07b8592020-06-16 21:51:33 +0300140func TestRoutePutNodebSetGeneralConfiguration(t *testing.T) {
141 router, _, nodebControllerMock, _ := setupRouterAndMocks()
142
143 req, err := http.NewRequest("PUT", "/v1/nodeb/parameters", nil)
144 if err != nil {
145 t.Fatal(err)
146 }
147 rr := httptest.NewRecorder()
148 router.ServeHTTP(rr, req)
149
150 nodebControllerMock.AssertNumberOfCalls(t, "SetGeneralConfiguration", 1)
151}
152
Irina5830d4d2020-07-08 18:11:04 +0000153func TestRoutePutUpdateEnb(t *testing.T) {
154 router, _, nodebControllerMock, _ := setupRouterAndMocks()
155
156 req, err := http.NewRequest("PUT", "/v1/nodeb/enb/ran1", nil)
157 if err != nil {
158 t.Fatal(err)
159 }
160 rr := httptest.NewRecorder()
161 router.ServeHTTP(rr, req)
162
163 nodebControllerMock.AssertNumberOfCalls(t, "UpdateEnb", 1)
164}
165
ss412gde190682019-10-24 09:29:26 +0300166func TestRouteNotFound(t *testing.T) {
idanshale0106ff2020-07-07 16:04:00 +0000167 router, _, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300168
169 req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
170 if err != nil {
171 t.Fatal(err)
172 }
173 rr := httptest.NewRecorder()
174 router.ServeHTTP(rr, req)
175
176 assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
ss412gefcb4522019-12-02 16:59:19 +0200177}
178
179func TestRunError(t *testing.T) {
180 log := initLog(t)
181 err := Run(log, 1234567, &mocks.RootControllerMock{}, &mocks.NodebControllerMock{}, &mocks.E2TControllerMock{})
182 assert.NotNil(t, err)
183}
184
185func TestRun(t *testing.T) {
186 log := initLog(t)
187 _, rootControllerMock, nodebControllerMock, e2tControllerMock := setupRouterAndMocks()
188 go Run(log, 11223, rootControllerMock, nodebControllerMock, e2tControllerMock)
189
190 time.Sleep(time.Millisecond * 100)
191 resp, err := http.Get("http://localhost:11223/v1/health")
192 if err != nil {
193 t.Fatalf("failed to perform GET to http://localhost:11223/v1/health")
194 }
195 assert.Equal(t, 200, resp.StatusCode)
196}
197
Amichai7e2e4932020-07-07 15:49:07 +0000198func TestRouteAddEnb(t *testing.T) {
199 router, _, nodebControllerMock, _ := setupRouterAndMocks()
200
201 req, err := http.NewRequest("POST", "/v1/nodeb/enb", nil)
202 if err != nil {
203 t.Fatal(err)
204 }
205 rr := httptest.NewRecorder()
206 router.ServeHTTP(rr, req)
207
208 assert.Equal(t, http.StatusCreated, rr.Code, "handler returned wrong status code")
209 nodebControllerMock.AssertNumberOfCalls(t, "AddEnb", 1)
210}
211
212func TestRouteDeleteEnb(t *testing.T) {
213 router, _, nodebControllerMock, _ := setupRouterAndMocks()
214
215 req, err := http.NewRequest("DELETE", "/v1/nodeb/enb/ran1", nil)
216 if err != nil {
217 t.Fatal(err)
218 }
219 rr := httptest.NewRecorder()
220 router.ServeHTTP(rr, req)
221
222 assert.Equal(t, http.StatusNoContent, rr.Code, "handler returned wrong status code")
223 nodebControllerMock.AssertNumberOfCalls(t, "DeleteEnb", 1)
224}
225
ss412gefcb4522019-12-02 16:59:19 +0200226func initLog(t *testing.T) *logger.Logger {
227 log, err := logger.InitLogger(logger.InfoLevel)
228 if err != nil {
229 t.Errorf("#initLog test - failed to initialize logger, error: %s", err)
230 }
231 return log
232}