Tests as separate object implementations
[yazpp-moved-to-github.git] / zlint / test-scan-01.cpp
1 /*
2  * Copyright (c) 2004, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: test-scan-01.cpp,v 1.1 2004-03-25 23:14:07 adam Exp $
6  */
7
8 #include <yaz/log.h>
9 #include <yaz/pquery.h>
10
11 #include <zlint.h>
12
13 static const char *try_scan [] = {
14     "@attr 1=4 ab",
15     "@attr 1=1003 ab",
16     "@attr 1=1016 ab",
17     0
18 };
19
20 Zlint_test_scan_01::Zlint_test_scan_01()
21 {
22     m_scan_no = 0;
23 }
24
25 Zlint_test_scan_01::~Zlint_test_scan_01()
26 {
27 }
28
29 Zlint_code Zlint_test_scan_01::init(Zlint *z)
30 {
31     int len;
32     Z_APDU *apdu = z->create_Z_PDU(Z_APDU_initRequest);
33     Z_InitRequest *init = apdu->u.initRequest;
34
35
36     ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
37     ODR_MASK_SET(init->options, Z_Options_namedResultSets);
38     ODR_MASK_SET(init->options, Z_Options_scan);
39     
40     int r = z->send_Z_PDU(apdu, &len);
41     if (r < 0)
42     {
43         z->msg_check_fail("unable to send init request");
44         return TEST_FINISHED;
45     }
46     return TEST_CONTINUE;
47 }
48
49 Zlint_code Zlint_test_scan_01::sendTest(Zlint *z)
50 {
51     if (try_scan[m_scan_no])
52     {
53         int len;
54         z->msg_check_for("scan support %s", try_scan[m_scan_no]);
55
56         Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_scanRequest);
57         YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
58         Z_ScanRequest *sr = apdu->u.scanRequest;
59         sr->termListAndStartPoint = yaz_pqf_scan(pqf_parser,
60                                                  z->odr_encode(),
61                                                  &sr->attributeSet,
62                                                  try_scan[m_scan_no]);
63         
64         z->getDatabase(&sr->databaseNames, &sr->num_databaseNames);
65         
66         yaz_pqf_destroy (pqf_parser);
67         z->send_Z_PDU(apdu, &len);
68         return TEST_CONTINUE;
69     }
70     else
71         return TEST_FINISHED;
72 }
73
74 Zlint_code Zlint_test_scan_01::recv_gdu(Zlint *z, Z_GDU *gdu)
75 {
76     if (gdu->which == Z_GDU_Z3950 &&
77         gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_initResponse)
78     {
79         Z_InitResponse *init = gdu->u.z3950->u.initResponse;
80         int ver = z->initResponseGetVersion(init);
81         int result = init->result ? *init->result : 0;
82         if (ver > 3 || ver < 2)
83             z->msg_check_fail("got version %d, expected 2 or 3", ver);
84         if (!result)
85         {
86             z->msg_check_fail("init rejected (result false)");
87             return TEST_FINISHED;
88         }
89         else if (!ODR_MASK_GET(init->options, Z_Options_scan))
90         {
91             z->msg_check_notapp();
92             z->msg_check_info("scan unsupported");
93             return TEST_FINISHED;
94         }
95         else
96         {
97             sendTest(z);
98             return TEST_CONTINUE;
99         }
100     }
101     else if (gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_scanResponse)
102     {
103         Z_ScanResponse *sr =  gdu->u.z3950->u.scanResponse;
104         if (sr->entries->nonsurrogateDiagnostics)
105         {
106             z->msg_check_ok();
107             z->msg_check_info("scan NSD for %s", try_scan[m_scan_no]);
108         }
109         else if (sr->entries->entries && sr->entries->num_entries > 0)
110         {
111             z->msg_check_ok();
112         }
113         else
114         {
115             z->msg_check_fail("scan no entries/diagnostics for %s",
116                               try_scan[m_scan_no]);
117         }
118         m_scan_no++;
119         return sendTest(z);
120     }
121     else
122         z->msg_check_fail("did not receive init response as expected");
123     return TEST_FINISHED;
124 }
125
126 Zlint_code Zlint_test_scan_01::recv_fail(Zlint *z, int reason)
127 {
128     m_scan_no++;
129     z->msg_check_fail("target closed connection");
130     return TEST_FINISHED;
131 }