package container

import (
	"LAPP_ACURA_MOM_BACKEND/global"
	"fmt"
	"testing"
)

func TestNewTransactionBrokerBuilder_New(t *testing.T) {

	builder := NewNewTransactionBrokerBuilder()
	if builder == nil {
		t.Fatalf("创建失败!")
	}
}

func TestNewTransactionBrokerBuilder_Check(t *testing.T) {

	methodName := "StringWithContext"
	method, ok := testServiceType.MethodByName(methodName)
	if !ok {
		t.Fatalf("意外的没找到方法:%s", methodName)
	}
	builder := NewNewTransactionBrokerBuilder()
	err := builder.Check(method.Type)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestNewTransactionBrokerBuilder_Check_Less(t *testing.T) {

	methodName := "String"
	method, ok := testServiceType.MethodByName(methodName)
	if !ok {
		t.Fatalf("意外的没找到方法:%s", methodName)
	}
	builder := NewNewTransactionBrokerBuilder()
	err := builder.Check(method.Type)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("参数太少!实际数量:0") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestNewTransactionBrokerBuilder_Check_FirstNotSession(t *testing.T) {

	methodName := "StringNotFirstContext"
	method, ok := testServiceType.MethodByName(methodName)
	if !ok {
		t.Fatalf("意外的没找到方法:%s", methodName)
	}
	builder := NewNewTransactionBrokerBuilder()
	err := builder.Check(method.Type)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("第一个参数必须是*RequestContext") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestNewTransactionBrokerBuilder_Check_OtherSession(t *testing.T) {

	methodName := "String3"
	method, ok := testServiceType.MethodByName(methodName)
	if !ok {
		t.Fatalf("意外的没找到方法:%s", methodName)
	}
	builder := NewNewTransactionBrokerBuilder()
	err := builder.Check(method.Type)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("第2个参数不能是*RequestContext") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestNewTransactionBrokerBuilder_Check_NullMethodType(t *testing.T) {

	builder := NewNewTransactionBrokerBuilder()
	err := builder.Check(nil)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("方法类型不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestNewTransactionBrokerBuilder_Build_NullCaller(t *testing.T) {

	userId := "userId"
	user := &global.User{UserId: userId}
	sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	builder := NewNewTransactionBrokerBuilder()
	_, err = builder.Build(sessionContext, nil)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("调用器不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestNewTransactionBrokerBuilder_Build_NullSession(t *testing.T) {

	caller, err := serviceInstance.GetCaller("String")
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	builder := NewNewTransactionBrokerBuilder()
	_, err = builder.Build(nil, caller)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("会话上下文不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}
