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

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

import java.util.Calendar;
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.industrypolicy.domain.BizIndustryPolicyPublicity;

/**
 * ZCZX-XC-UT-005～011
 */
@DisplayName("IndustryPolicyPublicityValidation")
class IndustryPolicyPublicityValidationTest
{

    @Test
    @DisplayName("ZCZX-XC-UT-005 政策名称长度")
    void ut005_policyName()
    {
        assertThrows(ServiceException.class, () -> IndustryPolicyPublicityValidation.validatePolicyName(""));
        assertThrows(ServiceException.class, () -> IndustryPolicyPublicityValidation.validatePolicyName(longStr(21)));
        assertDoesNotThrow(() -> IndustryPolicyPublicityValidation.validatePolicyName("政策"));
    }

    @Test
    @DisplayName("ZCZX-XC-UT-006 政策层级")
    void ut006_policyLevel()
    {
        assertDoesNotThrow(() -> IndustryPolicyPublicityValidation.validatePolicyLevel(1));
        assertThrows(ServiceException.class, () -> IndustryPolicyPublicityValidation.validatePolicyLevel(0));
    }

    @Test
    @DisplayName("ZCZX-XC-UT-007 简介与描述")
    void ut007_introDesc()
    {
        assertDoesNotThrow(() -> IndustryPolicyPublicityValidation.validateIntroduction(null));
        assertThrows(ServiceException.class, () -> IndustryPolicyPublicityValidation.validateIntroduction(longStr(51)));
        assertThrows(ServiceException.class, () -> IndustryPolicyPublicityValidation.validatePolicyDesc(""));
        assertDoesNotThrow(() -> IndustryPolicyPublicityValidation.validatePolicyDesc("描述"));
    }

    @Test
    @DisplayName("ZCZX-XC-UT-008 出台日期非未来")
    void ut008_issueDate()
    {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        assertThrows(ServiceException.class, () -> IndustryPolicyPublicityValidation.validateIssueDate(cal.getTime()));
        assertDoesNotThrow(() -> IndustryPolicyPublicityValidation.validateIssueDate(new Date()));
    }

    @Test
    @DisplayName("ZCZX-XC-UT-011 政策文件三件套")
    void ut011_policyFileGroup()
    {
        assertDoesNotThrow(() -> IndustryPolicyPublicityValidation.validatePolicyFileGroup(null, null, null));
        assertDoesNotThrow(() -> IndustryPolicyPublicityValidation.validatePolicyFileGroup(
                "http://x", "/profile/upload/a.pdf", "a.pdf"));
        assertThrows(ServiceException.class, () -> IndustryPolicyPublicityValidation.validatePolicyFileGroup(
                "http://x", null, "a.pdf"));
    }

    @Test
    @DisplayName("封面成对")
    void coverPair()
    {
        assertDoesNotThrow(() -> IndustryPolicyPublicityValidation.validateCoverPair(null, null));
        assertDoesNotThrow(() -> IndustryPolicyPublicityValidation.validateCoverPair(
                "http://c", "/profile/upload/c.jpg"));
        assertThrows(ServiceException.class, () -> IndustryPolicyPublicityValidation.validateCoverPair(
                "http://c", null));
    }

    @Test
    @DisplayName("trim 简介空白变 null")
    void trimIntro()
    {
        BizIndustryPolicyPublicity row = sample();
        row.setIntroduction("   ");
        IndustryPolicyPublicityValidation.trimFieldsForSave(row);
        org.junit.jupiter.api.Assertions.assertNull(row.getIntroduction());
    }

    private static BizIndustryPolicyPublicity sample()
    {
        BizIndustryPolicyPublicity r = new BizIndustryPolicyPublicity();
        r.setPolicyName("政策");
        r.setPolicyLevel(1);
        r.setIssueDate(new Date());
        r.setPolicyDesc("描述");
        return r;
    }

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