This a Groovy code example that shows how to use the GenRocket API to build a scenario from scratch to generate a 10,000 Users and output to an XML file.


Activity Diagram

The activity diagram defines the steps and API calls necessary to load, modify and run the Scenario.

Groovy Source Code Example

package com.genRocket.engine.examples

import com.genRocket.GenRocketException
import com.genRocket.engine.EngineAPI
import com.genRocket.engine.EngineManual
import com.genRocket.generator.*
import com.genRocket.receiver.DelimitedFileReceiver
import com.genRocket.receiver.JSONFileReceiver
import com.genRocket.receiver.SQLFileInsertReceiver
import com.genRocket.receiver.XMLFileReceiver

/**
 * Created by Hycel Taylor on 12/29/16.
 *
 * This a Groovy code example that shows how to use the GenRocket API to build
 * a scenario from scratch to generate a 10,000 Users and output to four different
 * file formats:
 * <ul>
 * <li>XML</li>
 * <li>Tab-Delimited</li>
 * <li>JSON</li>
 * <li>SQL</li>
 * </ul>
 *
 * The User Domain shall contain the following Attributes:
 * <ul>
 * <li>id</li>
 * <li>lastName</li>
 * <li>firstName</li>
 * <li>middleInitial</li>
 * <li>userName</li>
 * <li>emailAddress</li>
 * <li>password</li>
 * <li>ssn</li>
 * <li>creditCard</li>
 * <li>phoneNumber</li>
 * </ul>
 */
class Scenario10000Users {

  public static void main(String[] args) {
    String scenario = '/home/userName/anyScenario.grs'
    String domainName = 'User'
    String attributeName = null
    Map<String, String> parameters = [:]
    Boolean setLoop = false

    EngineAPI api = new EngineManual()

    try {
      // Initialize Scenario
      api.scenarioLoad(scenario)
      api.scenarioClear()

      // Add Domain and Attributes
      api.domainAdd(domainName, true)
      api.domainSetLoopCount(domainName, 10000.toString())

      // Add Attribute, lastName
      attributeName = "${domainName}.lastName"
      api.attributeAdd(attributeName)
      parameters = ['nameType': 'lastName', 'caseType': 'capitalize', 'sortOrder': 'random', 'waitAmount': '1']
      api.generatorAdd(attributeName, NameGen.canonicalName, setLoop, parameters)

      // Add Attribute, firstName
      attributeName = "${domainName}.firstName"
      api.attributeAdd(attributeName)
      parameters = ['nameType': 'firstName', 'caseType': 'capitalize', 'sortOrder': 'random', 'waitAmount': '1']
      api.generatorAdd(attributeName, NameGen.canonicalName, setLoop, parameters)

      // Add Attribute, middleInitial
      attributeName = "${domainName}.middleInitial"
      api.attributeAdd(attributeName)
      parameters = ['randomize': 'true']
      api.generatorAdd(attributeName, AlphabetGen.canonicalName, setLoop, parameters)

      // Add Attribute, username
      attributeName = "${domainName}.username"
      api.attributeAdd(attributeName)
      parameters = ['prefix': 'User', 'reference': '#{User.id}']
      api.generatorAdd(attributeName, UsernameGen.canonicalName, setLoop, parameters)

      // Add Attribute, emailAddress
      attributeName = "${domainName}.emailAddress"
      api.attributeAdd(attributeName)
      parameters = ['prefix': 'User', 'reference': '#{User.id}', 'suffix': 'email', 'extension': 'com']
      api.generatorAdd(attributeName, EmailGen.canonicalName, setLoop, parameters)

      // Add Attribute, password
      attributeName = "${domainName}.password"
      api.attributeAdd(attributeName)
      parameters = ['textToEncrypt': '#{User.id}', 'algorithm': 'SHA-256']
      api.generatorAdd(attributeName, EncryptGen.canonicalName, setLoop, parameters)

      // Add Attribute, ssn
      attributeName = "${domainName}.ssn"
      api.attributeAdd(attributeName)
      parameters = ['increment': '0', 'generationType': 'sequential', 'initArea': '001', 'initGroup': '01', 'initSerial': '0001']
      api.generatorAdd(attributeName, SSNGen.canonicalName, setLoop, parameters)

      // Add Attribute, creditCard
      attributeName = "${domainName}.creditCard"
      api.attributeAdd(attributeName)
      parameters = ['visa16': 'true', 'masterCard': 'true']
      api.generatorAdd(attributeName, CreditCardGen.canonicalName, setLoop, parameters)

      // Add Attribute, phoneNumber
      attributeName = "${domainName}.phoneNumber"
      api.attributeAdd(attributeName)
      parameters = ['useAreaCode': 'true', 'uniqueNumbers': 'true']
      api.generatorAdd(attributeName, RandomPhoneNumberGen.canonicalName, setLoop, parameters)

      // *** Receivers must be added after Domains ***

      // Add XMLFileReceiver
      String receiverName = 'XMLFileReceiver'
      parameters = [
        path: '#{resource.output.directory}',
        fileName:"${domainName}.xml"
      ]

      api.receiverAdd(domainName, XMLFileReceiver.canonicalName, receiverName, parameters)

      // Add DelimitedFileReceiver
      receiverName = 'DelimitedFileReceiver'
      parameters = [
        path: '#{resource.output.directory}',
        fileName:"${domainName}.txt"
      ]

      api.receiverAdd(domainName, DelimitedFileReceiver.canonicalName, receiverName, parameters)

      // Add JSONFileReceiver
      receiverName = 'JSONFileReceiver'
      parameters = [
        path: '#{resource.output.directory}',
        fileName:"${domainName}.json"
      ]

      api.receiverAdd(domainName, JSONFileReceiver.canonicalName, receiverName, parameters)

      // Add SQLFileInsertReceiver
      receiverName = 'SQLFileInsertReceiver'
      parameters = [
        path: '#{resource.output.directory}',
        fileName:"${domainName}.sql",
        tableName: 'user'
      ]

      api.receiverAdd(domainName, SQLFileInsertReceiver.canonicalName, receiverName, parameters)

      // Run Scenario
      api.scenarioRun()
    } catch (GenRocketException e) {
      println(e.getMessage())
    }
  }
}