How to generate json data from Json Schema Programmatically in Java - java

I am trying to create Body-parameter(JSON) for my POST Api , which is a JSON request . All I have is the JSON Schema . I am trying to come up with a list of different JSON test data covering Positive and negative flows for it .
Is there any option to generate / create the JSON data programmatic using Java ? . I have attached a small Json schema (just for understanding purpose) but my actual schema is more complicated with lot of Array's and Nested Json's .
My Json Schema :
{
"$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.",
"required": [
"FirstName",
"LastName",
"Age",
"Interest"
],
"properties": {
"FirstName": {
"$id": "#/properties/FirstName",
"type": "string",
"title": "The Firstname Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Vijay"
]
},
"LastName": {
"$id": "#/properties/LastName",
"type": "string",
"title": "The Lastname Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Karthik"
]
},
"Age": {
"$id": "#/properties/Age",
"type": "integer",
"title": "The Age Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
30
]
},
"Interest": {
"$id": "#/properties/Interest",
"type": "array",
"title": "The Interest Schema",
"description": "An explanation about the purpose of this instance.",
"default": [],
"items": {
"$id": "#/properties/Interest/items",
"type": "string",
"title": "The Items Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Food",
"movie",
"Learning",
"VideoGames"
]
}
}
}
}enter code here
My TestData looks like :
{
"FirstName":"Vivi",
"LastName":"Karrri",
"Age":30,
"Interest":["Food","movie","Learning","VideoGames"]
}
Any suggestions how can we achive this ?
Note : I am using Springboot and I have complete POJO for the request object

You can generate fake java-objects and then map them to JSON.
POJOs
If you already have the POJOs matching the schema, then we can skip this step.
If no, to generate a POJO from the schema, for example, can be used this library:
jsonschema2pojo.
Fake objects
Generating of objects with fake data can be done with a special library, some of them are listed here:
easy-random
podam
java-faker
Generating JSON
It's prettry simple and can be done with Jackson:
ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter prettyPrinter = objectMapper.writerWithDefaultPrettyPrinter();
String json = prettyPrinter.writeValueAsString(yourFakeObject);

If you have json schema then you can directly generate a sample JSON message from it.
https://github.com/jignesh-dhua/json-generator

Related

Avro Schema Data Validations with Regular Expressions in Java

I'm very new to Using AVRO Schema and I have a use case where I need to validate AVRO schema data with regular expressions. I mean the field value can allow Numbers OR only allow Alphabets OR AlphaNumeric values OR fixed min-max length etc. I tried like below but it's not working. if any idea please assist me for the same .
AVRO schema is :
{
"type": "record",
"name": "Employee",
"namespace": "com.test.avro",
"fields": [
{
"name": "empId",
"type": "string",
"pattern": "[1-9]"
},
{
"name": "empName",
"type": "string",
"pattern": "[a-zA-Z]"
},
{
"name": "createdDate",
"type": "string",
"pattern": "^[1-9]$"
},
{
"name": "mobile",
"type": "string",
"pattern": "^[1-9]*$"
}
]
}
Thanks in Advance.

How to remove an attribute from RequestSpecification/FilterableRequestSpecification body?

Dears,
I am working on creating a simple method which will take String argument which will be a path or other kind "pointer" to attribute/s in JSON and this method will remove those attribute/s.
My problem is I can find values of those attribute/s using JsonPath, but I can't find methods in rest assured (or other libraries) which could remove/delete attributes by given path.
JSON is already added earlier so i need to pull him from RequestSpecification or FilterableRequestSpecification object ex.
RequestSpecification rs = *objFromContext*;
FilterableRequestSpecification frs= (FilterableRequestSpecification) rs;
frs.getBody();
I've tried to work with JSONObject class and remove() but it doesn't work on complex JSONs.
given example JSON
{
"created": "string",
"updated": "string",
"items": [
{
"code": "TEST",
"nested": {
"code": "test",
"name": "name",
"other": [
{
"code": "TEST",
"name": "myName",
"quantity": 1
}
]
},
"itemsProperties": [
{
"code": "value1",
"name": "name",
"value": 123
}
]
},
{
"code": "TEST",
"nested": {
"code": "test",
"name": "name",
"other": [
{
"code": "TEST",
"name": "myName",
"quantity": 1
}
]
},
"itemsProperties": [
{
"code": "value2",
"name": "name",
"value": 123
}
]
}
],
"timer": {
"startDate": "2015-01-01",
"endDate": "2021-01-02"
},
"id": "myId"
}
using JsonPath jp = JsonPath.from(httpRequest.getBody().toString());
and then jp.get(items.itemsproperties.code) i can find value1 and value2.
I stuck in this point: How to remove those attributes from sended body?
I know i can convert body into JSONObject and then go field after field conversion between getJSONArray and GetJSONOBject and remove those fields, but i would like to make this metod much more universal.
Is this possible?
If you want to manipulate json in Rest-Assured JsonPath, then the answer is No. You can't do that. JsonPath help you to extract value from json, that's it, no more.
You have to use different libraries to remove key-value pair.
For example: using JsonPath Jayway
DocumentContext parse = JsonPath.parse(body);
parse.delete("$..itemsProperties..code");
System.out.println(parse.jsonString());

How to validate a field to have specific value only in AVRO Schema

I am using Avro Schema to validate my schema is:
{
"name": "Example",
"type": "record",
"fields": [
{
"name": "custId",
"type": "string"
},
{
"name": "danger",
"type": {
"type": "enum",
"symbols": [0,1,2,3],
"name": "danger"
}}
]
}
i have a requirement where i want field "danger" to have values 0,1,2,3 . If something else is given schema should not validate it. I know enum type are used for that but it allow only string.
How can i achieve this.

How can I get Avro SchemaBuilder to build a schema of a map between two 'records'?

Trying to serialize Java Objects represented as a Map. This requires me to build an Avro Schema first. I am using Avro SchemaBuilder for schema generation. Have already succeeded in schema generation for Map's objects. However can't deal with schema building for overall Map.
Final schema should look like the following (created this one manually):
{
"type": "record",
"name": "MapObject",
"namespace": "test",
"fields": [
{
"name": "CacheMap",
"type": {
"type": "map",
"values": [
{
"type": "record",
"name": "User",
"namespace": "test",
"fields": [
{
"name": "id",
"type": "long"
},
{
"name": "status",
"type": "string"
}
]
},
{
"type": "record",
"name": "UserKey",
"namespace": "test",
"fields": [
{
"name": "key",
"type": "long"
},
{
"name": "keyPrint",
"type": "string"
}
]
}
]
}
}
]
}
How can I pass to different records for key and value of a map in SchemaBuilder?
SchemaBuilder.map().values() does not allow to pass more than one Schema as a parameter for .value().
Apache Avro Unions can be of help here.
SchemaBuilder.builder()
.map()
.values(SchemaBuilder.unionOf().type(firstSchema).and().type(secondSchema).endUnion());
where the firstSchema and the secondSchema are separate schemas already defined for required User and UserKey 'records' (considering the schema from question)

error when trying to validate json schema

I am trying to validate my json response in my rest assured tests and i get the following error in my schema. not sure how to resolve this. any help will be appreciated :) thank you.
error:
io.restassured.module.jsv.JsonSchemaValidationException: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('s' (code 115)): was expecting double-quote to start field name
at [Source: java.io.StringReader#54c622a7; line: 1, column: 3]
schema:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"status"
],
"properties": {
"status": {
"$id": "#/properties/status",
"type": "string",
"title": "The Status Schema",
"default": "",
"examples": [
"PROCESSING"
],
"pattern": "^(.*)$"
}
}
}
method:
Assert.assertThat(bookingResponse, matchesJsonSchemaInClasspath("book-response-schema.json"));

Categories

Resources