blob: cdca13480b413e3a3b3dcbcee04248fc4e08f3f5 [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)
ss412gefcb4522019-12-02 16:59:19 +020043
44 e2tControllerMock := &mocks.E2TControllerMock{}
45
46 e2tControllerMock.On("GetE2TInstances").Return(nil)
ss412gde190682019-10-24 09:29:26 +030047
48 router := mux.NewRouter()
ss412gefcb4522019-12-02 16:59:19 +020049 initializeRoutes(router, rootControllerMock, nodebControllerMock, e2tControllerMock)
50 return router, rootControllerMock, nodebControllerMock, e2tControllerMock
ss412gde190682019-10-24 09:29:26 +030051}
52
ss412gde190682019-10-24 09:29:26 +030053func TestRouteGetNodebIds(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020054 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030055
56 req, err := http.NewRequest("GET", "/v1/nodeb/ids", nil)
57 if err != nil {
58 t.Fatal(err)
59 }
60 rr := httptest.NewRecorder()
61 router.ServeHTTP(rr, req)
62
ss412gefcb4522019-12-02 16:59:19 +020063 nodebControllerMock.AssertNumberOfCalls(t, "GetNodebIdList", 1)
ss412gde190682019-10-24 09:29:26 +030064}
65
66func TestRouteGetNodebRanName(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020067 router, _, nodebControllerMock, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030068
69 req, err := http.NewRequest("GET", "/v1/nodeb/ran1", nil)
70 if err != nil {
71 t.Fatal(err)
72 }
73 rr := httptest.NewRecorder()
74 router.ServeHTTP(rr, req)
75
76 assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code")
77 assert.Equal(t, "ran1", rr.Body.String(), "handler returned wrong body")
ss412gefcb4522019-12-02 16:59:19 +020078 nodebControllerMock.AssertNumberOfCalls(t, "GetNodeb", 1)
ss412gde190682019-10-24 09:29:26 +030079}
80
81func TestRouteGetHealth(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +020082 router, rootControllerMock, _, _ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +030083
84 req, err := http.NewRequest("GET", "/v1/health", 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 rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
ss412gde190682019-10-24 09:29:26 +030092}
93
94func TestRoutePutNodebShutdown(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("PUT", "/v1/nodeb/shutdown", nil)
98 if err != nil {
99 t.Fatal(err)
100 }
101 rr := httptest.NewRecorder()
102 router.ServeHTTP(rr, req)
103
ss412gefcb4522019-12-02 16:59:19 +0200104 nodebControllerMock.AssertNumberOfCalls(t, "Shutdown", 1)
ss412gde190682019-10-24 09:29:26 +0300105}
106
ss412ga07b8592020-06-16 21:51:33 +0300107func TestRoutePutNodebSetGeneralConfiguration(t *testing.T) {
108 router, _, nodebControllerMock, _ := setupRouterAndMocks()
109
110 req, err := http.NewRequest("PUT", "/v1/nodeb/parameters", nil)
111 if err != nil {
112 t.Fatal(err)
113 }
114 rr := httptest.NewRecorder()
115 router.ServeHTTP(rr, req)
116
117 nodebControllerMock.AssertNumberOfCalls(t, "SetGeneralConfiguration", 1)
118}
119
ss412gde190682019-10-24 09:29:26 +0300120func TestRouteNotFound(t *testing.T) {
ss412gefcb4522019-12-02 16:59:19 +0200121 router, _, _,_ := setupRouterAndMocks()
ss412gde190682019-10-24 09:29:26 +0300122
123 req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
124 if err != nil {
125 t.Fatal(err)
126 }
127 rr := httptest.NewRecorder()
128 router.ServeHTTP(rr, req)
129
130 assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
ss412gefcb4522019-12-02 16:59:19 +0200131}
132
133func TestRunError(t *testing.T) {
134 log := initLog(t)
135 err := Run(log, 1234567, &mocks.RootControllerMock{}, &mocks.NodebControllerMock{}, &mocks.E2TControllerMock{})
136 assert.NotNil(t, err)
137}
138
139func TestRun(t *testing.T) {
140 log := initLog(t)
141 _, rootControllerMock, nodebControllerMock, e2tControllerMock := setupRouterAndMocks()
142 go Run(log, 11223, rootControllerMock, nodebControllerMock, e2tControllerMock)
143
144 time.Sleep(time.Millisecond * 100)
145 resp, err := http.Get("http://localhost:11223/v1/health")
146 if err != nil {
147 t.Fatalf("failed to perform GET to http://localhost:11223/v1/health")
148 }
149 assert.Equal(t, 200, resp.StatusCode)
150}
151
152func initLog(t *testing.T) *logger.Logger {
153 log, err := logger.InitLogger(logger.InfoLevel)
154 if err != nil {
155 t.Errorf("#initLog test - failed to initialize logger, error: %s", err)
156 }
157 return log
158}