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