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) |
| 40 | nodebControllerMock.On("X2Reset").Return(nil) |
| 41 | nodebControllerMock.On("X2Setup").Return(nil) |
| 42 | nodebControllerMock.On("EndcSetup").Return(nil) |
| 43 | nodebControllerMock.On("GetNodeb").Return(nil) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 44 | nodebControllerMock.On("GetNodebIdList").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 | |
| 55 | func TestRoutePostEndcSetup(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("POST", "/v1/nodeb/endc-setup", 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, "EndcSetup", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | func TestRoutePostX2Setup(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("POST", "/v1/nodeb/x2-setup", nil) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | rr := httptest.NewRecorder() |
| 76 | router.ServeHTTP(rr, req) |
| 77 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 78 | nodebControllerMock.AssertNumberOfCalls(t, "X2Setup", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | func TestRouteGetNodebIds(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 82 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 83 | |
| 84 | req, err := http.NewRequest("GET", "/v1/nodeb/ids", nil) |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | rr := httptest.NewRecorder() |
| 89 | router.ServeHTTP(rr, req) |
| 90 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 91 | nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func TestRouteGetNodebRanName(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 95 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 96 | |
| 97 | req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | rr := httptest.NewRecorder() |
| 102 | router.ServeHTTP(rr, req) |
| 103 | |
| 104 | assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code") |
| 105 | assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body") |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 106 | nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | func TestRouteGetHealth(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 110 | router, rootControllerMock, _, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 111 | |
| 112 | req, err := http.NewRequest("GET", "/v1/health", nil) |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | rr := httptest.NewRecorder() |
| 117 | router.ServeHTTP(rr, req) |
| 118 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 119 | rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | func TestRoutePutNodebShutdown(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 123 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 124 | |
| 125 | req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil) |
| 126 | if err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | rr := httptest.NewRecorder() |
| 130 | router.ServeHTTP(rr, req) |
| 131 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 132 | nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | func TestRoutePutNodebResetRanName(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 136 | router, _, nodebControllerMock, _ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 137 | |
| 138 | req, err := http.NewRequest("PUT", "/v1/nodeb/ran1/reset", nil) |
| 139 | if err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | rr := httptest.NewRecorder() |
| 143 | router.ServeHTTP(rr, req) |
| 144 | |
| 145 | assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code") |
| 146 | assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body") |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 147 | nodebControllerMock.AssertNumberOfCalls(t, "X2Reset", 1) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | func TestRouteNotFound(t *testing.T) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 151 | router, _, _,_ := setupRouterAndMocks() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 152 | |
| 153 | req, err := http.NewRequest("GET", "/v1/no/such/route", nil) |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | rr := httptest.NewRecorder() |
| 158 | router.ServeHTTP(rr, req) |
| 159 | |
| 160 | assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code") |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | func TestRunError(t *testing.T) { |
| 164 | log := initLog(t) |
| 165 | err := Run(log, 1234567, &mocks.RootControllerMock{}, &mocks.NodebControllerMock{}, &mocks.E2TControllerMock{}) |
| 166 | assert.NotNil(t, err) |
| 167 | } |
| 168 | |
| 169 | func TestRun(t *testing.T) { |
| 170 | log := initLog(t) |
| 171 | _, rootControllerMock, nodebControllerMock, e2tControllerMock := setupRouterAndMocks() |
| 172 | go Run(log, 11223, rootControllerMock, nodebControllerMock, e2tControllerMock) |
| 173 | |
| 174 | time.Sleep(time.Millisecond * 100) |
| 175 | resp, err := http.Get("http://localhost:11223/v1/health") |
| 176 | if err != nil { |
| 177 | t.Fatalf("failed to perform GET to http://localhost:11223/v1/health") |
| 178 | } |
| 179 | assert.Equal(t, 200, resp.StatusCode) |
| 180 | } |
| 181 | |
| 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 | } |