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