package ${basePackage};

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.AESUtil;
import ${basePackage}.frame.utils.Base64Util;
import ${basePackage}.frame.utils.IDgenerator;
import ${basePackage}.frame.utils.MD5Util;
import ${basePackage}.frame.utils.MapperUtil;
import ${basePackage}.frame.utils.ProcessUtil;
import ${basePackage}.frame.utils.RSAUtil;
import ${basePackage}.frame.utils.ZipUtil;
import ${basePackage}.module.system.ent.Dept;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * UtilTest -  - 测试用例
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class UtilTest {

    @Test
    public void testGeneratePwd() {
        System.out.print("[123456]加密=");
        System.out.println(MD5Util.generatePwd("123456"));
    }

    @Test
    public void testIDgenerator() {
        for (int i = 0; i < 10; i++) {
            long l = IDgenerator.nextId();
            System.out.println(l);
        }
    }

    @Test
    public void testAESUtil() {
        // 加密
        String data = "我有一个苹果";
        String secret = "1234567890123456";
        System.out.println("加密后的Base64密文是:" + AESUtil.encrypt2Base64(data.getBytes(), secret));

        // 解密
        String encrypt2Base64 = AESUtil.encrypt2Base64(data.getBytes(), secret);
        byte[] decrypt = AESUtil.decrypt(Base64Util.decode(encrypt2Base64), secret);
        System.out.println("解密后的明文是:" + new String(decrypt));
    }

    @Test
    public void testBase64Util() {
        String s = Base64Util.encodeToString(("我搜搜").getBytes());
        System.out.println(s);

        byte[] decode = Base64Util.decode(s);
        System.out.println(new String(decode));
    }

    @Test
    public void testMD5Util() {
        String encode = MD5Util.encode("123456");
        System.out.println(encode);
    }

    @Test
    public void testProcessUtil() {
        ProcessUtil.execExe("D:\\example.exe");
        ProcessUtil.execBat("D:\\example.bat");
    }

    @Test
    public void testRSAUtil() {
        {//创建秘钥对
            RSAUtil.createKey();
        }

        {//加解密

            //加密
            String encrypt = RSAUtil.encrypt2Base64("我有一个苹果".getBytes());
            System.out.println(encrypt);

            //解密
            String decrypt = RSAUtil.decrypt2String(encrypt);
            System.out.println(decrypt);
        }


        String sign = RSAUtil.sign2Base64("我有一个苹果".getBytes());
        System.out.println(sign);

        boolean b = RSAUtil.doCheck("我有一个苹果".getBytes(), sign);
        System.out.println(b);
    }

    @Test
    public void testZipUtil() {
        //解压
        ZipUtil.unZip(new File("E:\\AAA.zip"), new File("E:\\AAA"));
        //压缩
        ZipUtil.toZip(new File("E:\\AAA"), new File("E:\\AAA.zip"));
    }

    @Test
    public void testExcel() {
        // 准备导出数据
        Dept dept = new Dept();
        dept.setDeptCode("A000101");
        dept.setDeptName("人事部");
        dept.setDeptAlias("人事部");
        dept.setSupCode("A0001");
        dept.setSupName("董事会");
        dept.setValid(true);
        ArrayList<Dept> depts = new ArrayList<Dept>();
        depts.add(dept);

        // 设置文件地址及导出
        try {
            File file = new File("E:\\E.xlsx");
            new WExcel<Dept>(Dept.class).loadData(depts).toFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 导入数据
        try {
            File file = new File("E:\\E.xlsx");
            WExcel.Processor<Dept> processor = new WExcel.Processor<Dept>() {
                @Override
                public List<String> exec(Dept o) {
                    System.out.println("从Excel中读取对象:" + MapperUtil.toJson(o));
                    return Arrays.asList("模拟处理对象业务时出现的错误信息1","模拟处理对象业务时出现的错误信息2");
                }
            };
            WExcel check = new WExcel<Dept>(Dept.class).loadData(file, processor);
            if (check.hasError()) {
                System.out.println("导入或处理有错误.");
                check.toFile(new File("E:\\E_err.xlsx"));
            }
        } catch (TemplateNotMatchException e) {
            Assert.assertTrue("导入文件与模板不匹配，请重新下载模板！", false);
        } catch (ReadErrorException e) {
            Assert.assertTrue("导入文件错误，请检查格式或文件是否被破坏！", false);
        } catch (IOException e) {
            Assert.assertTrue("读取错误！", false);
        }
    }
}
