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