Apex · REST

Mockaroo – An easy way to generate millions of test data

Recently, while working on a project I came across a need to generate data in bulk to be inserted into Objects for testing purpose. So, it was then while searching net, I got to know about ‘Mockaroo’ which proved to be a hassle free way to generate test data as and when required using REST API callouts. The best part being one can execute it using the ‘Anonymous code window’ of Developer console which means your Org is clean as there is no trace to how the data got generated. however, if same kind of data is to be generated several times, then, we can also have a class written and call it whenever required.

In the example explained in this document, bulk data will be generated for 2 objects and then the same will be done for a junction object which has the previous 2 objects as parent.

Prerequisites:

 

All set! Lets jump into the steps to get it done:

  1. Using the link shown above, register on ‘Mockaroo’. Once done, an API key will get generated for your account. This key will be used in all the API calls made to mockaroo from Salesforce org. To know the API key, go to Account page after logging in to Mockaroo.account_page
  2. Once ‘mockaroo’ account has been set up, login to your Salesforce Org and create 2 custom objects. (In this example, Parent One and Parent Two)
  3. Create another custom object in Salesforce (Junction Child) which should act as a junction object between previously created objects i.e. Parent One and Parent Two. In this example, API names of the lookup fields on ‘Junction Child’ object are Parent_One__c and Parent_Two__c.
  4. Go to Mockaroo account and create 2 separate schemas (each for objects Parent One and Parent Two) under the ‘Schemas’ tab. Mockaroo has it’s own set of data types (141 currently) which is needed while creating fields for the schema. Any external data type name isn’t recognized. (We can import schema details such as name of fields and data types using csv headers or JSON to prevent human error)
    NOTE: For field names in schema, we must use the same name as that of the API name of fields of objects in Salesforce.

    Schema Page
    List of schemas available in a mockaroo account
    Schema Builder page
    Schema Builder page
    Data Types
    Data types available in Mockaroo

     

  5. Save the schemas.
  6. Create a new schema either using the headers of CSV file which contains API names of fields of Junction_Child__c object for which mock data is to be generated or manually with following steps:
    • Create a field with the same name as that of the API name of the lookup field which relates Junction_Child__c to Parent_One__c.
    • Create a field with the same name as that of the API name of the lookup field which relates Junction_Child__c to Parent_Two__c. (Assign any data type to the fields for now. We’ll get back to the actual data types which is to be set here later in this document)
    • Save the schema
  7. Goto ‘APIS’ tab in mockaroo web page and click on ‘Create a New Mock API’.A page would open to configure the various parameters for the API.
  8. The ‘Route’ label, has 2 sections i.e. the API request methods (leave it as GET) and URL section which contains the section that gets appended after the domain part of the API endpoint.
    Edit the URL section as follows:
    /ParentOne.json
  9. The ‘Handler Script’ section describes the schema to be used while creating dummy data and the number of records to be generated.
    Edit these as follows:schema “Parent One”
    generate 10

    mock_API
    see description of each parameter (schema and generate) on the right side section

     

     

  10. Click on ‘Create API’
  11. Once done mockaroo generates a API endpoint URL which will be used to make the callouts from Apex code.
    mock_API_2
    Endpoint generated

     

  12. Repeat steps 7 to 11 for creating API for Parent Two.
  13. Then, goto the Developer Console and select ‘Open Execute Anonymous Window’ under ‘Debug’ menu.
  14. Here a REST API callout is needed get the random data that would be generated by mockaroo using the schema created in above steps. Code snippet is given below:
    HttpRequest req = new HttpRequest();
    
    //Set the request method to as ‘GET’.
    req.setMethod('GET');
    
    //Set endpoint as the API endpoint shown in step 11.
    req.setEndpoint('https://my.api.mockaroo.com/parentone.json?key=yourMockarooAccountKey');
    
    Http h = new Http();
    HttpResponse res = new HttpResponse();
    res = h.send(req);
    List pList = new List();
    pList = (List)JSON.deserialize(res.getBody(), List.Class);
    insert pList;
    
  15. The result returned by mockaroo API is in JSON format. So, it needs to be deserialized using JSON.deserialize() method and casted into concerned sObject  (Parent_One__c and Parent_Two__c in this case) before inserting the list into database.
  16. Repeat steps 13 to 15 for Parent Two.
  17. Extract the ‘Id’ values of the records using Workbench separately for
    Parent_One__c and Parent_Two__c in csv files.
  18. Go to Mockaroo and click on the tab labelled ‘DATASETS’.
  19. Upload the csv files extracted through workbench and save those. These
    will act as reference data to be populated in junction object fields.These datasets can be reused any number for times for reference as these reside at mockaroo end itself.1
  20. Again, navigate to ‘SCHEMAS’ tab and open Junction Child schema.
  21. In the Type section of parent_One__c and parent_Two__c fields, select ‘Dataset Column’.
  22. A dropdown will show up having the names of the dataset uploaded in the previous steps. Select the respective dataset
  23. Another dropdown would appear which will contain the field names of the corresponding dataset. Select the ‘Id’ field.
  24. Save the schema.
    NOTE: Datatypes used in mockaroo schemas are intrinsic to mockaroo and we cannot use any other data type.These datatypes can either be set manually or using a json file.
  25. Create another API for this schema by referring the steps 7 to 11.
  26. Go back to Developer Console and navigate to ‘Open Execute Anonymous Window’ again.
  27. Follow steps 13 to 15 for making a callout, but this time for Junction_Child__c. Make the necessary changes in the endpoint and deserializing section of the code.
    HttpRequest req = new HttpRequest();
    //Set the request method to as ‘GET’
    req.setMethod('GET');
    //Set endpoint as the API endpoint shown in step 11
    req.setEndpoint('https://my.api.mockaroo.com/junctionchild.json?key=yourMockarooAccountKey');
    Http h = new Http();
    HttpResponse res = new HttpResponse();
    res = h.send(req);
    List jList = new List();
    jList = (List)JSON.deserialize(res.getBody(), List.Class);
    insert jList;
    
  28. Once done, navigate to the tab assigned to Junction Child object and it would contain all the dummy data along with relations to Parent_One__c and Parent_Two__c.2

 

Limits and Considerations:

  1. 3 subscriptions of mockaroo are available:
    >>Free (200 API calls/day)
    >>Silver (no limit to number of API Calls, 1 million records/day)
    >>Gold (no limit to number of API calls, 10 million records/day)
  2. All mockaroo response are capped at 5000 records per API call.
  3. Datatypes used in mockaroo schemas are intrinsic to mockaroo and any other data type cannot be used.
  4. Mockaroo datatypes for fields in schema can either be set manually or using a json file having required parameters.
  5. POST request can be done with the schema details in request body (JSON format) and mockaroo will generate random data as per that schema but this schema does not get saved at mocaroo end. Please find beow a sample code ehich is sending a simple schema to mockaroo using a POST request:

    HttpRequest req = new HttpRequest();
    
    HttpResponse res = new HttpResponse();
    
    JSONGenerator gen = JSON.createGenerator(true);
    
    gen.writeStartObject();
    
    gen.writeStringField('name', 'first name');
    
    gen.writeStringField('type', 'First Name');
    
    gen.writeEndObject();
    
    String structure = gen.getAsString();
    String URL = '<a href="https://api.mockaroo.com/api/generate.csv?key=48071d40&amp;count=10" target="_blank" rel="noopener">https://api.mockaroo.com/api/generate.csv?key=48071d40&amp;count=10</a>';
    
    req.setEndpoint(URL);
    req.setMethod('POST');
    
    structure= '[\n'+structure+']\n';
    
    req.setBody(structure);
    
    Http h = new Http();
    
    res = h.send(req);
    
    system.debug('Generated Data::'+res.getBody());
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s