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: daigle $'
8
 *     '$Date: 2008-09-16 13:38:32 -0700 (Tue, 16 Sep 2008) $'
9
 * '$Revision: 4355 $'
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.Reader;
29
import java.io.StringReader;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.GregorianCalendar;
33
import java.util.SimpleTimeZone;
34
import java.util.TimeZone;
35

    
36
import edu.ucsb.nceas.MCTestCase;
37
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
38
import edu.ucsb.nceas.metacat.client.Metacat;
39
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
40
import edu.ucsb.nceas.metacat.client.MetacatException;
41
import edu.ucsb.nceas.metacat.client.MetacatFactory;
42
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
43
import edu.ucsb.nceas.metacat.service.PropertyService;
44
import edu.ucsb.nceas.utilities.IOUtil;
45
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
46
import junit.framework.Test;
47
import junit.framework.TestSuite;
48
import java.io.File;
49

    
50
/**
51
 * A JUnit test for testing Access Control for Inline data in Metacat
52
 */
53
public class InlineDataAccessTest
54
    extends MCTestCase {
55

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

    
74
    private String prefix = "test";
75
    private String newdocid = null;
76
    private String testdocument = "";
77

    
78
    private Metacat m;
79

    
80
    /**
81
     * These variables are for eml-2.0.1 only. For other eml versions,
82
     * this function might have to modified
83
     */
84

    
85
    private String testEmlHeader =
86
        "<?xml version=\"1.0\"?><eml:eml" +
87
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
88
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
89
        " packageId=\"eml.1.1\" system=\"knb\"" +
90
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
91
        " scope=\"system\">";
92

    
93
    private String testEmlCreatorBlock =
94
        "<creator scope=\"document\">                                       " +
95
        " <individualName>                                                  " +
96
        "    <surName>Smith</surName>                                       " +
97
        " </individualName>                                                 " +
98
        "</creator>                                                         ";
99

    
100
    private String testEmlContactBlock =
101
        "<contact scope=\"document\">                                       " +
102
        " <individualName>                                                  " +
103
        "    <surName>Jackson</surName>                                     " +
104
        " </individualName>                                                 " +
105
        "</contact>                                                         ";
106

    
107
    private String testEmlInlineBlock1 =
108
        "  <admin>                                                          " +
109
        "    <contact>                                                      " +
110
        "      <name>Operator</name>                                        " +
111
        "      <institution>PSI</institution>                               " +
112
        "    </contact>                                                     " +
113
        "  </admin>                                                         ";
114

    
115
    private String testEmlInlineBlock2 =
116
        "  <instrument>                                                     " +
117
        "    <instName>LCQ</instName>                                       " +
118
        "    <source type=\"ESI\"></source>                                 " +
119
        "    <detector type=\"EM\"></detector>                              " +
120
        "  </instrument>                                                    ";
121

    
122
    private String testdoc = testEmlHeader +
123
        "<dataset scope=\"document\"><title>the title</title>"
124
        +  testEmlCreatorBlock
125
        + "<distribution scope=\"document\" id=\"inlineEntity1\">"
126
        + "<inline>" + testEmlInlineBlock1 + "</inline></distribution>"
127
        + "<distribution scope=\"document\" id=\"inlineEntity2\">"
128
        + "<inline>" + testEmlInlineBlock2 + "</inline></distribution>"
129
        + testEmlContactBlock
130
        + getAccessBlock(anotheruser, true, false, false, false, true)
131
        + "</dataset><additionalMetadata><describes>inlineEntity2"
132
        + "</describes></additionalMetadata>"
133
        + "<additionalMetadata>"
134
        + "<describes>inlineEntity1</describes>"
135
        + "<describes>inlineEntity2</describes>"
136
        + getAccessBlock(anotheruser, false, false, false, false, true)
137
        + "</additionalMetadata></eml:eml>";
138

    
139

    
140
    /**
141
     * This function returns an access block based on the params passed
142
     */
143
    private String getAccessBlock(String principal, boolean grantAccess,
144
                                  boolean read, boolean write,
145
                                  boolean changePermission, boolean all) {
146
        String accessBlock = "<access " +
147
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
148
            " order=\"allowFirst\" scope=\"document\">";
149

    
150
        if (grantAccess) {
151
            accessBlock += "<allow>";
152
        }
153
        else {
154
            accessBlock += "<deny>";
155
        }
156

    
157
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
158

    
159
        if (all) {
160
            accessBlock += "<permission>all</permission>";
161
        }
162
        else {
163
            if (read) {
164
                accessBlock += "<permission>read</permission>";
165
            }
166
            if (write) {
167
                accessBlock += "<permission>write</permission>";
168
            }
169
            if (changePermission) {
170
                accessBlock += "<permission>changePermission</permission>";
171
            }
172
        }
173

    
174
        if (grantAccess) {
175
            accessBlock += "</allow>";
176
        }
177
        else {
178
            accessBlock += "</deny>";
179
        }
180
        accessBlock += "</access>";
181

    
182
        return accessBlock;
183

    
184
    }
185

    
186
    /**
187
     * This function returns a valid eml document with no access rules
188
     * This function is for eml-2.0.1 only. For other eml versions,
189
     * this function might have to modified
190
     */
191
    private String getTestEmlDoc(String title, String inlineData1,
192
                                 String inlineData2, String OnlineUrl1,
193
                                 String OnlineUrl2, String docAccessBlock,
194
                                 String inlineAccessBlock1,
195
                                 String inlineAccessBlock2,
196
                                 String OnlineAccessBlock1,
197
                                 String OnlineAccessBlock2) {
198

    
199
        String testDocument = "";
200
        testDocument = testDocument + testEmlHeader +
201
            "<dataset scope=\"document\"><title>" + title + "</title>" +
202
            testEmlCreatorBlock;
203

    
204
        if (inlineData1 != null) {
205
            testDocument = testDocument
206
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
207
                + "<inline>" + inlineData1 + "</inline></distribution>";
208
        }
209
        if (inlineData2 != null) {
210
            testDocument = testDocument
211
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
212
                + "<inline>" + inlineData2 + "</inline></distribution>";
213
        }
214
        if (OnlineUrl1 != null) {
215
            testDocument = testDocument
216
                + "<distribution scope=\"document\" id=\"InlineEntity1\">"
217
                + "<Inline><url function=\"download\">"
218
                + OnlineUrl1 + "</url></Inline></distribution>";
219
        }
220
        if (OnlineUrl2 != null) {
221
            testDocument = testDocument +
222
                "<distribution scope=\"document\" id=\"InlineEntity2\">"
223
                + "<Inline><url function=\"download\">"
224
                + OnlineUrl2 + "</url></Inline></distribution>";
225
        }
226
        testDocument += testEmlContactBlock;
227

    
228
        if (docAccessBlock != null) {
229
            testDocument += docAccessBlock;
230
        }
231

    
232
        testDocument += "</dataset>";
233

    
234
        if (inlineAccessBlock1 != null) {
235
            testDocument += "<additionalMetadata>";
236
            testDocument += "<describes>inlineEntity1</describes>";
237
            testDocument += inlineAccessBlock1;
238
            testDocument += "</additionalMetadata>";
239
        }
240

    
241
        if (inlineAccessBlock2 != null) {
242
            testDocument += "<additionalMetadata>";
243
            testDocument += "<describes>inlineEntity2</describes>";
244
            testDocument += inlineAccessBlock2;
245
            testDocument += "</additionalMetadata>";
246
        }
247

    
248
        if (OnlineAccessBlock1 != null) {
249
            testDocument += "<additionalMetadata>";
250
            testDocument += "<describes>InlineEntity1</describes>";
251
            testDocument += OnlineAccessBlock1;
252
            testDocument += "</additionalMetadata>";
253
        }
254

    
255
        if (OnlineAccessBlock2 != null) {
256
            testDocument += "<additionalMetadata>";
257
            testDocument += "<describes>InlineEntity2</describes>";
258
            testDocument += OnlineAccessBlock2;
259
            testDocument += "</additionalMetadata>";
260
        }
261

    
262
        testDocument += "</eml:eml>";
263

    
264
        //System.out.println("Returning following document" + testDocument);
265
        return testDocument;
266
    }
267

    
268
    /**
269
     * Constructor to build the test
270
     *
271
     * @param name the name of the test method
272
     */
273
    public InlineDataAccessTest(String name) {
274
        super(name);
275
        newdocid = generateDocid();
276
    }
277

    
278
    /**
279
     * Establish a testing framework by initializing appropriate objects
280
     */
281
    public void setUp() {
282
        try {
283
            System.err.println("Test Metacat: " + metacatUrl);
284
            m = MetacatFactory.createMetacatConnection(metacatUrl);
285
        }
286
        catch (MetacatInaccessibleException mie) {
287
            System.err.println("Metacat is: " + metacatUrl);
288
            fail("Metacat connection failed." + mie.getMessage());
289
        }
290
    }
291

    
292
    /**
293
     * Release any objects after tests are complete
294
     */
295
    public void tearDown() {
296
    }
297

    
298
    /**
299
     * Create a suite of tests to be run together
300
     */
301
    public static Test suite() {
302
        TestSuite suite = new TestSuite();
303
        suite.addTest(new InlineDataAccessTest("initialize"));
304

    
305
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_1"));
306
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_2"));
307
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_3"));
308
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_4"));
309
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_5"));
310
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_6"));
311

    
312
        return suite;
313
    }
314

    
315

    
316
    /**
317
     * Run an initial test that always passes to check that the test
318
     * harness is working.
319
     */
320
    public void initialize() {
321
        assertTrue(1 == 1);
322
    }
323

    
324
    /** *********
325
     * Checking the following cases:
326
     * when only Inline data is uploaded by a user and
327
     * -> he tries to read it  - success
328
     * -> he tries to add same docid again  - failure
329
     * -> he tries to update it  - success
330
     * -> he removes it and adds it again - success
331
     * -> he tries to delete it  - success
332
     * -> he tries to read it after deleteing - failure
333
     */
334
    public void inlineDataCasesTest_1() {
335
		debug("\nRunning: inlineDataCasesTest_1()");
336
        try {
337
            newdocid = generateDocid();
338
            m.login(username, password);
339

    
340
            // insert a document
341
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
342
                                         null, null, null, null,
343
                                         null, null, null, null);
344

    
345
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
346
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
347

    
348
            // insert same document again
349
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
350

    
351
            // update by changing inline data
352
            testdocument = getTestEmlDoc("Testing update inline",
353
                                         testEmlInlineBlock2,
354
                                         null, null, null, null,
355
                                         null, null, null, null);
356
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
357
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS, true);
358

    
359
            // update by removing inline data
360
            testdocument = getTestEmlDoc("Testing update inline",
361
                                         null,
362
                                         null, null, null, null,
363
                                         null, null, null, null);
364
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
365
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, true);
366

    
367
            // update by introducing inline data
368
            testdocument = getTestEmlDoc("Testing update inline",
369
                                         testEmlInlineBlock1,
370
                                         null, null, null, null,
371
                                         null, null, null, null);
372
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
373
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, true);
374

    
375
            // read inline data only
376
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
377
                                         testEmlInlineBlock1, SUCCESS, false);
378

    
379
            // delete the inline data
380
            // sleep needed only in case of inline data - jing said that
381
            // somehow the thread writing data to xml_index takes too much time
382
            // when used with inline data. hence if delete is requested too soon
383
            // database gives an error of xml_index records left with FK to
384
            // xml_document record which is going to be deleted.
385
            Thread.sleep(10000);
386

    
387
            deleteDocid(newdocid + ".4", SUCCESS, false);
388
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, FAILURE, false);
389
            m.logout();
390
        }
391
        catch (MetacatAuthException mae) {
392
            fail("Authorization failed:\n" + mae.getMessage());
393
        }
394
        catch (MetacatInaccessibleException mie) {
395
            fail("Metacat Inaccessible:\n" + mie.getMessage());
396
        }
397
        catch (Exception e) {
398
            fail("General exception:\n" + e.getMessage());
399
        }
400
    }
401

    
402
    /** *********
403
     * Checking the following cases:
404
     * when only inline data is uploaded by a user and another user
405
     * -> tries to read it  - failure
406
     * -> tries to read inline data only - failure
407
     * -> tries to update - failure
408
     * -> tries to delete it  - failure
409
     */
410
    public void inlineDataCasesTest_2() {
411
		debug("\nRunning: inlineDataCasesTest_2()");
412
        try {
413
            newdocid = generateDocid();
414
            m.login(username, password);
415

    
416
            // insert a document
417
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
418
                                         null, null, null, null,
419
                                         null, null, null, null);
420

    
421
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
422
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
423

    
424
            // login as another user
425
            m.logout();
426
            m.login(anotheruser, anotherpassword);
427

    
428
            // try to read document or data only
429
            readDocidWhichEqualsDoc(newdocid, testdocument, FAILURE, true);
430
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
431
                                         testEmlInlineBlock1, FAILURE, true);
432

    
433
            // try to update the document
434
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
435
                                         null, null, null, null,
436
                                         null, null, null, null);
437
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
438

    
439
            // try to delete the document
440
            deleteDocid(newdocid + ".1", FAILURE, true);
441
            m.logout();
442

    
443
            // delete the document
444
            m.login(username, password);
445
            deleteDocid(newdocid + ".1", SUCCESS, false);
446
            m.logout();
447

    
448
        }
449
        catch (MetacatAuthException mae) {
450
            fail("Authorization failed:\n" + mae.getMessage());
451
        }
452
        catch (MetacatInaccessibleException mie) {
453
            fail("Metacat Inaccessible:\n" + mie.getMessage());
454
        }
455
        catch (Exception e) {
456
            fail("General exception:\n" + e.getMessage());
457
        }
458
    }
459

    
460
    /** *********
461
     * Checking the following cases:
462
     * when only inline data is uploaded by a user with the following different
463
     * access controls in another document
464
     *   1.read
465
     *   2.write
466
     *   3.change permission
467
     *   4.all
468
     * And another user tries to do the following:
469
     * -> tries to read it
470
     * -> tries to update it
471
     * -> tries to set permissions on it
472
     * -> tries to delete it
473
     */
474
    public void inlineDataCasesTest_3() {
475
		debug("\nRunning: inlineDataCasesTest_3()");
476
        try {
477

    
478
            /////////Case 1./////////////////////
479
            // insert an inline document - read only
480
            m.login(username, password);
481
            newdocid = generateDocid();
482

    
483
            // insert a document which gives read access to the inline document
484
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
485
                                         testEmlInlineBlock1, null, null,
486
                                         null, getAccessBlock(anotheruser, true,
487
                true, false, false, false),
488
                                         null, null, null, null);
489
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
490
            m.logout();
491

    
492
            // login as another user
493
            m.login(anotheruser, anotherpassword);
494

    
495
            // try to read the document and the inline data
496
            readDocid(newdocid + ".1", SUCCESS, false);
497
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
498
                                         testEmlInlineBlock1, SUCCESS, false);
499

    
500
            // try to update the inline data
501
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
502
                                         testEmlInlineBlock2, null, null,
503
                                         null, getAccessBlock(anotheruser, true,
504
                true, false, false, false),
505
                                         null, null, null, null);
506
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
507

    
508
            // try to set the permissions for the inline data
509
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
510
                                         testEmlInlineBlock1, null, null,
511
                                         null, getAccessBlock(anotheruser, true,
512
                false, false, false, true),
513
                                         null, null, null, null);
514
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
515

    
516
            // try to delete the document
517
            deleteDocid(newdocid + ".1", FAILURE, true);
518
            m.logout();
519

    
520
            // delete the document
521
            m.login(username, password);
522
            deleteDocid(newdocid + ".1", SUCCESS, false);
523
            m.logout();
524

    
525
            /////////Case 2./////////////////////
526
            // insert an inline document - write only
527
            m.login(username, password);
528
            newdocid = generateDocid();
529

    
530
            // insert a document which gives read access to the inline document
531
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
532
                                         testEmlInlineBlock1, null, null,
533
                                         null, getAccessBlock(anotheruser, true,
534
                                         false, true, false, false),
535
                                         null, null, null, null);
536
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
537
            m.logout();
538

    
539
            // login as another user
540
            m.login(anotheruser, anotherpassword);
541

    
542
            // try to read the document and the inline data
543
            readDocid(newdocid + ".1", FAILURE, true);
544
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
545
                                         testEmlInlineBlock1, FAILURE, true);
546

    
547
            // try to update the inline data
548
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
549
                                         testEmlInlineBlock2, null, null,
550
                                         null, getAccessBlock(anotheruser, true,
551
                                         false, true, false, false),
552
                                         null, null, null, null);
553
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
554

    
555
            // try to set the permissions for the inline data
556
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
557
                                         testEmlInlineBlock1, null, null,
558
                                         null, getAccessBlock(anotheruser, true,
559
                false, false, false, true),
560
                                         null, null, null, null);
561
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
562

    
563
            // try to delete the document
564
            deleteDocid(newdocid + ".2", FAILURE, true);
565
            m.logout();
566

    
567
            // delete the document
568
            m.login(username, password);
569
            deleteDocid(newdocid + ".2", SUCCESS, false);
570
            m.logout();
571

    
572
            /////////Case 3./////////////////////
573
            // insert an inline document - change permission only
574
            m.login(username, password);
575
            newdocid = generateDocid();
576

    
577
            // insert a document which gives read access to the inline document
578
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
579
                                         testEmlInlineBlock1, null, null,
580
                                         null, getAccessBlock(anotheruser, true,
581
                false, false, true, false),
582
                                         null, null, null, null);
583
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
584
            m.logout();
585

    
586
            // login as another user
587
            m.login(anotheruser, anotherpassword);
588

    
589
            // try to read the document and the inline data
590
            readDocid(newdocid + ".1", FAILURE, true);
591
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
592
                                         testEmlInlineBlock1, FAILURE, true);
593

    
594
            // try to update the inline data
595
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
596
                                         testEmlInlineBlock2, null, null,
597
                                         null, getAccessBlock(anotheruser, true,
598
                false, false, true, false),
599
                                         null, null, null, null);
600
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
601

    
602
            // try to set the permissions for the inline data
603
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
604
                                         testEmlInlineBlock1, null, null,
605
                                         null, getAccessBlock(anotheruser, true,
606
                false, false, false, true),
607
                                         null, null, null, null);
608
            // ERRRRRRRRRRRR
609
            //updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
610

    
611
            // try to delete the document
612
            deleteDocid(newdocid + ".1", FAILURE, true);
613
            m.logout();
614

    
615
            // delete the document
616
            m.login(username, password);
617
            deleteDocid(newdocid + ".1", SUCCESS, false);
618
            m.logout();
619

    
620

    
621
            /////////Case 4./////////////////////
622
            // insert an inline document - change permission only
623
            m.login(username, password);
624
            newdocid = generateDocid();
625

    
626
            // insert a document which gives read access to the inline document
627
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
628
                                         testEmlInlineBlock1, null, null,
629
                                         null, getAccessBlock(anotheruser, true,
630
                                         false, false, false, true),
631
                                         null, null, null, null);
632
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
633
            m.logout();
634

    
635
            // login as another user
636
            m.login(anotheruser, anotherpassword);
637

    
638
            // try to read the document and the inline data
639
            readDocid(newdocid + ".1", SUCCESS, false);
640
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
641
                                         testEmlInlineBlock1, SUCCESS, false);
642

    
643
            // try to update the inline data
644
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
645
                                         testEmlInlineBlock2, null, null,
646
                                         null, getAccessBlock(anotheruser, true,
647
                                         false, false, false, true),
648
                                         null, null, null, null);
649
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
650

    
651
            // try to set the permissions for the inline data
652
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
653
                                         testEmlInlineBlock1, null, null,
654
                                         null, getAccessBlock(anotheruser, true,
655
                                         true, true, true, false),
656
                                         null, null, null, null);
657
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
658

    
659
            // try to delete the document
660
            // sleep needed only in case of inline data - jing said that
661
            // somehow the thread writing data to xml_index takes too much time
662
            // when used with inline data. hence if delete is requested too soon
663
            // database gives an error of xml_index records left with FK to
664
            // xml_document record which is going to be deleted.
665
            Thread.sleep(10000);
666

    
667
            deleteDocid(newdocid + ".3", SUCCESS, false);
668
            m.logout();
669

    
670
        }
671
        catch (MetacatAuthException mae) {
672
            fail("Authorization failed:\n" + mae.getMessage());
673
        }
674
        catch (MetacatInaccessibleException mie) {
675
            fail("Metacat Inaccessible:\n" + mie.getMessage());
676
        }
677
        catch (Exception e) {
678
            fail("General exception:\n" + e.getMessage());
679
        }
680
    }
681

    
682
    /** *********
683
     * Checking the following cases:
684
     * when only Inline data is uploaded by a user with the following different
685
     * access controls specified in addiotnal metadata in another document
686
     *   1.read
687
     *   2.write
688
     *   3.change permission
689
     *   4.all
690
     * And another user tries to do the following:
691
     * -> tries to read it
692
     * -> tries to update it
693
     * -> tries to set permissions on it
694
     * -> tries to delete it
695
     */
696
    public void inlineDataCasesTest_4() {
697
		debug("\nRunning: inlineDataCasesTest_4()");
698
        try {
699
            /////////Case 1./////////////////////
700
            // insert an inline document - read only
701
            m.login(username, password);
702
            newdocid = generateDocid();
703

    
704
            // insert a document which gives read access to the inline document
705
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
706
                                         testEmlInlineBlock1, null, null, null,
707
                                         null, getAccessBlock(anotheruser, true,
708
                                         true, false, false, false),
709
                                         null, null, null);
710
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
711
            m.logout();
712

    
713
            // login as another user
714
            m.login(anotheruser, anotherpassword);
715

    
716
            // try to read the document and the inline data
717
            readDocid(newdocid + ".1", FAILURE, true);
718
            // ERRRRRRRRRRRRRRR
719
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
720
                                         testEmlInlineBlock1, FAILURE, true);
721

    
722
            // try to update the inline data
723
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
724
                                         testEmlInlineBlock2, null, null,null,
725
                                         null, getAccessBlock(anotheruser, true,
726
                                         true, false, false, false),
727
                                         null, null, null);
728
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
729

    
730
            // try to set the permissions for the inline data
731
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
732
                                         testEmlInlineBlock1, null, null,null,
733
                                         null, getAccessBlock(anotheruser, true,
734
                                         false, false, false, true),
735
                                         null, null, null);
736
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
737

    
738
            // try to delete the document
739
            deleteDocid(newdocid + ".1", FAILURE, true);
740
            m.logout();
741

    
742
            // delete the document
743
            m.login(username, password);
744
            deleteDocid(newdocid + ".1", SUCCESS, false);
745
            m.logout();
746

    
747
            /////////Case 2./////////////////////
748
            // insert an inline document - write only
749
            m.login(username, password);
750
            newdocid = generateDocid();
751

    
752
            // insert a document which gives read access to the inline document
753
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
754
                                         testEmlInlineBlock1, null, null, null,
755
                                         null, getAccessBlock(anotheruser, true,
756
                                         false, true, false, false),
757
                                         null, null, null);
758
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
759
            m.logout();
760

    
761
            // login as another user
762
            m.login(anotheruser, anotherpassword);
763

    
764
            // try to read the document and the inline data
765
            readDocid(newdocid + ".1", FAILURE, true);
766
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
767
                                         testEmlInlineBlock1, FAILURE, true);
768

    
769
            // try to update the inline data
770
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
771
                                         testEmlInlineBlock2, null, null, null,
772
                                         null, getAccessBlock(anotheruser, true,
773
                                         false, true, false, false),
774
                                         null, null, null);
775
            //ERRRRRRRRRRRRRRRRRRRRRRRR
776
            // updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
777

    
778
            // try to set the permissions for the inline data
779
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
780
                                         testEmlInlineBlock1, null, null, null,
781
                                         null, getAccessBlock(anotheruser, true,
782
                                         false, false, false, true),
783
                                         null, null, null);
784
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
785

    
786
            // try to delete the document
787
            deleteDocid(newdocid + ".1", FAILURE, true);
788
            m.logout();
789

    
790
            // delete the document
791
            m.login(username, password);
792
            deleteDocid(newdocid + ".1", SUCCESS, false);
793
            m.logout();
794

    
795
            /////////Case 3./////////////////////
796
            // insert an inline document - change permission only
797
            m.login(username, password);
798
            newdocid = generateDocid();
799

    
800
            // insert a document which gives read access to the inline document
801
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
802
                                         testEmlInlineBlock1, null, null, null,
803
                                         null, getAccessBlock(anotheruser, true,
804
                                         false, false, true, false),
805
                                         null, null, null);
806
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
807
            m.logout();
808

    
809
            // login as another user
810
            m.login(anotheruser, anotherpassword);
811

    
812
            // try to read the document and the inline data
813
            readDocid(newdocid + ".1", FAILURE, true);
814
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
815
                                         testEmlInlineBlock1, FAILURE, true);
816

    
817
            // try to update the inline data
818
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
819
                                         testEmlInlineBlock2, null, null, null,
820
                                         null, getAccessBlock(anotheruser, true,
821
                                         false, false, true, false),
822
                                         null, null, null);
823
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
824

    
825
            // try to set the permissions for the inline data
826
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
827
                                         testEmlInlineBlock1, null, null, null,
828
                                         null, getAccessBlock(anotheruser, true,
829
                                         false, false, false, true),
830
                                         null, null, null);
831
            // ERRRRRRRRRRRR
832
            //updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
833

    
834
            // try to delete the document
835
            deleteDocid(newdocid + ".1", FAILURE, true);
836
            m.logout();
837

    
838
            // delete the document
839
            m.login(username, password);
840
            deleteDocid(newdocid + ".1", SUCCESS, false);
841
            m.logout();
842

    
843

    
844
            /////////Case 4./////////////////////
845
            // insert an inline document - change permission only
846
            m.login(username, password);
847
            newdocid = generateDocid();
848

    
849
            // insert a document which gives read access to the inline document
850
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
851
                                         testEmlInlineBlock1, null, null, null,
852
                                         null, getAccessBlock(anotheruser, true,
853
                                         false, false, false, true),
854
                                         null, null, null);
855
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
856
            m.logout();
857

    
858
            // login as another user
859
            m.login(anotheruser, anotherpassword);
860

    
861
            // try to read the document and the inline data
862
            readDocid(newdocid + ".1", FAILURE, true);
863
            // ERRRRRRRRRRRRRRR
864
            //readInlineDataWhichEqualsDoc(newdocid + ".1.1",
865
            //                             testEmlInlineBlock1, SUCCESS, false);
866

    
867
            // try to update the inline data
868
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
869
                                         testEmlInlineBlock2, null, null, null,
870
                                         null, getAccessBlock(anotheruser, true,
871
                                         false, false, false, true),
872
                                         null, null, null);
873
            // ERRRRRR
874
            // updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
875

    
876
            // try to set the permissions for the inline data
877
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
878
                                         testEmlInlineBlock1, null, null, null,
879
                                         null, getAccessBlock(anotheruser, true,
880
                                         true, true, true, false),
881
                                         null, null, null);
882
            // ERRRRRRRRRR
883
            // updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
884

    
885
            // try to delete the document
886
            deleteDocid(newdocid + ".3", FAILURE, true);
887
            m.logout();
888

    
889
            // delete the document
890
            m.login(username, password);
891
            deleteDocid(newdocid + ".1", SUCCESS, false);
892
            m.logout();
893

    
894
        }
895
        catch (MetacatAuthException mae) {
896
            fail("Authorization failed:\n" + mae.getMessage());
897
        }
898
        catch (MetacatInaccessibleException mie) {
899
            fail("Metacat Inaccessible:\n" + mie.getMessage());
900
        }
901
        catch (Exception e) {
902
            fail("General exception:\n" + e.getMessage());
903
        }
904
    }
905

    
906
    /** *********
907
     * Checking the following cases:
908
     * -> when no inline data is specified in the document but
909
     *    rules are specified in additional metadata
910
     * -> when a user has RW permission for inline data, can he delete it
911
     * -> when inline data with document refering to it is uploaded with read
912
     *    access for metadata and no access for data
913
     */
914
    public void inlineDataCasesTest_5() {
915
		debug("\nRunning: inlineDataCasesTest_5()");
916
        try {
917

    
918
            m.login(username, password);
919

    
920
            /////////Case 1
921
            debug("Case 1:");
922
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
923
                                         null, null,
924
                                         null, null, null,
925
                                         getAccessBlock(anotheruser, true,
926
                true, false, false, false), null, null, null);
927
            newdocid = generateDocid();
928

    
929
            // try to insert the wrong document
930
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
931
            m.logout();
932

    
933

    
934
            /////////Case 2
935
            debug("Case 2:");
936
            m.login(username, password);
937
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
938
                                         testEmlInlineBlock1, null, null,
939
                                         null, getAccessBlock(anotheruser, true,
940
                                         true, true, false, false), null, null,
941
                                         null, null);
942
            newdocid = generateDocid();
943
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
944
            m.logout();
945

    
946
            m.login(anotheruser, anotherpassword);
947
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
948
                                         null, null, null,
949
                                         null, getAccessBlock(anotheruser, true,
950
                                         true, true, false, false), null, null,
951
                                         null, null);
952
            /// ERRRRRRRRRRRR
953
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
954

    
955
            /////////Case 3
956
            debug("Case 3:");
957
            // insert a document
958
            m.login(username, password);
959
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
960
                                         testEmlInlineBlock1, null, null,
961
                                         null, getAccessBlock(anotheruser, true,
962
                                         false, false, false, true),
963
                                         getAccessBlock(anotheruser, false,
964
                                         false, false, false, true), null,
965
                                         null, null);
966
            newdocid = generateDocid();
967
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
968
            m.logout();
969

    
970

    
971
            // try to read the Inline data
972
            m.login(anotheruser, anotherpassword);
973
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
974
                             "", null, null,
975
                             null, getAccessBlock(anotheruser, true,
976
                             false, false, false, true),
977
                             getAccessBlock(anotheruser, false,
978
                             false, false, false, true), null,
979
                             null, null);
980
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument,
981
                                    SUCCESS, false);
982

    
983
            // try to update the rules for inline data
984
            /// ERRRRRRRRRRRR it lets you do that
985
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
986
                                         testEmlInlineBlock1, null, null,
987
                                         null, getAccessBlock(anotheruser, true,
988
                                         false, false, false, true),
989
                                         getAccessBlock(anotheruser, true,
990
                                         false, false, false, true), null,
991
                                         null, null);
992
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
993
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument,
994
                                    SUCCESS, false);
995

    
996
            m.logout();
997
        }
998
        catch (MetacatAuthException mae) {
999
            fail("Authorization failed:\n" + mae.getMessage());
1000
        }
1001
        catch (MetacatInaccessibleException mie) {
1002
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1003
        }
1004
        catch (Exception e) {
1005
            fail("General exception:\n" + e.getMessage());
1006
        }
1007
    }
1008

    
1009

    
1010
    /** *********
1011
       * Checking the following cases:
1012
       * -> when inline data in inserted and updated, do access rules apply
1013
       *    to the old document
1014
       */
1015
      public void inlineDataCasesTest_6() {
1016
  		debug("\nRunning: inlineDataCasesTest_6()");
1017
          try {
1018

    
1019
              // insert the document
1020
              m.login(username, password);
1021
              testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
1022
                                           testEmlInlineBlock1, testEmlInlineBlock2,
1023
                                           null, null,
1024
                                           getAccessBlock(anotheruser, true,
1025
                                           true, true, false, false),
1026
                                           getAccessBlock(anotheruser, false,
1027
                                           false, false, false, true),
1028
                                           getAccessBlock(anotheruser, false,
1029
                                           false, false, false, true),
1030
                                           null, null);
1031
              newdocid = generateDocid();
1032
              insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1033

    
1034
              m.logout();
1035

    
1036
              // update the document
1037
              m.login(anotheruser, anotherpassword);
1038
              testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
1039
                                           testEmlInlineBlock2, testEmlInlineBlock1,
1040
                                           null, null,
1041
                                           getAccessBlock(anotheruser, true,
1042
                                           true, true, false, false),
1043
                                           getAccessBlock(anotheruser, false,
1044
                                           false, false, false, true),
1045
                                           getAccessBlock(anotheruser, false,
1046
                                           false, false, false, true),
1047
                                           null, null);
1048

    
1049

    
1050
              updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1051

    
1052
              // try reading the inline document
1053
              readInlineDataWhichEqualsDoc(newdocid + ".2.1",
1054
                             testEmlInlineBlock1, FAILURE, true);
1055

    
1056
              System.out.print("Trying to read " + newdocid + ".1.1");
1057
              readInlineDataWhichEqualsDoc(newdocid + ".1.1",
1058
                             testEmlInlineBlock1, FAILURE, true);
1059

    
1060
              m.logout();
1061
          }
1062
          catch (MetacatAuthException mae) {
1063
              fail("Authorization failed:\n" + mae.getMessage());
1064
          }
1065
          catch (MetacatInaccessibleException mie) {
1066
              fail("Metacat Inaccessible:\n" + mie.getMessage());
1067
          }
1068
          catch (Exception e) {
1069
              fail("General exception:\n" + e.getMessage());
1070
          }
1071
      }
1072

    
1073

    
1074
    /**
1075
     * Insert a document into metacat. The expected result is passed as result
1076
     */
1077

    
1078
    private String insertDocid(String docid, String docText, boolean result,
1079
                               boolean expectKarmaException) {
1080
        String response = null;
1081
        try {
1082
            response = m.insert(docid,
1083
                                new StringReader(testdocument), null);
1084
            if (result) {
1085
                assertTrue( (response.indexOf("<success>") != -1));
1086
                assertTrue(response.indexOf(docid) != -1);
1087
            }
1088
            else {
1089
                assertTrue( (response.indexOf("<success>") == -1));
1090
            }
1091
            System.err.println(response);
1092
        }
1093
        catch (MetacatInaccessibleException mie) {
1094
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1095
        }
1096
        catch (InsufficientKarmaException ike) {
1097
            if (!expectKarmaException) {
1098
                fail("Insufficient karma:\n" + ike.getMessage());
1099
            }
1100
        }
1101
        catch (MetacatException me) {
1102
            if (result) {
1103
                fail("Metacat Error:\n" + me.getMessage());
1104
            }
1105
            else {
1106
                System.err.println("Metacat Error: " + me.getMessage());
1107
            }
1108
        }
1109
        catch (Exception e) {
1110
            fail("General exception:\n" + e.getMessage());
1111
        }
1112
        return response;
1113
    }
1114

    
1115
    /**
1116
     * Insert a document into metacat. The expected result is passed as result
1117
     */
1118

    
1119
    private String uploadDocid(String docid, String filePath, boolean result,
1120
                               boolean expectedKarmaException) {
1121
        String response = null;
1122
        try {
1123
            response = m.upload(docid, new File(filePath));
1124
            if (result) {
1125
                assertTrue( (response.indexOf("<success>") != -1));
1126
                assertTrue(response.indexOf(docid) != -1);
1127
            }
1128
            else {
1129
                assertTrue( (response.indexOf("<success>") == -1));
1130
            }
1131
            System.err.println("respose from metacat: " + response);
1132
        }
1133
        catch (MetacatInaccessibleException mie) {
1134
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1135
        }
1136
        catch (InsufficientKarmaException ike) {
1137
            if (!expectedKarmaException) {
1138
                fail("Insufficient karma:\n" + ike.getMessage());
1139
            }
1140
        }
1141
        catch (MetacatException me) {
1142
            if (result) {
1143
                fail("Metacat Error:\n" + me.getMessage());
1144
            }
1145
            else {
1146
                System.err.println("Metacat Error: " + me.getMessage());
1147
            }
1148
        }
1149
        catch (Exception e) {
1150
            fail("General exception:\n" + e.getMessage());
1151
        }
1152
        return response;
1153
    }
1154

    
1155
    /**
1156
     * Update a document in metacat. The expected result is passed as result
1157
     */
1158
    private String updateDocid(String docid, String docText, boolean result,
1159
                               boolean expectedKarmaFailure) {
1160
        String response = null;
1161
        try {
1162
            response = m.update(docid,
1163
                                new StringReader(testdocument), null);
1164

    
1165
            if (result) {
1166
                assertTrue( (response.indexOf("<success>") != -1));
1167
                assertTrue(response.indexOf(docid) != -1);
1168
            }
1169
            else {
1170
                assertTrue( (response.indexOf("<success>") == -1));
1171
            }
1172
            System.err.println(response);
1173
        }
1174
        catch (MetacatInaccessibleException mie) {
1175
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1176
        }
1177
        catch (InsufficientKarmaException ike) {
1178
            if (!expectedKarmaFailure) {
1179
                fail("Insufficient karma:\n" + ike.getMessage());
1180
            }
1181
        }
1182
        catch (MetacatException me) {
1183
            if (! (expectedKarmaFailure &&
1184
                   (me.getMessage().indexOf(
1185
                "User try to update a access module which it doesn't have \"ALL\" permission") !=
1186
                    -1))) {
1187
                fail("Metacat Error:\n" + me.getMessage());
1188
            }
1189
        }
1190
        catch (Exception e) {
1191
            fail("General exception:\n" + e.getMessage());
1192
        }
1193

    
1194
        return response;
1195
    }
1196

    
1197
    /**
1198
     * Delete a document into metacat. The expected result is passed as result
1199
     */
1200
    private void deleteDocid(String docid, boolean result,
1201
                             boolean expectedKarmaFailure) {
1202
        try {
1203
            String response = m.delete(docid);
1204
            if (result) {
1205
                assertTrue(response.indexOf("<success>") != -1);
1206
            }
1207
            else {
1208
                assertTrue(response.indexOf("<success>") == -1);
1209
            }
1210
            System.err.println(response);
1211
        }
1212
        catch (MetacatInaccessibleException mie) {
1213
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1214
        }
1215
        catch (InsufficientKarmaException ike) {
1216
            if (!expectedKarmaFailure) {
1217
                fail("Insufficient karma:\n" + ike.getMessage());
1218
            }
1219

    
1220
        }
1221
        catch (MetacatException me) {
1222
            if (result) {
1223
                fail("Metacat Error:\n" + me.getMessage());
1224
            }
1225
            else {
1226
                System.err.println("Metacat Error:\n" + me.getMessage());
1227
            }
1228
        }
1229
        catch (Exception e) {
1230
            fail("General exception:\n" + e.getMessage());
1231
        }
1232
    }
1233

    
1234
    /**
1235
     * Read inline data from metacat. The expected result is passed as result
1236
     */
1237
    private void readInlineDataWhichEqualsDoc(String docid, String testDoc,
1238
                                              boolean result,
1239
                                              boolean expextedKarmaFailure) {
1240
        try {
1241
            Reader r = m.readInlineData(docid);
1242
            String doc = IOUtil.getAsString(r, true);
1243

    
1244
            if (result) {
1245
                if (!testDoc.equals(doc)) {
1246
                    System.out.println("doc    :" + doc);
1247
                    System.out.println("testDoc:" + testDoc);
1248
                }
1249

    
1250
                assertTrue(testDoc.equals(doc));
1251
            }
1252
            else {
1253
                System.out.println("doc    :" + doc);
1254
                System.out.println("testDoc:" + testDoc);
1255
                assertTrue(doc.indexOf("<error>") != -1);
1256
            }
1257
        }
1258
        catch (MetacatInaccessibleException mie) {
1259
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1260
        }
1261
        catch (InsufficientKarmaException ike) {
1262
            if (!expextedKarmaFailure) {
1263
                fail("Insufficient karma:\n" + ike.getMessage());
1264
            }
1265
        }
1266
        catch (MetacatException me) {
1267
            if (result) {
1268
                fail("Metacat Error:\n" + me.getMessage());
1269
            }
1270
            else {
1271
                System.err.println("Metacat Error:\n" + me.getMessage());
1272
            }
1273
        }
1274
        catch (Exception e) {
1275
            fail("General exception:\n" + e.getMessage());
1276
        }
1277
    }
1278

    
1279
    /**
1280
     * Read a document from metacat. The expected result is passed as result
1281
     */
1282
    private void readDocid(String docid, boolean result,
1283
                           boolean expextedKarmaFailure) {
1284
        try {
1285
            Reader r = m.read(docid);
1286
            String response = IOUtil.getAsString(r, true);
1287

    
1288
            if (!result) {
1289
                assertTrue(response.indexOf("<success>") == -1);
1290
            }
1291
            System.err.println(response);
1292
        }
1293
        catch (MetacatInaccessibleException mie) {
1294
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1295
        }
1296
        catch (InsufficientKarmaException ike) {
1297
            if (!expextedKarmaFailure) {
1298
                fail("Insufficient karma:\n" + ike.getMessage());
1299
            }
1300
        }
1301
        catch (MetacatException me) {
1302
            if (result) {
1303
                fail("Metacat Error:\n" + me.getMessage());
1304
            }
1305
            else {
1306
                System.err.println("Metacat Error:\n" + me.getMessage());
1307
            }
1308
        }
1309
        catch (Exception e) {
1310
            fail("General exception:\n" + e.getMessage());
1311
        }
1312
    }
1313

    
1314
    /**
1315
     * Read a document from metacat and check if it is equal to a given string.
1316
     * The expected result is passed as result
1317
     */
1318

    
1319
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1320
                                         boolean result,
1321
                                         boolean expextedKarmaFailure) {
1322
        try {
1323
            Reader r = m.read(docid);
1324
            String doc = IOUtil.getAsString(r, true);
1325
            if (result) {
1326

    
1327
                if (!testDoc.equals(doc)) {
1328
					System.out.println("doc ***********************");
1329
					System.out.println(doc);
1330
					System.out.println("end doc ***********************");
1331
					System.out.println("testDoc ***********************");
1332
					System.out.println(testDoc);
1333
					System.out.println("end testDoc ***********************");
1334
                }
1335

    
1336
                assertTrue(testDoc.equals(doc));
1337
            }
1338
            else {
1339
                assertTrue(doc.indexOf("<error>") != -1);
1340
            }
1341
        }
1342
        catch (MetacatInaccessibleException mie) {
1343
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1344
        }
1345
        catch (InsufficientKarmaException ike) {
1346
        	if (result) {
1347
                fail("Metacat Error:\n" + ike.getMessage());
1348
            }
1349
            else {
1350
                System.out.println("Metacat Error:\n" + ike.getMessage());
1351
            }
1352
        }
1353
        catch (MetacatException me) {
1354
            if (result) {
1355
                fail("Metacat Error:\n" + me.getMessage());
1356
            }
1357
            else {
1358
                System.out.println("Metacat Error:\n" + me.getMessage());
1359
            }
1360
        }
1361
        catch (Exception e) {
1362
            fail("General exception:\n" + e.getMessage());
1363
        }
1364

    
1365
    }
1366

    
1367
    /**
1368
     * Create a hopefully unique docid for testing insert and update. Does
1369
     * not include the 'revision' part of the id.
1370
     *
1371
     * @return a String docid based on the current date and time
1372
     */
1373
    private String generateDocid() {
1374
        StringBuffer docid = new StringBuffer(prefix);
1375
        docid.append(".");
1376

    
1377
        // Create a calendar to get the date formatted properly
1378
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1379
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1380
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1381
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1382
                       2 * 60 * 60 * 1000);
1383
        Calendar calendar = new GregorianCalendar(pdt);
1384
        Date trialTime = new Date();
1385
        calendar.setTime(trialTime);
1386
        docid.append(calendar.get(Calendar.YEAR));
1387
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1388
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1389
        docid.append(calendar.get(Calendar.MINUTE));
1390
        docid.append(calendar.get(Calendar.SECOND));
1391

    
1392
        return docid.toString();
1393
    }
1394
}
(5-5/19)