package com.ylx.system.mapper;

import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

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

public class SysDeptMapperXmlTest {

    @Test
    public void selectCityListReturnsDistinctEnabledCities() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("selectCityList"));
        assertTrue(xml.contains("select distinct"));
        assertTrue(xml.contains("d.city_code as cityCode"));
        assertTrue(xml.contains("d.city_name as cityName"));
        assertTrue(xml.contains("from sys_dept d"));
        assertTrue(xml.contains("d.is_delete = 0"));
        assertTrue(xml.contains("d.status = '0'"));
        assertTrue(xml.contains("d.city_code is not null"));
        assertTrue(xml.contains("d.city_code != ''"));
        assertTrue(xml.contains("d.city_name is not null"));
        assertTrue(xml.contains("d.city_name != ''"));
        assertTrue(xml.contains("order by d.city_code, d.city_name"));
    }

    private String readMapperXml() throws Exception {
        try (InputStream inputStream = getClass().getClassLoader()
                .getResourceAsStream("mapper/system/SysDeptMapper.xml")) {
            assertNotNull(inputStream, "SysDeptMapper.xml should exist in test classpath");
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            return new String(bytes, StandardCharsets.UTF_8);
        }
    }
}
