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).
33 "github.com/stretchr/testify/suite"
36 type AppmgrHTTPServerTestSuite struct {
38 subscriptions chan subscriptionNotification
43 func (suite *AppmgrHTTPServerTestSuite) SetupSuite() {
44 // the url here is not actually used anywhere
45 suite.xappNotifURL = "http://127.0.0.1:8080" + vesmgrXappNotifPath
46 suite.subscriptions = make(chan subscriptionNotification)
50 func (suite *AppmgrHTTPServerTestSuite) SetupTest() {
51 suite.subscriptions = make(chan subscriptionNotification)
54 func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotifications() {
55 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
56 body, _ := ioutil.ReadAll(req.Body)
57 var result map[string]interface{}
58 err := json.Unmarshal([]byte(body), &result)
60 suite.Equal(5, int(result["maxRetries"].(float64)))
61 suite.Equal(5, int(result["retryTimer"].(float64)))
62 suite.Equal("all", result["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 go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL)
71 isSubscribed := <-suite.subscriptions
72 suite.Nil(isSubscribed.err)
73 suite.Equal("deadbeef1234567890", isSubscribed.subsID)
76 func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsWrongStatus() {
77 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
78 res.Header().Add("Content-Type", "application/json")
79 res.WriteHeader(http.StatusUnauthorized)
80 res.Write([]byte(`{"id":"deadbeef1234567890", "version":0, "eventType":"all"}`))
82 defer testServer.Close()
84 requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, suite.xappNotifURL))
85 req, _ := http.NewRequest("POST", testServer.URL, bytes.NewBuffer(requestBody))
86 req.Header.Set("Content-Type", "application/json")
87 client := &http.Client{}
89 subsID, err := subscribexAppNotificationsClientDo(req, client)
90 suite.Equal(errWrongStatusCode, err)
91 // after failed POST vesmgr.appmgrSubsId holds an initial values
92 suite.Equal("", subsID)
95 func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsWrongUrl() {
96 // use fake appmgrUrl that is not served in unit test
97 appmgrURL := "/I_do_not_exist/"
98 requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, suite.xappNotifURL))
99 req, _ := http.NewRequest("POST", appmgrURL, bytes.NewBuffer(requestBody))
100 req.Header.Set("Content-Type", "application/json")
101 client := &http.Client{}
103 subsID, err := subscribexAppNotificationsClientDo(req, client)
104 suite.Equal(errPostingFailed, err)
105 // after failed POST vesmgr.appmgrSubsId holds an initial values
106 suite.Equal("", subsID)
109 func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsReadBodyFails() {
110 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
111 res.Header().Set("Content-Length", "1")
112 res.Header().Add("Content-Type", "application/json")
113 res.WriteHeader(http.StatusCreated)
115 defer testServer.Close()
117 go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL)
118 isSubscribed := <-suite.subscriptions
119 suite.Equal("unexpected EOF", isSubscribed.err.Error())
120 suite.Equal("", isSubscribed.subsID)
123 func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsUnMarshalFails() {
124 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
125 res.Header().Add("Content-Type", "application/json")
126 res.WriteHeader(http.StatusCreated)
127 res.Write([]byte(`{""dump for UT": make(chan int),"}`))
129 defer testServer.Close()
131 go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL)
132 isSubscribed := <-suite.subscriptions
133 suite.Equal("invalid character 'd' after object key", isSubscribed.err.Error())
134 suite.Equal("", isSubscribed.subsID)
137 func TestAppmgrHttpServerTestSuite(t *testing.T) {
138 suite.Run(t, new(AppmgrHTTPServerTestSuite))