/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2012 University of California, Los Angeles
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
 *         卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
 *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
 */

#include <boost/test/unit_test.hpp>
#include <boost/test/output_test_stream.hpp> 
#include <map>
using boost::test_tools::output_test_stream;

#include <boost/make_shared.hpp>

#include "../model/sync-ccnx-wrapper.h"
#include "../model/sync-app-data-fetch.h"
#include "../model/sync-app-data-publish.h"

using namespace Sync;
using namespace std;
using namespace boost;

class TestStructApp {
public:
  map<string, string> data;
  void set(string str1, string str2) {
    data.insert(make_pair(str1, str2));
  }

  void erase(string str1, string str2) {
    data.erase(str1);
  }

  string toString(){
    map<string, string>::iterator it = data.begin(); 
    string str = "";
    for (; it != data.end(); ++it){
      str += "<";
      str += it->first;
      str += "|";
      str += it->second;
      str += ">";
    }
    return str;
  }

};

BOOST_AUTO_TEST_CASE (AppDataPublishAndFetchTest)
{
  TestStructApp foo;
  TestStructApp bar;
	
  string interest = "/april/fool";
  string seq[5] = {"1", "2", "3", "4", "5"};
  string str[5] = {"panda", "express", "tastes", "so", "good"};

  for (int i = 0; i < 5; i++) {
    foo.set(interest + "/" + seq[i], str[i]);
  }

  boost::function<void (string, string)> setFunc =
    bind(&TestStructApp::set, &bar, _1, _2);

  shared_ptr<CcnxWrapper> handle(new CcnxWrapper());

  AppDataFetch fetcher(handle, setFunc);
  AppDataPublish publisher(handle);

  for (int i = 1; i <= 5; i++) {
    publisher.publishData(interest, 0, str[i - 1], 5);
  }

  BOOST_CHECK_EQUAL(publisher.getHighestSeq(interest, 0), 5);
  BOOST_CHECK_EQUAL(publisher.getRecentData(interest, 0), str[4]);

  fetcher.fetch(interest, 1, 5);
  // give time for ccnd to react
  sleep(1);
  BOOST_CHECK_EQUAL(foo.toString(), bar.toString());


  boost::function<void (string, string)> eraseFunc =
    bind(&TestStructApp::erase, &bar, _1, _2);
  fetcher.setDataCallback(eraseFunc);

  fetcher.fetch(interest, 1, 5);
  // give time for ccnd to react
  sleep(1);
  TestStructApp poo;

  BOOST_CHECK_EQUAL(poo.toString(), bar.toString());

}


