mgmt: FaceEventNotification equality operators and formatted output

Change-Id: I92aca47a2e3823592d1c0bef07a42dbba5c6cb91
Refs: #3903
diff --git a/src/mgmt/nfd/face-event-notification.cpp b/src/mgmt/nfd/face-event-notification.cpp
index f3ea655..81ef384 100644
--- a/src/mgmt/nfd/face-event-notification.cpp
+++ b/src/mgmt/nfd/face-event-notification.cpp
@@ -25,14 +25,12 @@
 #include "encoding/tlv-nfd.hpp"
 #include "util/concepts.hpp"
 
+#include <iomanip>
+
 namespace ndn {
 namespace nfd {
 
-//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceEventNotification>));
-BOOST_CONCEPT_ASSERT((WireEncodable<FaceEventNotification>));
-BOOST_CONCEPT_ASSERT((WireDecodable<FaceEventNotification>));
-static_assert(std::is_base_of<tlv::Error, FaceEventNotification::Error>::value,
-              "FaceEventNotification::Error must inherit from tlv::Error");
+BOOST_CONCEPT_ASSERT((NotificationStreamItem<FaceEventNotification>));
 
 FaceEventNotification::FaceEventNotification()
   : m_kind(FACE_EVENT_NONE)
@@ -59,9 +57,9 @@
   totalLength += prependNonNegativeIntegerBlock(encoder,
                  tlv::nfd::FaceScope, m_faceScope);
   totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
-                 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
+                 reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
   totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
-                 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
+                 reinterpret_cast<const uint8_t*>(m_remoteUri.data()), m_remoteUri.size());
   totalLength += prependNonNegativeIntegerBlock(encoder,
                  tlv::nfd::FaceId, m_faceId);
   totalLength += prependNonNegativeIntegerBlock(encoder,
@@ -72,6 +70,12 @@
   return totalLength;
 }
 
+template size_t
+FaceEventNotification::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
+
+template size_t
+FaceEventNotification::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
+
 const Block&
 FaceEventNotification::wireEncode() const
 {
@@ -156,6 +160,7 @@
 
   if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
     m_flags = readNonNegativeInteger(*val);
+    ++val;
   }
   else {
     BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
@@ -170,42 +175,55 @@
   return *this;
 }
 
+bool
+operator==(const FaceEventNotification& a, const FaceEventNotification& b)
+{
+  return a.getFaceId() == b.getFaceId() &&
+      a.getRemoteUri() == b.getRemoteUri() &&
+      a.getLocalUri() == b.getLocalUri() &&
+      a.getFaceScope() == b.getFaceScope() &&
+      a.getFacePersistency() == b.getFacePersistency() &&
+      a.getLinkType() == b.getLinkType() &&
+      a.getFlags() == b.getFlags() &&
+      a.getKind() == b.getKind();
+}
+
+std::ostream&
+operator<<(std::ostream& os, FaceEventKind kind)
+{
+  switch (kind) {
+    case FACE_EVENT_NONE:
+      return os << "none";
+    case FACE_EVENT_CREATED:
+      return os << "created";
+    case FACE_EVENT_DESTROYED:
+      return os << "destroyed";
+    case FACE_EVENT_UP:
+      return os << "up";
+    case FACE_EVENT_DOWN:
+      return os << "down";
+  }
+  return os << static_cast<unsigned>(kind);
+}
+
 std::ostream&
 operator<<(std::ostream& os, const FaceEventNotification& notification)
 {
-  os << "FaceEventNotification(";
-
-  switch (notification.getKind()) {
-    case FACE_EVENT_NONE:
-      os << "Kind: none, ";
-      break;
-    case FACE_EVENT_CREATED:
-      os << "Kind: created, ";
-      break;
-    case FACE_EVENT_DESTROYED:
-      os << "Kind: destroyed, ";
-      break;
-    case FACE_EVENT_UP:
-      os << "Kind: up, ";
-      break;
-    case FACE_EVENT_DOWN:
-      os << "Kind: down, ";
-      break;
-  }
-
-  os << "FaceID: " << notification.getFaceId() << ", "
-     << "RemoteUri: " << notification.getRemoteUri() << ", "
-     << "LocalUri: " << notification.getLocalUri() << ", "
-     << "FaceScope: " << notification.getFaceScope() << ", "
-     << "FacePersistency: " << notification.getFacePersistency() << ", "
-     << "LinkType: " << notification.getLinkType() << ", ";
+  os << "FaceEvent(Kind: " << notification.getKind() << ",\n"
+     << "          FaceId: " << notification.getFaceId() << ",\n"
+     << "          RemoteUri: " << notification.getRemoteUri() << ",\n"
+     << "          LocalUri: " << notification.getLocalUri() << ",\n"
+     << "          FaceScope: " << notification.getFaceScope() << ",\n"
+     << "          FacePersistency: " << notification.getFacePersistency() << ",\n"
+     << "          LinkType: " << notification.getLinkType() << ",\n";
 
   auto osFlags = os.flags();
-  os << "Flags: " << std::showbase << std::hex << notification.getFlags();
+  // std::showbase doesn't work with number 0
+  os << "          Flags: 0x" << std::noshowbase << std::noshowpos << std::nouppercase
+     << std::hex << notification.getFlags() << "\n";
   os.flags(osFlags);
 
-  os << ")";
-  return os;
+  return os << "          )";
 }
 
 } // namespace nfd
diff --git a/src/mgmt/nfd/face-event-notification.hpp b/src/mgmt/nfd/face-event-notification.hpp
index 6abfee1..06836e7 100644
--- a/src/mgmt/nfd/face-event-notification.hpp
+++ b/src/mgmt/nfd/face-event-notification.hpp
@@ -38,6 +38,9 @@
   FACE_EVENT_DOWN = 4 ///< face went DOWN (from UP state)
 };
 
+std::ostream&
+operator<<(std::ostream& os, FaceEventKind kind);
+
 /**
  * \ingroup management
  * \brief represents a Face status change notification
@@ -81,6 +84,15 @@
   FaceEventKind m_kind;
 };
 
+bool
+operator==(const FaceEventNotification& a, const FaceEventNotification& b);
+
+inline bool
+operator!=(const FaceEventNotification& a, const FaceEventNotification& b)
+{
+  return !(a == b);
+}
+
 std::ostream&
 operator<<(std::ostream& os, const FaceEventNotification& notification);
 
diff --git a/src/util/concepts.hpp b/src/util/concepts.hpp
index 20d857e..bb8c744 100644
--- a/src/util/concepts.hpp
+++ b/src/util/concepts.hpp
@@ -106,16 +106,15 @@
   }
 };
 
-/** \brief a concept check for a Status Dataset item
- *  \sa https://redmine.named-data.net/projects/nfd/wiki/StatusDataset
- */
+namespace detail {
+
 template<class X>
-class StatusDatasetItem : public WireEncodable<X>
-                        , public WireEncodableWithEncodingBuffer<X>
-                        , public WireDecodable<X>
+class NfdMgmtProtocolStruct : public WireEncodable<X>
+                            , public WireEncodableWithEncodingBuffer<X>
+                            , public WireDecodable<X>
 {
 public:
-  BOOST_CONCEPT_USAGE(StatusDatasetItem)
+  BOOST_CONCEPT_USAGE(NfdMgmtProtocolStruct)
   {
     static_assert(std::is_default_constructible<X>::value, "");
     static_assert(boost::has_equal_to<X, X, bool>::value, "");
@@ -125,6 +124,24 @@
   }
 };
 
+} // namespace detail
+
+/** \brief concept check for an item in a Status Dataset
+ *  \sa https://redmine.named-data.net/projects/nfd/wiki/StatusDataset
+ */
+template<class X>
+class StatusDatasetItem : public detail::NfdMgmtProtocolStruct<X>
+{
+};
+
+/** \brief concept check for an item in a Notification Stream
+ *  \sa https://redmine.named-data.net/projects/nfd/wiki/Notification
+ */
+template<class X>
+class NotificationStreamItem : public detail::NfdMgmtProtocolStruct<X>
+{
+};
+
 // NDN_CXX_ASSERT_DEFAULT_CONSTRUCTIBLE and NDN_CXX_ASSERT_FORWARD_ITERATOR
 // originally written as part of NFD codebase
 
diff --git a/tests/unit-tests/mgmt/nfd/face-event-notification.t.cpp b/tests/unit-tests/mgmt/nfd/face-event-notification.t.cpp
index 09f024b..249cd63 100644
--- a/tests/unit-tests/mgmt/nfd/face-event-notification.t.cpp
+++ b/tests/unit-tests/mgmt/nfd/face-event-notification.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -22,6 +22,7 @@
 #include "mgmt/nfd/face-event-notification.hpp"
 
 #include "boost-test.hpp"
+#include <boost/lexical_cast.hpp>
 
 namespace ndn {
 namespace nfd {
@@ -34,7 +35,6 @@
 BOOST_AUTO_TEST_CASE(Traits)
 {
   FaceEventNotification notification;
-
   BOOST_CHECK_EQUAL(notification.getFaceScope(), FACE_SCOPE_NON_LOCAL);
   BOOST_CHECK_EQUAL(notification.getFacePersistency(), FACE_PERSISTENCY_PERSISTENT);
   BOOST_CHECK_EQUAL(notification.getLinkType(), LINK_TYPE_POINT_TO_POINT);
@@ -60,7 +60,7 @@
   BOOST_CHECK_EQUAL(notification.getLinkType(), LINK_TYPE_MULTI_ACCESS);
 }
 
-BOOST_AUTO_TEST_CASE(EncodeCreated)
+BOOST_AUTO_TEST_CASE(Created)
 {
   FaceEventNotification notification1;
   notification1.setKind(FACE_EVENT_CREATED)
@@ -71,8 +71,7 @@
                .setFacePersistency(FACE_PERSISTENCY_ON_DEMAND)
                .setLinkType(LINK_TYPE_MULTI_ACCESS)
                .setFlags(0x3);
-  Block wire;
-  BOOST_REQUIRE_NO_THROW(wire = notification1.wireEncode());
+  Block wire = notification1.wireEncode();
 
   // These octets are obtained by the snippet below.
   // This check is intended to detect unexpected encoding change in the future.
@@ -88,34 +87,25 @@
     0x3a, 0x36, 0x33, 0x36, 0x33, 0x84, 0x01, 0x01, 0x85, 0x01,
     0x01, 0x86, 0x01, 0x01, 0x6c, 0x01, 0x03,
   };
-  BOOST_REQUIRE_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
-                                  wire.begin(), wire.end());
+  BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                wire.begin(), wire.end());
 
-  BOOST_REQUIRE_NO_THROW(FaceEventNotification(wire));
   FaceEventNotification notification2(wire);
-  BOOST_CHECK_EQUAL(notification1.getKind(), notification2.getKind());
-  BOOST_CHECK_EQUAL(notification1.getFaceId(), notification2.getFaceId());
-  BOOST_CHECK_EQUAL(notification1.getRemoteUri(), notification2.getRemoteUri());
-  BOOST_CHECK_EQUAL(notification1.getLocalUri(), notification2.getLocalUri());
-  BOOST_CHECK_EQUAL(notification1.getFaceScope(), notification2.getFaceScope());
-  BOOST_CHECK_EQUAL(notification1.getFacePersistency(), notification2.getFacePersistency());
-  BOOST_CHECK_EQUAL(notification1.getLinkType(), notification2.getLinkType());
-  BOOST_CHECK_EQUAL(notification1.getFlags(), notification2.getFlags());
+  BOOST_CHECK_EQUAL(notification1, notification2);
 
-  std::ostringstream os;
-  os << notification2;
-  BOOST_CHECK_EQUAL(os.str(), "FaceEventNotification("
-                              "Kind: created, "
-                              "FaceID: 20, "
-                              "RemoteUri: tcp4://192.0.2.1:55555, "
-                              "LocalUri: tcp4://192.0.2.2:6363, "
-                              "FaceScope: local, "
-                              "FacePersistency: on-demand, "
-                              "LinkType: multi-access, "
-                              "Flags: 0x3)");
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(notification2),
+                    "FaceEvent(Kind: created,\n"
+                    "          FaceId: 20,\n"
+                    "          RemoteUri: tcp4://192.0.2.1:55555,\n"
+                    "          LocalUri: tcp4://192.0.2.2:6363,\n"
+                    "          FaceScope: local,\n"
+                    "          FacePersistency: on-demand,\n"
+                    "          LinkType: multi-access,\n"
+                    "          Flags: 0x3\n"
+                    "          )");
 }
 
-BOOST_AUTO_TEST_CASE(EncodeDestroyed)
+BOOST_AUTO_TEST_CASE(Destroyed)
 {
   FaceEventNotification notification1;
   notification1.setKind(FACE_EVENT_DESTROYED)
@@ -126,8 +116,7 @@
                .setFacePersistency(FACE_PERSISTENCY_ON_DEMAND)
                .setLinkType(LINK_TYPE_MULTI_ACCESS)
                .setFlags(0x4);
-  Block wire;
-  BOOST_REQUIRE_NO_THROW(wire = notification1.wireEncode());
+  Block wire = notification1.wireEncode();
 
   // These octets are obtained by the snippet below.
   // This check is intended to detect unexpected encoding change in the future.
@@ -143,34 +132,25 @@
     0x3a, 0x36, 0x33, 0x36, 0x33, 0x84, 0x01, 0x01, 0x85, 0x01,
     0x01, 0x86, 0x01, 0x01, 0x6c, 0x01, 0x04,
   };
-  BOOST_REQUIRE_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
-                                  wire.begin(), wire.end());
+  BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                wire.begin(), wire.end());
 
-  BOOST_REQUIRE_NO_THROW(FaceEventNotification(wire));
   FaceEventNotification notification2(wire);
-  BOOST_CHECK_EQUAL(notification1.getKind(), notification2.getKind());
-  BOOST_CHECK_EQUAL(notification1.getFaceId(), notification2.getFaceId());
-  BOOST_CHECK_EQUAL(notification1.getRemoteUri(), notification2.getRemoteUri());
-  BOOST_CHECK_EQUAL(notification1.getLocalUri(), notification2.getLocalUri());
-  BOOST_CHECK_EQUAL(notification1.getFaceScope(), notification2.getFaceScope());
-  BOOST_CHECK_EQUAL(notification1.getFacePersistency(), notification2.getFacePersistency());
-  BOOST_CHECK_EQUAL(notification1.getLinkType(), notification2.getLinkType());
-  BOOST_CHECK_EQUAL(notification1.getFlags(), notification2.getFlags());
+  BOOST_CHECK_EQUAL(notification1, notification2);
 
-  std::ostringstream os;
-  os << notification2;
-  BOOST_CHECK_EQUAL(os.str(), "FaceEventNotification("
-                              "Kind: destroyed, "
-                              "FaceID: 20, "
-                              "RemoteUri: tcp4://192.0.2.1:55555, "
-                              "LocalUri: tcp4://192.0.2.2:6363, "
-                              "FaceScope: local, "
-                              "FacePersistency: on-demand, "
-                              "LinkType: multi-access, "
-                              "Flags: 0x4)");
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(notification2),
+                    "FaceEvent(Kind: destroyed,\n"
+                    "          FaceId: 20,\n"
+                    "          RemoteUri: tcp4://192.0.2.1:55555,\n"
+                    "          LocalUri: tcp4://192.0.2.2:6363,\n"
+                    "          FaceScope: local,\n"
+                    "          FacePersistency: on-demand,\n"
+                    "          LinkType: multi-access,\n"
+                    "          Flags: 0x4\n"
+                    "          )");
 }
 
-BOOST_AUTO_TEST_CASE(EncodeUp)
+BOOST_AUTO_TEST_CASE(Up)
 {
   FaceEventNotification notification1;
   notification1.setKind(FACE_EVENT_UP)
@@ -181,8 +161,7 @@
                .setFacePersistency(FACE_PERSISTENCY_ON_DEMAND)
                .setLinkType(LINK_TYPE_MULTI_ACCESS)
                .setFlags(0x05);
-  Block wire;
-  BOOST_REQUIRE_NO_THROW(wire = notification1.wireEncode());
+  Block wire = notification1.wireEncode();
 
   // These octets are obtained by the snippet below.
   // This check is intended to detect unexpected encoding change in the future.
@@ -198,33 +177,25 @@
     0x3a, 0x36, 0x33, 0x36, 0x33, 0x84, 0x01, 0x01, 0x85, 0x01,
     0x01, 0x86, 0x01, 0x01, 0x6c, 0x01, 0x05,
   };
-  BOOST_REQUIRE_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
-                                  wire.begin(), wire.end());
+  BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                wire.begin(), wire.end());
 
-  BOOST_REQUIRE_NO_THROW(FaceEventNotification(wire));
   FaceEventNotification notification2(wire);
-  BOOST_CHECK_EQUAL(notification1.getKind(), notification2.getKind());
-  BOOST_CHECK_EQUAL(notification1.getFaceId(), notification2.getFaceId());
-  BOOST_CHECK_EQUAL(notification1.getRemoteUri(), notification2.getRemoteUri());
-  BOOST_CHECK_EQUAL(notification1.getLocalUri(), notification2.getLocalUri());
-  BOOST_CHECK_EQUAL(notification1.getFaceScope(), notification2.getFaceScope());
-  BOOST_CHECK_EQUAL(notification1.getFacePersistency(), notification2.getFacePersistency());
-  BOOST_CHECK_EQUAL(notification1.getLinkType(), notification2.getLinkType());
+  BOOST_CHECK_EQUAL(notification1, notification2);
 
-  std::ostringstream os;
-  os << notification2;
-  BOOST_CHECK_EQUAL(os.str(), "FaceEventNotification("
-                              "Kind: up, "
-                              "FaceID: 20, "
-                              "RemoteUri: tcp4://192.0.2.1:55555, "
-                              "LocalUri: tcp4://192.0.2.2:6363, "
-                              "FaceScope: local, "
-                              "FacePersistency: on-demand, "
-                              "LinkType: multi-access, "
-                              "Flags: 0x5)");
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(notification2),
+                    "FaceEvent(Kind: up,\n"
+                    "          FaceId: 20,\n"
+                    "          RemoteUri: tcp4://192.0.2.1:55555,\n"
+                    "          LocalUri: tcp4://192.0.2.2:6363,\n"
+                    "          FaceScope: local,\n"
+                    "          FacePersistency: on-demand,\n"
+                    "          LinkType: multi-access,\n"
+                    "          Flags: 0x5\n"
+                    "          )");
 }
 
-BOOST_AUTO_TEST_CASE(EncodeDown)
+BOOST_AUTO_TEST_CASE(Down)
 {
   FaceEventNotification notification1;
   notification1.setKind(FACE_EVENT_DOWN)
@@ -235,8 +206,7 @@
                .setFacePersistency(FACE_PERSISTENCY_ON_DEMAND)
                .setLinkType(LINK_TYPE_MULTI_ACCESS)
                .setFlags(0x06);
-  Block wire;
-  BOOST_REQUIRE_NO_THROW(wire = notification1.wireEncode());
+  Block wire = notification1.wireEncode();
 
   // These octets are obtained by the snippet below.
   // This check is intended to detect unexpected encoding change in the future.
@@ -252,30 +222,42 @@
     0x3a, 0x36, 0x33, 0x36, 0x33, 0x84, 0x01, 0x01, 0x85, 0x01,
     0x01, 0x86, 0x01, 0x01, 0x6c, 0x01, 0x06,
   };
-  BOOST_REQUIRE_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
-                                  wire.begin(), wire.end());
+  BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                wire.begin(), wire.end());
 
-  BOOST_REQUIRE_NO_THROW(FaceEventNotification(wire));
   FaceEventNotification notification2(wire);
-  BOOST_CHECK_EQUAL(notification1.getKind(), notification2.getKind());
-  BOOST_CHECK_EQUAL(notification1.getFaceId(), notification2.getFaceId());
-  BOOST_CHECK_EQUAL(notification1.getRemoteUri(), notification2.getRemoteUri());
-  BOOST_CHECK_EQUAL(notification1.getLocalUri(), notification2.getLocalUri());
-  BOOST_CHECK_EQUAL(notification1.getFaceScope(), notification2.getFaceScope());
-  BOOST_CHECK_EQUAL(notification1.getFacePersistency(), notification2.getFacePersistency());
-  BOOST_CHECK_EQUAL(notification1.getLinkType(), notification2.getLinkType());
+  BOOST_CHECK_EQUAL(notification1, notification2);
 
-  std::ostringstream os;
-  os << notification2;
-  BOOST_CHECK_EQUAL(os.str(), "FaceEventNotification("
-                              "Kind: down, "
-                              "FaceID: 20, "
-                              "RemoteUri: tcp4://192.0.2.1:55555, "
-                              "LocalUri: tcp4://192.0.2.2:6363, "
-                              "FaceScope: local, "
-                              "FacePersistency: on-demand, "
-                              "LinkType: multi-access, "
-                              "Flags: 0x6)");
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(notification2),
+                    "FaceEvent(Kind: down,\n"
+                    "          FaceId: 20,\n"
+                    "          RemoteUri: tcp4://192.0.2.1:55555,\n"
+                    "          LocalUri: tcp4://192.0.2.2:6363,\n"
+                    "          FaceScope: local,\n"
+                    "          FacePersistency: on-demand,\n"
+                    "          LinkType: multi-access,\n"
+                    "          Flags: 0x6\n"
+                    "          )");
+}
+
+BOOST_AUTO_TEST_CASE(Equality)
+{
+  FaceEventNotification notification1, notification2;
+  BOOST_CHECK_EQUAL(notification1, notification2);
+
+  notification1.setKind(FACE_EVENT_CREATED)
+               .setFaceId(123)
+               .setRemoteUri("tcp4://192.0.2.1:55555")
+               .setLocalUri("tcp4://192.0.2.2:6363");
+  notification2 = notification1;
+  BOOST_CHECK_EQUAL(notification1, notification2);
+
+  notification2.setFaceId(42);
+  BOOST_CHECK_NE(notification1, notification2);
+
+  notification2 = notification1;
+  notification2.setKind(FACE_EVENT_DESTROYED);
+  BOOST_CHECK_NE(notification1, notification2);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestFaceEventNotification