e1703dfad453334791789bff25b4072a9db9753f
[ric-plt/resource-status-manager.git] / RSM / rsmdb / rsm_reader_test.go
1 /*******************************************************************************
2  *
3  *   Copyright (c) 2019 AT&T Intellectual Property.
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 rsmdb
19
20 import (
21         "fmt"
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"
25 //      "os"
26         "rsm/enums"
27         "rsm/mocks"
28         "rsm/models"
29         "testing"
30 )
31
32 func TestGetRsmRanInfo(t *testing.T) {
33         sdl := &mocks.MockSdlInstance{}
34         reader := GetRsmReader(sdl)
35         ranName := "test1"
36         key, _ := common.ValidateAndBuildNodeBNameKey(ranName)
37         infoAsGoType := models.RsmRanInfo{
38                 RanName:           ranName,
39                 Enb1MeasurementId: 1,
40                 Enb2MeasurementId: 2,
41                 Action:            enums.Start,
42                 ActionStatus:      false,
43         }
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)
47         if err != nil {
48                 t.Errorf("want: success, got: error: %v\n", err)
49         }
50         assert.Equal(t, info, &infoAsGoType)
51 }
52
53 func TestGetRsmRanInfoValidationError(t *testing.T) {
54         sdl := &mocks.MockSdlInstance{}
55         reader := GetRsmReader(sdl)
56         ranName := ""
57         key, _ := common.ValidateAndBuildNodeBNameKey(ranName)
58         sdl.On("Get", []string{key}).Return(map[string]interface{}{key: ""}, nil)
59         _, err := reader.GetRsmRanInfo(ranName)
60         assert.NotNil(t, err)
61         assert.Equal(t, err.Error(), "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received")
62 }
63
64 func TestGetRsmRanInfoDbError(t *testing.T) {
65         sdl := &mocks.MockSdlInstance{}
66         reader := GetRsmReader(sdl)
67         ranName := "test1"
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)
71         assert.NotNil(t, err)
72         assert.Equal(t, err.Error(), "db error")
73 }
74
75 func TestGetGeneralConfiguration(t *testing.T) {
76         sdl := &mocks.MockSdlInstance{}
77         reader := GetRsmReader(sdl)
78         var testCases = []struct {
79                 cfgAsGoType models.RsmGeneralConfiguration
80                 cfgAsDbType string
81         }{
82                 {
83                         cfgAsGoType: models.RsmGeneralConfiguration{
84                                 EnableResourceStatus:         true,
85                                 PartialSuccessAllowed:        true,
86                                 PrbPeriodic:                  true,
87                                 TnlLoadIndPeriodic:           true,
88                                 HwLoadIndPeriodic:            true,
89                                 AbsStatusPeriodic:            true,
90                                 RsrpMeasurementPeriodic:      true,
91                                 CsiPeriodic:                  true,
92                                 PeriodicityMs:                enums.ReportingPeriodicity_one_thousand_ms,
93                                 PeriodicityRsrpMeasurementMs: enums.ReportingPeriodicityRSRPMR_four_hundred_80_ms,
94                                 PeriodicityCsiMs:             enums.ReportingPeriodicityCSIR_ms20,
95                         },
96
97                         cfgAsDbType: "{\"enableResourceStatus\":true,\"partialSuccessAllowed\":true,\"prbPeriodic\":true,\"tnlLoadIndPeriodic\":true,\"wwLoadIndPeriodic\":true,\"absStatusPeriodic\":true,\"rsrpMeasurementPeriodic\":true,\"csiPeriodic\":true,\"periodicityMs\":1,\"periodicityRsrpMeasurementMs\":3,\"periodicityCsiMs\":3}",
98                 },
99         }
100
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()
106                         if err != nil {
107                                 t.Errorf("want: success, got: error: %v\n", err)
108                         }
109                         assert.Equal(t, cfg, &tc.cfgAsGoType)
110                 })
111         }
112 }
113
114 func TestGetGeneralConfigurationNotFound(t *testing.T) {
115         sdl := &mocks.MockSdlInstance{}
116         reader := GetRsmReader(sdl)
117
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")
123 }
124
125 func TestGetGeneralConfigurationDbError(t *testing.T) {
126         sdl := &mocks.MockSdlInstance{}
127         reader := GetRsmReader(sdl)
128
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")
134 }
135
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")
145 }
146
147 /*
148 Test against redis.
149 Test execution depends on the existence of the environment variable DBAAS_SERVICE_HOST.
150 *
151
152 func TestGetGeneralConfigurationIntegration(t *testing.T) {
153         if len(os.Getenv("DBAAS_SERVICE_HOST")) == 0 {
154                 return
155         }
156         db := sdlgo.NewDatabase()
157         sdl := sdlgo.NewSdlInstance("rsm", db)
158         reader := GetRsmReader(sdl)
159         cfgAsGoType := models.RsmGeneralConfiguration{
160                 EnableResourceStatus:         true,
161                 PartialSuccessAllowed:        true,
162                 PrbPeriodic:                  true,
163                 TnlLoadIndPeriodic:           true,
164                 HwLoadIndPeriodic:            true,
165                 AbsStatusPeriodic:            true,
166                 RsrpMeasurementPeriodic:      true,
167                 CsiPeriodic:                  true,
168                 PeriodicityMs:                enums.ReportingPeriodicity_one_thousand_ms,
169                 PeriodicityRsrpMeasurementMs: enums.ReportingPeriodicityRSRPMR_four_hundred_80_ms,
170                 PeriodicityCsiMs:             enums.ReportingPeriodicityCSIR_ms20,
171         }
172         cfg, err := reader.GetRsmGeneralConfiguration()
173         if err != nil {
174                 t.Errorf("want: success, got: error: %v\n", err)
175         }
176
177         assert.Equal(t, &cfgAsGoType, cfg)
178 }
179 */