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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic MessageFormatterappendMessage(Event event, SchemaElementDefinition elementDef) Creates a new message and appends it to the specifiedEventwith defaultMessageProperties.static MessageFormatterappendMessage(Event event, SchemaElementDefinition elementDef, MessageProperties properties) Creates a new message and appends it to the specifiedEvent.static EventcreateEvent(Event.EventType eventType) Creates and returns anEventto be used for testing with the specifiedeventType.static TopiccreateTopic(Service service, boolean isActive) Creates a validTopicwith the specifiedserviceto support testing publishers.static ServicedeserializeService(InputStream inputStream) Creates aServiceobject from the specifiedinputStream.static SchemaElementDefinitiongetAdminMessageDefinition(Name messageName) Returns the definition for an admin message of the specifiedmessageName.
-
Constructor Details
-
TestUtil
public TestUtil()
-
-
Method Details
-
createEvent
Creates and returns anEventto be used for testing with the specifiedeventType. The returnedEventcannot be used for publishing. The behavior is undefined ifEventFormatteris used with the returnedEvent.- Parameters:
eventType- anEvent.EventTypetype of event.- Returns:
- an
Eventof the requested type.
-
appendMessage
Creates a new message and appends it to the specifiedEventwith defaultMessageProperties.- Parameters:
event- anEventcreated fromcreateEvent(com.bloomberglp.blpapi.Event.EventType).elementDef- aSchemaElementDefinitionthat 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 aMessageFormatterto format the last appended message. The specifiedEventmust be a testEventcreated fromcreateEvent(com.bloomberglp.blpapi.Event.EventType).elementDefis used to verify and encode the contents of the message and the specifiedpropertiesare used to set the metadata properties for the message. An exception is thrown if the method fails to append the message.- Parameters:
event- anEventcreated fromcreateEvent(com.bloomberglp.blpapi.Event.EventType).elementDef- aSchemaElementDefinitionthat provides schema for the messages to be encoded.properties- aMessagePropertiesthat represents the metadata to be added to the appended message.- Throws:
IllegalArgumentException- when fails to append message.
-
deserializeService
Creates aServiceobject from the specifiedinputStream. The format of theinputStreammust beXML. TheinputStreamshould only containASCIIcharacters without any embeddednullcharacters. Returns theServiceobject on success or throws on failure.- Parameters:
inputStream- anInputStreamthat provides anXML,ASCIIrepresentation ofService.- Throws:
IllegalArgumentException- wheninputStreamis null.RuntimeException- when fails to deserialize.
-
createTopic
Creates a validTopicwith the specifiedserviceto support testing publishers. The expected use case is to support returning a customTopicwhile mockingProviderSession.getTopic(com.bloomberglp.blpapi.Message)methods. The behavior is undefined ifserviceis 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 themessageNameis not found.
-