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