Refactor the code
[ric-plt/vespamgr.git] / cmd / vesmgr / subscribexAPPNotifications_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
18 package main
19
20 import (
21         "bytes"
22         "encoding/json"
23         "fmt"
24         "io/ioutil"
25         "net/http"
26         "net/http/httptest"
27         "testing"
28
29         "github.com/stretchr/testify/suite"
30 )
31
32 type AppmgrHTTPServerTestSuite struct {
33         suite.Suite
34         subscriptions chan subscriptionNotification
35         xappNotifURL  string
36 }
37
38 // suite setup
39 func (suite *AppmgrHTTPServerTestSuite) SetupSuite() {
40         // the url here is not actually used anywhere
41         suite.xappNotifURL = "http://127.0.0.1:8080" + vesmgrXappNotifPath
42         suite.subscriptions = make(chan subscriptionNotification)
43 }
44
45 // test setup
46 func (suite *AppmgrHTTPServerTestSuite) SetupTest() {
47         suite.subscriptions = make(chan subscriptionNotification)
48 }
49
50 func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotifications() {
51         testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
52                 body, _ := ioutil.ReadAll(req.Body)
53                 var result map[string]interface{}
54                 err := json.Unmarshal([]byte(body), &result)
55                 suite.Nil(err)
56                 suite.Equal(5, int(result["maxRetries"].(float64)))
57                 suite.Equal(5, int(result["retryTimer"].(float64)))
58                 suite.Equal("all", result["eventType"].(string))
59                 suite.Equal("POST", req.Method)
60                 res.Header().Add("Content-Type", "application/json")
61                 res.WriteHeader(http.StatusCreated)
62                 res.Write([]byte(`{"id":"deadbeef1234567890", "version":0, "eventType":"all"}`))
63         }))
64         defer testServer.Close()
65
66         go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL)
67         isSubscribed := <-suite.subscriptions
68         suite.Nil(isSubscribed.err)
69         suite.Equal("deadbeef1234567890", isSubscribed.subsID)
70 }
71
72 func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsWrongStatus() {
73         testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
74                 res.Header().Add("Content-Type", "application/json")
75                 res.WriteHeader(http.StatusUnauthorized)
76                 res.Write([]byte(`{"id":"deadbeef1234567890", "version":0, "eventType":"all"}`))
77         }))
78         defer testServer.Close()
79
80         requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, suite.xappNotifURL))
81         req, _ := http.NewRequest("POST", testServer.URL, bytes.NewBuffer(requestBody))
82         req.Header.Set("Content-Type", "application/json")
83         client := &http.Client{}
84
85         subsID, err := subscribexAppNotificationsClientDo(req, client)
86         suite.Equal(errWrongStatusCode, err)
87         // after failed POST vesmgr.appmgrSubsId holds an initial values
88         suite.Equal("", subsID)
89 }
90
91 func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsWrongUrl() {
92         // use fake appmgrUrl that is not served in unit test
93         appmgrURL := "/I_do_not_exist/"
94         requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, suite.xappNotifURL))
95         req, _ := http.NewRequest("POST", appmgrURL, bytes.NewBuffer(requestBody))
96         req.Header.Set("Content-Type", "application/json")
97         client := &http.Client{}
98
99         subsID, err := subscribexAppNotificationsClientDo(req, client)
100         suite.Equal(errPostingFailed, err)
101         // after failed POST vesmgr.appmgrSubsId holds an initial values
102         suite.Equal("", subsID)
103 }
104
105 func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsReadBodyFails() {
106         testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
107                 res.Header().Set("Content-Length", "1")
108                 res.Header().Add("Content-Type", "application/json")
109                 res.WriteHeader(http.StatusCreated)
110         }))
111         defer testServer.Close()
112
113         go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL)
114         isSubscribed := <-suite.subscriptions
115         suite.Equal("unexpected EOF", isSubscribed.err.Error())
116         suite.Equal("", isSubscribed.subsID)
117 }
118
119 func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsUnMarshalFails() {
120         testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
121                 res.Header().Add("Content-Type", "application/json")
122                 res.WriteHeader(http.StatusCreated)
123                 res.Write([]byte(`{""dump for UT": make(chan int),"}`))
124         }))
125         defer testServer.Close()
126
127         go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL)
128         isSubscribed := <-suite.subscriptions
129         suite.Equal("invalid character 'd' after object key", isSubscribed.err.Error())
130         suite.Equal("", isSubscribed.subsID)
131 }
132
133 func TestAppmgrHttpServerTestSuite(t *testing.T) {
134         suite.Run(t, new(AppmgrHTTPServerTestSuite))
135 }