package com.ruoyi.ccdi.project.mapper;

import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

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

class CcdiBankTagTaskMapperXmlTest {

    private static final String RESOURCE = "mapper/ccdi/project/CcdiBankTagTaskMapper.xml";

    @Test
    void mapper_shouldExposeLatestFailedTaskQuery() {
        Method method = Arrays.stream(CcdiBankTagTaskMapper.class.getDeclaredMethods())
            .filter(item -> "selectLatestFailedTaskByProjectId".equals(item.getName()))
            .findFirst()
            .orElse(null);

        assertNotNull(method, "应提供查询项目最近失败打标任务的方法");
    }

    @Test
    void xml_shouldSelectLatestFailedTaskByProjectId() throws Exception {
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(RESOURCE)) {
            String xml = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);

            assertTrue(xml.contains("selectLatestFailedTaskByProjectId"), xml);
            assertTrue(xml.contains("from ccdi_bank_tag_task"), xml);
            assertTrue(xml.contains("project_id = #{projectId}"), xml);
            assertTrue(xml.contains("status = 'FAILED'"), xml);
            assertTrue(xml.contains("order by id desc"), xml);
            assertTrue(xml.contains("limit 1"), xml);
        }
    }
}
