1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2012 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "filter_query_rewrite.hpp"
24 #include <metaproxy/util.hpp>
25 #include "router_chain.hpp"
26 #include <metaproxy/package.hpp>
28 #define BOOST_AUTO_TEST_MAIN
29 #define BOOST_TEST_DYN_LINK
30 #include <boost/test/auto_unit_test.hpp>
32 using namespace boost::unit_test;
34 namespace mp = metaproxy_1;
35 using namespace mp::util;
37 class FilterBounceZ3950: public mp::filter::Base {
39 void process(mp::Package & package) const {
41 if (package.session().is_closed())
43 std::cout << "Got Close.\n";
47 Z_GDU *gdu = package.request().get();
49 if (gdu && gdu->which == Z_GDU_Z3950
50 && gdu->u.z3950->which == Z_APDU_initRequest)
52 std::cout << "Got Z3950 Init PDU\n";
53 //Z_InitRequest *req = gdu->u.z3950->u.initRequest;
54 //package.request() = gdu;
57 else if (gdu && gdu->which == Z_GDU_Z3950
58 && gdu->u.z3950->which == Z_APDU_searchRequest)
60 std::cout << "Got Z3950 Search PDU\n";
61 //Z_SearchRequest *req = gdu->u.z3950->u.searchRequest;
62 //package.request() = gdu;
65 else if (gdu && gdu->which == Z_GDU_Z3950
66 && gdu->u.z3950->which == Z_APDU_scanRequest)
68 std::cout << "Got Z3950 Scan PDU\n";
69 //Z_ScanRequest *req = gdu->u.z3950->u.scanRequest;
70 //package.request() = gdu;
78 void check_query_rewrite_init(mp::RouterChain &router)
80 //std::cout << "QUERY REWRITE INIT\n";
82 // Create package with Z39.50 init request in it
86 Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
88 pack.request() = apdu;
89 // Done creating query.
92 pack.router(router).move();
94 // Inspect bounced back request
95 //yazpp_1::GDU *gdu = &pack.response();
96 yazpp_1::GDU *gdu = &pack.request();
98 Z_GDU *z_gdu = gdu->get();
100 //std::cout << "Z_GDU " << z_gdu << "\n";
103 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
104 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initRequest);
108 void check_query_rewrite_search(mp::RouterChain &router,
109 std::string query_in,
110 std::string query_expect)
112 //std::cout << "QUERY REWRITE SEARCH "
113 // << query_in << " " << query_expect << "\n";
115 // Create package with Z39.50 search request in it
119 Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
121 // create package PQF query here
122 mp::util::pqf(odr, apdu, query_in);
124 // create package PDF database info (needed!)
125 apdu->u.searchRequest->num_databaseNames = 1;
126 apdu->u.searchRequest->databaseNames
127 = (char**)odr_malloc(odr, sizeof(char *));
128 apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, "Default");
130 // Done creating request package
131 pack.request() = apdu;
134 pack.router(router).move();
136 // Inspect bounced back request
137 //yazpp_1::GDU *gdu = &pack.response();
138 yazpp_1::GDU *gdu = &pack.request();
140 Z_GDU *z_gdu = gdu->get();
141 //std::cout << "Z_GDU " << z_gdu << "\n";
145 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
146 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_searchRequest);
148 // take query out of package again and check rewrite
149 std::string query_changed
150 = zQueryToString(z_gdu->u.z3950->u.searchRequest->query);
151 BOOST_CHECK_EQUAL(query_expect, query_changed);
156 BOOST_AUTO_TEST_CASE( test_filter_query_rewrite_1 )
160 mp::filter::QueryRewrite f_query_rewrite;
167 BOOST_AUTO_TEST_CASE( test_filter_query_rewrite2 )
171 mp::RouterChain router;
173 mp::filter::QueryRewrite f_query_rewrite;
174 //FilterBounceZ3950 f_bounce_z3950;
176 router.append(f_query_rewrite);
177 //router.append(f_bounce_z3950);
179 check_query_rewrite_init(router);
180 check_query_rewrite_search(router,
181 "@attrset Bib-1 @attr 1=4 the",
182 "@attrset Bib-1 @attr 1=4 the");
191 BOOST_AUTO_TEST_CASE( test_filter_query_rewrite3 )
197 mp::RouterChain router;
200 std::string xmlconf =
201 "<?xml version='1.0'?>\n"
202 "<filter xmlns='http://indexdata.com/metaproxy'\n"
203 " id='qrw1' type='query_rewrite'>\n"
207 //std::cout << xmlconf << std::endl;
209 // reading and parsing XML conf
210 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
212 xmlNode *root_element = xmlDocGetRootElement(doc);
214 // creating and configuring filter
215 mp::filter::QueryRewrite f_query_rewrite;
216 f_query_rewrite.configure(root_element, true, 0);
218 // remeber to free XML DOM
221 // add only filter to router
222 router.append(f_query_rewrite);
225 check_query_rewrite_init(router);
226 check_query_rewrite_search(router,
227 "@attrset Bib-1 @attr 1=4 the",
228 "@attrset Bib-1 @attr 1=4 the");
232 catch (std::exception &e) {
233 std::cout << e.what() << "\n";
245 * c-file-style: "Stroustrup"
246 * indent-tabs-mode: nil
248 * vim: shiftwidth=4 tabstop=8 expandtab