configure: only consider hiredis if libgcrypt is also present
[yaz-moved-to-github.git] / test / test_solr.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <yaz/srw.h>
11 #include <yaz/log.h>
12 #if YAZ_HAVE_XML2
13 #include <libxml/parser.h>
14 #endif
15 #include <yaz/test.h>
16 #include <yaz/yaz-version.h>
17 #include <yaz/pquery.h>
18
19 #if YAZ_HAVE_XML2
20 int compare_solr_req(ODR odr, Z_SRW_PDU *sr,
21                      const char *charset, const char *expect)
22 {
23     int r;
24     Z_GDU *gdu = 0;
25     YAZ_CHECK(sr);
26
27     if (!sr)
28         return 0;
29
30     gdu = z_get_HTTP_Request_host_path(odr, "localhost", "Default");
31     YAZ_CHECK(gdu);
32     if (!gdu)
33         return 0;
34
35     yaz_solr_encode_request(gdu->u.HTTP_Request, sr, odr, charset);
36
37     r = z_GDU(odr, &gdu, 0, 0);
38     YAZ_CHECK(r);
39     if (r)
40     {
41         int len = 0;
42         char *buf = odr_getbuf(odr, &len, 0);
43
44         if (buf)
45         {
46             if (len == strlen(expect) && !memcmp(buf, expect, len))
47             {
48                 odr_reset(odr);
49                 return 1;
50             }
51             yaz_log(YLOG_WARN, "Expect:\n%s\n", expect);
52             yaz_log(YLOG_WARN, "Got:\n%.*s\n", len, buf);
53          }
54     }
55     odr_reset(odr);
56     return 0;
57 }
58 #endif
59
60 void tst_encoding(void)
61 {
62 #if YAZ_HAVE_XML2
63     ODR odr = odr_createmem(ODR_ENCODE);
64
65     {
66         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
67                                         "1.2");
68
69         sr->u.request->query = "title:solr";
70         YAZ_CHECK(compare_solr_req(
71                       odr, sr, 0,
72                       "GET Default/select?defType=lucene&q=title%3Asolr "
73                       "HTTP/1.1\r\n"
74                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
75                       "Host: localhost\r\n"
76                       "Content-Type: text/xml\r\n\r\n"));
77     }
78
79     {
80         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
81                                         "1.2");
82         sr->u.request->query = "title:solr";
83         YAZ_CHECK(compare_solr_req(
84                       odr, sr, "utf-8",
85                       "GET Default/select?defType=lucene&q=title%3Asolr "
86                       "HTTP/1.1\r\n"
87                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
88                       "Host: localhost\r\n"
89                       "Content-Type: text/xml; charset=utf-8\r\n\r\n"));
90     }
91
92     {
93         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
94                                         "1.2");
95
96         sr->u.request->query = "title:solr";
97         sr->u.request->startRecord = odr_intdup(odr, 3);
98         sr->u.request->maximumRecords = odr_intdup(odr, 10);
99
100         YAZ_CHECK(compare_solr_req(
101                       odr, sr, 0,
102                       "GET Default/select?defType=lucene&q=title%3Asolr&"
103                       "start=2&rows=10"
104                       " HTTP/1.1\r\n"
105                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
106                       "Host: localhost\r\n"
107                       "Content-Type: text/xml\r\n\r\n"));
108     }
109
110     {
111         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
112                                         "1.2");
113
114         sr->u.request->query = "title:solr";
115         sr->u.request->startRecord = odr_intdup(odr, 3);
116         sr->u.request->maximumRecords = odr_intdup(odr, 10);
117         sr->u.request->facetList = yaz_pqf_parse_facet_list(
118             odr, "@attr 1=date @attr 2=0, @attr 1=title_exact @attr 3=17");
119
120         YAZ_CHECK(compare_solr_req(
121                       odr, sr, 0,
122                       "GET Default/select?defType=lucene&q=title%3Asolr&"
123                       "start=2&rows=10"
124                       "&facet=true&facet.mincount=1&facet.field=date"
125                       "&facet.field=title_exact&f.title_exact.facet.limit=17"
126                       " HTTP/1.1\r\n"
127                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
128                       "Host: localhost\r\n"
129                       "Content-Type: text/xml\r\n\r\n"));
130     }
131
132     odr_destroy(odr);
133 /* YAZ_HAVE_XML2 */
134 #endif
135 }
136
137
138 int check_response(ODR o, const char *content, Z_SRW_searchRetrieveResponse **p)
139 {
140     int r;
141     Z_GDU *gdu;
142     Z_SRW_PDU *sr_p;
143     char *http_response = odr_malloc(o, strlen(content) + 300);
144
145     strcpy(http_response,
146            "HTTP/1.1 200 OK\r\n"
147            "Last-Modified: Wed, 13 Apr 2011 08:30:59 GMT\r\n"
148            "ETag: \"MjcyMWE5M2JiNDgwMDAwMFNvbHI=\"\r\n"
149            "Content-Type: text/xml; charset=utf-8\r\n");
150     sprintf(http_response + strlen(http_response),
151             "Content-Length: %d\r\n\r\n", (int) strlen(content));
152     strcat(http_response, content);
153
154     odr_setbuf(o, http_response, strlen(http_response), 0);
155
156     r = z_GDU(o, &gdu, 0, 0);
157     if (!r || gdu->which != Z_GDU_HTTP_Response)
158         return 0;
159     r = yaz_solr_decode_response(o, gdu->u.HTTP_Response, &sr_p);
160     if (r)
161         return 0;
162     if (sr_p->which != Z_SRW_searchRetrieve_response)
163         return 0;
164     *p = sr_p->u.response;
165     return 1;
166 }
167
168 void tst_decoding(void)
169 {
170 #if YAZ_HAVE_XML2
171     ODR odr = odr_createmem(ODR_DECODE);
172     Z_SRW_searchRetrieveResponse *response;
173
174     YAZ_CHECK(check_response(
175                   odr,
176                   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
177                   "<response>\n"
178                   "<lst name=\"responseHeader\"><int name=\"status\">0</int>"
179                   "<int name=\"QTime\">1</int><lst name=\"params\">"
180                   "<str name=\"start\">0</str><str name=\"q\">@attr 1=title solr</str>"
181                   "<str name=\"rows\">0</str></lst>"
182                   "</lst><result name=\"response\" numFound=\"91\" start=\"0\"/>\n"
183                   "</response>\n", &response));
184     YAZ_CHECK_EQ(*response->numberOfRecords, 91);
185     YAZ_CHECK_EQ(response->num_records, 0);
186     YAZ_CHECK(response->records == 0);
187     YAZ_CHECK_EQ(response->num_diagnostics, 0);
188     YAZ_CHECK(response->diagnostics == 0);
189     YAZ_CHECK(response->nextRecordPosition == 0);
190     YAZ_CHECK(response->facetList == 0);
191
192     odr_reset(odr);
193
194     YAZ_CHECK(
195         check_response(
196             odr,
197             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
198             "<response><lst name=\"responseHeader\">"
199             "<int name=\"status\">0</int><int name=\"QTime\">2</int>"
200             "<lst name=\"params\"><str name=\"facet\">true</str>"
201             "<str name=\"facet.mincount\">1</str><str name=\"start\">0</str>"
202             "<str name=\"q\">@attr 1=title solr</str>"
203             "<str name=\"f.date.facet.limit\">5</str>"
204             "<str name=\"facet.field\">date</str>"
205             "<str name=\"rows\">1</str></lst>"
206             "</lst><result name=\"response\" numFound=\"91000000000\" start=\"0\">"
207             "<doc><str name=\"author\">Alenius, Hans,</str>"
208             "<str name=\"author-date\">1937-</str>"
209             "<str name=\"author-title\"/>"
210             "<arr name=\"date\"><str>1969</str></arr>"
211             "<str name=\"id\">   73857731 </str>"
212             "<arr name=\"lccn\"><str>   73857731 </str></arr>"
213             "<arr name=\"medium\"><str>book</str></arr>"
214             "<arr name=\"medium_exact\"><str>book</str></arr>"
215             "<arr name=\"physical-accomp\"><str/></arr>"
216             "<arr name=\"physical-dimensions\"><str>20 cm.</str></arr>"
217             "<arr name=\"physical-extent\"><str>140, (1) p.</str></arr>"
218             "<arr name=\"physical-format\"><str>illus.</str></arr>"
219             "<arr name=\"physical-specified\"><str/></arr>"
220             "<arr name=\"physical-unitsize\"><str/></arr>"
221             "<arr name=\"physical-unittype\"><str/></arr>"
222             "<arr name=\"publication-date\"><str>1969.</str></arr>"
223             "<arr name=\"publication-name\"><str>Norstedt,</str></arr>"
224             "<arr name=\"publication-place\"><str>Stockholm,</str></arr>"
225             "<arr name=\"subject\"><str>Photography</str><str>Artistic</str></arr>"
226             "<arr name=\"subject-long\"><str>Photography, Artistic.</str></arr>"
227             "<arr name=\"subject_exact\"><str>Photography</str><str>Artistic</str></arr>"
228             "<arr name=\"system-control-nr\"><str>(OCoLC)36247690</str></arr>"
229             "<str name=\"title\">Solring.</str><str name=\"title-complete\">Solring.</str>"
230             "<str name=\"title-dates\"/><str name=\"title-medium\"/>"
231             "<str name=\"title-number-section\"/><str name=\"title-remainder\"/>"
232             "<str name=\"title-responsibility\"/><str name=\"title_exact\">Solring.</str>"
233             "</doc></result><lst name=\"facet_counts\">"
234             "<lst name=\"facet_queries\"/>"
235             "<lst name=\"facet_fields\">"
236             "<lst name=\"date\"><int name=\"1978\">5000000000</int><int name=\"1983\">4</int>"
237             "<int name=\"1987\">4</int><int name=\"1988\">4</int>"
238             "<int name=\"2003\">3</int></lst></lst><lst name=\"facet_dates\"/>"
239             "</lst></response>", &response));
240 #if HAVE_LONG_LONG
241     YAZ_CHECK(*response->numberOfRecords == 91000000000LL);
242 #endif
243     YAZ_CHECK_EQ(response->num_records, 1);
244     YAZ_CHECK(response->records);
245     if (response->records)
246     {
247         const char *doc =
248             "<doc><str name=\"author\">Alenius, Hans,</str>"
249             "<str name=\"author-date\">1937-</str>"
250             "<str name=\"author-title\"/>"
251             "<arr name=\"date\"><str>1969</str></arr>"
252             "<str name=\"id\">   73857731 </str>"
253             "<arr name=\"lccn\"><str>   73857731 </str></arr>"
254             "<arr name=\"medium\"><str>book</str></arr>"
255             "<arr name=\"medium_exact\"><str>book</str></arr>"
256             "<arr name=\"physical-accomp\"><str/></arr>"
257             "<arr name=\"physical-dimensions\"><str>20 cm.</str></arr>"
258             "<arr name=\"physical-extent\"><str>140, (1) p.</str></arr>"
259             "<arr name=\"physical-format\"><str>illus.</str></arr>"
260             "<arr name=\"physical-specified\"><str/></arr>"
261             "<arr name=\"physical-unitsize\"><str/></arr>"
262             "<arr name=\"physical-unittype\"><str/></arr>"
263             "<arr name=\"publication-date\"><str>1969.</str></arr>"
264             "<arr name=\"publication-name\"><str>Norstedt,</str></arr>"
265             "<arr name=\"publication-place\"><str>Stockholm,</str></arr>"
266             "<arr name=\"subject\"><str>Photography</str><str>Artistic</str></arr>"
267             "<arr name=\"subject-long\"><str>Photography, Artistic.</str></arr>"
268             "<arr name=\"subject_exact\"><str>Photography</str><str>Artistic</str></arr>"
269             "<arr name=\"system-control-nr\"><str>(OCoLC)36247690</str></arr>"
270             "<str name=\"title\">Solring.</str><str name=\"title-complete\">Solring.</str>"
271             "<str name=\"title-dates\"/><str name=\"title-medium\"/>"
272             "<str name=\"title-number-section\"/><str name=\"title-remainder\"/>"
273             "<str name=\"title-responsibility\"/><str name=\"title_exact\">Solring.</str>"
274             "</doc>";
275
276         Z_SRW_record *record = response->records;
277
278         YAZ_CHECK(record->recordData_len == strlen(doc) &&
279                   !memcmp(record->recordData_buf, doc, record->recordData_len));
280     }
281     YAZ_CHECK_EQ(response->num_diagnostics, 0);
282     YAZ_CHECK(response->diagnostics == 0);
283     YAZ_CHECK(response->nextRecordPosition == 0);
284
285     YAZ_CHECK(response->facetList);
286     if (response->facetList)
287     {
288         Z_FacetList *facetList = response->facetList;
289
290         YAZ_CHECK(facetList->num == 1);
291         if (facetList->num == 1)
292         {
293             Z_FacetField *facetField = facetList->elements[0];
294             int i;
295
296             YAZ_CHECK(facetField->num_terms == 5);
297             if (facetField->num_terms == 5)
298             {
299                 for (i = 0; i < facetField->num_terms; i++)
300                 {
301                     YAZ_CHECK(
302                         facetField->terms[i] &&
303                         facetField->terms[i]->term &&
304                         facetField->terms[i]->term->which == Z_Term_general);
305                 }
306 #if HAVE_LONG_LONG
307                 YAZ_CHECK(*facetField->terms[0]->count == 5000000000LL);
308 #endif
309                 YAZ_CHECK(facetField->terms[0]->term->u.general->len == 4
310                           && !memcmp(facetField->terms[0]->term->u.general->buf,
311                                      "1978", 4));
312                 YAZ_CHECK(*facetField->terms[1]->count == 4);
313                 YAZ_CHECK(facetField->terms[1]->term->u.general->len == 4
314                           && !memcmp(facetField->terms[1]->term->u.general->buf,
315                                      "1983", 4));
316                 YAZ_CHECK(*facetField->terms[2]->count == 4);
317                 YAZ_CHECK(facetField->terms[2]->term->u.general->len == 4
318                           && !memcmp(facetField->terms[2]->term->u.general->buf,
319                                      "1987", 4));
320                 YAZ_CHECK(*facetField->terms[3]->count == 4);
321                 YAZ_CHECK(facetField->terms[3]->term->u.general->len == 4
322                           && !memcmp(facetField->terms[3]->term->u.general->buf,
323                                      "1988", 4));
324                 YAZ_CHECK(*facetField->terms[4]->count == 3);
325                 YAZ_CHECK(facetField->terms[4]->term->u.general->len == 4
326                           && !memcmp(facetField->terms[4]->term->u.general->buf,
327                                      "2003", 4));
328             }
329         }
330     }
331
332     odr_reset(odr);
333
334     odr_destroy(odr);
335 #endif
336 }
337
338 void tst_yaz_700(void)
339 {
340     ODR odr = odr_createmem(ODR_ENCODE);
341     int r;
342     const char *url =
343         "http://localhost:9036/XXX/cproxydebug-7/node102/p/105/c=content_connector"
344         "a=usr/pw#&? r=cfusr/cfpw p=1.2.3.4:80/www.indexdata.com/staff/";
345     int use_full_host = 0;
346     Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, url, 0, use_full_host);
347     Z_HTTP_Request *hreq = gdu_req->u.HTTP_Request;
348     hreq->method = "GET";
349
350     hreq->content_buf = odr_strdup(odr, "");
351     hreq->content_len = 0;
352
353     r = z_GDU(odr, &gdu_req, 0, 0);
354     YAZ_CHECK(r);
355     if (r)
356     {
357         int len;
358         char *buf = odr_getbuf(odr, &len, 0);
359         ODR decode = odr_createmem(ODR_DECODE);
360         YAZ_CHECK(buf);
361         if (buf)
362         {
363             odr_setbuf(decode, buf, len, 0);
364             r = z_GDU(decode, &gdu_req, 0, 0);
365             YAZ_CHECK(r);
366         }
367         odr_destroy(decode);
368     }
369     odr_destroy(odr);
370 }
371
372
373 int main(int argc, char **argv)
374 {
375     YAZ_CHECK_INIT(argc, argv);
376 #if YAZ_HAVE_XML2
377     LIBXML_TEST_VERSION;
378 #endif
379     tst_encoding();
380     tst_decoding();
381     tst_yaz_700();
382     YAZ_CHECK_TERM;
383 }
384
385
386 /*
387  * Local variables:
388  * c-basic-offset: 4
389  * c-file-style: "Stroustrup"
390  * indent-tabs-mode: nil
391  * End:
392  * vim: shiftwidth=4 tabstop=8 expandtab
393  */
394