1 /*******************************************************************************
3 * Copyright (c) 2019 AT&T Intellectual Property.
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 *******************************************************************************/
22 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
23 // "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
24 "github.com/stretchr/testify/assert"
32 func TestGetRsmRanInfo(t *testing.T) {
33 sdl := &mocks.MockSdlInstance{}
34 reader := GetRsmReader(sdl)
36 key, _ := common.ValidateAndBuildNodeBNameKey(ranName)
37 infoAsGoType := models.RsmRanInfo{
44 infoAsDbType:= "{\"ranName\":\"test1\",\"enb1MeasurementId\":1,\"enb2MeasurementId\":2,\"action\":\"start\",\"actionStatus\":false}"
45 sdl.On("Get", []string{key}).Return(map[string]interface{}{key: infoAsDbType}, nil)
46 info, err := reader.GetRsmRanInfo(ranName)
48 t.Errorf("want: success, got: error: %v\n", err)
50 assert.Equal(t, info, &infoAsGoType)
53 func TestGetRsmRanInfoValidationError(t *testing.T) {
54 sdl := &mocks.MockSdlInstance{}
55 reader := GetRsmReader(sdl)
57 key, _ := common.ValidateAndBuildNodeBNameKey(ranName)
58 sdl.On("Get", []string{key}).Return(map[string]interface{}{key: ""}, nil)
59 _, err := reader.GetRsmRanInfo(ranName)
61 assert.Equal(t, err.Error(), "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received")
64 func TestGetRsmRanInfoDbError(t *testing.T) {
65 sdl := &mocks.MockSdlInstance{}
66 reader := GetRsmReader(sdl)
68 key, _ := common.ValidateAndBuildNodeBNameKey(ranName)
69 sdl.On("Get", []string{key}).Return((map[string]interface{})(nil), fmt.Errorf("db error"))
70 _, err := reader.GetRsmRanInfo(ranName)
72 assert.Equal(t, err.Error(), "db error")
75 func TestGetGeneralConfiguration(t *testing.T) {
76 sdl := &mocks.MockSdlInstance{}
77 reader := GetRsmReader(sdl)
78 var testCases = []struct {
79 cfgAsGoType models.RsmGeneralConfiguration
83 cfgAsGoType: models.RsmGeneralConfiguration{
84 EnableResourceStatus: true,
85 PartialSuccessAllowed: true,
87 TnlLoadIndPeriodic: true,
88 HwLoadIndPeriodic: true,
89 AbsStatusPeriodic: true,
90 RsrpMeasurementPeriodic: true,
92 PeriodicityMs: enums.ReportingPeriodicity_one_thousand_ms,
93 PeriodicityRsrpMeasurementMs: enums.ReportingPeriodicityRSRPMR_four_hundred_80_ms,
94 PeriodicityCsiMs: enums.ReportingPeriodicityCSIR_ms20,
97 cfgAsDbType: "{\"enableResourceStatus\":true,\"partialSuccessAllowed\":true,\"prbPeriodic\":true,\"tnlLoadIndPeriodic\":true,\"wwLoadIndPeriodic\":true,\"absStatusPeriodic\":true,\"rsrpMeasurementPeriodic\":true,\"csiPeriodic\":true,\"periodicityMs\":1,\"periodicityRsrpMeasurementMs\":3,\"periodicityCsiMs\":3}",
101 for _, tc := range testCases {
102 t.Run(tc.cfgAsDbType, func(t *testing.T) {
103 key := buildRsmGeneralConfigurationKey()
104 sdl.On("Get", []string{key}).Return(map[string]interface{}{key: tc.cfgAsDbType}, nil)
105 cfg, err := reader.GetRsmGeneralConfiguration()
107 t.Errorf("want: success, got: error: %v\n", err)
109 assert.Equal(t, cfg, &tc.cfgAsGoType)
114 func TestGetGeneralConfigurationNotFound(t *testing.T) {
115 sdl := &mocks.MockSdlInstance{}
116 reader := GetRsmReader(sdl)
118 key := buildRsmGeneralConfigurationKey()
119 sdl.On("Get", []string{key}).Return((map[string]interface{})(nil), nil)
120 _, err := reader.GetRsmGeneralConfiguration()
121 assert.NotNil(t, err)
122 assert.Equal(t, err.Error(), "#rsmReader.getByKeyAndUnmarshal - entity of type *models.RsmGeneralConfiguration not found. Key: CFG:GENERAL:v1.0.0")
125 func TestGetGeneralConfigurationDbError(t *testing.T) {
126 sdl := &mocks.MockSdlInstance{}
127 reader := GetRsmReader(sdl)
129 key := buildRsmGeneralConfigurationKey()
130 sdl.On("Get", []string{key}).Return((map[string]interface{})(nil), fmt.Errorf("db error"))
131 _, err := reader.GetRsmGeneralConfiguration()
132 assert.NotNil(t, err)
133 assert.Equal(t, err.Error(), "db error")
136 func TestGetGeneralConfigurationUnmarshalError(t *testing.T) {
137 sdl := &mocks.MockSdlInstance{}
138 reader := GetRsmReader(sdl)
139 cfgAsDbTYpe := "{\"enableResourceStatus\":true, partialSuccessAllowed\":true,\"prbPeriodic\":true,\"tnlLoadIndPeriodic\":true,\"wwLoadIndPeriodic\":true,\"absStatusPeriodic\":true,\"rsrpMeasurementPeriodic\":true,\"csiPeriodic\":true,\"periodicityMs\":1,\"periodicityRsrpMeasurementMs\":3,\"periodicityCsiMs\":3}"
140 key := buildRsmGeneralConfigurationKey()
141 sdl.On("Get", []string{key}).Return(map[string]interface{}{key: cfgAsDbTYpe}, nil)
142 _, err := reader.GetRsmGeneralConfiguration()
143 assert.NotNil(t, err)
144 assert.Equal(t, err.Error(), "invalid character 'p' looking for beginning of object key string")
149 Test execution depends on the existence of the environment variable DBAAS_SERVICE_HOST.
152 func TestGetGeneralConfigurationIntegration(t *testing.T) {
153 if len(os.Getenv("DBAAS_SERVICE_HOST")) == 0 {
156 db := sdlgo.NewDatabase()
157 sdl := sdlgo.NewSdlInstance("rsm", db)
158 reader := GetRsmReader(sdl)
159 cfgAsGoType := models.RsmGeneralConfiguration{
160 EnableResourceStatus: true,
161 PartialSuccessAllowed: true,
163 TnlLoadIndPeriodic: true,
164 HwLoadIndPeriodic: true,
165 AbsStatusPeriodic: true,
166 RsrpMeasurementPeriodic: true,
168 PeriodicityMs: enums.ReportingPeriodicity_one_thousand_ms,
169 PeriodicityRsrpMeasurementMs: enums.ReportingPeriodicityRSRPMR_four_hundred_80_ms,
170 PeriodicityCsiMs: enums.ReportingPeriodicityCSIR_ms20,
172 cfg, err := reader.GetRsmGeneralConfiguration()
174 t.Errorf("want: success, got: error: %v\n", err)
177 assert.Equal(t, &cfgAsGoType, cfg)