tests/integrated: Basic repo insert, delete and read test case

Integrated tests are separated from unit tests:

* ./build/unit-tests
* ./build/integrated-tests

Change-Id: I766478de6ec4b7925d4d0dd5ec44e7e667de891b
diff --git a/tests/unit/repo-command-parameter.cpp b/tests/unit/repo-command-parameter.cpp
new file mode 100644
index 0000000..3c5efcf
--- /dev/null
+++ b/tests/unit/repo-command-parameter.cpp
@@ -0,0 +1,73 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California.
+ *
+ * This file is part of NDN repo-ng (Next generation of NDN repository).
+ * See AUTHORS.md for complete list of repo-ng authors and contributors.
+ *
+ * repo-ng 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.
+ *
+ * repo-ng 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 a copy of the GNU General Public License along with
+ * repo-ng, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "repo-command-parameter.hpp"
+
+#include <ndn-cxx/selectors.hpp>
+
+#include <boost/test/unit_test.hpp>
+
+namespace repo {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(RepoCommandParameter)
+
+BOOST_AUTO_TEST_CASE(EncodeDecode)
+{
+  repo::RepoCommandParameter parameter;
+  parameter.setName("/a/b/c");
+  parameter.setStartBlockId(1);
+  parameter.setEndBlockId(100);
+  parameter.setProcessId(1234567890);
+  ndn::Selectors selectors;
+  selectors.setMaxSuffixComponents(1);
+  parameter.setSelectors(selectors);
+
+  ndn::Block wire = parameter.wireEncode();
+
+  // These octets are obtained by the snippet below.
+  // This check is intended to detect unexpected encoding change in the future.
+  //for (ndn::Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
+  //  printf("0x%02x, ", *it);
+  //}
+  static const uint8_t expected[] = {
+    0xc9, 0x1c, 0x07, 0x09, 0x08, 0x01, 0x61, 0x08, 0x01, 0x62, 0x08,
+    0x01, 0x63, 0x09, 0x03, 0x0e, 0x01, 0x01, 0xcc, 0x01, 0x01, 0xcd,
+    0x01, 0x64, 0xce, 0x04, 0x49, 0x96, 0x02, 0xd2
+  };
+
+  BOOST_REQUIRE_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                  wire.begin(), wire.end());
+
+  BOOST_REQUIRE_NO_THROW(repo::RepoCommandParameter(wire));
+
+  repo::RepoCommandParameter decoded(wire);
+  //std::cout << decoded << std::endl;
+  BOOST_CHECK_EQUAL(decoded.getName(), parameter.getName());
+  BOOST_CHECK_EQUAL(decoded.getStartBlockId(), parameter.getStartBlockId());
+  BOOST_CHECK_EQUAL(decoded.getEndBlockId(), parameter.getEndBlockId());
+  BOOST_CHECK_EQUAL(decoded.getProcessId(), parameter.getProcessId());
+  BOOST_CHECK_EQUAL(decoded.getSelectors().getMaxSuffixComponents(),
+                    parameter.getSelectors().getMaxSuffixComponents());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace repo
diff --git a/tests/unit/repo-command-response.cpp b/tests/unit/repo-command-response.cpp
new file mode 100644
index 0000000..6192473
--- /dev/null
+++ b/tests/unit/repo-command-response.cpp
@@ -0,0 +1,69 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California.
+ *
+ * This file is part of NDN repo-ng (Next generation of NDN repository).
+ * See AUTHORS.md for complete list of repo-ng authors and contributors.
+ *
+ * repo-ng 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.
+ *
+ * repo-ng 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 a copy of the GNU General Public License along with
+ * repo-ng, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "repo-command-response.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace repo {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(RepoCommandResponse)
+
+BOOST_AUTO_TEST_CASE(EncodeDecode)
+{
+  repo::RepoCommandResponse response;
+  response.setStatusCode(404);
+  response.setStartBlockId(1);
+  response.setEndBlockId(100);
+  response.setProcessId(1234567890);
+  response.setInsertNum(100);
+  response.setDeleteNum(100);
+
+  ndn::Block wire = response.wireEncode();
+
+  // These octets are obtained by the snippet below.
+  // This check is intended to detect unexpected encoding change in the future.
+  //for (ndn::Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
+  //  printf("0x%02x, ", *it);
+  //}
+  static const uint8_t expected[] = {
+    0xcf, 0x16, 0xce, 0x04, 0x49, 0x96, 0x02, 0xd2, 0xd0, 0x02,
+    0x01, 0x94, 0xcc, 0x01, 0x01, 0xcd, 0x01, 0x64, 0xd1, 0x01,
+    0x64, 0xd2, 0x01, 0x64
+  };
+
+  BOOST_REQUIRE_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                  wire.begin(), wire.end());
+
+  BOOST_REQUIRE_NO_THROW(repo::RepoCommandResponse(wire));
+
+  repo::RepoCommandResponse decoded(wire);
+  BOOST_CHECK_EQUAL(decoded.getStatusCode(), response.getStatusCode());
+  BOOST_CHECK_EQUAL(decoded.getStartBlockId(), response.getStartBlockId());
+  BOOST_CHECK_EQUAL(decoded.getEndBlockId(), response.getEndBlockId());
+  BOOST_CHECK_EQUAL(decoded.getProcessId(), response.getProcessId());
+  BOOST_CHECK_EQUAL(decoded.getInsertNum(), response.getInsertNum());
+  BOOST_CHECK_EQUAL(decoded.getDeleteNum(), response.getDeleteNum());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace repo
diff --git a/tests/unit/sqlite-handle.cpp b/tests/unit/sqlite-handle.cpp
new file mode 100644
index 0000000..9dbe801
--- /dev/null
+++ b/tests/unit/sqlite-handle.cpp
@@ -0,0 +1,74 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California.
+ *
+ * This file is part of NDN repo-ng (Next generation of NDN repository).
+ * See AUTHORS.md for complete list of repo-ng authors and contributors.
+ *
+ * repo-ng 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.
+ *
+ * repo-ng 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 a copy of the GNU General Public License along with
+ * repo-ng, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "storage/sqlite-handle.hpp"
+
+#include "../sqlite-fixture.hpp"
+#include "../dataset-fixtures.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace repo {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(SqliteHandle)
+
+template<class Dataset>
+class Fixture : public SqliteFixture, public Dataset
+{
+};
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertReadDelete, T, DatasetFixtures, Fixture<T>)
+{
+  BOOST_TEST_MESSAGE(T::getName());
+
+  // Insert
+  for (typename T::DataContainer::iterator i = this->data.begin();
+       i != this->data.end(); ++i) {
+    BOOST_CHECK_EQUAL(this->handle->insertData(**i), true);
+  }
+
+  BOOST_CHECK_EQUAL(this->handle->size(), this->data.size());
+
+  // Read (all items should exist)
+  for (typename T::InterestContainer::iterator i = this->interests.begin();
+       i != this->interests.end(); ++i) {
+    ndn::Data retrievedData;
+    BOOST_REQUIRE_EQUAL(this->handle->readData(i->first, retrievedData), true);
+    BOOST_CHECK_EQUAL(retrievedData, *i->second);
+  }
+
+  // Delete
+  for (typename T::DataContainer::iterator i = this->data.begin();
+       i != this->data.end(); ++i) {
+    BOOST_CHECK_EQUAL(this->handle->deleteData((*i)->getName()), true);
+  }
+
+  // Read (none of the items should exist)
+  for (typename T::InterestContainer::iterator i = this->interests.begin();
+       i != this->interests.end(); ++i) {
+    ndn::Data retrievedData;
+    BOOST_REQUIRE_EQUAL(this->handle->readData(i->first, retrievedData), false);
+  }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace repo
diff --git a/tests/unit/tcp-bulk-insert-handle.cpp b/tests/unit/tcp-bulk-insert-handle.cpp
new file mode 100644
index 0000000..a7d9137
--- /dev/null
+++ b/tests/unit/tcp-bulk-insert-handle.cpp
@@ -0,0 +1,185 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California.
+ *
+ * This file is part of NDN repo-ng (Next generation of NDN repository).
+ * See AUTHORS.md for complete list of repo-ng authors and contributors.
+ *
+ * repo-ng 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.
+ *
+ * repo-ng 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 a copy of the GNU General Public License along with
+ * repo-ng, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "handles/tcp-bulk-insert-handle.hpp"
+
+#include "../sqlite-fixture.hpp"
+#include "../dataset-fixtures.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace repo {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(TcpBulkInsertHandle)
+
+class TcpClient
+{
+public:
+  TcpClient()
+    : socket(ioService)
+  {
+  }
+
+  void
+  start(const std::string& host, const std::string& port)
+  {
+    using namespace boost::asio;
+
+    ip::tcp::resolver resolver(ioService);
+    ip::tcp::resolver::query query(host, port);
+
+    ip::tcp::resolver::iterator endpoint = resolver.resolve(query);
+    ip::tcp::resolver::iterator end;
+
+    if (endpoint == end)
+      BOOST_FAIL("Cannot resolve [" + host + ":" + port + "]");
+
+    ip::tcp::endpoint serverEndpoint = *endpoint;
+
+    socket.async_connect(serverEndpoint,
+                         bind(&TcpClient::onSuccessfullConnect, this, _1));
+  }
+
+  virtual void
+  onSuccessfullConnect(const boost::system::error_code& error)
+  {
+    if (error)
+      {
+        BOOST_FAIL("TCP connection aborted");
+        return;
+      }
+  }
+
+public:
+  boost::asio::io_service ioService;
+  boost::asio::ip::tcp::socket socket;
+};
+
+template<class Dataset>
+class TcpBulkInsertFixture : public TcpClient,
+                             public SqliteFixture,
+                             public Dataset
+{
+public:
+  TcpBulkInsertFixture()
+    : scheduler(ioService)
+    , bulkInserter(ioService, *handle)
+  {
+    guardEvent = scheduler.scheduleEvent(ndn::time::seconds(2),
+                                         bind(&TcpBulkInsertFixture::fail, this, "Test timed out"));
+  }
+
+  virtual void
+  onSuccessfullConnect(const boost::system::error_code& error)
+  {
+    TcpClient::onSuccessfullConnect(error);
+
+    // This value may need to be adjusted if some dataset exceeds 100k
+    socket.set_option(boost::asio::socket_base::send_buffer_size(100000));
+
+    // Initially I wrote the following to use scatter-gather approach (using
+    // std::vector<const_buffer> and a single socket.async_send operation). Unfortunately, as
+    // described in http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/overview/implementation.html,
+    // scatter-gather is limited to at most `min(64,IOV_MAX)` buffers to be transmitted
+    // in a single operation
+    for (typename Dataset::DataContainer::iterator i = this->data.begin();
+         i != this->data.end(); ++i) {
+
+      socket.async_send(boost::asio::buffer((*i)->wireEncode().wire(),  (*i)->wireEncode().size()),
+                        bind(&TcpBulkInsertFixture::onSendFinished, this, _1, false));
+    }
+
+    socket.async_send(boost::asio::buffer(static_cast<const uint8_t*>(0), 0),
+                      bind(&TcpBulkInsertFixture::onSendFinished, this, _1, true));
+  }
+
+  void
+  onSendFinished(const boost::system::error_code& error, bool isFinal)
+  {
+    if (error) {
+      BOOST_FAIL("TCP connection aborted");
+      return;
+    }
+
+    if (isFinal) {
+      scheduler.cancelEvent(guardEvent);
+
+      // In case there are some outstanding handlers
+      // ioService.post(bind(&TcpBulkInsertFixture::stop, this));
+      scheduler.scheduleEvent(ndn::time::seconds(1),
+                              bind(&TcpBulkInsertFixture::stop, this));
+    }
+  }
+
+  void
+  fail(const std::string& info)
+  {
+    ioService.stop();
+    BOOST_FAIL(info);
+  }
+
+  void
+  stop()
+  {
+    // Terminate test
+    socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
+    socket.close();
+
+    bulkInserter.stop();
+    // may be ioService.stop() as well
+  }
+
+public:
+  Scheduler scheduler;
+  ndn::EventId guardEvent;
+  repo::TcpBulkInsertHandle bulkInserter;
+};
+
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(BulkInsertAndRead, T, DatasetFixtures, TcpBulkInsertFixture<T>)
+{
+  BOOST_TEST_MESSAGE(T::getName());
+  // BOOST_CHECK_EQUAL(this->handle->size(), 1);
+
+  // start bulk inserter
+  this->bulkInserter.listen("localhost", "17376");
+
+  // start test
+  this->start("localhost", "17376");
+
+  // actually run the test
+  this->ioService.run();
+
+  BOOST_CHECK_EQUAL(this->handle->size(), this->data.size());
+
+  // Read (all items should exist)
+  for (typename T::InterestContainer::iterator i = this->interests.begin();
+       i != this->interests.end(); ++i) {
+    ndn::Data retrievedData;
+    BOOST_REQUIRE_EQUAL(this->handle->readData(i->first, retrievedData), true);
+    BOOST_CHECK_EQUAL(retrievedData, *i->second);
+  }
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace repo
+} // namespace tests