ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2019 AT&T Intellectual Property |
| 3 | // Copyright 2019 Nokia |
Rahul Banerji | 5f208b1 | 2020-10-01 17:00:32 +0530 | [diff] [blame] | 4 | // Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved. |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 5 | // |
| 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. |
nm755n | 15d3982 | 2019-11-28 16:56:00 +0000 | [diff] [blame] | 17 | |
| 18 | // This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | // platform project (RICP). |
| 20 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 21 | package httpserver |
| 22 | |
| 23 | import ( |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 24 | "e2mgr/logger" |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 25 | "e2mgr/mocks" |
| 26 | "github.com/gorilla/mux" |
| 27 | "github.com/stretchr/testify/assert" |
| 28 | "net/http" |
| 29 | "net/http/httptest" |
| 30 | "testing" |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 31 | "time" |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 32 | ) |
| 33 | |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 34 | func setupRouterAndMocks() (*mux.Router, *mocks.RootControllerMock, *mocks.NodebControllerMock, *mocks.E2TControllerMock, *mocks.SymptomdataControllerMock) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 35 | rootControllerMock := &mocks.RootControllerMock{} |
| 36 | rootControllerMock.On("HandleHealthCheckRequest").Return(nil) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 37 | |
| 38 | nodebControllerMock := &mocks.NodebControllerMock{} |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 39 | nodebControllerMock.On("Shutdown").Return(nil) |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 40 | nodebControllerMock.On("GetNodeb").Return(nil) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 41 | nodebControllerMock.On("GetNodebIdList").Return(nil) |
Rahul Banerji | 25cf760 | 2020-10-06 11:26:30 +0530 | [diff] [blame] | 42 | nodebControllerMock.On("GetNodebId").Return(nil) |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 43 | nodebControllerMock.On("SetGeneralConfiguration").Return(nil) |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame] | 44 | nodebControllerMock.On("DeleteEnb").Return(nil) |
| 45 | nodebControllerMock.On("AddEnb").Return(nil) |
Irina | 5830d4d | 2020-07-08 18:11:04 +0000 | [diff] [blame] | 46 | nodebControllerMock.On("UpdateEnb").Return(nil) |
Rahul Banerji | fe8d481 | 2020-08-24 14:26:41 +0530 | [diff] [blame] | 47 | nodebControllerMock.On("HealthCheckRequest").Return(nil) |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 48 | |
| 49 | e2tControllerMock := &mocks.E2TControllerMock{} |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 50 | e2tControllerMock.On("GetE2TInstances").Return(nil) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 51 | |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 52 | symptomdataControllerMock := &mocks.SymptomdataControllerMock{} |
| 53 | symptomdataControllerMock.On("GetSymptomData").Return(nil) |
| 54 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 55 | router := mux.NewRouter() |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 56 | initializeRoutes(router, rootControllerMock, nodebControllerMock, e2tControllerMock, symptomdataControllerMock) |
| 57 | return router, rootControllerMock, nodebControllerMock, e2tControllerMock, symptomdataControllerMock |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 58 | } |
| 59 | |
Rahul Banerji | 25cf760 | 2020-10-06 11:26:30 +0530 | [diff] [blame] | 60 | func TestRouteGetNodebIdList(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 61 | router, _, nodebControllerMock, _, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 62 | |
Irina | a590417 | 2020-07-16 21:21:51 +0300 | [diff] [blame] | 63 | req, err := http.NewRequest("GET", "/v1/nodeb/states", nil) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | rr := httptest.NewRecorder() |
| 68 | router.ServeHTTP(rr, req) |
| 69 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 70 | nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 71 | } |
| 72 | |
Rahul Banerji | 25cf760 | 2020-10-06 11:26:30 +0530 | [diff] [blame] | 73 | func TestRouteGetNodebId(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 74 | router, _, nodebControllerMock, _, _ := setupRouterAndMocks() |
Rahul Banerji | 25cf760 | 2020-10-06 11:26:30 +0530 | [diff] [blame] | 75 | |
| 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 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 87 | func TestRouteGetNodebRanName(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 88 | router, _, nodebControllerMock, _, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 89 | |
| 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") |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 99 | nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | func TestRouteGetHealth(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 103 | router, rootControllerMock, _, _, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 104 | |
| 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 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 112 | rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | func TestRoutePutNodebShutdown(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 116 | router, _, nodebControllerMock, _, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 117 | |
| 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 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 125 | nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 126 | } |
| 127 | |
Rahul Banerji | fe8d481 | 2020-08-24 14:26:41 +0530 | [diff] [blame] | 128 | func TestHealthCheckRequest(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 129 | router, _, nodebControllerMock, _, _ := setupRouterAndMocks() |
Rahul Banerji | fe8d481 | 2020-08-24 14:26:41 +0530 | [diff] [blame] | 130 | |
| 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 Banerji | 25cf760 | 2020-10-06 11:26:30 +0530 | [diff] [blame] | 138 | assert.Equal(t, http.StatusAccepted, rr.Code, "handler returned wrong status code") |
Rahul Banerji | fe8d481 | 2020-08-24 14:26:41 +0530 | [diff] [blame] | 139 | nodebControllerMock.AssertNumberOfCalls(t, "HealthCheckRequest", 1) |
| 140 | } |
| 141 | |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 142 | func TestRoutePutNodebSetGeneralConfiguration(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 143 | router, _, nodebControllerMock, _, _ := setupRouterAndMocks() |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 144 | |
| 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 | |
Irina | 5830d4d | 2020-07-08 18:11:04 +0000 | [diff] [blame] | 155 | func TestRoutePutUpdateEnb(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 156 | router, _, nodebControllerMock, _, _ := setupRouterAndMocks() |
Irina | 5830d4d | 2020-07-08 18:11:04 +0000 | [diff] [blame] | 157 | |
| 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 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 168 | func TestRouteNotFound(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 169 | router, _, _, _, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 170 | |
| 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") |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | func TestRunError(t *testing.T) { |
| 182 | log := initLog(t) |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 183 | err := Run(log, 1234567, &mocks.RootControllerMock{}, &mocks.NodebControllerMock{}, &mocks.E2TControllerMock{}, &mocks.SymptomdataControllerMock{}) |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 184 | assert.NotNil(t, err) |
| 185 | } |
| 186 | |
| 187 | func TestRun(t *testing.T) { |
| 188 | log := initLog(t) |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 189 | _, rootControllerMock, nodebControllerMock, e2tControllerMock, symptomdataControllerMock := setupRouterAndMocks() |
| 190 | go Run(log, 11223, rootControllerMock, nodebControllerMock, e2tControllerMock, symptomdataControllerMock) |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 191 | |
| 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 | |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame] | 200 | func TestRouteAddEnb(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 201 | router, _, nodebControllerMock, _, _ := setupRouterAndMocks() |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame] | 202 | |
| 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 | |
| 214 | func TestRouteDeleteEnb(t *testing.T) { |
Mohamed Abukar | 7adc947 | 2021-03-22 09:44:05 +0200 | [diff] [blame] | 215 | router, _, nodebControllerMock, _, _ := setupRouterAndMocks() |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame] | 216 | |
| 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 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 228 | func initLog(t *testing.T) *logger.Logger { |
rimverma | 4cd92f9 | 2023-04-25 11:51:14 +0530 | [diff] [blame] | 229 | InfoLevel := int8(3) |
| 230 | log, err := logger.InitLogger(InfoLevel) |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 231 | if err != nil { |
| 232 | t.Errorf("#initLog test - failed to initialize logger, error: %s", err) |
| 233 | } |
| 234 | return log |
| 235 | } |