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-07-15 10:16:06 -0700 (Tue, 15 Jul 2008) $'
9
 * '$Revision: 4126 $'
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.metacat.service.PropertyService;
43
import edu.ucsb.nceas.metacat.util.SystemUtil;
44
import edu.ucsb.nceas.utilities.IOUtil;
45
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
46
import junit.framework.Test;
47
import junit.framework.TestCase;
48
import junit.framework.TestSuite;
49
import java.io.File;
50

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

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

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

    
79
    private Metacat m;
80

    
81
    private boolean SUCCESS = true;
82
    private boolean FAILURE = false;
83

    
84
    /**
85
     * These variables are for eml-2.0.1 only. For other eml versions,
86
     * this function might have to modified
87
     */
88

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

    
97
    private String testEmlCreatorBlock =
98
        "<creator scope=\"document\">                                       " +
99
        " <individualName>                                                  " +
100
        "    <surName>Smith</surName>                                       " +
101
        " </individualName>                                                 " +
102
        "</creator>                                                         ";
103

    
104
    private String testEmlContactBlock =
105
        "<contact scope=\"document\">                                       " +
106
        " <individualName>                                                  " +
107
        "    <surName>Jackson</surName>                                     " +
108
        " </individualName>                                                 " +
109
        "</contact>                                                         ";
110

    
111
    private String testEmlInlineBlock1 =
112
        "  <admin>                                                          " +
113
        "    <contact>                                                      " +
114
        "      <name>Operator</name>                                        " +
115
        "      <institution>PSI</institution>                               " +
116
        "    </contact>                                                     " +
117
        "  </admin>                                                         ";
118

    
119
    private String testEmlInlineBlock2 =
120
        "  <instrument>                                                     " +
121
        "    <instName>LCQ</instName>                                       " +
122
        "    <source type=\"ESI\"></source>                                 " +
123
        "    <detector type=\"EM\"></detector>                              " +
124
        "  </instrument>                                                    ";
125

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

    
143

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

    
154
        if (grantAccess) {
155
            accessBlock += "<allow>";
156
        }
157
        else {
158
            accessBlock += "<deny>";
159
        }
160

    
161
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
162

    
163
        if (all) {
164
            accessBlock += "<permission>all</permission>";
165
        }
166
        else {
167
            if (read) {
168
                accessBlock += "<permission>read</permission>";
169
            }
170
            if (write) {
171
                accessBlock += "<permission>write</permission>";
172
            }
173
            if (changePermission) {
174
                accessBlock += "<permission>changePermission</permission>";
175
            }
176
        }
177

    
178
        if (grantAccess) {
179
            accessBlock += "</allow>";
180
        }
181
        else {
182
            accessBlock += "</deny>";
183
        }
184
        accessBlock += "</access>";
185

    
186
        return accessBlock;
187

    
188
    }
189

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

    
203
        String testDocument = "";
204
        testDocument = testDocument + testEmlHeader +
205
            "<dataset scope=\"document\"><title>" + title + "</title>" +
206
            testEmlCreatorBlock;
207

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

    
232
        if (docAccessBlock != null) {
233
            testDocument += docAccessBlock;
234
        }
235

    
236
        testDocument += "</dataset>";
237

    
238
        if (inlineAccessBlock1 != null) {
239
            testDocument += "<additionalMetadata>";
240
            testDocument += "<describes>inlineEntity1</describes>";
241
            testDocument += inlineAccessBlock1;
242
            testDocument += "</additionalMetadata>";
243
        }
244

    
245
        if (inlineAccessBlock2 != null) {
246
            testDocument += "<additionalMetadata>";
247
            testDocument += "<describes>inlineEntity2</describes>";
248
            testDocument += inlineAccessBlock2;
249
            testDocument += "</additionalMetadata>";
250
        }
251

    
252
        if (OnlineAccessBlock1 != null) {
253
            testDocument += "<additionalMetadata>";
254
            testDocument += "<describes>InlineEntity1</describes>";
255
            testDocument += OnlineAccessBlock1;
256
            testDocument += "</additionalMetadata>";
257
        }
258

    
259
        if (OnlineAccessBlock2 != null) {
260
            testDocument += "<additionalMetadata>";
261
            testDocument += "<describes>InlineEntity2</describes>";
262
            testDocument += OnlineAccessBlock2;
263
            testDocument += "</additionalMetadata>";
264
        }
265

    
266
        testDocument += "</eml:eml>";
267

    
268
        //System.out.println("Returning following document" + testDocument);
269
        return testDocument;
270
    }
271

    
272
    /**
273
     * Constructor to build the test
274
     *
275
     * @param name the name of the test method
276
     */
277
    public InlineDataAccessTest(String name) {
278
        super(name);
279
        newdocid = generateDocid();
280
    }
281

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

    
296
    /**
297
     * Release any objects after tests are complete
298
     */
299
    public void tearDown() {
300
    }
301

    
302
    /**
303
     * Create a suite of tests to be run together
304
     */
305
    public static Test suite() {
306
        TestSuite suite = new TestSuite();
307
        suite.addTest(new InlineDataAccessTest("initialize"));
308

    
309
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_1"));
310
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_2"));
311
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_3"));
312
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_4"));
313
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_5"));
314
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_6"));
315

    
316
        return suite;
317
    }
318

    
319

    
320
    /**
321
     * Run an initial test that always passes to check that the test
322
     * harness is working.
323
     */
324
    public void initialize() {
325
        assertTrue(1 == 1);
326
    }
327

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

    
343
            // insert a document
344
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
345
                                         null, null, null, null,
346
                                         null, null, null, null);
347

    
348
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
349
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
350

    
351
            // insert same document again
352
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
353

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

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

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

    
378
            // read inline data only
379
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
380
                                         testEmlInlineBlock1, SUCCESS, false);
381

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
621

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
843

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

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

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

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

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

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

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

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

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

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

    
917
            m.login(username, password);
918

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

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

    
931

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

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

    
952
            /////////Case 3
953

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

    
967

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

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

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

    
1006

    
1007
    /** *********
1008
       * Checking the following cases:
1009
       * -> when inline data in inserted and updated, do access rules apply
1010
       *    to the old document
1011
       */
1012
      public void inlineDataCasesTest_6() {
1013
          try {
1014

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

    
1030
              m.logout();
1031

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

    
1045

    
1046
              updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1047

    
1048
              // try reading the inline document
1049
              readInlineDataWhichEqualsDoc(newdocid + ".2.1",
1050
                             testEmlInlineBlock1, FAILURE, true);
1051

    
1052
              System.out.print("Trying to read " + newdocid + ".1.1");
1053
              readInlineDataWhichEqualsDoc(newdocid + ".1.1",
1054
                             testEmlInlineBlock1, FAILURE, true);
1055

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

    
1069

    
1070
    /**
1071
     * Insert a document into metacat. The expected result is passed as result
1072
     */
1073

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

    
1111
    /**
1112
     * Insert a document into metacat. The expected result is passed as result
1113
     */
1114

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

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

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

    
1190
        return response;
1191
    }
1192

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

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

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

    
1240
            if (result) {
1241
                if (!testDoc.equals(doc)) {
1242
                    System.out.println("doc    :" + doc);
1243
                    System.out.println("testDoc:" + testDoc);
1244
                }
1245

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

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

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

    
1310
    /**
1311
     * Read a document from metacat and check if it is equal to a given string.
1312
     * The expected result is passed as result
1313
     */
1314

    
1315
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1316
                                         boolean result,
1317
                                         boolean expextedKarmaFailure) {
1318
        try {
1319
            Reader r = m.read(docid);
1320
            String doc = IOUtil.getAsString(r, true);
1321
            if (result) {
1322

    
1323
                if (!testDoc.equals(doc)) {
1324
                    System.out.println("doc    :" + doc);
1325
                    System.out.println("testDoc:" + testDoc);
1326
                }
1327

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

    
1357
    }
1358

    
1359
    /**
1360
     * Create a hopefully unique docid for testing insert and update. Does
1361
     * not include the 'revision' part of the id.
1362
     *
1363
     * @return a String docid based on the current date and time
1364
     */
1365
    private String generateDocid() {
1366
        StringBuffer docid = new StringBuffer(prefix);
1367
        docid.append(".");
1368

    
1369
        // Create a calendar to get the date formatted properly
1370
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1371
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1372
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1373
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1374
                       2 * 60 * 60 * 1000);
1375
        Calendar calendar = new GregorianCalendar(pdt);
1376
        Date trialTime = new Date();
1377
        calendar.setTime(trialTime);
1378
        docid.append(calendar.get(Calendar.YEAR));
1379
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1380
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1381
        docid.append(calendar.get(Calendar.MINUTE));
1382
        docid.append(calendar.get(Calendar.SECOND));
1383

    
1384
        return docid.toString();
1385
    }
1386
}
(5-5/18)