package com.ssyf.test.helper;

import cn.hutool.json.JSONUtil;
import com.ssyf.common.security.domain.Certificate;
import com.ssyf.common.security.domain.ClientInfo;
import com.ssyf.common.security.helper.CertificateHelper;
import com.ssyf.common.security.helper.LoginHelper;
import com.ssyf.common.web.result.Result;
import com.ssyf.common.web.result.ResultHelper;
import org.springframework.test.web.servlet.MockMvc;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

/**
 * 测试帮助类
 * @author huwei
 * @date 2024/2/23 14:48
 */
public class TestHelper {

    /**
     * 转换为结果
     * @param string
     * @return
     */
    public static Result toResult(String string){
        return JSONUtil.toBean(string, Result.class);
    }

    /**
     * 断言返回值成功
     * @param responseString
     */
    public static void assertResultSuccess(String responseString){
        assertResultSuccess(toResult(responseString));
    }

    /**
     * 断言返回值成功
     * @param result
     */
    public static void assertResultSuccess(Result result){
        assertTrue(ResultHelper.isSuccess(result));
    }

    /**
     * 断言返回值失败
     * @param responseString
     */
    public static void assertResultFail(String responseString){
        assertResultFail(toResult(responseString));
    }

    /**
     * 断言返回值失败
     * @param result
     */
    public static void assertResultFail(Result result){
        assertFalse(ResultHelper.isSuccess(result));
    }

    /**
     * 根据给定值，断言结果
     * @param isSuccess
     * @param responseString
     */
    public static void assertResult(boolean isSuccess ,String responseString){
        assertResult(isSuccess ,JSONUtil.toBean(responseString, Result.class));
    }

    /**
     * 根据给定值，断言结果
     * @param isSuccess
     * @param result
     */
    public static void assertResult(boolean isSuccess ,Result result){
        if(isSuccess){
            assertResultSuccess(result);
        }else{
            assertResultFail(result);
        }
    }

    /**
     * 获取游客token
     * @param mockMvc
     * @return
     * @throws Exception
     */
    public static String getVisitorToken(MockMvc mockMvc) throws Exception {
        return getVisitorToken("/" ,mockMvc);
    }

    /**
     * 获取游客token
     * @param uri
     * @param mockMvc
     * @return
     * @throws Exception
     */
    public static String getVisitorToken(String uri , MockMvc mockMvc) throws Exception {
        return mockMvc.perform(post(uri)).andReturn().getResponse().getHeader("Authorization");
    }

    /**
     * 模拟证书
     * @param type
     * @param content
     * @param token
     * @return
     */
    public static String mockCertificate(String type ,String content ,String token){
        return mockCertificate(new Certificate(type ,null ,content) ,token);
    }


    /**
     * 模拟证书
     * @param type
     * @param event
     * @param content
     * @param token
     * @return
     */
    public static String mockCertificate(String type ,String event,String content ,String token){
        return mockCertificate(new Certificate(type ,event ,content) ,token);
    }

    /**
     * 模拟证书
     * @param certificate
     * @param token
     * @return
     */
    public static String mockCertificate(Certificate certificate ,String token){
        ClientInfo clientInfo = LoginHelper.parseAndGet(token);
        return CertificateHelper.generate(clientInfo.getDys().getBytes() ,certificate);
    }


}
