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);
        }
    }
  • 相关阅读:
    使用H5Stream实现rtsp流播放,并整合到web项目中
    浏览器通过RTSP协议取流实时显示在web页面(海康威视大华摄像机实时监控)
    Python-----获取excel的所有sheet页,并获取每个sheet页的内容
    MySQL表结构导出成Excel
    Hive 是什么?场景? vs RDBMS
    Scala “_” 的用法总结
    Hadoop主要组件知识点梳理
    javaIO:RandomAccessFile
    javaIO:IO和File
    java io 详细代码实现 纪录
  • 原文地址:https://www.cnblogs.com/zqlmmd/p/12074645.html
Copyright © 2011-2022 走看看