Project

General

Profile

1 2440 sgarg
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author$'
8
 *     '$Date$'
9
 * '$Revision$'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25
26
package edu.ucsb.nceas.metacattest;
27
28 5750 berkley
import java.io.InputStream;
29 5110 daigle
import java.io.InputStreamReader;
30 2440 sgarg
import java.io.Reader;
31
import java.io.StringReader;
32
import java.util.Calendar;
33
import java.util.Date;
34
import java.util.GregorianCalendar;
35
import java.util.SimpleTimeZone;
36
import java.util.TimeZone;
37
38 4145 daigle
import edu.ucsb.nceas.MCTestCase;
39 2440 sgarg
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
40
import edu.ucsb.nceas.metacat.client.Metacat;
41
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
42
import edu.ucsb.nceas.metacat.client.MetacatException;
43
import edu.ucsb.nceas.metacat.client.MetacatFactory;
44
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
45 5035 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
46 5752 leinfelder
import edu.ucsb.nceas.utilities.FileUtil;
47 2440 sgarg
import edu.ucsb.nceas.utilities.IOUtil;
48 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
49 2440 sgarg
import junit.framework.Test;
50
import junit.framework.TestSuite;
51
import java.io.File;
52
53 5750 berkley
import org.apache.commons.io.IOUtils;
54
55 2440 sgarg
/**
56
 * A JUnit test for testing Metacat when Non Ascii Characters are inserted
57
 */
58
public class NonAsciiCharacterTest
59 4145 daigle
    extends MCTestCase {
60 4080 daigle
61
    private static String metacatUrl;
62
    private static String username;
63
	private static String password;
64
	private static String anotheruser;
65
	private static String anotherpassword;
66
	static {
67
		try {
68 4231 daigle
		    metacatUrl = PropertyService.getProperty("test.metacatUrl");
69
			username = PropertyService.getProperty("test.mcUser");
70
			password = PropertyService.getProperty("test.mcPassword");
71
			anotheruser = PropertyService.getProperty("test.mcAnotherUser");
72
			anotherpassword = PropertyService.getProperty("test.mcAnotherPassword");
73 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
74
			System.err.println("Could not get property in static block: "
75
					+ pnfe.getMessage());
76
		}
77
	}
78
79 2440 sgarg
    private String prefix = "test";
80
    private String testdocument = "";
81
82
    private Metacat m;
83
84
    /**
85
     * These variables are for eml-2.0.1 only. For other eml versions,
86
     * this function might have to modified
87
     */
88
89 4356 daigle
    private String testEml_201_Header =
90 2440 sgarg
        "<?xml version=\"1.0\"?><eml:eml" +
91
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
92
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
93
        " packageId=\"eml.1.1\" system=\"knb\"" +
94
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
95
        " scope=\"system\">";
96 4356 daigle
97
    private String testEml_210_Header =
98
        "<?xml version=\"1.0\"?><eml:eml" +
99
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.0\"" +
100
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
101
        " packageId=\"eml.1.1\" system=\"knb\"" +
102
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.0 eml.xsd\"" +
103
        " scope=\"system\">";
104 2440 sgarg
105
    private String testEmlCreatorBlock =
106
        "<creator scope=\"document\">                                       " +
107
        " <individualName>                                                  " +
108
        "    <surName>Smith</surName>                                       " +
109
        " </individualName>                                                 " +
110
        "</creator>                                                         ";
111
112
    private String testEmlContactBlock =
113
        "<contact scope=\"document\">                                       " +
114
        " <individualName>                                                  " +
115
        "    <surName>Jackson</surName>                                     " +
116
        " </individualName>                                                 " +
117
        "</contact>                                                         ";
118
119
    /**
120
     * This function returns an access block based on the params passed
121
     */
122 4509 daigle
    protected String getAccessBlock(String principal, boolean grantAccess,
123 2440 sgarg
                                  boolean read, boolean write,
124
                                  boolean changePermission, boolean all) {
125
        String accessBlock = "<access " +
126
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
127
            " order=\"allowFirst\" scope=\"document\">";
128
129
        if (grantAccess) {
130
            accessBlock += "<allow>";
131
        }
132
        else {
133
            accessBlock += "<deny>";
134
        }
135
136
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
137
138
        if (all) {
139
            accessBlock += "<permission>all</permission>";
140
        }
141
        else {
142
            if (read) {
143
                accessBlock += "<permission>read</permission>";
144
            }
145
            if (write) {
146
                accessBlock += "<permission>write</permission>";
147
            }
148
            if (changePermission) {
149
                accessBlock += "<permission>changePermission</permission>";
150
            }
151
        }
152
153
        if (grantAccess) {
154
            accessBlock += "</allow>";
155
        }
156
        else {
157
            accessBlock += "</deny>";
158
        }
159
        accessBlock += "</access>";
160
161
        return accessBlock;
162
163
    }
164
165
    /**
166
     * Constructor to build the test
167
     *
168
     * @param name the name of the test method
169
     */
170
    public NonAsciiCharacterTest(String name) {
171
        super(name);
172
    }
173
174
    /**
175
     * Establish a testing framework by initializing appropriate objects
176
     */
177
    public void setUp() {
178
        try {
179
            System.err.println("Test Metacat: " + metacatUrl);
180
            m = MetacatFactory.createMetacatConnection(metacatUrl);
181
        }
182
        catch (MetacatInaccessibleException mie) {
183
            System.err.println("Metacat is: " + metacatUrl);
184
            fail("Metacat connection failed." + mie.getMessage());
185
        }
186
    }
187
188
    /**
189
     * Release any objects after tests are complete
190
     */
191
    public void tearDown() {
192
    }
193
194
    /**
195
     * Create a suite of tests to be run together
196
     */
197
    public static Test suite() {
198
        TestSuite suite = new TestSuite();
199
        suite.addTest(new NonAsciiCharacterTest("initialize"));
200
        // Test basic functions
201 5754 leinfelder
        suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters201Test"));
202 4356 daigle
        suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters210Test"));
203
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat201Test"));
204
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat210Test"));
205
        suite.addTest(new NonAsciiCharacterTest("quote201Test"));
206
        suite.addTest(new NonAsciiCharacterTest("quote210Test"));
207
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat201Test"));
208 5754 leinfelder
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat210Test"));
209
        suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter201Test"));
210
        suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter210Test"));
211 2440 sgarg
212
        return suite;
213
    }
214
215
    /**
216
     * Run an initial test that always passes to check that the test
217
     * harness is working.
218
     */
219
    public void initialize() {
220
        assertTrue(1 == 1);
221
    }
222
223 4356 daigle
    /**
224
     * Test inserting an EML 2.0.1 document with > & <
225 2440 sgarg
     * should fail because this means an invalid xml document is being inserted
226
     */
227 4356 daigle
    public void invalidXMLCharacters201Test() {
228
    	debug("\nRunning: invalidXMLCharacters201Test");
229 2440 sgarg
        try {
230 4356 daigle
            String newdocid = generateDocid();
231 2440 sgarg
            m.login(username, password);
232 4356 daigle
            testdocument = getTestEmlDoc("Checking > & < in doc: " + newdocid, EML2_0_1);
233 2440 sgarg
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
234
            m.logout();
235
        }
236
        catch (MetacatAuthException mae) {
237
            fail("Authorization failed:\n" + mae.getMessage());
238
        }
239
        catch (MetacatInaccessibleException mie) {
240
            fail("Metacat Inaccessible:\n" + mie.getMessage());
241
        }
242
        catch (Exception e) {
243
            fail("General exception:\n" + e.getMessage());
244
        }
245
    }
246 4356 daigle
247
    /**
248
     * Test inserting an EML 2.1.0 document with > & <
249 2440 sgarg
     * should fail because this means an invalid xml document is being inserted
250
     */
251 4356 daigle
    public void invalidXMLCharacters210Test() {
252
    	debug("\nRunning: invalidXMLCharacters210Test");
253 2440 sgarg
        try {
254 4356 daigle
            String newdocid = generateDocid();
255 2440 sgarg
            m.login(username, password);
256 4356 daigle
            testdocument = getTestEmlDoc("Checking > & < in doc: " + newdocid, EML2_1_0);
257
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
258
            m.logout();
259
        }
260
        catch (MetacatAuthException mae) {
261
            fail("Authorization failed:\n" + mae.getMessage());
262
        }
263
        catch (MetacatInaccessibleException mie) {
264
            fail("Metacat Inaccessible:\n" + mie.getMessage());
265
        }
266
        catch (Exception e) {
267
            fail("General exception:\n" + e.getMessage());
268
        }
269
    }
270
271
    /**
272
     * Test inserting and reading an EML 2.0.1 document with &gt; &amp; &lt;
273
     * Read should succeed since the same document should be read
274
     * back from disk that was submitted. Query should succeed as
275
     * well because the characters in this test are not changed in
276
     * the database.
277
     */
278
    public void symbolEncodedFormat201Test() {
279
    	debug("\nRunning: symbolEncodedFormat201Test");
280
        try {
281
            String newdocid = generateDocid();
282
            m.login(username, password);
283
284
            String testTitle =
285
            	"Checking &gt; &lt; &quot; &apos; &amp; in doc: " + newdocid  + ".1";
286
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
287
            insertDocid(newdocid  + ".1", testdocument, SUCCESS, false);
288
289
            // this tests reading the document back from disk
290 2448 sgarg
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
291 4356 daigle
292
            // this tests searching for the document in the database
293
            Thread.sleep(3000);
294
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_0_1, SUCCESS);
295
296 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
297
298 2440 sgarg
            m.logout();
299
        }
300
        catch (MetacatAuthException mae) {
301
            fail("Authorization failed:\n" + mae.getMessage());
302
        }
303
        catch (MetacatInaccessibleException mie) {
304
            fail("Metacat Inaccessible:\n" + mie.getMessage());
305
        }
306
        catch (Exception e) {
307
            fail("General exception:\n" + e.getMessage());
308
        }
309
    }
310 4356 daigle
311
    /**
312
     * Test inserting and reading an EML 2.1.0 document with &gt; &amp; &lt;
313
     * Read should succeed since the same document should be read
314
     * back from disk that was submitted. Query should succeed as
315
     * well because the characters in this test are not changed in
316
     * the database.
317
     */
318
    public void symbolEncodedFormat210Test() {
319
    	debug("\nRunning: symbolEncodedFormat210Test");
320
        try {
321
            String newdocid = generateDocid();
322
            m.login(username, password);
323
324
            String testTitle =
325
            	"Checking &gt; &lt; &quot; &apos; &amp; in doc: " + newdocid + ".1";
326
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
327
            insertDocid(newdocid  + ".1", testdocument, SUCCESS, false);
328
329
            // this tests reading the document back from disk
330
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
331
332
            // this tests searching for the document in the database
333
            Thread.sleep(3000);
334
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_1_0, SUCCESS);
335
336 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
337
338 4356 daigle
            m.logout();
339
        }
340
        catch (MetacatAuthException mae) {
341
            fail("Authorization failed:\n" + mae.getMessage());
342
        }
343
        catch (MetacatInaccessibleException mie) {
344
            fail("Metacat Inaccessible:\n" + mie.getMessage());
345
        }
346
        catch (Exception e) {
347
            fail("General exception:\n" + e.getMessage());
348
        }
349
    }
350 2440 sgarg
351 4356 daigle
    /**
352
     * Test inserting and reading an EML 2.0.1 document with single quote and double quote.
353
     * Read should succeed since the same document should be read back from disk
354
     * that was submitted.  Query should succeed because we look for the converted
355
     * character (&apos; and &quot;).
356 2440 sgarg
     */
357 4356 daigle
    public void quote201Test() {
358
    	debug("\nRunning: quote201Test");
359 2440 sgarg
        try {
360 4356 daigle
            String newdocid = generateDocid();
361 2440 sgarg
            m.login(username, password);
362 4356 daigle
363
            String testTitle = "Checking ' ` \" in doc: " + newdocid  + ".1";
364
            String convertedTestTitle = "Checking &apos; ` &quot; in doc: " + newdocid  + ".1";
365
366
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
367 3760 tao
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
368 4356 daigle
369
            // this tests reading the document back from disk
370 2440 sgarg
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
371 4356 daigle
372
            // this tests searching for the document in the database
373
            Thread.sleep(3000);
374
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_0_1, SUCCESS);
375
376 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
377
378 2440 sgarg
            m.logout();
379
        }
380
        catch (MetacatAuthException mae) {
381
            fail("Authorization failed:\n" + mae.getMessage());
382
        }
383
        catch (MetacatInaccessibleException mie) {
384
            fail("Metacat Inaccessible:\n" + mie.getMessage());
385
        }
386
        catch (Exception e) {
387
            fail("General exception:\n" + e.getMessage());
388
        }
389
    }
390 4356 daigle
391
    /**
392
     * Test inserting and reading an EML 2.1.0 document with single quote and double quote.
393
     * Read should succeed since the same document should be read back from disk
394
     * that was submitted.  Query shoud succeed because we look for the converted
395
     * character (&apos; and &quot;).
396
     */
397
    public void quote210Test() {
398
    	debug("\nRunning: quote210Test");
399
        try {
400
            String newdocid = generateDocid();
401
            m.login(username, password);
402
403
            String testTitle = "Checking ' ` \" in doc: " + newdocid  + ".1";
404
            String convertedTestTitle = "Checking &apos; ` &quot; in doc: " + newdocid  + ".1";
405
406
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
407
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
408
409
            // this tests reading the document back from disk
410
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
411
412
            // this tests searching for the document in the database
413
            Thread.sleep(3000);
414
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_1_0, SUCCESS);
415
416 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
417
418 4356 daigle
            m.logout();
419
        }
420
        catch (MetacatAuthException mae) {
421
            fail("Authorization failed:\n" + mae.getMessage());
422
        }
423
        catch (MetacatInaccessibleException mie) {
424
            fail("Metacat Inaccessible:\n" + mie.getMessage());
425
        }
426
        catch (Exception e) {
427
            fail("General exception:\n" + e.getMessage());
428
        }
429
    }
430 2440 sgarg
431 4356 daigle
    /**
432
     * Test inserting and reading an EML 2.0.1 document with the code representation
433
     * of a micro sign (&#181). Read should succeed since the same document should be
434
     * read back from disk that was submitted.  Query should succeed because we look
435 5752 leinfelder
     * for the converted character (µ).
436 4356 daigle
     */
437
    public void numericCharacterReferenceFormat201Test() {
438
    	debug("\nRunning: numericCharacterReferenceFormat201Test");
439
        try {
440
            String newdocid = generateDocid();
441
            m.login(username, password);
442
443
            String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
444 5752 leinfelder
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
445 4356 daigle
446
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
447
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
448 2440 sgarg
449 4356 daigle
            // this tests reading the document back from disk
450
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
451
452
            // this tests searching for the document in the database
453
            Thread.sleep(3000);
454
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_0_1, SUCCESS);
455 4407 daigle
456
            deleteDocid(newdocid  + ".1", SUCCESS, false);
457 4356 daigle
458
            m.logout();
459
        }
460
        catch (MetacatAuthException mae) {
461
            fail("Authorization failed:\n" + mae.getMessage());
462
        }
463
        catch (MetacatInaccessibleException mie) {
464
            fail("Metacat Inaccessible:\n" + mie.getMessage());
465
        }
466
        catch (Exception e) {
467
            fail("General exception:\n" + e.getMessage());
468
        }
469
    }
470 2440 sgarg
471 4356 daigle
    /**
472
     * Test inserting and reading an EML 2.1.0 document with the code representation
473
     * of a micro sign (&#181). Read should succeed since the same document should be
474
     * read back from disk that was submitted.  Query should succeed because we look
475 5752 leinfelder
     * for the converted character (µ).
476 2440 sgarg
     */
477 4356 daigle
    public void numericCharacterReferenceFormat210Test() {
478
    	debug("\nRunning: numericCharacterReferenceFormat210Test");
479 2440 sgarg
        try {
480 4356 daigle
            String newdocid = generateDocid();
481 2440 sgarg
            m.login(username, password);
482 4356 daigle
483
            String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
484 5752 leinfelder
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
485 4356 daigle
486
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
487 3760 tao
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
488 4356 daigle
489
            // this tests reading the document back from disk
490 2440 sgarg
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
491 4356 daigle
492
            // this tests searching for the document in the database
493
            Thread.sleep(3000);
494
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_1_0, SUCCESS);
495
496 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
497
498 2440 sgarg
            m.logout();
499
        }
500
        catch (MetacatAuthException mae) {
501
            fail("Authorization failed:\n" + mae.getMessage());
502
        }
503
        catch (MetacatInaccessibleException mie) {
504
            fail("Metacat Inaccessible:\n" + mie.getMessage());
505
        }
506
        catch (Exception e) {
507
            fail("General exception:\n" + e.getMessage());
508
        }
509
    }
510
511 4356 daigle
    /**
512 5752 leinfelder
     * Test inserting and reading an EML 2.0.1 document with the micro sign (µ).
513 4356 daigle
     * Read should succeed since the same document should be read back from disk
514
     * that was submitted.  Query should succeed because we look for the same
515 5752 leinfelder
     * character (µ).
516 2448 sgarg
     */
517 4356 daigle
    public void nonLatinUnicodeCharacter201Test() {
518
    	debug("\nRunning: nonLatinUnicodeCharacter201Test");
519 2448 sgarg
        try {
520 4356 daigle
            String newdocid = generateDocid();
521 2448 sgarg
            m.login(username, password);
522 4356 daigle
523
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
524
525
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
526 5752 leinfelder
527
            debug("original test document:	" + testdocument);
528
529 2448 sgarg
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
530 4356 daigle
531
            // this tests reading the document back from disk
532 2448 sgarg
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
533 4356 daigle
534
            // this tests searching for the document in the database
535
            Thread.sleep(3000);
536
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_0_1, SUCCESS);
537
538 5754 leinfelder
            deleteDocid(newdocid  + ".1", SUCCESS, false);
539 4407 daigle
540 2448 sgarg
            m.logout();
541
        }
542
        catch (MetacatAuthException mae) {
543
            fail("Authorization failed:\n" + mae.getMessage());
544
        }
545
        catch (MetacatInaccessibleException mie) {
546
            fail("Metacat Inaccessible:\n" + mie.getMessage());
547
        }
548
        catch (Exception e) {
549
            fail("General exception:\n" + e.getMessage());
550
        }
551
    }
552 4356 daigle
553
    /**
554 5744 berkley
     * Test inserting and reading an EML 2.1.0 document with the micro sign (µ).
555 4356 daigle
     * Read should succeed since the same document should be read back from disk
556
     * that was submitted.  Query should succeed because we look for the same
557 5744 berkley
     * character (µ).
558 4356 daigle
     */
559
    public void nonLatinUnicodeCharacter210Test() {
560
    	debug("\nRunning: nonLatinUnicodeCharacter210Test");
561
        try {
562
            String newdocid = generateDocid();
563
            m.login(username, password);
564
565 5752 leinfelder
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
566 4356 daigle
567
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
568
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
569 2448 sgarg
570 4356 daigle
            // this tests reading the document back from disk
571
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
572
573
            // this tests searching for the document in the database
574
            Thread.sleep(3000);
575
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_1_0, SUCCESS);
576
577 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
578
579 4356 daigle
            m.logout();
580
        }
581
        catch (MetacatAuthException mae) {
582
            fail("Authorization failed:\n" + mae.getMessage());
583
        }
584
        catch (MetacatInaccessibleException mie) {
585
            fail("Metacat Inaccessible:\n" + mie.getMessage());
586
        }
587
        catch (Exception e) {
588
            fail("General exception:\n" + e.getMessage());
589
        }
590
    }
591
592 2440 sgarg
    /**
593
     * Insert a document into metacat. The expected result is passed as result
594
     */
595
    private String insertDocid(String docid, String docText, boolean result,
596
                               boolean expectMetacatException) {
597
        String response = null;
598
        try {
599 5752 leinfelder
600
        	debug("doctext: " + docText);
601
602 2440 sgarg
            response = m.insert(docid,
603
                                new StringReader(docText), null);
604
            System.err.println(response);
605
            if (result) {
606
                assertTrue( (response.indexOf("<success>") != -1));
607
                assertTrue(response.indexOf(docid) != -1);
608
            }
609
            else {
610
                assertTrue( (response.indexOf("<success>") == -1));
611
            }
612
        }
613
        catch (MetacatInaccessibleException mie) {
614
            fail("Metacat Inaccessible:\n" + mie.getMessage());
615
        }
616
        catch (InsufficientKarmaException ike) {
617
                fail("Insufficient karma:\n" + ike.getMessage());
618
        }
619
        catch (MetacatException me) {
620
            if (!expectMetacatException) {
621
                fail("Metacat Error:\n" + me.getMessage());
622
            }
623
        }
624
        catch (Exception e) {
625
            fail("General exception:\n" + e.getMessage());
626
        }
627
        return response;
628
    }
629
630
    /**
631
     * Insert a document into metacat. The expected result is passed as result
632
     */
633
    private String uploadDocid(String docid, String filePath, boolean result,
634
                               boolean expectedKarmaException) {
635
        String response = null;
636
        try {
637
            response = m.upload(docid, new File(filePath));
638
            if (result) {
639
                assertTrue( (response.indexOf("<success>") != -1));
640
                assertTrue(response.indexOf(docid) != -1);
641
            }
642
            else {
643
                assertTrue( (response.indexOf("<success>") == -1));
644
            }
645
            System.err.println("respose from metacat: " + response);
646
        }
647
        catch (MetacatInaccessibleException mie) {
648
            fail("Metacat Inaccessible:\n" + mie.getMessage());
649
        }
650
        catch (InsufficientKarmaException ike) {
651
            if (!expectedKarmaException) {
652
                fail("Insufficient karma:\n" + ike.getMessage());
653
            }
654
        }
655
        catch (MetacatException me) {
656
            if (result) {
657
                fail("Metacat Error:\n" + me.getMessage());
658
            } else {
659
                System.err.println("Metacat Error:\n" + me.getMessage());
660
            }
661
        }
662
        catch (Exception e) {
663
            fail("General exception:\n" + e.getMessage());
664
        }
665
        return response;
666
    }
667
668
    /**
669
     * Update a document in metacat. The expected result is passed as result
670
     */
671
    private String updateDocid(String docid, String docText, boolean result,
672
                               boolean expectedKarmaFailure) {
673
        String response = null;
674
        try {
675
            response = m.update(docid,
676
                                new StringReader(testdocument), null);
677
678
            if (result) {
679
                assertTrue( (response.indexOf("<success>") != -1));
680
                assertTrue(response.indexOf(docid) != -1);
681
            }
682
            else {
683
                assertTrue( (response.indexOf("<success>") == -1));
684
            }
685
            System.err.println(response);
686
        }
687
        catch (MetacatInaccessibleException mie) {
688
            fail("Metacat Inaccessible:\n" + mie.getMessage());
689
        }
690
        catch (InsufficientKarmaException ike) {
691
            if (!expectedKarmaFailure) {
692
                fail("Insufficient karma:\n" + ike.getMessage());
693
            }
694
        }
695
        catch (MetacatException me) {
696
            if (result) {
697
                fail("Metacat Error:\n" + me.getMessage());
698
            } else {
699
                System.err.println("Metacat Error:\n" + me.getMessage());
700
            }
701
        }
702
        catch (Exception e) {
703
            fail("General exception:\n" + e.getMessage());
704
        }
705
706
        return response;
707
    }
708
709
    /**
710 4356 daigle
     * Delete a document from metacat. The expected result is passed as result
711 2440 sgarg
     */
712
    private void deleteDocid(String docid, boolean result,
713
                             boolean expextedKarmaFailure) {
714
        try {
715
            String response = m.delete(docid);
716
            if (result) {
717
                assertTrue(response.indexOf("<success>") != -1);
718
            }
719
            else {
720
                assertTrue(response.indexOf("<success>") == -1);
721
            }
722
            System.err.println(response);
723
        }
724
        catch (MetacatInaccessibleException mie) {
725
            fail("Metacat Inaccessible:\n" + mie.getMessage());
726
        }
727
        catch (InsufficientKarmaException ike) {
728
            if(!expextedKarmaFailure){
729
                fail("Insufficient karma:\n" + ike.getMessage());
730
            }
731
        }
732
        catch (MetacatException me) {
733
            if (result) {
734
                fail("Metacat Error:\n" + me.getMessage());
735
            } else {
736
                System.err.println("Metacat Error:\n" + me.getMessage());
737
            }
738
        }
739
        catch (Exception e) {
740
            fail("General exception:\n" + e.getMessage());
741
        }
742
    }
743
744
    /**
745
     * Read a document from metacat. The expected result is passed as result
746
     */
747
    private void readDocid(String docid, boolean result,
748
                           boolean expextedKarmaFailure) {
749
        try {
750 5110 daigle
            Reader r = new InputStreamReader(m.read(docid));
751 2440 sgarg
            String response = IOUtil.getAsString(r, true);
752
753
            if (!result) {
754
                assertTrue(response.indexOf("<success>") == -1);
755
            }
756
            // System.err.println(response);
757
        }
758
        catch (MetacatInaccessibleException mie) {
759
            fail("Metacat Inaccessible:\n" + mie.getMessage());
760
        }
761
        catch (InsufficientKarmaException ike) {
762
            if (!expextedKarmaFailure) {
763
                fail("Insufficient karma:\n" + ike.getMessage());
764
            }
765
        }
766
        catch (MetacatException me) {
767
            fail("Metacat Error:\n" + me.getMessage());
768
        }
769
        catch (Exception e) {
770
            fail("General exception:\n" + e.getMessage());
771
        }
772
    }
773
774
    /**
775
     * Read a document from metacat and check if it is equal to a given string.
776
     * The expected result is passed as result
777
     */
778
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
779
                                         boolean result,
780 4356 daigle
                                         boolean expectedKarmaFailure) {
781 2440 sgarg
        try {
782 5750 berkley
            Reader r = new InputStreamReader(m.read(docid), "UTF-8");
783
            //InputStream is = m.read(docid);
784 2440 sgarg
            String doc = IOUtil.getAsString(r, true);
785 5750 berkley
            //String doc = IOUtils.toString(is);
786
787 2440 sgarg
            if (result) {
788
789
                if (!testDoc.equals(doc)) {
790 4360 daigle
                    debug("doc    :" + doc);
791
                    debug("testDoc:" + testDoc);
792 2440 sgarg
                }
793
794
                assertTrue(testDoc.equals(doc));
795
            }
796
            else {
797
                assertTrue(doc.indexOf("<error>") != -1);
798
            }
799
        }
800
        catch (MetacatInaccessibleException mie) {
801
            fail("Metacat Inaccessible:\n" + mie.getMessage());
802
        }
803
        catch (InsufficientKarmaException ike) {
804 4356 daigle
            if (!expectedKarmaFailure) {
805 2440 sgarg
                fail("Insufficient karma:\n" + ike.getMessage());
806
            }
807
        }
808
        catch (MetacatException me) {
809
            fail("Metacat Error:\n" + me.getMessage());
810
        }
811
        catch (Exception e) {
812
            fail("General exception:\n" + e.getMessage());
813
        }
814
815
    }
816
817
    /**
818
     * Create a hopefully unique docid for testing insert and update. Does
819
     * not include the 'revision' part of the id.
820
     *
821
     * @return a String docid based on the current date and time
822
     */
823
    private String generateDocid() {
824
        StringBuffer docid = new StringBuffer(prefix);
825
        docid.append(".");
826
827
        // Create a calendar to get the date formatted properly
828
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
829
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
830
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
831
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
832
                       2 * 60 * 60 * 1000);
833
        Calendar calendar = new GregorianCalendar(pdt);
834
        Date trialTime = new Date();
835
        calendar.setTime(trialTime);
836
        docid.append(calendar.get(Calendar.YEAR));
837
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
838
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
839
        docid.append(calendar.get(Calendar.MINUTE));
840
        docid.append(calendar.get(Calendar.SECOND));
841 3584 tao
   	    //sometimes this number is not unique, so we append a random number
842
    	int random = (new Double(Math.random()*100)).intValue();
843
    	docid.append(random);
844 2440 sgarg
        return docid.toString();
845
    }
846
}