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: 2008-05-15 17:43:00 -0700 (Thu, 15 May 2008) $'
9
 * '$Revision: 3873 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacattest;
27

    
28
import java.io.Reader;
29
import java.io.StringReader;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.GregorianCalendar;
33
import java.util.SimpleTimeZone;
34
import java.util.TimeZone;
35

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

    
50
/**
51
 * A JUnit test for testing Access Control for Inline data in Metacat
52
 */
53
public class InlineDataAccessTest extends TestCase {
54
	 
55
	/* Initialize Options*/
56
    static
57
    {
58
  	  try
59
  	  {
60
  		  Options.initialize(new File("build/tests/metacat.properties"));
61
  		  MetaCatUtil.pathsForIndexing 
62
  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
63
  	  }
64
  	  catch(Exception e)
65
  	  {
66
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
67
  	  }
68
    }
69
    private String metacatUrl = MetaCatUtil.getOption("metacatUrl");;
70
    private String username = MetaCatUtil.getOption("mcuser");
71
    private String password = MetaCatUtil.getOption("mcpassword");
72
    private String anotheruser = MetaCatUtil.getOption("mcanotheruser");
73
    private String anotherpassword = MetaCatUtil.getOption("mcanotherpassword");
74
    private String prefix = "test";
75
    private String newdocid = null;
76
    private String testdocument = "";
77

    
78
    private Metacat m;
79

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

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

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

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

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

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

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

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

    
142

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

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

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

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

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

    
185
        return accessBlock;
186

    
187
    }
188

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
315
        return suite;
316
    }
317

    
318

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
620

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
842

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

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

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

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

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

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

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

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

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

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

    
916
            m.login(username, password);
917

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

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

    
930

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

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

    
951
            /////////Case 3
952

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

    
966

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

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

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

    
1005

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

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

    
1029
              m.logout();
1030

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

    
1044

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

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

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

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

    
1068

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

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

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

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

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

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

    
1189
        return response;
1190
    }
1191

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

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

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

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

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

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

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

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

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

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

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

    
1356
    }
1357

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

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

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