Skip to content

Latest commit

 

History

History
246 lines (215 loc) · 9.41 KB

README.md

File metadata and controls

246 lines (215 loc) · 9.41 KB

json-random-generator

Build Status

Json random generator is a tool to produce data from json schema automatically, even nested lists are supported! Written fully in Scala, uses POJOs generated by http://www.jsonschema2pojo.org.

Getting started

  1. Place your schema in: src/main/resources/schema.
  2. Run mvn generate-sources to obtain object oriented representation of your json schema. Generated POJOs will be placed in target directory.
  3. Set SchemaType in Config.scala to POJO type that you are interested in.
  4. Set schemaPath in Config.scala. It's path to generated POJO: output.<path_to_POJO>.<POJO_Class_Name>
  5. Set numOfRecords in Config.scala.
  6. Run Main.scala. Generated json will be in target directory.
  7. (Optional) If you are not pleased with default generated values in json, you can always overwrite it, using customRules method in Config.scala file and run Main.scala again.

Config

object Config {

  type SchemaType = AnyRef /* YOUR SCHEMA TYPE HERE (generated POJO type), e.g. ComplexSchema */
  
  def schemaPath = "" //e.g. output.ComplexSchema 

  def numOfRecords = 10

  def customRules: Seq[SchemaType] => Seq[SchemaType] =
    //You can define here additional rules for specific fields
    //Example:
    objects => Mapping(objects) { rows =>
      rows.zipWithIndex.map { case (r, idx) =>
        //set date-time between range of dates
        r.setCreatedDateTime(
          DateTypeGenerators.between(new Date(1581809593000L), new Date(1582809593000L)).sample.get
        )
        //set date the same as createdDateTime
        r.setCreatedDate(DateTimeUtils.date2Date(r.getCreatedDateTime))
        //set time the same as createdDateTime
        r.setCreatedTime(DateTimeUtils.date2Time(r.getCreatedDateTime))

        //set flatNo between 1000 and 10000 with precision 7 and scale 2
        r.getBillingAddress.setFlatNo(BigDecimalTypeGenerators.between(1000, 10000, 7, 2).sample.get)

        //set id of the user with increasing number
        r.getPerson.setId(idx.toLong)
        //set name using generator
        r.getPerson.setName(Gen.oneOf("Gabriel", "Alicja", "Rafal", "Vova", "Milton", "Pawel").sample.get)
        //set lastname using generator
        r.getPerson.setLastname(Gen.oneOf("Kowalski", "Smith", "Brown", "Wilson", "Miller", "Johnson").sample.get)
      }
    }
}

Usage for CLI

  1. Place your schema somewhere on the disk e.g. /c/schema
  2. Run: mvn clean package -Dschema.dir=/c/schema to build the project with generated object oriented representation of your json schema
  3. (Optional) If you want to check what classes have been generated, you can use this command:

jar tf target/json-random-generator-1.0-SNAPSHOT.jar | grep ^output*

All the classes are generated in output folder.

  1. Run:

java -jar target/json-random-generator-1.0-SNAPSHOT.jar output.<path_to_POJO>.<POJO_Class_Name> <num_of_events> <output_dir>

Mocking data for CLI usage

Json random generator supports mocking data by using annotations that was developed in this project: https://github.com/kklimexk/jsonschema2pojo/tree/develop. If you want to use these annotations, integration between projects is needed, because the modified jsonschema2pojo is not in the central maven repository. Basically we need to modify pom.xml in json random generator to use jsonschema2pojo in version 1.0.3-SNAPSHOT. Currently supported annotations in generator:

  • ValueHintDecimal
  • ValueHintOptions
  • ValueHintIterator
  • ValueHintPrefix
  • ValueHintPostfix
  • ValueHintRange

These annotations are generated in the POJOs when user defines jrg.properties for schema to control generating fields e.g.

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "definitions": {
    "address": {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city":           { "type": "string" , "jrg.properties" : {
                                              "options" : [
                                              "London",
                                              "New York",
                                              "Tokyo"],
                                              "postfix" :"_city"
                                              }},
        "state":          { "type": "string" ,"jrg.properties" : {
                                                "regex" : "^[2-9]\\d{2}-\\d{3}-\\d{4}$",
                                                "length" : {
                                                  "min" : 4,
                                                  "max" : 10
                                                }
                                              }},
        "buildingNo":     { "type": "integer" },
        "flatNo":         { "type": "number", "jrg.properties" : {
                                                "iterator" : {
                                                  "start": 1,
                                                  "restart": 200,
                                                  "step" : 5,
                                                  "initial" :100
                                                },
                                                "prefix" :"LN"
                                              }},
        "isTaken":        { "type": "boolean" },
        "credit":         { "type": "number","jrg.properties" : {
                                              "decimal" : {
                                                "precision": 11,
                                                "scale": 4
                                              },
                                              "range" : {
                                                "min" : 3,
                                                "max" : 44
                                              }

                                            }},
        "info":           { "type": "null" },
        "orderReleaseDate": {
          "type": "string",
          "format": "date"
        },
        "orderReleaseDateTime": {
          "type": "string",
          "format": "date-time"
        },        
        "orderReleaseTime": {
          "type": "string",
          "format": "time"
        },
        "typeOfAddress":  { "enum": ["normal", "special"] }
      },
      "required": ["street_address", "city", "state"]
    },
    
    "numbersArr": {
      "type": "array", "jrg.properties" : {
        "decimal" : {
          "precision": 11,
          "scale": 4
        }
      },
      "items": {
        "type": "number"
    }}
  },

  "type": "object",

  "properties": {
    "billing_address": { "$ref": "#/definitions/address" },
    "shipping_address": {
      "allOf": [
        { "$ref": "#/definitions/address" },
        { "properties":
        { "type": { "enum": [ "residential", "business" ] } },
          "required": ["type"]
        }
      ]
    },
    "numbersArray": { "$ref": "#/definitions/numbersArr" },
    "stringField": { "type": "string" },
    "coolMap": {
      "type": "object",
      "additionalProperties": { "type": "string" }
    },
    "stringArray": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "integerArray": {
      "type": "array",
      "items": {
        "type": "integer"
      }
    },
    "doubleArray": {
      "type": "array",
      "items": {
        "type": "number"
      }
    },
    "booleanArray": {
      "type": "array",
      "items": {
        "type": "boolean"
      }
    },
    "addressArray": {
      "type": "array",
      "items": { "$ref": "#/definitions/address" }
    },
    "integerNestedArray": {
      "type": "array",
      "items": {
        "type": "array",
        "items": {
          "type": "integer"
        }
      }
    },
    "addressNestedArray": {
      "type": "array",
      "items": {
        "type": "array",
        "items": {
          "type": "array",
          "items": { "$ref": "#/definitions/address" }
        }
      }
    }
  }
}

Supported Types

Schema type Java type
string java.lang.String
integer java.lang.Long
number java.math.BigDecimal
boolean java.lang.Boolean
null java.lang.Object
enum generated Java enum
object generated Java type
map<string,string> java.util.Map<String,String>
array java.util.List<String or Integer or JavaType etc.>
array<enum> java.util.List<EnumType>
nested array java.util.List<java.util.List<JavaType or another List>>
date-time java.util.Date in ISO 8601 format
date java.lang.String
time java.lang.String