Redesign and taking xapp-framework into use
[ric-plt/vespamgr.git] / cmd / vespamgr / vespamgr_test.go
old mode 100644 (file)
new mode 100755 (executable)
similarity index 51%
rename from cmd/vesmgr/subscribexAPPNotifications_test.go
rename to cmd/vespamgr/vespamgr_test.go
index 1521da3..2b84ef5
@@ -26,33 +26,32 @@ import (
        "encoding/json"
        "fmt"
        "io/ioutil"
+       "net"
        "net/http"
        "net/http/httptest"
+       "os"
        "testing"
+       "time"
 
+       app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
        "github.com/stretchr/testify/suite"
 )
 
-type AppmgrHTTPServerTestSuite struct {
+type VespaMgrTestSuite struct {
        suite.Suite
-       subscriptions chan subscriptionNotification
-       xappNotifURL  string
+       vespaMgr *VespaMgr
 }
 
 // suite setup
-func (suite *AppmgrHTTPServerTestSuite) SetupSuite() {
-       // the url here is not actually used anywhere
-       suite.xappNotifURL = "http://127.0.0.1:8080" + vesmgrXappNotifPath
-       suite.subscriptions = make(chan subscriptionNotification)
+func (suite *VespaMgrTestSuite) SetupSuite() {
+       os.Unsetenv("http_proxy")
+       os.Unsetenv("HTTP_PROXY")
+       suite.vespaMgr = NewVespaMgr()
 }
 
-// test setup
-func (suite *AppmgrHTTPServerTestSuite) SetupTest() {
-       suite.subscriptions = make(chan subscriptionNotification)
-}
-
-func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotifications() {
+func (suite *VespaMgrTestSuite) TestSubscribexAppNotifications() {
        testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
+
                body, _ := ioutil.ReadAll(req.Body)
                var result map[string]interface{}
                err := json.Unmarshal([]byte(body), &result)
@@ -68,13 +67,11 @@ func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotifications() {
        }))
        defer testServer.Close()
 
-       go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL)
-       isSubscribed := <-suite.subscriptions
-       suite.Nil(isSubscribed.err)
-       suite.Equal("deadbeef1234567890", isSubscribed.subsID)
+       suite.vespaMgr.SubscribeXappNotif(testServer.URL)
+       suite.Equal("deadbeef1234567890", suite.vespaMgr.subscriptionId)
 }
 
-func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsWrongStatus() {
+func (suite *VespaMgrTestSuite) TestSubscribexAppNotificationsWrongStatus() {
        testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
                res.Header().Add("Content-Type", "application/json")
                res.WriteHeader(http.StatusUnauthorized)
@@ -82,32 +79,16 @@ func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsWrongStatu
        }))
        defer testServer.Close()
 
-       requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, suite.xappNotifURL))
+       requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, "localhost:8080"))
        req, _ := http.NewRequest("POST", testServer.URL, bytes.NewBuffer(requestBody))
        req.Header.Set("Content-Type", "application/json")
-       client := &http.Client{}
-
-       subsID, err := subscribexAppNotificationsClientDo(req, client)
-       suite.Equal(errWrongStatusCode, err)
-       // after failed POST vesmgr.appmgrSubsId holds an initial values
-       suite.Equal("", subsID)
-}
-
-func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsWrongUrl() {
-       // use fake appmgrUrl that is not served in unit test
-       appmgrURL := "/I_do_not_exist/"
-       requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, suite.xappNotifURL))
-       req, _ := http.NewRequest("POST", appmgrURL, bytes.NewBuffer(requestBody))
-       req.Header.Set("Content-Type", "application/json")
-       client := &http.Client{}
 
-       subsID, err := subscribexAppNotificationsClientDo(req, client)
-       suite.Equal(errPostingFailed, err)
-       // after failed POST vesmgr.appmgrSubsId holds an initial values
-       suite.Equal("", subsID)
+       suite.vespaMgr.subscriptionId = ""
+       suite.vespaMgr.DoSubscribe(testServer.URL, requestBody)
+       suite.Equal("", suite.vespaMgr.subscriptionId)
 }
 
-func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsReadBodyFails() {
+func (suite *VespaMgrTestSuite) TestSubscribexAppNotificationsReadBodyFails() {
        testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
                res.Header().Set("Content-Length", "1")
                res.Header().Add("Content-Type", "application/json")
@@ -115,13 +96,12 @@ func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsReadBodyFa
        }))
        defer testServer.Close()
 
-       go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL)
-       isSubscribed := <-suite.subscriptions
-       suite.Equal("unexpected EOF", isSubscribed.err.Error())
-       suite.Equal("", isSubscribed.subsID)
+       suite.vespaMgr.subscriptionId = ""
+       suite.vespaMgr.DoSubscribe(testServer.URL, []byte{})
+       suite.Equal("", suite.vespaMgr.subscriptionId)
 }
 
-func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsUnMarshalFails() {
+func (suite *VespaMgrTestSuite) TestSubscribexAppNotificationsUnMarshalFails() {
        testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
                res.Header().Add("Content-Type", "application/json")
                res.WriteHeader(http.StatusCreated)
@@ -129,12 +109,57 @@ func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsUnMarshalF
        }))
        defer testServer.Close()
 
-       go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL)
-       isSubscribed := <-suite.subscriptions
-       suite.Equal("invalid character 'd' after object key", isSubscribed.err.Error())
-       suite.Equal("", isSubscribed.subsID)
+       suite.vespaMgr.subscriptionId = ""
+       suite.vespaMgr.DoSubscribe(testServer.URL, []byte{})
+       suite.Equal("", suite.vespaMgr.subscriptionId)
+}
+
+func (suite *VespaMgrTestSuite) TestQueryXAppsConfigOk() {
+       listener, err := net.Listen("tcp", ":0")
+       suite.Nil(err)
+
+       http.HandleFunc("/test_url/", func(w http.ResponseWriter, r *http.Request) {
+               switch r.Method {
+               case "GET":
+                       fmt.Fprintf(w, "reply message")
+               }
+       })
+
+       go http.Serve(listener, nil)
+
+       xappConfig, err := suite.vespaMgr.QueryXappConf("http://" + listener.Addr().String() + "/test_url/")
+       suite.NotNil(xappConfig)
+       suite.Nil(err)
+       suite.Equal(xappConfig, []byte("reply message"))
+}
+
+func (suite *VespaMgrTestSuite) TestHandlexAppNotification() {
+       data, err := ioutil.ReadFile("../../test/xApp_config_test_output.json")
+       suite.Nil(err)
+
+       pbodyEn, _ := json.Marshal(data)
+       req, _ := http.NewRequest("POST", "/ric/v1/xappnotif", bytes.NewBuffer(pbodyEn))
+       handleFunc := http.HandlerFunc(suite.vespaMgr.HandlexAppNotification)
+       response := executeRequest(req, handleFunc)
+       suite.Equal(http.StatusOK, response.Code)
+}
+
+func (suite *VespaMgrTestSuite) TestSubscribexAppNotificationsOnStartup() {
+       suite.vespaMgr.Run(false, false)
+       time.Sleep(2 * time.Second)
+
+       suite.vespaMgr.Consume(&app.RMRParams{})
+       suite.vespaMgr.StatusCB()
+       suite.vespaMgr.rmrReady = true
+       suite.vespaMgr.StatusCB()
+}
+
+func executeRequest(req *http.Request, handleR http.HandlerFunc) *httptest.ResponseRecorder {
+       rr := httptest.NewRecorder()
+       handleR.ServeHTTP(rr, req)
+       return rr
 }
 
-func TestAppmgrHttpServerTestSuite(t *testing.T) {
-       suite.Run(t, new(AppmgrHTTPServerTestSuite))
+func TestVespaMgrTestSuite(t *testing.T) {
+       suite.Run(t, new(VespaMgrTestSuite))
 }