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

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

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("BizLivestockMonitorMapper.xml")
class BizLivestockMonitorMapperXmlTest
{

    @Test
    @DisplayName("列表不含 IP、逻辑删除与筛选排序")
    void listSql()
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("selectListVo"));
        assertTrue(xml.contains("del_flag = '0'"));
        assertTrue(xml.contains("monitor_name like concat('%', #{monitorName}, '%')"));
        assertTrue(xml.contains("owner_type = #{ownerType}"));
        assertTrue(xml.contains("order by create_time desc, id desc"));
        int listVoStart = xml.indexOf("<sql id=\"selectListVo\">");
        int listVoEnd = xml.indexOf("</sql>", listVoStart);
        String listVo = xml.substring(listVoStart, listVoEnd);
        assertFalse(listVo.contains("ip_address"));
    }

    @Test
    @DisplayName("详情含 IP、逻辑删除")
    void detailAndDeleteSql()
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("selectVo"));
        assertTrue(xml.contains("ip_address"));
        assertTrue(xml.contains("del_flag = '2'"));
        assertTrue(xml.contains("logicDeleteBizLivestockMonitorByIds"));
    }

    private static String readMapperXml()
    {
        try (InputStream in = BizLivestockMonitorMapperXmlTest.class.getClassLoader()
                .getResourceAsStream("mapper/video/BizLivestockMonitorMapper.xml"))
        {
            if (in == null)
            {
                throw new IllegalStateException("mapper xml not found");
            }
            return IOUtils.toString(in, StandardCharsets.UTF_8);
        }
        catch (Exception ex)
        {
            throw new RuntimeException(ex);
        }
    }
}
