ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2019 AT&T Intellectual Property |
| 3 | // Copyright 2019 Nokia |
| 4 | // |
| 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 |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 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. |
nm755n | 15d3982 | 2019-11-28 16:56:00 +0000 | [diff] [blame] | 16 | |
| 17 | // This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | // platform project (RICP). |
| 19 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 20 | |
| 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) |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 42 | nodebControllerMock.On("SetGeneralConfiguration").Return(nil) |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame^] | 43 | nodebControllerMock.On("DeleteEnb").Return(nil) |
| 44 | nodebControllerMock.On("AddEnb").Return(nil) |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 45 | |
| 46 | e2tControllerMock := &mocks.E2TControllerMock{} |
| 47 | |
| 48 | e2tControllerMock.On("GetE2TInstances").Return(nil) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 49 | |
| 50 | router := mux.NewRouter() |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 51 | initializeRoutes(router, rootControllerMock, nodebControllerMock, e2tControllerMock) |
| 52 | return router, rootControllerMock, nodebControllerMock, e2tControllerMock |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 53 | } |
| 54 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 55 | func TestRouteGetNodebIds(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 56 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 57 | |
| 58 | req, err := http.NewRequest("GET", "/v1/nodeb/ids", nil) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | rr := httptest.NewRecorder() |
| 63 | router.ServeHTTP(rr, req) |
| 64 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 65 | nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | func TestRouteGetNodebRanName(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 69 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 70 | |
| 71 | req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | rr := httptest.NewRecorder() |
| 76 | router.ServeHTTP(rr, req) |
| 77 | |
| 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") |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 80 | nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | func TestRouteGetHealth(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 84 | router, rootControllerMock, _, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 85 | |
| 86 | req, err := http.NewRequest("GET", "/v1/health", nil) |
| 87 | if err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | rr := httptest.NewRecorder() |
| 91 | router.ServeHTTP(rr, req) |
| 92 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 93 | rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | func TestRoutePutNodebShutdown(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 97 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 98 | |
| 99 | req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | rr := httptest.NewRecorder() |
| 104 | router.ServeHTTP(rr, req) |
| 105 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 106 | nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 107 | } |
| 108 | |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 109 | func TestRoutePutNodebSetGeneralConfiguration(t *testing.T) { |
| 110 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
| 111 | |
| 112 | req, err := http.NewRequest("PUT", "/v1/nodeb/parameters", nil) |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | rr := httptest.NewRecorder() |
| 117 | router.ServeHTTP(rr, req) |
| 118 | |
| 119 | nodebControllerMock.AssertNumberOfCalls(t, "SetGeneralConfiguration", 1) |
| 120 | } |
| 121 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 122 | func TestRouteNotFound(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 123 | router, _, _,_ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 124 | |
| 125 | req, err := http.NewRequest("GET", "/v1/no/such/route", nil) |
| 126 | if err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | rr := httptest.NewRecorder() |
| 130 | router.ServeHTTP(rr, req) |
| 131 | |
| 132 | assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code") |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | func TestRunError(t *testing.T) { |
| 136 | log := initLog(t) |
| 137 | err := Run(log, 1234567, &mocks.RootControllerMock{}, &mocks.NodebControllerMock{}, &mocks.E2TControllerMock{}) |
| 138 | assert.NotNil(t, err) |
| 139 | } |
| 140 | |
| 141 | func TestRun(t *testing.T) { |
| 142 | log := initLog(t) |
| 143 | _, rootControllerMock, nodebControllerMock, e2tControllerMock := setupRouterAndMocks() |
| 144 | go Run(log, 11223, rootControllerMock, nodebControllerMock, e2tControllerMock) |
| 145 | |
| 146 | time.Sleep(time.Millisecond * 100) |
| 147 | resp, err := http.Get("http://localhost:11223/v1/health") |
| 148 | if err != nil { |
| 149 | t.Fatalf("failed to perform GET to http://localhost:11223/v1/health") |
| 150 | } |
| 151 | assert.Equal(t, 200, resp.StatusCode) |
| 152 | } |
| 153 | |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame^] | 154 | func TestRouteAddEnb(t *testing.T) { |
| 155 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
| 156 | |
| 157 | req, err := http.NewRequest("POST", "/v1/nodeb/enb", nil) |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | rr := httptest.NewRecorder() |
| 162 | router.ServeHTTP(rr, req) |
| 163 | |
| 164 | assert.Equal(t, http.StatusCreated, rr.Code, "handler returned wrong status code") |
| 165 | nodebControllerMock.AssertNumberOfCalls(t, "AddEnb", 1) |
| 166 | } |
| 167 | |
| 168 | func TestRouteDeleteEnb(t *testing.T) { |
| 169 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
| 170 | |
| 171 | req, err := http.NewRequest("DELETE", "/v1/nodeb/enb/ran1", 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.StatusNoContent, rr.Code, "handler returned wrong status code") |
| 179 | nodebControllerMock.AssertNumberOfCalls(t, "DeleteEnb", 1) |
| 180 | } |
| 181 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 182 | func initLog(t *testing.T) *logger.Logger { |
| 183 | log, err := logger.InitLogger(logger.InfoLevel) |
| 184 | if err != nil { |
| 185 | t.Errorf("#initLog test - failed to initialize logger, error: %s", err) |
| 186 | } |
| 187 | return log |
| 188 | } |