[RICPLT-2727] Update RoutingMangaerClient UTs + others.... 78/2078/1
authoris005q <idan.shalom@intl.att.com>
Thu, 19 Dec 2019 12:09:24 +0000 (14:09 +0200)
committeris005q <idan.shalom@intl.att.com>
Thu, 19 Dec 2019 12:09:28 +0000 (14:09 +0200)
Change-Id: I8ec8d7e015cf6a6b342c9ecfed682f7643a06460
Signed-off-by: is005q <idan.shalom@intl.att.com>
E2Manager/clients/routing_manager_client.go
E2Manager/clients/routing_manager_client_test.go
E2Manager/configuration/configuration.go
E2Manager/configuration/configuration_test.go
E2Manager/handlers/rmrmsghandlers/e2_term_init_notification_handler.go
E2Manager/handlers/rmrmsghandlers/e2_term_init_notification_handler_test.go
E2Manager/resources/configuration.yaml

index af31cdf..80ba071 100644 (file)
@@ -17,7 +17,6 @@
 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
 //  platform project (RICP).
 
-
 package clients
 
 import (
@@ -28,7 +27,6 @@ import (
        "e2mgr/models"
        "encoding/json"
        "fmt"
-       "io/ioutil"
        "net/http"
 )
 
@@ -66,26 +64,21 @@ func (c *RoutingManagerClient) AddE2TInstance(e2tAddress string) error {
        body := bytes.NewBuffer(marshaled)
        c.logger.Infof("[E2M -> Routing Manager] #RoutingManagerClient.AddE2TInstance - request body: %+v", body)
 
-       url := c.config.RoutingManagerBaseUrl + AddE2TInstanceApiSuffix
+       url := c.config.RoutingManager.BaseUrl + AddE2TInstanceApiSuffix
        resp, err := c.httpClient.Post(url, "application/json", body)
 
        if err != nil {
+               c.logger.Errorf("#RoutingManagerClient.AddE2TInstance - failed sending request. error: %s", err)
                return e2managererrors.NewRoutingManagerError(err)
        }
 
        defer resp.Body.Close()
 
-       respBody, err := ioutil.ReadAll(resp.Body)
-
-       if err != nil {
-               return e2managererrors.NewRoutingManagerError(err)
-       }
-
-       if resp.StatusCode != http.StatusOK { // TODO: shall we check for != 201?
-               c.logger.Errorf("[Routing Manager -> E2M] #RoutingManagerClient.AddE2TInstance - failure. http status code: %d, response body: %s", resp.StatusCode, string(respBody))
-               return e2managererrors.NewRoutingManagerError(fmt.Errorf("Invalid data")) // TODO: which error shall we return?
+       if resp.StatusCode == http.StatusCreated {
+               c.logger.Infof("[Routing Manager -> E2M] #RoutingManagerClient.AddE2TInstance - success. http status code: %d", resp.StatusCode)
+               return nil
        }
 
-       c.logger.Infof("[Routing Manager -> E2M] #RoutingManagerClient.AddE2TInstance - success. http status code: %d, response body: %s", resp.StatusCode, string(respBody))
-       return nil
+       c.logger.Errorf("[Routing Manager -> E2M] #RoutingManagerClient.AddE2TInstance - failure. http status code: %d", resp.StatusCode)
+       return e2managererrors.NewRoutingManagerError(fmt.Errorf("invalid data"))
 }
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 {
index 3cc24a9..efa2551 100644 (file)
@@ -33,6 +33,9 @@ type Configuration struct {
                Port       int
                MaxMsgSize int
        }
+       RoutingManager struct {
+               BaseUrl    string
+       }
        NotificationResponseBuffer   int
        BigRedButtonTimeoutSec       int
        MaxConnectionAttempts        int
@@ -40,7 +43,6 @@ type Configuration struct {
        RnibRetryIntervalMs          int
        KeepAliveResponseTimeoutMs       int
        KeepAliveDelayMs             int
-       RoutingManagerBaseUrl            string
 }
 
 func ParseConfiguration() *Configuration {
@@ -56,10 +58,10 @@ func ParseConfiguration() *Configuration {
        }
 
        config := Configuration{}
-       config.fillRmrConfig(viper.Sub("rmr"))
-       config.fillHttpConfig(viper.Sub("http"))
-       config.fillLoggingConfig(viper.Sub("logging"))
-
+       config.populateRmrConfig(viper.Sub("rmr"))
+       config.populateHttpConfig(viper.Sub("http"))
+       config.populateLoggingConfig(viper.Sub("logging"))
+       config.populateRoutingManagerConfig(viper.Sub("routingManager"))
        config.NotificationResponseBuffer = viper.GetInt("notificationResponseBuffer")
        config.BigRedButtonTimeoutSec = viper.GetInt("bigRedButtonTimeoutSec")
        config.MaxConnectionAttempts = viper.GetInt("maxConnectionAttempts")
@@ -67,28 +69,34 @@ func ParseConfiguration() *Configuration {
        config.RnibRetryIntervalMs = viper.GetInt("rnibRetryIntervalMs")
        config.KeepAliveResponseTimeoutMs = viper.GetInt("keepAliveResponseTimeoutMs")
        config.KeepAliveDelayMs = viper.GetInt("KeepAliveDelayMs")
-       config.RoutingManagerBaseUrl = viper.GetString("routingManagerBaseUrl")
        return &config
 }
 
-func (c *Configuration) fillLoggingConfig(logConfig *viper.Viper) {
+func (c *Configuration) populateLoggingConfig(logConfig *viper.Viper) {
        if logConfig == nil {
-               panic(fmt.Sprintf("#configuration.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n"))
+               panic(fmt.Sprintf("#configuration.populateLoggingConfig - failed to populate logging configuration: The entry 'logging' not found\n"))
        }
        c.Logging.LogLevel = logConfig.GetString("logLevel")
 }
 
-func (c *Configuration) fillHttpConfig(httpConfig *viper.Viper) {
+func (c *Configuration) populateHttpConfig(httpConfig *viper.Viper) {
        if httpConfig == nil {
-               panic(fmt.Sprintf("#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n"))
+               panic(fmt.Sprintf("#configuration.populateHttpConfig - failed to populate HTTP configuration: The entry 'http' not found\n"))
        }
        c.Http.Port = httpConfig.GetInt("port")
 }
 
-func (c *Configuration) fillRmrConfig(rmrConfig *viper.Viper) {
+func (c *Configuration) populateRmrConfig(rmrConfig *viper.Viper) {
        if rmrConfig == nil {
-               panic(fmt.Sprintf("#configuration.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n"))
+               panic(fmt.Sprintf("#configuration.populateRmrConfig - failed to populate RMR configuration: The entry 'rmr' not found\n"))
        }
        c.Rmr.Port = rmrConfig.GetInt("port")
        c.Rmr.MaxMsgSize = rmrConfig.GetInt("maxMsgSize")
 }
+
+func (c *Configuration) populateRoutingManagerConfig(rmConfig *viper.Viper) {
+       if rmConfig == nil {
+               panic(fmt.Sprintf("#configuration.populateRoutingManagerConfig - failed to populate Routing Manager configuration: The entry 'routingManager' not found\n"))
+       }
+       c.RoutingManager.BaseUrl = rmConfig.GetString("baseUrl")
+}
index a7c8cc5..ffd3123 100644 (file)
@@ -69,6 +69,7 @@ func TestRmrConfigNotFoundFailure(t *testing.T) {
        yamlMap := map[string]interface{}{
                "logging": map[string]interface{}{"logLevel": "info"},
                "http":    map[string]interface{}{"port": 3800},
+               "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
        }
        buf, err := yaml.Marshal(yamlMap)
        if err != nil {
@@ -78,7 +79,7 @@ func TestRmrConfigNotFoundFailure(t *testing.T) {
        if err != nil {
                t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
        }
-       assert.PanicsWithValue(t, "#configuration.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n", func() { ParseConfiguration() })
+       assert.PanicsWithValue(t, "#configuration.populateRmrConfig - failed to populate RMR configuration: The entry 'rmr' not found\n", func() { ParseConfiguration() })
 }
 
 func TestLoggingConfigNotFoundFailure(t *testing.T) {
@@ -97,6 +98,7 @@ func TestLoggingConfigNotFoundFailure(t *testing.T) {
        yamlMap := map[string]interface{}{
                "rmr":  map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
                "http": map[string]interface{}{"port": 3800},
+               "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
        }
        buf, err := yaml.Marshal(yamlMap)
        if err != nil {
@@ -106,7 +108,7 @@ func TestLoggingConfigNotFoundFailure(t *testing.T) {
        if err != nil {
                t.Errorf("#TestRmrConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
        }
-       assert.PanicsWithValue(t, "#configuration.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n",
+       assert.PanicsWithValue(t, "#configuration.populateLoggingConfig - failed to populate logging configuration: The entry 'logging' not found\n",
                func() { ParseConfiguration() })
 }
 
@@ -126,6 +128,7 @@ func TestHttpConfigNotFoundFailure(t *testing.T) {
        yamlMap := map[string]interface{}{
                "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
                "logging": map[string]interface{}{"logLevel": "info"},
+               "routingManager":    map[string]interface{}{"baseUrl": "http://iltlv740.intl.att.com:8080/ric/v1/handles/"},
        }
        buf, err := yaml.Marshal(yamlMap)
        if err != nil {
@@ -135,6 +138,36 @@ func TestHttpConfigNotFoundFailure(t *testing.T) {
        if err != nil {
                t.Errorf("#TestHttpConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
        }
-       assert.PanicsWithValue(t, "#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n",
+       assert.PanicsWithValue(t, "#configuration.populateHttpConfig - failed to populate HTTP configuration: The entry 'http' not found\n",
+               func() { ParseConfiguration() })
+}
+
+func TestRoutingManagerConfigNotFoundFailure(t *testing.T) {
+       configPath := "../resources/configuration.yaml"
+       configPathTmp := "../resources/configuration.yaml_tmp"
+       err := os.Rename(configPath, configPathTmp)
+       if err != nil {
+               t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
+       }
+       defer func() {
+               err = os.Rename(configPathTmp, configPath)
+               if err != nil {
+                       t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to rename configuration file: %s\n", configPath)
+               }
+       }()
+       yamlMap := map[string]interface{}{
+               "rmr":     map[string]interface{}{"port": 3801, "maxMsgSize": 4096},
+               "logging": map[string]interface{}{"logLevel": "info"},
+               "http": map[string]interface{}{"port": 3800},
+       }
+       buf, err := yaml.Marshal(yamlMap)
+       if err != nil {
+               t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to marshal configuration map\n")
+       }
+       err = ioutil.WriteFile("../resources/configuration.yaml", buf, 0644)
+       if err != nil {
+               t.Errorf("#TestRoutingManagerConfigNotFoundFailure - failed to write configuration file: %s\n", configPath)
+       }
+       assert.PanicsWithValue(t, "#configuration.populateRoutingManagerConfig - failed to populate Routing Manager configuration: The entry 'routingManager' not found\n",
                func() { ParseConfiguration() })
 }
index 904e34b..55cd6b5 100644 (file)
@@ -43,24 +43,23 @@ func NewE2TermInitNotificationHandler(logger *logger.Logger, ranReconnectionMana
 }
 
 func (h E2TermInitNotificationHandler) Handle(request *models.NotificationRequest) {
-
-       h.logger.Infof("#E2TermInitNotificationHandler.Handle - Handling E2_TERM_INIT")
-
        unmarshalledPayload := models.E2TermInitPayload{}
        err :=  json.Unmarshal(request.Payload, &unmarshalledPayload)
 
        if err != nil {
-               h.logger.Errorf("#E2TermInitNotificationHandler - Error unmarshaling E2 Term Init payload: %s", err)
+               h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Error unmarshaling E2 Term Init payload: %s", err)
                return
        }
 
        e2tAddress := unmarshalledPayload.Address
 
        if len(e2tAddress) == 0 {
-               h.logger.Errorf("#E2TermInitNotificationHandler - Empty E2T address received")
+               h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Empty E2T address received")
                return
        }
 
+       h.logger.Infof("#E2TermInitNotificationHandler.Handle - E2T address: %s - handling E2_TERM_INIT", e2tAddress)
+
        e2tInstance, err := h.e2tInstancesManager.GetE2TInstance(e2tAddress)
 
        if err != nil {
index 73637fd..1e52ea5 100644 (file)
@@ -138,10 +138,10 @@ func TestE2TermInitHandlerSuccessOneRan(t *testing.T) {
        _, handler, readerMock, writerMock, rmrMessengerMock, e2tInstancesManagerMock := initRanLostConnectionTest(t)
        var rnibErr error
 
-       var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
+       var initialNodeb = &entities.NodebInfo{RanName: RanName, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
        readerMock.On("GetNodeb", RanName).Return(initialNodeb, rnibErr)
 
-       var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, ConnectionAttempts: 1}
+       var argNodeb = &entities.NodebInfo{RanName: RanName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, ConnectionAttempts: 1}
        writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
 
        payload := e2pdus.PackedX2setupRequest
@@ -165,10 +165,10 @@ func TestE2TermInitHandlerSuccessOneRanShuttingdown(t *testing.T) {
        _, handler, readerMock, writerMock, rmrMessengerMock, e2tInstancesManagerMock := initRanLostConnectionTest(t)
        var rnibErr error
 
-       var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
+       var initialNodeb = &entities.NodebInfo{RanName: RanName, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
        readerMock.On("GetNodeb", RanName).Return(initialNodeb, rnibErr)
 
-       var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, ConnectionAttempts: 0}
+       var argNodeb = &entities.NodebInfo{RanName: RanName, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, ConnectionAttempts: 0}
        writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
 
        payload := e2pdus.PackedX2setupRequest
index 356b172..4d43e40 100644 (file)
@@ -5,6 +5,8 @@ http:
 rmr:
   port: 3801
   maxMsgSize: 65536
+routingManager:
+  baseUrl: http://iltlv740.intl.att.com:8080/ric/v1/handles/
 notificationResponseBuffer: 100
 bigRedButtonTimeoutSec: 5
 maxConnectionAttempts: 3
@@ -12,4 +14,3 @@ maxRnibConnectionAttempts: 3
 rnibRetryIntervalMs: 10
 keepAliveResponseTimeoutMs: 1500
 keepAliveDelayMs: 500
-routingManagerBaseUrl: http://iltlv740.intl.att.com:8080/ric/v1/handles/v1/