package com.ruoyi.web.modules.hub.support;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Date;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.hub.domain.BizDataSendRecord;

@DisplayName("DataSendRecordValidation")
class DataSendRecordValidationTest
{

    @Test
    @DisplayName("发送时间非空")
    void sendTime()
    {
        assertThrows(ServiceException.class, () -> DataSendRecordValidation.validateSendTime(null));
        assertDoesNotThrow(() -> DataSendRecordValidation.validateSendTime(new Date()));
    }

    @Test
    @DisplayName("发送内容 trim 与长度")
    void sendContent()
    {
        assertThrows(ServiceException.class, () -> DataSendRecordValidation.validateSendContent("   "));
        assertThrows(ServiceException.class, () -> DataSendRecordValidation.validateSendContent(longStr(2001)));
        assertDoesNotThrow(() -> DataSendRecordValidation.validateSendContent("牧业产值月报"));
    }

    @Test
    @DisplayName("接收方名称与地址")
    void receiverFields()
    {
        assertThrows(ServiceException.class, () -> DataSendRecordValidation.validateReceiverName(""));
        assertThrows(ServiceException.class, () -> DataSendRecordValidation.validateReceiverName(longStr(129)));
        assertDoesNotThrow(() -> DataSendRecordValidation.validateReceiverName("县级政务平台"));

        assertThrows(ServiceException.class, () -> DataSendRecordValidation.validateReceiverAddress(""));
        assertThrows(ServiceException.class, () -> DataSendRecordValidation.validateReceiverAddress(longStr(513)));
        assertDoesNotThrow(() -> DataSendRecordValidation.validateReceiverAddress("https://api.example.com/v1/receive"));
    }

    @Test
    @DisplayName("发送内容空白转失败")
    void emptyContentAfterTrim()
    {
        BizDataSendRecord row = sampleRow();
        row.setSendContent("   ");
        assertThrows(ServiceException.class, () -> DataSendRecordValidation.validateRowForSave(row));
    }

    @Test
    @DisplayName("列表日期区间")
    void listDateRange()
    {
        assertThrows(ServiceException.class,
                () -> DataSendRecordValidation.validateListDateRange("2025-12-01", "2025-06-01"));
        assertDoesNotThrow(() -> DataSendRecordValidation.validateListDateRange("2025-06-01", "2025-06-30"));
    }

    @Test
    @DisplayName("全量保存合法样本")
    void rowForSave()
    {
        BizDataSendRecord row = sampleRow();
        DataSendRecordValidation.validateRowForSave(row);
        assertEquals("草场利用统计", row.getSendContent());
    }

    private static BizDataSendRecord sampleRow()
    {
        BizDataSendRecord row = new BizDataSendRecord();
        row.setSendTime(new Date());
        row.setSendContent("  草场利用统计  ");
        row.setReceiverName("第三方监管平台");
        row.setReceiverAddress("https://api.example.com/v1/receive");
        return row;
    }

    private static String longStr(int len)
    {
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++)
        {
            sb.append('数');
        }
        return sb.toString();
    }
}
