JMeter自定义Java请求

public class Cnppurchase extends AbstractJavaSamplerClient {

    private final Logger logger = LoggingManager.getLoggerForClass();

    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    @Override

    public void setupTest(JavaSamplerContext context) {

    }

    @Override

    public Arguments getDefaultParameters() {

        Arguments arguments = new Arguments();

        arguments.addArgument("pan", "9558821001002575556");

        arguments.addArgument("merchantId","812333345110001");

        arguments.addArgument("cardHolderId","640102198102080616");

        arguments.addArgument("cardHolderName","ZhangSan");

        arguments.addArgument("amount","25.31");

        arguments.addArgument("terminalId","00007777");

        arguments.addArgument("customerId","123456");

        arguments.addArgument("storableCardNo","6226380095");

        arguments.addArgument("mgw_url","https://vpos.99bill.com/cnp/purchase");

        return arguments;

    }

    @Override

    public SampleResult runTest(JavaSamplerContext context) {

        String merchantId = context.getParameter("merchantId");//"812333345110001";//st1

        String pan = context.getParameter("pan");

        String storableCardNo = context.getParameter("storableCardNo");

        String amount = context.getParameter("amount");

        String terminalId = context.getParameter("terminalId");

        String customerId = context.getParameter("customerId");

        String mgw_url = context.getParameter("mgw_url");

        String cardHolderId = context.getParameter("cardHolderId");

        String cardHolderName = context.getParameter("cardHolderName");

        SampleResult sampleResult = new SampleResult();

        sampleResult.sampleStart();

        boolean success = false;

        String externalRefNumber = UUID.randomUUID().toString().replace("-", "");

        logger.debug("externalRefNumber is: " + externalRefNumber);

        String xmlstr = buildRequestBodyWithCNP(merchantId,externalRefNumber,terminalId,

                pan,amount,storableCardNo,customerId,cardHolderName,cardHolderId).toString();

        InputStream inputStream = HttpsRequestUtil.requestWithPost(xmlstr, merchantId, mgw_url);

        String response = HttpsRequestUtil.convertStreamToString(inputStream);

        logger.info("Response Message is: " + response);

        if(null != response && !"".equals(response)){

            if (response.contains("<responseCode>00</responseCode>")) {

                success = true;

            }

        }

        sampleResult.setResponseMessage(response);

        sampleResult.setSuccessful(success);

        sampleResult.sampleEnd();

        long costTime = sampleResult.getEndTime() - sampleResult.getStartTime();

        if( costTime > 1200){

            logger.error("The trade cost: " + costTime + "; tradeId is: " + externalRefNumber);

        }

        return sampleResult;

    }

    @Override

    public void teardownTest(JavaSamplerContext context) {

    }

    public StringBuffer buildRequestBodyWithCNP(String merchantId,String externalRefNumber,String terminalId,

                                                String pan,String amount,String storableCardNo,String customerId,

                                                String cardHolderName,String cardHolderId){

        StringBuffer buffer = new StringBuffer();

        buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");

        buffer.append("<MasMessage xmlns=\"http://www.99bill.com/mas_cnp_merchant_interface\">");

        buffer.append("<version>1.0</version>");

        buffer.append("<TxnMsgContent>");

        buffer.append("<txnType>PUR</txnType>");

        buffer.append("<interactiveStatus>TR1</interactiveStatus>");

        buffer.append("<cardNo>"+ pan +"</cardNo>");

        buffer.append("<amount>"+amount +"</amount>");

        buffer.append("<termInMonths></termInMonths>");

        buffer.append("<merchantId>"+ merchantId +"</merchantId>");

        buffer.append("<terminalId>"+terminalId+"</terminalId>");

        buffer.append("<entryTime>"+simpleDateFormat.format(new Date())+"</entryTime>");

        buffer.append("<externalRefNumber>"+ externalRefNumber +"</externalRefNumber>");

        buffer.append("<customerId>"+customerId+"</customerId>");

        //buffer.append("<storableCardNo>"+ storableCardNo +"</storableCardNo>");

        //buffer.append("<cardHolderName>"+ cardHolderName +"</cardHolderName>");

        //buffer.append("<cardHolderId>"+ cardHolderId +"</cardHolderId>");

        buffer.append("<spFlag>QPay02</spFlag>");

        buffer.append("<idType>0</idType>");

        buffer.append("<extMap>");

        buffer.append("<extDate>");

        buffer.append("<key>phone</key>");

        buffer.append("<value>13401928538</value>");

        buffer.append("</extDate>");

        buffer.append("</extMap>");

        buffer.append("<orgPartyId></orgPartyId>");

        buffer.append("<rifleMap/>");

        buffer.append("<supCardFlag></supCardFlag>");

        buffer.append("</TxnMsgContent>");

        buffer.append("</MasMessage>");

        return buffer;

    }

    public static void main(String[] args) {

        Cnppurchase test = new Cnppurchase();

        Arguments arguments = test.getDefaultParameters();

        JavaSamplerContext context = new JavaSamplerContext(arguments);

        test.setupTest(context);

        test.runTest(context);

    }

}

posted @ 2021-08-12 10:54  up~up  阅读(108)  评论(0编辑  收藏  举报