Loading a collection of entities from a set of files

You can load entities that are defined in XML or JSON by using the test driver createFileEntitySource (DataFormat format, String path) and loadEntities(Iterator<Entity> entities) methods.

About this task

To retrieve XML data from your system, you can use the REST API. Alternatively, you can create JSON representations of entities that you want to load.

You can specify a name pattern to select the files that you want to be converted. When you create the EntitySource, you can specify a regular expression, such as .*entity, to select the files that you want from a directory. For more information about regular expressions, see the Java™ Tutorials Lesson: Regular Expressions.

You can also configure the EntitySource implementation to take a specific action when an entity type is not recognized. The skipUnrecognizedEntities flag defines the failure behavior. If you set skipUnrecognizedEntities to true, the entities that are not recognized are skipped and the other recognized entities continue to load. If you set it to false and a file contains an entity that is not recognized, an exception is thrown and no other entities are loaded.

Procedure

  1. Create XML or JSON files that contain the representation of your entities:
    • Create an XML file that contains the GENERIC_XML representation of the entity. You can view the generic XML format by using the REST API to retrieve data from the system.
      <object xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ibm.com/ia/Entity" type="com.example.model.Customer"> 
         <attribute name="$IdAttrib">
            <string>customerId</string>
         </attribute>
         <attribute name="customerId">
            <int>0</int>
         </attribute>
         <attribute name="firstName">
            <string>Jack</string>
         </attribute>
         <attribute name="lastName">
            <string>Smith</string>
         </attribute>
         <attribute name="loyaltyCardOwner">
            <boolean>true</boolean>
         </attribute>
      </object> 
    • Create files that contain the JSON representation of the entities that you want to load:
      1. Go to the BMD to see the entity definitions.
      2. Map the BMD definition to its JSON counterpart by using the JSON equivalents in JSON mapping.
      3. Write the entity instances in JSON.
  2. Create an instance of an EntitySource<File> in your test client project by using the supplied factory methods on TestDriver. Specify the location of the files that you want to submit, and specify the format of the data within the files and the behavior when a failure occurs. For example:
    EntitySource<File> src = testDriver.createFileEntitySource(DataFormat.GENERIC_XML, “c:\entities”, “.*xml”);
    The pattern parameter ".*xml" converts all of the XML files in the specified location.
  3. Load the collection of entities by using an iterator that is retrieved from your EntitySource<File> instance. For example, the following code shows how to load the entities of the EntitySource<File> instance by passing the iterator to the loadEntities method:
    Map<String,RoutingStaus> results = testDriver.loadEntities(src.iterator());
    By default, entities are loaded to the server in ascending file name order, where a_entity.xml is loaded before b_entity.xml
  4. Use the testDriver.fetchEntity() method to verify that the entities are created.
  5. In Insight Designer, run the test client as a JUnit test.