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) {
34 rootControllerMock := &mocks.RootControllerMock{}
35 rootControllerMock.On("HandleHealthCheckRequest").Return(nil)
37 router := mux.NewRouter()
38 initializeRoutes(router, rootControllerMock)
39 return router, rootControllerMock
42 func TestRouteGetHealth(t *testing.T) {
43 router, rootControllerMock := setupRouterAndMocks()
45 req, err := http.NewRequest("GET", "/v1/health", nil)
49 rr := httptest.NewRecorder()
50 router.ServeHTTP(rr, req)
52 rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
55 func TestRouteNotFound(t *testing.T) {
56 router, _ := setupRouterAndMocks()
58 req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
62 rr := httptest.NewRecorder()
63 router.ServeHTTP(rr, req)
65 assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
68 func TestRunError(t *testing.T) {
69 rootControllerMock := &mocks.RootControllerMock{}
71 err := Run(111222333, rootControllerMock)
76 func TestRun(t *testing.T) {
77 rootControllerMock := &mocks.RootControllerMock{}
78 rootControllerMock.On("HandleHealthCheckRequest").Return(nil)
80 go Run(11223, rootControllerMock)
82 time.Sleep(time.Millisecond * 100)
83 resp, err := http.Get("http://localhost:11223/v1/health")
85 t.Fatalf("failed to perform GET to http://localhost:11223/v1/health")
87 assert.Equal(t, 200, resp.StatusCode)