testDBConnection.cc
Go to the documentation of this file.
1 
9 #include <utl/Reader.h>
10 #include <cppunit/extensions/HelperMacros.h>
11 #include <mysql/mysql.h>
12 #include <string>
13 #include <iostream>
14 
15 using namespace std;
16 using namespace utl;
17 
18 
19 class DBConnectionTest : public CppUnit::TestFixture {
20 
21  CPPUNIT_TEST_SUITE(DBConnectionTest);
22  CPPUNIT_TEST(testMasterConnection);
23  CPPUNIT_TEST_SUITE_END();
24 
25 public:
26  virtual ~DBConnectionTest() { }
27 
28  void setUp() override { }
29 
30  void tearDown() override { }
31 
32  void
34  {
35  Reader reader(MASTERDBLIST, Reader::eSCHEMA);
36 
37  const string database = "Atm_Aerosol_1_A";
38 
39  bool connectionPresent = false;
40 
41  // top branch = "masterDBList"
42  for (Branch dbB = reader.GetTopBranch().GetFirstChild();
43  dbB; dbB = dbB.GetNextSibling()) {
44 
45  const auto servers = dbB.GetChild("server").Get<vector<string>>();
46 
47  for (const auto& ser : servers) {
48 
49  cerr << "Testing connection to " << ser << endl;
50 
51  const auto userName = dbB.GetChild("userName").Get<string>();
52  const auto password = dbB.GetChild("password").Get<string>();
53  const auto port = dbB.GetChild("port").Get<unsigned int>();
54 
55  MYSQL* const mySQL = mysql_init(nullptr);
56  CPPUNIT_ASSERT(mySQL);
57 
58  if (mysql_real_connect(mySQL, ser.c_str(), userName.c_str(), password.c_str(),
59  database.c_str(), port, nullptr, 0)) {
60  connectionPresent = true;
61  mysql_close(mySQL);
62  break;
63  } else {
64  cerr << "Connection to the database '" << database << "' at "
65  << userName << ':' << password << '@' << ser << ':' << port << " "
66  "failed. Error message: \"" << mysql_error(mySQL) << '"' << endl;
67  }
68 
69  mysql_close(mySQL);
70 
71  }
72 
73  if (connectionPresent)
74  break;
75 
76  }
77 
78  CPPUNIT_ASSERT(connectionPresent);
79  }
80 
81 };
82 
83 
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
Branch GetTopBranch() const
Get the top Branch (represents same entity as document node)
Definition: Reader.h:45
Branch GetNextSibling() const
Get next sibling of this branch.
Definition: Branch.cc:284
Utility for parsing XML files.
Definition: Reader.h:25
Class representing a document branch.
Definition: Branch.h:107
void setUp() override
virtual ~DBConnectionTest()
void tearDown() override
Branch GetFirstChild() const
Get first child of this Branch.
Definition: Branch.cc:98

, generated on Tue Sep 26 2023.