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: tao $'
8
 *     '$Date: 2007-11-03 22:16:17 -0700 (Sat, 03 Nov 2007) $'
9
 * '$Revision: 3567 $'
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.client.InsufficientKarmaException;
37
import edu.ucsb.nceas.metacat.client.Metacat;
38
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
39
import edu.ucsb.nceas.metacat.client.MetacatException;
40
import edu.ucsb.nceas.metacat.client.MetacatFactory;
41
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
42
import edu.ucsb.nceas.utilities.IOUtil;
43
import junit.framework.Test;
44
import junit.framework.TestCase;
45
import junit.framework.TestSuite;
46
import java.io.File;
47

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

    
54
    private String metacatUrl = "@systemidserver@@servlet-path@";
55
    private String username = "@mcuser@";
56
    private String password = "@mcpassword@";
57
    private String anotheruser = "@mcanotheruser@";
58
    private String anotherpassword = "@mcanotherpassword@";
59
    private String prefix = "test";
60
    private String newdocid = null;
61
    private String testdocument = "";
62

    
63
    private Metacat m;
64

    
65
    private boolean SUCCESS = true;
66
    private boolean FAILURE = false;
67

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

    
73
    private String testEmlHeader =
74
        "<?xml version=\"1.0\"?><eml:eml" +
75
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
76
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
77
        " packageId=\"eml.1.1\" system=\"knb\"" +
78
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
79
        " scope=\"system\">";
80

    
81
    private String testEmlCreatorBlock =
82
        "<creator scope=\"document\">                                       " +
83
        " <individualName>                                                  " +
84
        "    <surName>Smith</surName>                                       " +
85
        " </individualName>                                                 " +
86
        "</creator>                                                         ";
87

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

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

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

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

    
127

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

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

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

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

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

    
170
        return accessBlock;
171

    
172
    }
173

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
300
        return suite;
301
    }
302

    
303

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
605

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
827

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

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

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

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

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

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

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

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

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

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

    
901
            m.login(username, password);
902

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

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

    
915

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

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

    
936
            /////////Case 3
937

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

    
951

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

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

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

    
990

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

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

    
1014
              m.logout();
1015

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

    
1029

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

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

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

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

    
1053

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

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

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

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

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

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

    
1174
        return response;
1175
    }
1176

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

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

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

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

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

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

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

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

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

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

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

    
1341
    }
1342

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

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

    
1368
        return docid.toString();
1369
    }
1370
}
(4-4/17)