package com.ruoyi.web.modules.architecture.domain;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import com.ruoyi.common.constant.Constants;

/**
 * 实体 Bean Validation（与 Controller @Validated 一致），补充 YZ-BZ-UT-008
 */
@DisplayName("BizBreedingStandard Bean Validation")
class BizBreedingStandardBeanValidationTest
{

    private static ValidatorFactory factory;
    private static Validator validator;

    @BeforeAll
    static void init()
    {
        factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
    }

    @AfterAll
    static void close()
    {
        factory.close();
    }

    @Test
    void nameLengthAndContentRequired()
    {
        BizBreedingStandard row = new BizBreedingStandard();
        row.setStandardName(repeat('x', 21));
        row.setStandardType("001001");
        row.setContentFileUrl("http://x");
        row.setContentFilePath(Constants.RESOURCE_PREFIX + "/upload/a.pdf");
        row.setContentFileName("a.pdf");
        assertFalse(violations(row).isEmpty());

        row.setStandardName("a");
        assertTrue(violations(row).isEmpty());
    }

    private static String repeat(char c, int n)
    {
        char[] a = new char[n];
        java.util.Arrays.fill(a, c);
        return new String(a);
    }

    private static Set<ConstraintViolation<BizBreedingStandard>> violations(BizBreedingStandard row)
    {
        return validator.validate(row);
    }
}
