blob: 149ab2ec725c541e43ef76ff359b3a0215f6d8db [file] [log] [blame]
David Bainbridge36d0b202019-10-23 18:33:34 +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 common
17
18import (
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080019 "context"
serkant.uluderyab38671c2019-11-01 09:35:38 -070020 adapterIf "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080021 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
22 "github.com/opencord/voltha-lib-go/v3/pkg/mocks"
23 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
24 "github.com/opencord/voltha-protos/v3/go/voltha"
25 "github.com/stretchr/testify/assert"
26 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/status"
28 "testing"
David Bainbridge36d0b202019-10-23 18:33:34 +000029)
30
31func TestCoreProxyImplementsAdapterIfCoreProxy(t *testing.T) {
32 proxy := &CoreProxy{}
33
34 if _, ok := interface{}(proxy).(adapterIf.CoreProxy); !ok {
35 t.Error("common CoreProxy does not implement adapterif.CoreProxy interface")
36 }
37
38}
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080039
40func TestCoreProxy_GetChildDevice_sn(t *testing.T) {
41
42 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
43 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -080044 Calls: make(map[int]mocks.InvokeRpcArgs),
45 Response: &voltha.Device{Id: "testDevice"},
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080046 },
47 }
48
49 proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
50
51 kwargs := make(map[string]interface{})
52 kwargs["serial_number"] = "TEST00000000001"
53
54 parentDeviceId := "aabbcc"
55 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
56
57 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
58 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
59 assert.Equal(t, call.Rpc, "GetChildDevice")
60 assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
61 assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
62 assert.Equal(t, call.WaitForResponse, true)
63 assert.Equal(t, call.Key, parentDeviceId)
64 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}})
65 assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "serial_number", Value: &ic.StrType{Val: kwargs["serial_number"].(string)}})
66
67 assert.Equal(t, "testDevice", device.Id)
68 assert.Equal(t, nil, error)
69}
70
71func TestCoreProxy_GetChildDevice_id(t *testing.T) {
72
73 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
74 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -080075 Calls: make(map[int]mocks.InvokeRpcArgs),
76 Response: &voltha.Device{Id: "testDevice"},
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080077 },
78 }
79
80 proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
81
82 kwargs := make(map[string]interface{})
83 kwargs["onu_id"] = uint32(1234)
84
85 parentDeviceId := "aabbcc"
86 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
87
88 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
89 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
90 assert.Equal(t, call.Rpc, "GetChildDevice")
91 assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
92 assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
93 assert.Equal(t, call.WaitForResponse, true)
94 assert.Equal(t, call.Key, parentDeviceId)
95 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}})
96 assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "onu_id", Value: &ic.IntType{Val: int64(kwargs["onu_id"].(uint32))}})
97
98 assert.Equal(t, "testDevice", device.Id)
99 assert.Equal(t, nil, error)
100}
101
102func TestCoreProxy_GetChildDevice_fail_timeout(t *testing.T) {
103
104 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
105 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -0800106 Calls: make(map[int]mocks.InvokeRpcArgs),
107 Timeout: true,
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800108 },
109 }
110
111 proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
112
113 kwargs := make(map[string]interface{})
114 kwargs["onu_id"] = uint32(1234)
115
116 parentDeviceId := "aabbcc"
117 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
118
119 assert.Nil(t, device)
120 parsedErr, _ := status.FromError(error)
121
Matteo Scandolob45cf592020-01-21 16:10:56 -0800122 assert.Equal(t, parsedErr.Code(), codes.DeadlineExceeded)
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800123}
124
125func TestCoreProxy_GetChildDevice_fail_unmarhsal(t *testing.T) {
126
127 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
128 InvokeRpcSpy: mocks.InvokeRpcSpy{
Matteo Scandolob45cf592020-01-21 16:10:56 -0800129 Calls: make(map[int]mocks.InvokeRpcArgs),
130 Response: &voltha.LogicalDevice{Id: "testDevice"},
Matteo Scandolo2ba00d32020-01-16 17:33:03 -0800131 },
132 }
133
134 proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
135
136 kwargs := make(map[string]interface{})
137 kwargs["onu_id"] = uint32(1234)
138
139 parentDeviceId := "aabbcc"
140 device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
141
142 assert.Nil(t, device)
143
144 parsedErr, _ := status.FromError(error)
145 assert.Equal(t, parsedErr.Code(), codes.InvalidArgument)
146}
Matteo Scandolob45cf592020-01-21 16:10:56 -0800147
148func TestCoreProxy_GetChildDevices_success(t *testing.T) {
149
150 devicesResponse := &voltha.Devices{}
151
152 devicesResponse.Items = append(devicesResponse.Items, &voltha.Device{Id: "testDevice1"})
153 devicesResponse.Items = append(devicesResponse.Items, &voltha.Device{Id: "testDevice2"})
154
155 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
156 InvokeRpcSpy: mocks.InvokeRpcSpy{
157 Calls: make(map[int]mocks.InvokeRpcArgs),
158 Response: devicesResponse,
159 },
160 }
161
162 proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
163
164 parentDeviceId := "aabbcc"
165 devices, error := proxy.GetChildDevices(context.TODO(), parentDeviceId)
166
167 assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
168 call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
169 assert.Equal(t, call.Rpc, "GetChildDevices")
170 assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
171 assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
172 assert.Equal(t, call.WaitForResponse, true)
173 assert.Equal(t, call.Key, parentDeviceId)
174 assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}})
175
176 assert.Equal(t, nil, error)
177 assert.Equal(t, 2, len(devices.Items))
178}
179
180func TestCoreProxy_GetChildDevices_fail_unmarhsal(t *testing.T) {
181
182 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
183 InvokeRpcSpy: mocks.InvokeRpcSpy{
184 Calls: make(map[int]mocks.InvokeRpcArgs),
185 Response: &voltha.LogicalDevice{Id: "testDevice"},
186 },
187 }
188
189 proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
190
191 parentDeviceId := "aabbcc"
192 devices, error := proxy.GetChildDevices(context.TODO(), parentDeviceId)
193
194 assert.Nil(t, devices)
195
196 parsedErr, _ := status.FromError(error)
197 assert.Equal(t, parsedErr.Code(), codes.InvalidArgument)
198}
199
200func TestCoreProxy_GetChildDevices_fail_timeout(t *testing.T) {
201
202 var mockKafkaIcProxy = mocks.MockKafkaICProxy{
203 InvokeRpcSpy: mocks.InvokeRpcSpy{
204 Calls: make(map[int]mocks.InvokeRpcArgs),
205 Timeout: true,
206 },
207 }
208
209 proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
210
211 parentDeviceId := "aabbcc"
212 devices, error := proxy.GetChildDevices(context.TODO(), parentDeviceId)
213
214 assert.Nil(t, devices)
215
216 parsedErr, _ := status.FromError(error)
217 assert.Equal(t, parsedErr.Code(), codes.DeadlineExceeded)
218}