package com.ylx.massage.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 MomentMediaMapperXmlTest {

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

        assertTrue(xml.contains("selectMediaListByMomentId"));
        assertTrue(xml.contains("moment_id = #{momentId}"));
        assertTrue(xml.contains("AND IFNULL(is_delete, 0) = 0"));
    }

    private String readMapperXml() throws Exception {
        InputStream inputStream = getClass().getClassLoader()
                .getResourceAsStream("mapper/moment/MomentMediaMapper.xml");
        assertNotNull(inputStream, "MomentMediaMapper.xml should exist");
        byte[] bytes = new byte[inputStream.available()];
        int read = inputStream.read(bytes);
        assertTrue(read > 0, "MomentMediaMapper.xml should not be empty");
        return new String(bytes, StandardCharsets.UTF_8);
    }
}
