package com.ruoyi.system.mapper;

import com.ruoyi.system.domain.PosStore;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

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

class PosStoreMapperXmlTest {

    @Test
    void storeDetailMapsServerTypeFromDatabaseColumn() throws IOException {
        Configuration configuration = mapperConfiguration();

        ResultMapping serverTypeMapping = configuration
                .getResultMap("com.ruoyi.system.mapper.PosStoreMapper.PosStoreResult")
                .getResultMappings()
                .stream()
                .filter(mapping -> "serverType".equals(mapping.getProperty()))
                .findFirst()
                .orElseThrow();

        assertEquals("server_type", serverTypeMapping.getColumn());
    }

    private Configuration mapperConfiguration() throws IOException {
        Configuration configuration = new Configuration();
        configuration.getTypeAliasRegistry().registerAlias("PosStore", PosStore.class);
        String resource = "mapper/chanting/PosStoreMapper.xml";
        try (InputStream input = getClass().getClassLoader().getResourceAsStream(resource)) {
            assertNotNull(input);
            new XMLMapperBuilder(input, configuration, resource, configuration.getSqlFragments()).parse();
        }
        return configuration;
    }
}
