Merge R4 branch to master
[ric-plt/e2mgr.git] / E2Manager / clients / routing_manager_client_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
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
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16
17 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package clients
21
22 import (
23         "bytes"
24         "e2mgr/configuration"
25         "e2mgr/e2managererrors"
26         "e2mgr/logger"
27         "e2mgr/mocks"
28         "e2mgr/models"
29         "encoding/json"
30         "github.com/pkg/errors"
31         "github.com/stretchr/testify/assert"
32         "io/ioutil"
33         "net/http"
34         "testing"
35 )
36
37 const E2TAddress = "10.0.2.15:38000"
38 const E2TAddress2 = "10.0.2.15:38001"
39 const RanName = "test1"
40
41 func initRoutingManagerClientTest(t *testing.T) (*RoutingManagerClient, *mocks.HttpClientMock, *configuration.Configuration) {
42         logger := initLog(t)
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
48 }
49
50 func TestDeleteE2TInstanceSuccess(t *testing.T) {
51         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
52
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"})
60         assert.Nil(t, err)
61 }
62
63 func TestDeleteE2TInstanceFailure(t *testing.T) {
64         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
65
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)
74 }
75
76 func TestDeleteE2TInstanceDeleteFailure(t *testing.T) {
77         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
78
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)
86 }
87
88 func TestAddE2TInstanceSuccess(t *testing.T) {
89         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
90
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)
98         assert.Nil(t, err)
99 }
100
101 func TestAddE2TInstanceHttpPostFailure(t *testing.T) {
102         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
103
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)
111 }
112
113 func TestAddE2TInstanceFailure(t *testing.T) {
114         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
115
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)
124 }
125
126 func TestAssociateRanToE2TInstance_Success(t *testing.T) {
127         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
128
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)
136         assert.Nil(t, err)
137 }
138
139 func TestAssociateRanToE2TInstance_RoutingManagerError(t *testing.T) {
140         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
141
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)
149 }
150
151 func TestAssociateRanToE2TInstance_RoutingManager_400(t *testing.T) {
152         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
153
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)
162 }
163
164 func TestDissociateRanE2TInstance_Success(t *testing.T) {
165         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
166
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)
174         assert.Nil(t, err)
175 }
176
177 func TestDissociateRanE2TInstance_RoutingManagerError(t *testing.T) {
178         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
179
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)
187 }
188
189 func TestDissociateRanE2TInstance_RoutingManager_400(t *testing.T) {
190         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
191
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)
200 }
201
202 // TODO: extract to test_utils
203 func initLog(t *testing.T) *logger.Logger {
204         log, err := logger.InitLogger(logger.InfoLevel)
205         if err != nil {
206                 t.Errorf("#delete_all_request_handler_test.TestHandleSuccessFlow - failed to initialize logger, error: %s", err)
207         }
208         return log
209 }
210
211 //func TestAddE2TInstanceInteg(t *testing.T) {
212 //      logger := initLog(t)
213 //      config := configuration.ParseConfiguration()
214 //      httpClient := &http.Client{}
215 //      rmClient := NewRoutingManagerClient(logger, config, httpClient)
216 //      err := rmClient.AddE2TInstance(E2TAddress)
217 //      assert.Nil(t, err)
218 //}