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 installLocation()
    {
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateInstallLocation(""));
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateInstallLocation(longStr(11)));
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateInstallLocation("1号栏"));
    }

    @Test
    @DisplayName("IPv4")
    void ipAddress()
    {
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateIpAddress("999.1.1.1"));
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateIpAddress("abc"));
        assertThrows(ServiceException.class, () -> LivestockMonitorValidation.validateIpAddress(longStr(21)));
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateIpAddress("192.168.1.1"));
    }

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

    @Test
    @DisplayName("合法行")
    void rowForSave()
    {
        assertDoesNotThrow(() -> LivestockMonitorValidation.validateRowForSave(
                LivestockMonitorTestSamples.samplePastureMonitor()));
    }

    @Test
    @DisplayName("缺主体ID")
    void ownerIdRequired()
    {
        BizLivestockMonitor row = LivestockMonitorTestSamples.samplePastureMonitor();
        row.setOwnerId(null);
        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();
    }
}
