blob: e6e1ef5b5ed965eae13ffec07548e07442f16f79 [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)
40 nodebControllerMock.On("X2Reset").Return(nil)
41 nodebControllerMock.On("X2Setup").Return(nil)
42 nodebControllerMock.On("EndcSetup").Return(nil)
43 nodebControllerMock.On("GetNodeb").Return(nil)
ss412gde190682019-10-24 09:29:26 +030044 nodebControllerMock.On("GetNodebIdList").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
55func TestRoutePostEndcSetup(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("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
ss412gefcb4522019-12-02 16:59:19 +020065 nodebControllerMock.AssertNumberOfCalls(t, "EndcSetup", 1)
ss412gde190682019-10-24 09:29:26 +030066}
67
68func TestRoutePostX2Setup(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("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
ss412gefcb4522019-12-02 16:59:19 +020078 nodebControllerMock.AssertNumberOfCalls(t, "X2Setup", 1)
ss412gde190682019-10-24 09:29:26 +030079}
80
81func TestRouteGetNodebIds(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020082 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030083
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
ss412gefcb4522019-12-02 16:59:19 +020091 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
ss412gde190682019-10-24 09:29:26 +030092}
93
94func TestRouteGetNodebRanName(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020095 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030096
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")
ss412gefcb4522019-12-02 16:59:19 +0200106 nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1)
ss412gde190682019-10-24 09:29:26 +0300107}
108
109func TestRouteGetHealth(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +0200110 router, rootControllerMock, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300111
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
ss412gefcb4522019-12-02 16:59:19 +0200119 rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
ss412gde190682019-10-24 09:29:26 +0300120}
121
122func TestRoutePutNodebShutdown(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +0200123 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300124
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
ss412gefcb4522019-12-02 16:59:19 +0200132 nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1)
ss412gde190682019-10-24 09:29:26 +0300133}
134
135func TestRoutePutNodebResetRanName(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +0200136 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300137
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")
ss412gefcb4522019-12-02 16:59:19 +0200147 nodebControllerMock.AssertNumberOfCalls(t, "X2Reset", 1)
ss412gde190682019-10-24 09:29:26 +0300148}
149
150func TestRouteNotFound(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +0200151 router, _, _,_ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300152
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")
ss412gefcb4522019-12-02 16:59:19 +0200161}
162
163func 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
169func 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
182func 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}