blob: c375f7dd33aa334770e6823745a64f260dd8e150 [file] [log] [blame]
Hardik Windlass84861682019-11-10 05:44:33 +00001/*
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 */
16package core
17
18import (
19 "context"
20 "github.com/opencord/voltha-go/db/model"
Thomas Lee Se5a44012019-11-07 20:32:24 +053021 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Hardik Windlass84861682019-11-10 05:44:33 +000022 "github.com/opencord/voltha-protos/v2/go/voltha"
23 "github.com/stretchr/testify/assert"
24 "reflect"
25 "testing"
26)
27
28func makeModelProxyManagerObj() *ModelProxyManager {
29 cdRoot := model.NewRoot(&voltha.Voltha{}, nil)
Thomas Lee Se5a44012019-11-07 20:32:24 +053030 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 Windlass84861682019-11-10 05:44:33 +000034 mpMgr := newModelProxyManager(cdProxy)
35 return mpMgr
36}
37
38func 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
58func 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
69func 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
80func 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
90func 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 Se5a44012019-11-07 20:32:24 +0530110 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 Windlass84861682019-11-10 05:44:33 +0000116 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
127func 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 Se5a44012019-11-07 20:32:24 +0530147 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 Windlass84861682019-11-10 05:44:33 +0000153 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
164func 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 Se5a44012019-11-07 20:32:24 +0530177 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 Windlass84861682019-11-10 05:44:33 +0000182 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
193func 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 Se5a44012019-11-07 20:32:24 +0530213 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 Windlass84861682019-11-10 05:44:33 +0000219 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
230func 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 Se5a44012019-11-07 20:32:24 +0530243 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 Windlass84861682019-11-10 05:44:33 +0000249 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 Paulc594bb32019-11-06 07:34:27 +0000260func TestListEventFilters(t *testing.T) {
261 wantResult := &voltha.EventFilters{
262 Filters: []*voltha.EventFilter{
Hardik Windlass84861682019-11-10 05:44:33 +0000263 {
264 Id: "id",
265 },
266 },
267 }
268
269 mpMgr := makeModelProxyManagerObj()
270
271 // Case 1: Not Found
Devmalya Paulc594bb32019-11-06 07:34:27 +0000272 result0, err0 := mpMgr.ListEventFilters(context.Background())
Hardik Windlass84861682019-11-10 05:44:33 +0000273 if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) {
Devmalya Paulc594bb32019-11-06 07:34:27 +0000274 t.Errorf("ListEventFilters() = %v, want %v", result0, wantResult)
Hardik Windlass84861682019-11-10 05:44:33 +0000275 }
276 assert.Nil(t, result0.Filters)
277 assert.Nil(t, err0)
278
279 // Case 2: Found
Thomas Lee Se5a44012019-11-07 20:32:24 +0530280 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 Paulc594bb32019-11-06 07:34:27 +0000286 t.Error("Failed to add event filter")
Hardik Windlass84861682019-11-10 05:44:33 +0000287 }
Devmalya Paulc594bb32019-11-06 07:34:27 +0000288 result1, err1 := mpMgr.ListEventFilters(context.Background())
Hardik Windlass84861682019-11-10 05:44:33 +0000289 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
Devmalya Paulc594bb32019-11-06 07:34:27 +0000290 t.Errorf("ListEventFilters() = %v, want %v", result1, wantResult)
Hardik Windlass84861682019-11-10 05:44:33 +0000291 }
292 assert.NotNil(t, result1.Filters)
293 assert.Nil(t, err1)
294 assert.Equal(t, wantResult, result1)
295}
296
Devmalya Paulc594bb32019-11-06 07:34:27 +0000297func TestGetEventFilter(t *testing.T) {
298 wantResult := &voltha.EventFilters{
299 Filters: []*voltha.EventFilter{
300 {
301 Id: "id",
302 },
303 },
304 }
305
Hardik Windlass84861682019-11-10 05:44:33 +0000306 mpMgr := makeModelProxyManagerObj()
307
308 // Case 1: Not Found
Devmalya Paulc594bb32019-11-06 07:34:27 +0000309 result0, _ := mpMgr.GetEventFilter(context.Background(), "id")
Hardik Windlass84861682019-11-10 05:44:33 +0000310 if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) {
Devmalya Paulc594bb32019-11-06 07:34:27 +0000311 t.Errorf("GetEventFilters() = %v, want %v", result0, wantResult)
Hardik Windlass84861682019-11-10 05:44:33 +0000312 }
313 assert.Nil(t, result0)
Hardik Windlass84861682019-11-10 05:44:33 +0000314 // Case 2: Found
Thomas Lee Se5a44012019-11-07 20:32:24 +0530315 //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 Paulc594bb32019-11-06 07:34:27 +0000322 t.Error("Failed to add event filter")
Hardik Windlass84861682019-11-10 05:44:33 +0000323 }
Devmalya Paulc594bb32019-11-06 07:34:27 +0000324 result1, err1 := mpMgr.ListEventFilters(context.Background())
Hardik Windlass84861682019-11-10 05:44:33 +0000325 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
Devmalya Paulc594bb32019-11-06 07:34:27 +0000326 t.Errorf("GetEventFilters() = %v, want %v", result1, wantResult)
Hardik Windlass84861682019-11-10 05:44:33 +0000327 }
328 assert.NotNil(t, result1)
329 assert.Nil(t, err1)
Devmalya Paulc594bb32019-11-06 07:34:27 +0000330 assert.Equal(t, wantResult, result1)
331
Hardik Windlass84861682019-11-10 05:44:33 +0000332}