2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-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).
36 app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
37 "github.com/stretchr/testify/suite"
40 type VespaMgrTestSuite struct {
46 func (suite *VespaMgrTestSuite) SetupSuite() {
47 os.Unsetenv("http_proxy")
48 os.Unsetenv("HTTP_PROXY")
49 suite.vespaMgr = NewVespaMgr()
52 func (suite *VespaMgrTestSuite) TestSubscribexAppNotifications() {
53 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
55 body, _ := ioutil.ReadAll(req.Body)
56 var result map[string]interface{}
57 err := json.Unmarshal([]byte(body), &result)
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"}`))
68 defer testServer.Close()
70 suite.vespaMgr.SubscribeXappNotif(testServer.URL)
71 suite.Equal("deadbeef1234567890", suite.vespaMgr.subscriptionId)
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"}`))
80 defer testServer.Close()
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")
86 suite.vespaMgr.subscriptionId = ""
87 suite.vespaMgr.DoSubscribe(testServer.URL, requestBody)
88 suite.Equal("", suite.vespaMgr.subscriptionId)
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)
97 defer testServer.Close()
99 suite.vespaMgr.subscriptionId = ""
100 suite.vespaMgr.DoSubscribe(testServer.URL, []byte{})
101 suite.Equal("", suite.vespaMgr.subscriptionId)
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),"}`))
110 defer testServer.Close()
112 suite.vespaMgr.subscriptionId = ""
113 suite.vespaMgr.DoSubscribe(testServer.URL, []byte{})
114 suite.Equal("", suite.vespaMgr.subscriptionId)
117 func (suite *VespaMgrTestSuite) TestQueryXAppsConfigOk() {
118 listener, err := net.Listen("tcp", ":0")
121 http.HandleFunc("/test_url/", func(w http.ResponseWriter, r *http.Request) {
124 fmt.Fprintf(w, "reply message")
128 go http.Serve(listener, nil)
130 xappConfig, err := suite.vespaMgr.QueryXappConf("http://" + listener.Addr().String() + "/test_url/")
131 suite.NotNil(xappConfig)
133 suite.Equal(xappConfig, []byte("reply message"))
136 func (suite *VespaMgrTestSuite) TestHandlexAppNotification() {
137 data, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
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)
147 func (suite *VespaMgrTestSuite) TestSubscribexAppNotificationsOnStartup() {
148 suite.vespaMgr.Run(false, false)
149 time.Sleep(2 * time.Second)
151 suite.vespaMgr.Consume(&app.RMRParams{})
152 suite.vespaMgr.StatusCB()
153 suite.vespaMgr.rmrReady = true
154 suite.vespaMgr.StatusCB()
157 func executeRequest(req *http.Request, handleR http.HandlerFunc) *httptest.ResponseRecorder {
158 rr := httptest.NewRecorder()
159 handleR.ServeHTTP(rr, req)
163 func TestVespaMgrTestSuite(t *testing.T) {
164 suite.Run(t, new(VespaMgrTestSuite))
167 func (suite *VespaMgrTestSuite) TestCreateConf() {
168 suite.vespaMgr.CreateConf("/unknown/text.txt", []byte{})
171 func (suite *VespaMgrTestSuite) TestHandleMeasurements() {
172 data, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
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)
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)