// ApplicationSelectionCatalog_t.cxx #include "dial_app/ApplicationSelectionCatalog.h" #include #include #include "dataset_util/ThreadCredential.h" #include "dataset_catalog/ConnectionResolver.h" #include "dataset_sql/SelectQuery.h" using std::string; using std::cout; using std::endl; using dset::SqlQuery; using dial::ApplicationId; using dial::ApplicationSelectionCatalog; typedef ApplicationSelectionCatalog::IdList IdList; typedef ApplicationSelectionCatalog::Attributes Attributes; namespace { // Display messsage. void msg(string txt) { cout << "--- " << txt << " ---" << endl; } } // end of unnamed namespace int ApplicationSelectionCatalog_t() { msg("Begin testing ApplicationSelectionCatalog"); system("rm -f resolver.dat asc.dat"); msg("Set thread credential owner"); ThreadCredential().set_default_owner("Annette"); cout << ThreadCredential() << endl; msg("Fetch default instance."); assert( ApplicationSelectionCatalog::create_default_instance() == 0 ); ApplicationSelectionCatalog& sc = ApplicationSelectionCatalog::default_instance(); cout << sc << endl; assert( sc.is_valid() ); assert( sc.size() == 0 ); assert( sc.schema().size() == 3 ); msg("Check ID encoding"); cout << sc.id_encoding() << endl; assert( sc.id_encoding() == "uid" ); msg("Insert an entry."); ApplicationId id1(101,201); Attributes atts1; atts1["uid"] = id1.to_string(); atts1["name"] = "entry 1"; atts1["owner"] = "Annette"; cout << atts1 << endl; assert( ! sc.has_name("entry 1") ); assert( sc.insert("entry 1", atts1) == 0 ); cout << sc << endl; assert( sc.size() == 1 ); assert( sc.has_name("entry 1") ); msg("Insert another entry."); ApplicationId id2(101,202); Attributes atts2; atts2["uid"] = id2.to_string(); atts2["owner"] = "Annette"; assert( sc.insert("entry 2", atts2) == 0 ); cout << sc << endl; assert( sc.size() == 2 ); assert( sc.has_name("entry 2") ); assert( sc.id("entry 1") == id1 ); msg("Check attributes"); ApplicationId id3(101,203); assert( sc.attributes("entry 3").size() == 0 ); assert( sc.attributes("entry 1") == atts1 ); msg("Check query count"); SqlQuery q0("name = Herb"); SqlQuery q1("name = 'entry 1'"); SqlQuery q2("owner = Annette"); assert( sc.query_count(q0) == 0 ); assert( sc.query_count(q1) == 1 ); assert( sc.query_count(q2) == 2 ); msg("Check query"); assert( sc.query(q0,100).size() == 0 ); assert( *sc.query(q1,100).begin() == "entry 1" ); assert( sc.query(q2,100).size() == 2 ); msg("Update an entry"); Attributes atts3; atts3["name"] = "my entry"; assert( sc.update("entry 3", atts3) != 0 ); assert( sc.has_name("entry 2") ); assert( ! sc.has_name("my entry") ); assert( sc.update("entry 2", atts3) == 0 ); assert( ! sc.has_name("entry 2") ); assert( sc.has_name("my entry") ); assert( sc.size() == 2 ); msg("Remove an entry"); assert( sc.remove("entry 3") != 0 ); assert( sc.size() == 2 ); assert( sc.remove("my entry") == 0 ); assert( sc.size() == 1 ); return 0; } #ifdef CTEST_MAIN int main() { return ApplicationSelectionCatalog_t(); } #endif