StdAir Logo  1.00.3
C++ Standard Airline IT Object Library
StandardAirlineITTestSuite.cpp
Go to the documentation of this file.
1 
5 // //////////////////////////////////////////////////////////////////////
6 // Import section
7 // //////////////////////////////////////////////////////////////////////
8 // STL
9 #include <sstream>
10 #include <fstream>
11 #include <string>
12 // Boost MPL
13 #include <boost/mpl/push_back.hpp>
14 #include <boost/mpl/vector.hpp>
15 #include <boost/mpl/at.hpp>
16 #include <boost/mpl/assert.hpp>
17 #include <boost/type_traits/is_same.hpp>
18 // Boost Unit Test Framework (UTF)
19 #define BOOST_TEST_DYN_LINK
20 #define BOOST_TEST_MAIN
21 #define BOOST_TEST_MODULE StdAirTest
22 #if BOOST_VERSION >= 103900
23 #include <boost/test/unit_test.hpp>
24 #else // BOOST_VERSION >= 103900
25 #include <boost/test/test_tools.hpp>
26 #include <boost/test/results_reporter.hpp>
27 #include <boost/test/unit_test_suite.hpp>
28 #include <boost/test/output_test_stream.hpp>
29 #include <boost/test/unit_test_log.hpp>
30 #include <boost/test/framework.hpp>
31 #include <boost/test/detail/unit_test_parameters.hpp>
32 #endif // BOOST_VERSION >= 103900
33 // Boost Serialisation
34 #include <boost/archive/text_oarchive.hpp>
35 #include <boost/archive/text_iarchive.hpp>
36 // StdAir
42 #include <stdair/bom/BomRoot.hpp>
46 // StdAir Test Suite
49 
50 namespace boost_utf = boost::unit_test;
51 
52 #if BOOST_VERSION >= 103900
53 
54 // (Boost) Unit Test XML Report
55 std::ofstream utfReportStream ("StandardAirlineITTestSuite_utfresults.xml");
56 
60 struct UnitTestConfig {
62  UnitTestConfig() {
63  boost_utf::unit_test_log.set_stream (utfReportStream);
64  boost_utf::unit_test_log.set_format (boost_utf::XML);
65  boost_utf::unit_test_log.set_threshold_level (boost_utf::log_test_units);
66  // boost_utf::unit_test_log.set_threshold_level (boost_utf::log_successful_tests);
67  }
68 
70  ~UnitTestConfig() {
71  }
72 };
73 
74 
75 // /////////////// Main: Unit Test Suite //////////////
76 
77 // Set the UTF configuration (re-direct the output to a specific file)
78 BOOST_GLOBAL_FIXTURE (UnitTestConfig);
79 
80 // Start the test suite
81 BOOST_AUTO_TEST_SUITE (master_test_suite)
82 
83 
87 BOOST_AUTO_TEST_CASE (float_comparison_test) {
88  float a = 0.2f;
89  a = 5*a;
90  const float b = 1.0f;
91 
92  // Test the Boost way
93  BOOST_CHECK_MESSAGE (a == b, "The two floats (" << a << " and " << b
94  << ") should be equal, but are not");
95  BOOST_CHECK_CLOSE (a, b, 0.0001);
96 
97  // Test the Google way
98  const FloatingPoint<float> lhs (a), rhs (b);
99  BOOST_CHECK_MESSAGE (lhs.AlmostEquals (rhs),
100  "The two floats (" << a << " and " << b
101  << ") should be equal, but are not");
102 }
103 
108 BOOST_AUTO_TEST_CASE (mpl_structure_test) {
109  const stdair::ClassCode_T lBookingClassCodeA ("A");
110  const stdair_test::BookingClass lA (lBookingClassCodeA);
111  const stdair_test::Cabin lCabin (lA);
112 
113  BOOST_CHECK_EQUAL (lCabin.toString(), lBookingClassCodeA);
114  BOOST_CHECK_MESSAGE (lCabin.toString() == lBookingClassCodeA,
115  "The cabin key, '" << lCabin.toString()
116  << "' is not equal to '" << lBookingClassCodeA << "'");
117 
118  // MPL
119  typedef boost::mpl::vector<stdair_test::BookingClass> MPL_BookingClass;
120  typedef boost::mpl::push_back<MPL_BookingClass,
121  stdair_test::Cabin>::type types;
122 
123  if (boost::is_same<stdair_test::BookingClass,
124  stdair_test::Cabin::child>::value == false) {
125  BOOST_ERROR ("The two types mut be equal, but are not");
126  }
127 
128  if (boost::is_same<boost::mpl::at_c<types, 1>::type,
129  stdair_test::Cabin>::value == false) {
130  BOOST_ERROR ("The type must be stdair_test::Cabin, but is not");
131  }
132 }
133 
137 BOOST_AUTO_TEST_CASE (stdair_service_initialisation_test) {
138  // Output log File
139  const std::string lLogFilename ("StandardAirlineITTestSuite_init.log");
140 
141  // Set the log parameters
142  std::ofstream logOutputFile;
143 
144  // Open and clean the log outputfile
145  logOutputFile.open (lLogFilename.c_str());
146  logOutputFile.clear();
147 
148  // Initialise the stdair BOM
149  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
150  stdair::STDAIR_Service stdairService (lLogParams);
151 
152  // Retrieve (a reference on) the top of the persistent BOM tree
153  stdair::BomRoot& lPersistentBomRoot = stdairService.getPersistentBomRoot();
154 
155  // Retrieve the BomRoot key, and compare it to the expected one
156  const std::string& lBomRootKeyStr = lPersistentBomRoot.describeKey();
157  const std::string lBomRootString (" -- ROOT -- ");
158 
159  // DEBUG
160  STDAIR_LOG_DEBUG ("The BOM root key is '" << lBomRootKeyStr
161  << "'. It should be equal to '" << lBomRootString << "'");
162 
163  BOOST_CHECK_EQUAL (lBomRootKeyStr, lBomRootString);
164  BOOST_CHECK_MESSAGE (lBomRootKeyStr == lBomRootString,
165  "The BOM root key, '" << lBomRootKeyStr
166  << "', should be equal to '" << lBomRootString
167  << "', but is not.");
168 
169  // Build a sample BOM tree
170  stdairService.buildSampleBom();
171 
172  // DEBUG: Display the whole BOM tree
173  const std::string& lCSVDump = stdairService.csvDisplay ();
174  STDAIR_LOG_DEBUG (lCSVDump);
175 
176  // Close the Log outputFile
177  logOutputFile.close();
178 }
179 
183 BOOST_AUTO_TEST_CASE (bom_structure_instantiation_test) {
184  // Step 0.0: initialisation
185  // Create the root of a Bom tree (i.e., a BomRoot object)
186  stdair::BomRoot& lBomRoot =
188 
189  // Step 0.1: Inventory level
190  // Create an Inventory (BA)
191  const stdair::AirlineCode_T lBAAirlineCode ("BA");
192  const stdair::InventoryKey lBAKey (lBAAirlineCode);
193  myprovider::Inventory& lBAInv =
195  stdair::FacBomManager::addToList (lBomRoot, lBAInv);
196 
197  BOOST_CHECK_EQUAL (lBAInv.describeKey(), lBAAirlineCode);
198  BOOST_CHECK_MESSAGE (lBAInv.describeKey() == lBAAirlineCode,
199  "The inventory key, '" << lBAInv.describeKey()
200  << "', should be equal to '" << lBAAirlineCode
201  << "', but is not");
202 
203  // Create an Inventory for AF
204  const stdair::AirlineCode_T lAFAirlineCode ("AF");
205  const stdair::InventoryKey lAFKey (lAFAirlineCode);
206  myprovider::Inventory& lAFInv =
208  stdair::FacBomManager::addToList (lBomRoot, lAFInv);
209 
210  BOOST_CHECK_EQUAL (lAFInv.describeKey(), lAFAirlineCode);
211  BOOST_CHECK_MESSAGE (lAFInv.describeKey() == lAFAirlineCode,
212  "The inventory key, '" << lAFInv.describeKey()
213  << "', should be equal to '" << lAFAirlineCode
214  << "', but is not");
215 
216  // Browse the inventories
217  const myprovider::InventoryList_T& lInventoryList =
218  stdair::BomManager::getList<myprovider::Inventory> (lBomRoot);
219  const std::string lInventoryKeyArray[2] = {lBAAirlineCode, lAFAirlineCode};
220  short idx = 0;
221  for (myprovider::InventoryList_T::const_iterator itInv =
222  lInventoryList.begin(); itInv != lInventoryList.end();
223  ++itInv, ++idx) {
224  const myprovider::Inventory* lInv_ptr = *itInv;
225  BOOST_REQUIRE (lInv_ptr != NULL);
226 
227  BOOST_CHECK_EQUAL (lInventoryKeyArray[idx], lInv_ptr->describeKey());
228  BOOST_CHECK_MESSAGE (lInventoryKeyArray[idx] == lInv_ptr->describeKey(),
229  "They inventory key, '" << lInventoryKeyArray[idx]
230  << "', does not match that of the Inventory object: '"
231  << lInv_ptr->describeKey() << "'");
232  }
233 }
234 
238 BOOST_AUTO_TEST_CASE (bom_structure_serialisation_test) {
239 
240  // Backup (thanks to Boost.Serialisation) file
241  const std::string lBackupFilename = "StandardAirlineITTestSuite_serial.txt";
242 
243  // Output log File
244  const std::string lLogFilename ("StandardAirlineITTestSuite_serial.log");
245 
246  // Set the log parameters
247  std::ofstream logOutputFile;
248 
249  // Open and clean the log outputfile
250  logOutputFile.open (lLogFilename.c_str());
251  logOutputFile.clear();
252 
253  // Initialise the stdair BOM
254  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
255  stdair::STDAIR_Service stdairService (lLogParams);
256 
257  // Build a sample BOM tree
258  stdairService.buildSampleBom();
259 
260  // Retrieve (a reference on) the top of the persistent BOM tree
261  stdair::BomRoot& lPersistentBomRoot = stdairService.getPersistentBomRoot();
262 
263  // DEBUG: Display the whole BOM tree
264  const std::string& lCSVDump = stdairService.csvDisplay ();
265  STDAIR_LOG_DEBUG (lCSVDump);
266 
267  // Clone the persistent BOM
268  stdairService.clonePersistentBom ();
269 
270  // Retrieve the BomRoot key, and compare it to the expected one
271  const std::string lBAInvKeyStr ("BA");
272  stdair::Inventory* lBAInv_ptr =
273  lPersistentBomRoot.getInventory (lBAInvKeyStr);
274 
275  // DEBUG
276  STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
277  << lBAInvKeyStr << "' key.");
278 
279  BOOST_REQUIRE_MESSAGE (lBAInv_ptr != NULL,
280  "An Inventory object should exist with the key, '"
281  << lBAInvKeyStr << "'.");
282 
283  // create and open a character archive for output
284  std::ofstream ofs (lBackupFilename.c_str());
285 
286  // save data to archive
287  {
288  boost::archive::text_oarchive oa (ofs);
289  // write class instance to archive
290  oa << lPersistentBomRoot;
291  // archive and stream closed when destructors are called
292  }
293 
294  // ... some time later restore the class instance to its orginal state
295  stdair::BomRoot& lRestoredBomRoot =
297  {
298  // create and open an archive for input
299  std::ifstream ifs (lBackupFilename.c_str());
300  boost::archive::text_iarchive ia(ifs);
301  // read class state from archive
302  ia >> lRestoredBomRoot;
303  // archive and stream closed when destructors are called
304  }
305 
306  // DEBUG: Display the whole restored BOM tree
307  const std::string& lRestoredCSVDump =
308  stdairService.csvDisplay(lRestoredBomRoot);
309  STDAIR_LOG_DEBUG (lRestoredCSVDump);
310 
311  // Retrieve the BomRoot key, and compare it to the expected one
312  const std::string& lBomRootKeyStr = lRestoredBomRoot.describeKey();
313  const std::string lBomRootString (" -- ROOT -- ");
314 
315  // DEBUG
316  STDAIR_LOG_DEBUG ("The BOM root key is '" << lBomRootKeyStr
317  << "'. It should be equal to '" << lBomRootString << "'");
318 
319  BOOST_CHECK_EQUAL (lBomRootKeyStr, lBomRootString);
320  BOOST_CHECK_MESSAGE (lBomRootKeyStr == lBomRootString,
321  "The BOM root key, '" << lBomRootKeyStr
322  << "', should be equal to '" << lBomRootString
323  << "', but is not.");
324 
325  // Retrieve the Inventory
326  stdair::Inventory* lRestoredBAInv_ptr =
327  lRestoredBomRoot.getInventory (lBAInvKeyStr);
328 
329  // DEBUG
330  STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
331  << lBAInvKeyStr << "' key in the restored BOM root.");
332 
333  BOOST_CHECK_MESSAGE (lRestoredBAInv_ptr != NULL,
334  "An Inventory object should exist with the key, '"
335  << lBAInvKeyStr << "' in the restored BOM root.");
336 
337  // Close the Log outputFile
338  logOutputFile.close();
339 }
340 
344 BOOST_AUTO_TEST_CASE (bom_structure_clone_test) {
345 
346  // Output log File
347  const std::string lLogFilename ("StandardAirlineITTestSuite_clone.log");
348 
349  // Set the log parameters
350  std::ofstream logOutputFile;
351 
352  // Open and clean the log outputfile
353  logOutputFile.open (lLogFilename.c_str());
354  logOutputFile.clear();
355 
356  // Initialise the stdair BOM
357  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
358  stdair::STDAIR_Service stdairService (lLogParams);
359 
360  // Build a sample BOM tree
361  stdairService.buildSampleBom();
362 
363  // Retrieve (a constant reference on) the top of the persistent BOM tree
364  const stdair::BomRoot& lPersistentBomRoot =
365  stdairService.getPersistentBomRoot();
366 
367  // DEBUG: Display the whole persistent BOM tree
368  const std::string& lCSVDump = stdairService.csvDisplay ();
369  STDAIR_LOG_DEBUG ("Display the persistent BOM tree.");
370  STDAIR_LOG_DEBUG (lCSVDump);
371 
372  // Clone the persistent BOM
373  stdairService.clonePersistentBom ();
374 
375  // Retrieve (a reference on) the top of the clone BOM tree
376  stdair::BomRoot& lCloneBomRoot = stdairService.getBomRoot();
377 
378  // DEBUG: Display the clone BOM tree after the clone process.
379  const std::string& lAfterCloneCSVDump =
380  stdairService.csvDisplay(lCloneBomRoot);
381  STDAIR_LOG_DEBUG ("Display the clone BOM tree after the clone process.");
382  STDAIR_LOG_DEBUG (lAfterCloneCSVDump);
383 
384  // Retrieve the clone BomRoot key, and compare it to the persistent BomRoot
385  // key.
386  const std::string& lCloneBomRootKeyStr = lCloneBomRoot.describeKey();
387  const std::string& lPersistentBomRootKeyStr =
388  lPersistentBomRoot.describeKey();
389 
390  // DEBUG
391  STDAIR_LOG_DEBUG ("The clone BOM root key is '" << lCloneBomRootKeyStr
392  << "'. It should be equal to '"
393  << lPersistentBomRootKeyStr << "'");
394 
395  BOOST_CHECK_EQUAL (lCloneBomRootKeyStr, lPersistentBomRootKeyStr);
396  BOOST_CHECK_MESSAGE (lCloneBomRootKeyStr == lPersistentBomRootKeyStr,
397  "The clone BOM root key, '" << lCloneBomRootKeyStr
398  << "', should be equal to '" << lPersistentBomRootKeyStr
399  << "', but is not.");
400 
401  // Retrieve the BA inventory in the clone BOM root
402  const std::string lBAInvKeyStr ("BA");
403  stdair::Inventory* lCloneBAInv_ptr =
404  lCloneBomRoot.getInventory (lBAInvKeyStr);
405 
406  // DEBUG
407  STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
408  << lBAInvKeyStr << "' key in the clone BOM root.");
409 
410  BOOST_CHECK_MESSAGE (lCloneBAInv_ptr != NULL,
411  "An Inventory object should exist with the key, '"
412  << lBAInvKeyStr << "' in the clone BOM root.");
413 
414  // Close the Log outputFile
415  logOutputFile.close();
416 }
417 
418 // End the test suite
419 BOOST_AUTO_TEST_SUITE_END()
420 
421 #else // BOOST_VERSION >= 103900
422 boost_utf::test_suite* init_unit_test_suite (int, char* []) {
423  boost_utf::test_suite* test = BOOST_TEST_SUITE ("Unit test example 1");
424  return test;
425 }
426 #endif // BOOST_VERSION >= 103900
427 
static FacBom & instance()
Definition: FacBom.hpp:84
std::string AirlineCode_T
Key of a given inventory, made of the airline code.
Structure holding parameters for logging.
Class representing the actual attributes for an airline inventory.
Definition: Inventory.hpp:34
static void addToList(OBJECT1 &, OBJECT2 &)
Inventory * getInventory(const std::string &iInventoryKeyStr) const
Definition: BomRoot.cpp:43
Class representing the actual attributes for the Bom root.
Definition: BomRoot.hpp:32
std::list< Inventory * > InventoryList_T
BOM & create()
Definition: FacBom.hpp:112
const std::string describeKey() const
Definition: BomRoot.hpp:131
Interface for the STDAIR Services.
std::string ClassCode_T
#define STDAIR_LOG_DEBUG(iToBeLogged)
Definition: Logger.hpp:32