2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
20 package httpmsghandlers
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"
34 func setupSetGeneralConfigurationHandlerTest(t *testing.T) (*SetGeneralConfigurationHandler, *mocks.RnibReaderMock, *mocks.RnibWriterMock) {
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
44 func TestSetGeneralConfigurationFalse_Success(t *testing.T) {
45 handler, readerMock, writerMock := setupSetGeneralConfigurationHandlerTest(t)
47 configuration := &entities.GeneralConfiguration{EnableRic: true}
48 readerMock.On("GetGeneralConfiguration").Return(configuration, nil)
50 updated := &entities.GeneralConfiguration{EnableRic: false}
51 writerMock.On("SaveGeneralConfiguration", updated).Return(nil)
53 response, err := handler.Handle(models.GeneralConfigurationRequest{EnableRic: false})
56 assert.NotNil(t, response)
57 assert.IsType(t, &models.GeneralConfigurationResponse{}, response)
60 func TestSetGeneralConfigurationTrue_Success(t *testing.T) {
61 handler, readerMock, writerMock := setupSetGeneralConfigurationHandlerTest(t)
63 configuration := &entities.GeneralConfiguration{EnableRic: false}
64 readerMock.On("GetGeneralConfiguration").Return(configuration, nil)
66 updated := &entities.GeneralConfiguration{EnableRic: true}
67 writerMock.On("SaveGeneralConfiguration", updated).Return(nil)
69 response, err := handler.Handle(models.GeneralConfigurationRequest{EnableRic: true})
72 assert.NotNil(t, response)
73 assert.IsType(t, &models.GeneralConfigurationResponse{}, response)
76 func TestSetGeneralConfigurationIgnore_Success(t *testing.T) {
77 handler, readerMock, _ := setupSetGeneralConfigurationHandlerTest(t)
79 configuration := &entities.GeneralConfiguration{EnableRic: false}
80 readerMock.On("GetGeneralConfiguration").Return(configuration, nil)
82 response, err := handler.Handle(models.GeneralConfigurationRequest{EnableRic: false})
85 assert.NotNil(t, response)
86 assert.IsType(t, &models.GeneralConfigurationResponse{}, response)
89 func TestSetGeneralConfigurationHandlerRnibError(t *testing.T) {
90 handler, readerMock, writerMock := setupSetGeneralConfigurationHandlerTest(t)
92 configuration := &entities.GeneralConfiguration{EnableRic: false}
93 readerMock.On("GetGeneralConfiguration").Return(configuration, nil)
95 updated := &entities.GeneralConfiguration{EnableRic: true}
96 writerMock.On("SaveGeneralConfiguration", updated).Return(common.NewInternalError(errors.New("error")))
98 response, err := handler.Handle(models.GeneralConfigurationRequest{EnableRic: true})
100 assert.NotNil(t, err)
101 assert.Nil(t, response)