blob: 10411bf25ce51fc1ce7dafe691614ad45238a6cb [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"
21 "github.com/opencord/voltha-protos/v2/go/voltha"
22 "github.com/stretchr/testify/assert"
23 "reflect"
24 "testing"
25)
26
27func 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
34func 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
54func 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
65func 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
76func 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
86func 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
118func 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
150func 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
175func 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
207func 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
Devmalya Paulc594bb32019-11-06 07:34:27 +0000232func TestListEventFilters(t *testing.T) {
233 wantResult := &voltha.EventFilters{
234 Filters: []*voltha.EventFilter{
Hardik Windlass84861682019-11-10 05:44:33 +0000235 {
236 Id: "id",
237 },
238 },
239 }
240
241 mpMgr := makeModelProxyManagerObj()
242
243 // Case 1: Not Found
Devmalya Paulc594bb32019-11-06 07:34:27 +0000244 result0, err0 := mpMgr.ListEventFilters(context.Background())
Hardik Windlass84861682019-11-10 05:44:33 +0000245 if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) {
Devmalya Paulc594bb32019-11-06 07:34:27 +0000246 t.Errorf("ListEventFilters() = %v, want %v", result0, wantResult)
Hardik Windlass84861682019-11-10 05:44:33 +0000247 }
248 assert.Nil(t, result0.Filters)
249 assert.Nil(t, err0)
250
251 // Case 2: Found
Devmalya Paulc594bb32019-11-06 07:34:27 +0000252 if added := mpMgr.clusterDataProxy.Add(context.Background(), "/event_filters", &voltha.EventFilter{Id: "id"}, ""); added == nil {
253 t.Error("Failed to add event filter")
Hardik Windlass84861682019-11-10 05:44:33 +0000254 }
Devmalya Paulc594bb32019-11-06 07:34:27 +0000255 result1, err1 := mpMgr.ListEventFilters(context.Background())
Hardik Windlass84861682019-11-10 05:44:33 +0000256 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
Devmalya Paulc594bb32019-11-06 07:34:27 +0000257 t.Errorf("ListEventFilters() = %v, want %v", result1, wantResult)
Hardik Windlass84861682019-11-10 05:44:33 +0000258 }
259 assert.NotNil(t, result1.Filters)
260 assert.Nil(t, err1)
261 assert.Equal(t, wantResult, result1)
262}
263
Devmalya Paulc594bb32019-11-06 07:34:27 +0000264func TestGetEventFilter(t *testing.T) {
265 wantResult := &voltha.EventFilters{
266 Filters: []*voltha.EventFilter{
267 {
268 Id: "id",
269 },
270 },
271 }
272
Hardik Windlass84861682019-11-10 05:44:33 +0000273 mpMgr := makeModelProxyManagerObj()
274
275 // Case 1: Not Found
Devmalya Paulc594bb32019-11-06 07:34:27 +0000276 result0, _ := mpMgr.GetEventFilter(context.Background(), "id")
Hardik Windlass84861682019-11-10 05:44:33 +0000277 if reflect.TypeOf(result0) != reflect.TypeOf(wantResult) {
Devmalya Paulc594bb32019-11-06 07:34:27 +0000278 t.Errorf("GetEventFilters() = %v, want %v", result0, wantResult)
Hardik Windlass84861682019-11-10 05:44:33 +0000279 }
280 assert.Nil(t, result0)
Hardik Windlass84861682019-11-10 05:44:33 +0000281 // Case 2: Found
Devmalya Paulc594bb32019-11-06 07:34:27 +0000282 if added := mpMgr.clusterDataProxy.Add(context.Background(), "/event_filters", &voltha.EventFilter{Id: "id"}, ""); added == nil {
283 t.Error("Failed to add event filter")
Hardik Windlass84861682019-11-10 05:44:33 +0000284 }
Devmalya Paulc594bb32019-11-06 07:34:27 +0000285 result1, err1 := mpMgr.ListEventFilters(context.Background())
Hardik Windlass84861682019-11-10 05:44:33 +0000286 if reflect.TypeOf(result1) != reflect.TypeOf(wantResult) {
Devmalya Paulc594bb32019-11-06 07:34:27 +0000287 t.Errorf("GetEventFilters() = %v, want %v", result1, wantResult)
Hardik Windlass84861682019-11-10 05:44:33 +0000288 }
289 assert.NotNil(t, result1)
290 assert.Nil(t, err1)
Devmalya Paulc594bb32019-11-06 07:34:27 +0000291 assert.Equal(t, wantResult, result1)
292
Hardik Windlass84861682019-11-10 05:44:33 +0000293}