1eb0c5a1d18b0dd2fc106704eeb7befffaf1ab10
[ric-plt/resource-status-manager.git] / RSM / httpserver / http_server_test.go
1 //
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.
16
17 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20
21 package httpserver
22
23 import (
24         "github.com/gorilla/mux"
25         "github.com/stretchr/testify/assert"
26         "net/http"
27         "net/http/httptest"
28         "rsm/mocks"
29         "testing"
30         "time"
31 )
32
33 func setupRouterAndMocks() (*mux.Router, *mocks.RootControllerMock) {
34         rootControllerMock := &mocks.RootControllerMock{}
35         rootControllerMock.On("HandleHealthCheckRequest").Return(nil)
36
37         router := mux.NewRouter()
38         initializeRoutes(router, rootControllerMock)
39         return router, rootControllerMock
40 }
41
42 func TestRouteGetHealth(t *testing.T) {
43         router, rootControllerMock := setupRouterAndMocks()
44
45         req, err := http.NewRequest("GET", "/v1/health", nil)
46         if err != nil {
47                 t.Fatal(err)
48         }
49         rr := httptest.NewRecorder()
50         router.ServeHTTP(rr, req)
51
52         rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
53 }
54
55 func TestRouteNotFound(t *testing.T) {
56         router, _ := setupRouterAndMocks()
57
58         req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
59         if err != nil {
60                 t.Fatal(err)
61         }
62         rr := httptest.NewRecorder()
63         router.ServeHTTP(rr, req)
64
65         assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
66 }
67
68 func TestRunError(t *testing.T) {
69         rootControllerMock := &mocks.RootControllerMock{}
70
71         err := Run(111222333, rootControllerMock)
72
73         assert.NotNil(t, err)
74 }
75
76 func TestRun(t *testing.T) {
77         rootControllerMock := &mocks.RootControllerMock{}
78         rootControllerMock.On("HandleHealthCheckRequest").Return(nil)
79
80         go Run(11223, rootControllerMock)
81
82         time.Sleep(time.Millisecond * 100)
83         resp, err := http.Get("http://localhost:11223/v1/health")
84         if err != nil {
85                 t.Fatalf("failed to perform GET to http://localhost:11223/v1/health")
86         }
87         assert.Equal(t, 200, resp.StatusCode)
88 }