zoukankan      html  css  js  c++  java
  • 接口自动化增加jsonschema验证

    1. pom.xml 配置

        <dependency>
          <groupId>com.github.fge</groupId>
          <artifactId>json-schema-validator</artifactId>
          <version>2.2.6</version>
        </dependency>

    2. 数据源即 jsonschema文件。

    文件放置于resources目录下,因为测试与线上的数据返回有所不同,所以结合环境隔离

     3. jsonschema和接口返回的校验

    package util;
    
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.github.fge.jackson.JsonNodeReader;
    import com.github.fge.jsonschema.core.report.ProcessingMessage;
    import com.github.fge.jsonschema.core.report.ProcessingReport;
    import com.github.fge.jsonschema.main.JsonSchemaFactory;
    import org.testng.Assert;
    import org.testng.Reporter;
    import java.io.IOException;
    import java.io.InputStream;
    
    /**
     * JsonSchema工具类
     */
    public class jsonSchemaUtil {
        /**
         * 从指定路径读取Schema信息
         *
         * @param filePath Schema路径
         * @return JsonNode型Schema
         * @throws IOException 抛出IO异常
         */
        private static JsonNode readJSONfile(String filePath) throws IOException {
            InputStream stream = jsonSchemaUtil.class.getClassLoader().getResourceAsStream(filePath);
            return new JsonNodeReader().fromInputStream(stream);
        }
    
    
        /**
         * 将Json的String型转JsonNode类型
         *
         * @param str 需要转换的Json String对象
         * @return 转换JsonNode对象
         * @throws IOException 抛出IO异常
         */
        private static JsonNode readJSONStr(String str) throws IOException {
            return new ObjectMapper().readTree(str);
        }
    
        /**
         * 将需要验证的JsonNode 与 JsonSchema标准对象 进行比较
         *
         * @param schema schema标准对象
         * @param data   需要比对的Schema对象
         */
        private static void assertJsonSchema(JsonNode schema, JsonNode data) {
            ProcessingReport report = JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data);
            if (!report.isSuccess()) {
                for (ProcessingMessage aReport : report) {
                    Reporter.log(aReport.getMessage(), true);
                }
            }
            Assert.assertTrue(report.isSuccess());
        }
    
    
        /**
         * 将需要验证的response 与 JsonSchema标准对象 进行比较
         *
         * @param schemaPath JsonSchema标准的路径
         * @param responseJson   需要验证的Json数据
         * @throws IOException 抛出IO异常
         */
        public static void assertResponseJsonSchema(String schemaPath, String responseJson) throws IOException {
            JsonNode jsonSchema = readJSONfile(schemaPath);
            JsonNode responseJN = readJSONStr(responseJson);
            assertJsonSchema(jsonSchema, responseJN);
        }
    }
  • 相关阅读:
    P1019 单词接龙
    最小生成树模板题POJ
    区间DP
    牛客多校第三场-A-PACM Team-多维背包的01变种
    洛谷P1004 方格取数-四维DP
    牛客多校第二场A run(基础DP)
    P1494 [国家集训队]小Z的袜子(莫队)
    洛谷:过河卒
    Codeforces Round #486 (Div. 3)-B. Substrings Sort
    判断的值是否为空
  • 原文地址:https://www.cnblogs.com/zqlmmd/p/12074645.html
Copyright © 2011-2022 走看看