David Bainbridge | 36d0b20 | 2019-10-23 18:33:34 +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 common |
| 17 | |
| 18 | import ( |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame^] | 19 | "context" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 20 | adapterIf "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif" |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame^] | 21 | "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 Bainbridge | 36d0b20 | 2019-10-23 18:33:34 +0000 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func 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 Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame^] | 39 | |
| 40 | func TestCoreProxy_GetChildDevice_sn(t *testing.T) { |
| 41 | |
| 42 | var mockKafkaIcProxy = mocks.MockKafkaICProxy{ |
| 43 | InvokeRpcSpy: mocks.InvokeRpcSpy{ |
| 44 | Calls: make(map[int]mocks.InvokeRpcArgs), |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 49 | |
| 50 | kwargs := make(map[string]interface{}) |
| 51 | kwargs["serial_number"] = "TEST00000000001" |
| 52 | |
| 53 | parentDeviceId := "aabbcc" |
| 54 | device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs) |
| 55 | |
| 56 | assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1) |
| 57 | call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1] |
| 58 | assert.Equal(t, call.Rpc, "GetChildDevice") |
| 59 | assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"}) |
| 60 | assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"}) |
| 61 | assert.Equal(t, call.WaitForResponse, true) |
| 62 | assert.Equal(t, call.Key, parentDeviceId) |
| 63 | assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}}) |
| 64 | assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "serial_number", Value: &ic.StrType{Val: kwargs["serial_number"].(string)}}) |
| 65 | |
| 66 | assert.Equal(t, "testDevice", device.Id) |
| 67 | assert.Equal(t, nil, error) |
| 68 | } |
| 69 | |
| 70 | func TestCoreProxy_GetChildDevice_id(t *testing.T) { |
| 71 | |
| 72 | var mockKafkaIcProxy = mocks.MockKafkaICProxy{ |
| 73 | InvokeRpcSpy: mocks.InvokeRpcSpy{ |
| 74 | Calls: make(map[int]mocks.InvokeRpcArgs), |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 79 | |
| 80 | kwargs := make(map[string]interface{}) |
| 81 | kwargs["onu_id"] = uint32(1234) |
| 82 | |
| 83 | parentDeviceId := "aabbcc" |
| 84 | device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs) |
| 85 | |
| 86 | assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1) |
| 87 | call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1] |
| 88 | assert.Equal(t, call.Rpc, "GetChildDevice") |
| 89 | assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"}) |
| 90 | assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"}) |
| 91 | assert.Equal(t, call.WaitForResponse, true) |
| 92 | assert.Equal(t, call.Key, parentDeviceId) |
| 93 | assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}}) |
| 94 | assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "onu_id", Value: &ic.IntType{Val: int64(kwargs["onu_id"].(uint32))}}) |
| 95 | |
| 96 | assert.Equal(t, "testDevice", device.Id) |
| 97 | assert.Equal(t, nil, error) |
| 98 | } |
| 99 | |
| 100 | func TestCoreProxy_GetChildDevice_fail_timeout(t *testing.T) { |
| 101 | |
| 102 | var mockKafkaIcProxy = mocks.MockKafkaICProxy{ |
| 103 | InvokeRpcSpy: mocks.InvokeRpcSpy{ |
| 104 | Calls: make(map[int]mocks.InvokeRpcArgs), |
| 105 | Fail: mocks.Timeout, |
| 106 | }, |
| 107 | } |
| 108 | |
| 109 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 110 | |
| 111 | kwargs := make(map[string]interface{}) |
| 112 | kwargs["onu_id"] = uint32(1234) |
| 113 | |
| 114 | parentDeviceId := "aabbcc" |
| 115 | device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs) |
| 116 | |
| 117 | assert.Nil(t, device) |
| 118 | parsedErr, _ := status.FromError(error) |
| 119 | |
| 120 | // TODO assert that the Code is not Internal but DeadlineExceeded |
| 121 | assert.Equal(t, parsedErr.Code(), codes.Internal) |
| 122 | } |
| 123 | |
| 124 | func TestCoreProxy_GetChildDevice_fail_unmarhsal(t *testing.T) { |
| 125 | |
| 126 | var mockKafkaIcProxy = mocks.MockKafkaICProxy{ |
| 127 | InvokeRpcSpy: mocks.InvokeRpcSpy{ |
| 128 | Calls: make(map[int]mocks.InvokeRpcArgs), |
| 129 | Fail: mocks.UnmarshalError, |
| 130 | }, |
| 131 | } |
| 132 | |
| 133 | proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic") |
| 134 | |
| 135 | kwargs := make(map[string]interface{}) |
| 136 | kwargs["onu_id"] = uint32(1234) |
| 137 | |
| 138 | parentDeviceId := "aabbcc" |
| 139 | device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs) |
| 140 | |
| 141 | assert.Nil(t, device) |
| 142 | |
| 143 | parsedErr, _ := status.FromError(error) |
| 144 | assert.Equal(t, parsedErr.Code(), codes.InvalidArgument) |
| 145 | } |