package container

import (
	"LAPP_LF_MOM_BACKEND/web/models"
	"fmt"
	"testing"
)

func TestSession_New(t *testing.T) {

	sessionId := "sessionId"
	userId := "userId"
	user := &models.Usertab{Userid: userId}
	handlerManager, err := NewHandlerManager(map[Interface]ComponentHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	session, err := NewSession(sessionContext, sessionId, handlerManager)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	if session == nil {
		t.Fatalf("创建失败!")
	}
	if session.user.Userid != userId {
		t.Fatalf("用户标识错误!")
	}
	if session.sessionId != sessionId {
		t.Fatalf("会话标识错误!")
	}
	if session.user != user {
		t.Fatalf("用户信息错误!")
	}
	if session.handlers != handlerManager {
		t.Fatalf("组件句柄管理器错误!")
	}
	if session.transactionHandlerFactory != transactionHandlerFactoryMock {
		t.Fatalf("事务句柄工厂不能为空!")
	}
}

func TestSession_New_EmptySessionId(t *testing.T) {

	sessionId := ""
	userId := "userId"
	user := &models.Usertab{Userid: userId}
	handlerManager, err := NewHandlerManager(map[Interface]ComponentHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	_, err = NewSession(sessionContext, sessionId, handlerManager)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("会话标识不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestSession_New_NullContext(t *testing.T) {

	sessionId := "sessionId"
	handlerManager, err := NewHandlerManager(map[Interface]ComponentHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	_, err = NewSession(nil, sessionId, handlerManager)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("会话上下文不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestSession_New_NullHandlerManager(t *testing.T) {

	sessionId := "sessionId"
	userId := "userId"
	user := &models.Usertab{Userid: userId}
	sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	_, err = NewSession(sessionContext, sessionId, nil)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("组件句柄管理器不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestSession_User(t *testing.T) {

	sessionId := "sessionId"
	userId := "userId"
	user := &models.Usertab{Userid: userId}
	handlerManager, err := NewHandlerManager(map[Interface]ComponentHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	session, err := NewSession(sessionContext, sessionId, handlerManager)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	user = &models.Usertab{Userid: "userId1"}
	session.user = user
	if session.User() != user {
		t.Fatalf("用户标识错误!")
	}
}

func TestSession_SessionId(t *testing.T) {

	sessionId := "sessionId"
	userId := "userId"
	user := &models.Usertab{Userid: userId}
	handlerManager, err := NewHandlerManager(map[Interface]ComponentHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	session, err := NewSession(sessionContext, sessionId, handlerManager)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	sessionId = "sessionId1"
	session.sessionId = sessionId
	if session.SessionId() != sessionId {
		t.Fatalf("用户标识错误!")
	}
}

func TestSession_Handlers(t *testing.T) {

	sessionId := "sessionId"
	userId := "userId"
	user := &models.Usertab{Userid: userId}
	handlerManager, err := NewHandlerManager(map[Interface]ComponentHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	session, err := NewSession(sessionContext, sessionId, handlerManager)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	handlerManager, err = NewHandlerManager(map[Interface]ComponentHandler{})
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	session.handlers = handlerManager
	if session.Handlers() != handlerManager {
		t.Fatalf("用户标识错误!")
	}
}
