Skip to main content
summaryrefslogtreecommitdiffstats
blob: c8d2cf8c968ddd2247e39e52c8b863400f98e5ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*******************************************************************************
 * Copyright (c) 2006 - 2006 Mylar eclipse.org project and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Mylar project committers - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.trac.tests;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Random;

import junit.framework.TestCase;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.eclipse.mylar.context.tests.support.MylarTestUtils;
import org.eclipse.mylar.context.tests.support.MylarTestUtils.Credentials;
import org.eclipse.mylar.context.tests.support.MylarTestUtils.PrivilegeLevel;
import org.eclipse.mylar.internal.trac.core.util.TracHttpClientTransportFactory;

/**
 * Test cases for <a href="http://trac-hacks.org/wiki/XmlRpcPlugin">Trac XML-RPC
 * Plugin</a> API. Revision 1070 or higher is required.
 * 
 * <p>
 * This class does not depend on any Mylar (connector) classes except for
 * TracHttpClientTransportFactory which is needed for initialization of
 * HttpClient.
 * 
 * @author Steffen Pingel
 */
public class TracXmlRpcTest extends TestCase {

	public static final String XMLRPC_URL = "/login/xmlrpc";

	private XmlRpcClient xmlrpc;

	private String username;

	// private String password;

	private Random random;

	private ArrayList<Integer> tickets;

	@Override
	protected void setUp() throws Exception {
		super.setUp();

		random = new Random();

		Credentials credentials = MylarTestUtils.readCredentials(PrivilegeLevel.ADMIN);
		createConnection(new URL(Constants.TEST_TRAC_010_URL + XMLRPC_URL), credentials.username, credentials.password);

		tickets = new ArrayList<Integer>();
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();

		for (int id : tickets) {
			call("ticket.delete", id);
		}
	}

	private void createConnection(URL url, String username, String password) throws Exception {
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setEncoding("UTF-8");
		config.setBasicUserName(username);
		config.setBasicPassword(password);
		config.setServerURL(url);

		xmlrpc = new XmlRpcClient();
		xmlrpc.setConfig(config);

		TracHttpClientTransportFactory factory = new TracHttpClientTransportFactory(xmlrpc);
		xmlrpc.setTransportFactory(factory);

		this.username = username;
		// this.password = password;
	}

	private int createTicket(String summary, String description, Map attributes) throws XmlRpcException, IOException {
		int id = (Integer) call("ticket.create", summary, description, attributes);
		tickets.add(id);
		return id;
	}

	private Object call(String method, Object... parameters) throws XmlRpcException, IOException {
		Object result = xmlrpc.execute(method, parameters);
		if (result instanceof XmlRpcException) {
			throw (XmlRpcException) result;
		}
		return result;
	}

	public Map<String, Object> createMultiCall(String methodName, Object... parameters) throws XmlRpcException,
			IOException {
		Map<String, Object> table = new Hashtable<String, Object>();
		table.put("methodName", methodName);
		table.put("params", parameters);
		return table;
	}

	private void internalTestCrud(String module) throws XmlRpcException, IOException {
		try {
			call(module + ".delete", "foo");
		} catch (XmlRpcException e) {
		}

		call(module + ".create", "foo", "bar");
		try {
			assertHasValue((Object[]) call(module + ".getAll"), "foo");
			assertEquals("bar", (String) (call(module + ".get", "foo")));

			call(module + ".update", "foo", "baz");
			assertEquals("baz", (String) (call(module + ".get", "foo")));
		} finally {
			call(module + ".delete", "foo");
		}
	}

	private Object createValue(Object fieldName, Object clazz) {
		if (clazz == String.class) {
			return fieldName.toString() + random.nextInt();
		} else if (clazz == Date.class) {
			return new Date();
		} else if (clazz == Boolean.class) {
			return random.nextBoolean();
		} else if (clazz == Double.class) {
			return random.nextDouble();
		} else if (clazz == Integer.class) {
			return random.nextInt();
		}

		throw new RuntimeException("Invalid test data: " + fieldName + ", " + clazz);
	}

	private void internalTestComponent(String module, Object... fields) throws XmlRpcException, IOException {
		try {
			call(module + ".delete", "foo");
		} catch (XmlRpcException e) {
		}

		Map<String, Object> attributes = new Hashtable<String, Object>();
		for (int i = 0; i < fields.length; i += 2) {
			attributes.put((String) fields[i], createValue(fields[i], fields[i + 1]));
		}

		call(module + ".create", "foo", attributes);

		try {
			assertHasValue((Object[]) call(module + ".getAll"), "foo");
			Map values = (Map) call(module + ".get", "foo");
			for (String attribute : attributes.keySet()) {
				assertEquals(attributes.get(attribute), values.get(attribute));
			}

			for (int i = 0; i < fields.length; i += 2) {
				attributes.put((String) fields[i], createValue(fields[i], fields[i + 1]));
			}

			call(module + ".update", "foo", attributes);
			values = (Map) call(module + ".get", "foo");
			for (String attribute : attributes.keySet()) {
				assertEquals(attributes.get(attribute), values.get(attribute));
			}
		} finally {
			call(module + ".delete", "foo");
		}
	}

	public void testMilestoneDate() throws XmlRpcException, IOException {
		try {
			call("ticket.milestone.delete", "foo");
		} catch (XmlRpcException e) {
		}

		int due = (int) (System.currentTimeMillis() / 1000) + 1000;
		int completed = (int) (System.currentTimeMillis() / 1000);

		Map<String, Object> attributes = new Hashtable<String, Object>();
		attributes.put("description", "description");
		attributes.put("due", due);
		attributes.put("completed", completed);

		call("ticket.milestone.create", "foo", attributes);

		Map values = (Map) call("ticket.milestone.get", "foo");
		assertEquals(new Integer(due), (Integer) values.get("due"));
		assertEquals(new Integer(completed), (Integer) values.get("completed"));

		call("ticket.milestone.delete", "foo");
	}

	private void assertHasValue(Object[] items, Object value) {
		for (Object item : items) {
			if (item.equals(value)) {
				return;
			}
		}
		fail("Could not find expected value: " + value);
	}

	private void assertTicketHasAttributes(Map<String, Object> attributes, int id, Object[] ticket) {
		assertTicketHasAttributes(attributes, id, ticket, true);
	}

	private void assertTicketHasAttributes(Map<String, Object> attributes, int id, Object[] ticket, boolean newTicket) {
		assertEquals(id, ticket[0]);
		assertTrue(ticket[1] instanceof Integer); // time created
		// time changed
		if (newTicket) {
			assertEquals(ticket[1], ticket[2]);
		} else {
			assertTrue((Integer) ticket[2] >= (Integer) ticket[1]);
		}
		Map values = (Map) ticket[3];
		for (String attribute : attributes.keySet()) {
			assertEquals(attributes.get(attribute), values.get(attribute));
		}
	}

	public void testGetTicket() throws XmlRpcException, IOException {
		Map<String, Object> attributes = new Hashtable<String, Object>();
		attributes.put("type", "task");
		attributes.put("status", "closed");
		int id = createTicket("summary", "description", attributes);

		attributes.put("summary", "summary");
		attributes.put("description", "description");

		Object[] ticket = (Object[]) call("ticket.get", id);
		assertTicketHasAttributes(attributes, id, ticket);
	}

	public void testGetTicketNonExistant() throws XmlRpcException, IOException {
		try {
			call("ticket.delete", Integer.MAX_VALUE);
		} catch (Exception e) {
			// ignore
		}

		try {
			List ticket = (List) call("ticket.get", Integer.MAX_VALUE);
			fail("Expected XmlRpcException, got ticket instead: " + ticket);
		} catch (XmlRpcException e) {
			// ignore
		}
	}

	public void testGetTicketUmlaute() throws XmlRpcException, IOException {
		Map<String, Object> attributes = new Hashtable<String, Object>();
		int id = createTicket("summaryäÖÜ", "ßßß", attributes);

		attributes.put("summary", "summaryäÖÜ");
		attributes.put("description", "ßßß");

		Object[] ticket = (Object[]) call("ticket.get", id);
		assertTicketHasAttributes(attributes, id, ticket);
	}

	public void testUpdateTicket() throws XmlRpcException, IOException {
		int id = createTicket("summary", "description", new Hashtable());

		Map<String, Object> attributes = new Hashtable<String, Object>();
		attributes.put("summary", "changed");
		call("ticket.update", id, "my comment", attributes);

		attributes.put("description", "description");

		Object[] ticket = (Object[]) call("ticket.get", id);
		Map values = (Map) ticket[3];
		for (String attribute : attributes.keySet()) {
			assertEquals(attributes.get(attribute), values.get(attribute));
		}
	}

	public void testTicketCustomFields() throws XmlRpcException, IOException {
		Map<String, Object> attributes = new Hashtable<String, Object>();
		attributes.put("custom_text_field", "myvalue");
		int id = createTicket("summary", "description", attributes);

		// check for default values
		attributes.put("custom_checkbox_field", "1");
		attributes.put("custom_select_field", "two");
		attributes.put("custom_radio_field", "baz");
		attributes.put("custom_textarea_field", "default text");

		Object[] ticket = (Object[]) call("ticket.get", id);
		assertTicketHasAttributes(attributes, id, ticket);

		attributes.put("custom_text_field", "myvalue2");
		attributes.put("custom_checkbox_field", "0");
		attributes.put("custom_select_field", "one");
		attributes.put("custom_radio_field", "foo");
		attributes.put("custom_textarea_field", "mytext");

		call("ticket.update", id, "my comment", attributes);

		ticket = (Object[]) call("ticket.get", id);
		assertTicketHasAttributes(attributes, id, ticket, false);
	}

	public void testGetChangeLog() throws XmlRpcException, IOException {
		int id = createTicket("summary", "description", new Hashtable());

		Map<String, Object> attributes = new Hashtable<String, Object>();
		attributes.put("summary", "changed");
		call("ticket.update", id, "my comment", attributes);

		Object[] log = (Object[]) call("ticket.changeLog", id, 0);
		Object[] entry = (Object[]) log[0];
		assertTrue(entry[0] instanceof Integer); // time
		assertEquals(username, entry[1]); // author
		assertEquals("summary", entry[2]); // field
		assertEquals("summary", entry[3]); // old value
		assertEquals("changed", entry[4]); // new value
	}

	public void testMultiGetTicket() throws XmlRpcException, IOException {
		int id1 = createTicket("summary1", "description1", new Hashtable());
		int id2 = createTicket("summary2", "description2", new Hashtable());

		List<Map> calls = new ArrayList<Map>();
		calls.add(createMultiCall("ticket.get", id1));
		calls.add(createMultiCall("ticket.get", id2));
		Object[] ret = (Object[]) call("system.multicall", calls);

		Object[] ticket = (Object[]) ((Object[]) ret[0])[0];
		Map<String, Object> attributes = new Hashtable<String, Object>();
		attributes.put("summary", "summary1");
		attributes.put("description", "description1");
		assertTicketHasAttributes(attributes, id1, ticket);

		ticket = (Object[]) ((Object[]) ret[1])[0];
		attributes.clear();
		attributes.put("summary", "summary2");
		attributes.put("description", "description2");
		assertTicketHasAttributes(attributes, id2, ticket);
	}

	public void testAttachment() throws XmlRpcException, IOException {
		int id = createTicket("summary", "description", new Hashtable());

		String filename = (String) call("ticket.putAttachment", id, "attach.txt", "data".getBytes(), true);
		// the returned filename may differ, since another ticket may have an
		// attachment named "attach.txt"
		// assertEquals("attach.txt", filename);

		Object[] ret = (Object[]) call("ticket.listAttachments", id);
		assertEquals(1, ret.length);
		assertHasValue(ret, filename);

		byte[] bytes = (byte[]) call("ticket.getAttachment", id, filename);
		String data = new String(bytes);
		assertEquals("data", data);

		String filename2 = (String) call("ticket.putAttachment", id, filename, "data".getBytes(), true);
		assertEquals(filename, filename2);
		ret = (Object[]) call("ticket.listAttachments", id);
		assertEquals(1, ret.length);
		assertHasValue(ret, filename);

		String filename3 = (String) call("ticket.putAttachment", id, "attach.txt", "data".getBytes(), false);
		assertFalse("attach.txt".equals(filename3));
		ret = (Object[]) call("ticket.listAttachments", id);
		assertEquals(2, ret.length);
		assertHasValue(ret, filename);
		assertHasValue(ret, filename3);
	}

	public void testDeleteAttachment() throws XmlRpcException, IOException {
		int id = createTicket("summary", "description", new Hashtable());

		String filename = (String) call("ticket.putAttachment", id, "attach.txt", "data".getBytes(), true);

		Object[] ret = (Object[]) call("ticket.listAttachments", id);
		assertEquals(1, ret.length);
		assertHasValue(ret, filename);

		call("ticket.deleteAttachment", id, filename);

		ret = (Object[]) call("ticket.listAttachments", id);
		assertEquals(0, ret.length);
	}

	public void testQuery() throws XmlRpcException, IOException {
		Object[] ret = (Object[]) call("ticket.query", "summary~=foo|bar|baz");
		for (Object id : ret) {
			call("ticket.delete", (Integer) id);
		}

		int id1 = createTicket("foobarsum1", "description", new Hashtable());
		int id2 = createTicket("foobaz sum2", "description", new Hashtable());
		int id3 = createTicket("foobarbaz3", "foobarbaz description3", new Hashtable());

		ret = (Object[]) call("ticket.query", "summary=foobarsum1|foobaz sum2");
		assertEquals(2, ret.length);
		assertEquals(id1, ret[0]);
		assertEquals(id2, ret[1]);

		// the first criterium is ignored
		ret = (Object[]) call("ticket.query", "summary~=foobarsum1&summary~=foobaz sum2");
		assertEquals(1, ret.length);
		assertEquals(id2, ret[0]);

		ret = (Object[]) call("ticket.query", "summary~=bar|baz");
		assertEquals(3, ret.length);

		ret = (Object[]) call("ticket.query", "description~=foobarbaz description3");
		assertEquals(1, ret.length);
		assertEquals(id3, ret[0]);
	}

	public void testQueryAll() throws XmlRpcException, IOException {
		int id = createTicket("foo", "description", new Hashtable());

		Object[] ret = (Object[]) call("ticket.query", "order=id");
		assertTrue(ret.length > 0);
		assertHasValue(ret, id);
	}

	public void testPriorities() throws XmlRpcException, IOException {
		internalTestCrud("ticket.priority");
	}

	public void testSeverities() throws XmlRpcException, IOException {
		internalTestCrud("ticket.severity");
	}

	public void testType() throws XmlRpcException, IOException {
		internalTestCrud("ticket.type");
	}

	public void testStatus() throws XmlRpcException, IOException {
		internalTestCrud("ticket.status");
	}

	public void testResolutions() throws XmlRpcException, IOException {
		internalTestCrud("ticket.resolution");
	}

	public void testVersions() throws XmlRpcException, IOException {
		internalTestComponent("ticket.version", "time", Integer.class, "description", String.class);
	}

	public void testComponents() throws XmlRpcException, IOException {
		internalTestComponent("ticket.component", "owner", String.class, "description", String.class);
	}

	public void testMilestones() throws XmlRpcException, IOException {
		internalTestComponent("ticket.milestone", "due", Integer.class, "completed", Integer.class, "description",
				String.class);
	}

}

Back to the top