1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2000 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
* Purpose: To test the MetaCatURL class by JUnit
|
6
|
* Authors: Jing Tao
|
7
|
*
|
8
|
* '$Author: tao $'
|
9
|
* '$Date: 2016-02-26 15:47:19 -0800 (Fri, 26 Feb 2016) $'
|
10
|
* '$Revision: 9537 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
package edu.ucsb.nceas.metacattest;
|
28
|
|
29
|
import java.io.InputStream;
|
30
|
import java.io.InputStreamReader;
|
31
|
import java.io.StringWriter;
|
32
|
import java.util.List;
|
33
|
import java.util.Properties;
|
34
|
|
35
|
import junit.framework.Test;
|
36
|
import junit.framework.TestSuite;
|
37
|
import edu.ucsb.nceas.MCTestCase;
|
38
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
39
|
import edu.ucsb.nceas.metacat.client.MetacatClient;
|
40
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
41
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
42
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
43
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
44
|
|
45
|
|
46
|
/**
|
47
|
* A JUnit test for testing Step class processing
|
48
|
*/
|
49
|
public class MetaCatServletTest extends MCTestCase {
|
50
|
private static String metacatURL;
|
51
|
private MetacatClient metacat = null;
|
52
|
private String serialNumber;
|
53
|
|
54
|
/* Initialize properties */
|
55
|
static {
|
56
|
try {
|
57
|
metacatURL = PropertyService.getProperty("test.metacatUrl");
|
58
|
} catch (PropertyNotFoundException pnfe) {
|
59
|
System.err.println("Could not get property in static block: "
|
60
|
+ pnfe.getMessage());
|
61
|
}
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Constructor to build the test
|
66
|
*
|
67
|
* @param name
|
68
|
* the name of the test method
|
69
|
*/
|
70
|
public MetaCatServletTest(String name) {
|
71
|
super(name);
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Constructor to build the test
|
76
|
*
|
77
|
* @param name
|
78
|
* the name of the test method
|
79
|
*/
|
80
|
public MetaCatServletTest(String name, String serial) {
|
81
|
super(name);
|
82
|
serialNumber = serial;
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Establish a testing framework by initializing appropriate objects
|
87
|
*/
|
88
|
public void setUp() {
|
89
|
try {
|
90
|
metacat = (MetacatClient) MetacatFactory.createMetacatConnection(metacatURL);
|
91
|
} catch (MetacatInaccessibleException e) {
|
92
|
fail("Could not initialize MetacatClient: " + e.getMessage());
|
93
|
e.printStackTrace();
|
94
|
}
|
95
|
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Release any objects after tests are complete
|
100
|
*/
|
101
|
public void tearDown() {
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Create a suite of tests to be run together
|
106
|
*/
|
107
|
public static Test suite() {
|
108
|
double number = 0;
|
109
|
String serial = null;
|
110
|
|
111
|
TestSuite suite = new TestSuite();
|
112
|
suite.addTest(new MetaCatServletTest("initialize"));
|
113
|
//suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
|
114
|
//suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
|
115
|
suite.addTest(new MetaCatServletTest("testOtherReferralLogin"));
|
116
|
suite.addTest(new MetaCatServletTest("testOtherReferralLoginFail"));
|
117
|
suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
|
118
|
// Should put a login successfully at the end of login test
|
119
|
// So insert or update can have cookie.
|
120
|
suite.addTest(new MetaCatServletTest("testNCEASLogin"));
|
121
|
|
122
|
// create random number for docid, so it can void repeat
|
123
|
number = Math.random() * 100000;
|
124
|
serial = Integer.toString(((new Double(number)).intValue()));
|
125
|
debug("serial: " + serial);
|
126
|
suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
|
127
|
suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", serial));
|
128
|
suite.addTest(new MetaCatServletTest("testUpdateXMLDocument", serial));
|
129
|
|
130
|
suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", serial));
|
131
|
suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", serial));
|
132
|
|
133
|
suite.addTest(new MetaCatServletTest("testDeleteXMLDocument", serial));
|
134
|
|
135
|
// insert invalid xml document
|
136
|
number = Math.random() * 100000;
|
137
|
serial = Integer.toString(((new Double(number)).intValue()));
|
138
|
suite.addTest(new MetaCatServletTest("testInsertInvalidateXMLDocument", serial));
|
139
|
// insert non well formed document
|
140
|
number = Math.random() * 100000;
|
141
|
serial = Integer.toString(((new Double(number)).intValue()));
|
142
|
suite
|
143
|
.addTest(new MetaCatServletTest("testInsertNonWellFormedXMLDocument",
|
144
|
serial));
|
145
|
|
146
|
suite.addTest(new MetaCatServletTest("testLogOut"));
|
147
|
|
148
|
suite.addTest(new MetaCatServletTest("testReindexFail"));
|
149
|
|
150
|
|
151
|
return suite;
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* Run an initial test that always passes to check that the test harness is
|
156
|
* working.
|
157
|
*/
|
158
|
public void initialize() {
|
159
|
assertTrue(1 == 1);
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* Test the login to neceas succesfully
|
164
|
*/
|
165
|
public void testNCEASLogin() {
|
166
|
debug("\nRunning: testNCEASLogin test");
|
167
|
try {
|
168
|
String user = PropertyService.getProperty("test.mcUser");
|
169
|
String passwd = PropertyService.getProperty("test.mcPassword");
|
170
|
assertTrue(logIn(user, passwd));
|
171
|
this.testLogOut();
|
172
|
|
173
|
} catch (PropertyNotFoundException pnfe) {
|
174
|
fail("Could not find property: " + pnfe.getMessage());
|
175
|
}
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Test the login to neceas failed
|
180
|
*/
|
181
|
public void testNCEASLoginFail() {
|
182
|
debug("\nRunning: testNCEASLoginFail test");
|
183
|
try {
|
184
|
String user = PropertyService.getProperty("test.mcUser");
|
185
|
String passwd = "BogusPasswordShouldFail";
|
186
|
assertTrue(!logIn(user, passwd));
|
187
|
this.testLogOut();
|
188
|
|
189
|
} catch (PropertyNotFoundException pnfe) {
|
190
|
fail("Could not find property: " + pnfe.getMessage());
|
191
|
}
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* Test the login to lter succesfully
|
196
|
*/
|
197
|
/*public void testLterReferralLogin() {
|
198
|
debug("\nRunning: testLterReferralLogin test");
|
199
|
String user = null;
|
200
|
String passwd = null;
|
201
|
try {
|
202
|
user = PropertyService.getProperty("test.lterUser");
|
203
|
passwd = PropertyService.getProperty("test.lterPassword");
|
204
|
} catch (PropertyNotFoundException pnfe) {
|
205
|
fail("Could not find property: " + pnfe.getMessage());
|
206
|
}
|
207
|
|
208
|
debug("Logging into lter: " + user + " : " + passwd);
|
209
|
assertTrue(logIn(user, passwd));
|
210
|
this.testLogOut();
|
211
|
|
212
|
|
213
|
}*/
|
214
|
|
215
|
/**
|
216
|
* Test the login to lter failed
|
217
|
*/
|
218
|
/*public void testLterReferralLoginFail() {
|
219
|
debug("\nRunning: testLterReferralLoginFail test");
|
220
|
String user = null;
|
221
|
String passwd = "wrong";
|
222
|
try {
|
223
|
user = PropertyService.getProperty("test.lterUser");
|
224
|
} catch (PropertyNotFoundException pnfe) {
|
225
|
fail("Could not find property: " + pnfe.getMessage());
|
226
|
}
|
227
|
assertTrue(!logIn(user, passwd));
|
228
|
// assertTrue( withProtocol.getProtocol().equals("http"));
|
229
|
this.testLogOut();
|
230
|
|
231
|
}*/
|
232
|
|
233
|
/**
|
234
|
* Test the login to Other succesfully
|
235
|
*/
|
236
|
public void testOtherReferralLogin() {
|
237
|
debug("\nRunning: testOtherReferralLogin test");
|
238
|
String user = null;
|
239
|
String passwd = null;
|
240
|
try {
|
241
|
user = PropertyService.getProperty("test.referralUser");
|
242
|
passwd = PropertyService.getProperty("test.referralPassword");
|
243
|
} catch (PropertyNotFoundException pnfe) {
|
244
|
fail("Could not find property: " + pnfe.getMessage());
|
245
|
}
|
246
|
debug("logging in Other user: " + user + ":" + passwd);
|
247
|
assertTrue(logIn(user, passwd));
|
248
|
// assertTrue( withProtocol.getProtocol().equals("http"));
|
249
|
this.testLogOut();
|
250
|
|
251
|
}
|
252
|
|
253
|
/**
|
254
|
* Test the login to Other failed
|
255
|
*/
|
256
|
public void testOtherReferralLoginFail() {
|
257
|
debug("\nRunning: testOtherReferralLoginFail test");
|
258
|
String user = null;
|
259
|
String passwd = "wrong";
|
260
|
try {
|
261
|
user = PropertyService.getProperty("test.referralUser");
|
262
|
} catch (PropertyNotFoundException pnfe) {
|
263
|
fail("Could not find property: " + pnfe.getMessage());
|
264
|
}
|
265
|
assertTrue(!logIn(user, passwd));
|
266
|
// assertTrue( withProtocol.getProtocol().equals("http"));
|
267
|
this.testLogOut();
|
268
|
|
269
|
}
|
270
|
|
271
|
|
272
|
|
273
|
/**
|
274
|
* Test insert a xml document successfully
|
275
|
*/
|
276
|
public void testInsertXMLDocument() {
|
277
|
debug("\nRunning: testInsertXMLDocument test");
|
278
|
String name = null;
|
279
|
try {
|
280
|
String user = PropertyService.getProperty("test.mcUser");
|
281
|
String passwd = PropertyService.getProperty("test.mcPassword");
|
282
|
metacat.login(user, passwd);
|
283
|
|
284
|
name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber + PropertyService.getProperty("document.accNumSeparator")
|
285
|
+ "1";
|
286
|
debug("insert docid: " + name);
|
287
|
String content = "<?xml version=\"1.0\"?>" + "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
|
288
|
+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb.edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
|
289
|
+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name + "</identifier>" + "<allow>" + "<principal>" + user
|
290
|
+ "</principal>" + "<permission>all</permission>" + "</allow>" + "<allow>" + "<principal>public</principal>"
|
291
|
+ "<permission>read</permission>" + "</allow>" + "</acl>";
|
292
|
debug("xml document: " + content);
|
293
|
assertTrue(handleXMLDocument(content, name, "insert"));
|
294
|
metacat.logout();
|
295
|
|
296
|
} catch (PropertyNotFoundException pnfe) {
|
297
|
fail("Could not find property: " + pnfe.getMessage());
|
298
|
} catch (Exception e) {
|
299
|
// TODO Auto-generated catch block
|
300
|
fail(e.getMessage());
|
301
|
e.printStackTrace();
|
302
|
}
|
303
|
}
|
304
|
|
305
|
/**
|
306
|
* Test insert a invalidate xml document successfully In the String, there
|
307
|
* is no <!Doctype ... Public/System/>
|
308
|
*/
|
309
|
public void testInsertInvalidateXMLDocument() {
|
310
|
debug("\nRunning: testInsertInvalidateXMLDocument test");
|
311
|
String name = null;
|
312
|
try {
|
313
|
String user = PropertyService.getProperty("test.mcUser");
|
314
|
String passwd = PropertyService.getProperty("test.mcPassword");
|
315
|
metacat.login(user, passwd);
|
316
|
|
317
|
name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber + PropertyService.getProperty("document.accNumSeparator")
|
318
|
+ "1";
|
319
|
debug("insert docid: " + name);
|
320
|
String content = "<?xml version=\"1.0\"?>" + "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name + "</identifier>" + "<allow>"
|
321
|
+ "<principal>" + user + "</principal>" + "<permission>all</permission>" + "</allow>" + "<allow>" + "<principal>public</principal>"
|
322
|
+ "<permission>read</permission>" + "</allow>" + "</acl>";
|
323
|
debug("xml document: " + content);
|
324
|
assertTrue(handleXMLDocument(content, name, "insert"));
|
325
|
metacat.logout();
|
326
|
|
327
|
} catch (Exception pnfe) {
|
328
|
fail(pnfe.getMessage());
|
329
|
}
|
330
|
}
|
331
|
|
332
|
/**
|
333
|
* Test insert a non well-formed xml document successfully There is no
|
334
|
* </acl> in this string
|
335
|
*/
|
336
|
public void testInsertNonWellFormedXMLDocument() {
|
337
|
debug("\nRunning: testInsertNonWellFormedXMLDocument test");
|
338
|
String name = null;
|
339
|
try {
|
340
|
String user = PropertyService.getProperty("test.mcUser");
|
341
|
String passwd = PropertyService.getProperty("test.mcPassword");
|
342
|
metacat.login(user, passwd);
|
343
|
|
344
|
name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber + PropertyService.getProperty("document.accNumSeparator")
|
345
|
+ "1";
|
346
|
debug("insert non well-formed docid: " + name);
|
347
|
String content = "<?xml version=\"1.0\"?>" + "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name + "</identifier>" + "<allow>"
|
348
|
+ "<principal>" + user + "</principal>" + "<permission>all</permission>" + "</allow>" + "<allow>" + "<principal>public</principal>"
|
349
|
+ "<permission>read</permission>" + "</allow>";
|
350
|
|
351
|
debug("xml document: " + content);
|
352
|
assertTrue(!handleXMLDocument(content, name, "insert"));
|
353
|
this.testLogOut();
|
354
|
|
355
|
} catch (Exception pnfe) {
|
356
|
fail(pnfe.getMessage());
|
357
|
}
|
358
|
}
|
359
|
|
360
|
/**
|
361
|
* Test read a xml document in xml format successfully
|
362
|
*/
|
363
|
public void testReadXMLDocumentXMLFormat() {
|
364
|
debug("\nRunning: testReadXMLDocumentXMLFormat test");
|
365
|
String name = null;
|
366
|
try {
|
367
|
String user = PropertyService.getProperty("test.mcUser");
|
368
|
String passwd = PropertyService.getProperty("test.mcPassword");
|
369
|
metacat.login(user, passwd);
|
370
|
name = "john" + PropertyService.getProperty("document.accNumSeparator")
|
371
|
+ serialNumber
|
372
|
+ PropertyService.getProperty("document.accNumSeparator") + "1";
|
373
|
assertTrue(handleReadAction(name, "xml"));
|
374
|
metacat.logout();
|
375
|
} catch (Exception pnfe) {
|
376
|
fail("Could not find property: " + pnfe.getMessage());
|
377
|
}
|
378
|
|
379
|
}
|
380
|
|
381
|
/**
|
382
|
* Test read a xml document in html format successfully
|
383
|
*/
|
384
|
public void testReadXMLDocumentHTMLFormat() {
|
385
|
debug("\nRunning: testReadXMLDocumentHTMLFormat test");
|
386
|
String name = null;
|
387
|
try {
|
388
|
String user = PropertyService.getProperty("test.mcUser");
|
389
|
String passwd = PropertyService.getProperty("test.mcPassword");
|
390
|
metacat.login(user, passwd);
|
391
|
name = "john" + PropertyService.getProperty("document.accNumSeparator")
|
392
|
+ serialNumber
|
393
|
+ PropertyService.getProperty("document.accNumSeparator") + "1";
|
394
|
assertTrue(handleReadAction(name, "html"));
|
395
|
metacat.logout();
|
396
|
} catch (Exception pnfe) {
|
397
|
fail(pnfe.getMessage());
|
398
|
}
|
399
|
|
400
|
}
|
401
|
|
402
|
/**
|
403
|
* Test read a xml document in zip format successfully
|
404
|
*/
|
405
|
public void testReadXMLDocumentZipFormat() {
|
406
|
debug("\nRunning: testReadXMLDocumentZipFormat test");
|
407
|
String name = null;
|
408
|
try {
|
409
|
String user = PropertyService.getProperty("test.mcUser");
|
410
|
String passwd = PropertyService.getProperty("test.mcPassword");
|
411
|
metacat.login(user, passwd);
|
412
|
name = "john" + PropertyService.getProperty("document.accNumSeparator")
|
413
|
+ serialNumber
|
414
|
+ PropertyService.getProperty("document.accNumSeparator") + "1";
|
415
|
assertTrue(handleReadAction(name, "zip"));
|
416
|
metacat.logout();
|
417
|
} catch (Exception pnfe) {
|
418
|
fail(pnfe.getMessage());
|
419
|
}
|
420
|
|
421
|
}
|
422
|
|
423
|
/**
|
424
|
* Test insert a xml document successfully
|
425
|
*/
|
426
|
public void testUpdateXMLDocument() {
|
427
|
debug("\nRunning: testUpdateXMLDocument test");
|
428
|
String name = null;
|
429
|
try {
|
430
|
String user = PropertyService.getProperty("test.mcUser");
|
431
|
String passwd = PropertyService.getProperty("test.mcPassword");
|
432
|
|
433
|
metacat.login(user, passwd);
|
434
|
|
435
|
name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber + PropertyService.getProperty("document.accNumSeparator")
|
436
|
+ "2";
|
437
|
debug("update docid: " + name);
|
438
|
String content = "<?xml version=\"1.0\"?>" + "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
|
439
|
+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb." + "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
|
440
|
+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name + "</identifier>" + "<allow>" + "<principal>" + user
|
441
|
+ "</principal>" + "<permission>all</permission>" + "</allow>" + "<allow>" + "<principal>public</principal>"
|
442
|
+ "<permission>read</permission>" + "</allow>" + "</acl>";
|
443
|
debug("xml document: " + content);
|
444
|
assertTrue(handleXMLDocument(content, name, "update"));
|
445
|
this.testLogOut();
|
446
|
} catch (Exception pnfe) {
|
447
|
fail(pnfe.getMessage());
|
448
|
}
|
449
|
}
|
450
|
|
451
|
/**
|
452
|
* Test delete a xml document successfully
|
453
|
*/
|
454
|
public void testDeleteXMLDocument() {
|
455
|
debug("\nRunning: testDeleteXMLDocument test");
|
456
|
|
457
|
String name = null;
|
458
|
try {
|
459
|
String user = PropertyService.getProperty("test.mcUser");
|
460
|
String passwd = PropertyService.getProperty("test.mcPassword");
|
461
|
metacat.login(user, passwd);
|
462
|
|
463
|
name = "john" + PropertyService.getProperty("document.accNumSeparator")
|
464
|
+ serialNumber
|
465
|
+ PropertyService.getProperty("document.accNumSeparator") + "2";
|
466
|
debug("delete docid: " + name);
|
467
|
assertTrue(handleDeleteFile(name));
|
468
|
metacat.logout();
|
469
|
|
470
|
} catch (Exception pnfe) {
|
471
|
fail(pnfe.getMessage());
|
472
|
}
|
473
|
|
474
|
|
475
|
}
|
476
|
|
477
|
|
478
|
/**
|
479
|
* Test logout action
|
480
|
*/
|
481
|
public void testLogOut() {
|
482
|
debug("\nRunning: testLogOut test");
|
483
|
assertTrue(handleLogOut());
|
484
|
|
485
|
}
|
486
|
|
487
|
public void testReindexFail() {
|
488
|
|
489
|
// find a pid to reindex
|
490
|
String identifier = null;
|
491
|
List<String> ids = IdentifierManager.getInstance().getAllSystemMetadataGUIDs();
|
492
|
if (ids != null && !ids.isEmpty()) {
|
493
|
identifier = ids.get(0);
|
494
|
}
|
495
|
Properties prop = new Properties();
|
496
|
prop.put("action", "reindex");
|
497
|
prop.put("pid", identifier);
|
498
|
|
499
|
String message = getMetacatString(prop);
|
500
|
debug("Reindex Message: " + message);
|
501
|
if (message.indexOf("<error>") != -1) {// there was an error
|
502
|
assertTrue(true);
|
503
|
} else if (message.indexOf("<success>") != -1) {
|
504
|
fail("Unauthenticated user should not be able to invoke this action: " + message);
|
505
|
} else {// something weird happened.
|
506
|
fail("There was an unexpected error reindexing pid: " + message);
|
507
|
}
|
508
|
}
|
509
|
|
510
|
/**
|
511
|
* Method to hanld login action
|
512
|
*
|
513
|
* @param usrerName,
|
514
|
* the DN name of the test method
|
515
|
* @param passWord,
|
516
|
* the passwd of the user
|
517
|
*/
|
518
|
|
519
|
public boolean logIn(String userName, String passWord) {
|
520
|
Properties prop = new Properties();
|
521
|
prop.put("action", "login");
|
522
|
prop.put("qformat", "xml");
|
523
|
prop.put("username", userName);
|
524
|
prop.put("password", passWord);
|
525
|
|
526
|
// Now contact metacat
|
527
|
String response = getMetacatString(prop);
|
528
|
debug("Login Message: " + response);
|
529
|
boolean connected = false;
|
530
|
if (response != null && response.indexOf("<login>") != -1) {
|
531
|
connected = true;
|
532
|
} else {
|
533
|
|
534
|
connected = false;
|
535
|
}
|
536
|
|
537
|
return connected;
|
538
|
}
|
539
|
|
540
|
/**
|
541
|
* Method to hanld logout action
|
542
|
*
|
543
|
* @param usrerName,
|
544
|
* the DN name of the test method
|
545
|
* @param passWord,
|
546
|
* the passwd of the user
|
547
|
*/
|
548
|
|
549
|
public boolean handleLogOut() {
|
550
|
boolean disConnected = false;
|
551
|
Properties prop = new Properties();
|
552
|
prop.put("action", "logout");
|
553
|
prop.put("qformat", "xml");
|
554
|
|
555
|
String response = getMetacatString(prop);
|
556
|
debug("Logout Message: " + response);
|
557
|
|
558
|
if (response.indexOf("<logout>") != -1) {
|
559
|
disConnected = true;
|
560
|
} else {
|
561
|
disConnected = false;
|
562
|
}
|
563
|
|
564
|
return disConnected;
|
565
|
}
|
566
|
|
567
|
/**
|
568
|
* Method to hanld read both xml and data file
|
569
|
*
|
570
|
* @param docid,
|
571
|
* the docid of the document want to read
|
572
|
* @param qformat,
|
573
|
* the format of document user want to get
|
574
|
*/
|
575
|
public boolean handleReadAction(String docid, String qformat) {
|
576
|
Properties prop = new Properties();
|
577
|
String message = "";
|
578
|
prop.put("action", "read");
|
579
|
prop.put("qformat", qformat);
|
580
|
prop.put("docid", docid);
|
581
|
|
582
|
message = getMetacatString(prop);
|
583
|
message = message.trim();
|
584
|
if (message == null || message.equals("") || message.indexOf("<error>") != -1) {// there
|
585
|
// was
|
586
|
// an
|
587
|
// error
|
588
|
|
589
|
return false;
|
590
|
} else {// successfully
|
591
|
return true;
|
592
|
}
|
593
|
|
594
|
}
|
595
|
|
596
|
/**
|
597
|
* Method to hanld inset or update xml document
|
598
|
*
|
599
|
* @param xmlDocument,
|
600
|
* the content of xml qformat
|
601
|
* @param docid,
|
602
|
* the docid of the document
|
603
|
* @param action,
|
604
|
* insert or update
|
605
|
*/
|
606
|
public boolean handleXMLDocument(String xmlDocument, String docid, String action)
|
607
|
|
608
|
{ // -attempt to write file to metacat
|
609
|
String access = "no";
|
610
|
StringBuffer fileText = new StringBuffer();
|
611
|
StringBuffer messageBuf = new StringBuffer();
|
612
|
String accessFileId = null;
|
613
|
Properties prop = new Properties();
|
614
|
prop.put("action", action);
|
615
|
prop.put("public", access); // This is the old way of controlling access
|
616
|
prop.put("doctext", xmlDocument);
|
617
|
prop.put("docid", docid);
|
618
|
|
619
|
String message = getMetacatString(prop);
|
620
|
debug("Insert or Update Message: " + message);
|
621
|
if (message.indexOf("<error>") != -1) {// there was an error
|
622
|
|
623
|
return false;
|
624
|
} else if (message.indexOf("<success>") != -1) {// the operation worked
|
625
|
// write the file to the cache and return the file object
|
626
|
return true;
|
627
|
|
628
|
} else {// something weird happened.
|
629
|
return false;
|
630
|
}
|
631
|
|
632
|
}
|
633
|
|
634
|
public boolean handleDeleteFile(String name) {
|
635
|
|
636
|
Properties prop = new Properties();
|
637
|
prop.put("action", "delete");
|
638
|
prop.put("docid", name);
|
639
|
|
640
|
String message = getMetacatString(prop);
|
641
|
debug("Delete Message: " + message);
|
642
|
if (message.indexOf("<error>") != -1) {// there was an error
|
643
|
|
644
|
return false;
|
645
|
} else if (message.indexOf("<success>") != -1) {// the operation worked
|
646
|
// write the file to the cache and return the file object
|
647
|
return true;
|
648
|
|
649
|
} else {// something weird happened.
|
650
|
return false;
|
651
|
}
|
652
|
}
|
653
|
|
654
|
public String getMetacatString(Properties prop) {
|
655
|
String response = null;
|
656
|
|
657
|
// Now contact metacat and send the request
|
658
|
try {
|
659
|
InputStreamReader returnStream = new InputStreamReader(
|
660
|
getMetacatInputStream(prop));
|
661
|
StringWriter sw = new StringWriter();
|
662
|
int len;
|
663
|
char[] characters = new char[512];
|
664
|
while ((len = returnStream.read(characters, 0, 512)) != -1) {
|
665
|
sw.write(characters, 0, len);
|
666
|
}
|
667
|
returnStream.close();
|
668
|
response = sw.toString();
|
669
|
sw.close();
|
670
|
} catch (Exception e) {
|
671
|
e.printStackTrace();
|
672
|
return null;
|
673
|
}
|
674
|
|
675
|
return response;
|
676
|
}
|
677
|
|
678
|
/**
|
679
|
* Send a request to Metacat
|
680
|
*
|
681
|
* @param prop
|
682
|
* the properties to be sent to Metacat
|
683
|
* @return InputStream as returned by Metacat
|
684
|
*/
|
685
|
public InputStream getMetacatInputStream(Properties prop) {
|
686
|
InputStream returnStream = null;
|
687
|
// Now contact metacat and send the request
|
688
|
try {
|
689
|
|
690
|
returnStream = metacat.sendParameters(prop);
|
691
|
return returnStream;
|
692
|
} catch (Exception e) {
|
693
|
e.printStackTrace(System.err);
|
694
|
|
695
|
}
|
696
|
return returnStream;
|
697
|
|
698
|
}
|
699
|
|
700
|
}
|