2d02cfd88b38c7c3ead516be493ff5dc031ac44e
[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
21 package clients
22
23 import (
24         "bytes"
25         "e2mgr/configuration"
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 RanName = "test1"
39
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 TestAddE2TInstanceSuccess(t *testing.T) {
51         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
52
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)
60         assert.Nil(t, err)
61 }
62
63 func TestAddE2TInstanceHttpPostFailure(t *testing.T) {
64         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
65
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)
72         assert.NotNil(t, err)
73 }
74
75 func TestAddE2TInstanceFailure(t *testing.T) {
76         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
77
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)
85         assert.NotNil(t, err)
86 }
87
88 func TestAssociateRanToE2TInstance_Success(t *testing.T) {
89         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
90
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)
98         assert.Nil(t, err)
99 }
100
101 func TestAssociateRanToE2TInstance_RoutingManagerError(t *testing.T) {
102         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
103
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)
111 }
112
113 func TestAssociateRanToE2TInstance_RoutingManager_400(t *testing.T) {
114         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
115
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)
124 }
125
126 func TestDissociateRanE2TInstance_Success(t *testing.T) {
127         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
128
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)
136         assert.Nil(t, err)
137 }
138
139 func TestDissociateRanE2TInstance_RoutingManagerError(t *testing.T) {
140         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
141
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)
149 }
150
151 func TestDissociateRanE2TInstance_RoutingManager_400(t *testing.T) {
152         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
153
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)
162 }
163
164 // TODO: extract to test_utils
165 func initLog(t *testing.T) *logger.Logger {
166         log, err := logger.InitLogger(logger.InfoLevel)
167         if err != nil {
168                 t.Errorf("#delete_all_request_handler_test.TestHandleSuccessFlow - failed to initialize logger, error: %s", err)
169         }
170         return log
171 }
172
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)
180 //}