1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
12 #include <libxml/parser.h>
15 #include <yaz/yaz-version.h>
16 #include <yaz/pquery.h>
19 int compare_solr_req(ODR odr, Z_SRW_PDU *sr,
20 const char *charset, const char *expect)
29 gdu = z_get_HTTP_Request_host_path(odr, "localhost", "Default");
34 yaz_solr_encode_request(gdu->u.HTTP_Request, sr, odr, charset);
36 r = z_GDU(odr, &gdu, 0, 0);
41 char *buf = odr_getbuf(odr, &len, 0);
45 if (len == strlen(expect) && !memcmp(buf, expect, len))
50 yaz_log(YLOG_WARN, "Expect:\n%s\n", expect);
51 yaz_log(YLOG_WARN, "Got:\n%.*s\n", len, buf);
59 void tst_encoding(void)
62 ODR odr = odr_createmem(ODR_ENCODE);
65 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
68 YAZ_CHECK(compare_solr_req(
70 "GET Default/select? HTTP/1.1\r\n"
71 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
73 "Content-Type: text/xml\r\n\r\n"));
77 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
79 YAZ_CHECK(compare_solr_req(
81 "GET Default/select? HTTP/1.1\r\n"
82 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
84 "Content-Type: text/xml; charset=utf-8\r\n\r\n"));
88 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
91 sr->u.request->query_type = Z_SRW_query_type_cql;
92 sr->u.request->query.cql = "title:solr";
93 sr->u.request->startRecord = odr_intdup(odr, 3);
94 sr->u.request->maximumRecords = odr_intdup(odr, 10);
96 YAZ_CHECK(compare_solr_req(
98 "GET Default/select?q=title%3Asolr&start=2&rows=10"
100 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
101 "Host: localhost\r\n"
102 "Content-Type: text/xml\r\n\r\n"));
106 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
109 sr->u.request->query_type = Z_SRW_query_type_cql;
110 sr->u.request->query.cql = "title:solr";
111 sr->u.request->startRecord = odr_intdup(odr, 3);
112 sr->u.request->maximumRecords = odr_intdup(odr, 10);
113 sr->u.request->facetList = yaz_pqf_parse_facet_list(
114 odr, "@attr 1=date @attr 2=0, @attr 1=title_exact @attr 3=17");
116 YAZ_CHECK(compare_solr_req(
118 "GET Default/select?q=title%3Asolr&start=2&rows=10"
119 "&facet=true&facet.mincount=1&facet.field=date"
120 "&facet.field=title_exact&f.title_exact.facet.limit=17"
122 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
123 "Host: localhost\r\n"
124 "Content-Type: text/xml\r\n\r\n"));
133 int check_response(ODR o, const char *content, Z_SRW_searchRetrieveResponse **p)
138 char *http_response = odr_malloc(o, strlen(content) + 300);
140 strcpy(http_response,
141 "HTTP/1.1 200 OK\r\n"
142 "Last-Modified: Wed, 13 Apr 2011 08:30:59 GMT\r\n"
143 "ETag: \"MjcyMWE5M2JiNDgwMDAwMFNvbHI=\"\r\n"
144 "Content-Type: text/xml; charset=utf-8\r\n");
145 sprintf(http_response + strlen(http_response),
146 "Content-Length: %d\r\n\r\n", (int) strlen(content));
147 strcat(http_response, content);
149 odr_setbuf(o, http_response, strlen(http_response), 0);
151 r = z_GDU(o, &gdu, 0, 0);
152 if (!r || gdu->which != Z_GDU_HTTP_Response)
154 r = yaz_solr_decode_response(o, gdu->u.HTTP_Response, &sr_p);
157 if (sr_p->which != Z_SRW_searchRetrieve_response)
159 *p = sr_p->u.response;
163 void tst_decoding(void)
166 ODR odr = odr_createmem(ODR_DECODE);
167 Z_SRW_searchRetrieveResponse *response;
169 YAZ_CHECK(check_response(
171 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
173 "<lst name=\"responseHeader\"><int name=\"status\">0</int>"
174 "<int name=\"QTime\">1</int><lst name=\"params\">"
175 "<str name=\"start\">0</str><str name=\"q\">@attr 1=title solr</str>"
176 "<str name=\"rows\">0</str></lst>"
177 "</lst><result name=\"response\" numFound=\"91\" start=\"0\"/>\n"
178 "</response>\n", &response));
179 YAZ_CHECK_EQ(*response->numberOfRecords, 91);
180 YAZ_CHECK_EQ(response->num_records, 0);
181 YAZ_CHECK(response->records == 0);
182 YAZ_CHECK_EQ(response->num_diagnostics, 0);
183 YAZ_CHECK(response->diagnostics == 0);
184 YAZ_CHECK(response->nextRecordPosition == 0);
185 YAZ_CHECK(response->facetList == 0);
192 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
193 "<response><lst name=\"responseHeader\">"
194 "<int name=\"status\">0</int><int name=\"QTime\">2</int>"
195 "<lst name=\"params\"><str name=\"facet\">true</str>"
196 "<str name=\"facet.mincount\">1</str><str name=\"start\">0</str>"
197 "<str name=\"q\">@attr 1=title solr</str>"
198 "<str name=\"f.date.facet.limit\">5</str>"
199 "<str name=\"facet.field\">date</str>"
200 "<str name=\"rows\">1</str></lst>"
201 "</lst><result name=\"response\" numFound=\"91000000000\" start=\"0\">"
202 "<doc><str name=\"author\">Alenius, Hans,</str>"
203 "<str name=\"author-date\">1937-</str>"
204 "<str name=\"author-title\"/>"
205 "<arr name=\"date\"><str>1969</str></arr>"
206 "<str name=\"id\"> 73857731 </str>"
207 "<arr name=\"lccn\"><str> 73857731 </str></arr>"
208 "<arr name=\"medium\"><str>book</str></arr>"
209 "<arr name=\"medium_exact\"><str>book</str></arr>"
210 "<arr name=\"physical-accomp\"><str/></arr>"
211 "<arr name=\"physical-dimensions\"><str>20 cm.</str></arr>"
212 "<arr name=\"physical-extent\"><str>140, (1) p.</str></arr>"
213 "<arr name=\"physical-format\"><str>illus.</str></arr>"
214 "<arr name=\"physical-specified\"><str/></arr>"
215 "<arr name=\"physical-unitsize\"><str/></arr>"
216 "<arr name=\"physical-unittype\"><str/></arr>"
217 "<arr name=\"publication-date\"><str>1969.</str></arr>"
218 "<arr name=\"publication-name\"><str>Norstedt,</str></arr>"
219 "<arr name=\"publication-place\"><str>Stockholm,</str></arr>"
220 "<arr name=\"subject\"><str>Photography</str><str>Artistic</str></arr>"
221 "<arr name=\"subject-long\"><str>Photography, Artistic.</str></arr>"
222 "<arr name=\"subject_exact\"><str>Photography</str><str>Artistic</str></arr>"
223 "<arr name=\"system-control-nr\"><str>(OCoLC)36247690</str></arr>"
224 "<str name=\"title\">Solring.</str><str name=\"title-complete\">Solring.</str>"
225 "<str name=\"title-dates\"/><str name=\"title-medium\"/>"
226 "<str name=\"title-number-section\"/><str name=\"title-remainder\"/>"
227 "<str name=\"title-responsibility\"/><str name=\"title_exact\">Solring.</str>"
228 "</doc></result><lst name=\"facet_counts\">"
229 "<lst name=\"facet_queries\"/>"
230 "<lst name=\"facet_fields\">"
231 "<lst name=\"date\"><int name=\"1978\">5000000000</int><int name=\"1983\">4</int>"
232 "<int name=\"1987\">4</int><int name=\"1988\">4</int>"
233 "<int name=\"2003\">3</int></lst></lst><lst name=\"facet_dates\"/>"
234 "</lst></response>", &response));
236 YAZ_CHECK(*response->numberOfRecords == 91000000000LL);
238 YAZ_CHECK_EQ(response->num_records, 1);
239 YAZ_CHECK(response->records);
240 if (response->records)
243 "<doc><str name=\"author\">Alenius, Hans,</str>"
244 "<str name=\"author-date\">1937-</str>"
245 "<str name=\"author-title\"/>"
246 "<arr name=\"date\"><str>1969</str></arr>"
247 "<str name=\"id\"> 73857731 </str>"
248 "<arr name=\"lccn\"><str> 73857731 </str></arr>"
249 "<arr name=\"medium\"><str>book</str></arr>"
250 "<arr name=\"medium_exact\"><str>book</str></arr>"
251 "<arr name=\"physical-accomp\"><str/></arr>"
252 "<arr name=\"physical-dimensions\"><str>20 cm.</str></arr>"
253 "<arr name=\"physical-extent\"><str>140, (1) p.</str></arr>"
254 "<arr name=\"physical-format\"><str>illus.</str></arr>"
255 "<arr name=\"physical-specified\"><str/></arr>"
256 "<arr name=\"physical-unitsize\"><str/></arr>"
257 "<arr name=\"physical-unittype\"><str/></arr>"
258 "<arr name=\"publication-date\"><str>1969.</str></arr>"
259 "<arr name=\"publication-name\"><str>Norstedt,</str></arr>"
260 "<arr name=\"publication-place\"><str>Stockholm,</str></arr>"
261 "<arr name=\"subject\"><str>Photography</str><str>Artistic</str></arr>"
262 "<arr name=\"subject-long\"><str>Photography, Artistic.</str></arr>"
263 "<arr name=\"subject_exact\"><str>Photography</str><str>Artistic</str></arr>"
264 "<arr name=\"system-control-nr\"><str>(OCoLC)36247690</str></arr>"
265 "<str name=\"title\">Solring.</str><str name=\"title-complete\">Solring.</str>"
266 "<str name=\"title-dates\"/><str name=\"title-medium\"/>"
267 "<str name=\"title-number-section\"/><str name=\"title-remainder\"/>"
268 "<str name=\"title-responsibility\"/><str name=\"title_exact\">Solring.</str>"
271 Z_SRW_record *record = response->records;
273 YAZ_CHECK(record->recordData_len == strlen(doc) &&
274 !memcmp(record->recordData_buf, doc, record->recordData_len));
276 YAZ_CHECK_EQ(response->num_diagnostics, 0);
277 YAZ_CHECK(response->diagnostics == 0);
278 YAZ_CHECK(response->nextRecordPosition == 0);
280 YAZ_CHECK(response->facetList);
281 if (response->facetList)
283 Z_FacetList *facetList = response->facetList;
285 YAZ_CHECK(facetList->num == 1);
286 if (facetList->num == 1)
288 Z_FacetField *facetField = facetList->elements[0];
291 YAZ_CHECK(facetField->num_terms == 5);
292 if (facetField->num_terms == 5)
294 for (i = 0; i < facetField->num_terms; i++)
297 facetField->terms[i] &&
298 facetField->terms[i]->term &&
299 facetField->terms[i]->term->which == Z_Term_general);
302 YAZ_CHECK(*facetField->terms[0]->count == 5000000000LL);
304 YAZ_CHECK(facetField->terms[0]->term->u.general->len == 4
305 && !memcmp(facetField->terms[0]->term->u.general->buf,
307 YAZ_CHECK(*facetField->terms[1]->count == 4);
308 YAZ_CHECK(facetField->terms[1]->term->u.general->len == 4
309 && !memcmp(facetField->terms[1]->term->u.general->buf,
311 YAZ_CHECK(*facetField->terms[2]->count == 4);
312 YAZ_CHECK(facetField->terms[2]->term->u.general->len == 4
313 && !memcmp(facetField->terms[2]->term->u.general->buf,
315 YAZ_CHECK(*facetField->terms[3]->count == 4);
316 YAZ_CHECK(facetField->terms[3]->term->u.general->len == 4
317 && !memcmp(facetField->terms[3]->term->u.general->buf,
319 YAZ_CHECK(*facetField->terms[4]->count == 3);
320 YAZ_CHECK(facetField->terms[4]->term->u.general->len == 4
321 && !memcmp(facetField->terms[4]->term->u.general->buf,
332 int main(int argc, char **argv)
334 YAZ_CHECK_INIT(argc, argv);
347 * c-file-style: "Stroustrup"
348 * indent-tabs-mode: nil
350 * vim: shiftwidth=4 tabstop=8 expandtab