Submitting past, present, and future events

You can test the business logic of a solution by using the test driver submitEvent(Event event) method. A test client is one way that you can submit events.

Before you begin

You must create a test client project before you can submit test events by using the TestDriver class.

About this task

Use the EventFactory API to create events, and then submit the events in a TestDriver.submitEvent method.

To test events that have time stamps in the past, add the setTestEpoch method to your test client. The value that is specified in the method sets the start of the test to a point in the past. The TestEpoch time must be the same as, or before, the time stamp of the earliest event that you want to test.

To trigger events that have time stamps in the future after all of your client tests are complete, use the endTest method.

Procedure

  1. Import the Java™ packages for your model into the .java file that contains the TestDriver class in the test client project. For example:
    import com.example.model.CreateCartEvent;
    import com.example.model.Customer;
    import com.example.model.CreateCartEntity;
    import com.example.model.ShoppingCart;
  2. Create an event by using the EventFactory API. For example, the following code creates an event type CreateCartEvent.
    CreateCartEvent createCartEvent = testDriver.getEventFactory().createEvent(CreateCartEvent.class);
    createCartEvent.setShoppingCartId(shoppingCartId);
    Relationship<Customer> customerRel = testDriver.getEventFactory().createRelationship(Customer.class, customerId);
    createCartEvent.setCustomer(customerRel);
  3. Send the event in a TestDriver.submitEvent method. For example, the following code submits the createCartEvent instance.
    testDriver.submitEvent(createCartEvent);
  4. Check that the entity is created as a result of submitting the event.

    For example, the following code checks that the ShoppingCart entity is created.

    if (testDriver.waitUntilSolutionIdle(10) == true) {
        ShoppingCart cart =
        testDriver.fetchEntity(ShoppingCart.class, shoppingCartId);  
        assertNotNull(cart);
    }
  5. Add any business logic in a test method that you want to run. For example, the following code checks that the shopping cart contains a Cola product.
    List <Relationship<Product>> products = cart.getProducts();
    	Product product = testDriver.fetchEntity(Product.class, products.get(0).getKey());
    	assertEquals("Cola", product.getId());
  6. Use the processPendingSchedules method to trigger the evaluation of a global event aggregate. For example, if you have an aggregate that counts the number of events during the last hour, you can call processPendingSchedules() to initiate the calculation of the aggregate value.
  7. Add the test setTestEpoch method to run tests that include events with time stamps that are in the past. The TestEpoch setting establishes a fixed point in time for the beginning of the test so that you can include past events. For example:
    testDriver.setTestEpoch("2015-01-05T14:30:00Z");
    The format for the string is any valid ZonedDateTime. To clear the setting after testing is complete, add the clearTestEpoch method.
    Restriction: Use the TestEpoch setting only for testing purposes. The setting is not intended for use on production servers.

    Some unexpected test results might occur when you use the TestEpoch setting with global entity aggregates, because batch job processing uses the time stamp of the scheduling event instead of the TestEpoch setting.

  8. Use the tearDownAfterClass and tearDown methods to call the test driver endTest(). This method processes all triggered events with future time stamps.

    Triggered events are normally processed in order according to the event time stamp, but the test driver endTest initiates immediate completion of asynchronous time operations for test purposes. This method allows tests to run at full test speed for triggered events with time stamps in the future, while still processing time operations as if they happened in normal time.

    Important: Events that are sent by a test client after the endTest() method is called are not run at full speed. Because the method is at the end of the test class, this is an issue only if there is a long processing queue. To avoid such issues, add time in the test code to make sure that all events are processed before the endTest method is called.
  9. In Insight Designer, run the test client as a JUnit test.

What to do next

After you run the test client project, use the administrator tools to validate the arrival of the events and the creation of the entities. The following URL examples show how you can validate the Product and ShoppingCart entities are created:

  • http://localhost:9080/ibm/ia/rest/solutions/MySolution/entity-types/com.solution.model.Product/entities
  • http://localhost:9080/ibm/ia/rest/solutions/MySolution/entity-types/com.solution.model.ShoppingCart/entities