Fixes for retrieval handling in SRU mode.. The "virtual" record syntax
[yaz-moved-to-github.git] / test / tst_retrieval.c
1 /*
2  * Copyright (C) 2005-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tst_retrieval.c,v 1.5 2006-05-09 13:39:47 adam Exp $
6  *
7  */
8 #include <yaz/retrieval.h>
9 #include <yaz/test.h>
10 #include <yaz/wrbuf.h>
11 #include <string.h>
12 #include <yaz/log.h>
13 #include <yaz/libxml2_error.h>
14
15 #if HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18
19 #if HAVE_XSLT
20
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
23
24 yaz_retrieval_t conv_configure(const char *xmlstring, WRBUF w)
25 {
26     xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
27     if (!doc)
28     {
29         wrbuf_printf(w, "xmlParseMemory");
30         return 0;
31     }
32     else
33     {
34         xmlNodePtr ptr = xmlDocGetRootElement(doc);
35         yaz_retrieval_t p = yaz_retrieval_create();
36
37         if (p)
38         {
39             const char *srcdir = getenv("srcdir");
40             if (srcdir)
41                 yaz_retrieval_set_path(p, srcdir);
42         }
43         if (!ptr)
44         {
45             wrbuf_printf(w, "xmlDocGetRootElement");
46             yaz_retrieval_destroy(p);
47             p = 0;
48         }
49         else if (!p)
50         {
51             wrbuf_printf(w, "yaz_retrieval_create");
52         }
53         else
54         {
55             int r = yaz_retrieval_configure(p, ptr);
56             
57             if (r)
58             {
59                 wrbuf_puts(w, yaz_retrieval_get_error(p));
60                 yaz_retrieval_destroy(p);
61                 p = 0;
62             }
63         }
64         xmlFreeDoc(doc);
65         return p;
66     }    
67 }
68
69 int conv_configure_test(const char *xmlstring, const char *expect_error,
70                         yaz_retrieval_t *pt)
71 {
72     WRBUF w = wrbuf_alloc();
73     int ret;
74
75     yaz_retrieval_t p = conv_configure(xmlstring, w);
76
77     if (!p)
78     {
79         if (expect_error && !strcmp(wrbuf_buf(w), expect_error))
80             ret = 1;
81         else
82         {
83             ret = 0;
84             printf("%.*s\n", wrbuf_len(w), wrbuf_buf(w));
85         }
86     }
87     else
88     {
89         if (expect_error)
90         {
91             ret = 0;
92             yaz_retrieval_destroy(p);
93         }
94         else
95         {
96             if (pt)
97                 *pt = p;
98             else
99                 yaz_retrieval_destroy(p);
100             ret = 1;
101         }
102     }
103     wrbuf_free(w, 1);
104     return ret;
105 }
106
107 static void tst_configure()
108 {
109     YAZ_CHECK(conv_configure_test("<bad", 
110                                   "xmlParseMemory", 0));
111
112     YAZ_CHECK(conv_configure_test("<bad/>", 
113                                   "Missing 'retrievalinfo' element", 0));
114
115     YAZ_CHECK(conv_configure_test("<retrievalinfo/>", 0, 0));
116
117     YAZ_CHECK(conv_configure_test("<retrievalinfo><bad/></retrievalinfo>",
118                                   "Bad element 'bad'."
119                                   " Expected 'retrieval'", 0));
120
121     YAZ_CHECK(conv_configure_test("<retrievalinfo><retrieval/>"
122                                   "</retrievalinfo>", 
123                                   "Missing 'syntax' attribute", 0));
124
125     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
126                                   "<retrieval syntax=\"usmarc\">\n"
127                                   "  "
128                                   "<convert>"
129                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
130                                   "<marc"
131                                   " inputcharset=\"utf-8\""
132                                   " outputcharset=\"marc-8\""
133                                   " inputformat=\"xml\""
134                                   " outputformat=\"marc\""
135                                   "/>"
136                                   "</convert>"
137                                   "</retrieval>"
138                                   "</retrievalinfo>",
139                                   0, 0));
140
141     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
142                                   "<retrieval syntax=\"usmarc\">"
143                                   "<convert>"
144                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
145                                   "<marc"
146                                   " inputcharset=\"utf-8\""
147                                   " outputcharset=\"marc-8\""
148                                   " inputformat=\"xml\""
149                                   " outputformat=\"marc\""
150                                   "/>"
151                                   "</convert>"
152                                   "</retrieval>"
153                                   "<retrieval syntax=\"usmarc\">"
154                                   "<convert>"
155                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
156                                   "<marc"
157                                   " inputcharset=\"utf-8\""
158                                   " outputcharset=\"marc-8\""
159                                   " inputformat=\"xml\""
160                                   " outputformat=\"marc\""
161                                   "/>"
162                                   "</convert>"
163                                   "</retrieval>"
164                                   "</retrievalinfo>",
165                                   0, 0));
166
167
168     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
169                                   "<retrieval" 
170                                   " syntax=\"usmarc\""
171                                   " name=\"marcxml\"" 
172                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
173                                   ">"
174                                   "<convert/>"
175                                   "</retrieval>"
176                                   "</retrievalinfo>",
177                                   0, 0));
178
179     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
180                                   "<retrieval" 
181                                   " syntax=\"usmarc\""
182                                   " name=\"marcxml\""
183                                   " backendsyntax=\"usmarc\""
184                                   " backendname=\"marcxml\""
185                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
186                                   ">"
187                                   "<convert/>"
188                                   "</retrieval>"
189                                   "</retrievalinfo>",
190                                   0, 0));
191
192     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
193                                   "<retrieval" 
194                                   " syntax=\"usmarc\""
195                                   " name=\"marcxml\""
196                                   " backendsyntax=\"usmarc\""
197                                   " backendschema=\"marcxml\""
198                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
199                                   ">"
200                                   "<convert/>"
201                                   "</retrieval>"
202                                   "</retrievalinfo>",
203                                   "Bad attribute 'backendschema'."
204                                   " Use 'backendname' instead", 
205                                   0));
206
207
208     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
209                                   "<retrieval" 
210                                   " syntax=\"unknown_synt\""
211                                   ">"
212                                   "<convert/>"
213                                   "</retrieval>"
214                                   "</retrievalinfo>",
215                                   "Bad syntax 'unknown_synt'", 0));
216
217     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
218                                   "<retrieval" 
219                                   " backendsyntax=\"unknown_synt\""
220                                   ">"
221                                   "<convert/>"
222                                   "</retrieval>"
223                                   "</retrievalinfo>",
224                                   "Bad backendsyntax 'unknown_synt'", 0));
225
226 }
227
228 #endif
229
230 int main(int argc, char **argv)
231 {
232     YAZ_CHECK_INIT(argc, argv);
233
234     libxml2_error_to_yazlog(0 /* disable it */, "");
235
236 #if HAVE_XSLT
237     tst_configure();
238 #endif
239     YAZ_CHECK_TERM;
240 }
241
242 /*
243  * Local variables:
244  * c-basic-offset: 4
245  * indent-tabs-mode: nil
246  * End:
247  * vim: shiftwidth=4 tabstop=8 expandtab
248  */
249