rpn2solr: support truncation left(2), left&right(3) YAZ-718
[yaz-moved-to-github.git] / test / test_retrieval.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 <yaz/retrieval.h>
10 #include <yaz/test.h>
11 #include <yaz/wrbuf.h>
12 #include <string.h>
13 #include <yaz/log.h>
14
15 #if YAZ_HAVE_XSLT
16
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
19
20 yaz_retrieval_t conv_configure(const char *xmlstring, WRBUF w)
21 {
22     xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
23     if (!doc)
24     {
25         wrbuf_printf(w, "xmlParseMemory");
26         return 0;
27     }
28     else
29     {
30         xmlNodePtr ptr = xmlDocGetRootElement(doc);
31         yaz_retrieval_t p = yaz_retrieval_create();
32
33         if (p)
34         {
35             const char *srcdir = getenv("srcdir");
36             if (srcdir)
37                 yaz_retrieval_set_path(p, srcdir);
38         }
39         if (!ptr)
40         {
41             wrbuf_printf(w, "xmlDocGetRootElement");
42             yaz_retrieval_destroy(p);
43             p = 0;
44         }
45         else if (!p)
46         {
47             wrbuf_printf(w, "yaz_retrieval_create");
48         }
49         else
50         {
51             int r = yaz_retrieval_configure(p, ptr);
52
53             if (r)
54             {
55                 wrbuf_puts(w, yaz_retrieval_get_error(p));
56                 yaz_retrieval_destroy(p);
57                 p = 0;
58             }
59         }
60         xmlFreeDoc(doc);
61         return p;
62     }
63 }
64
65 int conv_configure_test(const char *xmlstring, const char *expect_error,
66                         yaz_retrieval_t *pt)
67 {
68     WRBUF w = wrbuf_alloc();
69     int ret;
70
71     yaz_retrieval_t p = conv_configure(xmlstring, w);
72
73     if (!p)
74     {
75         if (expect_error && !strcmp(wrbuf_cstr(w), expect_error))
76             ret = 1;
77         else
78         {
79             ret = 0;
80             printf("%s\n", wrbuf_cstr(w));
81         }
82     }
83     else
84     {
85         if (expect_error)
86         {
87             ret = 0;
88             yaz_retrieval_destroy(p);
89         }
90         else
91         {
92             if (pt)
93                 *pt = p;
94             else
95                 yaz_retrieval_destroy(p);
96             ret = 1;
97         }
98     }
99     wrbuf_destroy(w);
100     return ret;
101 }
102
103 static void tst_configure(void)
104 {
105     YAZ_CHECK(conv_configure_test("<bad",
106                                   "xmlParseMemory", 0));
107
108     YAZ_CHECK(conv_configure_test("<bad/>",
109                                   "Expected element <retrievalinfo>", 0));
110
111     YAZ_CHECK(conv_configure_test("<retrievalinfo/>", 0, 0));
112
113     YAZ_CHECK(conv_configure_test("<retrievalinfo><bad/></retrievalinfo>",
114                                   "Element <retrievalinfo>:"
115                                   " expected element <retrieval>, got <bad>",
116                                   0));
117
118     YAZ_CHECK(conv_configure_test("<retrievalinfo><retrieval/>"
119                                   "</retrievalinfo>",
120                                   "Missing 'syntax' attribute", 0));
121
122
123     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
124                                   "<retrieval"
125                                   " unknown=\"unknown\""
126                                   ">"
127                                   "</retrieval>"
128                                   "</retrievalinfo>",
129                                   "Element <retrieval>:  expected attributes "
130                                   "'syntax', identifier' or 'name', got "
131                                   "'unknown'", 0));
132
133     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
134                                   "<retrieval"
135                                   " syntax=\"unknown_synt\""
136                                   ">"
137                                   "</retrieval>"
138                                   "</retrievalinfo>",
139                                   "Element <retrieval>:  unknown attribute "
140                                   "value syntax='unknown_synt'", 0));
141
142     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
143                                   "<retrieval"
144                                   " syntax=\"usmarc\""
145                                   "/>"
146                                   "</retrievalinfo>",
147                                   0, 0));
148
149     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
150                                   "<retrieval"
151                                   " syntax=\"usmarc\""
152                                   " name=\"marcxml\"/>"
153                                   "</retrievalinfo>",
154                                   0, 0));
155
156
157     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
158                                   "<retrieval"
159                                   " syntax=\"usmarc\""
160                                   " name=\"marcxml\""
161                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
162                                   "/>"
163                                   "</retrievalinfo>",
164                                   0, 0));
165
166
167
168     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
169                                   "<retrieval"
170                                   " syntax=\"usmarc\""
171                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
172                                   " name=\"marcxml\">"
173                                   "<convert/>"
174                                   "</retrieval>"
175                                   "</retrievalinfo>",
176                                   "Element <retrieval>: expected zero or one element "
177                                   "<backend>, got <convert>", 0));
178
179     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
180                                   "<retrieval"
181                                   " syntax=\"usmarc\""
182                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
183                                   " name=\"marcxml\">"
184                                   " <backend syntax=\"usmarc\""
185                                   " schema=\"marcxml\""
186                                   "/>"
187                                   "</retrieval>"
188                                   "</retrievalinfo>",
189                                   "Element <backend>: expected attributes 'syntax' or 'name,"
190                                   " got 'schema'", 0));
191
192     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
193                                   "<retrieval"
194                                   " syntax=\"usmarc\""
195                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
196                                   " name=\"marcxml\">"
197                                   " <backend syntax=\"usmarc\""
198                                   " name=\"marcxml\""
199                                   "/>"
200                                   "</retrieval>"
201                                   "</retrievalinfo>",
202                                   0, 0));
203
204     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
205                                   "<retrieval"
206                                   " syntax=\"usmarc\""
207                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
208                                   " name=\"marcxml\">"
209                                   " <backend syntax=\"unknown\""
210                                   "/>"
211                                   "</retrieval>"
212                                   "</retrievalinfo>",
213                                   "Element <backend syntax='unknown'>: "
214                                   "attribute 'syntax' has invalid value "
215                                   "'unknown'", 0));
216
217
218     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
219                                   "<retrieval"
220                                   " syntax=\"usmarc\""
221                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
222                                     " name=\"marcxml\">"
223                                   " <backend syntax=\"usmarc\""
224                                   " unknown=\"silly\""
225                                   "/>"
226                                   "</retrieval>"
227                                   "</retrievalinfo>",
228                                   "Element <backend>: expected attributes "
229                                   "'syntax' or 'name, got 'unknown'", 0));
230
231
232     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
233                                   "<retrieval syntax=\"usmarc\">"
234                                   "<backend syntax=\"xml\" name=\"dc\">"
235                                   "<xslt stylesheet=\"test_record_conv.xsl\"/>"
236                                   "<marc"
237                                   " inputcharset=\"utf-8\""
238                                   " outputcharset=\"non-existent\""
239                                   " inputformat=\"xml\""
240                                   " outputformat=\"marc\""
241                                   "/>"
242                                   "</backend>"
243                                   "</retrieval>"
244                                   "</retrievalinfo>",
245                                   "Element <marc inputcharset='utf-8'"
246                                   " outputcharset='non-existent'>: Unsupported character"
247                                   " set mapping defined by attribute values", 0));
248
249     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
250                                   "<retrieval syntax=\"usmarc\">"
251                                   "<backend syntax=\"xml\" name=\"dc\">"
252                                   "<xslt stylesheet=\"test_record_conv.xsl\"/>"
253                                   "<marc"
254                                   " inputcharset=\"utf-8\""
255                                   " outputcharset=\"marc-8\""
256                                   " inputformat=\"not-existent\""
257                                   " outputformat=\"marc\""
258                                   "/>"
259                                   "</backend>"
260                                   "</retrieval>"
261                                   "</retrievalinfo>",
262                                   "Element <marc inputformat='not-existent'>:  Unsupported"
263                                   " input format defined by attribute value", 0));
264
265
266     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
267                                   "<retrieval syntax=\"usmarc\">"
268                                   "<backend syntax=\"xml\" name=\"dc\">"
269                                   "<xslt stylesheet=\"test_record_conv.xsl\"/>"
270                                   "<marc"
271                                   " inputcharset=\"utf-8\""
272                                   " outputcharset=\"marc-8\""
273                                   " inputformat=\"xml\""
274                                   " outputformat=\"marc\""
275                                   "/>"
276                                   "</backend>"
277                                   "<backend/>"
278                                   "</retrieval>"
279                                   "</retrievalinfo>",
280                                   "Element <retrieval>: "
281                                   "only one <backend> allowed", 0));
282
283
284     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
285                                   "<retrieval syntax=\"usmarc\">"
286                                   "<backend syntax=\"xml\" name=\"dc\">"
287                                   "<xslt stylesheet=\"test_record_conv.xsl\"/>"
288                                   "<marc"
289                                   " inputcharset=\"utf-8\""
290                                   " outputcharset=\"marc-8\""
291                                   " inputformat=\"xml\""
292                                   " outputformat=\"marc\""
293                                   "/>"
294                                   "</backend>"
295                                   "</retrieval>"
296                                   "</retrievalinfo>",
297                                   0, 0));
298
299     YAZ_CHECK(conv_configure_test(
300                                   "<retrievalinfo "
301                                   " xmlns=\"http://indexdata.com/yaz\" version=\"1.0\">"
302                                   "<retrieval syntax=\"grs-1\"/>"
303                                   "<retrieval syntax=\"usmarc\" name=\"F\"/>"
304                                   "<retrieval syntax=\"usmarc\" name=\"B\"/>"
305                                   "<retrieval syntax=\"xml\" name=\"marcxml\" "
306                                   "           identifier=\"info:srw/schema/1/marcxml-v1.1\">"
307                                   "  <backend syntax=\"usmarc\" name=\"F\">"
308                                   "    <marc inputformat=\"marc\" outputformat=\"marcxml\" "
309                                   "            inputcharset=\"marc-8\"/>"
310                                   "  </backend>"
311                                   "</retrieval>"
312                                   "<retrieval syntax=\"xml\" name=\"danmarc\">"
313                                   "  <backend syntax=\"usmarc\" name=\"F\">"
314                                   "    <marc inputformat=\"marc\" outputformat=\"marcxchange\" "
315                                   "          inputcharset=\"marc-8\"/>"
316                                   "  </backend>"
317                                   "</retrieval>"
318                                   "<retrieval syntax=\"xml\" name=\"dc\" "
319                                   "           identifier=\"info:srw/schema/1/dc-v1.1\">"
320                                   "  <backend syntax=\"usmarc\" name=\"F\">"
321                                   "    <marc inputformat=\"marc\" outputformat=\"marcxml\" "
322                                   "          inputcharset=\"marc-8\"/>"
323                                   "    <xslt stylesheet=\"test_record_conv.xsl\"/> "
324                                   "  </backend>"
325                                   "</retrieval>"
326                                   "</retrievalinfo>",
327                                   0, 0));
328
329 }
330
331 #endif
332
333 int main(int argc, char **argv)
334 {
335     YAZ_CHECK_INIT(argc, argv);
336
337     yaz_log_xml_errors(0, 0 /* disable it */);
338
339 #if YAZ_HAVE_XSLT
340     tst_configure();
341 #endif
342     YAZ_CHECK_TERM;
343 }
344
345 /*
346  * Local variables:
347  * c-basic-offset: 4
348  * c-file-style: "Stroustrup"
349  * indent-tabs-mode: nil
350  * End:
351  * vim: shiftwidth=4 tabstop=8 expandtab
352  */
353