package com.bonus.common.core.utils.poi;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ExcelUtilTests {
    @Test
    public void testImportExcel() {
        String currentPath = System.getProperty("user.dir");
        System.out.println("当前执行路径: " + currentPath);
        ExcelUtil<Person> excelUtil = new ExcelUtil<Person>(Person.class);
        currentPath +="/src/test/java/com/bonus/common/core/utils/poi/test.xlsx";
        try (FileInputStream fileInputStream = new FileInputStream(currentPath)) {
            List<Person> persionList = excelUtil.importExcel(fileInputStream);
            assertEquals(4, persionList.size());
            String sex = persionList.get(0).getSex();
            assertEquals("1", sex);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testExportExcel() {
        String currentPath = System.getProperty("user.dir");
        ExcelUtil<Person> excelUtil = new ExcelUtil<Person>(Person.class);
        Person p1 = new Person("admin",10,"0");
        Person p2 = new Person("admin2",20,"1");
        List <Person> personList = new ArrayList<>();
        personList.add(p1);
        personList.add(p2);
        currentPath +="/src/test/java/com/bonus/common/core/utils/poi/testExport.xlsx";
        excelUtil.exportExcelToLocalFile(currentPath,personList, "sheet1", null);
    }

    @Test
    public void testExportExcelWithImportErrorMessage() {
        String currentPath = System.getProperty("user.dir");
        System.out.println("当前执行路径: " + currentPath);
        ExcelUtil<Person> excelUtil = new ExcelUtil<Person>(Person.class);
        Person p1 = new Person("admin",10,"0");
        p1.setComments("用户名存在");
        Person p2 = new Person("admin2",20,"1");
        p2.setComments("数据格式不正确");
        List <Person> personList = new ArrayList<>();
        personList.add(p1);
        personList.add(p2);
        currentPath +="/src/test/java/com/bonus/common/core/utils/poi/testExportWithImportErrorMsg.xlsx";
        excelUtil.exportExcelToLocalFile(currentPath,personList, "sheet1", null);

    }

}
