blob: 3c0620bb5afbab8ceb0c024ae66e08e062f9ce35 [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)
ss412gefcb4522019-12-02 16:59:19 +020042
43 e2tControllerMock := &mocks.E2TControllerMock{}
44
45 e2tControllerMock.On("GetE2TInstances").Return(nil)
ss412gde190682019-10-24 09:29:26 +030046
47 router := mux.NewRouter()
ss412gefcb4522019-12-02 16:59:19 +020048 initializeRoutes(router, rootControllerMock, nodebControllerMock, e2tControllerMock)
49 return router, rootControllerMock, nodebControllerMock, e2tControllerMock
ss412gde190682019-10-24 09:29:26 +030050}
51
ss412gde190682019-10-24 09:29:26 +030052func TestRouteGetNodebIds(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020053 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030054
55 req, err := http.NewRequest("GET", "/v1/nodeb/ids", nil)
56 if err != nil {
57 t.Fatal(err)
58 }
59 rr := httptest.NewRecorder()
60 router.ServeHTTP(rr, req)
61
ss412gefcb4522019-12-02 16:59:19 +020062 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
ss412gde190682019-10-24 09:29:26 +030063}
64
65func TestRouteGetNodebRanName(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020066 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030067
68 req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil)
69 if err != nil {
70 t.Fatal(err)
71 }
72 rr := httptest.NewRecorder()
73 router.ServeHTTP(rr, req)
74
75 assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code")
76 assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body")
ss412gefcb4522019-12-02 16:59:19 +020077 nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1)
ss412gde190682019-10-24 09:29:26 +030078}
79
80func TestRouteGetHealth(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020081 router, rootControllerMock, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030082
83 req, err := http.NewRequest("GET", "/v1/health", nil)
84 if err != nil {
85 t.Fatal(err)
86 }
87 rr := httptest.NewRecorder()
88 router.ServeHTTP(rr, req)
89
ss412gefcb4522019-12-02 16:59:19 +020090 rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
ss412gde190682019-10-24 09:29:26 +030091}
92
93func TestRoutePutNodebShutdown(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020094 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030095
96 req, err := http.NewRequest("PUT", "/v1/nodeb/shutdown", nil)
97 if err != nil {
98 t.Fatal(err)
99 }
100 rr := httptest.NewRecorder()
101 router.ServeHTTP(rr, req)
102
ss412gefcb4522019-12-02 16:59:19 +0200103 nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1)
ss412gde190682019-10-24 09:29:26 +0300104}
105
ss412gde190682019-10-24 09:29:26 +0300106func TestRouteNotFound(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +0200107 router, _, _,_ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300108
109 req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
110 if err != nil {
111 t.Fatal(err)
112 }
113 rr := httptest.NewRecorder()
114 router.ServeHTTP(rr, req)
115
116 assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
ss412gefcb4522019-12-02 16:59:19 +0200117}
118
119func TestRunError(t *testing.T) {
120 log := initLog(t)
121 err := Run(log, 1234567, &mocks.RootControllerMock{}, &mocks.NodebControllerMock{}, &mocks.E2TControllerMock{})
122 assert.NotNil(t, err)
123}
124
125func TestRun(t *testing.T) {
126 log := initLog(t)
127 _, rootControllerMock, nodebControllerMock, e2tControllerMock := setupRouterAndMocks()
128 go Run(log, 11223, rootControllerMock, nodebControllerMock, e2tControllerMock)
129
130 time.Sleep(time.Millisecond * 100)
131 resp, err := http.Get("http://localhost:11223/v1/health")
132 if err != nil {
133 t.Fatalf("failed to perform GET to http://localhost:11223/v1/health")
134 }
135 assert.Equal(t, 200, resp.StatusCode)
136}
137
138func initLog(t *testing.T) *logger.Logger {
139 log, err := logger.InitLogger(logger.InfoLevel)
140 if err != nil {
141 t.Errorf("#initLog test - failed to initialize logger, error: %s", err)
142 }
143 return log
144}