GPL v2.
[metaproxy-moved-to-github.git] / src / test_filter_virt_db.cpp
1 /* $Id: test_filter_virt_db.cpp,v 1.17 2007-05-09 21:23:09 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23 #include <iostream>
24 #include <stdexcept>
25
26 #include "util.hpp"
27 #include "filter_virt_db.hpp"
28 #include "filter_backend_test.hpp"
29 #include "filter_log.hpp"
30
31 #include "router_chain.hpp"
32 #include "session.hpp"
33 #include "package.hpp"
34
35 #define BOOST_AUTO_TEST_MAIN
36 #include <boost/test/auto_unit_test.hpp>
37
38 #include <yaz/zgdu.h>
39 #include <yaz/pquery.h>
40 #include <yaz/otherinfo.h>
41 using namespace boost::unit_test;
42
43 namespace mp = metaproxy_1;
44
45 BOOST_AUTO_UNIT_TEST( test_filter_virt_db_1 )
46 {
47     try 
48     {
49         mp::filter::VirtualDB vdb;
50     }
51     catch ( ... ) {
52         BOOST_CHECK (false);
53     }
54 }
55
56 BOOST_AUTO_UNIT_TEST( test_filter_virt_db_2 )
57 {
58     try 
59     {
60         mp::RouterChain router;
61         
62         mp::filter::VirtualDB vdb;
63         
64         router.append(vdb);
65         
66         // Create package with Z39.50 init request in it
67         // Since there is not vhost given, the virt will make its
68         // own init response (regardless of backend)
69         mp::Package pack;
70         
71         mp::odr odr;
72         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
73         
74         BOOST_CHECK(apdu);
75         
76         pack.request() = apdu;
77         
78         // Put it in router
79         pack.router(router).move(); 
80         
81         // Inspect that we got Z39.50 init Response OK.
82         yazpp_1::GDU *gdu = &pack.response();
83         
84         BOOST_CHECK(!pack.session().is_closed()); 
85         
86         Z_GDU *z_gdu = gdu->get();
87         BOOST_CHECK(z_gdu);
88         if (z_gdu) {
89             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
90             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
91         }
92     }
93     catch ( ... ) {
94         BOOST_CHECK (false);
95     }
96 }
97
98
99 static void init(mp::Package &pack, mp::Router &router)
100 {
101     // Create package with Z39.50 init request in it
102     mp::odr odr;
103     Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
104     
105     BOOST_CHECK(apdu);
106     if (!apdu)
107         return;
108     
109     pack.request() = apdu;
110     
111     // Put it in router
112     pack.router(router).move(); 
113     
114     // Inspect that we got Z39.50 init response
115     yazpp_1::GDU *gdu = &pack.response();
116     
117     BOOST_CHECK(!pack.session().is_closed()); 
118     
119     Z_GDU *z_gdu = gdu->get();
120     BOOST_CHECK(z_gdu);
121     if (!z_gdu)
122         return;
123     BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
124     BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
125 }
126                  
127 static void search(mp::Package &pack, mp::Router &router,
128                    const std::string &query, const char *db,
129                    const char *setname)
130 {
131     // Create package with Z39.50 search request in it
132             
133     mp::odr odr;
134     Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
135
136     mp::util::pqf(odr, apdu, query);
137
138     apdu->u.searchRequest->resultSetName = odr_strdup(odr, setname);
139     
140     apdu->u.searchRequest->num_databaseNames = 1;
141     apdu->u.searchRequest->databaseNames = (char**)
142         odr_malloc(odr, sizeof(char *));
143     apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, db);
144     
145     BOOST_CHECK(apdu);
146     if (!apdu)
147         return;
148     
149     pack.request() = apdu;
150     
151     Z_GDU *gdu_test = pack.request().get();
152     BOOST_CHECK(gdu_test);
153     
154     // Put it in router
155     pack.router(router).move(); 
156     
157     // Inspect that we got Z39.50 search response
158     yazpp_1::GDU *gdu = &pack.response();
159     
160     BOOST_CHECK(!pack.session().is_closed()); 
161     
162     Z_GDU *z_gdu = gdu->get();
163     BOOST_CHECK(z_gdu);
164     if (!z_gdu)
165         return;
166     BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
167     BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_searchResponse);
168 }
169
170 static void present(mp::Package &pack, mp::Router &router,
171                     int start, int number,
172                     const char *setname)
173 {
174     // Create package with Z39.50 present request in it
175             
176     mp::odr odr;
177     Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
178     
179     apdu->u.presentRequest->resultSetId  = odr_strdup(odr, setname);
180     *apdu->u.presentRequest->resultSetStartPoint = start;
181     *apdu->u.presentRequest->numberOfRecordsRequested = number;
182
183     BOOST_CHECK(apdu);
184     if (!apdu)
185         return;
186     
187     pack.request() = apdu;
188     
189     Z_GDU *gdu_test = pack.request().get();
190     BOOST_CHECK(gdu_test);
191     
192     // Put it in router
193     pack.router(router).move(); 
194     
195     // Inspect that we got Z39.50 present response
196     yazpp_1::GDU *gdu = &pack.response();
197     
198     BOOST_CHECK(!pack.session().is_closed()); 
199     
200     Z_GDU *z_gdu = gdu->get();
201     BOOST_CHECK(z_gdu);
202     if (!z_gdu)
203         return;
204     BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
205     BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_presentResponse);
206 }
207
208 BOOST_AUTO_UNIT_TEST( test_filter_virt_db_3 )
209 {
210     try 
211     {
212         mp::RouterChain router;
213
214         mp::filter::Log filter_log1("FRONT");
215 #if 0
216         router.append(filter_log1);
217 #endif
218    
219         mp::filter::VirtualDB vdb;        
220         router.append(vdb);
221         vdb.add_map_db2target("Default", "localhost:210", "");
222         mp::filter::Log filter_log2("BACK");
223 #if 0
224         router.append(filter_log2);
225 #endif
226         mp::filter::BackendTest btest;
227         router.append(btest);
228
229         mp::Session session1;
230         mp::Origin origin1;
231         
232         {
233             mp::Package pack(session1, origin1);
234             init(pack, router);
235         }
236         {
237             // search for database for which there is no map
238             mp::Package pack(session1, origin1);
239             search(pack, router, "computer", "bad_database", "default");
240         }
241         {
242             // search for database for which there a map
243             mp::Package pack(session1, origin1);
244             search(pack, router, "other", "Default", "default");
245         }
246         {
247             // present from last search
248             mp::Package pack(session1, origin1);
249             present(pack, router, 1, 2, "default");
250         }
251     }
252     catch ( ... ) {
253         BOOST_CHECK (false);
254     }
255 }
256
257
258 /*
259  * Local variables:
260  * c-basic-offset: 4
261  * indent-tabs-mode: nil
262  * c-file-style: "stroustrup"
263  * End:
264  * vim: shiftwidth=4 tabstop=8 expandtab
265  */