blob: 1883a8e22596c72bc214f518656722921806e2c8 [file] [log] [blame]
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "mgmt/notification-stream.hpp"
8#include "mgmt/internal-face.hpp"
9
10#include "tests/test-common.hpp"
11
12
13namespace nfd {
14namespace tests {
15
16NFD_LOG_INIT("NotificationStreamTest");
17
18
19
20class NotificationStreamFixture : public BaseFixture
21{
22public:
23 NotificationStreamFixture()
24 : m_callbackFired(false)
25 , m_prefix("/localhost/nfd/NotificationStreamTest")
26 , m_message("TestNotificationMessage")
27 , m_sequenceNo(0)
28 {
29 }
30
31 virtual
32 ~NotificationStreamFixture()
33 {
34 }
35
36 void
37 validateCallback(const Data& data)
38 {
39 Name expectedName(m_prefix);
40 expectedName.appendSegment(m_sequenceNo);
41 BOOST_REQUIRE_EQUAL(data.getName(), expectedName);
42
43 ndn::Block payload = data.getContent();
44 std::string message;
45
46 message.append(reinterpret_cast<const char*>(payload.value()), payload.value_size());
47
48 BOOST_REQUIRE_EQUAL(message, m_message);
49
50 m_callbackFired = true;
51 ++m_sequenceNo;
52 }
53
54 void
55 resetCallbackFired()
56 {
57 m_callbackFired = false;
58 }
59
60protected:
61 bool m_callbackFired;
62 const std::string m_prefix;
63 const std::string m_message;
64 uint64_t m_sequenceNo;
65};
66
67BOOST_FIXTURE_TEST_SUITE(MgmtNotificationStream, NotificationStreamFixture)
68
69class TestNotification
70{
71public:
72 TestNotification(const std::string& message)
73 : m_message(message)
74 {
75 }
76
77 ~TestNotification()
78 {
79 }
80
81 Block
82 wireEncode() const
83 {
84 ndn::EncodingBuffer buffer;
85
86 prependByteArrayBlock(buffer,
87 ndn::Tlv::Content,
88 reinterpret_cast<const uint8_t*>(m_message.c_str()),
89 m_message.size());
90 return buffer.block();
91 }
92
93private:
94 const std::string m_message;
95};
96
97BOOST_AUTO_TEST_CASE(TestPostEvent)
98{
99 shared_ptr<InternalFace> face(make_shared<InternalFace>());
100 NotificationStream notificationStream(face, "/localhost/nfd/NotificationStreamTest");
101
102 face->onReceiveData += bind(&NotificationStreamFixture::validateCallback, this, _1);
103
104 TestNotification event1(m_message);
105 notificationStream.postNotification(event1);
106
107 BOOST_REQUIRE(m_callbackFired);
108
109 resetCallbackFired();
110
111 TestNotification event2(m_message);
112 notificationStream.postNotification(event2);
113
114 BOOST_REQUIRE(m_callbackFired);
115}
116
117
118BOOST_AUTO_TEST_SUITE_END()
119
120
121} // namespace tests
122} // namespace nfd
123