2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
30 "github.com/pkg/errors"
31 "github.com/stretchr/testify/assert"
37 const E2TAddress = "10.0.2.15:38000"
38 const RanName = "test1"
41 func initRoutingManagerClientTest(t *testing.T) (*RoutingManagerClient, *mocks.HttpClientMock, *configuration.Configuration) {
43 config := &configuration.Configuration{}
44 config.RoutingManager.BaseUrl = "http://iltlv740.intl.att.com:8080/ric/v1/handles/"
45 httpClientMock := &mocks.HttpClientMock{}
46 rmClient := NewRoutingManagerClient(logger, config, httpClientMock)
47 return rmClient, httpClientMock, config
50 func TestAddE2TInstanceSuccess(t *testing.T) {
51 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
53 data := models.NewRoutingManagerE2TData(E2TAddress)
54 marshaled, _ := json.Marshal(data)
55 body := bytes.NewBuffer(marshaled)
56 url := config.RoutingManager.BaseUrl + "e2t"
57 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
58 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
59 err := rmClient.AddE2TInstance(E2TAddress)
63 func TestAddE2TInstanceHttpPostFailure(t *testing.T) {
64 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
66 data := models.NewRoutingManagerE2TData(E2TAddress)
67 marshaled, _ := json.Marshal(data)
68 body := bytes.NewBuffer(marshaled)
69 url := config.RoutingManager.BaseUrl + "e2t"
70 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
71 err := rmClient.AddE2TInstance(E2TAddress)
75 func TestAddE2TInstanceFailure(t *testing.T) {
76 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
78 data := models.NewRoutingManagerE2TData(E2TAddress)
79 marshaled, _ := json.Marshal(data)
80 body := bytes.NewBuffer(marshaled)
81 url := config.RoutingManager.BaseUrl + "e2t"
82 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
83 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body:respBody}, nil)
84 err := rmClient.AddE2TInstance(E2TAddress)
88 func TestAssociateRanToE2TInstance_Success(t *testing.T) {
89 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
91 data := models.NewRoutingManagerE2TData(E2TAddress,RanName)
92 marshaled, _ := json.Marshal(data)
93 body := bytes.NewBuffer(marshaled)
94 url := config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
95 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
96 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
97 err := rmClient.AssociateRanToE2TInstance(E2TAddress, RanName)
101 func TestAssociateRanToE2TInstance_RoutingManagerError(t *testing.T) {
102 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
104 data := models.NewRoutingManagerE2TData(E2TAddress,RanName)
105 marshaled, _ := json.Marshal(data)
106 body := bytes.NewBuffer(marshaled)
107 url := config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
108 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
109 err := rmClient.AssociateRanToE2TInstance(E2TAddress, RanName)
110 assert.NotNil(t, err)
113 func TestAssociateRanToE2TInstance_RoutingManager_400(t *testing.T) {
114 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
116 data := models.NewRoutingManagerE2TData(E2TAddress,RanName)
117 marshaled, _ := json.Marshal(data)
118 body := bytes.NewBuffer(marshaled)
119 url := config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
120 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
121 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body:respBody}, nil)
122 err := rmClient.AssociateRanToE2TInstance(E2TAddress, RanName)
123 assert.NotNil(t, err)
126 func TestDissociateRanE2TInstance_Success(t *testing.T) {
127 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
129 data := models.NewRoutingManagerE2TData(E2TAddress,RanName)
130 marshaled, _ := json.Marshal(data)
131 body := bytes.NewBuffer(marshaled)
132 url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
133 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
134 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
135 err := rmClient.DissociateRanE2TInstance(E2TAddress, RanName)
139 func TestDissociateRanE2TInstance_RoutingManagerError(t *testing.T) {
140 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
142 data := models.NewRoutingManagerE2TData(E2TAddress,RanName)
143 marshaled, _ := json.Marshal(data)
144 body := bytes.NewBuffer(marshaled)
145 url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
146 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
147 err := rmClient.DissociateRanE2TInstance(E2TAddress, RanName)
148 assert.NotNil(t, err)
151 func TestDissociateRanE2TInstance_RoutingManager_400(t *testing.T) {
152 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
154 data := models.NewRoutingManagerE2TData(E2TAddress,RanName)
155 marshaled, _ := json.Marshal(data)
156 body := bytes.NewBuffer(marshaled)
157 url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
158 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
159 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body:respBody}, nil)
160 err := rmClient.DissociateRanE2TInstance(E2TAddress, RanName)
161 assert.NotNil(t, err)
164 // TODO: extract to test_utils
165 func initLog(t *testing.T) *logger.Logger {
166 log, err := logger.InitLogger(logger.InfoLevel)
168 t.Errorf("#delete_all_request_handler_test.TestHandleSuccessFlow - failed to initialize logger, error: %s", err)
173 //func TestAddE2TInstanceInteg(t *testing.T) {
174 // logger := initLog(t)
175 // config := configuration.ParseConfiguration()
176 // httpClient := &http.Client{}
177 // rmClient := NewRoutingManagerClient(logger, config, httpClient)
178 // err := rmClient.AddE2TInstance(E2TAddress)
179 // assert.Nil(t, err)