blob: 11e7531b7bfa76ec3485d11971da8b7b2e2f869b [file] [log] [blame]
Katri Turunen412df962019-09-16 08:48:18 +03001/*
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.
Roni Riska6ffba082019-11-27 10:59:54 +020016 *
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
19 *
Katri Turunen412df962019-09-16 08:48:18 +030020 */
21
22package main
23
24import (
25 "fmt"
Katri Turunen412df962019-09-16 08:48:18 +030026 "net"
27 "net/http"
28 "net/url"
29 "os"
Mohamed Abukar67a790a2020-11-05 09:54:19 +020030 "sync"
Katri Turunen412df962019-09-16 08:48:18 +030031 "testing"
32 "time"
Roni Riskafc77ebb2019-09-26 08:20:44 +030033
34 "github.com/stretchr/testify/suite"
Katri Turunen412df962019-09-16 08:48:18 +030035)
36
37type do func(w http.ResponseWriter)
38
Roni Riskafc77ebb2019-09-26 08:20:44 +030039type QueryXAppsConfigTestSuite struct {
Katri Turunen412df962019-09-16 08:48:18 +030040 suite.Suite
41 listener net.Listener
42 xAppMgrFunc do
Mohamed Abukar67a790a2020-11-05 09:54:19 +020043 mu sync.Mutex
Katri Turunen412df962019-09-16 08:48:18 +030044}
45
46// suite setup creates the HTTP server
Roni Riskafc77ebb2019-09-26 08:20:44 +030047func (suite *QueryXAppsConfigTestSuite) SetupSuite() {
Katri Turunen412df962019-09-16 08:48:18 +030048 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
Roni Riskafc77ebb2019-09-26 08:20:44 +030056func runXAppMgr(listener net.Listener, url string, suite *QueryXAppsConfigTestSuite) {
Katri Turunen412df962019-09-16 08:48:18 +030057
58 http.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
59 switch r.Method {
60 case "GET":
Mohamed Abukar67a790a2020-11-05 09:54:19 +020061 suite.mu.Lock()
Katri Turunen412df962019-09-16 08:48:18 +030062 suite.xAppMgrFunc(w)
Mohamed Abukar67a790a2020-11-05 09:54:19 +020063 suite.mu.Unlock()
Katri Turunen412df962019-09-16 08:48:18 +030064 }
65 })
66 http.Serve(listener, nil)
67}
68
Roni Riskafc77ebb2019-09-26 08:20:44 +030069func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithTimeout() {
70 doSleep := func(w http.ResponseWriter) {
Katri Turunen412df962019-09-16 08:48:18 +030071 time.Sleep(time.Second * 2)
72 }
Roni Riskafc77ebb2019-09-26 08:20:44 +030073 suite.xAppMgrFunc = doSleep
Katri Turunen412df962019-09-16 08:48:18 +030074
Roni Riskafc77ebb2019-09-26 08:20:44 +030075 data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
76 suite.Equal([]byte("{}"), data)
Katri Turunen412df962019-09-16 08:48:18 +030077 suite.NotNil(err)
78 e, ok := err.(*url.Error)
79 suite.Equal(ok, true)
80 suite.Equal(e.Timeout(), true)
81}
82
Roni Riskafc77ebb2019-09-26 08:20:44 +030083func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithAnErrorReply() {
84 doReplyWithErr := func(w http.ResponseWriter) {
Katri Turunen412df962019-09-16 08:48:18 +030085 http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
86 }
Roni Riskafc77ebb2019-09-26 08:20:44 +030087 suite.xAppMgrFunc = doReplyWithErr
Katri Turunen412df962019-09-16 08:48:18 +030088
Roni Riskafc77ebb2019-09-26 08:20:44 +030089 data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
90 suite.Equal([]byte("{}"), data)
Katri Turunen412df962019-09-16 08:48:18 +030091 suite.NotNil(err)
92 suite.Equal("405 Method Not Allowed", err.Error())
93}
94
Roni Riskafc77ebb2019-09-26 08:20:44 +030095func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigOk() {
96 doReply := func(w http.ResponseWriter) {
Katri Turunen412df962019-09-16 08:48:18 +030097 fmt.Fprintf(w, "reply message")
98 }
Roni Riskafc77ebb2019-09-26 08:20:44 +030099 suite.xAppMgrFunc = doReply
Katri Turunen412df962019-09-16 08:48:18 +0300100
Roni Riskafc77ebb2019-09-26 08:20:44 +0300101 data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
Katri Turunen412df962019-09-16 08:48:18 +0300102 suite.NotNil(data)
103 suite.Nil(err)
104 suite.Equal(data, []byte("reply message"))
105}
106
Roni Riskafc77ebb2019-09-26 08:20:44 +0300107func TestQueryXAppsConfigTestSuite(t *testing.T) {
108 suite.Run(t, new(QueryXAppsConfigTestSuite))
Katri Turunen412df962019-09-16 08:48:18 +0300109}