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

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("BizSupplierMapper.xml")
class BizSupplierMapperXmlTest
{

    @Test
    @DisplayName("编号精确筛选")
    void exactCodeFilter() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("supplier_code = #{supplierCode}"));
        assertTrue(xml.contains("supplier_name = #{supplierName}"));
    }

    @Test
    @DisplayName("列表排序与编号锁")
    void orderAndForUpdate() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("order by supplier_code asc, id asc"));
        assertTrue(xml.contains("select max(supplier_code) from biz_supplier for update"));
    }

    @Test
    @DisplayName("查询含分配账号字段")
    void selectVoIncludesAccountAssign() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("account_assigned, sys_user_id, assigned_login_name"));
    }

    private static String readMapperXml()
    {
        try (InputStream in = BizSupplierMapperXmlTest.class.getClassLoader()
                .getResourceAsStream("mapper/trading/BizSupplierMapper.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);
        }
    }
}
