blob: 7103ff8f724403c14f93788cb1c2dcb6efb7d5a4 [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
Mohamed Abukar7adc9472021-03-22 09:44:05 +020034func setupRouterAndMocks() (*mux.Router, *mocks.RootControllerMock, *mocks.NodebControllerMock, *mocks.E2TControllerMock, *mocks.SymptomdataControllerMock) {
ss412gefcb4522019-12-02 16:59:19 +020035 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{}
ss412gefcb4522019-12-02 16:59:19 +020050 e2tControllerMock.On("GetE2TInstances").Return(nil)
ss412gde190682019-10-24 09:29:26 +030051
Mohamed Abukar7adc9472021-03-22 09:44:05 +020052 symptomdataControllerMock := &mocks.SymptomdataControllerMock{}
53 symptomdataControllerMock.On("GetSymptomData").Return(nil)
54
ss412gde190682019-10-24 09:29:26 +030055 router := mux.NewRouter()
Mohamed Abukar7adc9472021-03-22 09:44:05 +020056 initializeRoutes(router, rootControllerMock, nodebControllerMock, e2tControllerMock, symptomdataControllerMock)
57 return router, rootControllerMock, nodebControllerMock, e2tControllerMock, symptomdataControllerMock
ss412gde190682019-10-24 09:29:26 +030058}
59
Rahul Banerji25cf7602020-10-06 11:26:30 +053060func TestRouteGetNodebIdList(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +020061 router, _, nodebControllerMock, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030062
Irinaa5904172020-07-16 21:21:51 +030063 req, err := http.NewRequest("GET", "/v1/nodeb/states", nil)
ss412gde190682019-10-24 09:29:26 +030064 if err != nil {
65 t.Fatal(err)
66 }
67 rr := httptest.NewRecorder()
68 router.ServeHTTP(rr, req)
69
ss412gefcb4522019-12-02 16:59:19 +020070 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
ss412gde190682019-10-24 09:29:26 +030071}
72
Rahul Banerji25cf7602020-10-06 11:26:30 +053073func TestRouteGetNodebId(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +020074 router, _, nodebControllerMock, _, _ := setupRouterAndMocks()
Rahul Banerji25cf7602020-10-06 11:26:30 +053075
76 req, err := http.NewRequest("GET", "/v1/nodeb/states/ran1", nil)
77 if err != nil {
78 t.Fatal(err)
79 }
80 rr := httptest.NewRecorder()
81 router.ServeHTTP(rr, req)
82
83 assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code")
84 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebId", 1)
85}
86
ss412gde190682019-10-24 09:29:26 +030087func TestRouteGetNodebRanName(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +020088 router, _, nodebControllerMock, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030089
90 req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil)
91 if err != nil {
92 t.Fatal(err)
93 }
94 rr := httptest.NewRecorder()
95 router.ServeHTTP(rr, req)
96
97 assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code")
98 assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body")
ss412gefcb4522019-12-02 16:59:19 +020099 nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1)
ss412gde190682019-10-24 09:29:26 +0300100}
101
102func TestRouteGetHealth(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +0200103 router, rootControllerMock, _, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300104
105 req, err := http.NewRequest("GET", "/v1/health", nil)
106 if err != nil {
107 t.Fatal(err)
108 }
109 rr := httptest.NewRecorder()
110 router.ServeHTTP(rr, req)
111
ss412gefcb4522019-12-02 16:59:19 +0200112 rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
ss412gde190682019-10-24 09:29:26 +0300113}
114
115func TestRoutePutNodebShutdown(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +0200116 router, _, nodebControllerMock, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300117
118 req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil)
119 if err != nil {
120 t.Fatal(err)
121 }
122 rr := httptest.NewRecorder()
123 router.ServeHTTP(rr, req)
124
ss412gefcb4522019-12-02 16:59:19 +0200125 nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1)
ss412gde190682019-10-24 09:29:26 +0300126}
127
Rahul Banerjife8d4812020-08-24 14:26:41 +0530128func TestHealthCheckRequest(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +0200129 router, _, nodebControllerMock, _, _ := setupRouterAndMocks()
Rahul Banerjife8d4812020-08-24 14:26:41 +0530130
131 req, err := http.NewRequest("PUT", "/v1/nodeb/health", nil)
132 if err != nil {
133 t.Fatal(err)
134 }
135 rr := httptest.NewRecorder()
136 router.ServeHTTP(rr, req)
137
Rahul Banerji25cf7602020-10-06 11:26:30 +0530138 assert.Equal(t, http.StatusAccepted, rr.Code, "handler returned wrong status code")
Rahul Banerjife8d4812020-08-24 14:26:41 +0530139 nodebControllerMock.AssertNumberOfCalls(t, "HealthCheckRequest", 1)
140}
141
ss412ga07b8592020-06-16 21:51:33 +0300142func TestRoutePutNodebSetGeneralConfiguration(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +0200143 router, _, nodebControllerMock, _, _ := setupRouterAndMocks()
ss412ga07b8592020-06-16 21:51:33 +0300144
145 req, err := http.NewRequest("PUT", "/v1/nodeb/parameters", nil)
146 if err != nil {
147 t.Fatal(err)
148 }
149 rr := httptest.NewRecorder()
150 router.ServeHTTP(rr, req)
151
152 nodebControllerMock.AssertNumberOfCalls(t, "SetGeneralConfiguration", 1)
153}
154
Irina5830d4d2020-07-08 18:11:04 +0000155func TestRoutePutUpdateEnb(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +0200156 router, _, nodebControllerMock, _, _ := setupRouterAndMocks()
Irina5830d4d2020-07-08 18:11:04 +0000157
158 req, err := http.NewRequest("PUT", "/v1/nodeb/enb/ran1", nil)
159 if err != nil {
160 t.Fatal(err)
161 }
162 rr := httptest.NewRecorder()
163 router.ServeHTTP(rr, req)
164
165 nodebControllerMock.AssertNumberOfCalls(t, "UpdateEnb", 1)
166}
167
ss412gde190682019-10-24 09:29:26 +0300168func TestRouteNotFound(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +0200169 router, _, _, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300170
171 req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
172 if err != nil {
173 t.Fatal(err)
174 }
175 rr := httptest.NewRecorder()
176 router.ServeHTTP(rr, req)
177
178 assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
ss412gefcb4522019-12-02 16:59:19 +0200179}
180
181func TestRunError(t *testing.T) {
182 log := initLog(t)
Mohamed Abukar7adc9472021-03-22 09:44:05 +0200183 err := Run(log, 1234567, &mocks.RootControllerMock{}, &mocks.NodebControllerMock{}, &mocks.E2TControllerMock{}, &mocks.SymptomdataControllerMock{})
ss412gefcb4522019-12-02 16:59:19 +0200184 assert.NotNil(t, err)
185}
186
187func TestRun(t *testing.T) {
188 log := initLog(t)
Mohamed Abukar7adc9472021-03-22 09:44:05 +0200189 _, rootControllerMock, nodebControllerMock, e2tControllerMock, symptomdataControllerMock := setupRouterAndMocks()
190 go Run(log, 11223, rootControllerMock, nodebControllerMock, e2tControllerMock, symptomdataControllerMock)
ss412gefcb4522019-12-02 16:59:19 +0200191
192 time.Sleep(time.Millisecond * 100)
193 resp, err := http.Get("http://localhost:11223/v1/health")
194 if err != nil {
195 t.Fatalf("failed to perform GET to http://localhost:11223/v1/health")
196 }
197 assert.Equal(t, 200, resp.StatusCode)
198}
199
Amichai7e2e4932020-07-07 15:49:07 +0000200func TestRouteAddEnb(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +0200201 router, _, nodebControllerMock, _, _ := setupRouterAndMocks()
Amichai7e2e4932020-07-07 15:49:07 +0000202
203 req, err := http.NewRequest("POST", "/v1/nodeb/enb", nil)
204 if err != nil {
205 t.Fatal(err)
206 }
207 rr := httptest.NewRecorder()
208 router.ServeHTTP(rr, req)
209
210 assert.Equal(t, http.StatusCreated, rr.Code, "handler returned wrong status code")
211 nodebControllerMock.AssertNumberOfCalls(t, "AddEnb", 1)
212}
213
214func TestRouteDeleteEnb(t *testing.T) {
Mohamed Abukar7adc9472021-03-22 09:44:05 +0200215 router, _, nodebControllerMock, _, _ := setupRouterAndMocks()
Amichai7e2e4932020-07-07 15:49:07 +0000216
217 req, err := http.NewRequest("DELETE", "/v1/nodeb/enb/ran1", nil)
218 if err != nil {
219 t.Fatal(err)
220 }
221 rr := httptest.NewRecorder()
222 router.ServeHTTP(rr, req)
223
224 assert.Equal(t, http.StatusNoContent, rr.Code, "handler returned wrong status code")
225 nodebControllerMock.AssertNumberOfCalls(t, "DeleteEnb", 1)
226}
227
ss412gefcb4522019-12-02 16:59:19 +0200228func initLog(t *testing.T) *logger.Logger {
rimverma4cd92f92023-04-25 11:51:14 +0530229 InfoLevel := int8(3)
230 log, err := logger.InitLogger(InfoLevel)
ss412gefcb4522019-12-02 16:59:19 +0200231 if err != nil {
232 t.Errorf("#initLog test - failed to initialize logger, error: %s", err)
233 }
234 return log
235}