REST Assured 56 - JSON Schema Validation Without Rest Assured

REST Assured 系列汇总 之 REST Assured 56 - JSON Schema Validation Without Rest Assured

介绍

前面我们了解了 JSON Schema Validation In Rest Assured,你也许会好奇为啥还需要在不用 REST Assured的情况下验证 JSON Schema。我们不仅要验证 JSON response schema,还需要验证 JSON request payload, 确保 payload 是否正确。如果我们是动态创建或通过外部资源获取 payload,那么在传给 request 之前,我们需要验证 shcema。

我们可能需要验证任意 JSON,并不局限于通过 Rest Assured。

我们可以独立使用 json-schema-validator,不用结合 Rest Assured。有点类似 Junit 可以集成 Selenium 或 Rest Assured 或其他类库。如果明确不用Rest Assured,就必须添加 Hamcrest (一个核心 API 和 hamcrest matcher framework 库)

前提条件

添加 json-schema-validator 依赖包

<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>4.3.1</version>
</dependency>

添加 hamcrest 依赖包

<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest -->
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.1</version>
    <scope>test</scope>
</dependency>

Validate a JSON without Rest Assured

因为 json-schema-validator 库中 schema 验证方法会创建 一个 hamcrest matcher,所以我们在不用 Rest Assured 也能验证 schema。

Sample JSON schema
将下面 JSON Schema 存在 resource 以外的文件夹下。

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},
    "examples": [
        {
            "firstname": "Jim",
            "lastname": "Brown",
            "totalprice": 111,
            "depositpaid": true,
            "bookingdates": {
                "checkin": "2018-01-01",
                "checkout": "2019-01-01"
            },
            "additionalneeds": "Breakfast"
        }
    ],
    "required": [
        "firstname",
        "lastname",
        "totalprice",
        "depositpaid",
        "bookingdates",
        "additionalneeds"
    ],
    "properties": {
        "firstname": {
            "$id": "#/properties/firstname",
            "type": "string",
            "title": "The firstname schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "Jim"
            ]
        },
        "lastname": {
            "$id": "#/properties/lastname",
            "type": "string",
            "title": "The lastname schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "Brown"
            ]
        },
        "totalprice": {
            "$id": "#/properties/totalprice",
            "type": "integer",
            "title": "The totalprice schema",
            "description": "An explanation about the purpose of this instance.",
            "default": 0,
            "examples": [
                111
            ]
        },
        "depositpaid": {
            "$id": "#/properties/depositpaid",
            "type": "boolean",
            "title": "The depositpaid schema",
            "description": "An explanation about the purpose of this instance.",
            "default": false,
            "examples": [
                true
            ]
        },
        "bookingdates": {
            "$id": "#/properties/bookingdates",
            "type": "object",
            "title": "The bookingdates schema",
            "description": "An explanation about the purpose of this instance.",
            "default": {},
            "examples": [
                {
                    "checkin": "2018-01-01",
                    "checkout": "2019-01-01"
                }
            ],
            "required": [
                "checkin",
                "checkout"
            ],
            "properties": {
                "checkin": {
                    "$id": "#/properties/bookingdates/properties/checkin",
                    "type": "string",
                    "title": "The checkin schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": "",
                    "examples": [
                        "2018-01-01"
                    ]
                },
                "checkout": {
                    "$id": "#/properties/bookingdates/properties/checkout",
                    "type": "string",
                    "title": "The checkout schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": "",
                    "examples": [
                        "2019-01-01"
                    ]
                }
            },
            "additionalProperties": true
        },
        "additionalneeds": {
            "$id": "#/properties/additionalneeds",
            "type": "string",
            "title": "The additionalneeds schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "Breakfast"
            ]
        }
    },
    "additionalProperties": true
}

代码:

import java.io.File;
 
import org.hamcrest.MatcherAssert;
import org.junit.Test;
 
import io.restassured.module.jsv.JsonSchemaValidator;
 
 
public class VerifyJsonSchemaWithoutRestAssured {
	
	@Test
	public void verifyJsonSchemaWithoutRestAssured()
	{
		String json = "{\r\n" + 
				"    \"firstname\" : \"Jim\",\r\n" + 
				"    \"lastname\" : \"Brown\",\r\n" + 
				"    \"totalprice\" : 111,\r\n" + 
				"    \"depositpaid\" : true,\r\n" + 
				"    \"bookingdates\" : {\r\n" + 
				"        \"checkin\" : \"2018-01-01\",\r\n" + 
				"        \"checkout\" : \"2019-01-01\"\r\n" + 
				"    },\r\n" + 
				"    \"additionalneeds\" : \"Breakfast\"\r\n" + 
				"}";
		
		MatcherAssert.assertThat(json, JsonSchemaValidator.matchesJsonSchema(new File("C:\\Users\\kkk\\git\\master\\src\\test\\java\\JsonSchema\\SampleJsonSchemaCreateBooking.json")));
	}
 
}

如果将期望的 JSON Schema 存在 Resource folder 下,那么也可以使用 matchesJsonSchemaInClasspath 方法。

上一篇:dockerFile 学习


下一篇:graphql 介绍