Add header missing license header
[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, *mocks.ControllerMock) {
34         rootControllerMock := &mocks.RootControllerMock{}
35         rootControllerMock.On("HandleHealthCheckRequest").Return(nil)
36
37         controllerMock := &mocks.ControllerMock{}
38         controllerMock.On("ResourceStatus").Return(nil)
39
40         router := mux.NewRouter()
41         initializeRoutes(router, rootControllerMock, controllerMock)
42         return router, rootControllerMock, controllerMock
43 }
44 func TestResourceStatus(t *testing.T) {
45         router, _, controllerMock := setupRouterAndMocks()
46
47         req, err := http.NewRequest("PUT", "/v1/general/resourcestatus", nil)
48         if err != nil {
49                 t.Fatal(err)
50         }
51         rr := httptest.NewRecorder()
52         router.ServeHTTP(rr, req)
53
54         controllerMock.AssertNumberOfCalls(t, "ResourceStatus", 1)
55 }
56
57 func TestRouteGetHealth(t *testing.T) {
58         router, rootControllerMock, _ := setupRouterAndMocks()
59
60         req, err := http.NewRequest("GET", "/v1/health", nil)
61         if err != nil {
62                 t.Fatal(err)
63         }
64         rr := httptest.NewRecorder()
65         router.ServeHTTP(rr, req)
66
67         rootControllerMock.AssertNumberOfCalls(t, "HandleHealthCheckRequest", 1)
68 }
69
70 func TestRouteNotFound(t *testing.T) {
71         router, _, _ := setupRouterAndMocks()
72
73         req, err := http.NewRequest("GET", "/v1/no/such/route", nil)
74         if err != nil {
75                 t.Fatal(err)
76         }
77         rr := httptest.NewRecorder()
78         router.ServeHTTP(rr, req)
79
80         assert.Equal(t, http.StatusNotFound, rr.Code, "handler returned wrong status code")
81 }
82
83 func TestRunError(t *testing.T) {
84         _, rootControllerMock, controllerMock := setupRouterAndMocks()
85
86         err := Run(111222333, rootControllerMock, controllerMock)
87
88         assert.NotNil(t, err)
89 }
90
91 func TestRun(t *testing.T) {
92         _, rootControllerMock, controllerMock := setupRouterAndMocks()
93
94         go Run(11223, rootControllerMock, controllerMock)
95
96         time.Sleep(time.Millisecond * 100)
97         resp, err := http.Get("http://localhost:11223/v1/health")
98         if err != nil {
99                 t.Fatalf("failed to perform GET to http://localhost:11223/v1/health")
100         }
101         assert.Equal(t, 200, resp.StatusCode)
102 }