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