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