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