moved RouterChain class out of router.hpp file into own router_chain.hpp class
[metaproxy-moved-to-github.git] / src / test_filter_virt_db.cpp
1 /* $Id: test_filter_virt_db.cpp,v 1.5 2005-10-26 10:21:03 marc Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8 #include <iostream>
9 #include <stdexcept>
10
11 #include "test_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
29 BOOST_AUTO_TEST_CASE( test_filter_virt_db_1 )
30 {
31     try 
32     {
33         yp2::filter::Virt_db vdb;
34     }
35     catch ( ... ) {
36         BOOST_CHECK (false);
37     }
38 }
39
40 BOOST_AUTO_TEST_CASE( test_filter_virt_db_2 )
41 {
42     try 
43     {
44         yp2::RouterChain router;
45         
46         yp2::filter::Virt_db vdb;
47         
48         router.rule(vdb);
49         
50         // Create package with Z39.50 init request in it
51         // Since there is not vhost given, the virt will make its
52         // own init response (regardless of backend)
53         yp2::Package pack;
54         
55         ODR odr = odr_createmem(ODR_ENCODE);
56         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
57         
58         BOOST_CHECK(apdu);
59         
60         pack.request() = apdu;
61         odr_destroy(odr);
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(yp2::Package &pack, yp2::Router &router)
85 {
86     // Create package with Z39.50 init request in it
87     ODR odr = odr_createmem(ODR_ENCODE);
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     odr_destroy(odr);
96     
97     // Put it in router
98     pack.router(router).move(); 
99     
100     // Inspect that we got Z39.50 init response
101     yazpp_1::GDU *gdu = &pack.response();
102     
103     BOOST_CHECK(!pack.session().is_closed()); 
104     
105     Z_GDU *z_gdu = gdu->get();
106     BOOST_CHECK(z_gdu);
107     if (!z_gdu)
108         return;
109     BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
110     BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
111 }
112                  
113 static void search(yp2::Package &pack, yp2::Router &router,
114                    const std::string &query, const char *db,
115                    const char *setname)
116 {
117     // Create package with Z39.50 search request in it
118             
119     ODR odr = odr_createmem(ODR_ENCODE);
120     Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
121
122     yp2::util::pqf(odr, apdu, query);
123
124     apdu->u.searchRequest->resultSetName = odr_strdup(odr, setname);
125     
126     apdu->u.searchRequest->num_databaseNames = 1;
127     apdu->u.searchRequest->databaseNames = (char**)
128         odr_malloc(odr, sizeof(char *));
129     apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, db);
130     
131     BOOST_CHECK(apdu);
132     if (!apdu)
133         return;
134     
135     pack.request() = apdu;
136     
137     odr_destroy(odr);
138     
139     Z_GDU *gdu_test = pack.request().get();
140     BOOST_CHECK(gdu_test);
141     
142     // Put it in router
143     pack.router(router).move(); 
144     
145     // Inspect that we got Z39.50 search response
146     yazpp_1::GDU *gdu = &pack.response();
147     
148     BOOST_CHECK(!pack.session().is_closed()); 
149     
150     Z_GDU *z_gdu = gdu->get();
151     BOOST_CHECK(z_gdu);
152     if (!z_gdu)
153         return;
154     BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
155     BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_searchResponse);
156 }
157
158 static void present(yp2::Package &pack, yp2::Router &router,
159                     int start, int number,
160                     const char *setname)
161 {
162     // Create package with Z39.50 present request in it
163             
164     ODR odr = odr_createmem(ODR_ENCODE);
165     Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
166     
167     apdu->u.presentRequest->resultSetId  = odr_strdup(odr, setname);
168     *apdu->u.presentRequest->resultSetStartPoint = start;
169     *apdu->u.presentRequest->numberOfRecordsRequested = number;
170
171     BOOST_CHECK(apdu);
172     if (!apdu)
173         return;
174     
175     pack.request() = apdu;
176     
177     odr_destroy(odr);
178     
179     Z_GDU *gdu_test = pack.request().get();
180     BOOST_CHECK(gdu_test);
181     
182     // Put it in router
183     pack.router(router).move(); 
184     
185     // Inspect that we got Z39.50 present response
186     yazpp_1::GDU *gdu = &pack.response();
187     
188     BOOST_CHECK(!pack.session().is_closed()); 
189     
190     Z_GDU *z_gdu = gdu->get();
191     BOOST_CHECK(z_gdu);
192     if (!z_gdu)
193         return;
194     BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
195     BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_presentResponse);
196 }
197
198 BOOST_AUTO_TEST_CASE( test_filter_virt_db_3 )
199 {
200     try 
201     {
202         yp2::RouterChain router;
203
204         yp2::filter::Log filter_log1("FRONT");
205 #if 0
206         router.rule(filter_log1);
207 #endif
208    
209         yp2::filter::Virt_db vdb;        
210         router.rule(vdb);
211         vdb.add_map_db2vhost("Default", "localhost:210");
212         yp2::filter::Log filter_log2("BACK");
213 #if 0
214         router.rule(filter_log2);
215 #endif
216         yp2::filter::Backend_test btest;
217         router.rule(btest);
218
219         yp2::Session session1;
220         yp2::Origin origin1;
221         
222         {
223             yp2::Package pack(session1, origin1);
224             init(pack, router);
225         }
226         {
227             // search for database for which there is no map
228             yp2::Package pack(session1, origin1);
229             search(pack, router, "computer", "bad_database", "default");
230         }
231         {
232             // search for database for which there a map
233             yp2::Package pack(session1, origin1);
234             search(pack, router, "other", "Default", "default");
235         }
236         {
237             // present from last search
238             yp2::Package pack(session1, origin1);
239             present(pack, router, 1, 2, "default");
240         }
241     }
242     catch ( ... ) {
243         BOOST_CHECK (false);
244     }
245 }
246
247
248 /*
249  * Local variables:
250  * c-basic-offset: 4
251  * indent-tabs-mode: nil
252  * c-file-style: "stroustrup"
253  * End:
254  * vim: shiftwidth=4 tabstop=8 expandtab
255  */