Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 20 | "reflect" |
| 21 | "testing" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame^] | 22 | |
| 23 | "github.com/opencord/voltha-go/db/model" |
| 24 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 25 | "github.com/opencord/voltha-protos/v3/go/voltha" |
| 26 | "github.com/stretchr/testify/assert" |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func makeModelProxyManagerObj() *ModelProxyManager { |
| 30 | cdRoot := model.NewRoot(&voltha.Voltha{}, nil) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 31 | cdProxy, err := cdRoot.CreateProxy(context.Background(), "/", false) |
| 32 | if err != nil { |
| 33 | log.With(log.Fields{"error": err}).Fatal("Failed to create model proxy manager") |
| 34 | } |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 35 | mpMgr := newModelProxyManager(cdProxy) |
| 36 | return mpMgr |
| 37 | } |
| 38 | |
| 39 | func TestNewModelProxyManager(t *testing.T) { |
| 40 | type args struct { |
| 41 | clusterDataProxy *model.Proxy |
| 42 | } |
| 43 | tests := []struct { |
| 44 | name string |
| 45 | args args |
| 46 | want *ModelProxyManager |
| 47 | }{ |
| 48 | {"NewModelProxyManager", args{&model.Proxy{}}, &ModelProxyManager{}}, |
| 49 | } |
| 50 | for _, tt := range tests { |
| 51 | t.Run(tt.name, func(t *testing.T) { |
| 52 | if got := newModelProxyManager(tt.args.clusterDataProxy); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 53 | t.Errorf("newModelProxy() = %v, want %v", got, tt.want) |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestGetVoltha(t *testing.T) { |
| 60 | wantResult := &voltha.Voltha{} |
| 61 | mpMgr := makeModelProxyManagerObj() |
| 62 | result, err := mpMgr.GetVoltha(context.Background()) |
| 63 | if reflect.TypeOf(result) != reflect.TypeOf(wantResult) { |
| 64 | t.Errorf("GetVoltha() = %v, want %v", result, wantResult) |
| 65 | } |
| 66 | assert.NotNil(t, result) |
| 67 | assert.Nil(t, err) |
| 68 | } |
| 69 | |
| 70 | func TestListCoreInstances(t *testing.T) { |
| 71 | wantResult := &voltha.CoreInstances{} |
| 72 | mpMgr := makeModelProxyManagerObj() |
| 73 | result, err := mpMgr.ListCoreInstances(context.Background()) |
| 74 | if reflect.TypeOf(result) != reflect.TypeOf(wantResult) { |
| 75 | t.Errorf("ListCoreInstances() = %v, want %v", result, wantResult) |
| 76 | } |
| 77 | assert.Nil(t, result.Items) |
| 78 | assert.NotNil(t, err) |
| 79 | } |
| 80 | |
| 81 | func TestGetCoreInstance(t *testing.T) { |
| 82 | wantResult := &voltha.CoreInstance{} |
| 83 | mpMgr := makeModelProxyManagerObj() |
| 84 | result, err := mpMgr.GetCoreInstance(context.Background(), "id") |
| 85 | if reflect.TypeOf(result) != reflect.TypeOf(wantResult) { |
| 86 | t.Errorf("GetCoreInstance() = %v, want %v", result, wantResult) |
| 87 | } |
| 88 | assert.NotNil(t, err) |
| 89 | } |
| 90 | |
| 91 | func TestListAdapters(t *testing.T) { |
| 92 | wantResult := &voltha.Adapters{ |
| 93 | Items: []*voltha.Adapter{ |
| 94 | { |
| 95 | Id: "id", |
| 96 | }, |
| 97 | }, |
| 98 | } |
| 99 | |
| 100 | mpMgr := makeModelProxyManagerObj() |
| 101 | |
| 102 | // Case 1: Not Found |
| 103 | result0, err0 := mpMgr.ListAdapters(context.Background()) |
| 104 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 105 | t.Errorf("ListAdapters() = %v, want %v", result0, wantResult) |
| 106 | } |
| 107 | assert.Nil(t, result0.Items) |
| 108 | assert.Nil(t, err0) |
| 109 | |
| 110 | // Case 2: Found |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 111 | added, err := mpMgr.clusterDataProxy.Add(context.Background(), "/adapters", &voltha.Adapter{Id: "id"}, "") |
| 112 | if err != nil { |
| 113 | log.Errorw("failed-to-add-adapter-to-cluster-proxy", log.Fields{"error": err}) |
| 114 | assert.NotNil(t, err) |
| 115 | } |
| 116 | if added == nil { |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 117 | t.Error("Failed to add adapter") |
| 118 | } |
| 119 | result1, err1 := mpMgr.ListAdapters(context.Background()) |
| 120 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 121 | t.Errorf("ListAdapters() = %v, want %v", result1, wantResult) |
| 122 | } |
| 123 | assert.NotNil(t, result1.Items) |
| 124 | assert.Nil(t, err1) |
| 125 | assert.Equal(t, wantResult, result1) |
| 126 | } |
| 127 | |
| 128 | func TestListDeviceTypes(t *testing.T) { |
| 129 | wantResult := &voltha.DeviceTypes{ |
| 130 | Items: []*voltha.DeviceType{ |
| 131 | { |
| 132 | Id: "id", |
| 133 | }, |
| 134 | }, |
| 135 | } |
| 136 | |
| 137 | mpMgr := makeModelProxyManagerObj() |
| 138 | |
| 139 | // Case 1: Not Found |
| 140 | result0, err0 := mpMgr.ListDeviceTypes(context.Background()) |
| 141 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 142 | t.Errorf("ListDeviceTypes() = %v, want %v", result0, wantResult) |
| 143 | } |
| 144 | assert.Nil(t, result0.Items) |
| 145 | assert.Nil(t, err0) |
| 146 | |
| 147 | // Case 2: Found |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 148 | added, err := mpMgr.clusterDataProxy.Add(context.Background(), "/device_types", &voltha.DeviceType{Id: "id"}, "") |
| 149 | if err != nil { |
| 150 | log.Errorw("failed-to-add-device-types-to-cluster-proxy", log.Fields{"error": err}) |
| 151 | assert.NotNil(t, err) |
| 152 | } |
| 153 | if added == nil { |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 154 | t.Error("Failed to add device type") |
| 155 | } |
| 156 | result1, err1 := mpMgr.ListDeviceTypes(context.Background()) |
| 157 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 158 | t.Errorf("ListDeviceTypes() = %v, want %v", result1, wantResult) |
| 159 | } |
| 160 | assert.NotNil(t, result1.Items) |
| 161 | assert.Nil(t, err1) |
| 162 | assert.Equal(t, wantResult, result1) |
| 163 | } |
| 164 | |
| 165 | func TestGetDeviceType(t *testing.T) { |
| 166 | wantResult := &voltha.DeviceType{} |
| 167 | mpMgr := makeModelProxyManagerObj() |
| 168 | |
| 169 | // Case 1: Not Found |
| 170 | result0, err0 := mpMgr.GetDeviceType(context.Background(), "id") |
| 171 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 172 | t.Errorf("GetDeviceType() = %v, want %v", result0, wantResult) |
| 173 | } |
| 174 | assert.Nil(t, result0) |
| 175 | assert.NotNil(t, err0) |
| 176 | |
| 177 | // Case 2: Found |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 178 | added, err := mpMgr.clusterDataProxy.Add(context.Background(), "/device_types", &voltha.DeviceType{Id: "id"}, "") |
| 179 | if err != nil { |
| 180 | log.Errorw("failed-to-add-adapter-to-device-types-proxy", log.Fields{"error": err}) |
| 181 | assert.NotNil(t, err) |
| 182 | } else if added == nil { |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 183 | t.Error("Failed to add device type") |
| 184 | } |
| 185 | result1, err1 := mpMgr.GetDeviceType(context.Background(), "id") |
| 186 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 187 | t.Errorf("GetDeviceType() = %v, want %v", result1, wantResult) |
| 188 | } |
| 189 | assert.NotNil(t, result1) |
| 190 | assert.Nil(t, err1) |
| 191 | assert.Equal(t, "id", result1.Id) |
| 192 | } |
| 193 | |
| 194 | func TestListDeviceGroups(t *testing.T) { |
| 195 | wantResult := &voltha.DeviceGroups{ |
| 196 | Items: []*voltha.DeviceGroup{ |
| 197 | { |
| 198 | Id: "id", |
| 199 | }, |
| 200 | }, |
| 201 | } |
| 202 | |
| 203 | mpMgr := makeModelProxyManagerObj() |
| 204 | |
| 205 | // Case 1: Not Found |
| 206 | result0, err0 := mpMgr.ListDeviceGroups(context.Background()) |
| 207 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 208 | t.Errorf("ListDeviceGroups() = %v, want %v", result0, wantResult) |
| 209 | } |
| 210 | assert.Nil(t, result0.Items) |
| 211 | assert.Nil(t, err0) |
| 212 | |
| 213 | // Case 2: Found |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 214 | added, err := mpMgr.clusterDataProxy.Add(context.Background(), "/device_groups", &voltha.DeviceGroup{Id: "id"}, "") |
| 215 | if err != nil { |
| 216 | log.Errorw("failed-to-add-device-groups-to-cluster-proxy", log.Fields{"error": err}) |
| 217 | assert.NotNil(t, err) |
| 218 | } |
| 219 | if added == nil { |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 220 | t.Error("Failed to add device group") |
| 221 | } |
| 222 | result1, err1 := mpMgr.ListDeviceGroups(context.Background()) |
| 223 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 224 | t.Errorf("ListDeviceGroups() = %v, want %v", result1, wantResult) |
| 225 | } |
| 226 | assert.NotNil(t, result1.Items) |
| 227 | assert.Nil(t, err1) |
| 228 | assert.Equal(t, wantResult, result1) |
| 229 | } |
| 230 | |
| 231 | func TestGetDeviceGroup(t *testing.T) { |
| 232 | wantResult := &voltha.DeviceGroup{} |
| 233 | mpMgr := makeModelProxyManagerObj() |
| 234 | |
| 235 | // Case 1: Not Found |
| 236 | result0, err0 := mpMgr.GetDeviceGroup(context.Background(), "id") |
| 237 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 238 | t.Errorf("GetDeviceGroup() = %v, want %v", result0, wantResult) |
| 239 | } |
| 240 | assert.Nil(t, result0) |
| 241 | assert.NotNil(t, err0) |
| 242 | |
| 243 | // Case 2: Found |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 244 | added, err := mpMgr.clusterDataProxy.Add(context.Background(), "/device_groups", &voltha.DeviceGroup{Id: "id"}, "") |
| 245 | if err != nil { |
| 246 | log.Errorw("failed-to-add-device-groups-to-cluster-proxy", log.Fields{"error": err}) |
| 247 | assert.NotNil(t, err) |
| 248 | } |
| 249 | if added == nil { |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 250 | t.Error("Failed to add device group") |
| 251 | } |
| 252 | result1, err1 := mpMgr.GetDeviceGroup(context.Background(), "id") |
| 253 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 254 | t.Errorf("GetDeviceGroup() = %v, want %v", result1, wantResult) |
| 255 | } |
| 256 | assert.NotNil(t, result1) |
| 257 | assert.Nil(t, err1) |
| 258 | assert.Equal(t, "id", result1.Id) |
| 259 | } |
| 260 | |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 261 | func TestListEventFilters(t *testing.T) { |
| 262 | wantResult := &voltha.EventFilters{ |
| 263 | Filters: []*voltha.EventFilter{ |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 264 | { |
| 265 | Id: "id", |
| 266 | }, |
| 267 | }, |
| 268 | } |
| 269 | |
| 270 | mpMgr := makeModelProxyManagerObj() |
| 271 | |
| 272 | // Case 1: Not Found |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 273 | result0, err0 := mpMgr.ListEventFilters(context.Background()) |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 274 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 275 | t.Errorf("ListEventFilters() = %v, want %v", result0, wantResult) |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 276 | } |
| 277 | assert.Nil(t, result0.Filters) |
| 278 | assert.Nil(t, err0) |
| 279 | |
| 280 | // Case 2: Found |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 281 | added, err := mpMgr.clusterDataProxy.Add(context.Background(), "/event_filters", &voltha.EventFilter{Id: "id"}, "") |
| 282 | if err != nil { |
| 283 | log.Errorw("failed-to-add-alarm-filters-to-cluster-proxy", log.Fields{"error": err}) |
| 284 | assert.NotNil(t, err) |
| 285 | } |
| 286 | if added == nil { |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 287 | t.Error("Failed to add event filter") |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 288 | } |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 289 | result1, err1 := mpMgr.ListEventFilters(context.Background()) |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 290 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 291 | t.Errorf("ListEventFilters() = %v, want %v", result1, wantResult) |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 292 | } |
| 293 | assert.NotNil(t, result1.Filters) |
| 294 | assert.Nil(t, err1) |
| 295 | assert.Equal(t, wantResult, result1) |
| 296 | } |
| 297 | |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 298 | func TestGetEventFilter(t *testing.T) { |
| 299 | wantResult := &voltha.EventFilters{ |
| 300 | Filters: []*voltha.EventFilter{ |
| 301 | { |
| 302 | Id: "id", |
| 303 | }, |
| 304 | }, |
| 305 | } |
| 306 | |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 307 | mpMgr := makeModelProxyManagerObj() |
| 308 | |
| 309 | // Case 1: Not Found |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 310 | result0, _ := mpMgr.GetEventFilter(context.Background(), "id") |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 311 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 312 | t.Errorf("GetEventFilters() = %v, want %v", result0, wantResult) |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 313 | } |
| 314 | assert.Nil(t, result0) |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 315 | // Case 2: Found |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 316 | //added, err := mpMgr.clusterDataProxy.Add(context.Background(), "/alarm_filters", &voltha.EventFilter{Id: "id"}, "") |
| 317 | added, err := mpMgr.clusterDataProxy.Add(context.Background(), "/event_filters", &voltha.EventFilter{Id: "id"}, "") |
| 318 | if err != nil { |
| 319 | log.Errorw("failed-to-add-adapter-to-alarm-filter-to-proxy", log.Fields{"error": err}) |
| 320 | assert.NotNil(t, err) |
| 321 | } |
| 322 | if added == nil { |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 323 | t.Error("Failed to add event filter") |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 324 | } |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 325 | result1, err1 := mpMgr.ListEventFilters(context.Background()) |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 326 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 327 | t.Errorf("GetEventFilters() = %v, want %v", result1, wantResult) |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 328 | } |
| 329 | assert.NotNil(t, result1) |
| 330 | assert.Nil(t, err1) |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 331 | assert.Equal(t, wantResult, result1) |
| 332 | |
Hardik Windlass | 8486168 | 2019-11-10 05:44:33 +0000 | [diff] [blame] | 333 | } |