Add header missing license header
[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
19 /*
20 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
21 * platform project (RICP).
22 */
23
24 package rsmdb
25
26 import (
27         "fmt"
28         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
29 //      "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
30         "github.com/stretchr/testify/assert"
31 //      "os"
32         "rsm/enums"
33         "rsm/mocks"
34         "rsm/models"
35         "testing"
36 )
37
38 func TestGetRsmRanInfo(t *testing.T) {
39         sdl := &mocks.MockSdlInstance{}
40         reader := GetRsmReader(sdl)
41         ranName := "test1"
42         key, _ := common.ValidateAndBuildNodeBNameKey(ranName)
43         infoAsGoType := models.RsmRanInfo{
44                 RanName:           ranName,
45                 Enb1MeasurementId: 1,
46                 Enb2MeasurementId: 2,
47                 Action:            enums.Start,
48                 ActionStatus:      false,
49         }
50         infoAsDbType:= "{\"ranName\":\"test1\",\"enb1MeasurementId\":1,\"enb2MeasurementId\":2,\"action\":\"start\",\"actionStatus\":false}"
51         sdl.On("Get", []string{key}).Return(map[string]interface{}{key: infoAsDbType}, nil)
52         info, err := reader.GetRsmRanInfo(ranName)
53         if err != nil {
54                 t.Errorf("want: success, got: error: %v\n", err)
55         }
56         assert.Equal(t, info, &infoAsGoType)
57 }
58
59 func TestGetRsmRanInfoValidationError(t *testing.T) {
60         sdl := &mocks.MockSdlInstance{}
61         reader := GetRsmReader(sdl)
62         ranName := ""
63         key, _ := common.ValidateAndBuildNodeBNameKey(ranName)
64         sdl.On("Get", []string{key}).Return(map[string]interface{}{key: ""}, nil)
65         _, err := reader.GetRsmRanInfo(ranName)
66         assert.NotNil(t, err)
67         assert.Equal(t, err.Error(), "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received")
68 }
69
70 func TestGetRsmRanInfoDbError(t *testing.T) {
71         sdl := &mocks.MockSdlInstance{}
72         reader := GetRsmReader(sdl)
73         ranName := "test1"
74         key, _ := common.ValidateAndBuildNodeBNameKey(ranName)
75         sdl.On("Get", []string{key}).Return((map[string]interface{})(nil), fmt.Errorf("db error"))
76         _, err := reader.GetRsmRanInfo(ranName)
77         assert.NotNil(t, err)
78         assert.Equal(t, err.Error(), "db error")
79 }
80
81 func TestGetGeneralConfiguration(t *testing.T) {
82         sdl := &mocks.MockSdlInstance{}
83         reader := GetRsmReader(sdl)
84         var testCases = []struct {
85                 cfgAsGoType models.RsmGeneralConfiguration
86                 cfgAsDbType string
87         }{
88                 {
89                         cfgAsGoType: models.RsmGeneralConfiguration{
90                                 EnableResourceStatus:         true,
91                                 PartialSuccessAllowed:        true,
92                                 PrbPeriodic:                  true,
93                                 TnlLoadIndPeriodic:           true,
94                                 HwLoadIndPeriodic:            true,
95                                 AbsStatusPeriodic:            true,
96                                 RsrpMeasurementPeriodic:      true,
97                                 CsiPeriodic:                  true,
98                                 PeriodicityMs:                enums.ReportingPeriodicity_one_thousand_ms,
99                                 PeriodicityRsrpMeasurementMs: enums.ReportingPeriodicityRSRPMR_four_hundred_80_ms,
100                                 PeriodicityCsiMs:             enums.ReportingPeriodicityCSIR_ms20,
101                         },
102
103                         cfgAsDbType: "{\"enableResourceStatus\":true,\"partialSuccessAllowed\":true,\"prbPeriodic\":true,\"tnlLoadIndPeriodic\":true,\"wwLoadIndPeriodic\":true,\"absStatusPeriodic\":true,\"rsrpMeasurementPeriodic\":true,\"csiPeriodic\":true,\"periodicityMs\":1,\"periodicityRsrpMeasurementMs\":3,\"periodicityCsiMs\":3}",
104                 },
105         }
106
107         for _, tc := range testCases {
108                 t.Run(tc.cfgAsDbType, func(t *testing.T) {
109                         key := buildRsmGeneralConfigurationKey()
110                         sdl.On("Get", []string{key}).Return(map[string]interface{}{key: tc.cfgAsDbType}, nil)
111                         cfg, err := reader.GetRsmGeneralConfiguration()
112                         if err != nil {
113                                 t.Errorf("want: success, got: error: %v\n", err)
114                         }
115                         assert.Equal(t, cfg, &tc.cfgAsGoType)
116                 })
117         }
118 }
119
120 func TestGetGeneralConfigurationNotFound(t *testing.T) {
121         sdl := &mocks.MockSdlInstance{}
122         reader := GetRsmReader(sdl)
123
124         key := buildRsmGeneralConfigurationKey()
125         sdl.On("Get", []string{key}).Return((map[string]interface{})(nil), nil)
126         _, err := reader.GetRsmGeneralConfiguration()
127         assert.NotNil(t, err)
128         assert.Equal(t, err.Error(), "#rsmReader.getByKeyAndUnmarshal - entity of type *models.RsmGeneralConfiguration not found. Key: CFG:GENERAL:v1.0.0")
129 }
130
131 func TestGetGeneralConfigurationDbError(t *testing.T) {
132         sdl := &mocks.MockSdlInstance{}
133         reader := GetRsmReader(sdl)
134
135         key := buildRsmGeneralConfigurationKey()
136         sdl.On("Get", []string{key}).Return((map[string]interface{})(nil), fmt.Errorf("db error"))
137         _, err := reader.GetRsmGeneralConfiguration()
138         assert.NotNil(t, err)
139         assert.Equal(t, err.Error(), "db error")
140 }
141
142 func TestGetGeneralConfigurationUnmarshalError(t *testing.T) {
143         sdl := &mocks.MockSdlInstance{}
144         reader := GetRsmReader(sdl)
145         cfgAsDbTYpe := "{\"enableResourceStatus\":true, partialSuccessAllowed\":true,\"prbPeriodic\":true,\"tnlLoadIndPeriodic\":true,\"wwLoadIndPeriodic\":true,\"absStatusPeriodic\":true,\"rsrpMeasurementPeriodic\":true,\"csiPeriodic\":true,\"periodicityMs\":1,\"periodicityRsrpMeasurementMs\":3,\"periodicityCsiMs\":3}"
146         key := buildRsmGeneralConfigurationKey()
147         sdl.On("Get", []string{key}).Return(map[string]interface{}{key: cfgAsDbTYpe}, nil)
148         _, err := reader.GetRsmGeneralConfiguration()
149         assert.NotNil(t, err)
150         assert.Equal(t, err.Error(), "invalid character 'p' looking for beginning of object key string")
151 }
152
153 /*
154 Test against redis.
155 Test execution depends on the existence of the environment variable DBAAS_SERVICE_HOST.
156 *
157
158 func TestGetGeneralConfigurationIntegration(t *testing.T) {
159         if len(os.Getenv("DBAAS_SERVICE_HOST")) == 0 {
160                 return
161         }
162         db := sdlgo.NewDatabase()
163         sdl := sdlgo.NewSdlInstance("rsm", db)
164         reader := GetRsmReader(sdl)
165         cfgAsGoType := models.RsmGeneralConfiguration{
166                 EnableResourceStatus:         true,
167                 PartialSuccessAllowed:        true,
168                 PrbPeriodic:                  true,
169                 TnlLoadIndPeriodic:           true,
170                 HwLoadIndPeriodic:            true,
171                 AbsStatusPeriodic:            true,
172                 RsrpMeasurementPeriodic:      true,
173                 CsiPeriodic:                  true,
174                 PeriodicityMs:                enums.ReportingPeriodicity_one_thousand_ms,
175                 PeriodicityRsrpMeasurementMs: enums.ReportingPeriodicityRSRPMR_four_hundred_80_ms,
176                 PeriodicityCsiMs:             enums.ReportingPeriodicityCSIR_ms20,
177         }
178         cfg, err := reader.GetRsmGeneralConfiguration()
179         if err != nil {
180                 t.Errorf("want: success, got: error: %v\n", err)
181         }
182
183         assert.Equal(t, &cfgAsGoType, cfg)
184 }
185 */