Add R5 content to master
[ric-plt/e2mgr.git] / E2Manager / controllers / e2t_controller_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
18 package controllers
19
20 import (
21         "e2mgr/configuration"
22         "e2mgr/managers"
23         "e2mgr/mocks"
24         "e2mgr/models"
25         "e2mgr/providers/httpmsghandlerprovider"
26         "e2mgr/services"
27         "encoding/json"
28         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
29         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
30         "github.com/magiconair/properties/assert"
31         "github.com/pkg/errors"
32         "io/ioutil"
33         "net/http"
34         "net/http/httptest"
35         "testing"
36 )
37
38 const E2TAddress string = "10.0.2.15:38000"
39 const E2TAddress2 string = "10.0.2.16:38001"
40
41 type controllerE2TInstancesTestContext struct {
42         e2tAddresses         []string
43         e2tInstances         []*entities.E2TInstance
44         error                error
45         expectedStatusCode   int
46         expectedJsonResponse string
47 }
48
49 func setupE2TControllerTest(t *testing.T) (*E2TController, *mocks.RnibReaderMock) {
50         log := initLog(t)
51         config := configuration.ParseConfiguration()
52
53         readerMock := &mocks.RnibReaderMock{}
54
55         rnibDataService := services.NewRnibDataService(log, config, readerMock, nil)
56         e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, log)
57         handlerProvider := httpmsghandlerprovider.NewIncomingRequestHandlerProvider(log, nil, config, rnibDataService, nil, e2tInstancesManager, &managers.E2TAssociationManager{}, nil)
58         controller := NewE2TController(log, handlerProvider)
59         return controller, readerMock
60 }
61
62 func controllerGetE2TInstancesTestExecuter(t *testing.T, context *controllerE2TInstancesTestContext) {
63         controller, readerMock := setupE2TControllerTest(t)
64         writer := httptest.NewRecorder()
65         readerMock.On("GetE2TAddresses").Return(context.e2tAddresses, context.error)
66
67         if context.e2tInstances != nil {
68                 readerMock.On("GetE2TInstances", context.e2tAddresses).Return(context.e2tInstances, context.error)
69         }
70
71         req, _ := http.NewRequest("GET", "/e2t/list", nil)
72         controller.GetE2TInstances(writer, req)
73         assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
74         bodyBytes, _ := ioutil.ReadAll(writer.Body)
75         assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
76         readerMock.AssertExpectations(t)
77 }
78
79 func TestControllerGetE2TInstancesSuccess(t *testing.T) {
80         ranNames1 := []string{"test1", "test2", "test3"}
81         e2tInstanceResponseModel1 := models.NewE2TInstanceResponseModel(E2TAddress, ranNames1)
82         e2tInstanceResponseModel2 := models.NewE2TInstanceResponseModel(E2TAddress2, []string{})
83         e2tInstancesResponse := models.E2TInstancesResponse{e2tInstanceResponseModel1, e2tInstanceResponseModel2}
84         bytes, _ := json.Marshal(e2tInstancesResponse)
85
86         context := controllerE2TInstancesTestContext{
87                 e2tAddresses:         []string{E2TAddress, E2TAddress2},
88                 e2tInstances:         []*entities.E2TInstance{{Address: E2TAddress, AssociatedRanList: ranNames1}, {Address: E2TAddress2, AssociatedRanList: []string{}}},
89                 error:                nil,
90                 expectedStatusCode:   http.StatusOK,
91                 expectedJsonResponse: string(bytes),
92         }
93
94         controllerGetE2TInstancesTestExecuter(t, &context)
95 }
96
97 func TestControllerGetE2TInstancesEmptySuccess(t *testing.T) {
98         e2tInstancesResponse := models.E2TInstancesResponse{}
99         bytes, _ := json.Marshal(e2tInstancesResponse)
100
101         context := controllerE2TInstancesTestContext{
102                 e2tAddresses:         []string{},
103                 e2tInstances:         nil,
104                 error:                nil,
105                 expectedStatusCode:   http.StatusOK,
106                 expectedJsonResponse: string(bytes),
107         }
108
109         controllerGetE2TInstancesTestExecuter(t, &context)
110 }
111
112 func TestControllerGetE2TInstancesInternal(t *testing.T) {
113         context := controllerE2TInstancesTestContext{
114                 e2tAddresses:         nil,
115                 e2tInstances:         nil,
116                 error:                common.NewInternalError(errors.New("error")),
117                 expectedStatusCode:   http.StatusInternalServerError,
118                 expectedJsonResponse: "{\"errorCode\":500,\"errorMessage\":\"RNIB error\"}",
119         }
120
121         controllerGetE2TInstancesTestExecuter(t, &context)
122 }
123
124 func TestInvalidRequestName(t *testing.T) {
125         controller, _ := setupE2TControllerTest(t)
126
127         writer := httptest.NewRecorder()
128
129         header := &http.Header{}
130
131         controller.handleRequest(writer, header, "", nil, true)
132
133         var errorResponse = parseJsonRequest(t, writer.Body)
134
135         assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
136         assert.Equal(t, errorResponse.Code, 501)
137 }