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.DataType;
import cn.unitid.matrix.pkiservice.sdk.svs.request.P7VerifyRequest;
import cn.unitid.matrix.pkiservice.sdk.svs.response.P7VerifyResponse;
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 P7SignVerifyTest extends AbstractJavaSamplerClient {

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

    /**
     * 测试执行的循环体，根据线程数和循环次数的不同可执行多次
     * @param context
     * @return
     */
    @Override
    public SampleResult runTest(JavaSamplerContext context) {
        SampleResult sampleResult = new SampleResult();
        sampleResult.sampleStart();
        try {
            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 original = context.getParameter("original");
            String signature = context.getParameter("signature");

            P7VerifyRequest request = new P7VerifyRequest();
            // 签名值
            request.setSignature(signature);
            // 签名数据
            request.setData(original);
            //签名项目的值
            request.setDataType(DataType.ORIGINAL);
            // 签名是否包含原文
            request.setAttach(false);
            // 是否返回证书
            request.setReturnCert(true);

            P7VerifyResponse response = pkiClient.execute(request);

            //将结果写入结果树：在jmeter的监听器-查看结果树时即可查看返回结果
            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;
    }

}
