Add new license claim
[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  *  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/http"
30         "net/http/httptest"
31         "testing"
32
33         "github.com/stretchr/testify/suite"
34 )
35
36 type AppmgrHTTPServerTestSuite struct {
37         suite.Suite
38         subscriptions chan subscriptionNotification
39         xappNotifURL  string
40 }
41
42 // suite setup
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)
47 }
48
49 // test setup
50 func (suite *AppmgrHTTPServerTestSuite) SetupTest() {
51         suite.subscriptions = make(chan subscriptionNotification)
52 }
53
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)
59                 suite.Nil(err)
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"}`))
67         }))
68         defer testServer.Close()
69
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)
74 }
75
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"}`))
81         }))
82         defer testServer.Close()
83
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{}
88
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)
93 }
94
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{}
102
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)
107 }
108
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)
114         }))
115         defer testServer.Close()
116
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)
121 }
122
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),"}`))
128         }))
129         defer testServer.Close()
130
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)
135 }
136
137 func TestAppmgrHttpServerTestSuite(t *testing.T) {
138         suite.Run(t, new(AppmgrHTTPServerTestSuite))
139 }