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