Add R5 content to master
[ric-plt/e2mgr.git] / E2Manager / handlers / httpmsghandlers / set_general_configuration_handler_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 httpmsghandlers
21
22 import (
23         "e2mgr/configuration"
24         "e2mgr/mocks"
25         "e2mgr/models"
26         "e2mgr/services"
27         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
28         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
29         "github.com/pkg/errors"
30         "github.com/stretchr/testify/assert"
31         "testing"
32 )
33
34 func setupSetGeneralConfigurationHandlerTest(t *testing.T) (*SetGeneralConfigurationHandler, *mocks.RnibReaderMock, *mocks.RnibWriterMock) {
35         log := initLog(t)
36         config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
37         readerMock := &mocks.RnibReaderMock{}
38         writerMock := &mocks.RnibWriterMock{}
39         rnibDataService := services.NewRnibDataService(log, config, readerMock, writerMock)
40         handler := NewSetGeneralConfigurationHandler(log, rnibDataService)
41         return handler, readerMock, writerMock
42 }
43
44 func TestSetGeneralConfigurationFalse_Success(t *testing.T) {
45         handler, readerMock, writerMock := setupSetGeneralConfigurationHandlerTest(t)
46
47         configuration := &entities.GeneralConfiguration{EnableRic: true}
48         readerMock.On("GetGeneralConfiguration").Return(configuration, nil)
49
50         updated := &entities.GeneralConfiguration{EnableRic: false}
51         writerMock.On("SaveGeneralConfiguration", updated).Return(nil)
52
53         response, err := handler.Handle(models.GeneralConfigurationRequest{EnableRic: false})
54
55         assert.Nil(t, err)
56         assert.NotNil(t, response)
57         assert.IsType(t, &models.GeneralConfigurationResponse{}, response)
58 }
59
60 func TestSetGeneralConfigurationTrue_Success(t *testing.T) {
61         handler, readerMock, writerMock := setupSetGeneralConfigurationHandlerTest(t)
62
63         configuration := &entities.GeneralConfiguration{EnableRic: false}
64         readerMock.On("GetGeneralConfiguration").Return(configuration, nil)
65
66         updated := &entities.GeneralConfiguration{EnableRic: true}
67         writerMock.On("SaveGeneralConfiguration", updated).Return(nil)
68
69         response, err := handler.Handle(models.GeneralConfigurationRequest{EnableRic: true})
70
71         assert.Nil(t, err)
72         assert.NotNil(t, response)
73         assert.IsType(t, &models.GeneralConfigurationResponse{}, response)
74 }
75
76 func TestSetGeneralConfigurationIgnore_Success(t *testing.T) {
77         handler, readerMock, _ := setupSetGeneralConfigurationHandlerTest(t)
78
79         configuration := &entities.GeneralConfiguration{EnableRic: false}
80         readerMock.On("GetGeneralConfiguration").Return(configuration, nil)
81
82         response, err := handler.Handle(models.GeneralConfigurationRequest{EnableRic: false})
83
84         assert.Nil(t, err)
85         assert.NotNil(t, response)
86         assert.IsType(t, &models.GeneralConfigurationResponse{}, response)
87 }
88
89 func TestSetGeneralConfigurationHandlerRnibError(t *testing.T) {
90         handler, readerMock, writerMock := setupSetGeneralConfigurationHandlerTest(t)
91
92         configuration := &entities.GeneralConfiguration{EnableRic: false}
93         readerMock.On("GetGeneralConfiguration").Return(configuration, nil)
94
95         updated := &entities.GeneralConfiguration{EnableRic: true}
96         writerMock.On("SaveGeneralConfiguration", updated).Return(common.NewInternalError(errors.New("error")))
97
98         response, err := handler.Handle(models.GeneralConfigurationRequest{EnableRic: true})
99
100         assert.NotNil(t, err)
101         assert.Nil(t, response)
102 }