From b902abb8e65076ba8a2abc5200620e37f9ce0da6 Mon Sep 17 00:00:00 2001 From: fgerlits Date: Mon, 4 Oct 2004 09:00:25 +0000 Subject: [PATCH] this is a simple wrapper class; takes an xmlrpc method call, extracts the UniqueId of the playlist, and calls the appropriate method of a StorageClient --- .../scheduler/src/DeletePlaylistMethod.cxx | 140 ++++++++++++++ .../scheduler/src/DeletePlaylistMethod.h | 153 +++++++++++++++ .../src/DeletePlaylistMethodTest.cxx | 174 ++++++++++++++++++ .../scheduler/src/DeletePlaylistMethodTest.h | 145 +++++++++++++++ 4 files changed, 612 insertions(+) create mode 100644 livesupport/products/scheduler/src/DeletePlaylistMethod.cxx create mode 100644 livesupport/products/scheduler/src/DeletePlaylistMethod.h create mode 100644 livesupport/products/scheduler/src/DeletePlaylistMethodTest.cxx create mode 100644 livesupport/products/scheduler/src/DeletePlaylistMethodTest.h diff --git a/livesupport/products/scheduler/src/DeletePlaylistMethod.cxx b/livesupport/products/scheduler/src/DeletePlaylistMethod.cxx new file mode 100644 index 000000000..ba58b3e47 --- /dev/null +++ b/livesupport/products/scheduler/src/DeletePlaylistMethod.cxx @@ -0,0 +1,140 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: fgerlits $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/Attic/DeletePlaylistMethod.cxx,v $ + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include + +#include "LiveSupport/Core/StorageClientInterface.h" +#include "LiveSupport/Storage/StorageClientFactory.h" +#include "ScheduleInterface.h" +#include "ScheduleFactory.h" + +#include "DeletePlaylistMethod.h" + + +using namespace LiveSupport; +using namespace LiveSupport::Core; +using namespace LiveSupport::Storage; + +using namespace LiveSupport::Scheduler; + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + +/*------------------------------------------------------------------------------ + * The name of this XML-RPC method. + *----------------------------------------------------------------------------*/ +const std::string DeletePlaylistMethod::methodName = "deletePlaylist"; + +/*------------------------------------------------------------------------------ + * The name of the playlistId member in the XML-RPC parameter + * structure. + *----------------------------------------------------------------------------*/ +const std::string DeletePlaylistMethod::playlistIdName = "playlistId"; + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Construct the method and register it right away. + *----------------------------------------------------------------------------*/ +DeletePlaylistMethod :: DeletePlaylistMethod ( + Ptr::Ref xmlRpcServer) throw() + : XmlRpc::XmlRpcServerMethod(methodName, xmlRpcServer.get()) +{ +} + + +/*------------------------------------------------------------------------------ + * Extract the UniqueId from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +DeletePlaylistMethod :: extractPlaylistId( + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(playlistIdName)) { + throw std::invalid_argument("no playlist id in parameter structure"); + } + + Ptr::Ref id(new UniqueId((int) xmlRpcValue[playlistIdName])); + return id; +} + + +/*------------------------------------------------------------------------------ + * Execute the XML-RPC function call. + * (Overrides 'execute' in XmlRpcServerMethod.) + *----------------------------------------------------------------------------*/ +void +DeletePlaylistMethod :: execute(XmlRpc::XmlRpcValue & parameters, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + try { + if (!parameters.valid()) { + // TODO: mark error + returnValue = XmlRpc::XmlRpcValue(false); + return; + } + + Ptr::Ref id = extractPlaylistId(parameters[0]); + + Ptr::Ref scf; + Ptr::Ref storage; + + scf = StorageClientFactory::getInstance(); + storage = scf->getStorageClient(); + + if (!storage->existsPlaylist(id)) { + // TODO: mark error + returnValue = XmlRpc::XmlRpcValue(false); + return; + } + + storage->deletePlaylist(id); + + returnValue = XmlRpc::XmlRpcValue(true); + + } catch (std::invalid_argument &e) { + // TODO: mark error + returnValue = XmlRpc::XmlRpcValue(false); + return; + } +} diff --git a/livesupport/products/scheduler/src/DeletePlaylistMethod.h b/livesupport/products/scheduler/src/DeletePlaylistMethod.h new file mode 100644 index 000000000..8f5b0f5b5 --- /dev/null +++ b/livesupport/products/scheduler/src/DeletePlaylistMethod.h @@ -0,0 +1,153 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: fgerlits $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/Attic/DeletePlaylistMethod.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef DeletePlaylistMethod_h +#define DeletePlaylistMethod_h + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include +#include +#include +#include + +#include "LiveSupport/Core/Ptr.h" +#include "LiveSupport/Core/Playlist.h" + + +namespace LiveSupport { +namespace Scheduler { + +using namespace LiveSupport; +using namespace LiveSupport::Core; + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/** + * An XML-RPC method object to delete a playlist given by its playlist id. + * + * The name of the method when called through XML-RPC is "deletePlaylist". + * The expected parameter is an XML-RPC structure, with the following + * member: + *
    + *
  • playlistId - int - the unique id of the playlist + * to be deleted.
  • + *
+ * + * The XML-RPC function returns the XML-RPC value: + *
    + *
  • true if the operation completed successfully,
  • + *
  • false if playlist is not found or there was some other error.
  • + *
+ * + * @author $Author: fgerlits $ + * @version $Revision: 1.1 $ + */ +class DeletePlaylistMethod : public XmlRpc::XmlRpcServerMethod +{ + private: + /** + * The name of this method, as it will be registered into the + * XML-RPC server. + */ + static const std::string methodName; + + /** + * The name of the playlistId member in the XML-RPC parameter + * structure. + */ + static const std::string playlistIdName; + + /** + * Extract the playlist id from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return a UniqueId that was found in the XML-RPC parameter. + * @exception std::invalid_argument if there was no UniqueId + * in xmlRpcValue + */ + Ptr::Ref + extractPlaylistId(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + public: + /** + * A default constructor, for testing purposes. + */ + DeletePlaylistMethod(void) throw () + : XmlRpc::XmlRpcServerMethod(methodName) + { + } + + /** + * Constuctor that registers the method with the server right away. + * + * @param xmlRpcServer the XML-RPC server to register with. + */ + DeletePlaylistMethod( + Ptr::Ref xmlRpcServer) + throw (); + + /** + * Execute the display schedule command on the Scheduler daemon. + * + * @param parameters XML-RPC function call parameters + * @param returnValue the return value of the call (out parameter) + */ + void + execute( XmlRpc::XmlRpcValue & parameters, + XmlRpc::XmlRpcValue & returnValue) throw (); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace Scheduler +} // namespace LiveSupport + +#endif // DisplayPlaylistMethod_h + diff --git a/livesupport/products/scheduler/src/DeletePlaylistMethodTest.cxx b/livesupport/products/scheduler/src/DeletePlaylistMethodTest.cxx new file mode 100644 index 000000000..ee34ba6b5 --- /dev/null +++ b/livesupport/products/scheduler/src/DeletePlaylistMethodTest.cxx @@ -0,0 +1,174 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: fgerlits $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/Attic/DeletePlaylistMethodTest.cxx,v $ + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#if HAVE_UNISTD_H +#include +#else +#error "Need unistd.h" +#endif + + +#include +#include +#include + +#include "LiveSupport/Db/ConnectionManagerFactory.h" +#include "LiveSupport/Storage/StorageClientFactory.h" +#include "DeletePlaylistMethod.h" +#include "DeletePlaylistMethodTest.h" + + +using namespace LiveSupport::Db; +using namespace LiveSupport::Storage; +using namespace LiveSupport::Scheduler; + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + +CPPUNIT_TEST_SUITE_REGISTRATION(DeletePlaylistMethodTest); + +/** + * The name of the configuration file for the storage client factory. + */ +const std::string DeletePlaylistMethodTest::storageClientConfig = + "etc/storageClient.xml"; + +/** + * The name of the configuration file for the connection manager factory. + */ +const std::string DeletePlaylistMethodTest::connectionManagerConfig = + "etc/connectionManagerFactory.xml"; + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Configure a Configurable with an XML file. + *----------------------------------------------------------------------------*/ +void +DeletePlaylistMethodTest :: configure( + Ptr::Ref configurable, + const std::string fileName) + throw (std::invalid_argument, + xmlpp::exception) +{ + Ptr::Ref parser(new xmlpp::DomParser(fileName, true)); + const xmlpp::Document * document = parser->get_document(); + const xmlpp::Element * root = document->get_root_node(); + + configurable->configure(*root); +} + + +/*------------------------------------------------------------------------------ + * Set up the test environment + *----------------------------------------------------------------------------*/ +void +DeletePlaylistMethodTest :: setUp(void) throw () +{ + try { + Ptr::Ref scf + = StorageClientFactory::getInstance(); + configure(scf, storageClientConfig); + + Ptr::Ref cmf + = ConnectionManagerFactory::getInstance(); + configure(cmf, connectionManagerConfig); + + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL("semantic error in configuration file"); + } catch (xmlpp::exception &e) { + CPPUNIT_FAIL("error parsing configuration file"); + } catch (std::exception &e) { + CPPUNIT_FAIL(e.what()); + } +} + + +/*------------------------------------------------------------------------------ + * Clean up the test environment + *----------------------------------------------------------------------------*/ +void +DeletePlaylistMethodTest :: tearDown(void) throw () +{ +} + + +/*------------------------------------------------------------------------------ + * Just a very simple smoke test + *----------------------------------------------------------------------------*/ +void +DeletePlaylistMethodTest :: firstTest(void) + throw (CPPUNIT_NS::Exception) +{ + Ptr::Ref method(new DeletePlaylistMethod()); + XmlRpc::XmlRpcValue rootParameter; + XmlRpc::XmlRpcValue parameters; + XmlRpc::XmlRpcValue result; + + // set up a structure for the parameters + parameters["playlistId"] = 1; + rootParameter[0] = parameters; + + method->execute(rootParameter, result); + CPPUNIT_ASSERT(((bool) result) == true); +} + + +/*------------------------------------------------------------------------------ + * A very simple negative test + *----------------------------------------------------------------------------*/ +void +DeletePlaylistMethodTest :: negativeTest(void) + throw (CPPUNIT_NS::Exception) +{ + Ptr::Ref method(new DeletePlaylistMethod()); + XmlRpc::XmlRpcValue rootParameter; + XmlRpc::XmlRpcValue parameters; + XmlRpc::XmlRpcValue result; + + // set up a structure for the parameters + parameters["playlistId"] = 9999; + rootParameter[0] = parameters; + + method->execute(rootParameter, result); + CPPUNIT_ASSERT(((bool)result) == false); +} + diff --git a/livesupport/products/scheduler/src/DeletePlaylistMethodTest.h b/livesupport/products/scheduler/src/DeletePlaylistMethodTest.h new file mode 100644 index 000000000..afa9dd737 --- /dev/null +++ b/livesupport/products/scheduler/src/DeletePlaylistMethodTest.h @@ -0,0 +1,145 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: fgerlits $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/Attic/DeletePlaylistMethodTest.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef DeletePlaylistMethodTest_h +#define DeletePlaylistMethodTest_h + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include + + +namespace LiveSupport { +namespace Scheduler { + +using namespace LiveSupport; +using namespace LiveSupport::Core; + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/** + * Unit test for the DeletePlaylistMethod class. + * + * @author $Author: maroy, fgerlits + $ + * @version $Revision: 1.1 $ + * @see DeletePlaylistMethod + */ +class DeletePlaylistMethodTest : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(DeletePlaylistMethodTest); + CPPUNIT_TEST(firstTest); + CPPUNIT_TEST(negativeTest); + CPPUNIT_TEST_SUITE_END(); + + /** + * The name of the configuration file for the storage client factory. + */ + static const std::string storageClientConfig; + + /** + * The name of the configuration file for the connection manager + * factory. + */ + static const std::string connectionManagerConfig; + + /** + * Configure a configurable with an XML file. + * + * @param configurable configure this + * @param fileName the name of the XML file to configure with. + * @exception std::invalid_argument on configuration errors. + * @exception xmlpp::exception on XML parsing errors. + */ + void + configure(Ptr::Ref configurable, + std::string fileName) + throw (std::invalid_argument, + xmlpp::exception); + + + protected: + + /** + * A simple test. + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + firstTest(void) throw (CPPUNIT_NS::Exception); + + /** + * A simple negative test. + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + negativeTest(void) throw (CPPUNIT_NS::Exception); + + public: + + /** + * Set up the environment for the test case. + */ + void + setUp(void) throw (); + + /** + * Clean up the environment after the test case. + */ + void + tearDown(void) throw (); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace Scheduler +} // namespace LiveSupport + +#endif // DeletePlaylistMethodTest_h +