Update the CertificateRequest and JSON helper functions to the newest packet format

Change-Id: I28dd2753dfc5d8dda658be455e3804445a9efc26
diff --git a/src/certificate-request.cpp b/src/certificate-request.cpp
index 80b5a45..d1a4545 100644
--- a/src/certificate-request.cpp
+++ b/src/certificate-request.cpp
@@ -29,52 +29,24 @@
                                        const security::v2::Certificate& cert)
   : m_caName(caName)
   , m_requestId(requestId)
-  , m_status(Pending)
   , m_cert(static_cast<const Data&>(cert))
 {
 }
 
 CertificateRequest::CertificateRequest(const Name& caName,
                                        const std::string& requestId,
-                                       const ApplicationStatus& status,
+                                       const std::string& status,
                                        const std::string& challengeType,
-                                       const std::string& challengeStatus,
-                                       const std::string& challengeDefinedField,
+                                       const std::string& challengeSecrets,
                                        const security::v2::Certificate& cert)
   : m_caName(caName)
   , m_requestId(requestId)
   , m_status(status)
   , m_challengeType(challengeType)
-  , m_challengeStatus(challengeStatus)
-  , m_challengeDefinedField(challengeDefinedField)
   , m_cert(static_cast<const Data&>(cert))
 {
-}
-
-std::ostream&
-operator<<(std::ostream& os, CertificateRequest::ApplicationStatus status)
-{
-  std::string statusString;
-  switch (status) {
-    case CertificateRequest::Pending: {
-      statusString = "pending";
-      break;
-    }
-    case CertificateRequest::Verifying: {
-      statusString = "verifying";
-      break;
-    }
-    case CertificateRequest::Success: {
-      statusString = "success";
-      break;
-    }
-    case CertificateRequest::Failure: {
-      statusString = "failure";
-      break;
-    }
-  }
-  os << statusString;
-  return os;
+  std::istringstream ss(challengeSecrets);
+  boost::property_tree::json_parser::read_json(ss, m_challengeSecrets);
 }
 
 std::ostream&
@@ -84,16 +56,14 @@
   os << "  " << request.getCaName() << "\n";
   os << "Request ID:\n";
   os << "  " << request.getRequestId() << "\n";
-  os << "Request Status:\n";
-  os << "  " << request.getStatus() << "\n";
+  if (request.getStatus() != "") {
+    os << "Request Status:\n";
+    os << "  " << request.getStatus() << "\n";
+  }
   if (request.getChallengeType() != "") {
     os << "Request Challenge Type:\n";
     os << "  " << request.getChallengeType() << "\n";
   }
-  if (request.getChallengeStatus() != "") {
-    os << "Request Challenge Status:\n";
-    os << "  " << request.getChallengeStatus() << "\n";
-  }
   os << "Certificate:\n";
   util::IndentedStream os2(os, "  ");
   os2 << request.getCert();
diff --git a/src/certificate-request.hpp b/src/certificate-request.hpp
index b8d1ccd..35fb2f7 100644
--- a/src/certificate-request.hpp
+++ b/src/certificate-request.hpp
@@ -27,6 +27,8 @@
 namespace ndn {
 namespace ndncert {
 
+typedef boost::property_tree::ptree JsonSection;
+
 /**
  * @brief Represents a certificate request instance.
  *
@@ -37,20 +39,12 @@
 class CertificateRequest
 {
 public:
-  enum ApplicationStatus {
-    Pending = 0,
-    Verifying = 1,
-    Success = 2,
-    Failure = 3
-  };
-
-public:
   CertificateRequest(const Name& caName, const std::string& requestId,
                      const security::v2::Certificate& cert);
 
   CertificateRequest(const Name& caName, const std::string& requestId,
-                     const ApplicationStatus& status, const std::string& challengeType,
-                     const std::string& challengeStatus, const std::string& challengeDefinedField,
+                     const std::string& status, const std::string& challengeType,
+                     const std::string& challengeDefinedField,
                      const security::v2::Certificate& certBlock);
 
   const Name&
@@ -65,7 +59,7 @@
     return m_requestId;
   }
 
-  const ApplicationStatus&
+  const std::string&
   getStatus() const
   {
     return m_status;
@@ -77,22 +71,10 @@
     return m_challengeType;
   }
 
-  const std::string&
-  getChallengeDefinedField() const
+  const JsonSection&
+  getChallengeSecrets() const
   {
-    return m_challengeDefinedField;
-  }
-
-  const std::string&
-  getChallengeInstruction() const
-  {
-    return m_challengeInstruction;
-  }
-
-  const std::string&
-  getChallengeStatus() const
-  {
-    return m_challengeStatus;
+    return m_challengeSecrets;
   }
 
   const security::v2::Certificate&
@@ -101,11 +83,8 @@
     return m_cert;
   }
 
-  /**
-   * These setters should only be invoked by ChallengeModule
-   */
   void
-  setStatus(const ApplicationStatus& status)
+  setStatus(const std::string& status)
   {
     m_status = status;
   }
@@ -117,58 +96,28 @@
   }
 
   void
-  setChallengeStatus(const std::string& challengeStatus)
+  setChallengeSecrets(const JsonSection& challengeSecrets)
   {
-    m_challengeStatus = challengeStatus;
-  }
-
-  void
-  setChallengeDefinedField(const std::string& challengeDefinedField)
-  {
-    m_challengeDefinedField = challengeDefinedField;
-  }
-
-  void
-  setChallengeInstruction(const std::string& challengeInstruction)
-  {
-    m_challengeInstruction = challengeInstruction;
+    m_challengeSecrets = challengeSecrets;
   }
 
 private:
   Name m_caName;
   std::string m_requestId;
-  ApplicationStatus m_status;
+  std::string m_status;
   std::string m_challengeType;
 
   /**
-   * @brief Defined by ChallengeModule to indicate the verification status.
-   *
-   * This field will be stored by CA.
-   */
-  std::string m_challengeStatus;
-
-  /**
    * @brief Defined by ChallengeModule to store secret information.
    *
    * This field will be stored by CA.
    */
-  std::string m_challengeDefinedField;
-
-  /**
-   * @brief Defined by ChallengeModule to indicate end entity the next step.
-   *
-   * This field will be presented to end entity.
-   * This field will NOT be stored by CA.
-   */
-  std::string m_challengeInstruction;
+  JsonSection m_challengeSecrets;
 
   security::v2::Certificate m_cert;
 };
 
 std::ostream&
-operator<<(std::ostream& os, CertificateRequest::ApplicationStatus status);
-
-std::ostream&
 operator<<(std::ostream& os, const CertificateRequest& request);
 
 } // namespace ndncert
diff --git a/src/json-helper.cpp b/src/json-helper.cpp
index 609b426..a7cc897 100644
--- a/src/json-helper.cpp
+++ b/src/json-helper.cpp
@@ -36,18 +36,15 @@
 }
 
 const JsonSection
-genResponseNewJson(const CertificateRequest& request,
-                   const std::list<std::tuple<std::string, std::string>> challenges)
+genResponseNewJson(const std::string& requestId, const std::list<std::string>& challenges)
 {
   JsonSection root;
   JsonSection challengesSection;
-  root.put(JSON_STATUS, boost::lexical_cast<std::string>(request.getStatus()));
-  root.put(JSON_REQUEST_ID, request.getRequestId());
+  root.put(JSON_REQUEST_ID, requestId);
 
   for (const auto& entry : challenges) {
     JsonSection challenge;
-    challenge.put(JSON_CHALLENGE_TYPE, std::get<0>(entry));
-    challenge.put(JSON_CHALLENGE_INSTRUCTION, std::get<1>(entry));
+    challenge.put(JSON_CHALLENGE_TYPE, entry);
     challengesSection.push_back(std::make_pair("", challenge));
   }
   root.add_child(JSON_CHALLENGES, challengesSection);
@@ -56,13 +53,16 @@
 }
 
 const JsonSection
-genResponsePollJson(const CertificateRequest& request)
+genResponseChallengeJson(const std::string& requestId, const std::string& challengeType,
+                         const std::string& status, const Name& name)
 {
   JsonSection root;
-  root.put(JSON_STATUS, boost::lexical_cast<std::string>(request.getStatus()));
-  root.put(JSON_CHALLENGE_TYPE, request.getChallengeType());
-  root.put(JSON_CHALLENGE_STATUS, request.getChallengeStatus());
-  root.put(JSON_CHALLENGE_INSTRUCTION, request.getChallengeInstruction());
+  root.put(JSON_REQUEST_ID, requestId);
+  root.put(JSON_CHALLENGE_TYPE, challengeType);
+  root.put(JSON_STATUS, status);
+  if (name.toUri() != "") {
+    root.put(JSON_CERTIFICATE, name.toUri());
+  }
   return root;
 }
 
diff --git a/src/json-helper.hpp b/src/json-helper.hpp
index fe3ba80..c34c631 100644
--- a/src/json-helper.hpp
+++ b/src/json-helper.hpp
@@ -26,25 +26,22 @@
 namespace ndn {
 namespace ndncert {
 
-const std::string JSON_IDNENTIFIER = "Identifier";
-const std::string JSON_CA_INFO = "CA-Info";
-const std::string JSON_STATUS = "Status";
-const std::string JSON_REQUEST_ID = "Request-ID";
-const std::string JSON_CHALLENGES = "Challenges";
-const std::string JSON_CHALLENGE_TYPE = "Challenge-Type";
-const std::string JSON_CHALLENGE_INSTRUCTION = "Challenge-Instruction";
-const std::string JSON_CHALLENGE_STATUS = "Challenge-Status";
-const std::string JSON_ERROR_INFO = "Error-Info";
-
-typedef boost::property_tree::ptree JsonSection;
+const std::string JSON_IDNENTIFIER = "identifier";
+const std::string JSON_CA_INFO = "ca-info";
+const std::string JSON_STATUS = "status";
+const std::string JSON_REQUEST_ID = "request-id";
+const std::string JSON_CHALLENGES = "challenges";
+const std::string JSON_CHALLENGE_TYPE = "challenge-type";
+const std::string JSON_ERROR_INFO = "error-info";
+const std::string JSON_CERTIFICATE = "certificate";
 
 /**
  * @brief Generate JSON file to response PROBE insterest
  *
  * Target JSON format:
  * {
- *   "Identifier": "",
- *   "CA-Info": ""
+ *   "identifier": "",
+ *   "ca-info": ""
  * }
  */
 const JsonSection
@@ -55,46 +52,54 @@
  *
  * Target JSON format:
  * {
- *   "Status": "",
- *   "Request-ID": "",
- *   "Challenges": [
+ *   "request-id": "",
+ *   "challenges": [
  *     {
- *       "Challenge-Type": "",
- *       "Challenge-Instruction": ""
+ *       "challenge-type": ""
  *     },
  *     {
- *       "Challenge-Type": "",
- *       "Challenge-Instruction": ""
+ *       "challenge-type": ""
  *     },
  *     ...
  *   ]
  * }
  */
 const JsonSection
-genResponseNewJson(const CertificateRequest& request,
-                   const std::list<std::tuple<std::string, std::string>> challenges);
+genResponseNewJson(const std::string& requestId, const std::list<std::string>& challenges);
 
 /**
- * @brief Generate JSON file to response POLL interest
+ * @brief Generate JSON file to response _SELECT, _VALIDATE, and _STATUS interest
+ *
+ * if certificate name is not present:
  *
  * Target JSON format:
  * {
- *   "Status": "",
- *   "Challenge-Type": "",
- *   "Challenge-Status": "",
- *   "Challenge-Instruction": ""
+ *   "request-id": "@p requestId",
+ *   "challenge-type": "@p challengeType",
+ *   "status": "@p status"
+ * }
+ *
+ * if certificate name is present:
+ *
+ * Target JSON format:
+ * {
+ *   "request-id": "@p requestId",
+ *   "challenge-type": "@p challengeType",
+ *   "status": "@p status",
+ *   "certificate":"@p name"
  * }
  */
 const JsonSection
-genResponsePollJson(const CertificateRequest& request);
+genResponseChallengeJson(const std::string& requestId, const std::string& challengeType,
+                         const std::string& status, const Name& name = Name());
 
 /**
  * @brief Generate JSON file when there is an Error
  *
  * Target JSON format:
  * {
- *   "Status": "",
- *   "Error-Info": ""
+ *   "status": "",
+ *   "error-info": ""
  * }
  */
 const JsonSection
diff --git a/tests/unit-tests/certificate-request.t.cpp b/tests/unit-tests/certificate-request.t.cpp
index 113748d..3282ee9 100644
--- a/tests/unit-tests/certificate-request.t.cpp
+++ b/tests/unit-tests/certificate-request.t.cpp
@@ -38,49 +38,43 @@
   CertificateRequest request1(Name("/ndn/site1"), "123", cert);
   BOOST_CHECK_EQUAL(request1.getCaName().toUri(), "/ndn/site1");
   BOOST_CHECK_EQUAL(request1.getRequestId(), "123");
-  BOOST_CHECK_EQUAL(request1.getStatus(), CertificateRequest::Pending);
-  BOOST_CHECK_EQUAL(request1.getChallengeType(), "");
-  BOOST_CHECK_EQUAL(request1.getChallengeStatus(), "");
-  BOOST_CHECK_EQUAL(request1.getChallengeDefinedField(), "");
-  BOOST_CHECK_EQUAL(request1.getChallengeInstruction(), "");
+  BOOST_CHECK_EQUAL(request1.getStatus(), "");
+  BOOST_CHECK_EQUAL(request1.getChallengeSecrets().empty(), true);
+  BOOST_CHECK_EQUAL(request1.getCert(), cert);
   BOOST_CHECK_EQUAL(request1.getCert(), cert);
 
-  CertificateRequest request2(Name("/ndn/site1"), "123", CertificateRequest::Verifying,
-                              "Email", "NEED_CODE", "123456", cert);
+  JsonSection json;
+  json.put("code", "1234");
+  std::stringstream ss;
+  boost::property_tree::write_json(ss, json);
+  std::string jsonValue = ss.str();
+
+  CertificateRequest request2(Name("/ndn/site1"), "123", "need-verify", "EMAIL", jsonValue, cert);
   BOOST_CHECK_EQUAL(request2.getCaName().toUri(), "/ndn/site1");
   BOOST_CHECK_EQUAL(request2.getRequestId(), "123");
-  BOOST_CHECK_EQUAL(request2.getStatus(), CertificateRequest::Verifying);
-  BOOST_CHECK_EQUAL(request2.getChallengeType(), "Email");
-  BOOST_CHECK_EQUAL(request2.getChallengeStatus(), "NEED_CODE");
-  BOOST_CHECK_EQUAL(request2.getChallengeDefinedField(), "123456");
-  BOOST_CHECK_EQUAL(request2.getChallengeInstruction(), "");
+  BOOST_CHECK_EQUAL(request2.getStatus(), "need-verify");
+  BOOST_CHECK_EQUAL(request2.getChallengeType(), "EMAIL");
+  BOOST_CHECK(request2.getChallengeSecrets() == json);
   BOOST_CHECK_EQUAL(request2.getCert(), cert);
 }
 
-BOOST_AUTO_TEST_CASE(GetStatusOutput)
-{
-  CertificateRequest::ApplicationStatus status = CertificateRequest::Success;
-  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(status), "success");
-}
-
 BOOST_AUTO_TEST_CASE(GetterSetter)
 {
   auto identity = addIdentity(Name("/ndn/site1"));
   auto key = identity.getDefaultKey();
   auto cert = key.getDefaultCertificate();
 
-  CertificateRequest request(Name("/ndn/site1"), "123", cert);
-  request.setStatus(CertificateRequest::Verifying);
-  request.setChallengeType("Email");
-  request.setChallengeDefinedField("456");
-  request.setChallengeStatus("NEED_EMAIL");
-  request.setChallengeInstruction("Please provide your email address");
+  JsonSection json;
+  json.put("code", "1234");
 
-  BOOST_CHECK_EQUAL(request.getStatus(), CertificateRequest::Verifying);
-  BOOST_CHECK_EQUAL(request.getChallengeType(), "Email");
-  BOOST_CHECK_EQUAL(request.getChallengeDefinedField(), "456");
-  BOOST_CHECK_EQUAL(request.getChallengeStatus(), "NEED_EMAIL");
-  BOOST_CHECK_EQUAL(request.getChallengeInstruction(), "Please provide your email address");
+  CertificateRequest request(Name("/ndn/site1"), "123", cert);
+  request.setStatus("need-verify");
+  request.setChallengeType("EMAIL");
+  request.setChallengeSecrets(json);
+
+  BOOST_CHECK_EQUAL(request.getStatus(), "need-verify");
+  BOOST_CHECK_EQUAL(request.getChallengeType(), "EMAIL");
+  BOOST_CHECK(request.getChallengeSecrets() == json);
 }
 
 BOOST_AUTO_TEST_CASE(GetCertificateRequestOutput)
@@ -101,8 +95,6 @@
   /ndn/site1
 Request ID:
   123
-Request Status:
-  pending
 Certificate:
   Certificate name:
     /ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B
diff --git a/tests/unit-tests/json-helper.t.cpp b/tests/unit-tests/json-helper.t.cpp
index d258e54..61a74e5 100644
--- a/tests/unit-tests/json-helper.t.cpp
+++ b/tests/unit-tests/json-helper.t.cpp
@@ -18,14 +18,14 @@
  * See AUTHORS.md for complete list of ndncert authors and contributors.
  */
 
-#include "identity-management-fixture.hpp"
+#include "boost-test.hpp"
 #include "json-helper.hpp"
 
 namespace ndn {
 namespace ndncert {
 namespace tests {
 
-BOOST_FIXTURE_TEST_SUITE(TestJsonHelper, IdentityManagementV2Fixture)
+BOOST_AUTO_TEST_SUITE(TestJsonHelper)
 
 BOOST_AUTO_TEST_CASE(GenerateProbeJson)
 {
@@ -37,45 +37,33 @@
 
 BOOST_AUTO_TEST_CASE(GenerateNewResponseJson)
 {
-  auto identity = addIdentity(Name("/ndn/site1"));
-  auto key = identity.getDefaultKey();
-  auto cert = key.getDefaultCertificate();
+  std::list<std::string> challenges;
+  challenges.push_back("PIN");
+  challenges.push_back("EMAIL");
+  auto result = genResponseNewJson("598234759", challenges);
 
-  CertificateRequest request(Name("/ndn/site1"), "598234759", cert);
-  std::list<std::tuple<std::string, std::string>> challenges;
-  challenges.push_back(std::make_tuple("PIN", "Please ask ca officer"));
-  challenges.push_back(std::make_tuple("EMAIL", "Please provide your email"));
-  auto result = genResponseNewJson(request, challenges);
-
-  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "pending");
   BOOST_CHECK_EQUAL(result.get<std::string>(JSON_REQUEST_ID), "598234759");
   auto child = result.get_child(JSON_CHALLENGES);
   auto it = child.begin();
   BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_TYPE), "PIN");
-  BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_INSTRUCTION),
-                    "Please ask ca officer");
   it++;
   BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_TYPE), "EMAIL");
-  BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_INSTRUCTION),
-                    "Please provide your email");
 }
 
-BOOST_AUTO_TEST_CASE(GeneratePollResponseJson)
+BOOST_AUTO_TEST_CASE(GenerateChallengeResponseJson)
 {
-  auto identity = addIdentity(Name("/ndn/site1"));
-  auto key = identity.getDefaultKey();
-  auto cert = key.getDefaultCertificate();
+  auto result = genResponseChallengeJson("598234759", "EMAIL", "need-code");
 
-  CertificateRequest request(Name("/ndn/site1"), "598234759", CertificateRequest::Verifying,
-                             "Email", "NEED_CODE", "111", cert);
-  request.setChallengeInstruction("Please provide verification code");
-  auto result = genResponsePollJson(request);
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_REQUEST_ID), "598234759");
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CHALLENGE_TYPE), "EMAIL");
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "need-code");
 
-  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "verifying");
-  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CHALLENGE_TYPE), "Email");
-  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CHALLENGE_STATUS), "NEED_CODE");
-  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CHALLENGE_INSTRUCTION),
-                    "Please provide verification code");
+  result = genResponseChallengeJson("598234759", "EMAIL", "need-code", Name("/ndn/test"));
+
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_REQUEST_ID), "598234759");
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CHALLENGE_TYPE), "EMAIL");
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "need-code");
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CERTIFICATE), "/ndn/test");
 }
 
 BOOST_AUTO_TEST_CASE(GenerateErrorJson)