1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 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 <metaproxy/util.hpp>
24 #include "filter_virt_db.hpp"
25 #include "filter_backend_test.hpp"
26 #include "filter_log.hpp"
28 #include <metaproxy/router_chain.hpp>
29 #include <metaproxy/package.hpp>
31 #define BOOST_AUTO_TEST_MAIN
32 #define BOOST_TEST_DYN_LINK
33 #include <boost/test/auto_unit_test.hpp>
36 #include <yaz/pquery.h>
37 #include <yaz/otherinfo.h>
38 using namespace boost::unit_test;
40 namespace mp = metaproxy_1;
42 BOOST_AUTO_TEST_CASE( test_filter_virt_db_1 )
46 mp::filter::VirtualDB vdb;
53 BOOST_AUTO_TEST_CASE( test_filter_virt_db_2 )
57 mp::RouterChain router;
59 mp::filter::VirtualDB vdb;
63 // Create package with Z39.50 init request in it
64 // Since there is not vhost given, the virt will make its
65 // own init response (regardless of backend)
69 Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
73 pack.request() = apdu;
76 pack.router(router).move();
78 // Inspect that we got Z39.50 init Response OK.
79 yazpp_1::GDU *gdu = &pack.response();
81 BOOST_CHECK(!pack.session().is_closed());
83 Z_GDU *z_gdu = gdu->get();
86 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
87 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
96 static void init(mp::Package &pack, mp::Router &router)
98 // Create package with Z39.50 init request in it
100 Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
106 pack.request() = apdu;
109 pack.router(router).move();
111 // Inspect that we got Z39.50 init response
112 yazpp_1::GDU *gdu = &pack.response();
114 BOOST_CHECK(!pack.session().is_closed());
116 Z_GDU *z_gdu = gdu->get();
120 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
121 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
124 static void search(mp::Package &pack, mp::Router &router,
125 const std::string &query, const char *db,
128 // Create package with Z39.50 search request in it
131 Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
133 mp::util::pqf(odr, apdu, query);
135 apdu->u.searchRequest->resultSetName = odr_strdup(odr, setname);
137 apdu->u.searchRequest->num_databaseNames = 1;
138 apdu->u.searchRequest->databaseNames = (char**)
139 odr_malloc(odr, sizeof(char *));
140 apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, db);
146 pack.request() = apdu;
148 Z_GDU *gdu_test = pack.request().get();
149 BOOST_CHECK(gdu_test);
152 pack.router(router).move();
154 // Inspect that we got Z39.50 search response
155 yazpp_1::GDU *gdu = &pack.response();
157 BOOST_CHECK(!pack.session().is_closed());
159 Z_GDU *z_gdu = gdu->get();
163 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
164 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_searchResponse);
167 static void present(mp::Package &pack, mp::Router &router,
168 int start, int number,
171 // Create package with Z39.50 present request in it
174 Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
176 apdu->u.presentRequest->resultSetId = odr_strdup(odr, setname);
177 *apdu->u.presentRequest->resultSetStartPoint = start;
178 *apdu->u.presentRequest->numberOfRecordsRequested = number;
184 pack.request() = apdu;
186 Z_GDU *gdu_test = pack.request().get();
187 BOOST_CHECK(gdu_test);
190 pack.router(router).move();
192 // Inspect that we got Z39.50 present response
193 yazpp_1::GDU *gdu = &pack.response();
195 BOOST_CHECK(!pack.session().is_closed());
197 Z_GDU *z_gdu = gdu->get();
201 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
202 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_presentResponse);
205 BOOST_AUTO_TEST_CASE( test_filter_virt_db_3 )
209 mp::RouterChain router;
211 mp::filter::Log filter_log1("FRONT");
213 router.append(filter_log1);
216 mp::filter::VirtualDB vdb;
218 vdb.add_map_db2target("Default", "localhost:210", "");
219 mp::filter::Log filter_log2("BACK");
221 router.append(filter_log2);
223 mp::filter::BackendTest btest;
224 router.append(btest);
226 mp::Session session1;
230 mp::Package pack(session1, origin1);
234 // search for database for which there is no map
235 mp::Package pack(session1, origin1);
236 search(pack, router, "computer", "bad_database", "default");
239 // search for database for which there a map
240 mp::Package pack(session1, origin1);
241 search(pack, router, "other", "Default", "default");
244 // present from last search
245 mp::Package pack(session1, origin1);
246 present(pack, router, 1, 2, "default");
258 * c-file-style: "Stroustrup"
259 * indent-tabs-mode: nil
261 * vim: shiftwidth=4 tabstop=8 expandtab