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 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 34 | func setupRouterAndMocks() (*mux.Router, *mocks.RootControllerMock, *mocks.NodebControllerMock, *mocks.E2TControllerMock) { |
| 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{} |
| 50 | |
| 51 | e2tControllerMock.On("GetE2TInstances").Return(nil) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 52 | |
| 53 | router := mux.NewRouter() |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 54 | initializeRoutes(router, rootControllerMock, nodebControllerMock, e2tControllerMock) |
| 55 | return router, rootControllerMock, nodebControllerMock, e2tControllerMock |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 56 | } |
| 57 | |
Rahul Banerji | 25cf760 | 2020-10-06 11:26:30 +0530 | [diff] [blame^] | 58 | func TestRouteGetNodebIdList(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 59 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 60 | |
Irina | a590417 | 2020-07-16 21:21:51 +0300 | [diff] [blame] | 61 | req, err := http.NewRequest("GET", "/v1/nodeb/states", nil) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | rr := httptest.NewRecorder() |
| 66 | router.ServeHTTP(rr, req) |
| 67 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 68 | nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 69 | } |
| 70 | |
Rahul Banerji | 25cf760 | 2020-10-06 11:26:30 +0530 | [diff] [blame^] | 71 | func 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 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 85 | func TestRouteGetNodebRanName(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 86 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 87 | |
| 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") |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 97 | nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | func TestRouteGetHealth(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 101 | router, rootControllerMock, _, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 102 | |
| 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 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 110 | rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | func TestRoutePutNodebShutdown(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 114 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 115 | |
| 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 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 123 | nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 124 | } |
| 125 | |
Rahul Banerji | fe8d481 | 2020-08-24 14:26:41 +0530 | [diff] [blame] | 126 | func 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 Banerji | 25cf760 | 2020-10-06 11:26:30 +0530 | [diff] [blame^] | 136 | assert.Equal(t, http.StatusAccepted, rr.Code, "handler returned wrong status code") |
Rahul Banerji | fe8d481 | 2020-08-24 14:26:41 +0530 | [diff] [blame] | 137 | nodebControllerMock.AssertNumberOfCalls(t, "HealthCheckRequest", 1) |
| 138 | } |
| 139 | |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 140 | func 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 | |
Irina | 5830d4d | 2020-07-08 18:11:04 +0000 | [diff] [blame] | 153 | func 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 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 166 | func TestRouteNotFound(t *testing.T) { |
idanshal | e0106ff | 2020-07-07 16:04:00 +0000 | [diff] [blame] | 167 | router, _, _, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 168 | |
| 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") |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | func 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 | |
| 185 | func 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 | |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame] | 198 | func 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 | |
| 212 | func 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 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 226 | func 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 | } |