Add header missing license header
[ric-plt/resource-status-manager.git] / RSM / rsmdb / rsm_writer_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         "github.com/stretchr/testify/assert"
30         "rsm/enums"
31         "rsm/mocks"
32         "rsm/models"
33         "testing"
34 )
35
36 func TestSaveRsmRanInfo(t *testing.T) {
37         sdl := &mocks.MockSdlInstance{}
38         writer := GetRsmWriter(sdl)
39         ranName := "test1"
40         key, _ := common.ValidateAndBuildNodeBNameKey(ranName)
41         infoAsGoType := models.RsmRanInfo{
42                 RanName:           ranName,
43                 Enb1MeasurementId: 1,
44                 Enb2MeasurementId: 2,
45                 Action:            enums.Start,
46                 ActionStatus:      false,
47         }
48         infoAsDbType:= "{\"ranName\":\"test1\",\"enb1MeasurementId\":1,\"enb2MeasurementId\":2,\"action\":\"start\",\"actionStatus\":false}"
49         sdl.On("Set",[]interface{}{[]interface{}{key, []byte(infoAsDbType)}}).Return(nil)
50         err := writer.SaveRsmRanInfo(&infoAsGoType)
51         if err != nil {
52                 t.Errorf("want: success, got: error: %v\n", err)
53         }
54         sdl.AssertNumberOfCalls(t, "Set",1)
55 }
56
57
58 func TestSaveRsmRanInfoValidationError(t *testing.T) {
59         sdl := &mocks.MockSdlInstance{}
60         writer := GetRsmWriter(sdl)
61         ranName := ""
62         key, _ := common.ValidateAndBuildNodeBNameKey(ranName)
63         infoAsGoType := models.RsmRanInfo{
64                 RanName:           ranName,
65                 Enb1MeasurementId: 1,
66                 Enb2MeasurementId: 2,
67                 Action:            enums.Start,
68                 ActionStatus:      false,
69         }
70         infoAsDbType:= "{\"ranName\":\"test1\",\"enb1MeasurementId\":1,\"enb2MeasurementId\":2,\"action\":\"start\",\"actionStatus\":false}"
71         sdl.On("Set",[]interface{}{[]interface{}{key, []byte(infoAsDbType)}}).Return(nil)
72         err := writer.SaveRsmRanInfo(&infoAsGoType)
73         assert.NotNil(t, err)
74         assert.Equal(t, err.Error(), "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received")
75 }
76
77
78 func TestSaveGeneralConfiguration(t *testing.T) {
79         sdl := &mocks.MockSdlInstance{}
80         writer := GetRsmWriter(sdl)
81         var testCases = []struct {
82                 cfgAsGoType models.RsmGeneralConfiguration
83                 cfgAsDbType string
84         }{
85                 {
86                         cfgAsGoType: models.RsmGeneralConfiguration{
87                                 EnableResourceStatus:         true,
88                                 PartialSuccessAllowed:        true,
89                                 PrbPeriodic:                  true,
90                                 TnlLoadIndPeriodic:           true,
91                                 HwLoadIndPeriodic:            true,
92                                 AbsStatusPeriodic:            true,
93                                 RsrpMeasurementPeriodic:      true,
94                                 CsiPeriodic:                  true,
95                                 PeriodicityMs:                enums.ReportingPeriodicity_one_thousand_ms,
96                                 PeriodicityRsrpMeasurementMs: enums.ReportingPeriodicityRSRPMR_four_hundred_80_ms,
97                                 PeriodicityCsiMs:             enums.ReportingPeriodicityCSIR_ms20,
98                         },
99
100                         cfgAsDbType: "{\"enableResourceStatus\":true,\"partialSuccessAllowed\":true,\"prbPeriodic\":true,\"tnlLoadIndPeriodic\":true,\"wwLoadIndPeriodic\":true,\"absStatusPeriodic\":true,\"rsrpMeasurementPeriodic\":true,\"csiPeriodic\":true,\"periodicityMs\":1,\"periodicityRsrpMeasurementMs\":3,\"periodicityCsiMs\":3}",
101                 },
102         }
103
104         for _, tc := range testCases {
105                 t.Run(tc.cfgAsDbType, func(t *testing.T) {
106                         key:= buildRsmGeneralConfigurationKey()
107                         sdl.On("Set",[]interface{}{[]interface{}{key, []byte(tc.cfgAsDbType)}}).Return(nil)
108                         err := writer.SaveRsmGeneralConfiguration(&tc.cfgAsGoType)
109                         if err != nil {
110                                 t.Errorf("want: success, got: error: %v\n", err)
111                         }
112
113                         sdl.AssertNumberOfCalls(t, "Set",1)
114                 })
115         }
116 }
117
118
119 func TestSaveGeneralConfigurationDbError(t *testing.T) {
120         sdl := &mocks.MockSdlInstance{}
121         writer := GetRsmWriter(sdl)
122
123         cfgAsGoType:= models.RsmGeneralConfiguration{
124                 EnableResourceStatus:         true,
125                 PartialSuccessAllowed:        true,
126                 PrbPeriodic:                  true,
127                 TnlLoadIndPeriodic:           true,
128                 HwLoadIndPeriodic:            true,
129                 AbsStatusPeriodic:            true,
130                 RsrpMeasurementPeriodic:      true,
131                 CsiPeriodic:                  true,
132                 PeriodicityMs:                enums.ReportingPeriodicity_one_thousand_ms,
133                 PeriodicityRsrpMeasurementMs: enums.ReportingPeriodicityRSRPMR_four_hundred_80_ms,
134                 PeriodicityCsiMs:             enums.ReportingPeriodicityCSIR_ms20,
135         }
136         cfgAsDbTYpe:= "{\"enableResourceStatus\":true,\"partialSuccessAllowed\":true,\"prbPeriodic\":true,\"tnlLoadIndPeriodic\":true,\"wwLoadIndPeriodic\":true,\"absStatusPeriodic\":true,\"rsrpMeasurementPeriodic\":true,\"csiPeriodic\":true,\"periodicityMs\":1,\"periodicityRsrpMeasurementMs\":3,\"periodicityCsiMs\":3}"
137         key:= buildRsmGeneralConfigurationKey()
138         sdl.On("Set",[]interface{}{[]interface{}{key, []byte(cfgAsDbTYpe)}}).Return(fmt.Errorf("db error"))
139         err := writer.SaveRsmGeneralConfiguration(&cfgAsGoType)
140         assert.NotNil(t, err)
141         assert.Equal(t, err.Error(), "db error")
142 }