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 ( |
| 24 | "e2mgr/mocks" |
| 25 | "github.com/gorilla/mux" |
| 26 | "github.com/stretchr/testify/assert" |
| 27 | "net/http" |
| 28 | "net/http/httptest" |
| 29 | "testing" |
| 30 | ) |
| 31 | |
| 32 | func setupRouterAndMocks() (*mux.Router, *mocks.ControllerMock, *mocks.NodebControllerMock) { |
| 33 | controllerMock := &mocks.ControllerMock{} |
| 34 | controllerMock.On("Shutdown").Return(nil) |
| 35 | controllerMock.On("X2Reset").Return(nil) |
| 36 | controllerMock.On("X2Setup").Return(nil) |
| 37 | controllerMock.On("EndcSetup").Return(nil) |
| 38 | controllerMock.On("GetNodeb").Return(nil) |
| 39 | controllerMock.On("GetNodebIdList").Return(nil) |
| 40 | |
| 41 | |
| 42 | |
| 43 | nodebControllerMock := &mocks.NodebControllerMock{} |
| 44 | nodebControllerMock.On("GetNodebIdList").Return(nil) |
| 45 | nodebControllerMock.On("GetNodeb").Return(nil) // TODO: remove |
| 46 | nodebControllerMock.On("HandleHealthCheckRequest").Return(nil) |
| 47 | |
| 48 | router := mux.NewRouter() |
| 49 | initializeRoutes(router, nodebControllerMock, controllerMock) |
| 50 | return router, controllerMock, nodebControllerMock |
| 51 | } |
| 52 | |
| 53 | func TestRoutePostEndcSetup(t *testing.T) { |
| 54 | router, controllerMock, _ := setupRouterAndMocks() |
| 55 | |
| 56 | req, err := http.NewRequest("POST", "/v1/nodeb/endc-setup", nil) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | rr := httptest.NewRecorder() |
| 61 | router.ServeHTTP(rr, req) |
| 62 | |
| 63 | controllerMock.AssertNumberOfCalls(t,"EndcSetup", 1) |
| 64 | } |
| 65 | |
| 66 | func TestRoutePostX2Setup(t *testing.T) { |
| 67 | router, controllerMock, _ := setupRouterAndMocks() |
| 68 | |
| 69 | req, err := http.NewRequest("POST", "/v1/nodeb/x2-setup", nil) |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | rr := httptest.NewRecorder() |
| 74 | router.ServeHTTP(rr, req) |
| 75 | |
| 76 | controllerMock.AssertNumberOfCalls(t,"X2Setup", 1) |
| 77 | } |
| 78 | |
| 79 | func TestRouteGetNodebIds(t *testing.T) { |
| 80 | router, controllerMock, _ := setupRouterAndMocks() |
| 81 | |
| 82 | req, err := http.NewRequest("GET", "/v1/nodeb/ids", nil) |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | rr := httptest.NewRecorder() |
| 87 | router.ServeHTTP(rr, req) |
| 88 | |
| 89 | controllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1) |
| 90 | } |
| 91 | |
| 92 | func TestRouteGetNodebRanName(t *testing.T) { |
| 93 | router, controllerMock,_ := setupRouterAndMocks() |
| 94 | |
| 95 | req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil) |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | rr := httptest.NewRecorder() |
| 100 | router.ServeHTTP(rr, req) |
| 101 | |
| 102 | assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code") |
| 103 | assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body") |
| 104 | controllerMock.AssertNumberOfCalls(t, "GetNodeb", 1) |
| 105 | } |
| 106 | |
| 107 | func TestRouteGetHealth(t *testing.T) { |
| 108 | router, _, nodebControllerMock := setupRouterAndMocks() |
| 109 | |
| 110 | req, err := http.NewRequest("GET", "/v1/health", nil) |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | rr := httptest.NewRecorder() |
| 115 | router.ServeHTTP(rr, req) |
| 116 | |
| 117 | nodebControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1) |
| 118 | } |
| 119 | |
| 120 | func TestRoutePutNodebShutdown(t *testing.T) { |
| 121 | router, controllerMock, _ := setupRouterAndMocks() |
| 122 | |
| 123 | req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil) |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | rr := httptest.NewRecorder() |
| 128 | router.ServeHTTP(rr, req) |
| 129 | |
| 130 | controllerMock.AssertNumberOfCalls(t, "Shutdown", 1) |
| 131 | } |
| 132 | |
| 133 | func TestRoutePutNodebResetRanName(t *testing.T) { |
| 134 | router, controllerMock, _ := setupRouterAndMocks() |
| 135 | |
| 136 | req, err := http.NewRequest("PUT", "/v1/nodeb/ran1/reset", nil) |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | rr := httptest.NewRecorder() |
| 141 | router.ServeHTTP(rr, req) |
| 142 | |
| 143 | assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code") |
| 144 | assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body") |
| 145 | controllerMock.AssertNumberOfCalls(t, "X2Reset", 1) |
| 146 | } |
| 147 | |
| 148 | func TestRouteNotFound(t *testing.T) { |
| 149 | router, _, _ := setupRouterAndMocks() |
| 150 | |
| 151 | req, err := http.NewRequest("GET", "/v1/no/such/route", nil) |
| 152 | if err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | rr := httptest.NewRecorder() |
| 156 | router.ServeHTTP(rr, req) |
| 157 | |
| 158 | assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code") |
| 159 | } |