1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) 1998-2009 Index Data and Mike Taylor
3 * See the file LICENSE for details.
7 #include <yaz/pquery.h>
8 #include <yaz/sortspec.h>
11 #include <yaz/oid_db.h>
13 static const char *try_query[] = {
14 "@attr 1=4 petersson",
15 "@attr 1=1016 petersson",
17 "@attr 1=1016 kingdom",
25 "@attr 1=1016 computer",
33 static const char *try_syntax [] = {
44 static const char *try_sort [] = {
52 Zlint_test_search_01::Zlint_test_search_01()
55 m_record_syntax_no = 0;
60 Zlint_test_search_01::~Zlint_test_search_01()
64 Zlint_code Zlint_test_search_01::init(Zlint *z)
67 Z_APDU *apdu = z->create_Z_PDU(Z_APDU_initRequest);
68 Z_InitRequest *init = apdu->u.initRequest;
70 z->msg_check_for("search and retrieve");
72 ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
74 ODR_MASK_SET(init->options, Z_Options_namedResultSets);
75 ODR_MASK_SET(init->options, Z_Options_sort);
77 int r = z->send_Z_PDU(apdu, &len);
80 z->msg_check_fail("unable to send init request");
86 Zlint_code Zlint_test_search_01::sendTest(Zlint *z)
88 if (!m_got_result_set)
90 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_searchRequest);
92 sr = apdu->u.searchRequest;
93 sr->query = (Z_Query *) odr_malloc(z->odr_encode(), sizeof(*sr->query));
94 if (try_query[m_query_no] && sr)
96 sr->query->which = Z_Query_type_1;
98 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
100 z->getDatabase(&sr->databaseNames, &sr->num_databaseNames);
102 rpn = yaz_pqf_parse(pqf_parser, z->odr_encode(),
103 try_query[m_query_no]);
105 yaz_pqf_destroy (pqf_parser);
109 z->msg_check_fail("Query %s invalid", try_query[m_query_no]);
110 return TEST_FINISHED;
113 sr->query->u.type_1 = rpn;
114 z->send_Z_PDU(apdu, &len);
118 z->msg_check_notapp();
119 z->msg_check_info("unable to get any hit count");
120 return TEST_FINISHED;
123 else if (m_got_result_set && try_syntax[m_record_syntax_no])
126 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_presentRequest);
127 Z_PresentRequest *pr = apdu->u.presentRequest;
128 *pr->numberOfRecordsRequested = 1;
129 *pr->resultSetStartPoint = 1;
131 z->msg_check_for("record syntax %s", try_syntax[m_record_syntax_no]);
132 pr->preferredRecordSyntax =
133 yaz_string_to_oid_odr(yaz_oid_std(),
134 CLASS_RECSYN, try_syntax[m_record_syntax_no],
136 z->send_Z_PDU(apdu, &len);
137 return TEST_CONTINUE;
139 else if(m_got_result_set && !try_syntax[m_record_syntax_no])
141 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_sortRequest);
142 if (apdu && try_sort[m_sort_no])
144 z->msg_check_for("sort %s", try_sort[m_sort_no]);
146 const char *setstring = "default";
148 Z_SortRequest *sr = apdu->u.sortRequest;
150 sr->num_inputResultSetNames = 1;
151 sr->num_inputResultSetNames = 1;
152 sr->inputResultSetNames = (Z_InternationalString **)
153 odr_malloc (z->odr_encode(), sizeof(*sr->inputResultSetNames));
154 sr->inputResultSetNames[0] = odr_strdup (z->odr_encode(), setstring);
155 sr->sortedResultSetName = odr_strdup(z->odr_encode(), setstring);
156 sr->sortSequence = yaz_sort_spec(z->odr_encode(), try_sort[m_sort_no]);
157 z->send_Z_PDU(apdu, &len);
160 return TEST_FINISHED;
164 printf ("finished...\n");
165 return TEST_FINISHED;
167 return TEST_CONTINUE;
170 Zlint_code Zlint_test_search_01::recv_gdu(Zlint *z, Z_GDU *gdu)
172 if (gdu->which == Z_GDU_Z3950 &&
173 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_initResponse)
175 Z_InitResponse *init = gdu->u.z3950->u.initResponse;
176 int result = init->result ? *init->result : 0;
179 z->msg_check_notapp();
180 z->msg_check_info ("init rejected (result false)");
181 return TEST_FINISHED;
185 else if (gdu->which == Z_GDU_Z3950 &&
186 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_searchResponse)
188 Z_SearchResponse *sr = gdu->u.z3950->u.searchResponse;
189 if (sr->records && (sr->records->which == Z_Records_NSD
191 sr->records->which == Z_Records_multipleNSD))
193 else if (!sr->resultCount || *sr->resultCount == 0)
198 z->msg_check_info("got %d result count with %s", *sr->resultCount,
199 try_query[m_query_no]);
200 m_got_result_set = 1;
204 else if (gdu->which == Z_GDU_Z3950 &&
205 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_presentResponse)
207 Z_PresentResponse *sr = gdu->u.z3950->u.presentResponse;
208 if (sr->records && (sr->records->which == Z_Records_NSD
210 sr->records->which == Z_Records_multipleNSD))
213 z->msg_check_info("present returned NSD for %s",
214 try_syntax[m_record_syntax_no]);
216 else if (sr->records && sr->records->which == Z_Records_DBOSD
217 && sr->records->u.databaseOrSurDiagnostics->num_records>0
218 && sr->records->u.databaseOrSurDiagnostics->records[0])
220 if (sr->records->u.databaseOrSurDiagnostics->records[0]->which == Z_NamePlusRecord_databaseRecord)
222 Z_External *ext = sr->records->u.databaseOrSurDiagnostics->records[0]->u.databaseRecord;
223 Odr_oid *expectRecordSyntax =
224 yaz_string_to_oid_odr(
225 yaz_oid_std(), CLASS_RECSYN,
226 try_syntax[m_record_syntax_no], z->odr_decode());
227 if (oid_oidcmp(expectRecordSyntax,
228 ext->direct_reference))
230 z->msg_check_fail("Got Record in different syntax "
231 "from that requested %s",
232 try_syntax[m_record_syntax_no]);
237 else if (sr->records->u.databaseOrSurDiagnostics->records[0]->which == Z_NamePlusRecord_surrogateDiagnostic)
240 z->msg_check_info("present returned SD %s",
241 try_syntax[m_record_syntax_no]);
246 z->msg_check_info("present returned fragment %s",
247 try_syntax[m_record_syntax_no]);
252 z->msg_check_fail("present returned no records or diagnostics");
254 m_record_syntax_no++;
257 else if (gdu->which == Z_GDU_Z3950 &&
258 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_sortResponse)
260 Z_SortResponse *sr = gdu->u.z3950->u.sortResponse;
263 z->msg_check_info( "sort NSD for %s", try_sort[m_sort_no]);
268 z->msg_check_fail("did not receive init/search/present response "
270 return TEST_FINISHED;
273 Zlint_code Zlint_test_search_01::recv_fail(Zlint *z, int reason)
275 z->msg_check_fail("target closed connection");
276 return TEST_FINISHED;
281 * c-file-style: "Stroustrup"
282 * indent-tabs-mode: nil
284 * vim: shiftwidth=4 tabstop=8 expandtab