Jmeter 校驗response報文格式是否為JSON數(shù)據(jù)

Jmeter 接口測試中,通常要校驗請求報文返回的結果,對于返回的結果格式,有的是xml,有的是json。這里對返回報文是否為json格式進行校驗。

以下是接口返回報文:

{
    "accidentNo": "AN1111",
    "auditReport": {
        "auditRuleTriggers": [{
            "actualValue": "    次數(shù):\n            137\n",
            "auditScore": 10,
            "itemInfoList": [],
            "itemName": "本碰撞點:1",
            "redLineType": "00",
            "ruleName": "歷史出險碰撞點相同",
            "ruleNo": "A102010046",
            "ruleType": "02",
            "ruleValue": "    次數(shù):\n        6\n"
        },
        {
            "actualValue": "        24.04\n",
            "auditScore": 20,
            "itemInfoList": [{
                "claimItemUniqueId": null,
                "itemName": "xxxx柵",
                "itemType": "01",
                "materialFeeAfterDiscount": null,
                "quantity": 1,
                "removeLaborFeeDiscount": 24.04,
            }],
            "itemName": "xxxx柵\n",
            "redLineType": "00",
            "ruleName": "單項維修金額超過系統(tǒng)價",
            "ruleNo": "A101050009",
            "ruleType": "01",
            "ruleValue": "        24.00\n"
        }]
    },
    "claimUniqueId": "CN070210",
    "interfaceCode": "ClaimPush",
    "message": "success",
    "resultCode": "000"
}

校驗上述報文格式,詳細步驟:
1、使用在線json schema生成工具自動生成上述報文的schema
地址:https://jsonschema.net/。將生成的內容保存為responseSchema.json文件,原返回結果保存為auditReport.json

{
  "type": "object", 
  "definitions": {}, 
  "properties": {
    "accidentNo": {
      "type": "string"
    }, 
    "auditReport": {
      "type": "object", 
      "properties": {
        "auditRuleTriggers": {
          "type": "array", 
          "items": {
            "type": "object", 
            "properties": {
              "actualValue": {
                "type": "string"
              }, 
              "auditScore": {
                "type": "integer"
              }, 
              "itemInfoList": {
                "type": "array"
              }, 
              "itemName": {
                "type": "string"
              }, 
              "redLineType": {
                "type": "string"
              }, 
              "ruleName": {
                "type": "string"
              }, 
              "ruleNo": {
                "type": "string"
              }, 
              "ruleType": {
                "type": "string"
              }, 
              "ruleValue": {
                "type": "string"
              }
            }
          }
        }
      }
    }, 
    "claimUniqueId": {
      "type": "string"
    }, 
    "interfaceCode": {
      "type": "string"
    }, 
    "message": {
      "type": "string"
    }, 
    "resultCode": {
      "type": "string"
    }
  }
}

2、新建maven工程,將上述兩個文件放置main錄的resources下,并導入包:json-schema-validator、fastjson

 <!-- 用于校驗json報文格式-->
      <dependency>
          <groupId>com.github.fge</groupId>
          <artifactId>json-schema-validator</artifactId>
          <version>2.2.6</version>
      </dependency>
     <!-- 用于json報文的轉換-->
     <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>1.2.40</version>
     </dependency>

3、新增類 JsonSchemaValidator,代碼如下

package com.sc;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import java.io.IOException;


public class JsonSchemaValidator {
    private final static JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    public static ProcessingReport validatorSchema(String response) throws IOException, ProcessingException {
       JsonSchema jsonSchema = factory.getJsonSchema(JsonLoader.fromResource("/responseSchema.json"));
       JsonNode jsonNode = JsonLoader.fromString(response);
       ProcessingReport report = jsonSchema.validate(jsonNode);
       return  report;
    }
}

 /**
     * 在解析響應前,使用JsonSchema對響應的格式進行校驗,如果校驗失敗,則直接返回 fail
     *
     * @return 校驗結果
     */
public  static boolean checkJsonFormat() throws IOException, ProcessingException {
        ProcessingReport report = JsonSchemaValidator.validatorSchema(response);
        return  report.isSuccess();
    }

說明:
1)、方法validatorSchema
先通過JsonSchemaFactory.byDefault()獲取到JsonSchemaFactory
加載JsonSchema,即模板
將接口的返回結果轉換成JsonNode
最后使用加載的JsonSchema校驗JsonNode

2)、方法checkJsonFormat
使用validatorSchema的返回結果,調用ProcessingReport 的isSuccess方法,返回校驗結果

4、新增測試類,校驗auditReport.json格式

public class Test{
    public static void main(String[] args) throws IOException, ProcessingException {
        String response = FileUtils.readFileToString(new File("./src/main/resources/auditReport.json"),"utf-8");
        JsonResponse jsonResponse = new JsonResponse(response);
        boolean flag = JsonSchemaValidator.checkJsonFormat(response);
        if(flag){
            System.out.println("響應結果格式符合要求;");
        }else{
            System.out.println("響應結果格式不符合要求??!;");
        }
}
}

運行,結果


image.png

ok,準備工作完成。將上述工程打包,引入到jmeter,直接在beanshell中調用上述方法即可。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong閱讀 22,942評論 1 92
  • 國家電網(wǎng)公司企業(yè)標準(Q/GDW)- 面向對象的用電信息數(shù)據(jù)交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 12,420評論 6 13
  • “大海好黑啊” “也不是,天亮后便會很美的” 突然想起星爺《喜劇之王》中的這句臺詞,這是男女主角坐在漆黑的海邊,看...
    左行右可閱讀 239評論 0 1
  • 2017年4月9日今天分享一個奇跡,我一直想看看櫻花,前兩年去青島沒趕上看櫻花的花期,我這兩天盤算著再去一次青島呢...
    傳奇姐姐閱讀 208評論 0 0

友情鏈接更多精彩內容