Loading a collection of entities from a set of XML files

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

About this task

You can convert data into entities from generic XML files. Each file is read and converted into an entity by using the entity definitions in your BOM project.

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 it to true, then the files that are not recognized by the ModelFactory do not trigger an exception and continues loading the other entities. If set to false, then the file that is not recognized by the ModelFactory triggers an exception and stopping the loading of the rest of the entities.

Procedure

  1. 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> 
  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 have been created.
  5. In Insight Designer, run the test client as a JUnit test.