1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2009 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
24 #include "filter_virt_db.hpp"
25 #include "filter_backend_test.hpp"
26 #include "filter_log.hpp"
28 #include "router_chain.hpp"
29 #include "session.hpp"
30 #include "package.hpp"
32 #define BOOST_AUTO_TEST_MAIN
33 #define BOOST_TEST_DYN_LINK
34 #include <boost/test/auto_unit_test.hpp>
37 #include <yaz/pquery.h>
38 #include <yaz/otherinfo.h>
39 using namespace boost::unit_test;
41 namespace mp = metaproxy_1;
43 BOOST_AUTO_TEST_CASE( test_filter_virt_db_1 )
47 mp::filter::VirtualDB vdb;
54 BOOST_AUTO_TEST_CASE( test_filter_virt_db_2 )
58 mp::RouterChain router;
60 mp::filter::VirtualDB vdb;
64 // Create package with Z39.50 init request in it
65 // Since there is not vhost given, the virt will make its
66 // own init response (regardless of backend)
70 Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
74 pack.request() = apdu;
77 pack.router(router).move();
79 // Inspect that we got Z39.50 init Response OK.
80 yazpp_1::GDU *gdu = &pack.response();
82 BOOST_CHECK(!pack.session().is_closed());
84 Z_GDU *z_gdu = gdu->get();
87 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
88 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
97 static void init(mp::Package &pack, mp::Router &router)
99 // Create package with Z39.50 init request in it
101 Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
107 pack.request() = apdu;
110 pack.router(router).move();
112 // Inspect that we got Z39.50 init response
113 yazpp_1::GDU *gdu = &pack.response();
115 BOOST_CHECK(!pack.session().is_closed());
117 Z_GDU *z_gdu = gdu->get();
121 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
122 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
125 static void search(mp::Package &pack, mp::Router &router,
126 const std::string &query, const char *db,
129 // Create package with Z39.50 search request in it
132 Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
134 mp::util::pqf(odr, apdu, query);
136 apdu->u.searchRequest->resultSetName = odr_strdup(odr, setname);
138 apdu->u.searchRequest->num_databaseNames = 1;
139 apdu->u.searchRequest->databaseNames = (char**)
140 odr_malloc(odr, sizeof(char *));
141 apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, db);
147 pack.request() = apdu;
149 Z_GDU *gdu_test = pack.request().get();
150 BOOST_CHECK(gdu_test);
153 pack.router(router).move();
155 // Inspect that we got Z39.50 search response
156 yazpp_1::GDU *gdu = &pack.response();
158 BOOST_CHECK(!pack.session().is_closed());
160 Z_GDU *z_gdu = gdu->get();
164 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
165 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_searchResponse);
168 static void present(mp::Package &pack, mp::Router &router,
169 int start, int number,
172 // Create package with Z39.50 present request in it
175 Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
177 apdu->u.presentRequest->resultSetId = odr_strdup(odr, setname);
178 *apdu->u.presentRequest->resultSetStartPoint = start;
179 *apdu->u.presentRequest->numberOfRecordsRequested = number;
185 pack.request() = apdu;
187 Z_GDU *gdu_test = pack.request().get();
188 BOOST_CHECK(gdu_test);
191 pack.router(router).move();
193 // Inspect that we got Z39.50 present response
194 yazpp_1::GDU *gdu = &pack.response();
196 BOOST_CHECK(!pack.session().is_closed());
198 Z_GDU *z_gdu = gdu->get();
202 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
203 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_presentResponse);
206 BOOST_AUTO_TEST_CASE( test_filter_virt_db_3 )
210 mp::RouterChain router;
212 mp::filter::Log filter_log1("FRONT");
214 router.append(filter_log1);
217 mp::filter::VirtualDB vdb;
219 vdb.add_map_db2target("Default", "localhost:210", "");
220 mp::filter::Log filter_log2("BACK");
222 router.append(filter_log2);
224 mp::filter::BackendTest btest;
225 router.append(btest);
227 mp::Session session1;
231 mp::Package pack(session1, origin1);
235 // search for database for which there is no map
236 mp::Package pack(session1, origin1);
237 search(pack, router, "computer", "bad_database", "default");
240 // search for database for which there a map
241 mp::Package pack(session1, origin1);
242 search(pack, router, "other", "Default", "default");
245 // present from last search
246 mp::Package pack(session1, origin1);
247 present(pack, router, 1, 2, "default");
259 * c-file-style: "Stroustrup"
260 * indent-tabs-mode: nil
262 * vim: shiftwidth=4 tabstop=8 expandtab