X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=cmd%2Fvesmgr%2FsubscribexAPPNotifications_test.go;h=1521da3c4bec0c2eed90f59c0e62ceab04276834;hb=refs%2Fchanges%2F53%2F2353%2F2;hp=31621b3c58eb65c9863628f3349573dd12ce7e17;hpb=412df96a23a30a82d2a031556888aeaf9604ada8;p=ric-plt%2Fvespamgr.git diff --git a/cmd/vesmgr/subscribexAPPNotifications_test.go b/cmd/vesmgr/subscribexAPPNotifications_test.go index 31621b3..1521da3 100644 --- a/cmd/vesmgr/subscribexAPPNotifications_test.go +++ b/cmd/vesmgr/subscribexAPPNotifications_test.go @@ -13,6 +13,10 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * This source code is part of the near-RT RIC (RAN Intelligent Controller) + * platform project (RICP). + * */ package main @@ -21,46 +25,42 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/stretchr/testify/suite" "io/ioutil" "net/http" "net/http/httptest" "testing" + + "github.com/stretchr/testify/suite" ) -type AppmgrHttpServerTestSuite struct { +type AppmgrHTTPServerTestSuite struct { suite.Suite - subscriptions chan subsChannel - xappNotifUrl string + subscriptions chan subscriptionNotification + xappNotifURL string } // suite setup -func (suite *AppmgrHttpServerTestSuite) SetupSuite() { - vesmgr.appmgrSubsId = string("") - vesmgr.myIPAddress, _ = getMyIP() - suite.xappNotifUrl = "http://" + vesmgr.myIPAddress + ":" + vesmgrXappNotifPort + vesmgrXappNotifPath - suite.subscriptions = make(chan subsChannel) +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) } // test setup -func (suite *AppmgrHttpServerTestSuite) SetupTest() { - suite.subscriptions = make(chan subsChannel) -} - -// test teardown -func (suite *AppmgrHttpServerTestSuite) TearDownTest() { - vesmgr.appmgrSubsId = string("") +func (suite *AppmgrHTTPServerTestSuite) SetupTest() { + suite.subscriptions = make(chan subscriptionNotification) } -func (suite *AppmgrHttpServerTestSuite) TestSubscribexAppNotifications() { +func (suite *AppmgrHTTPServerTestSuite) 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) suite.Nil(err) - suite.Equal(5, int(result["maxRetries"].(float64))) - suite.Equal(5, int(result["retryTimer"].(float64))) - suite.Equal("all", result["eventType"].(string)) + data := result["Data"].(map[string]interface{}) + suite.Equal(5, int(data["maxRetries"].(float64))) + suite.Equal(5, int(data["retryTimer"].(float64))) + suite.Equal("all", data["eventType"].(string)) suite.Equal("POST", req.Method) res.Header().Add("Content-Type", "application/json") res.WriteHeader(http.StatusCreated) @@ -68,13 +68,13 @@ func (suite *AppmgrHttpServerTestSuite) TestSubscribexAppNotifications() { })) defer testServer.Close() - go subscribexAppNotifications(suite.xappNotifUrl, suite.subscriptions, 1, testServer.URL) + go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL) isSubscribed := <-suite.subscriptions suite.Nil(isSubscribed.err) - suite.Equal("deadbeef1234567890", vesmgr.appmgrSubsId) + suite.Equal("deadbeef1234567890", isSubscribed.subsID) } -func (suite *AppmgrHttpServerTestSuite) TestSubscribexAppNotificationsWrongStatus() { +func (suite *AppmgrHTTPServerTestSuite) 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 +82,32 @@ 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"}`, suite.xappNotifURL)) req, _ := http.NewRequest("POST", testServer.URL, bytes.NewBuffer(requestBody)) req.Header.Set("Content-Type", "application/json") client := &http.Client{} - err := subscribexAppNotificationsClientDo(req, client) + subsID, err := subscribexAppNotificationsClientDo(req, client) suite.Equal(errWrongStatusCode, err) // after failed POST vesmgr.appmgrSubsId holds an initial values - suite.Equal("", vesmgr.appmgrSubsId) + suite.Equal("", subsID) } -func (suite *AppmgrHttpServerTestSuite) TestSubscribexAppNotificationsWrongUrl() { +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)) + 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{} - err := subscribexAppNotificationsClientDo(req, client) + subsID, err := subscribexAppNotificationsClientDo(req, client) suite.Equal(errPostingFailed, err) // after failed POST vesmgr.appmgrSubsId holds an initial values - suite.Equal("", vesmgr.appmgrSubsId) + suite.Equal("", subsID) } -func (suite *AppmgrHttpServerTestSuite) TestSubscribexAppNotificationsReadBodyFails() { +func (suite *AppmgrHTTPServerTestSuite) 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 +115,13 @@ func (suite *AppmgrHttpServerTestSuite) TestSubscribexAppNotificationsReadBodyFa })) defer testServer.Close() - go subscribexAppNotifications(suite.xappNotifUrl, suite.subscriptions, 1, testServer.URL) + go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL) isSubscribed := <-suite.subscriptions suite.Equal("unexpected EOF", isSubscribed.err.Error()) - suite.Equal("", vesmgr.appmgrSubsId) + suite.Equal("", isSubscribed.subsID) } -func (suite *AppmgrHttpServerTestSuite) TestSubscribexAppNotificationsUnMarshalFails() { +func (suite *AppmgrHTTPServerTestSuite) 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 +129,12 @@ func (suite *AppmgrHttpServerTestSuite) TestSubscribexAppNotificationsUnMarshalF })) defer testServer.Close() - go subscribexAppNotifications(suite.xappNotifUrl, suite.subscriptions, 1, testServer.URL) + 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("", vesmgr.appmgrSubsId) + suite.Equal("", isSubscribed.subsID) } func TestAppmgrHttpServerTestSuite(t *testing.T) { - suite.Run(t, new(AppmgrHttpServerTestSuite)) + suite.Run(t, new(AppmgrHTTPServerTestSuite)) }