package com.ruoyi.web.modules.epidemic.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 java.math.BigDecimal;
import java.util.Calendar;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.epidemic.LabDetectionTestSamples;
import com.ruoyi.web.modules.epidemic.domain.BizLabDetection;

/**
 * SYJC-UT-002、005～006
 */
@DisplayName("LabDetectionValidation")
class LabDetectionValidationTest
{

    @Test
    @DisplayName("SYJC-UT-002 检测日期必填")
    void ut002_testDate()
    {
        assertThrows(ServiceException.class, () -> LabDetectionValidation.validateTestDate(null));
        assertDoesNotThrow(() -> LabDetectionValidation.validateTestDate(
                LabDetectionTestSamples.date(2026, Calendar.MAY, 1)));
    }

    @Test
    @DisplayName("SYJC-UT-005 阳性不大于检测量")
    void ut005_quantityRelation()
    {
        assertThrows(ServiceException.class, () -> LabDetectionValidation.validateQuantityRelation(10, 11));
        assertDoesNotThrow(() -> LabDetectionValidation.validateQuantityRelation(10, 10));
    }

    @Test
    @DisplayName("SYJC-UT-006 零检测量")
    void ut006_zeroQuantity()
    {
        assertThrows(ServiceException.class, () -> LabDetectionValidation.validateQuantityRelation(0, 1));
        assertDoesNotThrow(() -> LabDetectionValidation.validateQuantityRelation(0, 0));
    }

    @Test
    @DisplayName("applyDetectionRate 零检测量强制阳性为0")
    void applyRateZeroQuantity()
    {
        BizLabDetection row = LabDetectionTestSamples.sample();
        row.setTestQuantity(0);
        row.setPositiveCount(0);
        LabDetectionValidation.applyDetectionRate(row);
        assertEquals(0, row.getPositiveCount());
        assertEquals(new BigDecimal("0.0000"), row.getDetectionRate());
    }

    @Test
    @DisplayName("检疫点必填")
    void quarantineStationRequired()
    {
        assertThrows(ServiceException.class,
                () -> LabDetectionValidation.validateQuarantineStationIdRequired(null));
        BizLabDetection bad = LabDetectionTestSamples.sample();
        bad.setQuarantineStationId(null);
        assertThrows(ServiceException.class, () -> LabDetectionValidation.validateRowForSave(bad));
    }

    @Test
    @DisplayName("合法行校验")
    void validRow()
    {
        assertDoesNotThrow(() -> LabDetectionValidation.validateRowForSave(LabDetectionTestSamples.sample()));
        BizLabDetection bad = LabDetectionTestSamples.sample();
        bad.setSampleSource(9);
        assertThrows(ServiceException.class, () -> LabDetectionValidation.validateRowForSave(bad));
    }
}
