SOLR tests: start work on decode tests
[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_encoding(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
133 int check_response(ODR o, const char *content, Z_SRW_searchRetrieveResponse **p)
134 {
135     int r;
136     Z_GDU *gdu;
137     Z_SRW_PDU *sr_p;
138     char *http_response = odr_malloc(o, strlen(content) + 300);
139
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);
148
149     odr_setbuf(o, http_response, strlen(http_response), 0);
150
151     r = z_GDU(o, &gdu, 0, 0);
152     if (!r || gdu->which != Z_GDU_HTTP_Response)
153         return 0;
154     r = yaz_solr_decode_response(o, gdu->u.HTTP_Response, &sr_p);
155     if (r)
156         return 0;
157     if (sr_p->which != Z_SRW_searchRetrieve_response)
158         return 0;
159     *p = sr_p->u.response;
160     return 1;
161 }
162
163 void tst_decoding(void)
164 {
165 #if YAZ_HAVE_XML2
166     ODR odr = odr_createmem(ODR_DECODE);
167
168     Z_SRW_searchRetrieveResponse *response;
169     YAZ_CHECK(check_response(
170                   odr, 
171                   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
172                   "<response>\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
186     odr_reset(odr);
187     odr_destroy(odr);
188 #endif
189 }
190 int main(int argc, char **argv)
191 {
192     YAZ_CHECK_INIT(argc, argv);
193 #if YAZ_HAVE_XML2
194     LIBXML_TEST_VERSION;
195 #endif
196     tst_encoding();
197     tst_decoding();
198     YAZ_CHECK_TERM;
199 }
200
201
202 /*
203  * Local variables:
204  * c-basic-offset: 4
205  * c-file-style: "Stroustrup"
206  * indent-tabs-mode: nil
207  * End:
208  * vim: shiftwidth=4 tabstop=8 expandtab
209  */
210