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