Project

General

Profile

1
/**
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: leinfelder $'
8
 *     '$Date: 2012-01-03 15:38:02 -0800 (Tue, 03 Jan 2012) $'
9
 * '$Revision: 6829 $'
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
import java.io.File;
29
import java.io.InputStreamReader;
30
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
import junit.framework.Test;
39
import junit.framework.TestSuite;
40
import edu.ucsb.nceas.MCTestCase;
41
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
42
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
43
import edu.ucsb.nceas.metacat.client.MetacatException;
44
import edu.ucsb.nceas.metacat.client.MetacatFactory;
45
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
46
import edu.ucsb.nceas.metacat.properties.PropertyService;
47
import edu.ucsb.nceas.utilities.IOUtil;
48
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
49

    
50
/**
51
 * A JUnit test for testing Metacat when Non Ascii Characters are inserted
52
 */
53
public class NonAsciiCharacterTest
54
    extends MCTestCase {
55

    
56
	static {
57
		try {
58
		    metacatUrl = PropertyService.getProperty("test.metacatUrl");
59
			username = PropertyService.getProperty("test.mcUser");
60
			password = PropertyService.getProperty("test.mcPassword");
61
			anotheruser = PropertyService.getProperty("test.mcAnotherUser");
62
			anotherpassword = PropertyService.getProperty("test.mcAnotherPassword");
63
		} catch (PropertyNotFoundException pnfe) {
64
			System.err.println("Could not get property in static block: " 
65
					+ pnfe.getMessage());
66
		}
67
	}
68

    
69
    /**
70
     * These variables are for eml-2.0.1 only. For other eml versions,
71
     * this function might have to modified
72
     */
73

    
74
    private String testEml_201_Header =
75
        "<?xml version=\"1.0\"?><eml:eml" +
76
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
77
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
78
        " packageId=\"eml.1.1\" system=\"knb\"" +
79
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
80
        " scope=\"system\">";
81
    
82
    private String testEml_210_Header =
83
        "<?xml version=\"1.0\"?><eml:eml" +
84
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.0\"" +
85
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
86
        " packageId=\"eml.1.1\" system=\"knb\"" +
87
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.0 eml.xsd\"" +
88
        " scope=\"system\">";
89

    
90
    private String testEmlCreatorBlock =
91
        "<creator scope=\"document\">                                       " +
92
        " <individualName>                                                  " +
93
        "    <surName>Smith</surName>                                       " +
94
        " </individualName>                                                 " +
95
        "</creator>                                                         ";
96

    
97
    private String testEmlContactBlock =
98
        "<contact scope=\"document\">                                       " +
99
        " <individualName>                                                  " +
100
        "    <surName>Jackson</surName>                                     " +
101
        " </individualName>                                                 " +
102
        "</contact>                                                         ";
103

    
104
    /**
105
     * This function returns an access block based on the params passed
106
     */
107
    protected String getAccessBlock(String principal, boolean grantAccess,
108
                                  boolean read, boolean write,
109
                                  boolean changePermission, boolean all) {
110
        String accessBlock = "<access " +
111
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
112
            " order=\"allowFirst\" scope=\"document\">";
113

    
114
        if (grantAccess) {
115
            accessBlock += "<allow>";
116
        }
117
        else {
118
            accessBlock += "<deny>";
119
        }
120

    
121
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
122

    
123
        if (all) {
124
            accessBlock += "<permission>all</permission>";
125
        }
126
        else {
127
            if (read) {
128
                accessBlock += "<permission>read</permission>";
129
            }
130
            if (write) {
131
                accessBlock += "<permission>write</permission>";
132
            }
133
            if (changePermission) {
134
                accessBlock += "<permission>changePermission</permission>";
135
            }
136
        }
137

    
138
        if (grantAccess) {
139
            accessBlock += "</allow>";
140
        }
141
        else {
142
            accessBlock += "</deny>";
143
        }
144
        accessBlock += "</access>";
145

    
146
        return accessBlock;
147

    
148
    }
149

    
150
    /**
151
     * Constructor to build the test
152
     *
153
     * @param name the name of the test method
154
     */
155
    public NonAsciiCharacterTest(String name) {
156
        super(name);
157
    }
158

    
159
    /**
160
     * Establish a testing framework by initializing appropriate objects
161
     */
162
    public void setUp() {
163
        try {
164
            System.err.println("Test Metacat: " + metacatUrl);
165
            m = MetacatFactory.createMetacatConnection(metacatUrl);
166
        }
167
        catch (MetacatInaccessibleException mie) {
168
            System.err.println("Metacat is: " + metacatUrl);
169
            fail("Metacat connection failed." + mie.getMessage());
170
        }
171
    }
172

    
173
    /**
174
     * Release any objects after tests are complete
175
     */
176
    public void tearDown() {
177
    }
178

    
179
    /**
180
     * Create a suite of tests to be run together
181
     */
182
    public static Test suite() {
183
        TestSuite suite = new TestSuite();
184
        suite.addTest(new NonAsciiCharacterTest("initialize"));
185
        // Test basic functions
186
        suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters201Test"));
187
        suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters210Test"));
188
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat201Test"));
189
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat210Test"));
190
        suite.addTest(new NonAsciiCharacterTest("quote201Test"));
191
        suite.addTest(new NonAsciiCharacterTest("quote210Test"));
192
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat201Test"));
193
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat210Test"));
194
        suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter201Test"));
195
        suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter210Test"));
196

    
197
        return suite;
198
    }
199

    
200
    /**
201
     * Run an initial test that always passes to check that the test
202
     * harness is working.
203
     */
204
    public void initialize() {
205
        assertTrue(1 == 1);
206
    }
207

    
208
    /**
209
     * Test inserting an EML 2.0.1 document with > & <
210
     * should fail because this means an invalid xml document is being inserted
211
     */
212
    public void invalidXMLCharacters201Test() {
213
    	debug("\nRunning: invalidXMLCharacters201Test");
214
        try {
215
            String newdocid = generateDocid();
216
            m.login(username, password);
217
            testdocument = getTestEmlDoc("Checking > & < in doc: " + newdocid, EML2_0_1);
218
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
219
            m.logout();
220
        }
221
        catch (MetacatAuthException mae) {
222
            fail("Authorization failed:\n" + mae.getMessage());
223
        }
224
        catch (MetacatInaccessibleException mie) {
225
            fail("Metacat Inaccessible:\n" + mie.getMessage());
226
        }
227
        catch (Exception e) {
228
            fail("General exception:\n" + e.getMessage());
229
        }
230
    }
231
    
232
    /**
233
     * Test inserting an EML 2.1.0 document with > & <
234
     * should fail because this means an invalid xml document is being inserted
235
     */
236
    public void invalidXMLCharacters210Test() {
237
    	debug("\nRunning: invalidXMLCharacters210Test");
238
        try {
239
            String newdocid = generateDocid();
240
            m.login(username, password);
241
            testdocument = getTestEmlDoc("Checking > & < in doc: " + newdocid, EML2_1_0);
242
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
243
            m.logout();
244
        }
245
        catch (MetacatAuthException mae) {
246
            fail("Authorization failed:\n" + mae.getMessage());
247
        }
248
        catch (MetacatInaccessibleException mie) {
249
            fail("Metacat Inaccessible:\n" + mie.getMessage());
250
        }
251
        catch (Exception e) {
252
            fail("General exception:\n" + e.getMessage());
253
        }
254
    }
255

    
256
    /**
257
     * Test inserting and reading an EML 2.0.1 document with &gt; &amp; &lt;
258
     * Read should succeed since the same document should be read 
259
     * back from disk that was submitted. Query should succeed as 
260
     * well because the characters in this test are not changed in
261
     * the database.
262
     */
263
    public void symbolEncodedFormat201Test() {
264
    	debug("\nRunning: symbolEncodedFormat201Test");
265
        try {
266
            String newdocid = generateDocid();
267
            m.login(username, password);
268
            
269
            String testTitle = 
270
            	"Checking &gt; &lt; &quot; &apos; &amp; in doc: " + newdocid  + ".1";
271
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
272
            insertDocid(newdocid  + ".1", testdocument, SUCCESS, false);
273
            
274
            // this tests reading the document back from disk
275
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
276
            
277
            // this tests searching for the document in the database
278
            Thread.sleep(3000);
279
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_0_1, SUCCESS);
280
            
281
            deleteDocid(newdocid  + ".1", SUCCESS, false);
282
            
283
            m.logout();
284
        }
285
        catch (MetacatAuthException mae) {
286
            fail("Authorization failed:\n" + mae.getMessage());
287
        }
288
        catch (MetacatInaccessibleException mie) {
289
            fail("Metacat Inaccessible:\n" + mie.getMessage());
290
        }
291
        catch (Exception e) {
292
            fail("General exception:\n" + e.getMessage());
293
        }
294
    }
295
    
296
    /**
297
     * Test inserting and reading an EML 2.1.0 document with &gt; &amp; &lt;
298
     * Read should succeed since the same document should be read 
299
     * back from disk that was submitted. Query should succeed as 
300
     * well because the characters in this test are not changed in
301
     * the database.
302
     */
303
    public void symbolEncodedFormat210Test() {
304
    	debug("\nRunning: symbolEncodedFormat210Test");
305
        try {
306
            String newdocid = generateDocid();
307
            m.login(username, password);
308
            
309
            String testTitle = 
310
            	"Checking &gt; &lt; &quot; &apos; &amp; in doc: " + newdocid + ".1";
311
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
312
            insertDocid(newdocid  + ".1", testdocument, SUCCESS, false);
313
            
314
            // this tests reading the document back from disk
315
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
316
            
317
            // this tests searching for the document in the database
318
            Thread.sleep(3000);
319
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_1_0, SUCCESS);
320
            
321
            deleteDocid(newdocid  + ".1", SUCCESS, false);
322
            
323
            m.logout();
324
        }
325
        catch (MetacatAuthException mae) {
326
            fail("Authorization failed:\n" + mae.getMessage());
327
        }
328
        catch (MetacatInaccessibleException mie) {
329
            fail("Metacat Inaccessible:\n" + mie.getMessage());
330
        }
331
        catch (Exception e) {
332
            fail("General exception:\n" + e.getMessage());
333
        }
334
    }
335

    
336
    /**
337
     * Test inserting and reading an EML 2.0.1 document with single quote and double quote.
338
     * Read should succeed since the same document should be read back from disk 
339
     * that was submitted.  Query should succeed because we look for the converted
340
     * character (&apos; and &quot;).
341
     */
342
    public void quote201Test() {
343
    	debug("\nRunning: quote201Test");
344
        try {
345
            String newdocid = generateDocid();
346
            m.login(username, password);
347
            
348
            String testTitle = "Checking ' ` \" in doc: " + newdocid  + ".1";
349
            String convertedTestTitle = "Checking &apos; ` &quot; in doc: " + newdocid  + ".1";
350
            
351
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
352
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
353
            
354
            // this tests reading the document back from disk
355
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
356
            
357
            // this tests searching for the document in the database
358
            Thread.sleep(3000);
359
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_0_1, SUCCESS);
360
            
361
            deleteDocid(newdocid  + ".1", SUCCESS, false);
362
            
363
            m.logout();
364
        }
365
        catch (MetacatAuthException mae) {
366
            fail("Authorization failed:\n" + mae.getMessage());
367
        }
368
        catch (MetacatInaccessibleException mie) {
369
            fail("Metacat Inaccessible:\n" + mie.getMessage());
370
        }
371
        catch (Exception e) {
372
            fail("General exception:\n" + e.getMessage());
373
        }
374
    }
375
    
376
    /**
377
     * Test inserting and reading an EML 2.1.0 document with single quote and double quote.
378
     * Read should succeed since the same document should be read back from disk 
379
     * that was submitted.  Query shoud succeed because we look for the converted
380
     * character (&apos; and &quot;).
381
     */
382
    public void quote210Test() {
383
    	debug("\nRunning: quote210Test");
384
        try {
385
            String newdocid = generateDocid();
386
            m.login(username, password);
387
            
388
            String testTitle = "Checking ' ` \" in doc: " + newdocid  + ".1";
389
            String convertedTestTitle = "Checking &apos; ` &quot; in doc: " + newdocid  + ".1";
390
            
391
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
392
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
393
            
394
            // this tests reading the document back from disk
395
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
396
            
397
            // this tests searching for the document in the database
398
            Thread.sleep(3000);
399
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_1_0, SUCCESS);
400
            
401
            deleteDocid(newdocid  + ".1", SUCCESS, false);
402
            
403
            m.logout();
404
        }
405
        catch (MetacatAuthException mae) {
406
            fail("Authorization failed:\n" + mae.getMessage());
407
        }
408
        catch (MetacatInaccessibleException mie) {
409
            fail("Metacat Inaccessible:\n" + mie.getMessage());
410
        }
411
        catch (Exception e) {
412
            fail("General exception:\n" + e.getMessage());
413
        }
414
    }
415

    
416
    /**
417
     * Test inserting and reading an EML 2.0.1 document with the code representation 
418
     * of a micro sign (&#181). Read should succeed since the same document should be 
419
     * read back from disk that was submitted.  Query should succeed because we look 
420
     * for the converted character (µ).
421
     */
422
    public void numericCharacterReferenceFormat201Test() {
423
    	debug("\nRunning: numericCharacterReferenceFormat201Test");
424
        try {
425
            String newdocid = generateDocid();
426
            m.login(username, password);
427
            
428
            String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
429
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
430
            
431
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
432
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
433

    
434
            // this tests reading the document back from disk
435
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
436
            
437
            // this tests searching for the document in the database
438
            Thread.sleep(3000);
439
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_0_1, SUCCESS);
440
            
441
            deleteDocid(newdocid  + ".1", SUCCESS, false);
442
  
443
            m.logout();
444
        }
445
        catch (MetacatAuthException mae) {
446
            fail("Authorization failed:\n" + mae.getMessage());
447
        }
448
        catch (MetacatInaccessibleException mie) {
449
            fail("Metacat Inaccessible:\n" + mie.getMessage());
450
        }
451
        catch (Exception e) {
452
            fail("General exception:\n" + e.getMessage());
453
        }
454
    }
455

    
456
    /**
457
     * Test inserting and reading an EML 2.1.0 document with the code representation 
458
     * of a micro sign (&#181). Read should succeed since the same document should be 
459
     * read back from disk that was submitted.  Query should succeed because we look 
460
     * for the converted character (µ).
461
     */
462
    public void numericCharacterReferenceFormat210Test() {
463
    	debug("\nRunning: numericCharacterReferenceFormat210Test");
464
        try {
465
            String newdocid = generateDocid();
466
            m.login(username, password);
467
            
468
            String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
469
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
470
            
471
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
472
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
473
            
474
            // this tests reading the document back from disk
475
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
476
            
477
            // this tests searching for the document in the database
478
            Thread.sleep(3000);
479
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_1_0, SUCCESS);
480
            
481
            deleteDocid(newdocid  + ".1", SUCCESS, false);
482
            
483
            m.logout();
484
        }
485
        catch (MetacatAuthException mae) {
486
            fail("Authorization failed:\n" + mae.getMessage());
487
        }
488
        catch (MetacatInaccessibleException mie) {
489
            fail("Metacat Inaccessible:\n" + mie.getMessage());
490
        }
491
        catch (Exception e) {
492
            fail("General exception:\n" + e.getMessage());
493
        }
494
    }
495

    
496
    /**
497
     * Test inserting and reading an EML 2.0.1 document with the micro sign (µ). 
498
     * Read should succeed since the same document should be read back from disk 
499
     * that was submitted.  Query should succeed because we look for the same 
500
     * character (µ).
501
     */
502
    public void nonLatinUnicodeCharacter201Test() {
503
    	debug("\nRunning: nonLatinUnicodeCharacter201Test");
504
        try {
505
            String newdocid = generateDocid();
506
            m.login(username, password);
507
            
508
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
509
            
510
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
511
            
512
            debug("original test document:	" + testdocument);
513
            
514
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
515
            
516
            // this tests reading the document back from disk
517
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
518
            
519
            // this tests searching for the document in the database
520
            Thread.sleep(3000);
521
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_0_1, SUCCESS);
522
            
523
            deleteDocid(newdocid  + ".1", SUCCESS, false);
524
            
525
            m.logout();
526
        }
527
        catch (MetacatAuthException mae) {
528
            fail("Authorization failed:\n" + mae.getMessage());
529
        }
530
        catch (MetacatInaccessibleException mie) {
531
            fail("Metacat Inaccessible:\n" + mie.getMessage());
532
        }
533
        catch (Exception e) {
534
            fail("General exception:\n" + e.getMessage());
535
        }
536
    }
537
    
538
    /**
539
     * Test inserting and reading an EML 2.1.0 document with the micro sign (µ). 
540
     * Read should succeed since the same document should be read back from disk 
541
     * that was submitted.  Query should succeed because we look for the same 
542
     * character (µ).
543
     */
544
    public void nonLatinUnicodeCharacter210Test() {
545
    	debug("\nRunning: nonLatinUnicodeCharacter210Test");
546
        try {
547
            String newdocid = generateDocid();
548
            m.login(username, password);
549
            
550
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
551
            
552
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
553
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
554

    
555
            // this tests reading the document back from disk
556
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
557
            
558
            // this tests searching for the document in the database
559
            Thread.sleep(3000);
560
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_1_0, SUCCESS);
561
            
562
            deleteDocid(newdocid  + ".1", SUCCESS, false);
563
            
564
            m.logout();
565
        }
566
        catch (MetacatAuthException mae) {
567
            fail("Authorization failed:\n" + mae.getMessage());
568
        }
569
        catch (MetacatInaccessibleException mie) {
570
            fail("Metacat Inaccessible:\n" + mie.getMessage());
571
        }
572
        catch (Exception e) {
573
            fail("General exception:\n" + e.getMessage());
574
        }
575
    }
576

    
577
    /**
578
     * Insert a document into metacat. The expected result is passed as result
579
     */
580
    private String insertDocid(String docid, String docText, boolean result,
581
                               boolean expectMetacatException) {
582
        String response = null;
583
        try {
584
        	
585
        	debug("doctext: " + docText);
586
        	
587
            response = m.insert(docid,
588
                                new StringReader(docText), null);
589
            System.err.println(response);
590
            if (result) {
591
                assertTrue( (response.indexOf("<success>") != -1));
592
                assertTrue(response.indexOf(docid) != -1);
593
            }
594
            else {
595
                assertTrue( (response.indexOf("<success>") == -1));
596
            }
597
        }
598
        catch (MetacatInaccessibleException mie) {
599
            fail("Metacat Inaccessible:\n" + mie.getMessage());
600
        }
601
        catch (InsufficientKarmaException ike) {
602
                fail("Insufficient karma:\n" + ike.getMessage());
603
        }
604
        catch (MetacatException me) {
605
            if (!expectMetacatException) {
606
                fail("Metacat Error:\n" + me.getMessage());
607
            }
608
        }
609
        catch (Exception e) {
610
            fail("General exception:\n" + e.getMessage());
611
        }
612
        return response;
613
    }
614

    
615
    /**
616
     * Insert a document into metacat. The expected result is passed as result
617
     */
618
    private String uploadDocid(String docid, String filePath, boolean result,
619
                               boolean expectedKarmaException) {
620
        String response = null;
621
        try {
622
            response = m.upload(docid, new File(filePath));
623
            if (result) {
624
                assertTrue( (response.indexOf("<success>") != -1));
625
                assertTrue(response.indexOf(docid) != -1);
626
            }
627
            else {
628
                assertTrue( (response.indexOf("<success>") == -1));
629
            }
630
            System.err.println("respose from metacat: " + response);
631
        }
632
        catch (MetacatInaccessibleException mie) {
633
            fail("Metacat Inaccessible:\n" + mie.getMessage());
634
        }
635
        catch (InsufficientKarmaException ike) {
636
            if (!expectedKarmaException) {
637
                fail("Insufficient karma:\n" + ike.getMessage());
638
            }
639
        }
640
        catch (MetacatException me) {
641
            if (result) {
642
                fail("Metacat Error:\n" + me.getMessage());
643
            } else {
644
                System.err.println("Metacat Error:\n" + me.getMessage());
645
            }
646
        }
647
        catch (Exception e) {
648
            fail("General exception:\n" + e.getMessage());
649
        }
650
        return response;
651
    }
652

    
653
    /**
654
     * Update a document in metacat. The expected result is passed as result
655
     */
656
    private String updateDocid(String docid, String docText, boolean result,
657
                               boolean expectedKarmaFailure) {
658
        String response = null;
659
        try {
660
            response = m.update(docid,
661
                                new StringReader(testdocument), null);
662

    
663
            if (result) {
664
                assertTrue( (response.indexOf("<success>") != -1));
665
                assertTrue(response.indexOf(docid) != -1);
666
            }
667
            else {
668
                assertTrue( (response.indexOf("<success>") == -1));
669
            }
670
            System.err.println(response);
671
        }
672
        catch (MetacatInaccessibleException mie) {
673
            fail("Metacat Inaccessible:\n" + mie.getMessage());
674
        }
675
        catch (InsufficientKarmaException ike) {
676
            if (!expectedKarmaFailure) {
677
                fail("Insufficient karma:\n" + ike.getMessage());
678
            }
679
        }
680
        catch (MetacatException me) {
681
            if (result) {
682
                fail("Metacat Error:\n" + me.getMessage());
683
            } else {
684
                System.err.println("Metacat Error:\n" + me.getMessage());
685
            }
686
        }
687
        catch (Exception e) {
688
            fail("General exception:\n" + e.getMessage());
689
        }
690

    
691
        return response;
692
    }
693

    
694
    /**
695
     * Delete a document from metacat. The expected result is passed as result
696
     */
697
    private void deleteDocid(String docid, boolean result,
698
                             boolean expextedKarmaFailure) {
699
        try {
700
            String response = m.delete(docid);
701
            if (result) {
702
                assertTrue(response.indexOf("<success>") != -1);
703
            }
704
            else {
705
                assertTrue(response.indexOf("<success>") == -1);
706
            }
707
            System.err.println(response);
708
        }
709
        catch (MetacatInaccessibleException mie) {
710
            fail("Metacat Inaccessible:\n" + mie.getMessage());
711
        }
712
        catch (InsufficientKarmaException ike) {
713
            if(!expextedKarmaFailure){
714
                fail("Insufficient karma:\n" + ike.getMessage());
715
            }
716
        }
717
        catch (MetacatException me) {
718
            if (result) {
719
                fail("Metacat Error:\n" + me.getMessage());
720
            } else {
721
                System.err.println("Metacat Error:\n" + me.getMessage());
722
            }
723
        }
724
        catch (Exception e) {
725
            fail("General exception:\n" + e.getMessage());
726
        }
727
    }
728

    
729
    /**
730
     * Read a document from metacat. The expected result is passed as result
731
     */
732
    private void readDocid(String docid, boolean result,
733
                           boolean expextedKarmaFailure) {
734
        try {
735
            Reader r = new InputStreamReader(m.read(docid));
736
            String response = IOUtil.getAsString(r, true);
737

    
738
            if (!result) {
739
                assertTrue(response.indexOf("<success>") == -1);
740
            }
741
            // System.err.println(response);
742
        }
743
        catch (MetacatInaccessibleException mie) {
744
            fail("Metacat Inaccessible:\n" + mie.getMessage());
745
        }
746
        catch (InsufficientKarmaException ike) {
747
            if (!expextedKarmaFailure) {
748
                fail("Insufficient karma:\n" + ike.getMessage());
749
            }
750
        }
751
        catch (MetacatException me) {
752
            fail("Metacat Error:\n" + me.getMessage());
753
        }
754
        catch (Exception e) {
755
            fail("General exception:\n" + e.getMessage());
756
        }
757
    }
758

    
759
    /**
760
     * Read a document from metacat and check if it is equal to a given string.
761
     * The expected result is passed as result
762
     */
763
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
764
                                         boolean result,
765
                                         boolean expectedKarmaFailure) {
766
        try {
767
            Reader r = new InputStreamReader(m.read(docid), "UTF-8");
768
            //InputStream is = m.read(docid);
769
            String doc = IOUtil.getAsString(r, true);
770
            //String doc = IOUtils.toString(is);
771
            
772
            if (result) {
773

    
774
                if (!testDoc.equals(doc)) {
775
                    debug("doc    :" + doc);
776
                    debug("testDoc:" + testDoc);
777
                }
778

    
779
                assertTrue(testDoc.equals(doc));
780
            }
781
            else {
782
                assertTrue(doc.indexOf("<error>") != -1);
783
            }
784
        }
785
        catch (MetacatInaccessibleException mie) {
786
            fail("Metacat Inaccessible:\n" + mie.getMessage());
787
        }
788
        catch (InsufficientKarmaException ike) {
789
            if (!expectedKarmaFailure) {
790
                fail("Insufficient karma:\n" + ike.getMessage());
791
            }
792
        }
793
        catch (MetacatException me) {
794
            fail("Metacat Error:\n" + me.getMessage());
795
        }
796
        catch (Exception e) {
797
            fail("General exception:\n" + e.getMessage());
798
        }
799

    
800
    }
801

    
802
    /**
803
     * Create a hopefully unique docid for testing insert and update. Does
804
     * not include the 'revision' part of the id.
805
     *
806
     * @return a String docid based on the current date and time
807
     */
808
    private String generateDocid() {
809
        StringBuffer docid = new StringBuffer(prefix);
810
        docid.append(".");
811

    
812
        // Create a calendar to get the date formatted properly
813
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
814
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
815
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
816
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
817
                       2 * 60 * 60 * 1000);
818
        Calendar calendar = new GregorianCalendar(pdt);
819
        Date trialTime = new Date();
820
        calendar.setTime(trialTime);
821
        docid.append(calendar.get(Calendar.YEAR));
822
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
823
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
824
        docid.append(calendar.get(Calendar.MINUTE));
825
        docid.append(calendar.get(Calendar.SECOND));
826
   	    //sometimes this number is not unique, so we append a random number
827
    	int random = (new Double(Math.random()*100)).intValue();
828
    	docid.append(random);
829
        return docid.toString();
830
    }
831
}
(12-12/24)