tests: MockNfdMgmtFixture requires specific command Interest

MockNfdMgmtFixture previously allows a test case to respond to
the "last" command Interest, which causes difficulty when the
management client sends multiple command Interests together. It
now requires a specific command Interest for sending a response.

refs #3866

Change-Id: I80eeed2cdced618725a73f21bfaa387d831ca11a
diff --git a/tests/tools/nfdc/face-module.t.cpp b/tests/tools/nfdc/face-module.t.cpp
index 8d33ee8..bce68b7 100644
--- a/tests/tools/nfdc/face-module.t.cpp
+++ b/tests/tools/nfdc/face-module.t.cpp
@@ -227,15 +227,15 @@
 {
 protected:
   void
-  respond409(FacePersistency persistency)
+  respond409(const Interest& interest, FacePersistency persistency)
   {
-    MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/faces/create");
+    MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/faces/create");
     ControlParameters body;
     body.setFaceId(1172)
         .setUri("udp4://100.77.30.65:6363")
         .setFacePersistency(persistency)
         .setFlags(0);
-    this->failCommand(409, "conflict-409", body);
+    this->failCommand(interest, 409, "conflict-409", body);
   }
 };
 
@@ -244,7 +244,7 @@
 BOOST_AUTO_TEST_CASE(Creating)
 {
   this->processInterest = [this] (const Interest& interest) {
-    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/faces/create");
+    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/faces/create");
     BOOST_REQUIRE(req.hasUri());
     BOOST_CHECK_EQUAL(req.getUri(), "udp4://159.242.33.78:6363");
     BOOST_REQUIRE(req.hasFacePersistency());
@@ -253,7 +253,7 @@
     ControlParameters resp;
     resp.setFaceId(2130)
         .setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
-    this->succeedCommand(resp);
+    this->succeedCommand(interest, resp);
   };
 
   this->execute("face create udp://159.242.33.78");
@@ -266,12 +266,12 @@
 {
   bool hasUpdateCommand = false;
   this->processInterest = [this, &hasUpdateCommand] (const Interest& interest) {
-    if (this->getCommand("/localhost/nfd/faces/create")) {
-      this->respond409(FacePersistency::FACE_PERSISTENCY_ON_DEMAND);
+    if (parseCommand(interest, "/localhost/nfd/faces/create")) {
+      this->respond409(interest, FacePersistency::FACE_PERSISTENCY_ON_DEMAND);
       return;
     }
 
-    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/faces/update");
+    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/faces/update");
     hasUpdateCommand = true;
     BOOST_REQUIRE(req.hasFaceId());
     BOOST_CHECK_EQUAL(req.getFaceId(), 1172);
@@ -283,7 +283,7 @@
     resp.setFaceId(1172)
         .setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT)
         .setFlags(0);
-    this->succeedCommand(resp);
+    this->succeedCommand(interest, resp);
   };
 
   this->execute("face create udp://100.77.30.65");
@@ -296,7 +296,7 @@
 BOOST_AUTO_TEST_CASE(NotDowngradingPersistency)
 {
   this->processInterest = [this] (const Interest& interest) {
-    this->respond409(FacePersistency::FACE_PERSISTENCY_PERMANENT);
+    this->respond409(interest, FacePersistency::FACE_PERSISTENCY_PERMANENT);
     // no command other than faces/create is expected
   };
 
@@ -309,7 +309,7 @@
 BOOST_AUTO_TEST_CASE(SamePersistency)
 {
   this->processInterest = [this] (const Interest& interest) {
-    this->respond409(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
+    this->respond409(interest, FacePersistency::FACE_PERSISTENCY_PERSISTENT);
     // no command other than faces/create is expected
   };
 
@@ -336,7 +336,7 @@
 
   this->processInterest = [this] (const Interest& interest) {
     // conflict with udp4://100.77.30.65:6363
-    this->respond409(FacePersistency::FACE_PERSISTENCY_ON_DEMAND);
+    this->respond409(interest, FacePersistency::FACE_PERSISTENCY_ON_DEMAND);
   };
 
   this->execute("face create udp://20.53.73.45");
@@ -348,12 +348,12 @@
 BOOST_AUTO_TEST_CASE(ErrorUpdate)
 {
   this->processInterest = [this] (const Interest& interest) {
-    if (this->getCommand("/localhost/nfd/faces/create")) {
-      this->respond409(FacePersistency::FACE_PERSISTENCY_ON_DEMAND);
+    if (parseCommand(interest, "/localhost/nfd/faces/create")) {
+      this->respond409(interest, FacePersistency::FACE_PERSISTENCY_ON_DEMAND);
       return;
     }
 
-    MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/faces/update");
+    MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/faces/update");
     // no response to faces/update
   };
 
@@ -374,13 +374,13 @@
       return;
     }
 
-    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/faces/destroy");
+    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/faces/destroy");
     BOOST_REQUIRE(req.hasFaceId());
     BOOST_CHECK_EQUAL(req.getFaceId(), 10156);
 
     ControlParameters resp;
     resp.setFaceId(10156);
-    this->succeedCommand(resp);
+    this->succeedCommand(interest, resp);
   };
 
   this->execute("face destroy 10156");
@@ -397,13 +397,13 @@
       return;
     }
 
-    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/faces/destroy");
+    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/faces/destroy");
     BOOST_REQUIRE(req.hasFaceId());
     BOOST_CHECK_EQUAL(req.getFaceId(), 2249);
 
     ControlParameters resp;
     resp.setFaceId(2249);
-    this->succeedCommand(resp);
+    this->succeedCommand(interest, resp);
   };
 
   this->execute("face destroy tcp://32.121.182.82");
@@ -466,7 +466,7 @@
       return;
     }
 
-    MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/faces/destroy");
+    MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/faces/destroy");
     // no response to command
   };
 
diff --git a/tests/tools/nfdc/mock-nfd-mgmt-fixture.hpp b/tests/tools/nfdc/mock-nfd-mgmt-fixture.hpp
index 319a9f1..fd309bf 100644
--- a/tests/tools/nfdc/mock-nfd-mgmt-fixture.hpp
+++ b/tests/tools/nfdc/mock-nfd-mgmt-fixture.hpp
@@ -72,35 +72,15 @@
     return ControlParameters(interest.getName().at(expectedPrefix.size()).blockFromValue());
   }
 
-  DEPRECATED(
-  ndn::optional<ControlParameters>
-  getCommand(const Name& expectedPrefix) const)
-  {
-    if (face.sentInterests.empty()) {
-      return ndn::nullopt;
-    }
-    return parseCommand(face.sentInterests.back(), expectedPrefix);
-  }
-
-  /** \brief respond to the last command
+  /** \brief send successful response to a command Interest
    */
   void
   succeedCommand(const Interest& interest, const ControlParameters& parameters)
   {
-    ndn::nfd::ControlResponse resp(200, "OK");
-    resp.setBody(parameters.wireEncode());
-    this->sendCommandReply(interest, resp);
+    this->sendCommandReply(interest, 200, "OK", parameters.wireEncode());
   }
 
-  DEPRECATED(
-  void
-  succeedCommand(const ControlParameters& parameters))
-  {
-    this->succeedCommand(face.sentInterests.back(), parameters);
-  }
-
-  /** \brief respond to the last command
-   *  \pre last Interest is a command
+  /** \brief send failure response to a command Interest
    */
   void
   failCommand(const Interest& interest, uint32_t code, const std::string& text)
@@ -108,26 +88,14 @@
     this->sendCommandReply(interest, {code, text});
   }
 
-  DEPRECATED(
-  void
-  failCommand(uint32_t code, const std::string& text))
-  {
-    this->sendCommandReply(face.sentInterests.back(), {code, text});
-  }
-
+  /** \brief send failure response to a command Interest
+   */
   void
   failCommand(const Interest& interest, uint32_t code, const std::string& text, const ControlParameters& body)
   {
     this->sendCommandReply(interest, code, text, body.wireEncode());
   }
 
-  DEPRECATED(
-  void
-  failCommand(uint32_t code, const std::string& text, const ControlParameters& body))
-  {
-    this->failCommand(face.sentInterests.back(), code, text, body);
-  }
-
 protected: // StatusDataset
   /** \brief send an empty dataset in reply to StatusDataset request
    *  \param prefix dataset prefix without version and segment
@@ -307,6 +275,7 @@
 
 /** \brief require the command in \p interest has expected prefix
  *  \note This must be used in processInterest lambda, and the Interest must be named 'interest'.
+ *  \return ControlParameters, or nullopt if \p interest does match \p expectedPrefix
  */
 #define MOCK_NFD_MGMT_REQUIRE_COMMAND_IS(expectedPrefix) \
   [interest] { \
@@ -316,12 +285,4 @@
     return *params; \
   } ()
 
-///\deprecated use MOCK_NFD_MGMT_REQUIRE_COMMAND_IS
-#define MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS(expectedPrefix) \
-  [this] { \
-    BOOST_REQUIRE_MESSAGE(!face.sentInterests.empty(), "no Interest expressed"); \
-    const Interest& interest = face.sentInterests.back(); \
-    return MOCK_NFD_MGMT_REQUIRE_COMMAND_IS(expectedPrefix); \
-  } ()
-
 #endif // NFD_TESTS_TOOLS_NFDC_MOCK_NFD_MGMT_FIXTURE_HPP
diff --git a/tests/tools/nfdc/rib-module.t.cpp b/tests/tools/nfdc/rib-module.t.cpp
index df0a696..2cdc21d 100644
--- a/tests/tools/nfdc/rib-module.t.cpp
+++ b/tests/tools/nfdc/rib-module.t.cpp
@@ -45,7 +45,7 @@
       return;
     }
 
-    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/rib/register");
+    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/rib/register");
     ndn::nfd::RibRegisterCommand cmd;
     cmd.validateRequest(req);
     cmd.applyDefaultsToRequest(req);
@@ -56,7 +56,7 @@
     BOOST_CHECK_EQUAL(req.getFlags(), ndn::nfd::ROUTE_FLAGS_NONE);
     BOOST_CHECK_EQUAL(req.hasExpirationPeriod(), false);
 
-    this->succeedCommand(req);
+    this->succeedCommand(interest, req);
   };
 
   this->execute("route add /vxXoEaWeDB 10156 no-inherit");
@@ -73,7 +73,7 @@
       return;
     }
 
-    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/rib/register");
+    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/rib/register");
     ndn::nfd::RibRegisterCommand cmd;
     cmd.validateRequest(req);
     cmd.applyDefaultsToRequest(req);
@@ -88,7 +88,7 @@
 
     ControlParameters resp = req;
     resp.setExpirationPeriod(time::milliseconds(727411154)); // server side may change expiration
-    this->succeedCommand(resp);
+    this->succeedCommand(interest, resp);
   };
 
   this->execute("route add /FLQAsaYnYf tcp4://32.121.182.82:6363 "
@@ -152,7 +152,7 @@
       return;
     }
 
-    MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/rib/register");
+    MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/rib/register");
     // no response to command
   };