package com.cpop.oam.web;

import cn.hutool.core.util.IdUtil;
import com.cpop.core.base.entity.LoginUser;
import com.cpop.core.base.enums.OperationLogEnum;
import com.cpop.core.base.enums.UserType;
import com.cpop.core.service.CoreService;
import com.cpop.core.utils.PasswordEncoder;
import com.cpop.core.utils.RsaUtils;
import com.cpop.core.utils.SpringUtils;
import com.cpop.core.utils.uuid.IdUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Map;

/**
 * @author DB
 * @Description: RockBlade核心功能测试
 * @create 2023-08-27 11:11
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CpopCoreTests {

    @Autowired
    private CoreService coreService;

    @Autowired
    private RsaUtils rsaUtils;

    /**
     * @Description: 添加操作日志
     * @param
     * @return
     * @Author DB
     * @Date: 2023/8/27 11:11
     */
    @Test
    public void insertOperationLog() {
        LoginUser loginUser = new LoginUser();
        loginUser.setIpAddr("localhost")
                .setUserId("1")
                .setUserName("RockBlade");
        coreService.insertOperationLog(200, OperationLogEnum.SYSTEM_LOGIN, loginUser, null);
    }

    /**
     * @Description: 根据用户名获取用户信息
     * @param
     * @return
     * @Author DB
     * @Date: 2023/8/27 23:46
     */
    @Test
    public void loadUserByUsername() {
        System.out.println(coreService.getSysUserByUsername("RockBlade", UserType.OAM_USER));
    }

    /**
     * 初始化密钥对
     */
    @Test
    public void initKeyPair() {
        Map<String, String> keyPairMap = rsaUtils.initKeyPair();
        String publicKey = keyPairMap.get("publicKey");
        String privateKey = keyPairMap.get("privateKey");
        System.out.println("公钥：");
        System.out.println(publicKey);
        System.out.println();
        System.out.println("私钥：");
        System.out.println(privateKey);
        System.out.println();
        String source = "jambox5772";
        System.out.println("待加密字符串："+source);
        System.out.println();
        String strEncrypt = rsaUtils.encrypt(source);
        System.out.println("加密后的字符串：");
        System.out.println(strEncrypt);
        System.out.println();
        String strDecrypt = rsaUtils.decrypt(strEncrypt);
        System.out.println("解密后的字符串：");
        System.out.println(strDecrypt);
        //加密
        String encode = SpringUtils.getBean(PasswordEncoder.class).encode("Admin@123");
        System.out.println("encode加密："+encode);
    }

    /**
     * 初始化密钥对
     */
    @Test
    public void initPublicOrPrivateKey() {
        String snowflakeNextIdStr = IdUtil.getSnowflakeNextIdStr();
        String baseKeyRepository = "E:\\KeyRepository\\";
        String publicKeyFile = baseKeyRepository + "publicKey" + snowflakeNextIdStr;
        String privateKeyFile = baseKeyRepository + "privateKey" + snowflakeNextIdStr;
        Map<String, String> keyPairMap = rsaUtils.initKeyPair(publicKeyFile, privateKeyFile);
        String publicKey = keyPairMap.get("publicKey");
        String privateKey = keyPairMap.get("privateKey");
        System.out.println("公钥：");
        System.out.println(publicKey);
        System.out.println();
        System.out.println("私钥：");
        System.out.println(privateKey);
        System.out.println();
        String source = "Admin@123";
        System.out.println("待加密字符串：" + source);
        System.out.println();
        String strEncrypt = rsaUtils.encrypt(source);
        System.out.println("加密后的字符串：");
        System.out.println(strEncrypt);
        System.out.println();
        String strDecrypt = rsaUtils.decrypt(strEncrypt);
        System.out.println("解密后的字符串：");
        System.out.println(strDecrypt);
        //加密
        String encode = SpringUtils.getBean(PasswordEncoder.class).encode("Admin@123");
        System.out.println("encode加密：" + encode);
    }
}
