Submitting a collection of events from a set of XML files

You can submit events that are defined in XML by using the test driver createFileEventSource(DataFormat format, String path) and submitEvents(Iterator<Event> events) methods.

About this task

You can convert data into events from generic XML or typed XML files. You can use either the TYPED_XML or GENERIC_XML data format to submit events. Each file is read and converted into an event by using the event 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 EventSource, you can specify a regular expression, such as .*event, 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 EventSource implementation to take a specific action when an event type is not recognized. The following fields can be specified:

SKIP_NO_FAILURE

If data is not recognized when processing an XML file, throw an exception. When data is not recognized, the event delivery is stopped and the submitEvents method throws the exception. Use this command field when you expect your solution to recognize all data in the files as events.

SKIP_UNRECOGNIZED_EVENTS

If data is not recognized when processing an XML file, skip the file and process the next. Use this field when you expect the directory to contain files have both recognizable and unrecognizable data. When this field is used, event submission is not interrupted by an unexpected variation in event types.

Procedure

  1. Convert your existing data into XML files. Create one file per event. For more information, see Creating an event XML file from an XSD. In the New XML File wizard, select the event of interest as the root element. The result gives you an XML formatted message template. You must then edit the payload of the message for your test.

    If you are using TYPED_XML, export your event types to an XSD file. You can use a link in the solution map to open the export wizard. Select the solution in your workspace to open the solution map. For more information, see Exporting event types to an XML schema.

    Create an XML file (data/event.xml) from the XML schema. For more information, see Creating an event XML file from an XSD. In the New XML File wizard, select the event of interest as the root element. The result gives you an XML formatted message template. You must then edit the payload of the message for your test.

    The following example uses TYPED_XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <m:UserRegistration xmlns:m="http://www.ibm.com/ia/xmlns/default/TransferBuddyBOM/model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ibm.com/ia/xmlns/default/TransferBuddyBOM/model model.xsd ">
      <m:timestamp>2001-12-31T12:00:00</m:timestamp>
      <m:user>m:Adam</m:user>
      <m:address>
        <m:country>m:France</m:country>
        <m:state>m:Normandy</m:state>
        <m:street>m:My Street</m:street>
        <m:zip>m:76300</m:zip>
      </m:address>
    </m:UserRegistration>

    If you are using GENERIC_XML, create an XML file that contains the GENERIC_XML representation of the event.

  2. Create an instance of an EventSource<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:
    EventSource<File> fileEventSource = testDriver.createFileEventSource("C:\myEvents", ".*xml", DataFormat.GENERIC_XML, EventSource.SKIP_NO_FAILURE, null);
    If the myEvents folder contains three files: 123event.xml, 456text.txt, 789event.xml, then only the XML files 123event.xml and 789event.xml match the event pattern and 456text.txt is ignored.
  3. Submit the collection of events by using an iterator retrieved from your EventSource<File> instance. For example, the following code shows how to submit the events of the fileEventSource instance by passing the iterator to the submitEvents method:
    Map<String,RoutingStaus> results = testDriver.submitEvents(fileEventSource.iterator());
  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.

Results

Events are submitted in an order that is based on the implementation of the standard comparator for the Java File class. For more information, refer to the Java File class documentation.

Note: You can simplify how events are ordered by using a file naming convention, such as a standard prefix followed by an alphanumeric suffix. For example, payrollEvent-A1234.xml precedes payrollEvent-B1234.xml