1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) 1998-2011 Index Data and Mike Taylor
3 * See the file LICENSE for details.
10 #include <yaz/pquery.h>
11 #include <yaz/sortspec.h>
14 #include <yaz/oid_db.h>
16 static const char *try_query[] = {
17 "@attr 1=4 petersson",
18 "@attr 1=1016 petersson",
20 "@attr 1=1016 kingdom",
28 "@attr 1=1016 computer",
36 static const char *try_syntax [] = {
47 static const char *try_sort [] = {
55 Zlint_test_search_01::Zlint_test_search_01()
58 m_record_syntax_no = 0;
63 Zlint_test_search_01::~Zlint_test_search_01()
67 Zlint_code Zlint_test_search_01::init(Zlint *z)
70 Z_APDU *apdu = z->create_Z_PDU(Z_APDU_initRequest);
71 Z_InitRequest *init = apdu->u.initRequest;
73 z->msg_check_for("search and retrieve");
75 ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
77 ODR_MASK_SET(init->options, Z_Options_namedResultSets);
78 ODR_MASK_SET(init->options, Z_Options_sort);
80 int r = z->send_Z_PDU(apdu, &len);
83 z->msg_check_fail("unable to send init request");
89 Zlint_code Zlint_test_search_01::sendTest(Zlint *z)
91 if (!m_got_result_set)
93 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_searchRequest);
95 sr = apdu->u.searchRequest;
96 sr->query = (Z_Query *) odr_malloc(z->odr_encode(), sizeof(*sr->query));
97 if (try_query[m_query_no] && sr)
99 sr->query->which = Z_Query_type_1;
101 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
103 z->getDatabase(&sr->databaseNames, &sr->num_databaseNames);
105 rpn = yaz_pqf_parse(pqf_parser, z->odr_encode(),
106 try_query[m_query_no]);
108 yaz_pqf_destroy (pqf_parser);
112 z->msg_check_fail("Query %s invalid", try_query[m_query_no]);
113 return TEST_FINISHED;
116 sr->query->u.type_1 = rpn;
117 z->send_Z_PDU(apdu, &len);
121 z->msg_check_notapp();
122 z->msg_check_info("unable to get any hit count");
123 return TEST_FINISHED;
126 else if (m_got_result_set && try_syntax[m_record_syntax_no])
129 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_presentRequest);
130 Z_PresentRequest *pr = apdu->u.presentRequest;
131 *pr->numberOfRecordsRequested = 1;
132 *pr->resultSetStartPoint = 1;
134 z->msg_check_for("record syntax %s", try_syntax[m_record_syntax_no]);
135 pr->preferredRecordSyntax =
136 yaz_string_to_oid_odr(yaz_oid_std(),
137 CLASS_RECSYN, try_syntax[m_record_syntax_no],
139 z->send_Z_PDU(apdu, &len);
140 return TEST_CONTINUE;
142 else if(m_got_result_set && !try_syntax[m_record_syntax_no])
144 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_sortRequest);
145 if (apdu && try_sort[m_sort_no])
147 z->msg_check_for("sort %s", try_sort[m_sort_no]);
149 const char *setstring = "default";
151 Z_SortRequest *sr = apdu->u.sortRequest;
153 sr->num_inputResultSetNames = 1;
154 sr->num_inputResultSetNames = 1;
155 sr->inputResultSetNames = (Z_InternationalString **)
156 odr_malloc (z->odr_encode(), sizeof(*sr->inputResultSetNames));
157 sr->inputResultSetNames[0] = odr_strdup (z->odr_encode(), setstring);
158 sr->sortedResultSetName = odr_strdup(z->odr_encode(), setstring);
159 sr->sortSequence = yaz_sort_spec(z->odr_encode(), try_sort[m_sort_no]);
160 z->send_Z_PDU(apdu, &len);
163 return TEST_FINISHED;
167 printf ("finished...\n");
168 return TEST_FINISHED;
170 return TEST_CONTINUE;
173 Zlint_code Zlint_test_search_01::recv_gdu(Zlint *z, Z_GDU *gdu)
175 if (gdu->which == Z_GDU_Z3950 &&
176 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_initResponse)
178 Z_InitResponse *init = gdu->u.z3950->u.initResponse;
179 int result = init->result ? *init->result : 0;
182 z->msg_check_notapp();
183 z->msg_check_info ("init rejected (result false)");
184 return TEST_FINISHED;
188 else if (gdu->which == Z_GDU_Z3950 &&
189 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_searchResponse)
191 Z_SearchResponse *sr = gdu->u.z3950->u.searchResponse;
192 if (sr->records && (sr->records->which == Z_Records_NSD
194 sr->records->which == Z_Records_multipleNSD))
196 else if (!sr->resultCount || *sr->resultCount == 0)
201 z->msg_check_info("got %d result count with %s", *sr->resultCount,
202 try_query[m_query_no]);
203 m_got_result_set = 1;
207 else if (gdu->which == Z_GDU_Z3950 &&
208 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_presentResponse)
210 Z_PresentResponse *sr = gdu->u.z3950->u.presentResponse;
211 if (sr->records && (sr->records->which == Z_Records_NSD
213 sr->records->which == Z_Records_multipleNSD))
216 z->msg_check_info("present returned NSD for %s",
217 try_syntax[m_record_syntax_no]);
219 else if (sr->records && sr->records->which == Z_Records_DBOSD
220 && sr->records->u.databaseOrSurDiagnostics->num_records>0
221 && sr->records->u.databaseOrSurDiagnostics->records[0])
223 if (sr->records->u.databaseOrSurDiagnostics->records[0]->which == Z_NamePlusRecord_databaseRecord)
225 Z_External *ext = sr->records->u.databaseOrSurDiagnostics->records[0]->u.databaseRecord;
226 Odr_oid *expectRecordSyntax =
227 yaz_string_to_oid_odr(
228 yaz_oid_std(), CLASS_RECSYN,
229 try_syntax[m_record_syntax_no], z->odr_decode());
230 if (oid_oidcmp(expectRecordSyntax,
231 ext->direct_reference))
233 z->msg_check_fail("Got Record in different syntax "
234 "from that requested %s",
235 try_syntax[m_record_syntax_no]);
240 else if (sr->records->u.databaseOrSurDiagnostics->records[0]->which == Z_NamePlusRecord_surrogateDiagnostic)
243 z->msg_check_info("present returned SD %s",
244 try_syntax[m_record_syntax_no]);
249 z->msg_check_info("present returned fragment %s",
250 try_syntax[m_record_syntax_no]);
255 z->msg_check_fail("present returned no records or diagnostics");
257 m_record_syntax_no++;
260 else if (gdu->which == Z_GDU_Z3950 &&
261 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_sortResponse)
263 Z_SortResponse *sr = gdu->u.z3950->u.sortResponse;
266 z->msg_check_info( "sort NSD for %s", try_sort[m_sort_no]);
271 z->msg_check_fail("did not receive init/search/present response "
273 return TEST_FINISHED;
276 Zlint_code Zlint_test_search_01::recv_fail(Zlint *z, int reason)
278 z->msg_check_fail("target closed connection");
279 return TEST_FINISHED;
284 * c-file-style: "Stroustrup"
285 * indent-tabs-mode: nil
287 * vim: shiftwidth=4 tabstop=8 expandtab