Class TestUtil
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic MessageFormatter
appendMessage
(Event event, SchemaElementDefinition elementDef) Creates a new message and appends it to the specifiedEvent
with defaultMessageProperties
.static MessageFormatter
appendMessage
(Event event, SchemaElementDefinition elementDef, MessageProperties properties) Creates a new message and appends it to the specifiedEvent
.static Event
createEvent
(Event.EventType eventType) Creates and returns anEvent
to be used for testing with the specifiedeventType
.static Topic
createTopic
(Service service, boolean isActive) Creates a validTopic
with the specifiedservice
to support testing publishers.static Service
deserializeService
(InputStream inputStream) Creates aService
object from the specifiedinputStream
.static SchemaElementDefinition
getAdminMessageDefinition
(Name messageName) Returns the definition for an admin message of the specifiedmessageName
.
-
Constructor Details
-
TestUtil
public TestUtil()
-
-
Method Details
-
createEvent
Creates and returns anEvent
to be used for testing with the specifiedeventType
. The returnedEvent
cannot be used for publishing. The behavior is undefined ifEventFormatter
is used with the returnedEvent
.- Parameters:
eventType
- anEvent.EventType
type of event.- Returns:
- an
Event
of the requested type.
-
appendMessage
Creates a new message and appends it to the specifiedEvent
with defaultMessageProperties
.- Parameters:
event
- anEvent
created fromcreateEvent(com.bloomberglp.blpapi.Event.EventType)
.elementDef
- aSchemaElementDefinition
that provides schema for the messages to be encoded.- Throws:
IllegalArgumentException
- when fails to append message.- See Also:
-
appendMessage
public static MessageFormatter appendMessage(Event event, SchemaElementDefinition elementDef, MessageProperties properties) Creates a new message and appends it to the specifiedEvent
. Returns aMessageFormatter
to format the last appended message. The specifiedEvent
must be a testEvent
created fromcreateEvent(com.bloomberglp.blpapi.Event.EventType)
.elementDef
is used to verify and encode the contents of the message and the specifiedproperties
are used to set the metadata properties for the message. An exception is thrown if the method fails to append the message.- Parameters:
event
- anEvent
created fromcreateEvent(com.bloomberglp.blpapi.Event.EventType)
.elementDef
- aSchemaElementDefinition
that provides schema for the messages to be encoded.properties
- aMessageProperties
that represents the metadata to be added to the appended message.- Throws:
IllegalArgumentException
- when fails to append message.
-
deserializeService
Creates aService
object from the specifiedinputStream
. The format of theinputStream
must beXML
. TheinputStream
should only containASCII
characters without any embeddednull
characters. Returns theService
object on success or throws on failure.- Parameters:
inputStream
- anInputStream
that provides anXML
,ASCII
representation ofService
.- Throws:
IllegalArgumentException
- wheninputStream
is null.RuntimeException
- when fails to deserialize.
-
createTopic
Creates a validTopic
with the specifiedservice
to support testing publishers. The expected use case is to support returning a customTopic
while mockingProviderSession.getTopic(com.bloomberglp.blpapi.Message)
methods. The behavior is undefined ifservice
is not created fromdeserializeService(InputStream)
. -
getAdminMessageDefinition
Returns the definition for an admin message of the specifiedmessageName
.- Parameters:
messageName
- name of the requested message.- Throws:
IllegalArgumentException
- if message name isnull
.NotFoundException
- if the schema definition for themessageName
is not found.
-