Added retrieval handling support in Generic Frontend Server to support
[yaz-moved-to-github.git] / test / tst_record_conv.c
1 /*
2  * Copyright (C) 2005-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tst_record_conv.c,v 1.6 2006-05-07 14:48:25 adam Exp $
6  *
7  */
8 #include <yaz/record_conv.h>
9 #include <yaz/test.h>
10 #include <yaz/wrbuf.h>
11 #include <string.h>
12
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #if HAVE_XSLT
18
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
21
22 yaz_record_conv_t conv_configure(const char *xmlstring, WRBUF w)
23 {
24     xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
25     if (!doc)
26     {
27         wrbuf_printf(w, "xmlParseMemory");
28         return 0;
29     }
30     else
31     {
32         xmlNodePtr ptr = xmlDocGetRootElement(doc);
33         yaz_record_conv_t p = yaz_record_conv_create();
34
35         if (p)
36         {
37             const char *srcdir = getenv("srcdir");
38             if (srcdir)
39                 yaz_record_conv_set_path(p, srcdir);
40         }
41         if (!ptr)
42         {
43             wrbuf_printf(w, "xmlDocGetRootElement");
44             yaz_record_conv_destroy(p);
45             p = 0;
46         }
47         else if (!p)
48         {
49             wrbuf_printf(w, "yaz_record_conv_create");
50         }
51         else
52         {
53             int r = yaz_record_conv_configure(p, ptr);
54             
55             if (r)
56             {
57                 wrbuf_puts(w, yaz_record_conv_get_error(p));
58                 yaz_record_conv_destroy(p);
59                 p = 0;
60             }
61         }
62         xmlFreeDoc(doc);
63         return p;
64     }    
65 }
66
67 int conv_configure_test(const char *xmlstring, const char *expect_error,
68                         yaz_record_conv_t *pt)
69 {
70     WRBUF w = wrbuf_alloc();
71     int ret;
72
73     yaz_record_conv_t p = conv_configure(xmlstring, w);
74
75     if (!p)
76     {
77         if (expect_error && !strcmp(wrbuf_buf(w), expect_error))
78             ret = 1;
79         else
80         {
81             ret = 0;
82             printf("%.*s\n", wrbuf_len(w), wrbuf_buf(w));
83         }
84     }
85     else
86     {
87         if (expect_error)
88         {
89             ret = 0;
90             yaz_record_conv_destroy(p);
91         }
92         else
93         {
94             ret = 1;
95         }
96     }
97     if (pt)
98         *pt = p;
99     else
100         yaz_record_conv_destroy(p);
101
102     wrbuf_free(w, 1);
103     return ret;
104 }
105
106 static void tst_configure()
107 {
108     YAZ_CHECK(conv_configure_test("<bad", "xmlParseMemory", 0));
109     YAZ_CHECK(conv_configure_test("<bad/>", "Missing 'convert' element", 0));
110     YAZ_CHECK(conv_configure_test("<convert/>", 0, 0));
111     YAZ_CHECK(conv_configure_test("<convert><bad/></convert>",
112                                   "Bad element 'bad'."
113                                   "Expected marc, xslt, ..", 0));
114     YAZ_CHECK(conv_configure_test("<convert>"
115                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
116                                   "<marc"
117                                   " inputcharset=\"marc-8\""
118                                   " outputcharset=\"marc-8\""
119                                   "/>"
120                                   "</convert>",
121                                   "Attribute 'inputformat' required", 0));
122     YAZ_CHECK(conv_configure_test("<convert>"
123                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
124                                   "<marc"
125                                   " inputcharset=\"utf-8\""
126                                   " outputcharset=\"marc-8\""
127                                   " inputformat=\"xml\""
128                                   " outputformat=\"marc\""
129                                   "/>"
130                                   "</convert>",
131                                   0, 0));
132 }
133
134 static int conv_convert_test(yaz_record_conv_t p,
135                              const char *input_record,
136                              const char *output_expect_record)
137 {
138     int ret = 0;
139     if (!p)
140     {
141         YAZ_CHECK(ret);
142     }
143     else
144     {
145         WRBUF output_record = wrbuf_alloc();
146         int r = yaz_record_conv_record(p, input_record, strlen(input_record),
147                                        output_record);
148         if (r)
149         {
150             if (output_expect_record)
151             {
152                 printf("yaz_record_conv error=%s\n",
153                        yaz_record_conv_get_error(p));
154                 ret = 0;
155             }
156             else
157                 ret = 1;
158         }
159         else
160         {
161             if (!output_expect_record)
162             {
163                 ret = 0;
164             }
165             else if (strlen(output_expect_record) != wrbuf_len(output_record))
166             {
167                 int expect_len = strlen(output_expect_record);
168                 ret = 0;
169                 printf("output_record expect-len=%d got-len=%d\n", expect_len,
170                        wrbuf_len(output_record));
171                 printf("got-output_record = %.*s\n",
172                        wrbuf_len(output_record), wrbuf_buf(output_record));
173                 printf("output_expect_record = %s\n",
174                        output_expect_record);
175             }
176             else if (memcmp(output_expect_record, wrbuf_buf(output_record),
177                             strlen(output_expect_record)))
178             {
179                 ret = 0;
180                 printf("got-output_record = %.*s\n",
181                        wrbuf_len(output_record), wrbuf_buf(output_record));
182                 printf("output_expect_record = %s\n",
183                        output_expect_record);
184             }
185             else
186             {
187                 ret = 1;
188             }
189         }
190         wrbuf_free(output_record, 1);
191     }
192     return ret;
193 }
194
195 static void tst_convert()
196 {
197     yaz_record_conv_t p = 0;
198     const char *marcxml_rec =
199         "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
200         "  <leader>00080nam a22000498a 4500</leader>\n"
201         "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
202         "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
203         "    <subfield code=\"a\">   11224466 </subfield>\n"
204         "  </datafield>\n"
205         "</record>\n";
206     const char *iso2709_rec =
207         "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
208         "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
209         "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
210         "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
211         "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
212
213     YAZ_CHECK(conv_configure_test("<convert>"
214                                   "<marc"
215                                   " inputcharset=\"utf-8\""
216                                   " outputcharset=\"marc-8\""
217                                   " inputformat=\"xml\""
218                                   " outputformat=\"marc\""
219                                   "/>"
220                                   "</convert>",
221                                   0, &p));
222     YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
223     yaz_record_conv_destroy(p);
224
225     YAZ_CHECK(conv_configure_test("<convert>"
226                                   "<marc"
227                                   " outputcharset=\"utf-8\""
228                                   " inputcharset=\"marc-8\""
229                                   " outputformat=\"marcxml\""
230                                   " inputformat=\"marc\""
231                                   "/>"
232                                   "</convert>",
233                                   0, &p));
234     YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
235     yaz_record_conv_destroy(p);
236
237
238     YAZ_CHECK(conv_configure_test("<convert>"
239                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
240                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
241                                   "<marc"
242                                   " inputcharset=\"utf-8\""
243                                   " outputcharset=\"marc-8\""
244                                   " inputformat=\"xml\""
245                                   " outputformat=\"marc\""
246                                   "/>"
247                                   "<marc"
248                                   " outputcharset=\"utf-8\""
249                                   " inputcharset=\"marc-8\""
250                                   " outputformat=\"marcxml\""
251                                   " inputformat=\"marc\""
252                                   "/>"
253                                   "</convert>",
254                                   0, &p));
255     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
256     yaz_record_conv_destroy(p);
257
258
259     YAZ_CHECK(conv_configure_test("<convert>"
260                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
261                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
262                                   "<marc"
263                                   " outputcharset=\"marc-8\""
264                                   " inputformat=\"xml\""
265                                   " outputformat=\"marc\""
266                                   "/>"
267                                   "<marc"
268                                   " inputcharset=\"marc-8\""
269                                   " outputformat=\"marcxml\""
270                                   " inputformat=\"marc\""
271                                   "/>"
272                                   "</convert>",
273                                   0, &p));
274     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
275     yaz_record_conv_destroy(p);
276 }
277
278 #endif
279
280 int main(int argc, char **argv)
281 {
282     YAZ_CHECK_INIT(argc, argv);
283 #if HAVE_XSLT
284     tst_configure();
285     tst_convert();
286 #endif
287     YAZ_CHECK_TERM;
288 }
289
290 /*
291  * Local variables:
292  * c-basic-offset: 4
293  * indent-tabs-mode: nil
294  * End:
295  * vim: shiftwidth=4 tabstop=8 expandtab
296  */
297