Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 1 | /* -*- 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/manager-base.hpp" |
| 8 | #include "mgmt/internal-face.hpp" |
| 9 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 10 | #include <boost/test/unit_test.hpp> |
| 11 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | NFD_LOG_INIT("ManagerBaseTest"); |
| 15 | |
| 16 | BOOST_AUTO_TEST_SUITE(MgmtManagerBase) |
| 17 | |
| 18 | class ManagerBaseTest : public ManagerBase |
| 19 | { |
| 20 | |
| 21 | public: |
| 22 | |
| 23 | ManagerBaseTest() |
| 24 | : ManagerBase(make_shared<InternalFace>()), |
| 25 | m_callbackFired(false) |
| 26 | { |
| 27 | |
| 28 | } |
| 29 | |
| 30 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 31 | testSetResponse(ControlResponse& response, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 32 | uint32_t code, |
| 33 | const std::string& text) |
| 34 | { |
| 35 | setResponse(response, code, text); |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | testSendResponse(const Name& name, |
| 40 | uint32_t code, |
| 41 | const std::string& text) |
| 42 | { |
| 43 | sendResponse(name, code, text); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | testSendResponse(const Name& name, |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 48 | const ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 49 | { |
| 50 | sendResponse(name, response); |
| 51 | } |
| 52 | |
| 53 | shared_ptr<InternalFace> |
| 54 | getInternalFace() |
| 55 | { |
| 56 | return ndn::ptr_lib::static_pointer_cast<InternalFace>(m_face); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | validateControlResponse(const Data& response, |
| 61 | const Name& expectedName, |
| 62 | uint32_t expectedCode, |
| 63 | const std::string& expectedText) |
| 64 | { |
| 65 | m_callbackFired = true; |
| 66 | Block controlRaw = response.getContent().blockFromValue(); |
| 67 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 68 | ControlResponse control; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 69 | control.wireDecode(controlRaw); |
| 70 | |
| 71 | NFD_LOG_DEBUG("received control response" |
| 72 | << " name: " << response.getName() |
| 73 | << " code: " << control.getCode() |
| 74 | << " text: " << control.getText()); |
| 75 | |
| 76 | BOOST_REQUIRE(response.getName() == expectedName); |
| 77 | BOOST_REQUIRE(control.getCode() == expectedCode); |
| 78 | BOOST_REQUIRE(control.getText() == expectedText); |
| 79 | } |
| 80 | |
| 81 | bool |
| 82 | didCallbackFire() |
| 83 | { |
| 84 | return m_callbackFired; |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | |
| 89 | bool m_callbackFired; |
| 90 | |
| 91 | }; |
| 92 | |
| 93 | BOOST_FIXTURE_TEST_CASE(SetResponse, ManagerBaseTest) |
| 94 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 95 | ControlResponse response(200, "OK"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 96 | |
| 97 | BOOST_CHECK_EQUAL(response.getCode(), 200); |
| 98 | BOOST_CHECK_EQUAL(response.getText(), "OK"); |
| 99 | |
| 100 | testSetResponse(response, 100, "test"); |
| 101 | |
| 102 | BOOST_CHECK_EQUAL(response.getCode(), 100); |
| 103 | BOOST_CHECK_EQUAL(response.getText(), "test"); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | BOOST_FIXTURE_TEST_CASE(SendResponse3Arg, ManagerBaseTest) |
| 108 | { |
| 109 | getInternalFace()->onReceiveData += |
| 110 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 111 | "/response", 100, "test"); |
| 112 | |
| 113 | testSendResponse("/response", 100, "test"); |
| 114 | BOOST_REQUIRE(didCallbackFire()); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | BOOST_FIXTURE_TEST_CASE(SendResponse2Arg, ManagerBaseTest) |
| 119 | { |
| 120 | getInternalFace()->onReceiveData += |
| 121 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 122 | "/response", 100, "test"); |
| 123 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 124 | ControlResponse response(100, "test"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 125 | |
| 126 | testSendResponse("/response", 100, "test"); |
| 127 | BOOST_REQUIRE(didCallbackFire()); |
| 128 | } |
| 129 | |
| 130 | BOOST_AUTO_TEST_SUITE_END() |
| 131 | |
| 132 | } // namespace nfd |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |