BLPAPI C++  3.24.10
blpapi_testutil.h
Go to the documentation of this file.
1 /* Copyright 2019. Bloomberg Finance L.P.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions: The above
9  * copyright notice and this permission notice shall be included in all copies
10  * or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18  * IN THE SOFTWARE.
19  */
20 
28 #ifndef INCLUDED_BLPAPI_TESTUTIL
29 #define INCLUDED_BLPAPI_TESTUTIL
30 
128 #include <blpapi_correlationid.h>
129 #include <blpapi_datetime.h>
130 #include <blpapi_defs.h>
131 #include <blpapi_event.h>
132 #include <blpapi_messageformatter.h>
133 #include <blpapi_service.h>
134 #include <blpapi_streamproxy.h>
135 #include <blpapi_topic.h>
136 #include <blpapi_types.h>
137 
138 #ifdef __cplusplus
139 extern "C" {
140 #endif
141 
143 int blpapi_TestUtil_createEvent(blpapi_Event_t **event, int eventType);
144 
147  const char *schema, size_t schemaLength, blpapi_Service_t **service);
148 
151  void *stream,
152  const blpapi_Service_t *service);
153 
156  blpapi_Event_t *event,
157  const blpapi_SchemaElementDefinition_t *messageType,
158  const blpapi_MessageProperties_t *properties);
159 
162  blpapi_Topic_t **topic, const blpapi_Service_t *service, int isActive);
163 
167  blpapi_Name_t *messageName);
168 
171  blpapi_MessageProperties_t **messageProperties);
172 
175  blpapi_MessageProperties_t *messageProperties);
176 
179  const blpapi_MessageProperties_t *src);
180 
183  const blpapi_MessageProperties_t *rhs);
184 
187  blpapi_MessageProperties_t *messageProperties,
188  const blpapi_CorrelationId_t *correlationIds,
189  size_t numCorrelationIds);
190 
193  blpapi_MessageProperties_t *messageProperties,
194  int recap,
195  int fragment);
196 
199  blpapi_MessageProperties_t *messageProperties,
200  const blpapi_HighPrecisionDatetime_t *timestamp);
201 
204  blpapi_MessageProperties_t *messageProperties,
205  const blpapi_Service_t *service);
206 
209  blpapi_MessageProperties_t *messageProperties, const char *requestId);
210 
211 #ifdef __cplusplus
212 } // extern "C"
213 
214 #include <cassert>
215 #include <cstdio>
216 #include <fstream>
217 #include <vector>
218 
226 namespace BloombergLP {
227 namespace blpapi {
228 namespace test {
229 
230 // =======================
231 // class MessageProperties
232 // =======================
233 
242 
243  private:
244  blpapi_MessageProperties_t *d_handle_p;
245 
246  public:
247  // CREATORS
259  MessageProperties(const MessageProperties& original);
269  // MANIPULATORS
276  const blpapi_CorrelationId_t *correlationIds,
277  size_t correlationIdCount);
288  const std::vector<CorrelationId>& correlationIds);
295  MessageProperties& setCorrelationId(const CorrelationId& correlationId);
307  MessageProperties& setTimeReceived(const Datetime& timeReceived);
314  MessageProperties& setService(const Service& service);
319  MessageProperties& setRequestId(const char *requestId);
326  // ACCESSORS
332 };
333 
334 // ==============
335 // class TestUtil
336 // ==============
337 
345 class TestUtil {
346 
347  public:
348  // TYPES
349 
350  /*
351  * \deprecated Following typedef is provided for backwards compatibility.
352  * It will be removed in a future release.
353  */
355 
356  // CLASS METHODS
357  static Event createEvent(Event::EventType eventType);
366  static MessageFormatter appendMessage(Event& event,
367  const SchemaElementDefinition& elementDef,
368  const MessageProperties& properties = MessageProperties());
381  static Service deserializeService(std::istream& stream);
390  static void serializeService(std::ostream& stream, const Service& service);
397  static Topic createTopic(const Service& service, bool isActive = true);
406  const Name& messageName);
411 };
412 
416 // ============================================================================
417 // INLINE FUNCTION DEFINITIONS
418 // ============================================================================
419 
420 // --------------
421 // class TestUtil
422 // --------------
423 
425 {
426  blpapi_Event_t *event = 0;
428  BLPAPI_CALL(blpapi_TestUtil_createEvent)(&event, eventType));
429  return Event(event);
430 }
431 
432 inline Service TestUtil::deserializeService(std::istream& stream)
433 {
434  blpapi_Service_t *buffer = 0;
435 
436  // This is a workaround for SunOS compiler not supporting construction from
437  // 'std::istreambuf_iterator'.
438  std::string schema;
439  std::getline(stream, schema, '\0');
440 
443  schema.c_str(), schema.length(), &buffer));
444  return Service(buffer, Service::TestService());
445 }
446 
448  std::ostream& stream, const Service& service)
449 {
451  StreamProxyOstream::writeToStream, &stream, service.handle()));
452 }
453 
455  const SchemaElementDefinition& elementDef,
456  const MessageProperties& properties)
457 {
458  blpapi_MessageFormatter_t *formatter = 0;
460  &formatter, event.impl(), elementDef.impl(), properties.handle()));
461 
462  return MessageFormatter(formatter);
463 }
464 
465 inline Topic TestUtil::createTopic(const Service& service, bool isActive)
466 {
467  blpapi_Topic_t *topic = 0;
469  &topic, service.handle(), isActive));
470  return Topic(topic);
471 }
472 
474  const Name& messageName)
475 {
476  blpapi_SchemaElementDefinition_t *definition = 0;
479  &definition, messageName.impl()));
480  return SchemaElementDefinition(definition);
481 }
482 
484 {
487 }
488 
490 {
492  &d_handle_p, original.handle()));
493 }
494 
496  const MessageProperties& rhs)
497 {
499  d_handle_p, rhs.handle()));
500  return *this;
501 }
502 
504 {
506 }
507 
509 {
510  return d_handle_p;
511 }
512 
514  const CorrelationId& cid)
515 {
518  d_handle_p, &cid.impl(), 1));
519  return *this;
520 }
521 
523  const std::vector<CorrelationId>& cids)
524 {
525  assert(!cids.empty());
526 
529  d_handle_p, &cids[0].impl(), cids.size()));
530  return *this;
531 }
532 
534  const blpapi_CorrelationId_t *correlationIds,
535  size_t correlationIdCount)
536 {
539  d_handle_p, correlationIds, correlationIdCount));
540  return *this;
541 }
542 
544  Message::RecapType::Type recapType, Message::Fragment fragmentType)
545 {
548  static_cast<int>(recapType),
549  static_cast<int>(fragmentType)));
550  return *this;
551 }
552 
554  const Datetime& timeReceived)
555 {
558  d_handle_p, &timeReceived.rawHighPrecisionValue()));
559  return *this;
560 }
561 
563 {
566  d_handle_p, service.handle()));
567  return *this;
568 }
569 
571  const char *requestId)
572 {
574  blpapi_MessageProperties_setRequestId)(d_handle_p, requestId));
575  return *this;
576 }
577 
578 } // close namespace test
579 
580 // *Deprecated*
581 // Following typedef is provided for backwards compatibility. It will be
582 // removed in a future release.
584 
585 } // close namespace blpapi
586 } // close namespace BloombergLP
587 
588 #endif // #ifdef __cplusplus
589 #endif // #ifndef INCLUDED_BLPAPI_TESTUTIL
int blpapi_MessageProperties_setRecapType(blpapi_MessageProperties_t *messageProperties, int recap, int fragment)
int blpapi_TestUtil_deserializeService(const char *schema, size_t schemaLength, blpapi_Service_t **service)
Definition: blpapi_testutil.h:241
MessageProperties & setCorrelationIds(const blpapi_CorrelationId_t *correlationIds, size_t correlationIdCount)
Definition: blpapi_testutil.h:533
message is not fragmented
Definition: blpapi_message.h:173
MessageProperties()
Definition: blpapi_testutil.h:483
Common definitions used by the library.
struct blpapi_Event blpapi_Event_t
Definition: blpapi_types.h:139
MessageProperties & setTimeReceived(const Datetime &timeReceived)
Definition: blpapi_testutil.h:553
MessageProperties & setRecapType(Message::RecapType::Type recapType, Message::Fragment fragmentType=Message::FRAGMENT_NONE)
Definition: blpapi_testutil.h:543
Represents a date and/or time.
Type
Definition: blpapi_message.h:197
test::TestUtil TestUtil
Definition: blpapi_testutil.h:583
Definition: blpapi_abstractsession.h:187
Definition: blpapi_name.h:150
#define BLPAPI_CALL(FUNCNAME)
Definition: blpapi_call.h:353
Definition: blpapi_service.h:304
int blpapi_TestUtil_getAdminMessageDefinition(blpapi_SchemaElementDefinition_t **definition, blpapi_Name_t *messageName)
int blpapi_MessageProperties_create(blpapi_MessageProperties_t **messageProperties)
Definition: blpapi_messageformatter.h:258
Definition: blpapi_service.h:313
blpapi_Name_t * impl() const
Definition: blpapi_name.h:403
int blpapi_TestUtil_createTopic(blpapi_Topic_t **topic, const blpapi_Service_t *service, int isActive)
struct blpapi_Name blpapi_Name_t
Definition: blpapi_types.h:154
static Service deserializeService(std::istream &stream)
Definition: blpapi_testutil.h:432
blpapi_Service_t * handle() const
Definition: blpapi_service.h:706
Definition: blpapi_schema.h:327
int(* blpapi_StreamWriter_t)(const char *data, int length, void *stream)
Definition: blpapi_streamproxy.h:53
A service which provides access to API data (provide or consume).
A component which defines events related operations.
Provide a key to identify individual subscriptions or requests.
EventType
The possible types of event.
Definition: blpapi_event.h:202
test::MessageProperties MessageProperties
Definition: blpapi_testutil.h:354
struct blpapi_SchemaElementDefinition blpapi_SchemaElementDefinition_t
Definition: blpapi_types.h:191
static void throwOnError(int errorCode)
Definition: blpapi_exception.h:526
static void serializeService(std::ostream &stream, const Service &service)
Definition: blpapi_testutil.h:447
~MessageProperties()
Definition: blpapi_testutil.h:503
test::MessageFormatter MessageFormatter
Definition: blpapi_messageformatter.h:833
MessageProperties & setService(const Service &service)
Definition: blpapi_testutil.h:562
int blpapi_TestUtil_serializeService(blpapi_StreamWriter_t streamWriter, void *stream, const blpapi_Service_t *service)
int blpapi_MessageProperties_setTimeReceived(blpapi_MessageProperties_t *messageProperties, const blpapi_HighPrecisionDatetime_t *timestamp)
Definition: blpapi_correlationid.h:201
Provide representation of a Topic.
blpapi_MessageProperties_t * handle() const
Definition: blpapi_testutil.h:508
struct blpapi_Topic blpapi_Topic_t
Definition: blpapi_dispatchtbl.h:75
struct blpapi_MessageProperties blpapi_MessageProperties_t
Definition: blpapi_types.h:200
int blpapi_MessageProperties_assign(blpapi_MessageProperties_t *lhs, const blpapi_MessageProperties_t *rhs)
int blpapi_MessageProperties_copy(blpapi_MessageProperties_t **dest, const blpapi_MessageProperties_t *src)
static SchemaElementDefinition getAdminMessageDefinition(const Name &messageName)
Definition: blpapi_testutil.h:473
int blpapi_TestUtil_appendMessage(blpapi_MessageFormatter_t **formatter, blpapi_Event_t *event, const blpapi_SchemaElementDefinition_t *messageType, const blpapi_MessageProperties_t *properties)
blpapi_HighPrecisionDatetime_t & rawHighPrecisionValue()
Definition: blpapi_datetime.h:1954
A signature for callback on print and default C++ implementation.
Definition: blpapi_topic.h:114
Fragment
Definition: blpapi_message.h:171
blpapi_SchemaElementDefinition_t * impl() const
Definition: blpapi_schema.h:744
Definition: blpapi_event.h:196
static Topic createTopic(const Service &service, bool isActive=true)
Definition: blpapi_testutil.h:465
Format messages for publishing.
#define BLPAPI_CALL_UNCHECKED(FUNCNAME)
Definition: blpapi_call.h:354
int blpapi_TestUtil_createEvent(blpapi_Event_t **event, int eventType)
static int writeToStream(const char *data, int length, void *stream)
Definition: blpapi_streamproxy.h:93
MessageProperties & operator=(const MessageProperties &rhs)
Definition: blpapi_testutil.h:495
MessageProperties & setCorrelationId(const CorrelationId &correlationId)
Definition: blpapi_testutil.h:513
struct blpapi_MessageFormatter blpapi_MessageFormatter_t
Definition: blpapi_types.h:197
#define BLPAPI_EXPORT
Definition: blpapi_defs.h:171
struct blpapi_Service blpapi_Service_t
Definition: blpapi_types.h:166
static Event createEvent(Event::EventType eventType)
Definition: blpapi_testutil.h:424
void blpapi_MessageProperties_destroy(blpapi_MessageProperties_t *messageProperties)
Definition: blpapi_testutil.h:345
Definition: blpapi_datetime.h:245
int blpapi_MessageProperties_setService(blpapi_MessageProperties_t *messageProperties, const blpapi_Service_t *service)
int blpapi_MessageProperties_setCorrelationIds(blpapi_MessageProperties_t *messageProperties, const blpapi_CorrelationId_t *correlationIds, size_t numCorrelationIds)
blpapi_Event_t * impl() const
Definition: blpapi_event.h:553
Provide BLPAPI types.
MessageProperties & setRequestId(const char *requestId)
Definition: blpapi_testutil.h:570
static MessageFormatter appendMessage(Event &event, const SchemaElementDefinition &elementDef, const MessageProperties &properties=MessageProperties())
Definition: blpapi_testutil.h:454
Definition: blpapi_datetime.h:115
int blpapi_MessageProperties_setRequestId(blpapi_MessageProperties_t *messageProperties, const char *requestId)