816a838649d1e0942de5bd3df46071f5c86cc376
[yazpp-moved-to-github.git] / zlint / test-search-01.cpp
1 /*
2  * Copyright (c) 2004-2007, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: test-search-01.cpp,v 1.7 2007-04-12 15:00:33 adam Exp $
6  */
7
8 #include <yaz/log.h>
9 #include <yaz/pquery.h>
10 #include <yaz/sortspec.h>
11
12 #include <zlint.h>
13 #include <yaz/oid_db.h>
14
15 static const char *try_query[] = {
16     "@attr 1=4 petersson",
17     "@attr 1=1016 petersson",
18     "@attr 1=4 kingdom",
19     "@attr 1=1016 kingdom",
20     "@attr 1=62 sword",
21     "sword"
22     "seven",
23     "@attr 1=4 water",
24     "@attr 1=1016 water",
25     "computer",
26     "@attr 1=4 computer",
27     "@attr 1=1016 computer",
28     "water",
29     "join",
30     "about",
31     "map",
32     0,
33 };
34
35 static const char *try_syntax [] = {
36     "usmarc",
37     "unimarc",
38     "danmarc",
39     "sutrs",
40     "grs1",
41     "xml",
42     "normarc",
43     0
44 };
45
46 static const char *try_sort [] = {
47     "1=4 <",
48     "1=4 >",
49     "1=62 >",
50     "1=62 <",
51     0
52 };
53
54 Zlint_test_search_01::Zlint_test_search_01()
55 {
56     m_query_no = 0;
57     m_record_syntax_no = 0;
58     m_got_result_set = 0;
59     m_sort_no = 0;
60 }
61
62 Zlint_test_search_01::~Zlint_test_search_01()
63 {
64 }
65
66 Zlint_code Zlint_test_search_01::init(Zlint *z)
67 {
68     int len;
69     Z_APDU *apdu = z->create_Z_PDU(Z_APDU_initRequest);
70     Z_InitRequest *init = apdu->u.initRequest;
71
72     z->msg_check_for("search and retrieve");
73
74     ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
75
76     ODR_MASK_SET(init->options, Z_Options_namedResultSets);
77     ODR_MASK_SET(init->options, Z_Options_sort);
78
79     int r = z->send_Z_PDU(apdu, &len);
80     if (r < 0)
81     {
82         z->msg_check_fail("unable to send init request");
83         return TEST_FINISHED;
84     }
85     return TEST_CONTINUE;
86 }
87
88 Zlint_code Zlint_test_search_01::sendTest(Zlint *z)
89 {
90     if (!m_got_result_set)
91     {
92         Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_searchRequest);
93         Z_SearchRequest *sr;
94         sr = apdu->u.searchRequest;
95         sr->query = (Z_Query *) odr_malloc(z->odr_encode(), sizeof(*sr->query));
96         if (try_query[m_query_no] && sr)
97         {
98             sr->query->which = Z_Query_type_1;
99             Z_RPNQuery *rpn;
100             YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
101             
102             z->getDatabase(&sr->databaseNames, &sr->num_databaseNames);
103             
104             rpn = yaz_pqf_parse(pqf_parser, z->odr_encode(),
105                                 try_query[m_query_no]);
106             
107             yaz_pqf_destroy (pqf_parser);
108             
109             if (!rpn)
110             {
111                 z->msg_check_fail("Query %s invalid", try_query[m_query_no]);
112                 return TEST_FINISHED;
113             }
114             int len;
115             sr->query->u.type_1 = rpn;
116             z->send_Z_PDU(apdu, &len);
117         }
118         else
119         {
120             z->msg_check_notapp();
121             z->msg_check_info("unable to get any hit count");
122             return TEST_FINISHED;
123         }
124     }
125     else if (m_got_result_set && try_syntax[m_record_syntax_no])
126     {
127         int len;
128         Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_presentRequest);
129         Z_PresentRequest *pr = apdu->u.presentRequest;
130         *pr->numberOfRecordsRequested = 1;
131         *pr->resultSetStartPoint = 1;
132
133         z->msg_check_for("record syntax %s", try_syntax[m_record_syntax_no]);
134         pr->preferredRecordSyntax =
135             yaz_string_to_oid_odr(yaz_oid_std(),
136                                   CLASS_RECSYN, try_syntax[m_record_syntax_no],
137                                   z->odr_encode());
138         z->send_Z_PDU(apdu, &len);
139         return TEST_CONTINUE;
140     }
141     else if(m_got_result_set && !try_syntax[m_record_syntax_no])
142     {
143         Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_sortRequest);
144         if (apdu && try_sort[m_sort_no])
145         {
146             z->msg_check_for("sort %s", try_sort[m_sort_no]);
147
148             char *setstring = "default";
149             int len;
150             Z_SortRequest *sr = apdu->u.sortRequest;
151             
152             sr->num_inputResultSetNames = 1;
153             sr->num_inputResultSetNames = 1;
154             sr->inputResultSetNames = (Z_InternationalString **)
155                 odr_malloc (z->odr_encode(), sizeof(*sr->inputResultSetNames));
156             sr->inputResultSetNames[0] = odr_strdup (z->odr_encode(), setstring);
157             sr->sortedResultSetName = odr_strdup(z->odr_encode(), setstring);
158             sr->sortSequence = yaz_sort_spec(z->odr_encode(), try_sort[m_sort_no]);
159             z->send_Z_PDU(apdu, &len);
160         }
161         else
162             return TEST_FINISHED;
163     }
164     else
165     {
166         printf ("finished...\n");
167         return TEST_FINISHED;
168     }
169     return TEST_CONTINUE;
170 }
171
172 Zlint_code Zlint_test_search_01::recv_gdu(Zlint *z, Z_GDU *gdu)
173 {
174     if (gdu->which == Z_GDU_Z3950 &&
175         gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_initResponse)
176     {
177         Z_InitResponse *init = gdu->u.z3950->u.initResponse;
178         int result = init->result ? *init->result : 0;
179         if (!result)
180         {
181             z->msg_check_notapp();
182             z->msg_check_info ("init rejected (result false)");
183             return TEST_FINISHED;
184         }
185         return sendTest(z);
186     }
187     else if (gdu->which == Z_GDU_Z3950 &&
188              gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_searchResponse)
189     {
190         Z_SearchResponse *sr = gdu->u.z3950->u.searchResponse;
191         if (sr->records && (sr->records->which == Z_Records_NSD 
192                             || 
193                             sr->records->which == Z_Records_multipleNSD))
194             m_query_no++;
195         else if (!sr->resultCount || *sr->resultCount == 0)
196             m_query_no++;
197         else
198         {
199             z->msg_check_ok();
200             z->msg_check_info("got %d result count with %s", *sr->resultCount,
201                               try_query[m_query_no]);
202             m_got_result_set = 1;
203         }
204         return sendTest(z);
205     }
206     else if (gdu->which == Z_GDU_Z3950 && 
207              gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_presentResponse)
208     {
209         Z_PresentResponse *sr = gdu->u.z3950->u.presentResponse;
210         if (sr->records && (sr->records->which == Z_Records_NSD 
211                             || 
212                             sr->records->which == Z_Records_multipleNSD))
213         {
214             z->msg_check_ok();
215             z->msg_check_info("present returned NSD for %s",
216                               try_syntax[m_record_syntax_no]);
217         }
218         else if (sr->records && sr->records->which == Z_Records_DBOSD
219                  && sr->records->u.databaseOrSurDiagnostics->num_records>0
220                  && sr->records->u.databaseOrSurDiagnostics->records[0])
221         {
222             if (sr->records->u.databaseOrSurDiagnostics->records[0]->which == Z_NamePlusRecord_databaseRecord)
223             {
224                 Z_External *ext = sr->records->u.databaseOrSurDiagnostics->records[0]->u.databaseRecord;
225                 Odr_oid *expectRecordSyntax =
226                     yaz_string_to_oid_odr(
227                         yaz_oid_std(), CLASS_RECSYN,
228                         try_syntax[m_record_syntax_no], z->odr_decode());
229                 if (oid_oidcmp(expectRecordSyntax,
230                                ext->direct_reference))
231                 {
232                     z->msg_check_fail("Got Record in different syntax "
233                                       "from that requested %s",
234                                       try_syntax[m_record_syntax_no]);
235                 }
236                 else
237                     z->msg_check_ok();
238             }
239             else if (sr->records->u.databaseOrSurDiagnostics->records[0]->which == Z_NamePlusRecord_surrogateDiagnostic)
240             {
241                 z->msg_check_ok();
242                 z->msg_check_info("present returned SD %s",
243                                   try_syntax[m_record_syntax_no]);
244             }
245             else
246             {
247                 z->msg_check_ok();
248                 z->msg_check_info("present returned fragment %s",
249                                   try_syntax[m_record_syntax_no]);
250             }
251         }
252         else
253         {
254             z->msg_check_fail("present returned no records or diagnostics");
255         }
256         m_record_syntax_no++;
257         return sendTest(z);
258     }
259     else if (gdu->which == Z_GDU_Z3950 && 
260              gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_sortResponse)
261     {
262         Z_SortResponse *sr =  gdu->u.z3950->u.sortResponse;
263         z->msg_check_ok();
264         if (sr->diagnostics)
265             z->msg_check_info( "sort NSD for %s", try_sort[m_sort_no]);
266         m_sort_no++;
267         return sendTest(z);
268     }
269     else
270         z->msg_check_fail("did not receive init/search/present response "
271                           "as expected");
272     return TEST_FINISHED;
273 }
274
275 Zlint_code Zlint_test_search_01::recv_fail(Zlint *z, int reason)
276 {
277     z->msg_check_fail("target closed connection");
278     return TEST_FINISHED;
279 }
280 /*
281  * Local variables:
282  * c-basic-offset: 4
283  * indent-tabs-mode: nil
284  * End:
285  * vim: shiftwidth=4 tabstop=8 expandtab
286  */
287