The configure method takes test_only flag so we can avoid
[metaproxy-moved-to-github.git] / src / test_filter_query_rewrite.cpp
index 0fe19a2..06f8c0e 100644 (file)
@@ -1,7 +1,22 @@
-/* $Id: test_filter_query_rewrite.cpp,v 1.2 2006-01-20 22:38:12 marc Exp $
-   Copyright (c) 2005, Index Data.
+/* $Id: test_filter_query_rewrite.cpp,v 1.15 2008-02-20 15:07:53 adam Exp $
+   Copyright (c) 2005-2007, Index Data.
 
-%LICENSE%
+This file is part of Metaproxy.
+
+Metaproxy 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, or (at your option) any later
+version.
+
+Metaproxy 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 Metaproxy; see the file LICENSE.  If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
  */
 
 #include "config.hpp"
 #include "package.hpp"
 
 #define BOOST_AUTO_TEST_MAIN
+#define BOOST_TEST_DYN_LINK
 #include <boost/test/auto_unit_test.hpp>
 
 using namespace boost::unit_test;
 
-class FilterBounceZ3950: public yp2::filter::Base {
+namespace mp = metaproxy_1;
+using namespace mp::util;
+
+class FilterBounceZ3950: public mp::filter::Base {
 public:
-    void process(yp2::Package & package) const {
+    void process(mp::Package & package) const {
         
         if (package.session().is_closed())
         {
             std::cout << "Got Close.\n";
+            return;
         }
        
         Z_GDU *gdu = package.request().get();
@@ -36,34 +56,37 @@ public:
             std::cout << "Got Z3950 Init PDU\n";         
             //Z_InitRequest *req = gdu->u.z3950->u.initRequest;
             //package.request() = gdu;
+            return;
         } 
-        else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
-            Z_APDU_searchRequest)
+        else if (gdu && gdu->which == Z_GDU_Z3950 
+                 && gdu->u.z3950->which == Z_APDU_searchRequest)
         {
             std::cout << "Got Z3950 Search PDU\n";   
             //Z_SearchRequest *req = gdu->u.z3950->u.searchRequest;
             //package.request() = gdu;
+            return;
         } 
-        else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
-            Z_APDU_scanRequest)
+        else if (gdu && gdu->which == Z_GDU_Z3950 
+                 && gdu->u.z3950->which == Z_APDU_scanRequest)
         {
             std::cout << "Got Z3950 Scan PDU\n";   
             //Z_ScanRequest *req = gdu->u.z3950->u.scanRequest;
             //package.request() = gdu;
+            return;
         } 
         
         package.move();
     };
 };
 
-void check_query_rewrite_init(yp2::RouterChain &router)
+void check_query_rewrite_init(mp::RouterChain &router)
 {
     //std::cout << "QUERY REWRITE INIT\n";
 
     // Create package with Z39.50 init request in it
-    yp2::Package pack;
+    mp::Package pack;
         
-    yp2::odr odr;
+    mp::odr odr;
     Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
     
     pack.request() = apdu;
@@ -86,7 +109,7 @@ void check_query_rewrite_init(yp2::RouterChain &router)
     }
 }
 
-void check_query_rewrite_search(yp2::RouterChain &router, 
+void check_query_rewrite_search(mp::RouterChain &router, 
                                 std::string query_in,
                                 std::string query_expect)
 {
@@ -94,13 +117,13 @@ void check_query_rewrite_search(yp2::RouterChain &router,
     //          << query_in << " " << query_expect << "\n";
     
     // Create package with Z39.50 search request in it
-    yp2::Package pack;
+    mp::Package pack;
         
-    yp2::odr odr;
+    mp::odr odr;
     Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
 
     // create package PQF query here    
-    yp2::util::pqf(odr, apdu, query_in);
+    mp::util::pqf(odr, apdu, query_in);
 
     // create package PDF database info (needed!)
     apdu->u.searchRequest->num_databaseNames = 1;
@@ -127,40 +150,31 @@ void check_query_rewrite_search(yp2::RouterChain &router,
         BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_searchRequest);
 
         // take query out of package again and check rewrite
-        //std::string query_changed = yp2::util::apduToPqf(apdu);
         std::string query_changed 
-            = yp2::util::zQueryToString(z_gdu->u.z3950->u.searchRequest->query);
-
-        //BOOST_CHECK_EQUAL(query_in, query_expect);
-
-        //std::cout << "'" << query_expect << "'\n";
-        //std::cout << "'" << query_changed << "'\n";
-        
-        // need ugly whitespace here ..
-        //BOOST_CHECK_EQUAL(query_expect, std::string(" ") + query_changed);
+            = zQueryToString(z_gdu->u.z3950->u.searchRequest->query);
         BOOST_CHECK_EQUAL(query_expect, query_changed);
     }
 }
 
 
-BOOST_AUTO_UNIT_TEST( test_filter_query_rewrite_1 )
+BOOST_AUTO_TEST_CASE( test_filter_query_rewrite_1 )
 {
     try 
     {
-        yp2::filter::QueryRewrite f_query_rewrite;
+        mp::filter::QueryRewrite f_query_rewrite;
     }
     catch ( ... ) {
         BOOST_CHECK (false);
     }
 }
 
-BOOST_AUTO_UNIT_TEST( test_filter_query_rewrite2 )
+BOOST_AUTO_TEST_CASE( test_filter_query_rewrite2 )
 {
     try 
     {
-        yp2::RouterChain router;
+        mp::RouterChain router;
         
-        yp2::filter::QueryRewrite f_query_rewrite;
+        mp::filter::QueryRewrite f_query_rewrite;
         //FilterBounceZ3950 f_bounce_z3950;
         
         router.append(f_query_rewrite);
@@ -177,6 +191,58 @@ BOOST_AUTO_UNIT_TEST( test_filter_query_rewrite2 )
     }
 }
 
+
+BOOST_AUTO_TEST_CASE( test_filter_query_rewrite3 )
+{
+    
+
+    try 
+    {
+        mp::RouterChain router;
+        
+
+        std::string xmlconf = 
+            "<?xml version='1.0'?>\n"
+            "<filter xmlns='http://indexdata.com/metaproxy'\n"
+            "        id='qrw1' type='query_rewrite'>\n"
+            "</filter>\n"
+            ;
+         
+        //std::cout << xmlconf  << std::endl;
+
+        // reading and parsing XML conf
+        xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
+        BOOST_CHECK(doc);
+        xmlNode *root_element = xmlDocGetRootElement(doc);
+
+        // creating and configuring filter
+        mp::filter::QueryRewrite f_query_rewrite;
+        f_query_rewrite.configure(root_element, true);
+        
+        // remeber to free XML DOM
+        xmlFreeDoc(doc);
+        
+        // add only filter to router
+        router.append(f_query_rewrite);
+
+        // start testing
+        check_query_rewrite_init(router);
+        check_query_rewrite_search(router, 
+                                   "@attrset Bib-1 @attr 1=4 the", 
+                                   "@attrset Bib-1 @attr 1=4 the");
+
+    }
+
+    catch (std::exception &e) {
+        std::cout << e.what() << "\n";
+        BOOST_CHECK (false);
+    }
+
+    catch ( ... ) {
+        BOOST_CHECK (false);
+    }
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4