/*
 * This file is part of the Owl Positioning System (OwlPS) project.
 * It is subject to the copyright notice and license terms in the
 * COPYRIGHT.t2t file found in the top-level directory of this
 * distribution and at
 *   https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
 * No part of the OwlPS Project, including this file, may be copied,
 * modified, propagated, or distributed except according to the terms
 * contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
 * distributed along with this file, either separately or by replacing
 * this notice by the COPYRIGHT.t2t file's contents.
 */

#include <cxxtest/TestSuite.h>

#include "inputcsv.hh"
#include "stock.hh"

#include <string>
#include <fstream>
#include <cstdio>

class InputCSV_test: public CxxTest::TestSuite
{
private:
  std::string csv_file_name ; // Test CSV file name
  std::vector<CapturePoint> aps ; // List of test CapturePoint


public:
  InputCSV_test(void)
  {
    // If we cannot open the file, we want to stop the test
    CxxTest::setAbortTestOnFail(true) ;

    csv_file_name = "/tmp/InputCSV_test_csv_file.csv" ;
    TestUtil::create_test_csv_file(csv_file_name, true) ;

    // Back to the normal behaviour (i.e. do not abort on fail)
    CxxTest::setAbortTestOnFail(false) ;
  }


  ~InputCSV_test(void)
  {
    // Delete the test CSV file
    TestUtil::remove_file(csv_file_name) ;
  }


  static InputCSV_test* createSuite(void)
  {
    return new InputCSV_test() ;
  }


  static void destroySuite(InputCSV_test *suite)
  {
    delete suite ;
  }


  void test_inputcsv(void)
  {
    // Opening file
    InputCSV inputcsv1(csv_file_name) ;
    TS_ASSERT(inputcsv1) ;
    TS_ASSERT(! inputcsv1.eof()) ;

    Request request1 ;

    // First Request //

    request1 = inputcsv1.get_next_request() ;
    TS_ASSERT(request1) ;
    assert(request1.get_mobile()) ;
    TS_ASSERT_EQUALS(request1.get_mobile()->get_mac_addr(),
                     TestUtil::mobiles[0].get_mac_addr()) ;
    TS_ASSERT_EQUALS(request1.get_time_sent(),
                     TestUtil::requests.at(0)->get_time_sent()) ;

    for (auto i = TestUtil::aps.begin() ; i != TestUtil::aps.end() ; ++i)
      {
        auto measurement_it1 =
          request1.get_measurements().find(i->get_mac_addr()) ;
        TS_ASSERT(request1.get_measurements().end() != measurement_it1) ;

        // We cannot compare the complete ss_list since there is not a
        // Measurement::get_ss_list() any more.
      }

    TS_ASSERT(inputcsv1) ;
    TS_ASSERT(! inputcsv1.eof()) ;

    // Second Request //

    request1 = inputcsv1.get_next_request() ;
    TS_ASSERT(request1) ;
    assert(request1.get_mobile()) ;
    TS_ASSERT_EQUALS(request1.get_mobile()->get_mac_addr(),
                     TestUtil::mobiles[1].get_mac_addr()) ;
    TS_ASSERT_EQUALS(request1.get_time_sent(),
                     TestUtil::requests.at(1)->get_time_sent()) ;

    for (auto i = TestUtil::aps.begin() ; i != TestUtil::aps.end() ; ++i)
      {
        auto measurement_it1 =
          request1.get_measurements().find(i->get_mac_addr()) ;
        TS_ASSERT(request1.get_measurements().end() != measurement_it1) ;
      }

    // Third Request //

    TS_ASSERT(inputcsv1) ;
    TS_ASSERT(request1) ;
    assert(request1.get_mobile()) ;
    TS_ASSERT(! inputcsv1.eof()) ;

    request1 = inputcsv1.get_next_request() ;
    TS_ASSERT_EQUALS(request1.get_mobile()->get_mac_addr(),
                     TestUtil::mobiles[0].get_mac_addr()) ;
    TS_ASSERT_EQUALS(request1.get_time_sent(),
                     TestUtil::requests.at(2)->get_time_sent()) ;

    for (auto i = TestUtil::aps.begin() ; i != TestUtil::aps.end() ; ++i)
      {
        auto measurement_it1 =
          request1.get_measurements().find(i->get_mac_addr()) ;
        TS_ASSERT(request1.get_measurements().end() != measurement_it1) ;
      }

    // End of file

    TS_ASSERT(inputcsv1) ;
    TS_ASSERT(! inputcsv1.eof()) ;

    request1 = inputcsv1.get_next_request() ;
    TS_ASSERT(! request1) ;
    TS_ASSERT(! inputcsv1) ;
    TS_ASSERT(inputcsv1.eof()) ;
  }

} ;
