Add LICENSE file and Refer to it from the source. Include license material
[metaproxy-moved-to-github.git] / src / test_filter_backend_test.cpp
1 /* $Id: test_filter_backend_test.cpp,v 1.8 2006-06-10 14:29:12 adam Exp $
2    Copyright (c) 2005-2006, 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_backend_test.hpp"
13 #include "filter_log.hpp"
14
15 #include "router_chain.hpp"
16 #include "session.hpp"
17 #include "package.hpp"
18
19 #include <yaz/zgdu.h>
20 #include <yaz/pquery.h>
21 #include <yaz/otherinfo.h>
22
23 #define BOOST_AUTO_TEST_MAIN
24 #include <boost/test/auto_unit_test.hpp>
25 using namespace boost::unit_test;
26
27 namespace mp = metaproxy_1;
28
29 BOOST_AUTO_UNIT_TEST( test_filter_backend_test_1 )
30 {
31     try 
32     {
33         mp::filter::Backend_test btest;
34     }
35     catch ( ... ) {
36         BOOST_CHECK (false);
37     }
38 }
39
40 BOOST_AUTO_UNIT_TEST( test_filter_backend_test_2 )
41 {
42     try 
43     {
44         mp::RouterChain router;
45         
46         mp::filter::Backend_test btest;
47         router.append(btest);
48         
49         mp::Package pack;
50         
51         mp::odr odr;
52         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
53         
54         BOOST_CHECK(apdu);
55         
56         pack.request() = apdu;
57         
58         // Put it in router
59         pack.router(router).move(); 
60         
61         // Inspect that we got Z39.50 init Response OK.
62         yazpp_1::GDU *gdu = &pack.response();
63         
64         BOOST_CHECK(!pack.session().is_closed()); 
65         
66         Z_GDU *z_gdu = gdu->get();
67         BOOST_CHECK(z_gdu);
68         if (z_gdu) {
69             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
70             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
71         }
72     }
73     catch ( ... ) {
74         BOOST_CHECK (false);
75     }
76 }
77
78 BOOST_AUTO_UNIT_TEST( test_filter_backend_test_3 )
79 {
80     try 
81     {
82         mp::RouterChain router;
83         
84         mp::filter::Backend_test btest;
85         router.append(btest);
86         
87         mp::Package pack;
88         
89         // send search request as first request.. That should fail with
90         // a close from the backend
91         mp::odr odr;
92         Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
93
94         mp::util::pqf(odr, apdu, "computer");
95         
96         apdu->u.searchRequest->num_databaseNames = 1;
97         apdu->u.searchRequest->databaseNames = (char**)
98             odr_malloc(odr, sizeof(char *));
99         apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, "Default");
100         
101         BOOST_CHECK(apdu);
102         
103         pack.request() = apdu;
104         
105         // Put it in router
106         pack.router(router).move(); 
107         
108         // Inspect that we got Z39.50 close
109         yazpp_1::GDU *gdu = &pack.response();
110         
111         BOOST_CHECK(pack.session().is_closed()); 
112         
113         Z_GDU *z_gdu = gdu->get();
114         BOOST_CHECK(z_gdu);
115         if (z_gdu) {
116             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
117             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
118         }
119     }
120     catch ( ... ) {
121         BOOST_CHECK (false);
122     }
123 }
124
125 BOOST_AUTO_UNIT_TEST( test_filter_backend_test_4 )
126 {
127     try 
128     {
129         mp::RouterChain router;
130         
131         mp::filter::Backend_test btest;
132         router.append(btest);
133         
134         mp::Package pack;
135         
136         // send present request as first request.. That should fail with
137         // a close from the backend
138         mp::odr odr;
139         Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
140
141         BOOST_CHECK(apdu);
142         
143         pack.request() = apdu;
144         
145         // Put it in router
146         pack.router(router).move(); 
147         
148         // Inspect that we got Z39.50 close
149         yazpp_1::GDU *gdu = &pack.response();
150         
151         BOOST_CHECK(pack.session().is_closed()); 
152         
153         Z_GDU *z_gdu = gdu->get();
154         BOOST_CHECK(z_gdu);
155         if (z_gdu) {
156             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
157             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
158         }
159     }
160     catch ( ... ) {
161         BOOST_CHECK (false);
162     }
163 }
164
165
166 /*
167  * Local variables:
168  * c-basic-offset: 4
169  * indent-tabs-mode: nil
170  * c-file-style: "stroustrup"
171  * End:
172  * vim: shiftwidth=4 tabstop=8 expandtab
173  */