// ApplicationRepository_t.cxx #include "dial_app/ApplicationRepository.h" #include "dataset_catalog/SqlGenericRepository.h" #include "dataset_catalog/ConnectionResolver.h" #include "dataset_id/SimpleUniqueIdGenerator.h" #include "dataset_sql/SqlResultTable.h" #include "dataset_sql/SqlResult.h" #include "dataset_util/Text.h" #include "dataset_util/XmlElement.h" #include "dataset_credential/CredentialSelectionCatalog.h" #include "dataset_credential/GssCredentialManager.h" #include #include #include using std::string; using std::cout; using std::endl; using dset::SqlResult; using dset::SqlResultTable; using dset::SqlGenericRepository; using dset::ConnectionResolver; using dset::CredentialSelectionCatalog; using dset::GssCredentialManager; using dial::Application; using dial::ApplicationId; using dial::ApplicationRepository; namespace { void msg(string txt) { cout << "--- " << txt << " ---" << endl; } } // end unnamed namespace int ApplicationRepository_t() { msg("Begin testing ApplicationRepository"); msg("Constructor from a generic repository"); //Constructing a Result set to construct a SqlResultTable. string names[9] = { "identifier", "idhi", "idlo", "sxml", "owner", "ctime", "comment", "created", "lastModified" }; SqlResult::NameList cols(names, names+9); SqlResult res(cols); int ustat = SimpleUniqueIdGenerator::create_collection("Application", 200, 0); assert( ustat == 0 ); SimpleUniqueIdGenerator::set_as_default(); msg("Fetch default instance of CSC."); assert( CredentialSelectionCatalog::create_default_instance() == 0 ); CredentialSelectionCatalog& sc = CredentialSelectionCatalog::default_instance(); cout << sc << endl; assert( sc.is_valid() ); string owner = "me"; assert( GssCredentialManager::set_default() == 0 ); assert( GssCredentialManager::set_owner("me") == 0 ); msg("Create application 1 from files"); Text abc("abc.dat"); abc.append("Testing ApplicationRepository-1"); abc.write(); Application::NameList files1; files1.push_back("abc.dat"); Application app1(files1); assert(app1.is_valid()); const XmlElement *pele = app1.xml(); string xml1 = pele->to_xml_text(); msg("Create application 2 from files"); Text def("def.dat"); def.append("Testing ApplicationRepository-2"); def.write(); Application::NameList files2; files2.push_back("def.dat"); Application app2(files2); assert(app2.is_valid()); pele = app2.xml(); string xml2 = pele->to_xml_text(); msg("Insert row specifying type"); SqlResult::Row row0; row0["idhi"] = "0"; row0["idlo"] = "0"; row0["sxml"] = "application"; res.insert_row(row0); assert( res.num_rows() == 1 ); msg("Insert a row"); SqlResult::Row row1; row1["idhi"] = "400"; row1["idlo"] = "1"; row1["sxml"] = xml1; res.insert_row(row1); assert( res.num_rows() == 2 ); msg("Insert another row"); SqlResult::Row row2; row2["idhi"] = "400"; row2["idlo"] = "2"; row2["sxml"] = xml2; assert( res.insert_row(row2) == 0 ); assert( res.num_rows() == 3 ); SqlResultTable sqltable(res); SqlGenericRepository sqlgr(sqltable); assert( sqlgr.is_valid() ); ApplicationRepository trep(&sqlgr); msg("Testing validity"); assert(trep.is_valid()); assert( trep.size() == 2 ); Text run("run"); run.append("#!/bin/sh"); run.append(""); run.append("date"); run.write(); Text conf("config.dat"); conf.append("min = -20.0"); conf.append("max = 40.0"); conf.append("nbin = 150"); conf.write(); msg("Create application from files"); Application::NameList files; files.push_back("run"); files.push_back("config.dat"); Application app(files); assert( app.is_valid() ); msg("Write to repository"); if ( ! trep.insert(&app).is_valid() ) { cout << "Insert failed with error: " << trep.error() << endl; cout << trep.error_message() << endl; assert(false); } assert( trep.size() == 3 ); msg("Check valid application"); if ( !trep.has(app.id()) ) { cout << "Check failed with error: " << trep.error() << endl; cout << trep.error_message() << endl; assert(false); } msg("Read from repository"); const Application *papp = trep.extract(app.id()); if ( papp == 0 || !papp->is_valid() ) { cout << "Extract failed with error: " << trep.error() << endl; cout << trep.error_message() << endl; assert(false); } cout << *papp << endl; msg("Remove from repository"); if ( trep.remove(app.id()) ) { cout << "Delete failed with error: " << trep.error() << endl; cout << trep.error_message() << endl; assert(false); } assert( trep.size() == 2 ); msg("Verify all entries"); if( !trep.verify() ) { cout << "Verification failed" << endl; assert(false); } msg("Define connection resolver"); Text txt("resolver.dat");; txt.append("ApplicationRepository"); txt.append("SQLRESULT:ar.dat"); txt.write(); ConnectionResolver::set_configuration_file("resolver.dat"); msg("Construct file description of AR"); system("rm -f ar.dat"); Text ttr("ar.dat"); ttr.append("SQLRESULT"); ttr.append("idhi,idlo,sxml"); ttr.append("0,0,ApplicationRepository"); ttr.write(); msg("Testing default instance"); ApplicationRepository& tr2 = ApplicationRepository::default_instance(); assert( tr2.is_valid() ); msg("All tests passed successfully"); return 0; } #ifdef CTEST_MAIN int main() { return ApplicationRepository_t(); } #endif