RIC-710: Adding E2T counters to Vespa-Manager
[ric-plt/vespamgr.git] / cmd / vespamgr / vespamgr_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
22 package main
23
24 import (
25         "bytes"
26         "encoding/json"
27         "fmt"
28         "io/ioutil"
29         "net"
30         "net/http"
31         "net/http/httptest"
32         "os"
33         "testing"
34         "time"
35
36         app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
37         "github.com/stretchr/testify/suite"
38 )
39
40 type VespaMgrTestSuite struct {
41         suite.Suite
42         vespaMgr *VespaMgr
43 }
44
45 // suite setup
46 func (suite *VespaMgrTestSuite) SetupSuite() {
47         os.Unsetenv("http_proxy")
48         os.Unsetenv("HTTP_PROXY")
49         suite.vespaMgr = NewVespaMgr()
50 }
51
52 func (suite *VespaMgrTestSuite) TestSubscribexAppNotifications() {
53         testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
54
55                 body, _ := ioutil.ReadAll(req.Body)
56                 var result map[string]interface{}
57                 err := json.Unmarshal([]byte(body), &result)
58                 suite.Nil(err)
59                 data := result["Data"].(map[string]interface{})
60                 suite.Equal(5, int(data["maxRetries"].(float64)))
61                 suite.Equal(5, int(data["retryTimer"].(float64)))
62                 suite.Equal("all", data["eventType"].(string))
63                 suite.Equal("POST", req.Method)
64                 res.Header().Add("Content-Type", "application/json")
65                 res.WriteHeader(http.StatusCreated)
66                 res.Write([]byte(`{"id":"deadbeef1234567890", "version":0, "eventType":"all"}`))
67         }))
68         defer testServer.Close()
69
70         suite.vespaMgr.SubscribeXappNotif(testServer.URL)
71         suite.Equal("deadbeef1234567890", suite.vespaMgr.subscriptionId)
72 }
73
74 func (suite *VespaMgrTestSuite) TestSubscribexAppNotificationsWrongStatus() {
75         testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
76                 res.Header().Add("Content-Type", "application/json")
77                 res.WriteHeader(http.StatusUnauthorized)
78                 res.Write([]byte(`{"id":"deadbeef1234567890", "version":0, "eventType":"all"}`))
79         }))
80         defer testServer.Close()
81
82         requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, "localhost:8080"))
83         req, _ := http.NewRequest("POST", testServer.URL, bytes.NewBuffer(requestBody))
84         req.Header.Set("Content-Type", "application/json")
85
86         suite.vespaMgr.subscriptionId = ""
87         suite.vespaMgr.DoSubscribe(testServer.URL, requestBody)
88         suite.Equal("", suite.vespaMgr.subscriptionId)
89 }
90
91 func (suite *VespaMgrTestSuite) TestSubscribexAppNotificationsReadBodyFails() {
92         testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
93                 res.Header().Set("Content-Length", "1")
94                 res.Header().Add("Content-Type", "application/json")
95                 res.WriteHeader(http.StatusCreated)
96         }))
97         defer testServer.Close()
98
99         suite.vespaMgr.subscriptionId = ""
100         suite.vespaMgr.DoSubscribe(testServer.URL, []byte{})
101         suite.Equal("", suite.vespaMgr.subscriptionId)
102 }
103
104 func (suite *VespaMgrTestSuite) TestSubscribexAppNotificationsUnMarshalFails() {
105         testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
106                 res.Header().Add("Content-Type", "application/json")
107                 res.WriteHeader(http.StatusCreated)
108                 res.Write([]byte(`{""dump for UT": make(chan int),"}`))
109         }))
110         defer testServer.Close()
111
112         suite.vespaMgr.subscriptionId = ""
113         suite.vespaMgr.DoSubscribe(testServer.URL, []byte{})
114         suite.Equal("", suite.vespaMgr.subscriptionId)
115 }
116
117 func (suite *VespaMgrTestSuite) TestQueryXAppsConfigOk() {
118         listener, err := net.Listen("tcp", ":0")
119         suite.Nil(err)
120
121         http.HandleFunc("/test_url/", func(w http.ResponseWriter, r *http.Request) {
122                 switch r.Method {
123                 case "GET":
124                         fmt.Fprintf(w, "reply message")
125                 }
126         })
127
128         go http.Serve(listener, nil)
129
130         xappConfig, err := suite.vespaMgr.QueryXappConf("http://" + listener.Addr().String() + "/test_url/")
131         suite.NotNil(xappConfig)
132         suite.Nil(err)
133         suite.Equal(xappConfig, []byte("reply message"))
134 }
135
136 func (suite *VespaMgrTestSuite) TestHandlexAppNotification() {
137         data, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
138         suite.Nil(err)
139
140         pbodyEn, _ := json.Marshal(data)
141         req, _ := http.NewRequest("POST", "/ric/v1/xappnotif", bytes.NewBuffer(pbodyEn))
142         handleFunc := http.HandlerFunc(suite.vespaMgr.HandlexAppNotification)
143         response := executeRequest(req, handleFunc)
144         suite.Equal(http.StatusOK, response.Code)
145 }
146
147 func (suite *VespaMgrTestSuite) TestSubscribexAppNotificationsOnStartup() {
148         suite.vespaMgr.Run(false, false)
149         time.Sleep(2 * time.Second)
150
151         suite.vespaMgr.Consume(&app.RMRParams{})
152         suite.vespaMgr.StatusCB()
153         suite.vespaMgr.rmrReady = true
154         suite.vespaMgr.StatusCB()
155 }
156
157 func executeRequest(req *http.Request, handleR http.HandlerFunc) *httptest.ResponseRecorder {
158         rr := httptest.NewRecorder()
159         handleR.ServeHTTP(rr, req)
160         return rr
161 }
162
163 func TestVespaMgrTestSuite(t *testing.T) {
164         suite.Run(t, new(VespaMgrTestSuite))
165 }
166
167 func (suite *VespaMgrTestSuite) TestCreateConf() {
168         suite.vespaMgr.CreateConf("/unknown/text.txt", []byte{})
169 }
170
171 func (suite *VespaMgrTestSuite) TestHandleMeasurements() {
172         data, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
173         suite.Nil(err)
174
175         pbodyEn, _ := json.Marshal(data)
176         req, _ := http.NewRequest("POST", "/ric/v1/measurements", bytes.NewBuffer(pbodyEn))
177         handleFunc := http.HandlerFunc(suite.vespaMgr.HandleMeasurements)
178         response := executeRequest(req, handleFunc)
179         suite.Equal(http.StatusOK, response.Code)
180 }
181
182 func (suite *VespaMgrTestSuite) TestSymptomDataHandler() {
183         req, _ := http.NewRequest("GET", "/ric/v1/symptomdata", nil)
184         handleFunc := http.HandlerFunc(suite.vespaMgr.SymptomDataHandler)
185         resp := executeRequest(req, handleFunc)
186         suite.Equal(http.StatusOK, resp.Code)
187 }