Fixed bug #1589: tests does not compile for libboost 1.34.1.
[metaproxy-moved-to-github.git] / src / test_filter_virt_db.cpp
1 /* $Id: test_filter_virt_db.cpp,v 1.18 2007-11-02 17:47:41 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 #define BOOST_TEST_DYN_LINK
37 #include <boost/test/auto_unit_test.hpp>
38
39 #include <yaz/zgdu.h>
40 #include <yaz/pquery.h>
41 #include <yaz/otherinfo.h>
42 using namespace boost::unit_test;
43
44 namespace mp = metaproxy_1;
45
46 BOOST_AUTO_TEST_CASE( test_filter_virt_db_1 )
47 {
48     try 
49     {
50         mp::filter::VirtualDB vdb;
51     }
52     catch ( ... ) {
53         BOOST_CHECK (false);
54     }
55 }
56
57 BOOST_AUTO_TEST_CASE( test_filter_virt_db_2 )
58 {
59     try 
60     {
61         mp::RouterChain router;
62         
63         mp::filter::VirtualDB vdb;
64         
65         router.append(vdb);
66         
67         // Create package with Z39.50 init request in it
68         // Since there is not vhost given, the virt will make its
69         // own init response (regardless of backend)
70         mp::Package pack;
71         
72         mp::odr odr;
73         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
74         
75         BOOST_CHECK(apdu);
76         
77         pack.request() = apdu;
78         
79         // Put it in router
80         pack.router(router).move(); 
81         
82         // Inspect that we got Z39.50 init Response OK.
83         yazpp_1::GDU *gdu = &pack.response();
84         
85         BOOST_CHECK(!pack.session().is_closed()); 
86         
87         Z_GDU *z_gdu = gdu->get();
88         BOOST_CHECK(z_gdu);
89         if (z_gdu) {
90             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
91             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
92         }
93     }
94     catch ( ... ) {
95         BOOST_CHECK (false);
96     }
97 }
98
99
100 static void init(mp::Package &pack, mp::Router &router)
101 {
102     // Create package with Z39.50 init request in it
103     mp::odr odr;
104     Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
105     
106     BOOST_CHECK(apdu);
107     if (!apdu)
108         return;
109     
110     pack.request() = apdu;
111     
112     // Put it in router
113     pack.router(router).move(); 
114     
115     // Inspect that we got Z39.50 init response
116     yazpp_1::GDU *gdu = &pack.response();
117     
118     BOOST_CHECK(!pack.session().is_closed()); 
119     
120     Z_GDU *z_gdu = gdu->get();
121     BOOST_CHECK(z_gdu);
122     if (!z_gdu)
123         return;
124     BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
125     BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
126 }
127                  
128 static void search(mp::Package &pack, mp::Router &router,
129                    const std::string &query, const char *db,
130                    const char *setname)
131 {
132     // Create package with Z39.50 search request in it
133             
134     mp::odr odr;
135     Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
136
137     mp::util::pqf(odr, apdu, query);
138
139     apdu->u.searchRequest->resultSetName = odr_strdup(odr, setname);
140     
141     apdu->u.searchRequest->num_databaseNames = 1;
142     apdu->u.searchRequest->databaseNames = (char**)
143         odr_malloc(odr, sizeof(char *));
144     apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, db);
145     
146     BOOST_CHECK(apdu);
147     if (!apdu)
148         return;
149     
150     pack.request() = apdu;
151     
152     Z_GDU *gdu_test = pack.request().get();
153     BOOST_CHECK(gdu_test);
154     
155     // Put it in router
156     pack.router(router).move(); 
157     
158     // Inspect that we got Z39.50 search response
159     yazpp_1::GDU *gdu = &pack.response();
160     
161     BOOST_CHECK(!pack.session().is_closed()); 
162     
163     Z_GDU *z_gdu = gdu->get();
164     BOOST_CHECK(z_gdu);
165     if (!z_gdu)
166         return;
167     BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
168     BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_searchResponse);
169 }
170
171 static void present(mp::Package &pack, mp::Router &router,
172                     int start, int number,
173                     const char *setname)
174 {
175     // Create package with Z39.50 present request in it
176             
177     mp::odr odr;
178     Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
179     
180     apdu->u.presentRequest->resultSetId  = odr_strdup(odr, setname);
181     *apdu->u.presentRequest->resultSetStartPoint = start;
182     *apdu->u.presentRequest->numberOfRecordsRequested = number;
183
184     BOOST_CHECK(apdu);
185     if (!apdu)
186         return;
187     
188     pack.request() = apdu;
189     
190     Z_GDU *gdu_test = pack.request().get();
191     BOOST_CHECK(gdu_test);
192     
193     // Put it in router
194     pack.router(router).move(); 
195     
196     // Inspect that we got Z39.50 present response
197     yazpp_1::GDU *gdu = &pack.response();
198     
199     BOOST_CHECK(!pack.session().is_closed()); 
200     
201     Z_GDU *z_gdu = gdu->get();
202     BOOST_CHECK(z_gdu);
203     if (!z_gdu)
204         return;
205     BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
206     BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_presentResponse);
207 }
208
209 BOOST_AUTO_TEST_CASE( test_filter_virt_db_3 )
210 {
211     try 
212     {
213         mp::RouterChain router;
214
215         mp::filter::Log filter_log1("FRONT");
216 #if 0
217         router.append(filter_log1);
218 #endif
219    
220         mp::filter::VirtualDB vdb;        
221         router.append(vdb);
222         vdb.add_map_db2target("Default", "localhost:210", "");
223         mp::filter::Log filter_log2("BACK");
224 #if 0
225         router.append(filter_log2);
226 #endif
227         mp::filter::BackendTest btest;
228         router.append(btest);
229
230         mp::Session session1;
231         mp::Origin origin1;
232         
233         {
234             mp::Package pack(session1, origin1);
235             init(pack, router);
236         }
237         {
238             // search for database for which there is no map
239             mp::Package pack(session1, origin1);
240             search(pack, router, "computer", "bad_database", "default");
241         }
242         {
243             // search for database for which there a map
244             mp::Package pack(session1, origin1);
245             search(pack, router, "other", "Default", "default");
246         }
247         {
248             // present from last search
249             mp::Package pack(session1, origin1);
250             present(pack, router, 1, 2, "default");
251         }
252     }
253     catch ( ... ) {
254         BOOST_CHECK (false);
255     }
256 }
257
258
259 /*
260  * Local variables:
261  * c-basic-offset: 4
262  * indent-tabs-mode: nil
263  * c-file-style: "stroustrup"
264  * End:
265  * vim: shiftwidth=4 tabstop=8 expandtab
266  */