2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
24 "github.com/gorilla/mux"
25 "github.com/stretchr/testify/assert"
33 func setupRouterAndMocks() (*mux.Router, *mocks.RootControllerMock, *mocks.ControllerMock) {
34 rootControllerMock := &mocks.RootControllerMock{}
35 rootControllerMock.On("HandleHealthCheckRequest").Return(nil)
37 controllerMock := &mocks.ControllerMock{}
38 controllerMock.On("ResourceStatus").Return(nil)
40 router := mux.NewRouter()
41 initializeRoutes(router, rootControllerMock, controllerMock)
42 return router, rootControllerMock, controllerMock
44 func TestResourceStatus(t *testing.T) {
45 router, _, controllerMock := setupRouterAndMocks()
47 req, err := http.NewRequest("PUT", "/v1/general/resourcestatus", nil)
51 rr := httptest.NewRecorder()
52 router.ServeHTTP(rr, req)
54 controllerMock.AssertNumberOfCalls(t, "ResourceStatus", 1)
57 func TestRouteGetHealth(t *testing.T) {
58 router, rootControllerMock, _ := setupRouterAndMocks()
60 req, err := http.NewRequest("GET", "/v1/health", nil)
64 rr := httptest.NewRecorder()
65 router.ServeHTTP(rr, req)
67 rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
70 func TestRouteNotFound(t *testing.T) {
71 router, _, _ := setupRouterAndMocks()
73 req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
77 rr := httptest.NewRecorder()
78 router.ServeHTTP(rr, req)
80 assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
83 func TestRunError(t *testing.T) {
84 _, rootControllerMock, controllerMock := setupRouterAndMocks()
86 err := Run(111222333, rootControllerMock, controllerMock)
91 func TestRun(t *testing.T) {
92 _, rootControllerMock, controllerMock := setupRouterAndMocks()
94 go Run(11223, rootControllerMock, controllerMock)
96 time.Sleep(time.Millisecond * 100)
97 resp, err := http.Get("http://localhost:11223/v1/health")
99 t.Fatalf("failed to perform GET to http://localhost:11223/v1/health")
101 assert.Equal(t, 200, resp.StatusCode)