[RICPLT-2727] Update RoutingMangaerClient UTs + others....
[ric-plt/e2mgr.git] / E2Manager / clients / routing_manager_client_test.go
index 5efc753..8ac2b3e 100644 (file)
 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
 //  platform project (RICP).
 
-
 package clients
 
 import (
+       "bytes"
        "e2mgr/configuration"
        "e2mgr/logger"
        "e2mgr/mocks"
+       "e2mgr/models"
+       "encoding/json"
+       "github.com/pkg/errors"
+       "github.com/stretchr/testify/assert"
+       "io/ioutil"
+       "net/http"
        "testing"
 )
 
@@ -33,37 +39,50 @@ const E2TAddress = "10.0.2.15:38000"
 
 func initRoutingManagerClientTest(t *testing.T) (*RoutingManagerClient, *mocks.HttpClientMock, *configuration.Configuration) {
        logger := initLog(t)
-       config := &configuration.Configuration{
-               RoutingManagerBaseUrl: "http://iltlv740.intl.att.com:8080/ric/v1/handles/v1/",
-       }
+       config := &configuration.Configuration{}
+       config.RoutingManager.BaseUrl = "http://iltlv740.intl.att.com:8080/ric/v1/handles/"
        httpClientMock := &mocks.HttpClientMock{}
        rmClient := NewRoutingManagerClient(logger, config, httpClientMock)
        return rmClient, httpClientMock, config
 }
 
-//func TestAddE2TInstanceSuccess(t *testing.T) {
-//     rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
-//
-//     data := models.NewRoutingManagerE2TData(E2TAddress)
-//     marshaled, _ := json.Marshal(data)
-//     body := bytes.NewBuffer(marshaled)
-//     url := config.RoutingManagerBaseUrl + "e2t"
-//     httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: 200}, nil)
-//     err := rmClient.AddE2TInstance(E2TAddress)
-//     assert.Nil(t, err)
-//}
-//
-//func TestAddE2TInstanceFailure(t *testing.T) {
-//     rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
-//
-//     data := models.NewRoutingManagerE2TData(E2TAddress)
-//     marshaled, _ := json.Marshal(data)
-//     body := bytes.NewBuffer(marshaled)
-//     url := config.RoutingManagerBaseUrl + "e2t"
-//     httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: 400}, nil)
-//     err := rmClient.AddE2TInstance(E2TAddress)
-//     assert.NotNil(t, err)
-//}
+func TestAddE2TInstanceSuccess(t *testing.T) {
+       rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
+
+       data := models.NewRoutingManagerE2TData(E2TAddress)
+       marshaled, _ := json.Marshal(data)
+       body := bytes.NewBuffer(marshaled)
+       url := config.RoutingManager.BaseUrl + "e2t"
+       respBody := ioutil.NopCloser(bytes.NewBufferString(""))
+       httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
+       err := rmClient.AddE2TInstance(E2TAddress)
+       assert.Nil(t, err)
+}
+
+func TestAddE2TInstanceHttpPostFailure(t *testing.T) {
+       rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
+
+       data := models.NewRoutingManagerE2TData(E2TAddress)
+       marshaled, _ := json.Marshal(data)
+       body := bytes.NewBuffer(marshaled)
+       url := config.RoutingManager.BaseUrl + "e2t"
+       httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
+       err := rmClient.AddE2TInstance(E2TAddress)
+       assert.NotNil(t, err)
+}
+
+func TestAddE2TInstanceFailure(t *testing.T) {
+       rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
+
+       data := models.NewRoutingManagerE2TData(E2TAddress)
+       marshaled, _ := json.Marshal(data)
+       body := bytes.NewBuffer(marshaled)
+       url := config.RoutingManager.BaseUrl + "e2t"
+       respBody := ioutil.NopCloser(bytes.NewBufferString(""))
+       httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body:respBody}, nil)
+       err := rmClient.AddE2TInstance(E2TAddress)
+       assert.NotNil(t, err)
+}
 
 // TODO: extract to test_utils
 func initLog(t *testing.T) *logger.Logger {