StdAir Logo  0.43.0
C++ Standard Airline IT Library
StandardAirlineITTestSuite.cpp
Go to the documentation of this file.
00001 
00005 // //////////////////////////////////////////////////////////////////////
00006 // Import section
00007 // //////////////////////////////////////////////////////////////////////
00008 // STL
00009 #include <sstream>
00010 #include <fstream>
00011 #include <string>
00012 // Boost MPL
00013 #include <boost/mpl/push_back.hpp>
00014 #include <boost/mpl/vector.hpp>
00015 #include <boost/mpl/at.hpp>
00016 #include <boost/mpl/assert.hpp>
00017 #include <boost/type_traits/is_same.hpp>
00018 // Boost Unit Test Framework (UTF)
00019 #define BOOST_TEST_DYN_LINK
00020 #define BOOST_TEST_MAIN
00021 #define BOOST_TEST_MODULE StdAirTest
00022 #include <boost/test/unit_test.hpp>
00023 // Boost Serialisation
00024 #include <boost/archive/text_oarchive.hpp>
00025 #include <boost/archive/text_iarchive.hpp>
00026 // StdAir
00027 #include <stdair/stdair_inventory_types.hpp>
00028 #include <stdair/service/Logger.hpp>
00029 #include <stdair/STDAIR_Service.hpp>
00030 #include <stdair/basic/float_utils.hpp>
00031 #include <stdair/bom/BomDisplay.hpp>
00032 #include <stdair/bom/BomRoot.hpp>
00033 #include <stdair/bom/BomManager.hpp>
00034 #include <stdair/factory/FacBom.hpp>
00035 #include <stdair/factory/FacBomManager.hpp>
00036 // StdAir Test Suite
00037 #include <test/stdair/StdairTestLib.hpp>
00038 #include <test/stdair/MPInventory.hpp>
00039 
00040 namespace boost_utf = boost::unit_test;
00041 
00042 // (Boost) Unit Test XML Report
00043 std::ofstream utfReportStream ("StandardAirlineITTestSuite_utfresults.xml");
00044 
00048 struct UnitTestConfig {
00050   UnitTestConfig() {
00051     boost_utf::unit_test_log.set_stream (utfReportStream);
00052     boost_utf::unit_test_log.set_format (boost_utf::XML);
00053     boost_utf::unit_test_log.set_threshold_level (boost_utf::log_test_units);
00054     //boost_utf::unit_test_log.set_threshold_level (boost_utf::log_successful_tests);
00055   }
00056 
00058   ~UnitTestConfig() {
00059   }
00060 };
00061 
00062 
00063 // /////////////// Main: Unit Test Suite //////////////
00064 
00065 // Set the UTF configuration (re-direct the output to a specific file)
00066 BOOST_GLOBAL_FIXTURE (UnitTestConfig);
00067 
00068 // Start the test suite
00069 BOOST_AUTO_TEST_SUITE (master_test_suite)
00070 
00071 
00075 BOOST_AUTO_TEST_CASE (float_comparison_test) {
00076   float a = 0.2f;
00077   a = 5*a;
00078   const float b = 1.0f;
00079 
00080   // Test the Boost way
00081   BOOST_CHECK_MESSAGE (a == b, "The two floats (" << a << " and " << b
00082                        << ") should be equal, but are not");
00083   BOOST_CHECK_CLOSE (a, b, 0.0001);
00084 
00085   // Test the Google way
00086   const FloatingPoint<float> lhs (a), rhs (b);
00087   BOOST_CHECK_MESSAGE (lhs.AlmostEquals (rhs),
00088                        "The two floats (" << a << " and " << b
00089                        << ") should be equal, but are not");
00090 }
00091 
00096 BOOST_AUTO_TEST_CASE (mpl_structure_test) {
00097   const stdair::ClassCode_T lBookingClassCodeA ("A");
00098   const stdair_test::BookingClass lA (lBookingClassCodeA);
00099   const stdair_test::Cabin lCabin (lA);
00100 
00101   BOOST_CHECK_EQUAL (lCabin.toString(), lBookingClassCodeA);
00102   BOOST_CHECK_MESSAGE (lCabin.toString() == lBookingClassCodeA,
00103                        "The cabin key, '" << lCabin.toString()
00104                        << "' is not equal to '" << lBookingClassCodeA << "'");
00105 
00106   // MPL
00107   typedef boost::mpl::vector<stdair_test::BookingClass> MPL_BookingClass;
00108   typedef boost::mpl::push_back<MPL_BookingClass,
00109                                 stdair_test::Cabin>::type types;
00110   
00111   if (boost::is_same<stdair_test::BookingClass,
00112                      stdair_test::Cabin::child>::value == false) {
00113     BOOST_REQUIRE ("The two types mut be equal, but are not");
00114   }
00115   
00116   if (boost::is_same<boost::mpl::at_c<types, 1>::type,
00117                      stdair_test::Cabin>::value == false) {
00118     BOOST_REQUIRE ("The type must be stdair_test::Cabin, but is not");
00119   }
00120 }
00121 
00125 BOOST_AUTO_TEST_CASE (stdair_service_initialisation_test) {
00126   // Output log File
00127   const std::string lLogFilename ("StandardAirlineITTestSuite_init.log");
00128     
00129   // Set the log parameters
00130   std::ofstream logOutputFile;
00131   
00132   // Open and clean the log outputfile
00133   logOutputFile.open (lLogFilename.c_str());
00134   logOutputFile.clear();
00135   
00136   // Initialise the stdair BOM
00137   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00138   stdair::STDAIR_Service stdairService (lLogParams);
00139 
00140   // Retrieve (a reference on) the top of the BOM tree
00141   stdair::BomRoot& lBomRoot = stdairService.getBomRoot();
00142 
00143   // Retrieve the BomRoot key, and compare it to the expected one
00144   const std::string& lBomRootKeyStr = lBomRoot.describeKey();
00145   const std::string lBomRootString (" -- ROOT -- ");
00146 
00147   // DEBUG
00148   STDAIR_LOG_DEBUG ("The BOM root key is '" << lBomRootKeyStr
00149                     << "'. It should be equal to '" << lBomRootString << "'");
00150   
00151   BOOST_CHECK_EQUAL (lBomRootKeyStr, lBomRootString);
00152   BOOST_CHECK_MESSAGE (lBomRootKeyStr == lBomRootString,
00153                        "The BOM root key, '" << lBomRootKeyStr
00154                        << "', should be equal to '" << lBomRootString
00155                        << "', but is not.");
00156 
00157   // Build a sample BOM tree
00158   stdairService.buildSampleBom();
00159 
00160   // DEBUG: Display the whole BOM tree
00161   const std::string& lCSVDump = stdairService.csvDisplay();
00162   STDAIR_LOG_DEBUG (lCSVDump);
00163 
00164   // Close the Log outputFile
00165   logOutputFile.close();
00166 }
00167 
00171 BOOST_AUTO_TEST_CASE (bom_structure_instantiation_test) {
00172   // Step 0.0: initialisation
00173   // Create the root of the Bom tree (i.e., a BomRoot object)
00174   stdair::BomRoot& lBomRoot =
00175     stdair::FacBom<stdair::BomRoot>::instance().create();
00176         
00177   // Step 0.1: Inventory level
00178   // Create an Inventory (BA)
00179   const stdair::AirlineCode_T lBAAirlineCode ("BA");
00180   const stdair::InventoryKey lBAKey (lBAAirlineCode);
00181   myprovider::Inventory& lBAInv =
00182     stdair::FacBom<myprovider::Inventory>::instance().create (lBAKey);
00183   stdair::FacBomManager::addToList (lBomRoot, lBAInv);
00184 
00185   BOOST_CHECK_EQUAL (lBAInv.describeKey(), lBAAirlineCode);
00186   BOOST_CHECK_MESSAGE (lBAInv.describeKey() == lBAAirlineCode,
00187                        "The inventory key, '" << lBAInv.describeKey()
00188                        << "', should be equal to '" << lBAAirlineCode
00189                        << "', but is not");
00190 
00191   // Create an Inventory for AF
00192   const stdair::AirlineCode_T lAFAirlineCode ("AF");
00193   const stdair::InventoryKey lAFKey (lAFAirlineCode);
00194   myprovider::Inventory& lAFInv =
00195     stdair::FacBom<myprovider::Inventory>::instance().create (lAFKey);
00196   stdair::FacBomManager::addToList (lBomRoot, lAFInv);
00197 
00198   BOOST_CHECK_EQUAL (lAFInv.describeKey(), lAFAirlineCode);
00199   BOOST_CHECK_MESSAGE (lAFInv.describeKey() == lAFAirlineCode,
00200                        "The inventory key, '" << lAFInv.describeKey()
00201                        << "', should be equal to '" << lAFAirlineCode
00202                        << "', but is not");
00203   
00204   // Browse the inventories
00205   const myprovider::InventoryList_T& lInventoryList =
00206       stdair::BomManager::getList<myprovider::Inventory> (lBomRoot);
00207   const std::string lInventoryKeyArray[2] = {lBAAirlineCode, lAFAirlineCode};
00208   short idx = 0;
00209   for (myprovider::InventoryList_T::const_iterator itInv =
00210          lInventoryList.begin(); itInv != lInventoryList.end();
00211        ++itInv, ++idx) {
00212     const myprovider::Inventory* lInv_ptr = *itInv;
00213     BOOST_REQUIRE (lInv_ptr != NULL);
00214     
00215     BOOST_CHECK_EQUAL (lInventoryKeyArray[idx], lInv_ptr->describeKey());
00216     BOOST_CHECK_MESSAGE (lInventoryKeyArray[idx] == lInv_ptr->describeKey(),
00217                          "They inventory key, '" << lInventoryKeyArray[idx]
00218                          << "', does not match that of the Inventory object: '"
00219                          << lInv_ptr->describeKey() << "'");
00220   }
00221 }
00222 
00226 BOOST_AUTO_TEST_CASE (bom_structure_serialisation_test) {
00227 
00228   // Backup (thanks to Boost.Serialisation) file
00229   const std::string lBackupFilename = "StandardAirlineITTestSuite_serial.txt";
00230 
00231   // Output log File
00232   const std::string lLogFilename ("StandardAirlineITTestSuite_serial.log");
00233     
00234   // Set the log parameters
00235   std::ofstream logOutputFile;
00236   
00237   // Open and clean the log outputfile
00238   logOutputFile.open (lLogFilename.c_str());
00239   logOutputFile.clear();
00240   
00241   // Initialise the stdair BOM
00242   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00243   stdair::STDAIR_Service stdairService (lLogParams);
00244 
00245   // Build a sample BOM tree
00246   stdairService.buildSampleBom();
00247 
00248   // DEBUG: Display the whole BOM tree
00249   const std::string& lCSVDump = stdairService.csvDisplay();
00250   STDAIR_LOG_DEBUG (lCSVDump);
00251 
00252   // Retrieve (a reference on) the top of the BOM tree
00253   stdair::BomRoot& lBomRoot = stdairService.getBomRoot();
00254 
00255   // Retrieve the BomRoot key, and compare it to the expected one
00256   const std::string lBAInvKeyStr ("BA");
00257   stdair::Inventory* lBAInv_ptr = lBomRoot.getInventory (lBAInvKeyStr);
00258 
00259   // DEBUG
00260   STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
00261                     << lBAInvKeyStr << "' key.");
00262 
00263   BOOST_REQUIRE_MESSAGE (lBAInv_ptr != NULL,
00264                          "An Inventory object should exist with the key, '"
00265                          << lBAInvKeyStr << "'.");
00266 
00267   // create and open a character archive for output
00268   std::ofstream ofs (lBackupFilename.c_str());
00269 
00270   // save data to archive
00271   {
00272     boost::archive::text_oarchive oa (ofs);
00273     // write class instance to archive
00274     oa << lBomRoot;
00275     // archive and stream closed when destructors are called
00276   }
00277 
00278   // ... some time later restore the class instance to its orginal state
00279   stdair::BomRoot& lRestoredBomRoot =
00280     stdair::FacBom<stdair::BomRoot>::instance().create();
00281   {
00282     // create and open an archive for input
00283     std::ifstream ifs (lBackupFilename.c_str());
00284     boost::archive::text_iarchive ia(ifs);
00285     // read class state from archive
00286     ia >> lRestoredBomRoot;
00287     // archive and stream closed when destructors are called
00288   }
00289   
00290   // DEBUG: Display the whole BOM tree
00291   std::ostringstream oRestoredCSVDumpStr;
00292   stdair::BomDisplay::csvDisplay (oRestoredCSVDumpStr, lRestoredBomRoot);
00293   STDAIR_LOG_DEBUG (oRestoredCSVDumpStr.str());
00294 
00295   // Retrieve the BomRoot key, and compare it to the expected one
00296   const std::string& lBomRootKeyStr = lRestoredBomRoot.describeKey();
00297   const std::string lBomRootString (" -- ROOT -- ");
00298 
00299   // DEBUG
00300   STDAIR_LOG_DEBUG ("The BOM root key is '" << lBomRootKeyStr
00301                     << "'. It should be equal to '" << lBomRootString << "'");
00302   
00303   BOOST_CHECK_EQUAL (lBomRootKeyStr, lBomRootString);
00304   BOOST_CHECK_MESSAGE (lBomRootKeyStr == lBomRootString,
00305                        "The BOM root key, '" << lBomRootKeyStr
00306                        << "', should be equal to '" << lBomRootString
00307                        << "', but is not.");
00308 
00309   // Retrieve the Inventory
00310   stdair::Inventory* lRestoredBAInv_ptr =
00311     lRestoredBomRoot.getInventory (lBAInvKeyStr);
00312 
00313   // DEBUG
00314   STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
00315                     << lBAInvKeyStr << "' key.");
00316 
00317   BOOST_CHECK_MESSAGE (lRestoredBAInv_ptr != NULL,
00318                        "An Inventory object should exist with the key, '"
00319                        << lBAInvKeyStr << "'.");
00320 
00321   // Close the Log outputFile
00322   logOutputFile.close();
00323 }
00324 
00325 // End the test suite
00326 BOOST_AUTO_TEST_SUITE_END()
00327 
00328