public final class TestUtil extends Object
MessageFormatter) provides static utility for
unit testing BLPAPI applications.
This component defines a class, TestUtil, that provides helpers
unit-testing BLPAPI. This includes deserializing services, creating test
events, and appending/formatting messages in these events.
Usage
-----
Example: Create subscription data with correlation id:
- - - - - - - - - - - - - - - - - - - - - - - - - - -
For the purpose of unit testing the behavior of tick processing, create
a subscription data event containing a single tick for a given subscription,
identified with a correlation id CorrelationID that is available in
the test.
First create the service from a string:
String schema = "<ServiceDefinition name=...>...</ServiceDefinition>";
InputStream stream = new ByteArrayInputStream(
schema.getBytes(Charset.forName("UTF-8")));
Service service = TestUtil.deserializeService(stream);
// Then create a subscription event
Event event = TestUtil.createEvent(Event.EventType.SUBSCRIPTION_DATA);
// Prepare the message properties:
MessageProperties properties = new MessageProperties();
properties.setCorrelationId(correlationId);
// Then, obtain the definition for the message from the schema:
Name msgName = new Name("MarketDataEvents");
SchemaElementDefinition def = service.getEventDefinition(msgName);
// Now append a message and obtain a formatter:
MessageFormatter fmt = TestUtil.appendMessage(event, def, properties);
The formatter can now be used to fill in the data to be used in the test.
Note that service creation and the definitions for the ticks can be shared by all of the unit tests that need to create the same type of events.
| Constructor and Description |
|---|
TestUtil() |
| Modifier and Type | Method and Description |
|---|---|
static MessageFormatter |
appendMessage(Event event,
SchemaElementDefinition elementDef)
Creates a new message and appends it to the specified
Event with
default MessageProperties. |
static MessageFormatter |
appendMessage(Event event,
SchemaElementDefinition elementDef,
MessageProperties properties)
Creates a new message and appends it to the specified
Event. |
static Event |
createEvent(Event.EventType eventType)
Creates and returns an
Event to be used for testing with the
specified eventType. |
static Topic |
createTopic(Service service,
boolean isActive)
Creates a valid
Topic with the specified service to
support testing publishers. |
static Service |
deserializeService(InputStream inputStream)
Creates a
Service object from the specified inputStream. |
static SchemaElementDefinition |
getAdminMessageDefinition(Name messageName)
Returns the definition for an admin message of the specified
messageName. |
public static Event createEvent(Event.EventType eventType)
Event to be used for testing with the
specified eventType. The returned Event cannot be
used for publishing. The behavior is undefined if EventFormatter
is used with the returned Event.eventType - an Event.EventType type of event.Event of the requested type.public static MessageFormatter appendMessage(Event event, SchemaElementDefinition elementDef)
Event with
default MessageProperties.event - an Event created from createEvent(com.bloomberglp.blpapi.Event.EventType).elementDef - a SchemaElementDefinition that provides schema for the
messages to be encoded.IllegalArgumentException - when fails to append message.appendMessage(Event, SchemaElementDefinition, MessageProperties)public static MessageFormatter appendMessage(Event event, SchemaElementDefinition elementDef, MessageProperties properties)
Event.
Returns a MessageFormatter to format the last appended message.
The specified Event must be a test Event created from
createEvent(com.bloomberglp.blpapi.Event.EventType). elementDef is used to verify and
encode the contents of the message and the specified properties
are used to set the metadata properties for the message. An exception is
thrown if the method fails to append the message.event - an Event created from createEvent(com.bloomberglp.blpapi.Event.EventType).elementDef - a SchemaElementDefinition that provides schema for the
messages to be encoded.properties - a MessageProperties that represents the metadata to be
added to the appended message.IllegalArgumentException - when fails to append message.public static Service deserializeService(InputStream inputStream)
Service object from the specified inputStream.
The format of the inputStream must be XML. The
inputStream should only contain ASCII characters without
any embedded null characters. Returns the Service object
on success or throws on failure.inputStream - an InputStream that provides an XML,
ASCII representation of Service.IllegalArgumentException - when inputStream is null.RuntimeException - when fails to deserialize.public static Topic createTopic(Service service, boolean isActive)
Topic with the specified service to
support testing publishers. The expected use case is to support
returning a custom Topic while mocking ProviderSession.getTopic(com.bloomberglp.blpapi.Message)
methods. The behavior is undefined if service is not created
from deserializeService(InputStream).public static SchemaElementDefinition getAdminMessageDefinition(Name messageName)
messageName.messageName - name of the requested message.IllegalArgumentException - if message name is null.NotFoundException - if the schema definition for the messageName is not
found.Copyright © 2021 Bloomberg L.P.. All rights reserved.