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

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

import java.math.BigDecimal;
import java.time.Year;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.domain.BizDisposableIncome;

@DisplayName("DisposableIncomeValidation")
class DisposableIncomeValidationTest
{
    @Test
    @DisplayName("缺年份失败")
    void missingYear()
    {
        BizDisposableIncome row = validRow();
        row.setStatYear(null);
        ServiceException ex = assertThrows(ServiceException.class, () -> DisposableIncomeValidation.validateRowForSave(row));
        assertTrue(ex.getMessage().contains("所属年份"));
    }

    @Test
    @DisplayName("非法年份失败")
    void illegalYear()
    {
        BizDisposableIncome row = validRow();
        row.setStatYear(1999);
        ServiceException ex = assertThrows(ServiceException.class, () -> DisposableIncomeValidation.validateRowForSave(row));
        assertTrue(ex.getMessage().contains("可选范围"));
    }

    @Test
    @DisplayName("负可支配收入失败")
    void negativeRuralIncome()
    {
        BizDisposableIncome row = validRow();
        row.setRuralDisposableIncome(new BigDecimal("-1"));
        ServiceException ex = assertThrows(ServiceException.class, () -> DisposableIncomeValidation.validateRowForSave(row));
        assertTrue(ex.getMessage().contains("不能为负数"));
    }

    @Test
    @DisplayName("备注超长失败")
    void remarkTooLong()
    {
        BizDisposableIncome row = validRow();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 501; i++)
        {
            sb.append('a');
        }
        row.setRemark(sb.toString());
        ServiceException ex = assertThrows(ServiceException.class, () -> DisposableIncomeValidation.validateRowForSave(row));
        assertTrue(ex.getMessage().contains("500"));
    }

    @Test
    @DisplayName("合法数据通过")
    void validRowPasses()
    {
        assertDoesNotThrow(() -> DisposableIncomeValidation.validateRowForSave(validRow()));
    }

    private static BizDisposableIncome validRow()
    {
        BizDisposableIncome row = new BizDisposableIncome();
        row.setStatYear(Year.now().getValue());
        row.setRuralDisposableIncome(new BigDecimal("22190"));
        row.setPovertyNetIncome(new BigDecimal("16965"));
        row.setRemark("ok");
        return row;
    }
}
