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-04-02 16:28:31 -0700 (Wed, 02 Apr 2008) $'
9
 * '$Revision: 3780 $'
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.metacat.MetaCatUtil;
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.utilities.IOUtil;
44
import junit.framework.Test;
45
import junit.framework.TestCase;
46
import junit.framework.TestSuite;
47
import java.io.File;
48

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

    
55
    private String metacatUrl = MetaCatUtil.getOption("metacatUrl");;
56
    private String username = MetaCatUtil.getOption("mcuser");
57
    private String password = MetaCatUtil.getOption("mcpassword");
58
    private String anotheruser = MetaCatUtil.getOption("mcanotheruser");
59
    private String anotherpassword = MetaCatUtil.getOption("mcanotherpassword");
60
    private String prefix = "test";
61
    private String newdocid = null;
62
    private String testdocument = "";
63

    
64
    private Metacat m;
65

    
66
    private boolean SUCCESS = true;
67
    private boolean FAILURE = false;
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 testEmlHeader =
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 testEmlCreatorBlock =
83
        "<creator scope=\"document\">                                       " +
84
        " <individualName>                                                  " +
85
        "    <surName>Smith</surName>                                       " +
86
        " </individualName>                                                 " +
87
        "</creator>                                                         ";
88

    
89
    private String testEmlContactBlock =
90
        "<contact scope=\"document\">                                       " +
91
        " <individualName>                                                  " +
92
        "    <surName>Jackson</surName>                                     " +
93
        " </individualName>                                                 " +
94
        "</contact>                                                         ";
95

    
96
    private String testEmlInlineBlock1 =
97
        "  <admin>                                                          " +
98
        "    <contact>                                                      " +
99
        "      <name>Operator</name>                                        " +
100
        "      <institution>PSI</institution>                               " +
101
        "    </contact>                                                     " +
102
        "  </admin>                                                         ";
103

    
104
    private String testEmlInlineBlock2 =
105
        "  <instrument>                                                     " +
106
        "    <instName>LCQ</instName>                                       " +
107
        "    <source type=\"ESI\"></source>                                 " +
108
        "    <detector type=\"EM\"></detector>                              " +
109
        "  </instrument>                                                    ";
110

    
111
    private String testdoc = testEmlHeader +
112
        "<dataset scope=\"document\"><title>the title</title>"
113
        +  testEmlCreatorBlock
114
        + "<distribution scope=\"document\" id=\"inlineEntity1\">"
115
        + "<inline>" + testEmlInlineBlock1 + "</inline></distribution>"
116
        + "<distribution scope=\"document\" id=\"inlineEntity2\">"
117
        + "<inline>" + testEmlInlineBlock2 + "</inline></distribution>"
118
        + testEmlContactBlock
119
        + getAccessBlock(anotheruser, true, false, false, false, true)
120
        + "</dataset><additionalMetadata><describes>inlineEntity2"
121
        + "</describes></additionalMetadata>"
122
        + "<additionalMetadata>"
123
        + "<describes>inlineEntity1</describes>"
124
        + "<describes>inlineEntity2</describes>"
125
        + getAccessBlock(anotheruser, false, false, false, false, true)
126
        + "</additionalMetadata></eml:eml>";
127

    
128

    
129
    /**
130
     * This function returns an access block based on the params passed
131
     */
132
    private String getAccessBlock(String principal, boolean grantAccess,
133
                                  boolean read, boolean write,
134
                                  boolean changePermission, boolean all) {
135
        String accessBlock = "<access " +
136
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
137
            " order=\"allowFirst\" scope=\"document\">";
138

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

    
146
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
147

    
148
        if (all) {
149
            accessBlock += "<permission>all</permission>";
150
        }
151
        else {
152
            if (read) {
153
                accessBlock += "<permission>read</permission>";
154
            }
155
            if (write) {
156
                accessBlock += "<permission>write</permission>";
157
            }
158
            if (changePermission) {
159
                accessBlock += "<permission>changePermission</permission>";
160
            }
161
        }
162

    
163
        if (grantAccess) {
164
            accessBlock += "</allow>";
165
        }
166
        else {
167
            accessBlock += "</deny>";
168
        }
169
        accessBlock += "</access>";
170

    
171
        return accessBlock;
172

    
173
    }
174

    
175
    /**
176
     * This function returns a valid eml document with no access rules
177
     * This function is for eml-2.0.1 only. For other eml versions,
178
     * this function might have to modified
179
     */
180
    private String getTestEmlDoc(String title, String inlineData1,
181
                                 String inlineData2, String OnlineUrl1,
182
                                 String OnlineUrl2, String docAccessBlock,
183
                                 String inlineAccessBlock1,
184
                                 String inlineAccessBlock2,
185
                                 String OnlineAccessBlock1,
186
                                 String OnlineAccessBlock2) {
187

    
188
        String testDocument = "";
189
        testDocument = testDocument + testEmlHeader +
190
            "<dataset scope=\"document\"><title>" + title + "</title>" +
191
            testEmlCreatorBlock;
192

    
193
        if (inlineData1 != null) {
194
            testDocument = testDocument
195
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
196
                + "<inline>" + inlineData1 + "</inline></distribution>";
197
        }
198
        if (inlineData2 != null) {
199
            testDocument = testDocument
200
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
201
                + "<inline>" + inlineData2 + "</inline></distribution>";
202
        }
203
        if (OnlineUrl1 != null) {
204
            testDocument = testDocument
205
                + "<distribution scope=\"document\" id=\"InlineEntity1\">"
206
                + "<Inline><url function=\"download\">"
207
                + OnlineUrl1 + "</url></Inline></distribution>";
208
        }
209
        if (OnlineUrl2 != null) {
210
            testDocument = testDocument +
211
                "<distribution scope=\"document\" id=\"InlineEntity2\">"
212
                + "<Inline><url function=\"download\">"
213
                + OnlineUrl2 + "</url></Inline></distribution>";
214
        }
215
        testDocument += testEmlContactBlock;
216

    
217
        if (docAccessBlock != null) {
218
            testDocument += docAccessBlock;
219
        }
220

    
221
        testDocument += "</dataset>";
222

    
223
        if (inlineAccessBlock1 != null) {
224
            testDocument += "<additionalMetadata>";
225
            testDocument += "<describes>inlineEntity1</describes>";
226
            testDocument += inlineAccessBlock1;
227
            testDocument += "</additionalMetadata>";
228
        }
229

    
230
        if (inlineAccessBlock2 != null) {
231
            testDocument += "<additionalMetadata>";
232
            testDocument += "<describes>inlineEntity2</describes>";
233
            testDocument += inlineAccessBlock2;
234
            testDocument += "</additionalMetadata>";
235
        }
236

    
237
        if (OnlineAccessBlock1 != null) {
238
            testDocument += "<additionalMetadata>";
239
            testDocument += "<describes>InlineEntity1</describes>";
240
            testDocument += OnlineAccessBlock1;
241
            testDocument += "</additionalMetadata>";
242
        }
243

    
244
        if (OnlineAccessBlock2 != null) {
245
            testDocument += "<additionalMetadata>";
246
            testDocument += "<describes>InlineEntity2</describes>";
247
            testDocument += OnlineAccessBlock2;
248
            testDocument += "</additionalMetadata>";
249
        }
250

    
251
        testDocument += "</eml:eml>";
252

    
253
        //System.out.println("Returning following document" + testDocument);
254
        return testDocument;
255
    }
256

    
257
    /**
258
     * Constructor to build the test
259
     *
260
     * @param name the name of the test method
261
     */
262
    public InlineDataAccessTest(String name) {
263
        super(name);
264
        newdocid = generateDocid();
265
    }
266

    
267
    /**
268
     * Establish a testing framework by initializing appropriate objects
269
     */
270
    public void setUp() {
271
        try {
272
            System.err.println("Test Metacat: " + metacatUrl);
273
            m = MetacatFactory.createMetacatConnection(metacatUrl);
274
        }
275
        catch (MetacatInaccessibleException mie) {
276
            System.err.println("Metacat is: " + metacatUrl);
277
            fail("Metacat connection failed." + mie.getMessage());
278
        }
279
    }
280

    
281
    /**
282
     * Release any objects after tests are complete
283
     */
284
    public void tearDown() {
285
    }
286

    
287
    /**
288
     * Create a suite of tests to be run together
289
     */
290
    public static Test suite() {
291
        TestSuite suite = new TestSuite();
292
        suite.addTest(new InlineDataAccessTest("initialize"));
293

    
294
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_1"));
295
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_2"));
296
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_3"));
297
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_4"));
298
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_5"));
299
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_6"));
300

    
301
        return suite;
302
    }
303

    
304

    
305
    /**
306
     * Run an initial test that always passes to check that the test
307
     * harness is working.
308
     */
309
    public void initialize() {
310
        assertTrue(1 == 1);
311
    }
312

    
313
    /** *********
314
     * Checking the following cases:
315
     * when only Inline data is uploaded by a user and
316
     * -> he tries to read it  - success
317
     * -> he tries to add same docid again  - failure
318
     * -> he tries to update it  - success
319
     * -> he removes it and adds it again - success
320
     * -> he tries to delete it  - success
321
     * -> he tries to read it after deleteing - failure
322
     */
323
    public void inlineDataCasesTest_1() {
324
        try {
325
            newdocid = generateDocid();
326
            m.login(username, password);
327

    
328
            // insert a document
329
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
330
                                         null, null, null, null,
331
                                         null, null, null, null);
332

    
333
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
334
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
335

    
336
            // insert same document again
337
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
338

    
339
            // update by changing inline data
340
            testdocument = getTestEmlDoc("Testing update inline",
341
                                         testEmlInlineBlock2,
342
                                         null, null, null, null,
343
                                         null, null, null, null);
344
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
345
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS, true);
346

    
347
            // update by removing inline data
348
            testdocument = getTestEmlDoc("Testing update inline",
349
                                         null,
350
                                         null, null, null, null,
351
                                         null, null, null, null);
352
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
353
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, true);
354

    
355
            // update by introducing inline data
356
            testdocument = getTestEmlDoc("Testing update inline",
357
                                         testEmlInlineBlock1,
358
                                         null, null, null, null,
359
                                         null, null, null, null);
360
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
361
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, true);
362

    
363
            // read inline data only
364
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
365
                                         testEmlInlineBlock1, SUCCESS, false);
366

    
367
            // delete the inline data
368
            // sleep needed only in case of inline data - jing said that
369
            // somehow the thread writing data to xml_index takes too much time
370
            // when used with inline data. hence if delete is requested too soon
371
            // database gives an error of xml_index records left with FK to
372
            // xml_document record which is going to be deleted.
373
            Thread.sleep(10000);
374

    
375
            deleteDocid(newdocid + ".4", SUCCESS, false);
376
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, FAILURE, false);
377
            m.logout();
378
        }
379
        catch (MetacatAuthException mae) {
380
            fail("Authorization failed:\n" + mae.getMessage());
381
        }
382
        catch (MetacatInaccessibleException mie) {
383
            fail("Metacat Inaccessible:\n" + mie.getMessage());
384
        }
385
        catch (Exception e) {
386
            fail("General exception:\n" + e.getMessage());
387
        }
388
    }
389

    
390
    /** *********
391
     * Checking the following cases:
392
     * when only inline data is uploaded by a user and another user
393
     * -> tries to read it  - failure
394
     * -> tries to read inline data only - failure
395
     * -> tries to update - failure
396
     * -> tries to delete it  - failure
397
     */
398
    public void inlineDataCasesTest_2() {
399
        try {
400
            newdocid = generateDocid();
401
            m.login(username, password);
402

    
403
            // insert a document
404
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
405
                                         null, null, null, null,
406
                                         null, null, null, null);
407

    
408
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
409
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
410

    
411
            // login as another user
412
            m.logout();
413
            m.login(anotheruser, anotherpassword);
414

    
415
            // try to read document or data only
416
            readDocidWhichEqualsDoc(newdocid, testdocument, FAILURE, true);
417
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
418
                                         testEmlInlineBlock1, FAILURE, true);
419

    
420
            // try to update the document
421
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
422
                                         null, null, null, null,
423
                                         null, null, null, null);
424
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
425

    
426
            // try to delete the document
427
            deleteDocid(newdocid + ".1", FAILURE, true);
428
            m.logout();
429

    
430
            // delete the document
431
            m.login(username, password);
432
            deleteDocid(newdocid + ".1", SUCCESS, false);
433
            m.logout();
434

    
435
        }
436
        catch (MetacatAuthException mae) {
437
            fail("Authorization failed:\n" + mae.getMessage());
438
        }
439
        catch (MetacatInaccessibleException mie) {
440
            fail("Metacat Inaccessible:\n" + mie.getMessage());
441
        }
442
        catch (Exception e) {
443
            fail("General exception:\n" + e.getMessage());
444
        }
445
    }
446

    
447
    /** *********
448
     * Checking the following cases:
449
     * when only inline data is uploaded by a user with the following different
450
     * access controls in another document
451
     *   1.read
452
     *   2.write
453
     *   3.change permission
454
     *   4.all
455
     * And another user tries to do the following:
456
     * -> tries to read it
457
     * -> tries to update it
458
     * -> tries to set permissions on it
459
     * -> tries to delete it
460
     */
461
    public void inlineDataCasesTest_3() {
462
        try {
463

    
464
            /////////Case 1./////////////////////
465
            // insert an inline document - read only
466
            m.login(username, password);
467
            newdocid = generateDocid();
468

    
469
            // insert a document which gives read access to the inline document
470
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
471
                                         testEmlInlineBlock1, null, null,
472
                                         null, getAccessBlock(anotheruser, true,
473
                true, false, false, false),
474
                                         null, null, null, null);
475
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
476
            m.logout();
477

    
478
            // login as another user
479
            m.login(anotheruser, anotherpassword);
480

    
481
            // try to read the document and the inline data
482
            readDocid(newdocid + ".1", SUCCESS, false);
483
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
484
                                         testEmlInlineBlock1, SUCCESS, false);
485

    
486
            // try to update the inline data
487
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
488
                                         testEmlInlineBlock2, null, null,
489
                                         null, getAccessBlock(anotheruser, true,
490
                true, false, false, false),
491
                                         null, null, null, null);
492
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
493

    
494
            // try to set the permissions for the inline data
495
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
496
                                         testEmlInlineBlock1, null, null,
497
                                         null, getAccessBlock(anotheruser, true,
498
                false, false, false, true),
499
                                         null, null, null, null);
500
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
501

    
502
            // try to delete the document
503
            deleteDocid(newdocid + ".1", FAILURE, true);
504
            m.logout();
505

    
506
            // delete the document
507
            m.login(username, password);
508
            deleteDocid(newdocid + ".1", SUCCESS, false);
509
            m.logout();
510

    
511
            /////////Case 2./////////////////////
512
            // insert an inline document - write only
513
            m.login(username, password);
514
            newdocid = generateDocid();
515

    
516
            // insert a document which gives read access to the inline document
517
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
518
                                         testEmlInlineBlock1, null, null,
519
                                         null, getAccessBlock(anotheruser, true,
520
                                         false, true, false, false),
521
                                         null, null, null, null);
522
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
523
            m.logout();
524

    
525
            // login as another user
526
            m.login(anotheruser, anotherpassword);
527

    
528
            // try to read the document and the inline data
529
            readDocid(newdocid + ".1", FAILURE, true);
530
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
531
                                         testEmlInlineBlock1, FAILURE, true);
532

    
533
            // try to update the inline data
534
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
535
                                         testEmlInlineBlock2, null, null,
536
                                         null, getAccessBlock(anotheruser, true,
537
                                         false, true, false, false),
538
                                         null, null, null, null);
539
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
540

    
541
            // try to set the permissions for the inline data
542
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
543
                                         testEmlInlineBlock1, null, null,
544
                                         null, getAccessBlock(anotheruser, true,
545
                false, false, false, true),
546
                                         null, null, null, null);
547
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
548

    
549
            // try to delete the document
550
            deleteDocid(newdocid + ".2", FAILURE, true);
551
            m.logout();
552

    
553
            // delete the document
554
            m.login(username, password);
555
            deleteDocid(newdocid + ".2", SUCCESS, false);
556
            m.logout();
557

    
558
            /////////Case 3./////////////////////
559
            // insert an inline document - change permission only
560
            m.login(username, password);
561
            newdocid = generateDocid();
562

    
563
            // insert a document which gives read access to the inline document
564
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
565
                                         testEmlInlineBlock1, null, null,
566
                                         null, getAccessBlock(anotheruser, true,
567
                false, false, true, false),
568
                                         null, null, null, null);
569
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
570
            m.logout();
571

    
572
            // login as another user
573
            m.login(anotheruser, anotherpassword);
574

    
575
            // try to read the document and the inline data
576
            readDocid(newdocid + ".1", FAILURE, true);
577
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
578
                                         testEmlInlineBlock1, FAILURE, true);
579

    
580
            // try to update the inline data
581
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
582
                                         testEmlInlineBlock2, null, null,
583
                                         null, getAccessBlock(anotheruser, true,
584
                false, false, true, false),
585
                                         null, null, null, null);
586
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
587

    
588
            // try to set the permissions for the inline data
589
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
590
                                         testEmlInlineBlock1, null, null,
591
                                         null, getAccessBlock(anotheruser, true,
592
                false, false, false, true),
593
                                         null, null, null, null);
594
            // ERRRRRRRRRRRR
595
            //updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
596

    
597
            // try to delete the document
598
            deleteDocid(newdocid + ".1", FAILURE, true);
599
            m.logout();
600

    
601
            // delete the document
602
            m.login(username, password);
603
            deleteDocid(newdocid + ".1", SUCCESS, false);
604
            m.logout();
605

    
606

    
607
            /////////Case 4./////////////////////
608
            // insert an inline document - change permission only
609
            m.login(username, password);
610
            newdocid = generateDocid();
611

    
612
            // insert a document which gives read access to the inline document
613
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
614
                                         testEmlInlineBlock1, null, null,
615
                                         null, getAccessBlock(anotheruser, true,
616
                                         false, false, false, true),
617
                                         null, null, null, null);
618
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
619
            m.logout();
620

    
621
            // login as another user
622
            m.login(anotheruser, anotherpassword);
623

    
624
            // try to read the document and the inline data
625
            readDocid(newdocid + ".1", SUCCESS, false);
626
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
627
                                         testEmlInlineBlock1, SUCCESS, false);
628

    
629
            // try to update the inline data
630
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
631
                                         testEmlInlineBlock2, null, null,
632
                                         null, getAccessBlock(anotheruser, true,
633
                                         false, false, false, true),
634
                                         null, null, null, null);
635
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
636

    
637
            // try to set the permissions for the inline data
638
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
639
                                         testEmlInlineBlock1, null, null,
640
                                         null, getAccessBlock(anotheruser, true,
641
                                         true, true, true, false),
642
                                         null, null, null, null);
643
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
644

    
645
            // try to delete the document
646
            // sleep needed only in case of inline data - jing said that
647
            // somehow the thread writing data to xml_index takes too much time
648
            // when used with inline data. hence if delete is requested too soon
649
            // database gives an error of xml_index records left with FK to
650
            // xml_document record which is going to be deleted.
651
            Thread.sleep(10000);
652

    
653
            deleteDocid(newdocid + ".3", SUCCESS, false);
654
            m.logout();
655

    
656
        }
657
        catch (MetacatAuthException mae) {
658
            fail("Authorization failed:\n" + mae.getMessage());
659
        }
660
        catch (MetacatInaccessibleException mie) {
661
            fail("Metacat Inaccessible:\n" + mie.getMessage());
662
        }
663
        catch (Exception e) {
664
            fail("General exception:\n" + e.getMessage());
665
        }
666
    }
667

    
668
    /** *********
669
     * Checking the following cases:
670
     * when only Inline data is uploaded by a user with the following different
671
     * access controls specified in addiotnal metadata in another document
672
     *   1.read
673
     *   2.write
674
     *   3.change permission
675
     *   4.all
676
     * And another user tries to do the following:
677
     * -> tries to read it
678
     * -> tries to update it
679
     * -> tries to set permissions on it
680
     * -> tries to delete it
681
     */
682
    public void inlineDataCasesTest_4() {
683
        try {
684
            /////////Case 1./////////////////////
685
            // insert an inline document - read only
686
            m.login(username, password);
687
            newdocid = generateDocid();
688

    
689
            // insert a document which gives read access to the inline document
690
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
691
                                         testEmlInlineBlock1, null, null, null,
692
                                         null, getAccessBlock(anotheruser, true,
693
                                         true, false, false, false),
694
                                         null, null, null);
695
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
696
            m.logout();
697

    
698
            // login as another user
699
            m.login(anotheruser, anotherpassword);
700

    
701
            // try to read the document and the inline data
702
            readDocid(newdocid + ".1", FAILURE, true);
703
            // ERRRRRRRRRRRRRRR
704
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
705
                                         testEmlInlineBlock1, FAILURE, true);
706

    
707
            // try to update the inline data
708
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
709
                                         testEmlInlineBlock2, null, null,null,
710
                                         null, getAccessBlock(anotheruser, true,
711
                                         true, false, false, false),
712
                                         null, null, null);
713
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
714

    
715
            // try to set the permissions for the inline data
716
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
717
                                         testEmlInlineBlock1, null, null,null,
718
                                         null, getAccessBlock(anotheruser, true,
719
                                         false, false, false, true),
720
                                         null, null, null);
721
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
722

    
723
            // try to delete the document
724
            deleteDocid(newdocid + ".1", FAILURE, true);
725
            m.logout();
726

    
727
            // delete the document
728
            m.login(username, password);
729
            deleteDocid(newdocid + ".1", SUCCESS, false);
730
            m.logout();
731

    
732
            /////////Case 2./////////////////////
733
            // insert an inline document - write only
734
            m.login(username, password);
735
            newdocid = generateDocid();
736

    
737
            // insert a document which gives read access to the inline document
738
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
739
                                         testEmlInlineBlock1, null, null, null,
740
                                         null, getAccessBlock(anotheruser, true,
741
                                         false, true, false, false),
742
                                         null, null, null);
743
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
744
            m.logout();
745

    
746
            // login as another user
747
            m.login(anotheruser, anotherpassword);
748

    
749
            // try to read the document and the inline data
750
            readDocid(newdocid + ".1", FAILURE, true);
751
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
752
                                         testEmlInlineBlock1, FAILURE, true);
753

    
754
            // try to update the inline data
755
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
756
                                         testEmlInlineBlock2, null, null, null,
757
                                         null, getAccessBlock(anotheruser, true,
758
                                         false, true, false, false),
759
                                         null, null, null);
760
            //ERRRRRRRRRRRRRRRRRRRRRRRR
761
            // updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
762

    
763
            // try to set the permissions for the inline data
764
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
765
                                         testEmlInlineBlock1, null, null, null,
766
                                         null, getAccessBlock(anotheruser, true,
767
                                         false, false, false, true),
768
                                         null, null, null);
769
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
770

    
771
            // try to delete the document
772
            deleteDocid(newdocid + ".1", FAILURE, true);
773
            m.logout();
774

    
775
            // delete the document
776
            m.login(username, password);
777
            deleteDocid(newdocid + ".1", SUCCESS, false);
778
            m.logout();
779

    
780
            /////////Case 3./////////////////////
781
            // insert an inline document - change permission only
782
            m.login(username, password);
783
            newdocid = generateDocid();
784

    
785
            // insert a document which gives read access to the inline document
786
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
787
                                         testEmlInlineBlock1, null, null, null,
788
                                         null, getAccessBlock(anotheruser, true,
789
                                         false, false, true, false),
790
                                         null, null, null);
791
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
792
            m.logout();
793

    
794
            // login as another user
795
            m.login(anotheruser, anotherpassword);
796

    
797
            // try to read the document and the inline data
798
            readDocid(newdocid + ".1", FAILURE, true);
799
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
800
                                         testEmlInlineBlock1, FAILURE, true);
801

    
802
            // try to update the inline data
803
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
804
                                         testEmlInlineBlock2, null, null, null,
805
                                         null, getAccessBlock(anotheruser, true,
806
                                         false, false, true, false),
807
                                         null, null, null);
808
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
809

    
810
            // try to set the permissions for the inline data
811
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
812
                                         testEmlInlineBlock1, null, null, null,
813
                                         null, getAccessBlock(anotheruser, true,
814
                                         false, false, false, true),
815
                                         null, null, null);
816
            // ERRRRRRRRRRRR
817
            //updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
818

    
819
            // try to delete the document
820
            deleteDocid(newdocid + ".1", FAILURE, true);
821
            m.logout();
822

    
823
            // delete the document
824
            m.login(username, password);
825
            deleteDocid(newdocid + ".1", SUCCESS, false);
826
            m.logout();
827

    
828

    
829
            /////////Case 4./////////////////////
830
            // insert an inline document - change permission only
831
            m.login(username, password);
832
            newdocid = generateDocid();
833

    
834
            // insert a document which gives read access to the inline document
835
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
836
                                         testEmlInlineBlock1, null, null, null,
837
                                         null, getAccessBlock(anotheruser, true,
838
                                         false, false, false, true),
839
                                         null, null, null);
840
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
841
            m.logout();
842

    
843
            // login as another user
844
            m.login(anotheruser, anotherpassword);
845

    
846
            // try to read the document and the inline data
847
            readDocid(newdocid + ".1", FAILURE, true);
848
            // ERRRRRRRRRRRRRRR
849
            //readInlineDataWhichEqualsDoc(newdocid + ".1.1",
850
            //                             testEmlInlineBlock1, SUCCESS, false);
851

    
852
            // try to update the inline data
853
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
854
                                         testEmlInlineBlock2, null, null, null,
855
                                         null, getAccessBlock(anotheruser, true,
856
                                         false, false, false, true),
857
                                         null, null, null);
858
            // ERRRRRR
859
            // updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
860

    
861
            // try to set the permissions for the inline data
862
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
863
                                         testEmlInlineBlock1, null, null, null,
864
                                         null, getAccessBlock(anotheruser, true,
865
                                         true, true, true, false),
866
                                         null, null, null);
867
            // ERRRRRRRRRR
868
            // updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
869

    
870
            // try to delete the document
871
            deleteDocid(newdocid + ".3", FAILURE, true);
872
            m.logout();
873

    
874
            // delete the document
875
            m.login(username, password);
876
            deleteDocid(newdocid + ".1", SUCCESS, false);
877
            m.logout();
878

    
879
        }
880
        catch (MetacatAuthException mae) {
881
            fail("Authorization failed:\n" + mae.getMessage());
882
        }
883
        catch (MetacatInaccessibleException mie) {
884
            fail("Metacat Inaccessible:\n" + mie.getMessage());
885
        }
886
        catch (Exception e) {
887
            fail("General exception:\n" + e.getMessage());
888
        }
889
    }
890

    
891
    /** *********
892
     * Checking the following cases:
893
     * -> when no inline data is specified in the document but
894
     *    rules are specified in additional metadata
895
     * -> when a user has RW permission for inline data, can he delete it
896
     * -> when inline data with document refering to it is uploaded with read
897
     *    access for metadata and no access for data
898
     */
899
    public void inlineDataCasesTest_5() {
900
        try {
901

    
902
            m.login(username, password);
903

    
904
            /////////Case 1
905
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
906
                                         null, null,
907
                                         null, null, null,
908
                                         getAccessBlock(anotheruser, true,
909
                true, false, false, false), null, null, null);
910
            newdocid = generateDocid();
911

    
912
            // try to insert the wrong document
913
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
914
            m.logout();
915

    
916

    
917
            /////////Case 2
918
            m.login(username, password);
919
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
920
                                         testEmlInlineBlock1, null, null,
921
                                         null, getAccessBlock(anotheruser, true,
922
                                         true, true, false, false), null, null,
923
                                         null, null);
924
            newdocid = generateDocid();
925
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
926
            m.logout();
927

    
928
            m.login(anotheruser, anotherpassword);
929
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
930
                                         null, null, null,
931
                                         null, getAccessBlock(anotheruser, true,
932
                                         true, true, false, false), null, null,
933
                                         null, null);
934
            /// ERRRRRRRRRRRR
935
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
936

    
937
            /////////Case 3
938

    
939
            // insert a document
940
            m.login(username, password);
941
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
942
                                         testEmlInlineBlock1, null, null,
943
                                         null, getAccessBlock(anotheruser, true,
944
                                         false, false, false, true),
945
                                         getAccessBlock(anotheruser, false,
946
                                         false, false, false, true), null,
947
                                         null, null);
948
            newdocid = generateDocid();
949
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
950
            m.logout();
951

    
952

    
953
            // try to read the Inline data
954
            m.login(anotheruser, anotherpassword);
955
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
956
                             "", null, null,
957
                             null, getAccessBlock(anotheruser, true,
958
                             false, false, false, true),
959
                             getAccessBlock(anotheruser, false,
960
                             false, false, false, true), null,
961
                             null, null);
962
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument,
963
                                    SUCCESS, false);
964

    
965
            // try to update the rules for inline data
966
            /// ERRRRRRRRRRRR it lets you do that
967
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
968
                                         testEmlInlineBlock1, null, null,
969
                                         null, getAccessBlock(anotheruser, true,
970
                                         false, false, false, true),
971
                                         getAccessBlock(anotheruser, true,
972
                                         false, false, false, true), null,
973
                                         null, null);
974
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
975
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument,
976
                                    SUCCESS, false);
977

    
978
            m.logout();
979
        }
980
        catch (MetacatAuthException mae) {
981
            fail("Authorization failed:\n" + mae.getMessage());
982
        }
983
        catch (MetacatInaccessibleException mie) {
984
            fail("Metacat Inaccessible:\n" + mie.getMessage());
985
        }
986
        catch (Exception e) {
987
            fail("General exception:\n" + e.getMessage());
988
        }
989
    }
990

    
991

    
992
    /** *********
993
       * Checking the following cases:
994
       * -> when inline data in inserted and updated, do access rules apply
995
       *    to the old document
996
       */
997
      public void inlineDataCasesTest_6() {
998
          try {
999

    
1000
              // insert the document
1001
              m.login(username, password);
1002
              testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
1003
                                           testEmlInlineBlock1, testEmlInlineBlock2,
1004
                                           null, null,
1005
                                           getAccessBlock(anotheruser, true,
1006
                                           true, true, false, false),
1007
                                           getAccessBlock(anotheruser, false,
1008
                                           false, false, false, true),
1009
                                           getAccessBlock(anotheruser, false,
1010
                                           false, false, false, true),
1011
                                           null, null);
1012
              newdocid = generateDocid();
1013
              insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1014

    
1015
              m.logout();
1016

    
1017
              // update the document
1018
              m.login(anotheruser, anotherpassword);
1019
              testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
1020
                                           testEmlInlineBlock2, testEmlInlineBlock1,
1021
                                           null, null,
1022
                                           getAccessBlock(anotheruser, true,
1023
                                           true, true, false, false),
1024
                                           getAccessBlock(anotheruser, false,
1025
                                           false, false, false, true),
1026
                                           getAccessBlock(anotheruser, false,
1027
                                           false, false, false, true),
1028
                                           null, null);
1029

    
1030

    
1031
              updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1032

    
1033
              // try reading the inline document
1034
              readInlineDataWhichEqualsDoc(newdocid + ".2.1",
1035
                             testEmlInlineBlock1, FAILURE, true);
1036

    
1037
              System.out.print("Trying to read " + newdocid + ".1.1");
1038
              readInlineDataWhichEqualsDoc(newdocid + ".1.1",
1039
                             testEmlInlineBlock1, FAILURE, true);
1040

    
1041
              m.logout();
1042
          }
1043
          catch (MetacatAuthException mae) {
1044
              fail("Authorization failed:\n" + mae.getMessage());
1045
          }
1046
          catch (MetacatInaccessibleException mie) {
1047
              fail("Metacat Inaccessible:\n" + mie.getMessage());
1048
          }
1049
          catch (Exception e) {
1050
              fail("General exception:\n" + e.getMessage());
1051
          }
1052
      }
1053

    
1054

    
1055
    /**
1056
     * Insert a document into metacat. The expected result is passed as result
1057
     */
1058

    
1059
    private String insertDocid(String docid, String docText, boolean result,
1060
                               boolean expectKarmaException) {
1061
        String response = null;
1062
        try {
1063
            response = m.insert(docid,
1064
                                new StringReader(testdocument), null);
1065
            if (result) {
1066
                assertTrue( (response.indexOf("<success>") != -1));
1067
                assertTrue(response.indexOf(docid) != -1);
1068
            }
1069
            else {
1070
                assertTrue( (response.indexOf("<success>") == -1));
1071
            }
1072
            System.err.println(response);
1073
        }
1074
        catch (MetacatInaccessibleException mie) {
1075
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1076
        }
1077
        catch (InsufficientKarmaException ike) {
1078
            if (!expectKarmaException) {
1079
                fail("Insufficient karma:\n" + ike.getMessage());
1080
            }
1081
        }
1082
        catch (MetacatException me) {
1083
            if (result) {
1084
                fail("Metacat Error:\n" + me.getMessage());
1085
            }
1086
            else {
1087
                System.err.println("Metacat Error: " + me.getMessage());
1088
            }
1089
        }
1090
        catch (Exception e) {
1091
            fail("General exception:\n" + e.getMessage());
1092
        }
1093
        return response;
1094
    }
1095

    
1096
    /**
1097
     * Insert a document into metacat. The expected result is passed as result
1098
     */
1099

    
1100
    private String uploadDocid(String docid, String filePath, boolean result,
1101
                               boolean expectedKarmaException) {
1102
        String response = null;
1103
        try {
1104
            response = m.upload(docid, new File(filePath));
1105
            if (result) {
1106
                assertTrue( (response.indexOf("<success>") != -1));
1107
                assertTrue(response.indexOf(docid) != -1);
1108
            }
1109
            else {
1110
                assertTrue( (response.indexOf("<success>") == -1));
1111
            }
1112
            System.err.println("respose from metacat: " + response);
1113
        }
1114
        catch (MetacatInaccessibleException mie) {
1115
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1116
        }
1117
        catch (InsufficientKarmaException ike) {
1118
            if (!expectedKarmaException) {
1119
                fail("Insufficient karma:\n" + ike.getMessage());
1120
            }
1121
        }
1122
        catch (MetacatException me) {
1123
            if (result) {
1124
                fail("Metacat Error:\n" + me.getMessage());
1125
            }
1126
            else {
1127
                System.err.println("Metacat Error: " + me.getMessage());
1128
            }
1129
        }
1130
        catch (Exception e) {
1131
            fail("General exception:\n" + e.getMessage());
1132
        }
1133
        return response;
1134
    }
1135

    
1136
    /**
1137
     * Update a document in metacat. The expected result is passed as result
1138
     */
1139
    private String updateDocid(String docid, String docText, boolean result,
1140
                               boolean expectedKarmaFailure) {
1141
        String response = null;
1142
        try {
1143
            response = m.update(docid,
1144
                                new StringReader(testdocument), null);
1145

    
1146
            if (result) {
1147
                assertTrue( (response.indexOf("<success>") != -1));
1148
                assertTrue(response.indexOf(docid) != -1);
1149
            }
1150
            else {
1151
                assertTrue( (response.indexOf("<success>") == -1));
1152
            }
1153
            System.err.println(response);
1154
        }
1155
        catch (MetacatInaccessibleException mie) {
1156
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1157
        }
1158
        catch (InsufficientKarmaException ike) {
1159
            if (!expectedKarmaFailure) {
1160
                fail("Insufficient karma:\n" + ike.getMessage());
1161
            }
1162
        }
1163
        catch (MetacatException me) {
1164
            if (! (expectedKarmaFailure &&
1165
                   (me.getMessage().indexOf(
1166
                "User try to update a access module which it doesn't have \"ALL\" permission") !=
1167
                    -1))) {
1168
                fail("Metacat Error:\n" + me.getMessage());
1169
            }
1170
        }
1171
        catch (Exception e) {
1172
            fail("General exception:\n" + e.getMessage());
1173
        }
1174

    
1175
        return response;
1176
    }
1177

    
1178
    /**
1179
     * Delete a document into metacat. The expected result is passed as result
1180
     */
1181
    private void deleteDocid(String docid, boolean result,
1182
                             boolean expectedKarmaFailure) {
1183
        try {
1184
            String response = m.delete(docid);
1185
            if (result) {
1186
                assertTrue(response.indexOf("<success>") != -1);
1187
            }
1188
            else {
1189
                assertTrue(response.indexOf("<success>") == -1);
1190
            }
1191
            System.err.println(response);
1192
        }
1193
        catch (MetacatInaccessibleException mie) {
1194
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1195
        }
1196
        catch (InsufficientKarmaException ike) {
1197
            if (!expectedKarmaFailure) {
1198
                fail("Insufficient karma:\n" + ike.getMessage());
1199
            }
1200

    
1201
        }
1202
        catch (MetacatException me) {
1203
            if (result) {
1204
                fail("Metacat Error:\n" + me.getMessage());
1205
            }
1206
            else {
1207
                System.err.println("Metacat Error:\n" + me.getMessage());
1208
            }
1209
        }
1210
        catch (Exception e) {
1211
            fail("General exception:\n" + e.getMessage());
1212
        }
1213
    }
1214

    
1215
    /**
1216
     * Read inline data from metacat. The expected result is passed as result
1217
     */
1218
    private void readInlineDataWhichEqualsDoc(String docid, String testDoc,
1219
                                              boolean result,
1220
                                              boolean expextedKarmaFailure) {
1221
        try {
1222
            Reader r = m.readInlineData(docid);
1223
            String doc = IOUtil.getAsString(r, true);
1224

    
1225
            if (result) {
1226
                if (!testDoc.equals(doc)) {
1227
                    System.out.println("doc    :" + doc);
1228
                    System.out.println("testDoc:" + testDoc);
1229
                }
1230

    
1231
                assertTrue(testDoc.equals(doc));
1232
            }
1233
            else {
1234
                System.out.println("doc    :" + doc);
1235
                System.out.println("testDoc:" + testDoc);
1236
                assertTrue(doc.indexOf("<error>") != -1);
1237
            }
1238
        }
1239
        catch (MetacatInaccessibleException mie) {
1240
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1241
        }
1242
        catch (InsufficientKarmaException ike) {
1243
            if (!expextedKarmaFailure) {
1244
                fail("Insufficient karma:\n" + ike.getMessage());
1245
            }
1246
        }
1247
        catch (MetacatException me) {
1248
            if (result) {
1249
                fail("Metacat Error:\n" + me.getMessage());
1250
            }
1251
            else {
1252
                System.err.println("Metacat Error:\n" + me.getMessage());
1253
            }
1254
        }
1255
        catch (Exception e) {
1256
            fail("General exception:\n" + e.getMessage());
1257
        }
1258
    }
1259

    
1260
    /**
1261
     * Read a document from metacat. The expected result is passed as result
1262
     */
1263
    private void readDocid(String docid, boolean result,
1264
                           boolean expextedKarmaFailure) {
1265
        try {
1266
            Reader r = m.read(docid);
1267
            String response = IOUtil.getAsString(r, true);
1268

    
1269
            if (!result) {
1270
                assertTrue(response.indexOf("<success>") == -1);
1271
            }
1272
            System.err.println(response);
1273
        }
1274
        catch (MetacatInaccessibleException mie) {
1275
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1276
        }
1277
        catch (InsufficientKarmaException ike) {
1278
            if (!expextedKarmaFailure) {
1279
                fail("Insufficient karma:\n" + ike.getMessage());
1280
            }
1281
        }
1282
        catch (MetacatException me) {
1283
            if (result) {
1284
                fail("Metacat Error:\n" + me.getMessage());
1285
            }
1286
            else {
1287
                System.err.println("Metacat Error:\n" + me.getMessage());
1288
            }
1289
        }
1290
        catch (Exception e) {
1291
            fail("General exception:\n" + e.getMessage());
1292
        }
1293
    }
1294

    
1295
    /**
1296
     * Read a document from metacat and check if it is equal to a given string.
1297
     * The expected result is passed as result
1298
     */
1299

    
1300
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1301
                                         boolean result,
1302
                                         boolean expextedKarmaFailure) {
1303
        try {
1304
            Reader r = m.read(docid);
1305
            String doc = IOUtil.getAsString(r, true);
1306
            if (result) {
1307

    
1308
                if (!testDoc.equals(doc)) {
1309
                    System.out.println("doc    :" + doc);
1310
                    System.out.println("testDoc:" + testDoc);
1311
                }
1312

    
1313
                assertTrue(testDoc.equals(doc));
1314
            }
1315
            else {
1316
                assertTrue(doc.indexOf("<error>") != -1);
1317
            }
1318
        }
1319
        catch (MetacatInaccessibleException mie) {
1320
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1321
        }
1322
        catch (InsufficientKarmaException ike) {
1323
        	if (result) {
1324
                fail("Metacat Error:\n" + ike.getMessage());
1325
            }
1326
            else {
1327
                System.out.println("Metacat Error:\n" + ike.getMessage());
1328
            }
1329
        }
1330
        catch (MetacatException me) {
1331
            if (result) {
1332
                fail("Metacat Error:\n" + me.getMessage());
1333
            }
1334
            else {
1335
                System.out.println("Metacat Error:\n" + me.getMessage());
1336
            }
1337
        }
1338
        catch (Exception e) {
1339
            fail("General exception:\n" + e.getMessage());
1340
        }
1341

    
1342
    }
1343

    
1344
    /**
1345
     * Create a hopefully unique docid for testing insert and update. Does
1346
     * not include the 'revision' part of the id.
1347
     *
1348
     * @return a String docid based on the current date and time
1349
     */
1350
    private String generateDocid() {
1351
        StringBuffer docid = new StringBuffer(prefix);
1352
        docid.append(".");
1353

    
1354
        // Create a calendar to get the date formatted properly
1355
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1356
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1357
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1358
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1359
                       2 * 60 * 60 * 1000);
1360
        Calendar calendar = new GregorianCalendar(pdt);
1361
        Date trialTime = new Date();
1362
        calendar.setTime(trialTime);
1363
        docid.append(calendar.get(Calendar.YEAR));
1364
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1365
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1366
        docid.append(calendar.get(Calendar.MINUTE));
1367
        docid.append(calendar.get(Calendar.SECOND));
1368

    
1369
        return docid.toString();
1370
    }
1371
}
(5-5/18)