package com.ruoyi.web.modules.industrypolicy.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 static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;

/**
 * ZCZX-BTSX-UT-001～008、016
 */
@DisplayName("SubsidyMatterValidation")
class SubsidyMatterValidationTest
{
    @Test
    @DisplayName("ZCZX-BTSX-UT-001 补贴名称长度")
    void ut001_subsidyName()
    {
        assertThrows(ServiceException.class, () -> SubsidyMatterValidation.validateSubsidyName(""));
        assertThrows(ServiceException.class, () -> SubsidyMatterValidation.validateSubsidyName(longStr(21)));
        assertDoesNotThrow(() -> SubsidyMatterValidation.validateSubsidyName("补贴"));
    }

    @Test
    @DisplayName("ZCZX-BTSX-UT-002 简介字数")
    void ut002_intro()
    {
        assertDoesNotThrow(() -> SubsidyMatterValidation.validateIntroduction(null));
        assertThrows(ServiceException.class, () -> SubsidyMatterValidation.validateIntroduction(longStr(51)));
    }

    @Test
    @DisplayName("ZCZX-BTSX-UT-003 发放状态枚举")
    void ut003_grantStatus()
    {
        assertDoesNotThrow(() -> SubsidyMatterValidation.validateGrantStatus(1));
        assertDoesNotThrow(() -> SubsidyMatterValidation.validateGrantStatus(2));
        assertThrows(ServiceException.class, () -> SubsidyMatterValidation.validateGrantStatus(0));
        assertThrows(ServiceException.class, () -> SubsidyMatterValidation.validateGrantStatus(null));
    }

    @Test
    @DisplayName("ZCZX-BTSX-UT-004 明细后缀")
    void ut004_detailExt()
    {
        assertTrue(SubsidyMatterRules.isDetailFileExtensionAllowed("a.pdf"));
        assertTrue(SubsidyMatterRules.isDetailFileExtensionAllowed("a.xlsx"));
        org.junit.jupiter.api.Assertions.assertFalse(SubsidyMatterRules.isDetailFileExtensionAllowed("a.exe"));
    }

    @Test
    @DisplayName("ZCZX-BTSX-UT-005 封面后缀")
    void ut005_coverExt()
    {
        assertTrue(SubsidyMatterRules.isCoverExtensionAllowed("c.jpg"));
        org.junit.jupiter.api.Assertions.assertFalse(SubsidyMatterRules.isCoverExtensionAllowed("c.bmp"));
    }

    @Test
    @DisplayName("ZCZX-BTSX-UT-006 明细三件套")
    void ut006_detailGroup()
    {
        assertThrows(ServiceException.class, () -> SubsidyMatterValidation.validateDetailFileGroup(
                "http://x", null, "a.pdf"));
        assertDoesNotThrow(() -> SubsidyMatterValidation.validateDetailFileGroup(
                "http://x", "/profile/upload/a.pdf", "a.pdf"));
    }

    @Test
    @DisplayName("ZCZX-BTSX-UT-007 封面成对")
    void ut007_coverPair()
    {
        assertDoesNotThrow(() -> SubsidyMatterValidation.validateCoverPair(null, null));
        assertThrows(ServiceException.class, () -> SubsidyMatterValidation.validateCoverPair(
                "http://c", null));
        assertDoesNotThrow(() -> SubsidyMatterValidation.validateCoverPair(
                "http://c", "/profile/upload/c.jpg"));
    }

    @Test
    @DisplayName("ZCZX-BTSX-UT-008 缺省发放状态")
    void ut008_defaultGrantStatus()
    {
        assertEquals(SubsidyMatterRules.GRANT_APPLYING, SubsidyMatterValidation.resolveGrantStatus(null));
    }

    @Test
    @DisplayName("ZCZX-BTSX-UT-016 列表排序 SQL")
    void ut016_listOrderBy() throws Exception
    {
        String path = "src/main/resources/mapper/industrypolicy/BizSubsidyMatterMapper.xml";
        String xml = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
        assertTrue(xml.contains("create_time desc"));
        assertTrue(xml.contains("id desc"));
    }

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