• rest-assured之Schema validation(包括JSON Schema validation及Xml Schema validation)


      rest-assured从2.1.0版本开始支持  Schema 验证,包括JSON Schema validation及Xml Schema validation。我们之前断言响应体都是一个一个字段来进行断言,这样如果断言的字段比较多的话就非常的麻烦,为了解决这个问题,我们可以使用schema文件来进行响应体的断言,schema文件可以断言整个response 。

    1.JSON Schema validation

    例如:在classpath下面放置以下的schema文件,products-schema.json:

     1 {
     2     "$schema": "http://json-schema.org/draft-04/schema#",
     3     "title": "Product set",
     4     "type": "array",
     5     "items": {
     6         "title": "Product",
     7         "type": "object",
     8         "properties": {
     9             "id": {
    10                 "description": "The unique identifier for a product",
    11                 "type": "number"
    12             },
    13             "name": {
    14                 "type": "string"
    15             },
    16             "price": {
    17                 "type": "number",
    18                 "minimum": 0,
    19                 "exclusiveMinimum": true
    20             },
    21             "tags": {
    22                 "type": "array",
    23                 "items": {
    24                     "type": "string"
    25                 },
    26                 "minItems": 1,
    27                 "uniqueItems": true
    28             },
    29             "dimensions": {
    30                 "type": "object",
    31                 "properties": {
    32                     "length": {"type": "number"},
    33                     "width": {"type": "number"},
    34                     "height": {"type": "number"}
    35                 },
    36                 "required": ["length", "width", "height"]
    37             },
    38             "warehouseLocation": {
    39                 "description": "Coordinates of the warehouse with the product",
    40                 "$ref": "http://json-schema.org/geo"
    41             }
    42         },
    43         "required": ["id", "name", "price"]
    44     }
    45 }

    我们可以通过上面的schema文件来验证 "/products" 这个请求的响应数据是否符合规范:

    1 get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json"));

    matchesJsonSchemaInClasspath 是从 io.restassured.module.jsv.JsonSchemaValidator 这个类中静态导入的,并且推荐静态导入这个类中的所有方法,然而为了能够使用io.restassured.module.jsv.JsonSchemaValidator 这个类必须依赖 json-schema-validator module ,我们可以从这个网页下载它,或者是通过maven添加以下依赖来获取:

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>3.0.6</version>
    </dependency>

    1.1 JSON Schema Validation 设置 

      rest-assured 的 json-schema-validator module 使用的是 Francis Galiegue's  json-schema-validator(fge) 库来实现验证(Validation)。如果想要配置基础 fge 库,我们可以这样写:

    1 // Given
    2 JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder().setValidationConfiguration(ValidationConfiguration.newBuilder().setDefaultVersion(DRAFTV4).freeze()).freeze();
    3 
    4 // When
    5 get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json").using(jsonSchemaFactory));

      这个 using 方法允许我们传递一个jsonSchemaFactory 实例,rest-assured在进行验证的时候就会使用这个实例。这种配置就允许我们在验证响应结果时进行更详细的配置。

      fge 库同时也允许设置validation为 checked或者unchecked,默认情况下rest-assured使用的是checked的validation,如果想要改变这个值,我们可以提供一个 JsonSchemaValidatorSettings 的实例给matcher。例如:

    1 get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json").using(settings().with().checkedValidation(false)));

    上面的 setting 方法是从 JsonSchemaValidatorSettings 这个类中静态导入的。

    1.2  Json Schema Validation静态配置

      让我们来想象一下,假如我们想一直使用unchecked的validation并且想设置Json Schema的版本为3,与其将JsonSchemaValidatorSettings 的实例一个个提供给所有的matchers,我们不如将它定义为一个静态的:

    1 JsonSchemaValidator.settings = settings().with().jsonSchemaFactory(
    2         JsonSchemaFactory.newBuilder().setValidationConfiguration(ValidationConfiguration.newBuilder().setDefaultVersion(DRAFTV3).freeze()).freeze()).
    3         and().with().checkedValidation(false);
    4 
    5 get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json"));

    通过上面的方式,现在任意一个导入了  JsonSchemaValidator 的matcher都会使用 DRAFTV3 作为默认的版本并且使用unchecked的validation。

      为了重置 JsonSchemaValidato 为默认的配置,我们可以简单的调用一下 reset 方法来实现:

    1 JsonSchemaValidato.reset();

    1.3 不依赖rest-assured的JSON Schema Valition

      我们不依赖rest-assured也一样可以使用 json-schema-valition ,只要我们把JSON文件表示为 String 类型,我们可以这么做:

     1 import org.junit.Test;
     2 import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
     3 import static org.hamcrest.MatcherAssert.assertThat;
     4  
     5 public class JsonSchemaValidatorWithoutRestAssuredTest {
     6  
     7  
     8     @Test public void
     9     validates_schema_in_classpath() {
    10         // Given
    11         String json = ... // Greeting response
    12  
    13         // Then
    14         assertThat(json, matchesJsonSchemaInClasspath("greeting-schema.json"));
    15     }
    16 }

    2.Xml的 Schema 和 DTD Validation(验证)

      通过使用 Xml Schema(XSD)或者DTD我们同样可以验证Xml响应体。

    XSD例子:

    1 get("/carRecords").then().assertThat().body(matchesXsd(xsd));

    DTD例子:

    1 get("/videos").then().assertThat().body(matchesDtd(dtd));

    matchesXsd 方法和 matchesDtd 方法属于 Hamcrest matchers 包里,我们需要静态导入 io.restassured.matcher.RestAssuredMatchers

  • 相关阅读:
    设计模式:备忘录模式??
    jQuery总结
    MYSQL(三)
    MYSQL(二)
    MYSQL(一)
    Web.xml配置详解
    Spring-JDBC通用Dao
    网络七层协议的形象说明
    网络编程概念
    JSP 9 大内置对象详解
  • 原文地址:https://www.cnblogs.com/lwjnicole/p/8297350.html
Copyright © 2020-2023  润新知