8ac2b3e4fce62e06e659fce7f286f65b5677d87b
[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/logger"
26         "e2mgr/mocks"
27         "e2mgr/models"
28         "encoding/json"
29         "github.com/pkg/errors"
30         "github.com/stretchr/testify/assert"
31         "io/ioutil"
32         "net/http"
33         "testing"
34 )
35
36 const E2TAddress = "10.0.2.15:38000"
37
38 // TODO: add response Body and dont check for nil in prod code. itll always be populated
39
40 func initRoutingManagerClientTest(t *testing.T) (*RoutingManagerClient, *mocks.HttpClientMock, *configuration.Configuration) {
41         logger := initLog(t)
42         config := &configuration.Configuration{}
43         config.RoutingManager.BaseUrl = "http://iltlv740.intl.att.com:8080/ric/v1/handles/"
44         httpClientMock := &mocks.HttpClientMock{}
45         rmClient := NewRoutingManagerClient(logger, config, httpClientMock)
46         return rmClient, httpClientMock, config
47 }
48
49 func TestAddE2TInstanceSuccess(t *testing.T) {
50         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
51
52         data := models.NewRoutingManagerE2TData(E2TAddress)
53         marshaled, _ := json.Marshal(data)
54         body := bytes.NewBuffer(marshaled)
55         url := config.RoutingManager.BaseUrl + "e2t"
56         respBody := ioutil.NopCloser(bytes.NewBufferString(""))
57         httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
58         err := rmClient.AddE2TInstance(E2TAddress)
59         assert.Nil(t, err)
60 }
61
62 func TestAddE2TInstanceHttpPostFailure(t *testing.T) {
63         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
64
65         data := models.NewRoutingManagerE2TData(E2TAddress)
66         marshaled, _ := json.Marshal(data)
67         body := bytes.NewBuffer(marshaled)
68         url := config.RoutingManager.BaseUrl + "e2t"
69         httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
70         err := rmClient.AddE2TInstance(E2TAddress)
71         assert.NotNil(t, err)
72 }
73
74 func TestAddE2TInstanceFailure(t *testing.T) {
75         rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
76
77         data := models.NewRoutingManagerE2TData(E2TAddress)
78         marshaled, _ := json.Marshal(data)
79         body := bytes.NewBuffer(marshaled)
80         url := config.RoutingManager.BaseUrl + "e2t"
81         respBody := ioutil.NopCloser(bytes.NewBufferString(""))
82         httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body:respBody}, nil)
83         err := rmClient.AddE2TInstance(E2TAddress)
84         assert.NotNil(t, err)
85 }
86
87 // TODO: extract to test_utils
88 func initLog(t *testing.T) *logger.Logger {
89         log, err := logger.InitLogger(logger.InfoLevel)
90         if err != nil {
91                 t.Errorf("#delete_all_request_handler_test.TestHandleSuccessFlow - failed to initialize logger, error: %s", err)
92         }
93         return log
94 }
95
96 //func TestAddE2TInstanceInteg(t *testing.T) {
97 //      logger := initLog(t)
98 //      config := configuration.ParseConfiguration()
99 //      httpClient := &http.Client{}
100 //      rmClient := NewRoutingManagerClient(logger, config, httpClient)
101 //      err := rmClient.AddE2TInstance(E2TAddress)
102 //      assert.Nil(t, err)
103 //}