2 * Copyright (c) 2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: test-search-01.cpp,v 1.2 2004-09-06 07:50:51 adam Exp $
9 #include <yaz/pquery.h>
10 #include <yaz/sortspec.h>
14 static const char *try_query[] = {
15 "@attr 1=4 petersson",
16 "@attr 1=1016 petersson",
18 "@attr 1=1016 kingdom",
26 "@attr 1=1016 computer",
34 static const char *try_syntax [] = {
45 static const char *try_sort [] = {
53 Zlint_test_search_01::Zlint_test_search_01()
56 m_record_syntax_no = 0;
61 Zlint_test_search_01::~Zlint_test_search_01()
65 Zlint_code Zlint_test_search_01::init(Zlint *z)
68 Z_APDU *apdu = z->create_Z_PDU(Z_APDU_initRequest);
69 Z_InitRequest *init = apdu->u.initRequest;
71 z->msg_check_for("search and retrieve");
73 ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
75 ODR_MASK_SET(init->options, Z_Options_namedResultSets);
76 ODR_MASK_SET(init->options, Z_Options_sort);
78 int r = z->send_Z_PDU(apdu, &len);
81 z->msg_check_fail("unable to send init request");
87 Zlint_code Zlint_test_search_01::sendTest(Zlint *z)
89 if (!m_got_result_set)
91 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_searchRequest);
93 sr = apdu->u.searchRequest;
94 sr->query = (Z_Query *) odr_malloc(z->odr_encode(), sizeof(*sr->query));
95 if (try_query[m_query_no] && sr)
97 sr->query->which = Z_Query_type_1;
99 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
101 z->getDatabase(&sr->databaseNames, &sr->num_databaseNames);
103 rpn = yaz_pqf_parse(pqf_parser, z->odr_encode(),
104 try_query[m_query_no]);
106 yaz_pqf_destroy (pqf_parser);
110 z->msg_check_fail("Query %s invalid", try_query[m_query_no]);
111 return TEST_FINISHED;
114 sr->query->u.type_1 = rpn;
115 z->send_Z_PDU(apdu, &len);
119 z->msg_check_notapp();
120 z->msg_check_info("unable to get any hit count");
121 return TEST_FINISHED;
124 else if (m_got_result_set && try_syntax[m_record_syntax_no])
127 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_presentRequest);
128 Z_PresentRequest *pr = apdu->u.presentRequest;
129 *pr->numberOfRecordsRequested = 1;
130 *pr->resultSetStartPoint = 1;
132 z->msg_check_for("record syntax %s", try_syntax[m_record_syntax_no]);
133 pr->preferredRecordSyntax =
134 yaz_str_to_z3950oid(z->odr_encode(), CLASS_RECSYN,
135 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 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 ver = z->initResponseGetVersion(init);
177 int result = init->result ? *init->result : 0;
180 z->msg_check_notapp();
181 z->msg_check_info ("init rejected (result false)");
182 return TEST_FINISHED;
186 else if (gdu->which == Z_GDU_Z3950 &&
187 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_searchResponse)
189 Z_SearchResponse *sr = gdu->u.z3950->u.searchResponse;
190 if (sr->records && (sr->records->which == Z_Records_NSD
192 sr->records->which == Z_Records_multipleNSD))
194 else if (!sr->resultCount || *sr->resultCount == 0)
199 z->msg_check_info("got %d result count with %s", *sr->resultCount,
200 try_query[m_query_no]);
201 m_got_result_set = 1;
205 else if (gdu->which == Z_GDU_Z3950 &&
206 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_presentResponse)
208 Z_PresentResponse *sr = gdu->u.z3950->u.presentResponse;
209 if (sr->records && (sr->records->which == Z_Records_NSD
211 sr->records->which == Z_Records_multipleNSD))
214 z->msg_check_info("present returned NSD for %s",
215 try_syntax[m_record_syntax_no]);
217 else if (sr->records && sr->records->which == Z_Records_DBOSD
218 && sr->records->u.databaseOrSurDiagnostics->num_records>0
219 && sr->records->u.databaseOrSurDiagnostics->records[0])
221 if (sr->records->u.databaseOrSurDiagnostics->records[0]->which == Z_NamePlusRecord_databaseRecord)
223 Z_External *ext = sr->records->u.databaseOrSurDiagnostics->records[0]->u.databaseRecord;
224 Odr_oid *expectRecordSyntax =
225 yaz_str_to_z3950oid(z->odr_decode(), CLASS_RECSYN,
226 try_syntax[m_record_syntax_no]);
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;