package tech.easyflow.datacenter.connector.support;

import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.common.web.exceptions.BusinessException;

import java.sql.SQLException;

/**
 * {@link DatacenterConnectorExceptionSupport} 异常包装测试。
 */
public class DatacenterConnectorExceptionSupportTest {

    /**
     * 验证通用访问错误保留底层 SQL 异常。
     */
    @Test
    public void wrapAccessExceptionShouldPreserveSqlCause() {
        SQLException cause = new SQLException(
                "You have an error in your SQL syntax",
                "42000");

        BusinessException wrapped =
                DatacenterConnectorExceptionSupport.wrapAccessException(
                        "SQL 流式查询失败",
                        cause);

        Assert.assertEquals("SQL 流式查询失败", wrapped.getMessage());
        Assert.assertSame(cause, wrapped.getCause());
    }

    /**
     * 验证连接不可用错误保留底层 SQL 异常并返回安全文案。
     */
    @Test
    public void wrapAccessExceptionShouldPreserveUnavailableCause() {
        SQLException cause = new SQLException(
                "Unknown database 'missing'",
                "42000");

        BusinessException wrapped =
                DatacenterConnectorExceptionSupport.wrapAccessException(
                        "SQL 流式查询失败",
                        cause);

        Assert.assertEquals(
                DatacenterConnectorExceptionSupport.SOURCE_UNAVAILABLE_MESSAGE,
                wrapped.getMessage());
        Assert.assertSame(cause, wrapped.getCause());
    }
}
