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

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

import java.util.Arrays;
import java.util.Collections;

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

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.architecture.domain.BizDrugIncompatibilityCompanion;
import com.ruoyi.web.modules.architecture.domain.BizDrugIncompatibilityStandard;

@DisplayName("DrugIncompatibilityStandardValidation")
class DrugIncompatibilityStandardValidationTest
{

    @Test
    @DisplayName("主药名称")
    void mainName()
    {
        assertThrows(ServiceException.class, () -> DrugIncompatibilityStandardValidation.validateMainDrugName(""));
        assertThrows(ServiceException.class, () -> DrugIncompatibilityStandardValidation.validateMainDrugName(repeat('a', 21)));
        assertDoesNotThrow(() -> DrugIncompatibilityStandardValidation.validateMainDrugName("头孢"));
    }

    @Test
    @DisplayName("配伍结果")
    void compatResult()
    {
        assertDoesNotThrow(() -> DrugIncompatibilityStandardValidation.validateCompatResult(null));
        assertDoesNotThrow(() -> DrugIncompatibilityStandardValidation.validateCompatResult("  "));
        assertDoesNotThrow(() -> DrugIncompatibilityStandardValidation.validateCompatResult(repeat('x', 50)));
        assertThrows(ServiceException.class, () -> DrugIncompatibilityStandardValidation.validateCompatResult(repeat('x', 51)));
    }

    @Test
    @DisplayName("配伍列表")
    void companions()
    {
        BizDrugIncompatibilityStandard row = new BizDrugIncompatibilityStandard();
        row.setMainDrugName("主药");
        row.setCompanions(Collections.emptyList());
        assertThrows(ServiceException.class, () -> DrugIncompatibilityStandardValidation.validateCompanions(row));

        row.setCompanions(Arrays.asList(comp("A"), comp("A")));
        assertThrows(ServiceException.class, () -> DrugIncompatibilityStandardValidation.validateCompanions(row));

        row.setCompanions(Collections.singletonList(comp("主药")));
        assertThrows(ServiceException.class, () -> DrugIncompatibilityStandardValidation.validateCompanions(row));

        row.setCompanions(Arrays.asList(comp("配伍一"), comp("配伍二")));
        assertDoesNotThrow(() -> DrugIncompatibilityStandardValidation.validateCompanions(row));
    }

    private static BizDrugIncompatibilityCompanion comp(String name)
    {
        BizDrugIncompatibilityCompanion c = new BizDrugIncompatibilityCompanion();
        c.setCompanionDrugName(name);
        return c;
    }

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