/*
 * 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 "inputlogcsv.hh"
#include "inputcsv.hh"

#include <vector>

class InputLogCSV_test: public CxxTest::TestSuite
{
private:
  std::string csv_file_name ;
  std::string log_file_name ;

public:

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

    csv_file_name = "/tmp/InputLogCSV_test_csv_file.csv" ;
    TestUtil::create_test_csv_file(csv_file_name) ;

    log_file_name = "/tmp/InputLogCSV_test_log_file.csv" ;

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


  ~InputLogCSV_test(void)
  {
    TestUtil::remove_file(csv_file_name) ;
    TestUtil::remove_file(log_file_name) ;
  }


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


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


  void test_inputlogcsv(void)
  {
    // Note: we need to dynamically create and delete an InputLogCSV
    // instance in order to be able to close the file (and therefore
    // flush the stream) before to read it with InputCSV
    InputLogCSV *inputlogcsv1 = new InputLogCSV(log_file_name) ;
    for (auto i = TestUtil::requests.begin() ;
         i != TestUtil::requests.end() ; ++i)
      inputlogcsv1->log_request(**i) ;
    delete inputlogcsv1 ; // Close the output file

    // Even if data is not in the same order, size of the two files
    // must be identical
    TS_ASSERT(TestUtil::file_size_equals(csv_file_name, log_file_name)) ;

    // Read the two files and compare to the reference Request list
    InputCSV inputcsv_csv(csv_file_name) ;
    InputCSV inputcsv_log(log_file_name) ;
    for (auto i = TestUtil::requests.begin() ;
         i != TestUtil::requests.end() ; ++i)
      {
        Stock::clear() ;
        Request request_csv = inputcsv_csv.get_next_request() ;
        TS_ASSERT(TestUtil::request_equals(**i, request_csv)) ;

        Stock::clear() ;
        Request request_log = inputcsv_log.get_next_request() ;
        TS_ASSERT(TestUtil::request_equals(**i, request_log)) ;
      }

    TS_ASSERT(! inputcsv_csv.eof()) ;
    TS_ASSERT(! inputcsv_log.eof()) ;

    TS_ASSERT(! inputcsv_csv.get_next_request()) ;
    TS_ASSERT(! inputcsv_log.get_next_request()) ;

    TS_ASSERT(inputcsv_csv.eof()) ;
    TS_ASSERT(inputcsv_log.eof()) ;
  }

} ;
