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

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

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.video.LivestockMonitorTestSamples;
import com.ruoyi.web.modules.video.domain.BizLivestockMonitor;

@DisplayName("LivestockMonitorValidation")
class LivestockMonitorValidationTest
{

    @Test
    @DisplayName("监控名称")
    void monitorName()
    {
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateMonitorName(""));
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateMonitorName("   "));
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateMonitorName(longStr(11)));
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateMonitorName("圈舍监控1"));
    }

    @Test
    @DisplayName("安装位置可选")
    void installLocationOptional()
    {
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateInstallLocationIfPresent(null));
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateInstallLocationIfPresent(""));
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateInstallLocationIfPresent(longStr(11)));
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateInstallLocationIfPresent("1号栏"));
    }

    @Test
    @DisplayName("主体类型")
    void ownerType()
    {
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateOwnerTypeIfPresent(0));
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateOwnerTypeIfPresent(
                LivestockMonitorRules.OWNER_TYPE_PASTURE));
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateOwnerTypeIfPresent(null));
    }

    @Test
    @DisplayName("视频来源")
    void videoSource()
    {
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateVideoSource(""));
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateVideoSource("invalid"));
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateVideoSource(
                LivestockMonitorRules.VIDEO_SOURCE_WVP));
    }

    @Test
    @DisplayName("摄像头ID")
    void monitorId()
    {
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateMonitorId(""));
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateMonitorId("50001"));
    }

    @Test
    @DisplayName("完整行")
    void rowForSaveWithOwner()
    {
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateRowForSave(
                LivestockMonitorTestSamples.samplePastureMonitor()));
    }

    @Test
    @DisplayName("仅监控名称")
    void rowForSaveMinimal()
    {
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateRowForSave(
                LivestockMonitorTestSamples.sampleMinimal()));
    }

    @Test
    @DisplayName("主体信息不完整")
    void ownerIncomplete()
    {
        BizLivestockMonitor row = LivestockMonitorTestSamples.sampleMinimal();
        row.setOwnerType(LivestockMonitorRules.OWNER_TYPE_PASTURE);
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateRowForSave(row));
    }

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