集成 Rest-Assured 和 JSON Schema 验证 JSON Schema 是一个非常强大的 JSON 结构验证工具,它通过定义 JSON 的结构、数据取值范围等方式验证 JSON 数据。
常用类型JSON Schema 将 JSON 数据类型划分为 6 种类型: - string:文本类型,可以包含 Unicode 字符;
- number/integer:数字型或整数型;
- object:类似于 Java 中 map 的概念,包含 key 机器对应的 value,key 在 JSON 中对应与 properties,因此 object 在 JSON 中对应的是包含零个或多个的 properties 的复杂结构;
- array:对应于数组或 Java 中 list 的概念。
- boolean:布尔数据类型。
- null:null 通常用来表示一个不存在的值,当 Schema 中定义某一个 property 是 null,那么这个 property 的取值只有一个:null。
另外,加之 JSON Schema 也支持"引用"的概念,实现 Schema 定义的复用,因此,使用上述常用类型通过各种组合,就可以定义出各种复杂的数据类型。
定义 Schema 验证 Card 这一数据模型本文所述的 Open API 的返回值也是 JSON 数据,样例如下 snippet 所示。从中可以看出,返回的 JSON 数据最外层是一个通用的结构,用于表示本次 API 调用结果;然后,"data"property 是 API 调用所返回的业务数据。在本例中,它是一个卡片信息描述数据,包括了"id","cardNum"等诸多 properties。同时,还包含了一个"cardBillingAddressList"用于标识持卡人的账单地址信息列表。 清单 12. Card data response- {
- "errName": null,
- "errMsg": "SUCCESS",
- "errCode": 0,
- "data": [
- {
- "id": 1,
- "cardNum": "C0000001",
- "cardOwnerName": "CENT LUI",
- "cardType": "0",
- "cardSeqNum": 0,
- "starPoints": 1024,
- "cardBillingAddressList": [
- {
- "id": 1,
- "cardNum": "C0000001",
- "region": "AP",
- "country": "CN",
- "state": "HeNan",
- "city": "LuoYang",
- "street": "Peking Rd",
- "extDetail": "Apartment 1-13-01 No.777"
- },
- {
- "id": 7,
- "cardNum": "C0000001",
- "region": "EU",
- "country": "ES",
- "state": "Madrid",
- "city": "Sol",
- "street": "Century Rd",
- "extDetail": "Apartment 1-13-01 No.777"
- }
- ],
- "primaryCard": true
- }
- ]
- }
复制代码
对于这样的返回值,根据上面所述的 JSON Schema 知识,定义出的 Schema 信息如下: 清单 13. Card data JSON Schema- {
- "$schema": "http://json-schema.org/draft-04/schema#",
- "title": "银行卡数据格式验证 Schema",
- "definitions": {
- "eleInnerData": {
- "properties": {
- "id": {
- "type": "integer",
- "minimum": 1
- },
- "cardNum": {
- "$ref":"common-schema.json#/definitions/cardNum"
- },
- "cardOwnerName": {
- "type": "string",
- "minLength": 2,
- "maxLength": 128
- },
- "cardType": {
- "type": "string",
- "minLength": 1,
- "maxLength": 1,
- "enum": [
- "0",
- "1"
- ]
- },
- "cardSeqNum": {
- "type": "integer",
- "minimum": 0,
- "maximum": 127
- },
- "starPoints": {
- "type": "number",
- "minimum": 0.00
- },
- "cardBillingAddressList": {
- "$ref": "address-schema.json"
- },
- "primaryCard": {
- "type": "boolean",
- "enum": [
- true,
- false
- ]
- }
- },
- "required": [
- "id",
- "cardNum",
- "cardOwnerName",
- "cardType",
- "cardSeqNum",
- "starPoints",
- "cardBillingAddressList",
- "primaryCard"
- ],
- "additionalProperties": false
- },
- "eleData": {
- "type": "array",
- "items": {
- "$ref": "#/definitions/eleInnerData"
- },
- "minItems": 0
- }
- },
- "allOf": [
- {
- "$ref": "common-schema.json"
- },
- {
- "type": "object",
- "properties": {
- "data": {
- "$ref": "#/definitions/eleData"
- }
- },
- "required": [
- "data"
- ],
- "additionalProperties": true
- }
- ]
- }
复制代码
其中引用了 common-schema 的定义如下: 清单 14. common-schema 定义- {
- "$schema": "http://json-schema.org/draft-04/schema#",
- "title": "通用交互数据格式验证 Schema",
- "definitions": {
- "cardNum": {
- "type": "string",
- "minLength": 8,
- "maxLength": 8,
- "pattern": "[C|S](0*)\\d+"
- },
- "errName": {
- "anyOf": [
- {
- "type": "string",
- "minLength": 1
- },
- {
- "type": "null"
- }
- ]
- },
- "errMsg": {
- "type": "string",
- "minLength": 1
- },
- "errCode": {
- "type": "integer",
- "maximum": 0
- }
- },
- "type": "object",
- "properties": {
- "errName": {
- "$ref": "#/definitions/errName"
- },
- "errMsg": {
- "$ref": "#/definitions/errMsg"
- },
- "errCode": {
- "$ref": "#/definitions/errCode"
- }
- },
- "required": ["errName","errMsg","errCode"],
- "additionalProperties": true
- }
复制代码
在 Rest-Assured 中对返回数据执行 JSON Schema ValidationRest-Assured 从 version 2.10 开始支持 JSON Schema Validation,读者只需要在 pom 文件中添加如下的 dependency 就可以支持 JSON Schema Validation 了: 清单 15. JSON Schema Validation 所需的 Maven dependencies- <dependency>
- <groupId>com.jayway.restassured</groupId>
- <artifactId>json-schema-validator</artifactId>
- <version>2.9.0</version>
- <scope>test</scope>
- </dependency>
复制代码
使用 Rest-Assured 提供的 Schema Validator 验证 Rest-Assured Response 返回数据是非常简单的,下面这个例子中,只是一行代码就能实现以 schemaFile 所指定的 JSON Schema 来验证 response 的 body。 清单 16. 使用 Rest-Assured 做 JSON Schema Validation
- public void assertThatRepliedCardDataMetSchemaDefinedSpecs(String schemaFile) {
- response.body(JsonSchemaValidator.
- matchesJsonSchemaInClasspath("schemas/" + schemaFile));
- }
复制代码
|