package cn.unitid.matrix.pkiservice.test.svs.jmetertest;

import cn.unitid.matrix.pkiservice.sdk.DefaultPkiClient;
import cn.unitid.matrix.pkiservice.sdk.PkiClient;
import cn.unitid.matrix.pkiservice.sdk.consts.CertSearchEnum;
import cn.unitid.matrix.pkiservice.sdk.consts.DataType;
import cn.unitid.matrix.pkiservice.sdk.consts.SignatureAlgorithm;
import cn.unitid.matrix.pkiservice.sdk.svs.request.P1VerifyRequest;
import cn.unitid.matrix.pkiservice.sdk.svs.response.P1VerifyResponse;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

/**
 * 作者： 徐霁
 * <p>
 * 时间： 2021/5/21 14:47
 * <p>
 * 描述信息： P1签名测试
 */
public class P1SignVerifyTest extends AbstractJavaSamplerClient {

    @Override
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
        arguments.addArgument("certBase64", "");
        arguments.addArgument("original", "");
        arguments.addArgument("signature", "");
        return arguments;
    }

    /**
     * 测试执行的循环体，根据线程数和循环次数的不同可执行多次
     * @param context
     * @return
     */
    @Override
    public SampleResult runTest(JavaSamplerContext context) {
        SampleResult sampleResult = new SampleResult();

        String url = "http://127.0.0.1:20402/openapi/svs/v1";
        String appKey = "60a1d401bb8c707040029624";
        String appSecret = "w4ouV1Vw24HMpDJyCWs40qMPaNS7WL4V";
        PkiClient pkiClient = new DefaultPkiClient(url, appKey, appSecret);
        String certBase64 = context.getParameter("certBase64");
        String original = context.getParameter("original");
        String signature = context.getParameter("signature");
        String certId = context.getParameter("certId");

        // 计时开始
        sampleResult.sampleStart();

        //测试方法
        try {
            P1VerifyRequest request = new P1VerifyRequest();
            request.setSignType(CertSearchEnum.CERT_ID);
            //签名项目的值
            request.setSignItemValue(certId);
            //签名项目的值
            request.setDataType(DataType.BASE64);
            // 签名数据
            request.setData(original);
            // 签名值
            request.setSignature(signature);
            // 摘要算法，SM2证书签名固定为SM3，RSA默认SHA256
            request.setSignatureAlgorithm(SignatureAlgorithm.SHA1withRSA);
            // 是否返回证书
            request.setReturnCert(true);

            P1VerifyResponse response = pkiClient.execute(request);
            sampleResult.setRequestHeaders("url:" + url);
            sampleResult.setResponseData("result:" + response.getBody(), null);
            sampleResult.setSuccessful(response.isSuccess() && response.getResult() != null && response.getResult().getOriginal() != null);
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            e.printStackTrace();
        } finally {
            sampleResult.sampleEnd();
        }
        return sampleResult;
    }

}
