Akash Soni | d36d23b | 2023-08-18 12:51:40 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | package controller |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "sync" |
| 21 | "testing" |
| 22 | "voltha-go-controller/internal/pkg/holder" |
| 23 | "voltha-go-controller/internal/pkg/of" |
| 24 | "voltha-go-controller/internal/test/mocks" |
| 25 | |
| 26 | "github.com/golang/mock/gomock" |
| 27 | "github.com/stretchr/testify/assert" |
| 28 | ) |
| 29 | |
| 30 | func TestModMeterTask_Start(t *testing.T) { |
| 31 | type args struct { |
| 32 | ctx context.Context |
| 33 | taskID uint8 |
| 34 | } |
| 35 | tests := []struct { |
| 36 | name string |
| 37 | args args |
| 38 | wantErr bool |
| 39 | }{ |
| 40 | { |
| 41 | name: "mmt.command == of.MeterCommandAdd", |
| 42 | args: args{ |
| 43 | ctx: context.Background(), |
| 44 | taskID: uint8(1), |
| 45 | }, |
| 46 | }, |
| 47 | } |
| 48 | for _, tt := range tests { |
| 49 | t.Run(tt.name, func(t *testing.T) { |
| 50 | meters := make(map[uint32]*of.Meter) |
| 51 | meters[uint32(1)] = &of.Meter{ |
| 52 | ID: uint32(1), |
| 53 | } |
| 54 | volthaClientMock := mocks.NewMockVolthaServiceClient(gomock.NewController(t)) |
| 55 | mmt := &ModMeterTask{ |
| 56 | meter: &of.Meter{ |
| 57 | ID: uint32(1), |
| 58 | }, |
| 59 | device: &Device{ |
| 60 | meterLock: sync.RWMutex{}, |
| 61 | meters: meters, |
| 62 | State: DeviceStateUP, |
| 63 | vclientHolder: &holder.VolthaServiceClientHolder{ |
| 64 | VolthaSvcClient: volthaClientMock, |
| 65 | }, |
| 66 | }, |
| 67 | } |
| 68 | mmt.meter.ID = uint32(2) |
| 69 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 70 | db = dbintf |
| 71 | dbintf.EXPECT().DelDeviceMeter(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() |
| 72 | volthaClientMock.EXPECT().UpdateLogicalDeviceMeterTable(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() |
| 73 | err := mmt.Start(tt.args.ctx, tt.args.taskID) |
| 74 | assert.Nil(t, err) |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestNewModMeterTask(t *testing.T) { |
| 80 | type args struct { |
| 81 | ctx context.Context |
| 82 | command of.MeterCommand |
| 83 | meter *of.Meter |
| 84 | device *Device |
| 85 | } |
| 86 | tests := []struct { |
| 87 | name string |
| 88 | args args |
| 89 | want *ModMeterTask |
| 90 | }{ |
| 91 | { |
| 92 | name: "NewModMeterTask", |
| 93 | args: args{ |
| 94 | ctx: context.Background(), |
| 95 | command: of.MeterCommandAdd, |
| 96 | meter: &of.Meter{ |
| 97 | ID: uint32(1), |
| 98 | }, |
| 99 | }, |
| 100 | }, |
| 101 | } |
| 102 | for _, tt := range tests { |
| 103 | t.Run(tt.name, func(t *testing.T) { |
| 104 | got := NewModMeterTask(tt.args.ctx, tt.args.command, tt.args.meter, tt.args.device) |
| 105 | assert.NotNil(t, got) |
| 106 | }) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestModMeterTask_Name(t *testing.T) { |
| 111 | tests := []struct { |
| 112 | name string |
| 113 | want string |
| 114 | }{ |
| 115 | { |
| 116 | name: "ModMeterTask_Name", |
| 117 | want: "Add Flows Task", |
| 118 | }, |
| 119 | } |
| 120 | for _, tt := range tests { |
| 121 | t.Run(tt.name, func(t *testing.T) { |
| 122 | mmt := &ModMeterTask{} |
| 123 | if got := mmt.Name(); got != tt.want { |
| 124 | t.Errorf("ModMeterTask.Name() = %v, want %v", got, tt.want) |
| 125 | } |
| 126 | }) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestModMeterTask_TaskID(t *testing.T) { |
| 131 | tests := []struct { |
| 132 | name string |
| 133 | want uint8 |
| 134 | }{ |
| 135 | { |
| 136 | name: "ModMeterTask_TaskID", |
| 137 | }, |
| 138 | } |
| 139 | for _, tt := range tests { |
| 140 | t.Run(tt.name, func(t *testing.T) { |
| 141 | mmt := &ModMeterTask{} |
| 142 | if got := mmt.TaskID(); got != tt.want { |
| 143 | t.Errorf("ModMeterTask.TaskID() = %v, want %v", got, tt.want) |
| 144 | } |
| 145 | }) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestModMeterTask_Timestamp(t *testing.T) { |
| 150 | tests := []struct { |
| 151 | name string |
| 152 | want string |
| 153 | }{ |
| 154 | { |
| 155 | name: "ModMeterTask_Timestamp", |
| 156 | }, |
| 157 | } |
| 158 | for _, tt := range tests { |
| 159 | t.Run(tt.name, func(t *testing.T) { |
| 160 | mmt := &ModMeterTask{} |
| 161 | if got := mmt.Timestamp(); got != tt.want { |
| 162 | t.Errorf("ModMeterTask.Timestamp() = %v, want %v", got, tt.want) |
| 163 | } |
| 164 | }) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestModMeterTask_Stop(t *testing.T) { |
| 169 | tests := []struct { |
| 170 | name string |
| 171 | }{ |
| 172 | { |
| 173 | name: "ModMeterTask_Stop", |
| 174 | }, |
| 175 | } |
| 176 | for _, tt := range tests { |
| 177 | t.Run(tt.name, func(t *testing.T) { |
| 178 | mmt := &ModMeterTask{} |
| 179 | mmt.Stop() |
| 180 | }) |
| 181 | } |
| 182 | } |