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.svs.request.CertTemplateVerifyRequest;
import cn.unitid.matrix.pkiservice.sdk.svs.response.CertTemplateVerifyResponse;
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;


/**
 * @Author 南京壹证通信息科技有限公司 - PKI服务项目组
 * @date： 2021/6/2 14:29
 * @description： 模板校验证书
 */
public class TemplateVerifyTest extends AbstractJavaSamplerClient {

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

    @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);

            CertTemplateVerifyRequest request = new CertTemplateVerifyRequest();

            String cert = context.getParameter("cert", null);
            String templateId = context.getParameter("templateId", null);

            request.setCert(cert);
            request.setTemplateId(templateId);

            CertTemplateVerifyResponse response = pkiClient.execute(request);

            //将结果写入结果树：在jmeter的监听器-查看结果树时即可查看返回结果
            sampleResult.setRequestHeaders("url:" + url);
            sampleResult.setResponseData("result:" + response.getBody(), null);
            sampleResult.setSuccessful(response.isSuccess() && response.getResult() != null);
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            e.printStackTrace();
        } finally {
            sampleResult.sampleEnd();
        }
        return sampleResult;
    }

}
