Added new user api unit test for the new fields.
[libgcal.git] / utests / utest_xpath.c
blob5a49e2bbb2104592292f370a4c5c21571a84c308
1 #define _GNU_SOURCE
2 /*
3 * @file utest_gcal.h
4 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
5 * @date Thu Mar 27 2008
7 * @brief Header module for xpath utests.
9 */
11 #include "utest_xpath.h"
12 #include "atom_parser.h"
13 #include "xml_aux.h"
14 #include "gcal.h"
15 #include "internal_gcal.h"
16 #include <string.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include "utils.h"
25 char *xml_data = NULL;
27 static void setup(void)
29 if (find_load_file("/utests/4entries_location.xml", &xml_data))
30 exit(1);
33 static void teardown(void)
35 /* and here we clean up */
36 if (xml_data)
37 free(xml_data);
40 START_TEST (test_normalize_url)
43 char *copy;
44 const char * const added = "http://www.google.com/calendar/feeds/"
45 "default/private/full/ujq52gb0lggdjb0qqi10nt07m8";
47 char retrieved[] = "http://www.google.com/calendar/feeds/"
48 "gcalntester%40gmail.com/private/full/ujq52gb0lggdjb0qqi10nt07m8";
49 workaround_edit_url(retrieved);
50 fail_if(strcmp(added, retrieved) != 0, "String is not normalized!");
52 copy = strdup(added);
53 workaround_edit_url(copy);
54 fail_if(strcmp(copy, added) != 0, "String should be equal!");
55 free(copy);
58 END_TEST
60 START_TEST (test_entry_list)
63 int num_entries, res;
64 xmlDoc *doc = NULL;
66 num_entries = atom_entries(doc);
67 fail_if(num_entries != -1, "Function tried to proceed with NULL doc!");
69 res = build_doc_tree(&doc, xml_data);
70 fail_if(res == -1, "failed to build document tree!");
72 num_entries = atom_entries(doc);
73 fail_if(num_entries != 4, "failed get correct number of entries: "
74 "4 != %d\n", num_entries);
76 clean_doc_tree(&doc);
78 END_TEST
82 START_TEST (test_get_entries)
84 xmlXPathObject *xpath_obj = NULL;
85 xmlDoc *doc = NULL;
86 xmlNodeSet *nodes;
87 struct gcal_event known_value;
88 struct gcal_event extracted;
89 int res;
91 gcal_init_event(&known_value);
92 gcal_init_event(&extracted);
94 res = build_doc_tree(&doc, xml_data);
95 fail_if(res == -1, "failed to build document tree!");
97 xpath_obj = atom_get_entries(doc);
98 fail_if(xpath_obj == NULL, "failed to get entry node list!");
100 nodes = xpath_obj->nodesetval;
101 fail_if(nodes->nodeNr != 4, "should return 4 entries!");
103 res = atom_extract_data(nodes->nodeTab[0], &extracted);
104 fail_if(res == -1, "failed to extract data from node!");
106 known_value.common.title = "an event with location";
107 known_value.common.id = "http://www.google.com/calendar/feeds/gcal4tester%40gmail.com/private/full/saq81ktu4iqv7r20b8ctv70q7s";
108 known_value.common.edit_uri = "http://www.google.com/calendar/feeds/gcal4tester%40gmail.com/private/full/saq81ktu4iqv7r20b8ctv70q7s/63342246051";
109 known_value.content = "I should be there";
110 /* The event is not recurrent: for empty fields, I use a empty string */
111 known_value.dt_recurrent = "";
112 known_value.dt_start = "2008-03-26T18:00:00.000-05:00";
113 known_value.dt_end = "2008-03-26T19:00:00.000-05:00";
114 known_value.where = "my house";
115 known_value.status = "http://schemas.google.com/g/2005#event.confirmed";
116 known_value.common.updated = "2008-03-26T20:20:51.000Z";
118 fail_if(strcmp(known_value.common.title, extracted.common.title),
119 "failed field extraction");
120 fail_if(strcmp(known_value.common.id, extracted.common.id),
121 "failed field extraction");
122 fail_if(strcmp(known_value.common.edit_uri, extracted.common.edit_uri),
123 "failed field extraction");
124 fail_if(strcmp(known_value.content, extracted.content),
125 "failed field extraction");
126 fail_if(strcmp(known_value.dt_recurrent, extracted.dt_recurrent),
127 "failed field extraction");
128 fail_if(strcmp(known_value.dt_start, extracted.dt_start),
129 "failed field extraction");
130 fail_if(strcmp(known_value.dt_end, extracted.dt_end),
131 "failed field extraction");
132 fail_if(strcmp(known_value.where, extracted.where),
133 "failed field extraction");
134 fail_if(strcmp(known_value.status, extracted.status),
135 "failed field extraction");
136 fail_if(strcmp(known_value.common.updated, extracted.common.updated),
137 "failed field extraction");
139 if (xpath_obj)
140 xmlXPathFreeObject(xpath_obj);
142 gcal_destroy_entry(&extracted);
143 clean_doc_tree(&doc);
145 END_TEST
147 START_TEST (test_get_recurrence)
149 xmlXPathObject *xpath_obj = NULL;
150 xmlDoc *doc = NULL;
151 xmlNodeSet *nodes;
152 char recurrence_str[] = "DTSTART;TZID=America/Manaus:20080618T143000";
153 struct gcal_event extracted;
154 char *file_contents = NULL;
155 int res;
157 gcal_init_event(&extracted);
159 if (find_load_file("/utests/3entries_recurrence.xml", &file_contents))
160 fail_if(1, "Cannot load test XML file!");
162 res = build_doc_tree(&doc, file_contents);
163 fail_if(res == -1, "failed to build document tree!");
165 xpath_obj = atom_get_entries(doc);
166 fail_if(xpath_obj == NULL, "failed to get entry node list!");
168 nodes = xpath_obj->nodesetval;
169 res = atom_extract_data(nodes->nodeTab[0], &extracted);
170 fail_if(res == -1, "failed to extract data from node!");
172 fail_if(!strstr(extracted.dt_recurrent, recurrence_str),
173 "failed recurrence field extraction!");
175 free(file_contents);
176 if (xpath_obj)
177 xmlXPathFreeObject(xpath_obj);
179 gcal_destroy_entry(&extracted);
180 clean_doc_tree(&doc);
183 END_TEST
185 START_TEST (test_get_event_deleted)
187 xmlXPathObject *xpath_obj = NULL;
188 xmlDoc *doc = NULL;
189 xmlNodeSet *nodes;
190 struct gcal_event extracted;
191 char *file_contents = NULL;
192 int res;
194 gcal_init_event(&extracted);
196 if (find_load_file("/utests/up_deleted_event.xml", &file_contents))
197 fail_if(1, "Cannot load test XML file!");
199 res = build_doc_tree(&doc, file_contents);
200 fail_if(res == -1, "failed to build document tree!");
202 xpath_obj = atom_get_entries(doc);
203 fail_if(xpath_obj == NULL, "failed to get entry node list!");
205 nodes = xpath_obj->nodesetval;
206 res = atom_extract_data(nodes->nodeTab[0], &extracted);
207 fail_if(res == -1, "failed to extract data from node!");
209 fail_if(extracted.common.deleted != 1,
210 "failed parsing deleted event field!");
212 free(file_contents);
213 if (xpath_obj)
214 xmlXPathFreeObject(xpath_obj);
216 gcal_destroy_entry(&extracted);
217 clean_doc_tree(&doc);
220 END_TEST
222 START_TEST (test_get_contact_deleted)
224 xmlXPathObject *xpath_obj = NULL;
225 xmlDoc *doc = NULL;
226 xmlNodeSet *nodes;
227 struct gcal_contact extracted;
228 char *file_contents = NULL;
229 int res;
231 gcal_init_contact(&extracted);
233 if (find_load_file("/utests/up_new_delete_contact.xml", &file_contents))
234 fail_if(1, "Cannot load test XML file!");
236 res = build_doc_tree(&doc, file_contents);
237 fail_if(res == -1, "failed to build document tree!");
239 xpath_obj = atom_get_entries(doc);
240 fail_if(xpath_obj == NULL, "failed to get entry node list!");
241 nodes = xpath_obj->nodesetval;
242 res = atom_extract_contact(nodes->nodeTab[0], &extracted);
243 fail_if(res == -1, "failed to extract data from node!");
245 fail_if(extracted.common.deleted != 1,
246 "failed parsing deleted contact field!");
248 free(file_contents);
249 if (xpath_obj)
250 xmlXPathFreeObject(xpath_obj);
252 gcal_destroy_contact(&extracted);
253 clean_doc_tree(&doc);
256 END_TEST
258 START_TEST (test_get_contact_nophoto)
260 xmlXPathObject *xpath_obj = NULL;
261 xmlDoc *doc = NULL;
262 xmlNodeSet *nodes;
263 struct gcal_contact extracted;
264 char *file_contents = NULL;
265 int res;
267 gcal_init_contact(&extracted);
269 if (find_load_file("/utests/empty_photo.xml", &file_contents))
270 fail_if(1, "Cannot load test XML file!");
272 res = build_doc_tree(&doc, file_contents);
273 fail_if(res == -1, "failed to build document tree!");
275 xpath_obj = atom_get_entries(doc);
276 fail_if(xpath_obj == NULL, "failed to get entry node list!");
278 nodes = xpath_obj->nodesetval;
279 res = atom_extract_contact(nodes->nodeTab[0], &extracted);
280 fail_if(res == -1, "failed to extract data from node!");
282 fail_if(extracted.photo_length != 0,
283 "this contact was supposed to have no photo!");
285 fail_if(strcmp(extracted.photo, "http://www.google.com/m8/feeds/photos"
286 "/media/gcalntester%40gmail.com/b4d61ee8bdbf314") != 0,
287 "wrong photo url!");
289 free(file_contents);
290 if (xpath_obj)
291 xmlXPathFreeObject(xpath_obj);
293 gcal_destroy_contact(&extracted);
294 clean_doc_tree(&doc);
296 END_TEST
298 START_TEST (test_get_contact_photo)
300 xmlXPathObject *xpath_obj = NULL;
301 xmlDoc *doc = NULL;
302 xmlNodeSet *nodes;
303 struct gcal_contact extracted;
304 char *file_contents = NULL;
305 int res;
307 gcal_init_contact(&extracted);
309 if (find_load_file("/utests/with_photo.xml", &file_contents))
310 fail_if(1, "Cannot load test XML file!");
312 res = build_doc_tree(&doc, file_contents);
313 fail_if(res == -1, "failed to build document tree!");
315 xpath_obj = atom_get_entries(doc);
316 fail_if(xpath_obj == NULL, "failed to get entry node list!");
318 nodes = xpath_obj->nodesetval;
319 res = atom_extract_contact(nodes->nodeTab[0], &extracted);
320 fail_if(res == -1, "failed to extract data from node!");
322 fail_if(extracted.photo_length != 1,
323 "this contact was supposed to have a photo!");
325 fail_if(strcmp(extracted.photo, "http://www.google.com/m8/feeds/photos"
326 "/media/gcalntester%40gmail.com/1bd255c2889042a7") != 0,
327 "wrong photo url!");
329 free(file_contents);
330 if (xpath_obj)
331 xmlXPathFreeObject(xpath_obj);
333 gcal_destroy_contact(&extracted);
334 clean_doc_tree(&doc);
336 END_TEST
339 TCase *xpath_tcase_create(void)
341 TCase *tc = NULL;
342 tc = tcase_create("xpath");
343 tcase_add_checked_fixture(tc, setup, teardown);
344 tcase_add_test(tc, test_entry_list);
345 tcase_add_test(tc, test_get_entries);
346 tcase_add_test(tc, test_get_recurrence);
347 tcase_add_test(tc, test_get_event_deleted);
348 tcase_add_test(tc, test_get_contact_deleted);
349 tcase_add_test(tc, test_get_contact_nophoto);
350 tcase_add_test(tc, test_get_contact_photo);
351 tcase_add_test(tc, test_normalize_url);
352 return tc;