Using GData 3.0 gd:structuredPostalAddress/gd:formattedAddress instead of gd:postalAd...
[libgcal.git] / utests / utest_xmlmode.c
blob0bd7695cb22b2cfe850d26af5aef8c2a8f3141bc
1 /*
2 * @file utest_xmlmode.h
3 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
4 * @date Wed Jul 23 14:03:31 2008
6 * @brief Test module for XML operating mode functions for libgcal.
9 */
11 #include "utest_xmlmode.h"
12 #include "gcalendar.h"
13 #include "gcontact.h"
14 #include <stdio.h>
15 #include <string.h>
16 #include "utils.h"
17 #include "gcal_status.h"
18 #include "gcontact.h"
20 START_TEST (test_get_xmlentries)
23 gcal_t gcal;
24 struct gcal_event_array event_array;
25 gcal_event_t event;
26 int result;
27 char *xml_entry, *tmp;
29 gcal = gcal_new(GCALENDAR);
30 fail_if(gcal == NULL, "Failed constructing gcal object!");
32 result = gcal_get_authentication(gcal, "gcal4tester", "66libgcal");
33 fail_if(result == -1, "Cannot authenticate!");
35 /* Set flag to save XML in internal field of each event */
36 gcal_set_store_xml(gcal, 1);
38 result = gcal_get_events(gcal, &event_array);
39 fail_if(result == -1, "Failed downloading events!");
41 event = gcal_event_element(&event_array, 0);
42 xml_entry = gcal_event_get_xml(event);
44 fail_if(xml_entry == NULL, "Cannot access raw XML!");
45 tmp = strstr(xml_entry, "<gd:who");
46 fail_if(xml_entry == NULL, "Raw XML lacks field!");
48 /* Cleanup */
49 gcal_cleanup_events(&event_array);
50 gcal_delete(gcal);
53 END_TEST
55 START_TEST (test_get_xmlcontacts)
58 gcal_t gcal;
59 struct gcal_contact_array contact_array;
60 gcal_contact_t contact;
61 int result;
62 char *xml_entry, *tmp;
64 gcal = gcal_new(GCONTACT);
65 fail_if(gcal == NULL, "Failed constructing gcal object!");
67 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
68 fail_if(result == -1, "Cannot authenticate!");
70 /* Set flag to save XML in internal field of each event */
71 gcal_set_store_xml(gcal, 1);
73 result = gcal_get_contacts(gcal, &contact_array);
74 fail_if(result == -1, "Failed downloading events!");
76 contact = gcal_contact_element(&contact_array, 0);
77 xml_entry = gcal_contact_get_xml(contact);
79 fail_if(xml_entry == NULL, "Cannot access raw XML!");
80 tmp = strstr(xml_entry, "<gd:email");
81 fail_if(xml_entry == NULL, "Raw XML lacks field!");
83 /* Cleanup */
84 gcal_cleanup_contacts(&contact_array);
85 gcal_delete(gcal);
88 END_TEST
90 START_TEST (test_oper_xmlevents)
92 gcal_t gcal;
93 gcal_event_t event;
94 int result;
95 char *xml_entry, *tmp;
97 gcal = gcal_new(GCALENDAR);
98 fail_if(gcal == NULL, "Failed constructing gcal object!");
100 result = gcal_get_authentication(gcal, "gcal4tester", "66libgcal");
101 fail_if(result == -1, "Cannot authenticate!");
103 /* Set flag to save XML in internal field of each event */
104 gcal_set_store_xml(gcal, 1);
106 /* Create a new event object */
107 event = gcal_event_new(NULL);
108 fail_if (!event, "Cannot construct event object!");
109 gcal_event_set_title(event, "A new event");
110 gcal_event_set_content(event, "Here goes the description");
111 gcal_event_set_start(event, "2008-06-24T16:00:00Z");
112 gcal_event_set_end(event, "2008-06-24T18:00:00Z");
113 gcal_event_set_where(event, "A nice place for a meeting");
115 /* Add a new event */
116 result = gcal_add_event(gcal, event);
117 fail_if(result == -1, "Failed adding a new event!");
118 xml_entry = gcal_event_get_xml(event);
119 fail_if(xml_entry == NULL, "Cannot access raw XML!");
120 tmp = strstr(xml_entry, "<gd:who");
121 fail_if(xml_entry == NULL, "Raw XML lacks field!");
124 /* Edit this event */
125 gcal_event_set_title(event, "Changing the title");
126 result = gcal_update_event(gcal, event);
127 fail_if(result == -1, "Failed editing event!");
128 xml_entry = gcal_event_get_xml(event);
129 fail_if(xml_entry == NULL, "Cannot access raw XML!");
130 tmp = strstr(xml_entry, "Changing the title");
131 fail_if(xml_entry == NULL, "Raw XML lacks field!");
134 /* Delete this event (note: google doesn't really deletes
135 * the event, but set its status to 'cancelled' and keeps
136 * then for nearly 4 weeks).
138 result = gcal_erase_event(gcal, event);
139 fail_if(result == -1, "Failed deleting event!");
141 /* Cleanup */
142 gcal_event_delete(event);
143 gcal_delete(gcal);
146 END_TEST
148 START_TEST (test_oper_xmlcontact)
150 gcal_t gcal;
151 gcal_contact_t contact;
152 int result;
153 char *xml_entry, *tmp;
155 gcal = gcal_new(GCONTACT);
156 fail_if(gcal == NULL, "Failed constructing gcal object!");
158 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
159 fail_if(result == -1, "Cannot authenticate!");
161 /* Set flag to save XML in internal field of each contact */
162 gcal_set_store_xml(gcal, 1);
164 /* Create a new contact object */
165 contact = gcal_contact_new(NULL);
166 fail_if(!contact, "Cannot construct contact object!");
167 gcal_contact_set_title(contact, "Jonhy Generic Guy");
168 gcal_contact_delete_email_addresses(contact);
169 gcal_contact_add_email_address(contact, "johnthedoe@nobody.com", E_OTHER, 1);
172 /* Add a new contact */
173 result = gcal_add_contact(gcal, contact);
174 fail_if(result == -1, "Failed adding a new contact!");
175 xml_entry = gcal_contact_get_xml(contact);
176 fail_if(xml_entry == NULL, "Cannot access raw XML!");
177 tmp = strstr(xml_entry, "<gd:email");
178 fail_if(xml_entry == NULL, "Raw XML lacks field!");
181 /* Edit this contact */
182 gcal_contact_set_title(contact, "Jonhy Super Generic Guy");
183 result = gcal_update_contact(gcal, contact);
184 fail_if(result == -1, "Failed editing contact!");
185 xml_entry = gcal_contact_get_xml(contact);
186 fail_if(xml_entry == NULL, "Cannot access raw XML!");
187 tmp = strstr(xml_entry, "Super");
188 fail_if(xml_entry == NULL, "Raw XML lacks field!");
191 /* Delete this contact (the contact can still be retrieved
192 * using query parameter 'showdeleted=true' for
193 * for nearly 4 weeks).
195 result = gcal_erase_contact(gcal, contact);
196 fail_if(result == -1, "Failed deleting contact!");
198 /* Cleanup */
199 gcal_contact_delete(contact);
200 gcal_delete(gcal);
203 END_TEST
205 START_TEST (test_oper_purexml)
207 char *super_contact = NULL, *edit_url = NULL, *etag = NULL;
208 char *updated1 = NULL, *updated2 = NULL, *updated3 = NULL;
209 gcal_t gcal;
210 gcal_contact_t contact;
211 gcal_structured_subvalues_t structured_entry;
212 int result;
214 gcal = gcal_new(GCONTACT);
215 fail_if(gcal == NULL, "Failed constructing gcal object!");
217 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
218 fail_if(result == -1, "Cannot authenticate!");
220 if (find_load_file("/utests/supercontact.xml", &super_contact))
221 fail_if(1, "Cannot load contact XML file!");
222 contact = gcal_contact_new(super_contact);
223 gcal_contact_delete(contact);
225 /* Add and update */
226 result = gcal_add_xmlentry(gcal, super_contact, &updated1);
227 fail_if(result == -1, "Failed adding a new contact! HTTP code: %d"
228 "\nmsg: %s\n", gcal_status_httpcode(gcal),
229 gcal_status_msg(gcal));
230 contact = gcal_contact_new(updated1);
231 gcal_contact_delete(contact);
233 result = gcal_update_xmlentry(gcal, updated1, &updated2, NULL, NULL);
234 fail_if(result == -1, "Failed editing a new contact! HTTP code: %d"
235 "\nmsg: %s\n", gcal_status_httpcode(gcal),
236 gcal_status_msg(gcal));
237 contact = gcal_contact_new(updated2);
238 gcal_contact_delete(contact);
240 /* Create a contact object out of raw XML: useful to get the
241 * updated edit_url, id, etc.
243 contact = gcal_contact_new(updated2);
244 fail_if(!contact, "Cannot create contact object!\n");
246 structured_entry = gcal_contact_get_structured_name(contact);
247 fail_if(strcmp("John 'Super' Doe", gcal_contact_get_structured_entry(structured_entry,0,1,"fullName")),
248 "Failure parsing contact XML: fullName!");
250 /* update corner case where the new XML doesn't have the edit URL */
251 free(super_contact);
252 if (find_load_file("/utests/contact_documentation.xml", &super_contact))
253 fail_if(1, "Cannot load contact XML file!");
255 result = gcal_get_edit_url(updated2, &edit_url);
256 fail_if(result == -1, "Cannot extract edit URL!");
257 result = gcal_get_extract_etag(updated2, &etag);
258 fail_if(result == -1, "Cannot extract etag!");
260 result = gcal_update_xmlentry(gcal, super_contact, &updated3,
261 edit_url, etag);
262 fail_if(result == -1, "Failed editing a new contact! HTTP code: %d"
263 "\nmsg: %s\n", gcal_status_httpcode(gcal),
264 gcal_status_msg(gcal));
266 /* delete */
267 result = gcal_erase_xmlentry(gcal, updated3);
268 fail_if(result == -1, "Failed deleting a new contact! HTTP code: %d"
269 "\nmsg: %s\n", gcal_status_httpcode(gcal),
270 gcal_status_msg(gcal));
272 /* Cleanup */
273 free(super_contact);
274 free(updated1);
275 free(updated2);
276 free(updated3);
277 free(edit_url);
278 gcal_delete(gcal);
279 gcal_contact_delete(contact);
281 END_TEST
283 START_TEST (test_oper_purexmlcal)
285 char *super_calendar = NULL, *edit_url = NULL, *etag = NULL;
286 char *updated1 = NULL, *updated2 = NULL, *updated3 = NULL;
287 gcal_t gcal;
288 gcal_event_t event;
289 int result;
291 gcal = gcal_new(GCALENDAR);
292 fail_if(gcal == NULL, "Failed constructing gcal object!");
294 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
295 fail_if(result == -1, "Cannot authenticate!");
297 if (find_load_file("/utests/fullcalendar.xml", &super_calendar))
298 fail_if(1, "Cannot load calendar XML file!");
300 /* Add and update */
301 result = gcal_add_xmlentry(gcal, super_calendar, &updated1);
302 fail_if(result == -1, "Failed adding a new calendar! HTTP code: %d"
303 "\nmsg: %s\n", gcal_status_httpcode(gcal),
304 gcal_status_msg(gcal));
306 result = gcal_update_xmlentry(gcal, updated1, &updated2, NULL, NULL);
307 fail_if(result == -1, "Failed editing a new calendar! HTTP code: %d"
308 "\nmsg: %s\n", gcal_status_httpcode(gcal),
309 gcal_status_msg(gcal));
311 /* Create an event object out of raw XML: useful to get the
312 * updated edit_url, id, etc.
314 event = gcal_event_new(updated2);
315 fail_if(!event, "Cannot create event object!\n");
316 fail_if(strcmp("Hockey with Beth", gcal_event_get_title(event)),
317 "Failure parsing event XML: title!\n");
320 /* update corner case: where the new XML doesn't have the edit URL */
321 free(super_calendar);
322 if (find_load_file("/utests/calendar_documentation.xml", &super_calendar))
323 fail_if(1, "Cannot load calendar XML file!");
325 result = gcal_get_edit_url(updated2, &edit_url);
326 fail_if(result == -1, "Cannot extract edit URL!");
327 result = gcal_get_extract_etag(updated2, &etag);
328 fail_if(result == -1, "Cannot extract etag!");
330 result = gcal_update_xmlentry(gcal, super_calendar, &updated3,
331 edit_url, etag);
332 fail_if(result == -1, "Failed editing a new event! HTTP code: %d"
333 "\nmsg: %s\n", gcal_status_httpcode(gcal),
334 gcal_status_msg(gcal));
336 /* delete */
337 result = gcal_erase_xmlentry(gcal, updated3);
338 fail_if(result == -1, "Failed deleting a new calendar! HTTP code: %d"
339 "\nmsg: %s\n", gcal_status_httpcode(gcal),
340 gcal_status_msg(gcal));
342 /* Cleanup */
343 free(super_calendar);
344 free(updated1);
345 free(updated2);
346 free(updated3);
347 free(edit_url);
348 gcal_delete(gcal);
349 gcal_event_delete(event);
352 END_TEST
354 TCase *xmlmode_tcase_create(void)
356 TCase *tc = NULL;
357 int timeout_seconds = 60;
358 tc = tcase_create("xmlmode");
360 tcase_set_timeout(tc, timeout_seconds);
361 tcase_add_test(tc, test_get_xmlentries);
362 tcase_add_test(tc, test_get_xmlcontacts);
363 tcase_add_test(tc, test_oper_xmlevents);
364 tcase_add_test(tc, test_oper_xmlcontact);
365 tcase_add_test(tc, test_oper_purexml);
366 tcase_add_test(tc, test_oper_purexmlcal);
367 return tc;