package mqMgr

import (
	"fmt"
	"testing"
)

var (
	queueObj = NewQueue(MQ_REGION_GUANGZHOU, "AD-Click", "AKIDjBUWGmIkDPJHCqRb2DCQM2RbUUL1MWMx", "RcldV6PRCBwUkjGVdVjiPq0IJ2VhEZtO")
)

func TestHandleDelaySeconds(t *testing.T) {
	delaySeconds := -1
	expected := 0
	delaySeconds = queueObj.handleDelaySeconds(delaySeconds)
	if delaySeconds != expected {
		t.Errorf("Expected %d, but now got %d", expected, delaySeconds)
	}

	delaySeconds = 10
	expected = 10
	delaySeconds = queueObj.handleDelaySeconds(delaySeconds)
	if delaySeconds != expected {
		t.Errorf("Expected %d, but now got %d", expected, delaySeconds)
	}
}

func TestHandlePollingWaitSeconds(t *testing.T) {
	pollingWaitSeconds := -1
	expected := 0
	pollingWaitSeconds = queueObj.handlePollingWaitSeconds(pollingWaitSeconds)
	if pollingWaitSeconds != expected {
		t.Errorf("Expected %d, but now got %d", expected, pollingWaitSeconds)
	}

	pollingWaitSeconds = 100
	expected = 30
	pollingWaitSeconds = queueObj.handlePollingWaitSeconds(pollingWaitSeconds)
	if pollingWaitSeconds != expected {
		t.Errorf("Expected %d, but now got %d", expected, pollingWaitSeconds)
	}

	pollingWaitSeconds = 15
	expected = 15
	pollingWaitSeconds = queueObj.handlePollingWaitSeconds(pollingWaitSeconds)
	if pollingWaitSeconds != expected {
		t.Errorf("Expected %d, but now got %d", expected, pollingWaitSeconds)
	}
}

func TestValidBatchList(t *testing.T) {
	var list []string
	expected := EMPTY_BATCH_LIST_ERROR
	err := queueObj.validBatchList(list)
	if err == nil {
		t.Errorf("There should be an error, but now there isn't")
	}
	if err.Error() != expected {
		t.Errorf("Expected %s, but now got %s", EMPTY_BATCH_LIST_ERROR, err.Error())
	}

	list = make([]string, 0, 16)
	expected = EMPTY_BATCH_LIST_ERROR
	err = queueObj.validBatchList(list)
	if err == nil {
		t.Errorf("There should be an error, but now there isn't")
	}
	if err.Error() != expected {
		t.Errorf("Expected %s, but now got %s", EMPTY_BATCH_LIST_ERROR, err.Error())
	}

	for i := 0; i <= 2*MAX_BATCH_COUNT; i++ {
		list = append(list, fmt.Sprintf("Test.%d", i))
	}

	expected = EXCEED_MAX_BATCH_COUNT_ERROR
	err = queueObj.validBatchList(list)
	if err == nil {
		t.Errorf("There should be an error, but now there isn't")
	}
	if err.Error() != expected {
		t.Errorf("Expected %s, but now got %s", EMPTY_BATCH_LIST_ERROR, err.Error())
	}
}

func TestSendMessage(t *testing.T) {
	message := "这是测试内容. Test"

	// SendMessage
	err := queueObj.SendMessage(message, 0)
	if err != nil {
		t.Errorf("There should be no error, but now there is: %s", err)
		return
	}

	// ReceiveMessage
	receiptHandle, actualMessage, exist, err := queueObj.ReceiveMessage(3)
	if err != nil {
		t.Errorf("There should be no error, but now there is: %s", err)
		return
	}
	if exist == false {
		t.Errorf("There should be one message, but now it's empty.")
		return
	}
	if message != actualMessage {
		t.Errorf("Expected %s, but got %s", message, actualMessage)
		return
	}

	// DeleteMessage
	err = queueObj.DeleteMessage(receiptHandle)
	if err != nil {
		t.Errorf("There should be no error, but now there is:%s", err)
		return
	}

	// Verify
	_, _, exist, err = queueObj.ReceiveMessage(3)
	if err != nil {
		t.Errorf("There should be no error, but now there is: %s", err)
		return
	}
	if exist {
		t.Errorf("There should be no message, but now there is.")
		return
	}
}

func TestBatchSendMessage(t *testing.T) {
	count := 5
	messageList := make([]string, 0, count)
	for i := 0; i < count; i++ {
		messageList = append(messageList, fmt.Sprintf("This is for test:%d", i+1))
	}

	// BatchSendMessage
	err := queueObj.BatchSendMessage(messageList, 0)
	if err != nil {
		t.Errorf("There should be no error, but now there is:%s", err)
		return
	}

	// BatchReceiveMessage
	receiptHandleList, retMessageList, exist, err := queueObj.BatchReceiveMessage(count, 3)
	if err != nil {
		t.Errorf("There should be no error, but now there is:%s", err)
		return
	}
	if !exist || messageList == nil || len(messageList) != count {
		t.Errorf("There should be %d messages, but now there isn't.", count)
		return
	}

	for i := 0; i < count; i++ {
		if messageList[i] != retMessageList[i] {
			t.Errorf("Expected %s, but got %s", messageList[i], retMessageList[i])
			return
		}
	}

	// BatchDeleteMessage
	_, err = queueObj.BatchDeleteMessage(receiptHandleList)
	if err != nil {
		t.Errorf("There should be no error, but now there is:%s", err)
		return
	}

	// Verify
	_, _, exist, err = queueObj.ReceiveMessage(3)
	if err != nil {
		t.Errorf("There should be no error, but now there is: %s", err)
		return
	}
	if exist {
		t.Errorf("There should be no message, but now there is.")
		return
	}
}
