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).
25 "e2mgr/e2managererrors"
30 "github.com/pkg/errors"
31 "github.com/stretchr/testify/assert"
37 const E2TAddress = "10.0.2.15:38000"
38 const E2TAddress2 = "10.0.2.15:38001"
39 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 TestDeleteE2TInstanceSuccess(t *testing.T) {
51 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
53 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1"}, nil)
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("Delete", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusOK, Body: respBody}, nil)
59 err := rmClient.DeleteE2TInstance(E2TAddress, []string{"test1"})
63 func TestDeleteE2TInstanceFailure(t *testing.T) {
64 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
66 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1"}, nil)
67 marshaled, _ := json.Marshal(data)
68 body := bytes.NewBuffer(marshaled)
69 url := config.RoutingManager.BaseUrl + "e2t"
70 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
71 httpClientMock.On("Delete", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body: respBody}, nil)
72 err := rmClient.DeleteE2TInstance(E2TAddress, []string{"test1"})
73 assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
76 func TestDeleteE2TInstanceDeleteFailure(t *testing.T) {
77 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
79 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1"}, nil)
80 marshaled, _ := json.Marshal(data)
81 body := bytes.NewBuffer(marshaled)
82 url := config.RoutingManager.BaseUrl + "e2t"
83 httpClientMock.On("Delete", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
84 err := rmClient.DeleteE2TInstance(E2TAddress, []string{"test1"})
85 assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
88 func TestAddE2TInstanceSuccess(t *testing.T) {
89 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
91 data := models.NewRoutingManagerE2TData(E2TAddress)
92 marshaled, _ := json.Marshal(data)
93 body := bytes.NewBuffer(marshaled)
94 url := config.RoutingManager.BaseUrl + "e2t"
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.AddE2TInstance(E2TAddress)
101 func TestAddE2TInstanceHttpPostFailure(t *testing.T) {
102 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
104 data := models.NewRoutingManagerE2TData(E2TAddress)
105 marshaled, _ := json.Marshal(data)
106 body := bytes.NewBuffer(marshaled)
107 url := config.RoutingManager.BaseUrl + "e2t"
108 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
109 err := rmClient.AddE2TInstance(E2TAddress)
110 assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
113 func TestAddE2TInstanceFailure(t *testing.T) {
114 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
116 data := models.NewRoutingManagerE2TData(E2TAddress)
117 marshaled, _ := json.Marshal(data)
118 body := bytes.NewBuffer(marshaled)
119 url := config.RoutingManager.BaseUrl + "e2t"
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.AddE2TInstance(E2TAddress)
123 assert.NotNil(t, err)
126 func TestAssociateRanToE2TInstance_Success(t *testing.T) {
127 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
129 data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
130 marshaled, _ := json.Marshal(data)
131 body := bytes.NewBuffer(marshaled)
132 url := config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
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.AssociateRanToE2TInstance(E2TAddress, RanName)
139 func TestAssociateRanToE2TInstance_RoutingManagerError(t *testing.T) {
140 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
142 data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
143 marshaled, _ := json.Marshal(data)
144 body := bytes.NewBuffer(marshaled)
145 url := config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
146 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
147 err := rmClient.AssociateRanToE2TInstance(E2TAddress, RanName)
148 assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
151 func TestAssociateRanToE2TInstance_RoutingManager_400(t *testing.T) {
152 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
154 data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
155 marshaled, _ := json.Marshal(data)
156 body := bytes.NewBuffer(marshaled)
157 url := config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
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.AssociateRanToE2TInstance(E2TAddress, RanName)
161 assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
164 func TestDissociateRanE2TInstance_Success(t *testing.T) {
165 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
167 data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
168 marshaled, _ := json.Marshal(data)
169 body := bytes.NewBuffer(marshaled)
170 url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
171 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
172 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
173 err := rmClient.DissociateRanE2TInstance(E2TAddress, RanName)
177 func TestDissociateRanE2TInstance_RoutingManagerError(t *testing.T) {
178 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
180 data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
181 marshaled, _ := json.Marshal(data)
182 body := bytes.NewBuffer(marshaled)
183 url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
184 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
185 err := rmClient.DissociateRanE2TInstance(E2TAddress, RanName)
186 assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
189 func TestDissociateRanE2TInstance_RoutingManager_400(t *testing.T) {
190 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
192 data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
193 marshaled, _ := json.Marshal(data)
194 body := bytes.NewBuffer(marshaled)
195 url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
196 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
197 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body: respBody}, nil)
198 err := rmClient.DissociateRanE2TInstance(E2TAddress, RanName)
199 assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
202 func TestDissociateAllRans_Success(t *testing.T) {
203 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
205 data := models.RoutingManagerE2TDataList{
206 models.NewRoutingManagerE2TData(E2TAddress),
207 models.NewRoutingManagerE2TData(E2TAddress2),
209 marshaled, _ := json.Marshal(data)
210 body := bytes.NewBuffer(marshaled)
211 url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
212 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
213 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
214 err := rmClient.DissociateAllRans([]string{E2TAddress, E2TAddress2})
218 func TestDissociateAllRans_RoutingManagerError(t *testing.T) {
219 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
221 data := models.RoutingManagerE2TDataList{
222 models.NewRoutingManagerE2TData(E2TAddress),
223 models.NewRoutingManagerE2TData(E2TAddress2),
225 marshaled, _ := json.Marshal(data)
226 body := bytes.NewBuffer(marshaled)
227 url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
228 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
229 err := rmClient.DissociateAllRans([]string{E2TAddress, E2TAddress2})
230 assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
233 func TestDissociateAllRans_RoutingManager_400(t *testing.T) {
234 rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
236 data := models.RoutingManagerE2TDataList{
237 models.NewRoutingManagerE2TData(E2TAddress),
238 models.NewRoutingManagerE2TData(E2TAddress2),
240 marshaled, _ := json.Marshal(data)
241 body := bytes.NewBuffer(marshaled)
242 url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
243 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
244 httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body: respBody}, nil)
245 err := rmClient.DissociateAllRans([]string{E2TAddress, E2TAddress2})
246 assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
249 // TODO: extract to test_utils
250 func initLog(t *testing.T) *logger.Logger {
252 log, err := logger.InitLogger(level)
254 t.Errorf("#delete_all_request_handler_test.TestHandleSuccessFlow - failed to initialize logger, error: %s", err)
259 //func TestAddE2TInstanceInteg(t *testing.T) {
260 // logger := initLog(t)
261 // config := configuration.ParseConfiguration()
262 // httpClient := &http.Client{}
263 // rmClient := NewRoutingManagerClient(logger, config, httpClient)
264 // err := rmClient.AddE2TInstance(E2TAddress)
265 // assert.Nil(t, err)