package container

import (
	"fmt"
	"testing"
)

func TestServiceHandler_New(t *testing.T) {
	methodName := "String"
	caller, err := serviceInstance.GetCaller(methodName)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	methodHandler, err := NewServiceMethodHandler(caller)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	serviceHandler, err := NewServiceHandler(serviceInstance, map[string]*ServiceMethodHandler{methodName: methodHandler})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	if serviceHandler == nil {
		t.Fatalf("创建失败!")
	}
	if serviceHandler.instance != serviceInstance {
		t.Fatalf("实例设置错误!")
	}
	if len(serviceHandler.methodMapping) != 1 {
		t.Fatalf("方法句柄映射数量错误!")
	}
	methodHandler1, ok := serviceHandler.methodMapping[methodName]
	if !ok {
		t.Fatalf("方法句柄映名称设置错误")
	}
	if methodHandler != methodHandler1 {
		t.Fatalf("方法句柄映设置错误")
	}
}

func TestServiceHandler_New_NullMethodMapping(t *testing.T) {
	_, err := NewServiceHandler(serviceInstance, nil)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("方法句柄映射不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestServiceHandler_New_EmptyMethodName(t *testing.T) {
	methodName := "String"
	caller, err := serviceInstance.GetCaller(methodName)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	methodHandler, err := NewServiceMethodHandler(caller)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	_, err = NewServiceHandler(serviceInstance, map[string]*ServiceMethodHandler{"": methodHandler})
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("方法名不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestServiceHandler_New_NullMethodHandler(t *testing.T) {
	methodName := "String"
	_, err := NewServiceHandler(serviceInstance, map[string]*ServiceMethodHandler{methodName: nil})
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("方法句柄不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestServiceHandler_Instance(t *testing.T) {
	handler, err := NewServiceHandler(ZeroInstance, map[string]*ServiceMethodHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	handler.instance = serviceInstance
	if handler.Instance() != serviceInstance {
		t.Fatalf("获取实例错误!")
	}
}

func TestServiceHandler_Method(t *testing.T) {
	serviceHandler, err := NewServiceHandler(serviceInstance, map[string]*ServiceMethodHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	if serviceHandler.instance != serviceInstance {
		t.Fatalf("实例设置错误!")
	}
	methodName := "String"
	caller, err := serviceInstance.GetCaller(methodName)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	methodHandler, err := NewServiceMethodHandler(caller)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	serviceHandler.methodMapping[methodName] = methodHandler
	if serviceHandler.Method(methodName) != methodHandler {
		t.Fatalf("获取方法句柄映射错误")
	}
}

func TestServiceHandler_Method_EmptyMethodName(t *testing.T) {
	serviceHandler, err := NewServiceHandler(serviceInstance, map[string]*ServiceMethodHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	if serviceHandler.instance != serviceInstance {
		t.Fatalf("实例设置错误!")
	}
	methodName := "String"
	caller, err := serviceInstance.GetCaller(methodName)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	methodHandler, err := NewServiceMethodHandler(caller)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	serviceHandler.methodMapping[methodName] = methodHandler
	if serviceHandler.Method("") != nil {
		t.Fatalf("获取空方法名的方法句柄映射错误")
	}
}

func TestServiceHandler_Method_MissingMethod(t *testing.T) {
	serviceHandler, err := NewServiceHandler(serviceInstance, map[string]*ServiceMethodHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	if serviceHandler.instance != serviceInstance {
		t.Fatalf("实例设置错误!")
	}
	methodName := "String"
	caller, err := serviceInstance.GetCaller(methodName)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	methodHandler, err := NewServiceMethodHandler(caller)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	serviceHandler.methodMapping[methodName] = methodHandler
	if serviceHandler.Method("String1") != nil {
		t.Fatalf("找到了不存在的方法句柄")
	}
}
