[VOL-2364] Adding unit test in the core_proxy package

Change-Id: Ifcaa986ae27280de9f16f3a9cabf45bb94c0d5d8
diff --git a/pkg/adapters/common/adapter_proxy.go b/pkg/adapters/common/adapter_proxy.go
index b302214..02fa3de 100644
--- a/pkg/adapters/common/adapter_proxy.go
+++ b/pkg/adapters/common/adapter_proxy.go
@@ -29,12 +29,12 @@
 )
 
 type AdapterProxy struct {
-	kafkaICProxy *kafka.InterContainerProxy
+	kafkaICProxy kafka.InterContainerProxy
 	adapterTopic string
 	coreTopic    string
 }
 
-func NewAdapterProxy(kafkaProxy *kafka.InterContainerProxy, adapterTopic string, coreTopic string) *AdapterProxy {
+func NewAdapterProxy(kafkaProxy kafka.InterContainerProxy, adapterTopic string, coreTopic string) *AdapterProxy {
 	var proxy AdapterProxy
 	proxy.kafkaICProxy = kafkaProxy
 	proxy.adapterTopic = adapterTopic
diff --git a/pkg/adapters/common/common_test.go b/pkg/adapters/common/common_test.go
new file mode 100644
index 0000000..d2d9f0e
--- /dev/null
+++ b/pkg/adapters/common/common_test.go
@@ -0,0 +1,54 @@
+/*
+ * Portions copyright 2019-present Open Networking Foundation
+ * Original copyright 2019-present Ciena Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the"github.com/stretchr/testify/assert" "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package common
+
+/*
+ * This file has common code that is imported for all test cases, but
+ * is not built into production binaries.
+ */
+
+import (
+	"github.com/opencord/voltha-lib-go/v3/pkg/log"
+)
+
+const (
+	/*
+	 * This sets the LogLevel of the Voltha logger. It's pinned to FatalLevel here, as we
+	 * generally don't want to see logger output, even when running go test in verbose
+	 * mode. Even "Error" level messages are expected to be output by some unit tests.
+	 *
+	 * If you are developing a unit test, and experiencing problems or wish additional
+	 * debugging from Voltha, then changing this constant to log.DebugLevel may be
+	 * useful.
+	 */
+
+	VOLTHA_LOGLEVEL = log.FatalLevel
+)
+
+// Unit test initialization. This init() function will be run once for all unit tests in afrouter
+func init() {
+	// Logger must be configured or bad things happen
+	_, err := log.SetDefaultLogger(log.JSON, VOLTHA_LOGLEVEL, log.Fields{"instanceId": 1})
+	if err != nil {
+		panic(err)
+	}
+
+	_, err = log.AddPackage(log.JSON, VOLTHA_LOGLEVEL, nil)
+	if err != nil {
+		panic(err)
+	}
+}
diff --git a/pkg/adapters/common/core_proxy.go b/pkg/adapters/common/core_proxy.go
index 9b46c28..cf80858 100644
--- a/pkg/adapters/common/core_proxy.go
+++ b/pkg/adapters/common/core_proxy.go
@@ -30,14 +30,14 @@
 )
 
 type CoreProxy struct {
-	kafkaICProxy        *kafka.InterContainerProxy
+	kafkaICProxy        kafka.InterContainerProxy
 	adapterTopic        string
 	coreTopic           string
 	deviceIdCoreMap     map[string]string
 	lockDeviceIdCoreMap sync.RWMutex
 }
 
-func NewCoreProxy(kafkaProxy *kafka.InterContainerProxy, adapterTopic string, coreTopic string) *CoreProxy {
+func NewCoreProxy(kafkaProxy kafka.InterContainerProxy, adapterTopic string, coreTopic string) *CoreProxy {
 	var proxy CoreProxy
 	proxy.kafkaICProxy = kafkaProxy
 	proxy.adapterTopic = adapterTopic
diff --git a/pkg/adapters/common/core_proxy_test.go b/pkg/adapters/common/core_proxy_test.go
index b47f43e..6d2f78c 100644
--- a/pkg/adapters/common/core_proxy_test.go
+++ b/pkg/adapters/common/core_proxy_test.go
@@ -16,9 +16,16 @@
 package common
 
 import (
-	"testing"
-
+	"context"
 	adapterIf "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
+	"github.com/opencord/voltha-lib-go/v3/pkg/kafka"
+	"github.com/opencord/voltha-lib-go/v3/pkg/mocks"
+	ic "github.com/opencord/voltha-protos/v3/go/inter_container"
+	"github.com/opencord/voltha-protos/v3/go/voltha"
+	"github.com/stretchr/testify/assert"
+	"google.golang.org/grpc/codes"
+	"google.golang.org/grpc/status"
+	"testing"
 )
 
 func TestCoreProxyImplementsAdapterIfCoreProxy(t *testing.T) {
@@ -29,3 +36,110 @@
 	}
 
 }
+
+func TestCoreProxy_GetChildDevice_sn(t *testing.T) {
+
+	var mockKafkaIcProxy = mocks.MockKafkaICProxy{
+		InvokeRpcSpy: mocks.InvokeRpcSpy{
+			Calls: make(map[int]mocks.InvokeRpcArgs),
+		},
+	}
+
+	proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+
+	kwargs := make(map[string]interface{})
+	kwargs["serial_number"] = "TEST00000000001"
+
+	parentDeviceId := "aabbcc"
+	device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
+
+	assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
+	call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
+	assert.Equal(t, call.Rpc, "GetChildDevice")
+	assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
+	assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
+	assert.Equal(t, call.WaitForResponse, true)
+	assert.Equal(t, call.Key, parentDeviceId)
+	assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}})
+	assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "serial_number", Value: &ic.StrType{Val: kwargs["serial_number"].(string)}})
+
+	assert.Equal(t, "testDevice", device.Id)
+	assert.Equal(t, nil, error)
+}
+
+func TestCoreProxy_GetChildDevice_id(t *testing.T) {
+
+	var mockKafkaIcProxy = mocks.MockKafkaICProxy{
+		InvokeRpcSpy: mocks.InvokeRpcSpy{
+			Calls: make(map[int]mocks.InvokeRpcArgs),
+		},
+	}
+
+	proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+
+	kwargs := make(map[string]interface{})
+	kwargs["onu_id"] = uint32(1234)
+
+	parentDeviceId := "aabbcc"
+	device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
+
+	assert.Equal(t, mockKafkaIcProxy.InvokeRpcSpy.CallCount, 1)
+	call := mockKafkaIcProxy.InvokeRpcSpy.Calls[1]
+	assert.Equal(t, call.Rpc, "GetChildDevice")
+	assert.Equal(t, call.ToTopic, &kafka.Topic{Name: "testCoreTopic"})
+	assert.Equal(t, call.ReplyToTopic, &kafka.Topic{Name: "testAdapterTopic"})
+	assert.Equal(t, call.WaitForResponse, true)
+	assert.Equal(t, call.Key, parentDeviceId)
+	assert.Equal(t, call.KvArgs[0], &kafka.KVArg{Key: "device_id", Value: &voltha.ID{Id: parentDeviceId}})
+	assert.Equal(t, call.KvArgs[1], &kafka.KVArg{Key: "onu_id", Value: &ic.IntType{Val: int64(kwargs["onu_id"].(uint32))}})
+
+	assert.Equal(t, "testDevice", device.Id)
+	assert.Equal(t, nil, error)
+}
+
+func TestCoreProxy_GetChildDevice_fail_timeout(t *testing.T) {
+
+	var mockKafkaIcProxy = mocks.MockKafkaICProxy{
+		InvokeRpcSpy: mocks.InvokeRpcSpy{
+			Calls: make(map[int]mocks.InvokeRpcArgs),
+			Fail:  mocks.Timeout,
+		},
+	}
+
+	proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+
+	kwargs := make(map[string]interface{})
+	kwargs["onu_id"] = uint32(1234)
+
+	parentDeviceId := "aabbcc"
+	device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
+
+	assert.Nil(t, device)
+	parsedErr, _ := status.FromError(error)
+
+	// TODO assert that the Code is not Internal but DeadlineExceeded
+	assert.Equal(t, parsedErr.Code(), codes.Internal)
+}
+
+func TestCoreProxy_GetChildDevice_fail_unmarhsal(t *testing.T) {
+
+	var mockKafkaIcProxy = mocks.MockKafkaICProxy{
+		InvokeRpcSpy: mocks.InvokeRpcSpy{
+			Calls: make(map[int]mocks.InvokeRpcArgs),
+			Fail:  mocks.UnmarshalError,
+		},
+	}
+
+	proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+
+	kwargs := make(map[string]interface{})
+	kwargs["onu_id"] = uint32(1234)
+
+	parentDeviceId := "aabbcc"
+	device, error := proxy.GetChildDevice(context.TODO(), parentDeviceId, kwargs)
+
+	assert.Nil(t, device)
+
+	parsedErr, _ := status.FromError(error)
+	assert.Equal(t, parsedErr.Code(), codes.InvalidArgument)
+}