/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2023,  The University of Memphis,
 *                           Regents of the University of California,
 *                           Arizona Board of Regents.
 *
 * This file is part of NLSR (Named-data Link State Routing).
 * See AUTHORS.md for complete list of NLSR authors and contributors.
 *
 * NLSR 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.
 *
 * NLSR 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
 * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "communication/sync-protocol-adapter.hpp"

#include "tests/boost-test.hpp"
#include "tests/io-key-chain-fixture.hpp"

#include <ndn-cxx/util/dummy-client-face.hpp>

namespace nlsr {
namespace test {

using ndn::Name;

class SyncProtocolAdapterFixture : public IoKeyChainFixture
{
public:
  SyncProtocolAdapterFixture()
    : syncPrefix("/localhop/ndn/nlsr/sync")
    , nameLsaUserPrefix("/localhop/ndn/nlsr/LSA/NAME")
    , syncInterestLifetime(60_s)
  {
    syncPrefix.appendVersion(4);
  }

  void
  addNodes()
  {
    for (int i = 0; i < 2; i++) {
      faces[i] = std::make_shared<ndn::DummyClientFace>(m_io, ndn::DummyClientFace::Options{true, true});
      userPrefixes[i] = Name(nameLsaUserPrefix).appendNumber(i);
      nodes[i] = std::make_shared<SyncProtocolAdapter>(
        *faces[i], m_keyChain, SyncProtocol::PSYNC, syncPrefix, userPrefixes[i],
        syncInterestLifetime,
        [i, this] (const Name& updateName, uint64_t highSeq, uint64_t incomingFaceId) {
          prefixToSeq[i].emplace(updateName, highSeq);
        });
    }

    faces[0]->linkTo(*faces[1]);
    advanceClocks(10_ms, 10);
  }

public:
  Name syncPrefix;
  Name nameLsaUserPrefix;
  Name userPrefixes[2];
  ndn::time::milliseconds syncInterestLifetime;
  std::shared_ptr<ndn::DummyClientFace> faces[2];
  std::shared_ptr<SyncProtocolAdapter> nodes[2];
  std::map<Name, uint64_t> prefixToSeq[2];
};

BOOST_AUTO_TEST_SUITE(TestSyncProtocolAdapter)

BOOST_FIXTURE_TEST_CASE(Basic, SyncProtocolAdapterFixture)
{
  addNodes();

  nodes[0]->publishUpdate(userPrefixes[0], 10);
  advanceClocks(1_s, 100);

  auto it = prefixToSeq[1].find(userPrefixes[0]);
  BOOST_CHECK(it != prefixToSeq[1].end());
  BOOST_CHECK_EQUAL(it->first, userPrefixes[0]);
  BOOST_CHECK_EQUAL(it->second, 10);

  nodes[1]->publishUpdate(userPrefixes[1], 100);
  advanceClocks(1_s, 100);

  it = prefixToSeq[0].find(userPrefixes[1]);
  BOOST_CHECK(it != prefixToSeq[0].end());
  BOOST_CHECK_EQUAL(it->first, userPrefixes[1]);
  BOOST_CHECK_EQUAL(it->second, 100);

  Name adjLsaUserPrefix("/localhop/ndn/nlsr/LSA/ADJACENCY");
  nodes[0]->addUserNode(adjLsaUserPrefix);
  advanceClocks(1_s, 100);
  nodes[0]->publishUpdate(adjLsaUserPrefix, 10);
  advanceClocks(1_s, 100);

  it = prefixToSeq[1].find(adjLsaUserPrefix);
  BOOST_CHECK(it != prefixToSeq[1].end());
  BOOST_CHECK_EQUAL(it->first, adjLsaUserPrefix);
  BOOST_CHECK_EQUAL(it->second, 10);
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace test
} // namespace nlsr
