Add a set of helper functions to support JSON reading/writing

Change-Id: I93b3cd78efd9b04102f4928be99dfbe20f30e355
diff --git a/src/json-helper.cpp b/src/json-helper.cpp
new file mode 100644
index 0000000..609b426
--- /dev/null
+++ b/src/json-helper.cpp
@@ -0,0 +1,79 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "json-helper.hpp"
+#include <boost/lexical_cast.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+const JsonSection
+genResponseProbeJson(const Name& identifier, const Name& CaInformation)
+{
+  JsonSection root;
+
+  root.put(JSON_IDNENTIFIER, identifier.toUri());
+  root.put(JSON_CA_INFO, CaInformation.toUri());
+
+  return root;
+}
+
+const JsonSection
+genResponseNewJson(const CertificateRequest& request,
+                   const std::list<std::tuple<std::string, 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());
+
+  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));
+    challengesSection.push_back(std::make_pair("", challenge));
+  }
+  root.add_child(JSON_CHALLENGES, challengesSection);
+
+  return root;
+}
+
+const JsonSection
+genResponsePollJson(const CertificateRequest& request)
+{
+  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());
+  return root;
+}
+
+const JsonSection
+genErrorJson(const std::string& errorInfo)
+{
+  JsonSection root;
+  root.put(JSON_STATUS, "error");
+  root.put(JSON_ERROR_INFO, errorInfo);
+  return root;
+}
+
+} // namespace ndncert
+} // namespace ndn
diff --git a/src/json-helper.hpp b/src/json-helper.hpp
new file mode 100644
index 0000000..fe3ba80
--- /dev/null
+++ b/src/json-helper.hpp
@@ -0,0 +1,106 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_JSON_HELPER_HPP
+#define NDNCERT_JSON_HELPER_HPP
+
+#include "certificate-request.hpp"
+
+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;
+
+/**
+ * @brief Generate JSON file to response PROBE insterest
+ *
+ * Target JSON format:
+ * {
+ *   "Identifier": "",
+ *   "CA-Info": ""
+ * }
+ */
+const JsonSection
+genResponseProbeJson(const Name& identifier, const Name& CaInformation);
+
+/**
+ * @brief Generate JSON file to response NEW interest
+ *
+ * Target JSON format:
+ * {
+ *   "Status": "",
+ *   "Request-ID": "",
+ *   "Challenges": [
+ *     {
+ *       "Challenge-Type": "",
+ *       "Challenge-Instruction": ""
+ *     },
+ *     {
+ *       "Challenge-Type": "",
+ *       "Challenge-Instruction": ""
+ *     },
+ *     ...
+ *   ]
+ * }
+ */
+const JsonSection
+genResponseNewJson(const CertificateRequest& request,
+                   const std::list<std::tuple<std::string, std::string>> challenges);
+
+/**
+ * @brief Generate JSON file to response POLL interest
+ *
+ * Target JSON format:
+ * {
+ *   "Status": "",
+ *   "Challenge-Type": "",
+ *   "Challenge-Status": "",
+ *   "Challenge-Instruction": ""
+ * }
+ */
+const JsonSection
+genResponsePollJson(const CertificateRequest& request);
+
+/**
+ * @brief Generate JSON file when there is an Error
+ *
+ * Target JSON format:
+ * {
+ *   "Status": "",
+ *   "Error-Info": ""
+ * }
+ */
+const JsonSection
+genErrorJson(const std::string& errorInfo);
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_JSON_HELPER_HPP
diff --git a/tests/unit-tests/json-helper.t.cpp b/tests/unit-tests/json-helper.t.cpp
new file mode 100644
index 0000000..d258e54
--- /dev/null
+++ b/tests/unit-tests/json-helper.t.cpp
@@ -0,0 +1,93 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "identity-management-fixture.hpp"
+#include "json-helper.hpp"
+
+namespace ndn {
+namespace ndncert {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(TestJsonHelper, IdentityManagementV2Fixture)
+
+BOOST_AUTO_TEST_CASE(GenerateProbeJson)
+{
+  auto result = genResponseProbeJson(Name("/ndn/edu/ucla/cs/zhiyi/macbook"),
+                                     Name("/ndn/edu/ucla/cs/zhiyi/ca-info"));
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_IDNENTIFIER), "/ndn/edu/ucla/cs/zhiyi/macbook");
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CA_INFO), "/ndn/edu/ucla/cs/zhiyi/ca-info");
+}
+
+BOOST_AUTO_TEST_CASE(GenerateNewResponseJson)
+{
+  auto identity = addIdentity(Name("/ndn/site1"));
+  auto key = identity.getDefaultKey();
+  auto cert = key.getDefaultCertificate();
+
+  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)
+{
+  auto identity = addIdentity(Name("/ndn/site1"));
+  auto key = identity.getDefaultKey();
+  auto cert = key.getDefaultCertificate();
+
+  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_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");
+}
+
+BOOST_AUTO_TEST_CASE(GenerateErrorJson)
+{
+  auto result = genErrorJson("The certificate name already exists");
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "error");
+  BOOST_CHECK_EQUAL(result.get<std::string>(JSON_ERROR_INFO),
+                    "The certificate name already exists");
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestJsonHelper
+
+} // namespace tests
+} // namespace ndncert
+} // namespace ndn