Search This Blog

Monday, November 10, 2014

JSON Schema Validation Using REST-assured Framework

JSON Schema

  • IS a JSON based data format
  • Represents the structure of JSON data in terms of few attributes like : 'type','description','required' etc.
  • Follows a hierarchy(object-attribute) of representation
  • Every JSON returned has been based upon a format which is called JSON Schema

Test Case

Scenario: Verify the JSON returned from a service resource follows a JSON Schema
@Given:  WebService Resource URL [http://localhost:8080/rest-assured-example/service/single-user]
@Input:    
  • Generate JSON schema file and put it on the classpath root
  • Specify JAR download dependency of Rest-Assured JSON Validator Schema in pom.xml

Generating JSON Schema

Below is the sample JSON response returned when /service/single-user is accessed:

 {  
   "email": "test@hascode.com",  
   "firstName": "Tim",  
   "id": "1",  
   "lastName": "Testerman"  
 }

Corresponding JSON schema can be obtained from here and JSON schema file can be generated which would look like this:

{  
      "type":"object",  
      "$schema": "http://json-schema.org/draft-03/schema",  
      "id": "http://jsonschema.net",  
      "required":false,  
      "properties":{  
           "email": {  
                "type":"string",  
                "id": "http://jsonschema.net/email",  
                "required":false  
           },  
           "firstName": {  
                "type":"string",  
                "id": "http://jsonschema.net/firstName",  
                "required":false  
           },  
           "id": {  
                "type":"string",  
                "id": "http://jsonschema.net/id",  
                "required":false  
           },  
           "lastName": {  
                "type":"string",  
                "id": "http://jsonschema.net/lastName",  
                "required":false  
           }  
      }  
 }  

Identify Root Classpath & Save the JSON Schema


Step-1: Normally we face problem while identifying Root Classpath file.In these case you can use the below code snippet to find out the path:

 System.out.println(this.getClass().getResource("/").getPath());  

Step-2: Save the above schema content in a file say: getService.json and put it under the root classpath(which we go from Step-1).

Specify JSON Validator Schema Dependency

Below is the Dependency snippet which can be specified in POM.xml in order to download the dependency:

      <dependency>  
           <groupId>com.jayway.restassured</groupId>  
           <artifactId>json-schema-validator</artifactId>  
           <version>2.2.0</version>  
      </dependency>  

Write Your Test

Before we write the test,We need to import matchesJsonSchemaInClasspath static method using static import as shown below:

 import static com.jayway.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;  

Define your unit test like below:
     @Test  
      public void testJsonSchema(){  
           //Uses the generated schema to Assert  
           get("http://localhost:8080/rest-assured-example/service/single-user").then().assertThat().body(matchesJsonSche  maInClasspath("getService.json"));  
      }  

Troubleshooting

Exception : java.lang.IllegalArgumentException: Schema to use cannot be null

 java.lang.IllegalArgumentException: Schema to use cannot be null  
      at com.jayway.restassured.module.jsv.JsonSchemaValidator.validateSchemaIsNotNull(JsonSchemaValidator.java:240)  
      at com.jayway.restassured.module.jsv.JsonSchemaValidator.access$100(JsonSchemaValidator.java:75)  
      at com.jayway.restassured.module.jsv.JsonSchemaValidator$JsonSchemaValidatorFactory.create(JsonSchemaValidator.java:251)  
      at com.jayway.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchema(JsonSchemaValidator.java:161)  
      at com.jayway.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath(JsonSchemaValidator.java:117)  
      at testcases.ApiTesting.testJsonSchema(ApiTesting.java:19)  
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
      at java.lang.reflect.Method.invoke(Unknown Source)  
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)  
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)  
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)  
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)  
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)  
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)  
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)  
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)  
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)  
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)  
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)  
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)  
      at org.junit.runners.ParentRunner.run(ParentRunner.java:300)  
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)  
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)  
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)  
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)  
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)  
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)  

Solution: Generally this exception noticed when we have the JSON schema file which is absent on the root classpath. Please use the above Step-1 and Step-2 to solve this out.

Hope this helps!!

2 comments:

  1. You have worked to perfection on this article. Thanks for taking the time to post search valuable information. I Recommendation this. JSON Beautifier

    ReplyDelete
  2. OR, if you are in a multi module project just find the module with the `resources` folder. Usually where the properties files lie as well.

    ReplyDelete