blob: 325dd9ce6c8c10c238f9332b86ec58e351aa94d6 [file] [log] [blame]
vinokuma703a70b2023-07-17 10:06:43 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16package application
17
18import (
19 "context"
20 "encoding/json"
21 "errors"
22 "testing"
23 cntlr "voltha-go-controller/internal/pkg/controller"
24 "voltha-go-controller/internal/test/mocks"
25
26 "github.com/golang/mock/gomock"
27 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
28 "github.com/stretchr/testify/assert"
29)
30
31var vm = &VoltMeter{
32 Name: "test_name",
33 Version: "test_version",
34}
35var write_to_db_error = "WriteToDb_error"
36var invalid_value = "invalid_value"
37
38func TestVoltApplication_DelMeterProf(t *testing.T) {
39 type args struct {
40 cntx context.Context
41 name string
42 }
43 tests := []struct {
44 name string
45 args args
46 wantErr bool
47 }{
48 {
49 name: "VoltApplication_DelMeterProf",
50 args: args{
51 cntx: context.Background(),
52 name: "test_name",
53 },
54 },
55 {
56 name: "GetMeterByName_!ok",
57 args: args{
58 cntx: context.Background(),
59 name: "test_name",
60 },
61 },
62 {
63 name: "cfg.AssociatedServices != 0",
64 args: args{
65 cntx: context.Background(),
66 name: "test_name",
67 },
68 },
69 {
70 name: "delmeterFromDevice",
71 args: args{
72 cntx: context.Background(),
73 name: "test_name",
74 },
75 },
76 }
77 for _, tt := range tests {
78 t.Run(tt.name, func(t *testing.T) {
79 va := &VoltApplication{}
80 switch tt.name {
81 case "VoltApplication_DelMeterProf":
82 vm1 := &VoltMeter{
83 Name: "test_name",
84 Version: "test_version",
85 }
86 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
87 db = dbintf
88 dbintf.EXPECT().DelMeter(tt.args.cntx, tt.args.name).Return(nil).Times(1)
89 va.MeterMgr.Meters.Store("test_name", vm1)
90 if err := va.DelMeterProf(tt.args.cntx, tt.args.name); (err != nil) != tt.wantErr {
91 t.Errorf("VoltApplication.DelMeterProf() error = %v, wantErr %v", err, tt.wantErr)
92 }
93 case "GetMeterByName_!ok":
94 vm2 := &VoltMeter{
95 Name: "test_name",
96 Version: "test_version",
97 }
98 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
99 db = dbintf
100 va.MeterMgr.Meters.Store("test_name1", vm2)
101 err := va.DelMeterProf(tt.args.cntx, tt.args.name)
102 assert.NotNil(t, err)
103 case "cfg.AssociatedServices != 0":
104 vm3 := &VoltMeter{
105 Name: "test_name",
106 Version: "test_version",
107 AssociatedServices: 1,
108 }
109 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
110 db = dbintf
111 va.MeterMgr.Meters.Store("test_name", vm3)
112 err := va.DelMeterProf(tt.args.cntx, tt.args.name)
113 assert.NotNil(t, err)
114 case "delmeterFromDevice":
115 vd := &VoltDevice{
116 Name: test_device,
117 }
118 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
119 db = dbintf
120 _ = cntlr.NewController(context.Background(), mocks.NewMockApp(gomock.NewController(t)))
121 dbintf.EXPECT().DelMeter(tt.args.cntx, tt.args.name).Return(nil).Times(1)
122 va.MeterMgr.Meters.Store("test_name", vm)
123 va.DevicesDisc.Store(test_device, vd)
124 if err := va.DelMeterProf(tt.args.cntx, tt.args.name); (err != nil) != tt.wantErr {
125 t.Errorf("VoltApplication.DelMeterProf() error = %v, wantErr %v", err, tt.wantErr)
126 }
127 }
128 })
129 }
130}
131
132func TestMeterMgr_GetMeterByProfID(t *testing.T) {
133 type args struct {
134 id uint32
135 }
136 tests := []struct {
137 name string
138 args args
139 want *VoltMeter
140 wantErr bool
141 }{
142 {
143 name: "MeterMgr_GetMeterByProfID",
144 args: args{
145 id: uint32(1),
146 },
147 },
148 }
149 for _, tt := range tests {
150 t.Run(tt.name, func(t *testing.T) {
151 m := &MeterMgr{}
152 vm4 := &VoltMeter{
153 Name: "test_name",
154 }
155 m.MetersByID.Store(tt.args.id, vm4)
156 got, err := m.GetMeterByProfID(tt.args.id)
157 if (err != nil) != tt.wantErr {
158 t.Errorf("MeterMgr.GetMeterByProfID() error = %v, wantErr %v", err, tt.wantErr)
159 return
160 }
161 assert.NotNil(t, got)
162 })
163 }
164}
165
166func TestVoltApplication_UpdateMeterProf(t *testing.T) {
167 type args struct {
168 cntx context.Context
169 cfg VoltMeter
170 }
171 tests := []struct {
172 name string
173 args args
174 }{
175 {
176 name: "VoltApplication_UpdateMeterProf",
177 args: args{
178 cntx: context.Background(),
179 cfg: VoltMeter{
180 Name: "test_name",
181 },
182 },
183 },
184 {
185 name: write_to_db_error,
186 args: args{
187 cntx: context.Background(),
188 cfg: VoltMeter{
189 Name: "test_name",
190 },
191 },
192 },
193 }
194 for _, tt := range tests {
195 t.Run(tt.name, func(t *testing.T) {
196 va := &VoltApplication{}
197 switch tt.name {
198 case "VoltApplication_UpdateMeterProf":
199 va.MeterMgr.Meters.Store("test_name", vm)
200 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
201 db = dbintf
202 dbintf.EXPECT().PutMeter(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
203 va.UpdateMeterProf(tt.args.cntx, tt.args.cfg)
204 case write_to_db_error:
205 va.MeterMgr.Meters.Store("test_name", vm)
206 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
207 db = dbintf
208 dbintf.EXPECT().PutMeter(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("error")).Times(1)
209 va.UpdateMeterProf(tt.args.cntx, tt.args.cfg)
210 }
211 })
212 }
213}
214
215func TestVoltApplication_AddMeterProf(t *testing.T) {
216 type args struct {
217 cntx context.Context
218 cfg VoltMeter
219 }
220 tests := []struct {
221 name string
222 args args
223 }{
224 {
225 name: "VoltApplication_AddMeterProf",
226 args: args{
227 cntx: context.Background(),
228 cfg: VoltMeter{Name: "test_name"},
229 },
230 },
231 {
232 name: "GetMeterByName_ok",
233 args: args{
234 cntx: context.Background(),
235 cfg: VoltMeter{Name: "test_name"},
236 },
237 },
238 {
239 name: write_to_db_error,
240 args: args{
241 cntx: context.Background(),
242 cfg: VoltMeter{Name: "test_name"},
243 },
244 },
245 }
246 for _, tt := range tests {
247 t.Run(tt.name, func(t *testing.T) {
248 va := &VoltApplication{}
249 switch tt.name {
250 case "VoltApplication_AddMeterProf":
251 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
252 db = dbintf
253 dbintf.EXPECT().PutMeter(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
254 va.AddMeterProf(tt.args.cntx, tt.args.cfg)
255 case "GetMeterByName_ok":
256 mm := &va.MeterMgr
257 mm.Meters.Store(tt.args.cfg.Name, vm)
258 va.AddMeterProf(tt.args.cntx, tt.args.cfg)
259 case write_to_db_error:
260 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
261 db = dbintf
262 dbintf.EXPECT().PutMeter(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("error")).Times(1)
263 va.AddMeterProf(tt.args.cntx, tt.args.cfg)
264 }
265 })
266 }
267}
268
269func TestMeterMgr_RestoreMetersFromDb(t *testing.T) {
270 type args struct {
271 cntx context.Context
272 }
273 tests := []struct {
274 name string
275 args args
276 }{
277 {
278 name: "MeterMgr_RestoreMetersFromDb",
279 args: args{
280 cntx: context.Background(),
281 },
282 },
283 {
284 name: invalid_value,
285 args: args{
286 cntx: context.Background(),
287 },
288 },
289 {
290 name: "unmarshal_error",
291 args: args{
292 cntx: context.Background(),
293 },
294 },
295 }
296 for _, tt := range tests {
297 t.Run(tt.name, func(t *testing.T) {
298 m := &MeterMgr{}
299 switch tt.name {
300 case "MeterMgr_RestoreMetersFromDb":
301 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
302 db = dbintf
303 vm.ID = uint32(3)
304 m.LastMeterID = uint32(2)
305 b, err := json.Marshal(vm)
306 if err != nil {
307 panic(err)
308 }
309 test := map[string]*kvstore.KVPair{}
310 test["test_device_id"] = &kvstore.KVPair{
311 Key: "test_device_id",
312 Value: b,
313 }
314 dbintf.EXPECT().GetMeters(gomock.Any()).Return(test, nil).Times(1)
315 m.RestoreMetersFromDb(tt.args.cntx)
316 case invalid_value:
317 test := map[string]*kvstore.KVPair{}
318 test["test_device_id"] = &kvstore.KVPair{
319 Key: "test_device_id",
320 Value: "test",
321 }
322 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
323 db = dbintf
324 dbintf.EXPECT().GetMeters(gomock.Any()).Return(test, nil).Times(1)
325 m.RestoreMetersFromDb(tt.args.cntx)
326 case "unmarshal_error":
327 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
328 db = dbintf
329 vm.ID = uint32(3)
330 m.LastMeterID = uint32(2)
331 b, err := json.Marshal("test")
332 if err != nil {
333 panic(err)
334 }
335 test := map[string]*kvstore.KVPair{}
336 test["test_device_id"] = &kvstore.KVPair{
337 Key: "test_device_id",
338 Value: b,
339 }
340 dbintf.EXPECT().GetMeters(gomock.Any()).Return(test, nil).Times(1)
341 m.RestoreMetersFromDb(tt.args.cntx)
342 }
343 })
344 }
345}
346
347func TestMeterMgr_AddMeterToDevice(t *testing.T) {
348 type args struct {
349 port string
350 device string
351 meterID uint32
352 aggMeterID uint32
353 }
354 tests := []struct {
355 name string
356 args args
357 }{
358 {
359 name: "MeterMgr_AddMeterToDevice",
360 args: args{
361 port: "test_port",
362 device: test_device,
363 meterID: uint32(3),
364 aggMeterID: uint32(2),
365 },
366 },
367 }
368 for _, tt := range tests {
369 t.Run(tt.name, func(t *testing.T) {
370 m := &MeterMgr{}
371 m.MetersByID.Store(tt.args.meterID, vm)
372 m.AddMeterToDevice(tt.args.port, tt.args.device, tt.args.meterID, tt.args.aggMeterID)
373 })
374 }
375}
376
377func TestVoltMeter_AddToDevice(t *testing.T) {
378 type args struct {
379 port string
380 device string
381 aggVM *VoltMeter
382 }
383 tests := []struct {
384 name string
385 args args
386 }{
387 {
388 name: "VoltMeter_AddToDevice",
389 args: args{
390 port: "test_port",
391 device: test_device,
392 },
393 },
394 {
395 name: "Gir == 0",
396 args: args{
397 port: "test_port",
398 device: test_device,
399 },
400 },
401 }
402 for _, tt := range tests {
403 t.Run(tt.name, func(t *testing.T) {
404 vm1 := &VoltMeter{}
405 switch tt.name {
406 case "VoltMeter_AddToDevice":
407 vm1.Cir = uint32(1)
408 vm1.Air = uint32(1)
409 vm1.Pir = uint32(1)
410 vm1.Gir = uint32(1)
411 vm1.Pbs = uint32(1)
412 vm1.AddToDevice(tt.args.port, tt.args.device, tt.args.aggVM)
413 case "Gir == 0":
414 vm1.Cir = uint32(1)
415 vm1.Air = uint32(1)
416 vm1.Pir = uint32(1)
417 vm1.Pbs = uint32(1)
418 vm1.AddToDevice(tt.args.port, tt.args.device, tt.args.aggVM)
419 }
420 })
421 }
422}