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" |
| 21 | "github.com/opencord/voltha-protos/v2/go/voltha" |
| 22 | "github.com/stretchr/testify/assert" |
| 23 | "reflect" |
| 24 | "testing" |
| 25 | ) |
| 26 | |
| 27 | func makeModelProxyManagerObj() *ModelProxyManager { |
| 28 | cdRoot := model.NewRoot(&voltha.Voltha{}, nil) |
| 29 | cdProxy := cdRoot.CreateProxy(context.Background(), "/", false) |
| 30 | mpMgr := newModelProxyManager(cdProxy) |
| 31 | return mpMgr |
| 32 | } |
| 33 | |
| 34 | func TestNewModelProxyManager(t *testing.T) { |
| 35 | type args struct { |
| 36 | clusterDataProxy *model.Proxy |
| 37 | } |
| 38 | tests := []struct { |
| 39 | name string |
| 40 | args args |
| 41 | want *ModelProxyManager |
| 42 | }{ |
| 43 | {"NewModelProxyManager", args{&model.Proxy{}}, &ModelProxyManager{}}, |
| 44 | } |
| 45 | for _, tt := range tests { |
| 46 | t.Run(tt.name, func(t *testing.T) { |
| 47 | if got := newModelProxyManager(tt.args.clusterDataProxy); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 48 | t.Errorf("newModelProxy() = %v, want %v", got, tt.want) |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestGetVoltha(t *testing.T) { |
| 55 | wantResult := &voltha.Voltha{} |
| 56 | mpMgr := makeModelProxyManagerObj() |
| 57 | result, err := mpMgr.GetVoltha(context.Background()) |
| 58 | if reflect.TypeOf(result) != reflect.TypeOf(wantResult) { |
| 59 | t.Errorf("GetVoltha() = %v, want %v", result, wantResult) |
| 60 | } |
| 61 | assert.NotNil(t, result) |
| 62 | assert.Nil(t, err) |
| 63 | } |
| 64 | |
| 65 | func TestListCoreInstances(t *testing.T) { |
| 66 | wantResult := &voltha.CoreInstances{} |
| 67 | mpMgr := makeModelProxyManagerObj() |
| 68 | result, err := mpMgr.ListCoreInstances(context.Background()) |
| 69 | if reflect.TypeOf(result) != reflect.TypeOf(wantResult) { |
| 70 | t.Errorf("ListCoreInstances() = %v, want %v", result, wantResult) |
| 71 | } |
| 72 | assert.Nil(t, result.Items) |
| 73 | assert.NotNil(t, err) |
| 74 | } |
| 75 | |
| 76 | func TestGetCoreInstance(t *testing.T) { |
| 77 | wantResult := &voltha.CoreInstance{} |
| 78 | mpMgr := makeModelProxyManagerObj() |
| 79 | result, err := mpMgr.GetCoreInstance(context.Background(), "id") |
| 80 | if reflect.TypeOf(result) != reflect.TypeOf(wantResult) { |
| 81 | t.Errorf("GetCoreInstance() = %v, want %v", result, wantResult) |
| 82 | } |
| 83 | assert.NotNil(t, err) |
| 84 | } |
| 85 | |
| 86 | func TestListAdapters(t *testing.T) { |
| 87 | wantResult := &voltha.Adapters{ |
| 88 | Items: []*voltha.Adapter{ |
| 89 | { |
| 90 | Id: "id", |
| 91 | }, |
| 92 | }, |
| 93 | } |
| 94 | |
| 95 | mpMgr := makeModelProxyManagerObj() |
| 96 | |
| 97 | // Case 1: Not Found |
| 98 | result0, err0 := mpMgr.ListAdapters(context.Background()) |
| 99 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 100 | t.Errorf("ListAdapters() = %v, want %v", result0, wantResult) |
| 101 | } |
| 102 | assert.Nil(t, result0.Items) |
| 103 | assert.Nil(t, err0) |
| 104 | |
| 105 | // Case 2: Found |
| 106 | if added := mpMgr.clusterDataProxy.Add(context.Background(), "/adapters", &voltha.Adapter{Id: "id"}, ""); added == nil { |
| 107 | t.Error("Failed to add adapter") |
| 108 | } |
| 109 | result1, err1 := mpMgr.ListAdapters(context.Background()) |
| 110 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 111 | t.Errorf("ListAdapters() = %v, want %v", result1, wantResult) |
| 112 | } |
| 113 | assert.NotNil(t, result1.Items) |
| 114 | assert.Nil(t, err1) |
| 115 | assert.Equal(t, wantResult, result1) |
| 116 | } |
| 117 | |
| 118 | func TestListDeviceTypes(t *testing.T) { |
| 119 | wantResult := &voltha.DeviceTypes{ |
| 120 | Items: []*voltha.DeviceType{ |
| 121 | { |
| 122 | Id: "id", |
| 123 | }, |
| 124 | }, |
| 125 | } |
| 126 | |
| 127 | mpMgr := makeModelProxyManagerObj() |
| 128 | |
| 129 | // Case 1: Not Found |
| 130 | result0, err0 := mpMgr.ListDeviceTypes(context.Background()) |
| 131 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 132 | t.Errorf("ListDeviceTypes() = %v, want %v", result0, wantResult) |
| 133 | } |
| 134 | assert.Nil(t, result0.Items) |
| 135 | assert.Nil(t, err0) |
| 136 | |
| 137 | // Case 2: Found |
| 138 | if added := mpMgr.clusterDataProxy.Add(context.Background(), "/device_types", &voltha.DeviceType{Id: "id"}, ""); added == nil { |
| 139 | t.Error("Failed to add device type") |
| 140 | } |
| 141 | result1, err1 := mpMgr.ListDeviceTypes(context.Background()) |
| 142 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 143 | t.Errorf("ListDeviceTypes() = %v, want %v", result1, wantResult) |
| 144 | } |
| 145 | assert.NotNil(t, result1.Items) |
| 146 | assert.Nil(t, err1) |
| 147 | assert.Equal(t, wantResult, result1) |
| 148 | } |
| 149 | |
| 150 | func TestGetDeviceType(t *testing.T) { |
| 151 | wantResult := &voltha.DeviceType{} |
| 152 | mpMgr := makeModelProxyManagerObj() |
| 153 | |
| 154 | // Case 1: Not Found |
| 155 | result0, err0 := mpMgr.GetDeviceType(context.Background(), "id") |
| 156 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 157 | t.Errorf("GetDeviceType() = %v, want %v", result0, wantResult) |
| 158 | } |
| 159 | assert.Nil(t, result0) |
| 160 | assert.NotNil(t, err0) |
| 161 | |
| 162 | // Case 2: Found |
| 163 | if added := mpMgr.clusterDataProxy.Add(context.Background(), "/device_types", &voltha.DeviceType{Id: "id"}, ""); added == nil { |
| 164 | t.Error("Failed to add device type") |
| 165 | } |
| 166 | result1, err1 := mpMgr.GetDeviceType(context.Background(), "id") |
| 167 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 168 | t.Errorf("GetDeviceType() = %v, want %v", result1, wantResult) |
| 169 | } |
| 170 | assert.NotNil(t, result1) |
| 171 | assert.Nil(t, err1) |
| 172 | assert.Equal(t, "id", result1.Id) |
| 173 | } |
| 174 | |
| 175 | func TestListDeviceGroups(t *testing.T) { |
| 176 | wantResult := &voltha.DeviceGroups{ |
| 177 | Items: []*voltha.DeviceGroup{ |
| 178 | { |
| 179 | Id: "id", |
| 180 | }, |
| 181 | }, |
| 182 | } |
| 183 | |
| 184 | mpMgr := makeModelProxyManagerObj() |
| 185 | |
| 186 | // Case 1: Not Found |
| 187 | result0, err0 := mpMgr.ListDeviceGroups(context.Background()) |
| 188 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 189 | t.Errorf("ListDeviceGroups() = %v, want %v", result0, wantResult) |
| 190 | } |
| 191 | assert.Nil(t, result0.Items) |
| 192 | assert.Nil(t, err0) |
| 193 | |
| 194 | // Case 2: Found |
| 195 | if added := mpMgr.clusterDataProxy.Add(context.Background(), "/device_groups", &voltha.DeviceGroup{Id: "id"}, ""); added == nil { |
| 196 | t.Error("Failed to add device group") |
| 197 | } |
| 198 | result1, err1 := mpMgr.ListDeviceGroups(context.Background()) |
| 199 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 200 | t.Errorf("ListDeviceGroups() = %v, want %v", result1, wantResult) |
| 201 | } |
| 202 | assert.NotNil(t, result1.Items) |
| 203 | assert.Nil(t, err1) |
| 204 | assert.Equal(t, wantResult, result1) |
| 205 | } |
| 206 | |
| 207 | func TestGetDeviceGroup(t *testing.T) { |
| 208 | wantResult := &voltha.DeviceGroup{} |
| 209 | mpMgr := makeModelProxyManagerObj() |
| 210 | |
| 211 | // Case 1: Not Found |
| 212 | result0, err0 := mpMgr.GetDeviceGroup(context.Background(), "id") |
| 213 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 214 | t.Errorf("GetDeviceGroup() = %v, want %v", result0, wantResult) |
| 215 | } |
| 216 | assert.Nil(t, result0) |
| 217 | assert.NotNil(t, err0) |
| 218 | |
| 219 | // Case 2: Found |
| 220 | if added := mpMgr.clusterDataProxy.Add(context.Background(), "/device_groups", &voltha.DeviceGroup{Id: "id"}, ""); added == nil { |
| 221 | t.Error("Failed to add device group") |
| 222 | } |
| 223 | result1, err1 := mpMgr.GetDeviceGroup(context.Background(), "id") |
| 224 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 225 | t.Errorf("GetDeviceGroup() = %v, want %v", result1, wantResult) |
| 226 | } |
| 227 | assert.NotNil(t, result1) |
| 228 | assert.Nil(t, err1) |
| 229 | assert.Equal(t, "id", result1.Id) |
| 230 | } |
| 231 | |
| 232 | func TestListAlarmFilters(t *testing.T) { |
| 233 | wantResult := &voltha.AlarmFilters{ |
| 234 | Filters: []*voltha.AlarmFilter{ |
| 235 | { |
| 236 | Id: "id", |
| 237 | }, |
| 238 | }, |
| 239 | } |
| 240 | |
| 241 | mpMgr := makeModelProxyManagerObj() |
| 242 | |
| 243 | // Case 1: Not Found |
| 244 | result0, err0 := mpMgr.ListAlarmFilters(context.Background()) |
| 245 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 246 | t.Errorf("ListAlarmFilters() = %v, want %v", result0, wantResult) |
| 247 | } |
| 248 | assert.Nil(t, result0.Filters) |
| 249 | assert.Nil(t, err0) |
| 250 | |
| 251 | // Case 2: Found |
| 252 | if added := mpMgr.clusterDataProxy.Add(context.Background(), "/alarm_filters", &voltha.AlarmFilter{Id: "id"}, ""); added == nil { |
| 253 | t.Error("Failed to add alarm filter") |
| 254 | } |
| 255 | result1, err1 := mpMgr.ListAlarmFilters(context.Background()) |
| 256 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 257 | t.Errorf("ListAlarmFilters() = %v, want %v", result1, wantResult) |
| 258 | } |
| 259 | assert.NotNil(t, result1.Filters) |
| 260 | assert.Nil(t, err1) |
| 261 | assert.Equal(t, wantResult, result1) |
| 262 | } |
| 263 | |
| 264 | func TestGetAlarmFilter(t *testing.T) { |
| 265 | wantResult := &voltha.AlarmFilter{} |
| 266 | mpMgr := makeModelProxyManagerObj() |
| 267 | |
| 268 | // Case 1: Not Found |
| 269 | result0, err0 := mpMgr.GetAlarmFilter(context.Background(), "id") |
| 270 | if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) { |
| 271 | t.Errorf("GetAlarmFilter() = %v, want %v", result0, wantResult) |
| 272 | } |
| 273 | assert.Nil(t, result0) |
| 274 | assert.NotNil(t, err0) |
| 275 | |
| 276 | // Case 2: Found |
| 277 | if added := mpMgr.clusterDataProxy.Add(context.Background(), "/alarm_filters", &voltha.AlarmFilter{Id: "id"}, ""); added == nil { |
| 278 | t.Error("Failed to add alarm filter") |
| 279 | } |
| 280 | result1, err1 := mpMgr.GetAlarmFilter(context.Background(), "id") |
| 281 | if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) { |
| 282 | t.Errorf("GetAlarmFilter() = %v, want %v", result1, wantResult) |
| 283 | } |
| 284 | assert.NotNil(t, result1) |
| 285 | assert.Nil(t, err1) |
| 286 | assert.Equal(t, "id", result1.Id) |
| 287 | } |