blob: 05173f1e03d4ec64b2aef73f9e940fcefc8d7e79 [file] [log] [blame]
ss412gde190682019-10-24 09:29:26 +03001//
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.
nm755n15d39822019-11-28 16:56:00 +000016
17// This source code is part of the near-RT RIC (RAN Intelligent Controller)
18// platform project (RICP).
19
ss412gde190682019-10-24 09:29:26 +030020
21package httpserver
22
23import (
ss412gefcb4522019-12-02 16:59:19 +020024 "e2mgr/logger"
ss412gde190682019-10-24 09:29:26 +030025 "e2mgr/mocks"
26 "github.com/gorilla/mux"
27 "github.com/stretchr/testify/assert"
28 "net/http"
29 "net/http/httptest"
30 "testing"
ss412gefcb4522019-12-02 16:59:19 +020031 "time"
ss412gde190682019-10-24 09:29:26 +030032)
33
ss412gefcb4522019-12-02 16:59:19 +020034func setupRouterAndMocks() (*mux.Router, *mocks.RootControllerMock, *mocks.NodebControllerMock, *mocks.E2TControllerMock) {
35 rootControllerMock := &mocks.RootControllerMock{}
36 rootControllerMock.On("HandleHealthCheckRequest").Return(nil)
ss412gde190682019-10-24 09:29:26 +030037
38 nodebControllerMock := &mocks.NodebControllerMock{}
ss412gefcb4522019-12-02 16:59:19 +020039 nodebControllerMock.On("Shutdown").Return(nil)
ss412gefcb4522019-12-02 16:59:19 +020040 nodebControllerMock.On("GetNodeb").Return(nil)
ss412gde190682019-10-24 09:29:26 +030041 nodebControllerMock.On("GetNodebIdList").Return(nil)
ss412ga07b8592020-06-16 21:51:33 +030042 nodebControllerMock.On("SetGeneralConfiguration").Return(nil)
Amichai7e2e4932020-07-07 15:49:07 +000043 nodebControllerMock.On("DeleteEnb").Return(nil)
44 nodebControllerMock.On("AddEnb").Return(nil)
ss412gefcb4522019-12-02 16:59:19 +020045
46 e2tControllerMock := &mocks.E2TControllerMock{}
47
48 e2tControllerMock.On("GetE2TInstances").Return(nil)
ss412gde190682019-10-24 09:29:26 +030049
50 router := mux.NewRouter()
ss412gefcb4522019-12-02 16:59:19 +020051 initializeRoutes(router, rootControllerMock, nodebControllerMock, e2tControllerMock)
52 return router, rootControllerMock, nodebControllerMock, e2tControllerMock
ss412gde190682019-10-24 09:29:26 +030053}
54
ss412gde190682019-10-24 09:29:26 +030055func TestRouteGetNodebIds(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020056 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030057
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
ss412gefcb4522019-12-02 16:59:19 +020065 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
ss412gde190682019-10-24 09:29:26 +030066}
67
68func TestRouteGetNodebRanName(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020069 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030070
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")
ss412gefcb4522019-12-02 16:59:19 +020080 nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1)
ss412gde190682019-10-24 09:29:26 +030081}
82
83func TestRouteGetHealth(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020084 router, rootControllerMock, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030085
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
ss412gefcb4522019-12-02 16:59:19 +020093 rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
ss412gde190682019-10-24 09:29:26 +030094}
95
96func TestRoutePutNodebShutdown(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020097 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030098
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
ss412gefcb4522019-12-02 16:59:19 +0200106 nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1)
ss412gde190682019-10-24 09:29:26 +0300107}
108
ss412ga07b8592020-06-16 21:51:33 +0300109func 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
ss412gde190682019-10-24 09:29:26 +0300122func TestRouteNotFound(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +0200123 router, _, _,_ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300124
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")
ss412gefcb4522019-12-02 16:59:19 +0200133}
134
135func 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
141func 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
Amichai7e2e4932020-07-07 15:49:07 +0000154func 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
168func 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
ss412gefcb4522019-12-02 16:59:19 +0200182func 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}