Add new license claim
[ric-plt/vespamgr.git] / cmd / vesmgr / httpserver_test.go
1 /*
2  *  Copyright (c) 2019 AT&T Intellectual Property.
3  *  Copyright (c) 2018-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 main
22
23 import (
24         "io/ioutil"
25         "net/http"
26         "os"
27         "strings"
28         "testing"
29
30         "github.com/stretchr/testify/suite"
31 )
32
33 type HTTPServerTestSuite struct {
34         suite.Suite
35         chNotif       chan []byte
36         chSupervision chan chan string
37         server        HTTPServer
38 }
39
40 // suite setup creates the HTTP server
41 func (suite *HTTPServerTestSuite) SetupSuite() {
42         os.Unsetenv("http_proxy")
43         os.Unsetenv("HTTP_PROXY")
44         suite.chNotif = make(chan []byte)
45         suite.chSupervision = make(chan chan string)
46         suite.server = HTTPServer{}
47         suite.server.init(":0")
48         suite.server.start("/vesmgr_notif/", suite.chNotif, suite.chSupervision)
49 }
50
51 func (suite *HTTPServerTestSuite) TestHtppServerSupervisionInvalidOperation() {
52         resp, reply := suite.doPost("http://"+suite.server.addr().String()+SupervisionURL, "supervision")
53         suite.Equal("405 method not allowed\n", reply)
54         suite.Equal(405, resp.StatusCode)
55         suite.Equal("405 Method Not Allowed", resp.Status)
56 }
57
58 func (suite *HTTPServerTestSuite) doGet(url string) (*http.Response, string) {
59         resp, err := http.Get(url)
60         suite.Nil(err)
61
62         defer resp.Body.Close()
63         contents, err := ioutil.ReadAll(resp.Body)
64         suite.Nil(err)
65         return resp, string(contents)
66 }
67
68 func (suite *HTTPServerTestSuite) doPost(serverURL string, msg string) (*http.Response, string) {
69         resp, err := http.Post(serverURL, "data", strings.NewReader(msg))
70         suite.Nil(err)
71
72         defer resp.Body.Close()
73         contents, err := ioutil.ReadAll(resp.Body)
74         suite.Nil(err)
75         return resp, string(contents)
76 }
77
78 func replySupervision(chSupervision chan chan string, reply string) {
79         chSupervisionAck := <-chSupervision
80         chSupervisionAck <- reply
81 }
82
83 func (suite *HTTPServerTestSuite) TestHttpServerSupervision() {
84
85         // start the "main loop" to reply to the supervision to the HTTPServer
86         go replySupervision(suite.chSupervision, "I'm just fine")
87
88         resp, reply := suite.doGet("http://" + suite.server.addr().String() + SupervisionURL)
89
90         suite.Equal("I'm just fine", reply)
91         suite.Equal(200, resp.StatusCode)
92         suite.Equal("200 OK", resp.Status)
93 }
94
95 func (suite *HTTPServerTestSuite) TestHttpServerInvalidUrl() {
96         resp, reply := suite.doPost("http://"+suite.server.addr().String()+"/invalid_url", "foo")
97         suite.Equal("404 page not found\n", reply)
98         suite.Equal(404, resp.StatusCode)
99         suite.Equal("404 Not Found", resp.Status)
100 }
101
102 func readXAppNotification(chNotif chan []byte, ch chan []byte) {
103         notification := <-chNotif
104         ch <- notification
105 }
106
107 func (suite *HTTPServerTestSuite) TestHttpServerXappNotif() {
108         // start the "main loop" to receive the xAppNotification message from the HTTPServer
109         ch := make(chan []byte)
110         go readXAppNotification(suite.chNotif, ch)
111
112         resp, reply := suite.doPost("http://"+suite.server.addr().String()+"/vesmgr_notif/", "test data")
113         suite.Equal("", reply)
114         suite.Equal(200, resp.StatusCode)
115         suite.Equal("200 OK", resp.Status)
116         notification := <-ch
117         suite.Equal([]byte("test data"), notification)
118 }
119
120 func (suite *HTTPServerTestSuite) TestHttpServerXappNotifInvalidOperation() {
121         resp, reply := suite.doGet("http://" + suite.server.addr().String() + "/vesmgr_notif/")
122         suite.Equal("405 method not allowed\n", reply)
123         suite.Equal(405, resp.StatusCode)
124         suite.Equal("405 Method Not Allowed", resp.Status)
125 }
126
127 func TestHttpServerSuite(t *testing.T) {
128         suite.Run(t, new(HTTPServerTestSuite))
129 }