VOL-1925 Method-router unit tests;
return errors on invalid ReplyHandler calls
return error and print error messages in Route calls

Change-Id: I846801d1ff403c02b8e1326061c37629fef83838
diff --git a/afrouter/afrouter/method-router.go b/afrouter/afrouter/method-router.go
index 56d025e..2916edf 100644
--- a/afrouter/afrouter/method-router.go
+++ b/afrouter/afrouter/method-router.go
@@ -168,12 +168,10 @@
 		if r, ok := mr.methodRouter[NoMeta][sl.method]; ok {
 			return r.ReplyHandler(sel)
 		}
-		// TODO: this case should also be an error
-	default: //TODO: This should really be a big error
-		// A reply handler should only be called on the responseFrame
-		return nil
+		return errors.New("MethodRouter.ReplyHandler called with unknown meta or method")
+	default:
+		return errors.New("MethodRouter.ReplyHandler called with non-reponseFrame")
 	}
-	return nil
 }
 
 func (mr MethodRouter) Route(sel interface{}) (*backend, *connection) {
@@ -182,9 +180,11 @@
 		if r, ok := mr.methodRouter[sl.metaKey][sl.methodInfo.method]; ok {
 			return r.Route(sel)
 		}
-		log.Errorf("Attept to route on non-existent method '%s'", sl.methodInfo.method)
+		sl.err = fmt.Errorf("MethodRouter.Route unable to resolve meta %s, method %s", sl.metaKey, sl.methodInfo.method)
+		log.Error(sl.err)
 		return nil, nil
 	default:
+		log.Errorf("Internal: invalid data type in Route call %v", sel)
 		return nil, nil
 	}
 }
diff --git a/afrouter/afrouter/method-router_test.go b/afrouter/afrouter/method-router_test.go
new file mode 100644
index 0000000..21599e5
--- /dev/null
+++ b/afrouter/afrouter/method-router_test.go
@@ -0,0 +1,396 @@
+/*
+ * 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 afrouter
+
+import (
+	"fmt"
+	"github.com/golang/protobuf/proto"
+	"github.com/opencord/voltha-go/common/log"
+	common_pb "github.com/opencord/voltha-protos/go/common"
+	voltha_pb "github.com/opencord/voltha-protos/go/voltha"
+	"github.com/stretchr/testify/assert"
+	"google.golang.org/grpc"
+	"testing"
+)
+
+const (
+	METHOD_ROUTER_PROTOFILE = "../../vendor/github.com/opencord/voltha-protos/go/voltha.pb"
+)
+
+// Unit test initialization
+func init() {
+	// Logger must be configured or bad things happen
+	log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
+	log.AddPackage(log.JSON, log.WarnLevel, nil)
+}
+
+// Build an method router configuration
+func MakeMethodTestConfig(numBackends int, numConnections int) (*RouteConfig, *RouterConfig) {
+
+	var backends []BackendConfig
+	for backendIndex := 0; backendIndex < numBackends; backendIndex++ {
+		var connections []ConnectionConfig
+		for connectionIndex := 0; connectionIndex < numConnections; connectionIndex++ {
+			connectionConfig := ConnectionConfig{
+				Name: fmt.Sprintf("rw_vcore%d%d", backendIndex, connectionIndex+1),
+				Addr: "foo",
+				Port: "123",
+			}
+			connections = append(connections, connectionConfig)
+		}
+
+		backendConfig := BackendConfig{
+			Name:        fmt.Sprintf("rw_vcore%d", backendIndex),
+			Type:        BackendSingleServer,
+			Connections: connections,
+		}
+
+		backends = append(backends, backendConfig)
+	}
+
+	backendClusterConfig := BackendClusterConfig{
+		Name:     "vcore",
+		Backends: backends,
+	}
+
+	routeConfig := RouteConfig{
+		Name:             "dev_manager",
+		Type:             RouteTypeRpcAffinityMessage,
+		Association:      AssociationRoundRobin,
+		BackendCluster:   "vcore",
+		backendCluster:   &backendClusterConfig,
+		RouteField:       "id",
+		Methods:          []string{"CreateDevice", "EnableDevice"},
+		NbBindingMethods: []string{"CreateDevice"},
+	}
+
+	routerConfig := RouterConfig{
+		Name:         "vcore",
+		ProtoService: "VolthaService",
+		ProtoPackage: "voltha",
+		Routes:       []RouteConfig{routeConfig},
+		ProtoFile:    METHOD_ROUTER_PROTOFILE,
+	}
+	return &routeConfig, &routerConfig
+}
+
+// Route() requires an open connection, so pretend we have one.
+func PretendMethodOpenConnection(router Router, clusterName string, backendIndex int, connectionName string) {
+	cluster := router.FindBackendCluster(clusterName)
+
+	// Route Method expects an open connection
+	conn := cluster.backends[backendIndex].connections[connectionName]
+	cluster.backends[backendIndex].openConns[conn] = &grpc.ClientConn{}
+}
+
+// Common setup to run before each unit test
+func MethodTestSetup() {
+	// reset globals that need to be clean for each unit test
+
+	clusters = make(map[string]*cluster)
+	allRouters = make(map[string]Router)
+}
+
+// Test creation of a new AffinityRouter, and the Service(), Name(), FindBackendCluster(), and
+// methods.
+func TestMethodRouterInit(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newMethodRouter(routerConfig)
+
+	assert.NotNil(t, router)
+	assert.Nil(t, err)
+
+	assert.Equal(t, router.Service(), "VolthaService")
+	assert.Equal(t, router.Name(), "vcore")
+
+	cluster, err := router.BackendCluster("EnableDevice", NoMeta)
+	assert.Equal(t, cluster, clusters["vcore"])
+	assert.Nil(t, err)
+
+	assert.Equal(t, router.FindBackendCluster("vcore"), clusters["vcore"])
+}
+
+// Passing an invalid meta should return an error
+func TestMethodRouterBackendClusterInvalidMeta(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newMethodRouter(routerConfig)
+
+	assert.NotNil(t, router)
+	assert.Nil(t, err)
+
+	cluster, err := router.BackendCluster("EnableDevice", "wrongmeta")
+	assert.EqualError(t, err, "No backend cluster exists for method 'EnableDevice' using meta key 'wrongmeta'")
+	assert.Nil(t, cluster)
+}
+
+// Passing an invalid method name should return an error
+func TestMethodRouterBackendClusterInvalidMethod(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newMethodRouter(routerConfig)
+
+	assert.NotNil(t, router)
+	assert.Nil(t, err)
+
+	cluster, err := router.BackendCluster("WrongMethod", NoMeta)
+	assert.EqualError(t, err, "No backend cluster exists for method 'WrongMethod' using meta key 'nometa'")
+	assert.Nil(t, cluster)
+}
+
+// Search for a backend cluster that doesn't exist
+func TestMethodRouterFindBackendClusterNoExist(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newMethodRouter(routerConfig)
+
+	assert.NotNil(t, router)
+	assert.Nil(t, err)
+
+	assert.Nil(t, router.FindBackendCluster("wrong"))
+}
+
+// MethodRouter's route will cause another router's route, in this case AffinityRouter.
+func TestMethodRoute(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newMethodRouter(routerConfig)
+	assert.Nil(t, err)
+
+	PretendMethodOpenConnection(router, "vcore", 0, "rw_vcore01")
+
+	idMessage := &common_pb.ID{Id: "1234"}
+
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	sel := &requestFrame{payload: idData,
+		err:        nil,
+		metaKey:    NoMeta,
+		methodInfo: newMethodDetails("/voltha.VolthaService/EnableDevice")}
+
+	backend, connection := router.Route(sel)
+
+	assert.Nil(t, sel.err)
+	assert.NotNil(t, backend)
+	assert.Equal(t, "rw_vcore0", backend.name)
+	assert.Nil(t, connection)
+
+	// Since we only have one backend, calling Route a second time should return the same one
+
+	backend, connection = router.Route(sel)
+
+	assert.Nil(t, sel.err)
+	assert.NotNil(t, backend)
+	assert.Equal(t, "rw_vcore0", backend.name)
+	assert.Nil(t, connection)
+}
+
+// Try to route to a nonexistent method
+func TestMethodRouteNonexistent(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newMethodRouter(routerConfig)
+	assert.Nil(t, err)
+
+	idMessage := &common_pb.ID{Id: "1234"}
+
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	sel := &requestFrame{payload: idData,
+		err:        nil,
+		metaKey:    NoMeta,
+		methodInfo: newMethodDetails("/voltha.VolthaService/NonexistentMethod")}
+
+	backend, connection := router.Route(sel)
+
+	assert.Nil(t, backend)
+	assert.Nil(t, connection)
+
+	assert.EqualError(t, sel.err, "MethodRouter.Route unable to resolve meta nometa, method NonexistentMethod")
+}
+
+// Try to route to a nonexistent meta key
+func TestMethodRouteWrongMeta(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newMethodRouter(routerConfig)
+	assert.Nil(t, err)
+
+	idMessage := &common_pb.ID{Id: "1234"}
+
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	sel := &requestFrame{payload: idData,
+		err:        nil,
+		metaKey:    "wrongkey",
+		methodInfo: newMethodDetails("/voltha.VolthaService/EnableDevice")}
+
+	backend, connection := router.Route(sel)
+
+	assert.Nil(t, backend)
+	assert.Nil(t, connection)
+
+	assert.EqualError(t, sel.err, "MethodRouter.Route unable to resolve meta wrongkey, method EnableDevice")
+}
+
+// Try to route to a the wrong type of key
+func TestMethodRouteWrongFrame(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newMethodRouter(routerConfig)
+	assert.Nil(t, err)
+
+	idMessage := &voltha_pb.Device{Id: "1234"}
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	// Note that sel.backend must be set. As this is a response, it must
+	// have come from a backend and that backend must be known.
+
+	sel := &responseFrame{payload: idData,
+		metaKey: NoMeta,
+		backend: router.FindBackendCluster("vcore").backends[0],
+		method:  "CreateDevice",
+	}
+
+	// Note: Does not return any error, but does print an error message. Returns nil.
+
+	backend, connection := router.Route(sel)
+
+	assert.Nil(t, backend)
+	assert.Nil(t, connection)
+}
+
+// MethodRouter calls another Router's ReplyHandler, in this case AffinityRouter
+func TestMethodRouteReply(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newRouter(routerConfig)
+	assert.Nil(t, err)
+
+	aRouter := allRouters["vcoredev_manager"].(AffinityRouter)
+
+	PretendMethodOpenConnection(router, "vcore", 0, "rw_vcore01")
+
+	idMessage := &voltha_pb.Device{Id: "1234"}
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	// Note that sel.backend must be set. As this is a response, it must
+	// have come from a backend and that backend must be known.
+
+	sel := &responseFrame{payload: idData,
+		metaKey: NoMeta,
+		backend: router.FindBackendCluster("vcore").backends[0],
+		method:  "CreateDevice",
+	}
+
+	// affinity should be unset as we have not routed yet
+	assert.Empty(t, aRouter.affinity)
+
+	err = router.ReplyHandler(sel)
+	assert.Nil(t, err)
+
+	// affinity should now be set
+	assert.NotEmpty(t, aRouter.affinity)
+	assert.Equal(t, router.FindBackendCluster("vcore").backends[0], aRouter.affinity["1234"])
+}
+
+// Call ReplyHandler with the wrong type of frame
+func TestMethodRouteReplyWrongFrame(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newRouter(routerConfig)
+	assert.Nil(t, err)
+
+	PretendMethodOpenConnection(router, "vcore", 0, "rw_vcore01")
+
+	idMessage := &common_pb.ID{Id: "1234"}
+
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	sel := &requestFrame{payload: idData,
+		err:        nil,
+		metaKey:    "wrongkey",
+		methodInfo: newMethodDetails("/voltha.VolthaService/EnableDevice")}
+
+	err = router.ReplyHandler(sel)
+	assert.EqualError(t, err, "MethodRouter.ReplyHandler called with non-reponseFrame")
+}
+
+// Call ReplyHandler with an invalid method name
+func TestMethodRouteReplyWrongMethod(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newRouter(routerConfig)
+	assert.Nil(t, err)
+
+	PretendMethodOpenConnection(router, "vcore", 0, "rw_vcore01")
+
+	idMessage := &common_pb.ID{Id: "1234"}
+
+	idData, err := proto.Marshal(idMessage)
+	assert.Nil(t, err)
+
+	sel := &requestFrame{payload: idData,
+		err:        nil,
+		metaKey:    "wrongkey",
+		methodInfo: newMethodDetails("/voltha.VolthaService/WrongMethod")}
+
+	err = router.ReplyHandler(sel)
+	assert.EqualError(t, err, "MethodRouter.ReplyHandler called with non-reponseFrame")
+}
+
+func TestMethodIsMethodStreaming(t *testing.T) {
+	MethodTestSetup()
+
+	_, routerConfig := MakeMethodTestConfig(1, 1)
+
+	router, err := newRouter(routerConfig)
+	assert.Nil(t, err)
+
+	request, response := router.IsStreaming("EnableDevice")
+	assert.False(t, request)
+	assert.False(t, response)
+}