23087577888c049c9e1d4a6c51d1d3ddbd5fd3cb
[yaz-moved-to-github.git] / test / test_solr.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 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 #if YAZ_HAVE_XML2
12 #include <libxml/parser.h>
13 #endif
14 #include <yaz/test.h>
15 #include <yaz/yaz-version.h>
16 #include <yaz/pquery.h>
17
18 #if YAZ_HAVE_XML2
19 int compare_solr_req(ODR odr, Z_SRW_PDU *sr,
20                      const char *charset, const char *expect)
21 {
22     int r;
23     Z_GDU *gdu = 0;
24     YAZ_CHECK(sr);
25
26     if (!sr)
27         return 0;
28
29     gdu = z_get_HTTP_Request_host_path(odr, "localhost", "Default");
30     YAZ_CHECK(gdu);
31     if (!gdu)
32         return 0;
33
34     yaz_solr_encode_request(gdu->u.HTTP_Request, sr, odr, charset);
35
36     r = z_GDU(odr, &gdu, 0, 0);
37     YAZ_CHECK(r);
38     if (r)
39     {
40         int len = 0;
41         char *buf = odr_getbuf(odr, &len, 0);
42         
43         if (buf)
44         {
45             if (len == strlen(expect) && !memcmp(buf, expect, len))
46             {
47                 odr_reset(odr);
48                 return 1;
49             }
50             yaz_log(YLOG_WARN, "Expect:\n%s\n", expect);
51             yaz_log(YLOG_WARN, "Got:\n%.*s\n", len, buf);
52          }
53     }
54     odr_reset(odr);
55     return 0;
56 }
57 #endif
58
59 void tst(void)
60 {
61 #if YAZ_HAVE_XML2
62     ODR odr = odr_createmem(ODR_ENCODE);
63
64     {
65         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
66                                         "1.2");
67         
68         YAZ_CHECK(compare_solr_req(
69                       odr, sr, 0,
70                       "GET Default/select? HTTP/1.1\r\n"
71                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
72                       "Host: localhost\r\n"
73                       "Content-Type: text/xml\r\n\r\n"));
74     }
75
76     {
77         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
78                                         "1.2");
79         YAZ_CHECK(compare_solr_req(
80                       odr, sr, "utf-8",
81                       "GET Default/select? HTTP/1.1\r\n"
82                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
83                       "Host: localhost\r\n"
84                       "Content-Type: text/xml; charset=utf-8\r\n\r\n"));
85     }
86
87     {
88         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
89                                         "1.2");
90         
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);
95
96         YAZ_CHECK(compare_solr_req(
97                       odr, sr, 0,
98                       "GET Default/select?q=title%3Asolr&start=2&rows=10"
99                       " HTTP/1.1\r\n"
100                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
101                       "Host: localhost\r\n"
102                       "Content-Type: text/xml\r\n\r\n"));
103     }
104
105     {
106         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
107                                         "1.2");
108         
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");
115
116         YAZ_CHECK(compare_solr_req(
117                       odr, sr, 0,
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"
121                       " HTTP/1.1\r\n"
122                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
123                       "Host: localhost\r\n"
124                       "Content-Type: text/xml\r\n\r\n"));
125     }
126
127     odr_destroy(odr);
128 /* YAZ_HAVE_XML2 */
129 #endif
130 }
131
132 int main(int argc, char **argv)
133 {
134     YAZ_CHECK_INIT(argc, argv);
135 #if YAZ_HAVE_XML2
136     LIBXML_TEST_VERSION;
137 #endif
138     tst();
139     YAZ_CHECK_TERM;
140 }
141
142
143 /*
144  * Local variables:
145  * c-basic-offset: 4
146  * c-file-style: "Stroustrup"
147  * indent-tabs-mode: nil
148  * End:
149  * vim: shiftwidth=4 tabstop=8 expandtab
150  */
151