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.CrlVerifyRequest;
import cn.unitid.matrix.pkiservice.sdk.svs.response.CrlVerifyResponse;
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/24 13:07
 * <p>
 * 描述信息： 证书校验
 */
public class CrlTest extends AbstractJavaSamplerClient {

    @Override
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
        arguments.addArgument("certBase64", "");
        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 = "60a1bec51c3fe02776fefa98";
            String appSecret = "VIbMZ5tfqoR3Kg8wJJsaTxCGtv7P8z1L";
            PkiClient pkiClient = new DefaultPkiClient(url, appKey, appSecret);

            String certBase64 = context.getParameter("certBase64");
            String ou = context.getParameter("ou");

            CrlVerifyRequest request = new CrlVerifyRequest();
            request.setCert(certBase64);

            CrlVerifyResponse 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;
    }

}
