【问题标题】:How to Validate JSON data by Using JSONSchema in jMeter [closed]如何在 jMeter 中使用 JSONSchema 验证 JSON 数据 [关闭]
【发布时间】:2016-12-13 12:00:18
【问题描述】:

对于我的请求,我收到了json 响应。所以,我想在jMeter 中使用JSONSchema 进行结构和数据类型验证。

我怎样才能以最好的方式实现这一目标?
jMeter 中是否有可用的插件来执行此类任务?

示例 :

响应 Json 是

{
  "id": "60D5B6D2-9607-4E23-91D0-14EB5EA806EA",
  "imp": [
    {
      "id": "1",
      "tagid": "759926",
      "banner": {
        "w": 300,
        "h": 250,
        "topframe": 1,
        "api": [
          5
        ]
      }
    }
  ]
}

JSONSchema 是

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "imp": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "tagid": {
            "type": "string"
          },
          "banner": {
            "type": "object",
            "properties": {
              "w": {
                "type": "integer",
                "minimim": 10,
                "maximum": 150
              },
              "h": {
                "type": "integer",
                "minimim": 10,
                "maximum": 150
              },
              "topframe": {
                "type": "integer",
                "minimim": 10,
                "maximum": 150
              },
              "api": {
                "type": "array",
                "items": {
                  "type": "integer"
                }
              }
            },
            "additionalProperties": false,
            "required": [
              "w",
              "h",
              "topframe",
              "api"
            ]
          }
        },
        "additionalProperties": false,
        "required": [
          "id",
          "tagid",
          "banner"
        ]
      }
    }
  },
  "additionalProperties": false,
  "required": [
    "id",
    "imp"
  ]
}

【问题讨论】:

    标签: json jmeter jsonschema jmeter-plugins


    【解决方案1】:

    没有针对模式验证 JSON 对象的内置支持,因此您必须执行一些脚本,如Validate JSON against Schema in Java 文章中所述。万一引用的链接失效,您将需要JMeter Classpath中的以下库

    activation-1.1.jar
    btf-1.2.jar
    guava-16.0.1.jar
    jackson-annotations-2.2.3.jar
    jackson-core-2.2.3.jar
    jackson-coreutils-1.8.jar
    jackson-databind-2.2.3.jar
    joda-time-2.3.jar
    jopt-simple-4.6.jar
    json-schema-core-1.2.5.jar
    json-schema-validator-2.2.6.jar
    jsr305-3.0.0.jar
    libphonenumber-6.2.jar
    mailapi-1.4.3.jar
    msg-simple-1.1.jar
    rhino-1.7R4.jar
    uri-template-0.9.jar
    

    获取它们的最快和最简单的方法是创建最小的pom.xml 文件,例如:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>foo</groupId>
        <artifactId>bar</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/com.github.fge/json-schema-validator -->
            <dependency>
                <groupId>com.github.fge</groupId>
                <artifactId>json-schema-validator</artifactId>
                <version>2.2.6</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.github.fge/jackson-coreutils -->
            <dependency>
                <groupId>com.github.fge</groupId>
                <artifactId>jackson-coreutils</artifactId>
                <version>1.8</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.github.fge/json-schema-core -->
            <dependency>
                <groupId>com.github.fge</groupId>
                <artifactId>json-schema-core</artifactId>
                <version>1.2.5</version>
            </dependency>
        </dependencies>
    
    
    </project>
    

    并执行mvn dependency:copy-dependencies 命令。

    之后你就可以使用类似的东西了:

    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.node.ObjectNode;
    import com.github.fge.jackson.JsonLoader;
    import com.github.fge.jsonschema.core.exceptions.ProcessingException;
    import com.github.fge.jsonschema.core.report.ProcessingMessage;
    import com.github.fge.jsonschema.core.report.ProcessingReport;
    import com.github.fge.jsonschema.main.JsonSchema;
    import com.github.fge.jsonschema.main.JsonSchemaFactory;
    
    
    public class ValidationUtils {
    
        public static final String JSON_V4_SCHEMA_IDENTIFIER = "http://json-schema.org/draft-04/schema#";
        public static final String JSON_SCHEMA_IDENTIFIER_ELEMENT = "$schema";
    
        public static JsonNode getJsonNode(String jsonText)
                throws IOException
        {
            return JsonLoader.fromString(jsonText);
        } // getJsonNode(text) ends
    
        public static JsonNode getJsonNode(File jsonFile)
                throws IOException
        {
            return JsonLoader.fromFile(jsonFile);
        } // getJsonNode(File) ends
    
        public static JsonNode getJsonNode(URL url)
                throws IOException
        {
            return JsonLoader.fromURL(url);
        } // getJsonNode(URL) ends
    
        public static JsonNode getJsonNodeFromResource(String resource)
                throws IOException
        {
            return JsonLoader.fromResource(resource);
        } // getJsonNode(Resource) ends
    
        public static JsonSchema getSchemaNode(String schemaText)
                throws IOException, ProcessingException
        {
            final JsonNode schemaNode = getJsonNode(schemaText);
            return _getSchemaNode(schemaNode);
        } // getSchemaNode(text) ends
    
        public static JsonSchema getSchemaNode(File schemaFile)
                throws IOException, ProcessingException
        {
            final JsonNode schemaNode = getJsonNode(schemaFile);
            return _getSchemaNode(schemaNode);
        } // getSchemaNode(File) ends
    
        public static JsonSchema getSchemaNode(URL schemaFile)
                throws IOException, ProcessingException
        {
            final JsonNode schemaNode = getJsonNode(schemaFile);
            return _getSchemaNode(schemaNode);
        } // getSchemaNode(URL) ends
    
        public static JsonSchema getSchemaNodeFromResource(String resource)
                throws IOException, ProcessingException
        {
            final JsonNode schemaNode = getJsonNodeFromResource(resource);
            return _getSchemaNode(schemaNode);
        } // getSchemaNode() ends
    
        public static void validateJson(JsonSchema jsonSchemaNode, JsonNode jsonNode)
                throws ProcessingException
        {
            ProcessingReport report = jsonSchemaNode.validate(jsonNode);
            if (!report.isSuccess()) {
                for (ProcessingMessage processingMessage : report) {
                    throw new ProcessingException(processingMessage);
                }
            }
        } // validateJson(Node) ends
    
        public static boolean isJsonValid(JsonSchema jsonSchemaNode, JsonNode jsonNode) throws ProcessingException
        {
            ProcessingReport report = jsonSchemaNode.validate(jsonNode);
            return report.isSuccess();
        } // validateJson(Node) ends
    
        public static boolean isJsonValid(String schemaText, String jsonText) throws ProcessingException, IOException
        {
            final JsonSchema schemaNode = getSchemaNode(schemaText);
            final JsonNode jsonNode = getJsonNode(jsonText);
            return isJsonValid(schemaNode, jsonNode);
        } // validateJson(Node) ends
    
        public static boolean isJsonValid(File schemaFile, File jsonFile) throws ProcessingException, IOException
        {
            final JsonSchema schemaNode = getSchemaNode(schemaFile);
            final JsonNode jsonNode = getJsonNode(jsonFile);
            return isJsonValid(schemaNode, jsonNode);
        } // validateJson(Node) ends
    
        public static boolean isJsonValid(URL schemaURL, URL jsonURL) throws ProcessingException, IOException
        {
            final JsonSchema schemaNode = getSchemaNode(schemaURL);
            final JsonNode jsonNode = getJsonNode(jsonURL);
            return isJsonValid(schemaNode, jsonNode);
        } // validateJson(Node) ends    
    
        public static void validateJson(String schemaText, String jsonText) throws IOException, ProcessingException{
            final JsonSchema schemaNode = getSchemaNode(schemaText);
            final JsonNode jsonNode = getJsonNode(jsonText);
            validateJson(schemaNode, jsonNode);
        } // validateJson(text) ends
    
        public static void validateJson(File schemaFile, File jsonFile) throws IOException, ProcessingException{
            final JsonSchema schemaNode = getSchemaNode(schemaFile);
            final JsonNode jsonNode = getJsonNode(jsonFile);
            validateJson(schemaNode, jsonNode);
        } // validateJson(File) ends
    
        public static void validateJson(URL schemaDocument, URL jsonDocument) throws IOException, ProcessingException{
            final JsonSchema schemaNode = getSchemaNode(schemaDocument);
            final JsonNode jsonNode = getJsonNode(jsonDocument);
            validateJson(schemaNode, jsonNode);
        } // validateJson(URL) ends
    
        public static void validateJsonResource(String schemaResource, String jsonResource) throws IOException, ProcessingException{
            final JsonSchema schemaNode = getSchemaNode(schemaResource);
            final JsonNode jsonNode = getJsonNodeFromResource(jsonResource);
            validateJson(schemaNode, jsonNode);
        } // validateJsonResource() ends
    
        private static JsonSchema _getSchemaNode(JsonNode jsonNode)
                throws ProcessingException
        {
            final JsonNode schemaIdentifier = jsonNode.get(JSON_SCHEMA_IDENTIFIER_ELEMENT);
            if (null == schemaIdentifier){
                ((ObjectNode) jsonNode).put(JSON_SCHEMA_IDENTIFIER_ELEMENT, JSON_V4_SCHEMA_IDENTIFIER);
            }
    
            final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
            return factory.getJsonSchema(jsonNode);
        } // _getSchemaNode() ends
    }
    

    JSR223 Assertion

    【讨论】:

      【解决方案2】:

      试试JSON Path Assertion JMeter 插件,它允许您执行 JSON 文档的验证(如结构、值、一次一个类型),但不能用于模式验证。

      您可以使用JMeter-plugin Manager下载

      手动下载jar并放入/lib/ext文件夹并重启JMeter。


      来自文档:

      此组件允许您执行 JSON 文档的验证。

      首先,它会解析 JSON,如果数据不是 JSON,则会失败。

      其次,它会搜索指定的路径,使用 Jayway 的语法 JsonPath 1.2.0。如果找不到路径,就会失败。

      第三,如果在文档中找到 JSON 路径,并针对 请求了期望值,它将执行验证。对于空 值在 GUI 中有一个特殊的复选框。

      【讨论】:

      • 如何使用 JSON 路径断言验证 JSON 的结构?
      • 又是不完整的结构。当您使用Jayway JsonPath 1.2.0 样式编写断言时,当找不到元素的路径时,会引发失败,这间接地是您正在测试结构。请参阅我答案的Docs 部分中的第二点
      猜你喜欢
      • 2018-11-18
      • 2022-01-21
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多