More UT fixes
[ric-plt/vespamgr.git] / cmd / vesmgr / vesmgr_queryxappconfig_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         "fmt"
26         "net"
27         "net/http"
28         "net/url"
29         "os"
30         "sync"
31         "testing"
32         "time"
33
34         "github.com/stretchr/testify/suite"
35 )
36
37 type do func(w http.ResponseWriter)
38
39 type QueryXAppsConfigTestSuite struct {
40         suite.Suite
41         listener    net.Listener
42         xAppMgrFunc do
43         mu          sync.Mutex
44 }
45
46 // suite setup creates the HTTP server
47 func (suite *QueryXAppsConfigTestSuite) SetupSuite() {
48         os.Unsetenv("http_proxy")
49         os.Unsetenv("HTTP_PROXY")
50         var err error
51         suite.listener, err = net.Listen("tcp", ":0")
52         suite.Nil(err)
53         go runXAppMgr(suite.listener, "/test_url/", suite)
54 }
55
56 func runXAppMgr(listener net.Listener, url string, suite *QueryXAppsConfigTestSuite) {
57
58         http.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
59                 switch r.Method {
60                 case "GET":
61                         suite.mu.Lock()
62                         defer suite.mu.Unlock()
63                         suite.xAppMgrFunc(w)
64                 }
65         })
66
67         http.Serve(listener, nil)
68 }
69
70 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithTimeout() {
71         doSleep := func(w http.ResponseWriter) {
72                 time.Sleep(time.Second * 2)
73         }
74
75         suite.mu.Lock()
76         suite.xAppMgrFunc = doSleep
77         suite.mu.Unlock()
78
79         data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
80         suite.Equal([]byte("{}"), data)
81         suite.NotNil(err)
82         e, ok := err.(*url.Error)
83         suite.Equal(ok, true)
84         suite.Equal(e.Timeout(), true)
85 }
86
87 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithAnErrorReply() {
88         doReplyWithErr := func(w http.ResponseWriter) {
89                 http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
90         }
91
92         suite.mu.Lock()
93         suite.xAppMgrFunc = doReplyWithErr
94         suite.mu.Unlock()
95
96         data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
97         suite.Equal([]byte("{}"), data)
98         suite.NotNil(err)
99         suite.Equal("405 Method Not Allowed", err.Error())
100 }
101
102 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigOk() {
103         doReply := func(w http.ResponseWriter) {
104                 fmt.Fprintf(w, "reply message")
105         }
106
107         suite.mu.Lock()
108         suite.xAppMgrFunc = doReply
109         suite.mu.Unlock()
110
111         data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
112         suite.NotNil(data)
113         suite.Nil(err)
114         suite.Equal(data, []byte("reply message"))
115 }
116
117 func TestQueryXAppsConfigTestSuite(t *testing.T) {
118         suite.Run(t, new(QueryXAppsConfigTestSuite))
119 }