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

    
26
package edu.ucsb.nceas.metacattest;
27

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

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

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

    
54
    private String metacatUrl = "@systemidserver@@servlet-path@";
55
    private String username = "@mcuser@";
56
    private String password = "@mcpassword@";
57
    private String anotheruser = "@mcanotheruser@";
58
    private String anotherpassword = "@mcanotherpassword@";
59
    private String prefix = "test";
60
    private String newdocid = null;
61
    private String onlineDocid = null;
62
    private String testdocument = "";
63
    private String onlinetestdatafile1 = "test/onlineDataFile1";
64
    private String onlinetestdatafile2 = "test/onlineDataFile2";
65

    
66
    private Metacat m;
67

    
68
    private boolean SUCCESS = true;
69
    private boolean FAILURE = false;
70

    
71
    /**
72
     * These variables are for eml-2.0.1 only. For other eml versions,
73
     * this function might have to modified
74
     */
75

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

    
84
    private String testEmlCreatorBlock =
85
        "<creator scope=\"document\">                                       " +
86
        " <individualName>                                                  " +
87
        "    <surName>Smith</surName>                                       " +
88
        " </individualName>                                                 " +
89
        "</creator>                                                         ";
90

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

    
98
    private String testEmlInlineBlock1 =
99
        "<inline>                                                           " +
100
        "  <admin>                                                          " +
101
        "    <contact>                                                      " +
102
        "      <name>Operator</name>                                        " +
103
        "      <institution>PSI</institution>                               " +
104
        "    </contact>                                                     " +
105
        "  </admin>                                                         " +
106
        "</inline>                                                          ";
107

    
108
    private String testEmlInlineBlock2 =
109
        "<inline>                                                           " +
110
        "  <instrument>                                                     " +
111
        "    <instName>LCQ</instName>                                       " +
112
        "    <source type=\"ESI\"></source>                                 " +
113
        "    <detector type=\"EM\"></detector>                              " +
114
        "  </instrument>                                                    " +
115
        "</inline>                                                          ";
116

    
117
    /**
118
     * This function returns an access block based on the params passed
119
     */
120
    private String getAccessBlock(String principal, boolean grantAccess,
121
                                  boolean read, boolean write,
122
                                  boolean changePermission, boolean all) {
123
        String accessBlock = "<access " +
124
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
125
            " order=\"allowFirst\" scope=\"document\">";
126

    
127
        if (grantAccess) {
128
            accessBlock += "<allow>";
129
        }
130
        else {
131
            accessBlock += "<deny>";
132
        }
133

    
134
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
135

    
136
        if (all) {
137
            accessBlock += "<permission>all</permission>";
138
        }
139
        else {
140
            if (read) {
141
                accessBlock += "<permission>read</permission>";
142
            }
143
            if (write) {
144
                accessBlock += "<permission>write</permission>";
145
            }
146
            if (changePermission) {
147
                accessBlock += "<permission>changePermission</permission>";
148
            }
149
        }
150

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

    
159
        return accessBlock;
160

    
161
    }
162

    
163
    /**
164
     * This function returns a valid eml document with no access rules
165
     * This function is for eml-2.0.1 only. For other eml versions,
166
     * this function might have to modified
167
     */
168
    private String getTestEmlDoc(String title, String inlineData1,
169
                                 String inlineData2, String onlineUrl1,
170
                                 String onlineUrl2, String docAccessBlock,
171
                                 String inlineAccessBlock1,
172
                                 String inlineAccessBlock2,
173
                                 String onlineAccessBlock1,
174
                                 String onlineAccessBlock2) {
175

    
176
        String testDocument = "";
177
        testDocument = testDocument + testEmlHeader +
178
            "<dataset scope=\"document\"><title>" + title + "</title>" +
179
            testEmlCreatorBlock;
180

    
181
        if (inlineData1 != null) {
182
            testDocument = testDocument
183
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
184
                + inlineData1 + "</distribution>";
185
        }
186
        if (inlineData2 != null) {
187
            testDocument = testDocument
188
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
189
                + inlineData2 + "</distribution>";
190
        }
191
        if (onlineUrl1 != null) {
192
            testDocument = testDocument
193
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
194
                + "<online><url function=\"download\">"
195
                + onlineUrl1 + "</url></online></distribution>";
196
        }
197
        if (onlineUrl2 != null) {
198
            testDocument = testDocument +
199
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
200
                + "<online><url function=\"download\">"
201
                + onlineUrl2 + "</url></online></distribution>";
202
        }
203
        testDocument += testEmlContactBlock;
204

    
205
        if (docAccessBlock != null) {
206
            testDocument += docAccessBlock;
207
        }
208

    
209
        testDocument += "</dataset>";
210

    
211
        // if(inlineAccessBlock1 != null || inlineAccessBlock2 != null ||
212
        //    onlineAccessBlock1 != null || onlineAccessBlock2 != null){
213
        //     testDocument += "<addtionalMetadata>";
214
        // }
215

    
216
        if (inlineAccessBlock1 != null) {
217
            testDocument += "<additionalMetadata>";
218
            testDocument += "<describes>inlineEntity1</describes>";
219
            testDocument += inlineAccessBlock1;
220
            testDocument += "</additionalMetadata>";
221
        }
222

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

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

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

    
244
        testDocument += "</eml:eml>";
245

    
246
        //System.out.println("Returning following document" + testDocument);
247
        return testDocument;
248
    }
249

    
250
    /**
251
     * Constructor to build the test
252
     *
253
     * @param name the name of the test method
254
     */
255
    public AccessControlTest(String name) {
256
        super(name);
257
        newdocid = generateDocid();
258
    }
259

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

    
274
    /**
275
     * Release any objects after tests are complete
276
     */
277
    public void tearDown() {
278
    }
279

    
280
    /**
281
     * Create a suite of tests to be run together
282
     */
283
    public static Test suite() {
284
        TestSuite suite = new TestSuite();
285
        suite.addTest(new AccessControlTest("initialize"));
286
        // Test basic functions
287
        //suite.addTest(new AccessControlTest("login"));
288
        //suite.addTest(new AccessControlTest("insert"));
289
        //suite.addTest(new AccessControlTest("read"));
290
        //suite.addTest(new AccessControlTest("update"));
291
        //suite.addTest(new AccessControlTest("delete"));
292
        // Tests when no access is specified
293
        //suite.addTest(new AccessControlTest("documentTest"));
294
        //suite.addTest(new AccessControlTest("documentWithInlineDataTest"));
295
        //suite.addTest(new AccessControlTest("documentWithOnlineDataTest"));
296
        suite.addTest(new AccessControlTest("onlineDataCasesTest_1"));
297
        suite.addTest(new AccessControlTest("onlineDataCasesTest_2"));
298
        suite.addTest(new AccessControlTest("onlineDataCasesTest_3"));
299

    
300
        //suite.addTest(new AccessControlTest("noAccessSpecified_Random"));
301
        //suite.addTest(new AccessControlTest("noAccessSpecified_Public"));
302

    
303
        return suite;
304
    }
305

    
306
    /** *********
307
     * Test the case when no access is specified and owner is logged in
308
     * No online or inline data is involved
309
     */
310

    
311
    public void onlineDataCasesTest_1() {
312
        try {
313
            /////////Case 1./////////////////////
314
            // upload an online document
315
            onlineDocid = generateDocid();
316
            m.login(username, password);
317
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
318
            m.logout();
319

    
320
            // write a document with no access rules from a
321
            // different userid which refers to this document
322
            m.login(anotheruser, anotherpassword);
323
            testdocument = getTestEmlDoc("Testing insert", null, null,
324
                                         "ecogrid://knb/" + onlineDocid + ".1",
325
                                         null, null, null, null, null, null);
326
            newdocid = generateDocid();
327
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
328
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
329

    
330
            m.logout();
331
            m.login(anotheruser, anotherpassword);
332

    
333
            // write a document with access rules from a
334
            // different userid which refers to this document
335
            testdocument = getTestEmlDoc("Another insert", null, null,
336
                                         "ecogrid://knb/" + onlineDocid + ".1",
337
                                         null, getAccessBlock(anotheruser, true,
338
                false, false, false, true),
339
                                         null, null, null, null);
340
            newdocid = generateDocid();
341
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
342

    
343
            m.logout();
344

    
345
            /////////Case 2./////////////////////
346
            // upload an online document
347
            onlineDocid = generateDocid();
348
            m.login(username, password);
349
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
350

    
351
            testdocument = getTestEmlDoc("Testing insert", null, null,
352
                                         "ecogrid://knb/" + onlineDocid + ".1",
353
                                         null, null,
354
                                         null, null, null, null);
355
            newdocid = generateDocid();
356
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
357

    
358
            m.logout();
359

    
360
            // write a document with no access rules from a
361
            // different userid which refers to this document
362
            m.login(anotheruser, anotherpassword);
363
            testdocument = getTestEmlDoc("Testing insert", null, null,
364
                                         "ecogrid://knb/" + onlineDocid + ".1",
365
                                         null, null, null, null, null, null);
366
            newdocid = generateDocid();
367
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
368
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
369

    
370
            m.logout();
371
            m.login(anotheruser, anotherpassword);
372
            // write a document with access rules from a
373
            // different userid which refers to this document
374
            testdocument = getTestEmlDoc("Another insert", null, null,
375
                                         "ecogrid://knb/" + onlineDocid + ".1",
376
                                         null, getAccessBlock(anotheruser, true,
377
                false, false, false, true),
378
                                         null, null, null, null);
379
            newdocid = generateDocid();
380
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
381

    
382
            m.logout();
383
        }
384
        catch (MetacatAuthException mae) {
385
            fail("Authorization failed:\n" + mae.getMessage());
386
        }
387
        catch (MetacatInaccessibleException mie) {
388
            fail("Metacat Inaccessible:\n" + mie.getMessage());
389
        }
390
        catch (Exception e) {
391
            fail("General exception:\n" + e.getMessage());
392
        }
393
    }
394

    
395
    public void onlineDataCasesTest_2() {
396
        try {
397

    
398
            /////////Case 3./////////////////////
399
            // upload an online document
400
            onlineDocid = generateDocid();
401
            m.login(username, password);
402
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
403

    
404
            testdocument = getTestEmlDoc("Testing insert", null, null,
405
                                         "ecogrid://knb/" + onlineDocid + ".1",
406
                                         null, getAccessBlock(anotheruser, true,
407
                true, false, false, false),
408
                                         null, null, null, null);
409
            newdocid = generateDocid();
410
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
411

    
412
            m.logout();
413

    
414
            // write a document with no access rules from a
415
            // different userid which refers to this document
416
            m.login(anotheruser, anotherpassword);
417
            testdocument = getTestEmlDoc("Testing insert", null, null,
418
                                         "ecogrid://knb/" + onlineDocid + ".1",
419
                                         null, null, null, null, null, null);
420
            newdocid = generateDocid();
421
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
422
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
423

    
424
            m.logout();
425
            m.login(anotheruser, anotherpassword);
426

    
427
            // write a document with access rules from a
428
            // different userid which refers to this document
429
            testdocument = getTestEmlDoc("Another insert", null, null,
430
                                         "ecogrid://knb/" + onlineDocid + ".1",
431
                                         null, getAccessBlock(anotheruser, true,
432
                false, false, false, true),
433
                                         null, null, null, null);
434
            newdocid = generateDocid();
435
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
436

    
437
            m.logout();
438

    
439
            /////////Case 4./////////////////////
440
            // upload an online document
441
            onlineDocid = generateDocid();
442
            m.login(username, password);
443
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
444

    
445
            testdocument = getTestEmlDoc("Testing insert", null, null,
446
                                         "ecogrid://knb/" + onlineDocid + ".1",
447
                                         null, getAccessBlock(anotheruser, true,
448
                false, true, false, false),
449
                                         null, null, null, null);
450
            newdocid = generateDocid();
451
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
452

    
453
            m.logout();
454

    
455
            // write a document with no access rules from a
456
            // different userid which refers to this document
457
            m.login(anotheruser, anotherpassword);
458
            testdocument = getTestEmlDoc("Testing insert", null, null,
459
                                         "ecogrid://knb/" + onlineDocid + ".1",
460
                                         null, null, null, null, null, null);
461
            newdocid = generateDocid();
462
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
463
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
464

    
465
            m.logout();
466
            m.login(anotheruser, anotherpassword);
467

    
468
            // write a document with access rules from a
469
            // different userid which refers to this document
470
            testdocument = getTestEmlDoc("Another insert", null, null,
471
                                         "ecogrid://knb/" + onlineDocid + ".1",
472
                                         null, getAccessBlock(anotheruser, true,
473
                false, false, false, true),
474
                                         null, null, null, null);
475
            newdocid = generateDocid();
476
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
477

    
478
            m.logout();
479

    
480
            /////////Case 5./////////////////////
481
            // upload an online document
482
            onlineDocid = generateDocid();
483
            m.login(username, password);
484
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
485

    
486
            testdocument = getTestEmlDoc("Testing insert", null, null,
487
                                         "ecogrid://knb/" + onlineDocid + ".1",
488
                                         null, getAccessBlock(anotheruser, true,
489
                false, false, false, true),
490
                                         null, null, null, null);
491
            newdocid = generateDocid();
492
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
493

    
494
            m.logout();
495

    
496
            // write a document with no access rules from a
497
            // different userid which refers to this document
498
            m.login(anotheruser, anotherpassword);
499
            testdocument = getTestEmlDoc("Testing insert", null, null,
500
                                         "ecogrid://knb/" + onlineDocid + ".1",
501
                                         null, null, null, null, null, null);
502
            newdocid = generateDocid();
503
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
504
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
505

    
506
            m.logout();
507
            m.login(anotheruser, anotherpassword);
508

    
509
            // write a document with access rules from a
510
            // different userid which refers to this document
511
            testdocument = getTestEmlDoc("Another insert", null, null,
512
                                         "ecogrid://knb/" + onlineDocid + ".1",
513
                                         null, getAccessBlock(anotheruser, true,
514
                false, false, true, false),
515
                                         null, null, null, null);
516
            newdocid = generateDocid();
517
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
518
            m.logout();
519

    
520
            /////////Case 6./////////////////////
521
            // upload an online document
522
            onlineDocid = generateDocid();
523
            m.login(username, password);
524
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
525

    
526
            testdocument = getTestEmlDoc("Testing insert", null, null,
527
                                         "ecogrid://knb/" + onlineDocid + ".1",
528
                                         null, getAccessBlock(anotheruser, true,
529
                false, false, false, true),
530
                                         null, null, null, null);
531
            newdocid = generateDocid();
532
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
533

    
534
            m.logout();
535

    
536
            // write a document with no access rules from a
537
            // different userid which refers to this document
538
            m.login(anotheruser, anotherpassword);
539
            testdocument = getTestEmlDoc("Testing insert", null, null,
540
                                         "ecogrid://knb/" + onlineDocid + ".1",
541
                                         null, null, null, null, null, null);
542
            newdocid = generateDocid();
543
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
544
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
545

    
546
            m.logout();
547
            m.login(anotheruser, anotherpassword);
548

    
549
            // write a document with access rules from a
550
            // different userid which refers to this document
551
            testdocument = getTestEmlDoc("Another insert", null, null,
552
                                         "ecogrid://knb/" + onlineDocid + ".1",
553
                                         null, getAccessBlock(anotheruser, true,
554
                true, false, false, false),
555
                                         null, null, null, null);
556
            newdocid = generateDocid();
557
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
558

    
559
            m.logout();
560
        }
561
        catch (MetacatAuthException mae) {
562
            fail("Authorization failed:\n" + mae.getMessage());
563
        }
564
        catch (MetacatInaccessibleException mie) {
565
            fail("Metacat Inaccessible:\n" + mie.getMessage());
566
        }
567
        catch (Exception e) {
568
            fail("General exception:\n" + e.getMessage());
569
        }
570
    }
571

    
572

    
573

    
574

    
575
    public void onlineDataCasesTest_4() {
576
        try {
577
            /////////Case 7./////////////////////
578
            // upload an online document
579
            onlineDocid = generateDocid();
580
            m.login(username, password);
581
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
582

    
583
            testdocument = getTestEmlDoc("Testing insert", null, null,
584
                                         "ecogrid://knb/" + onlineDocid + ".1",
585
                                         null, null, null, null,
586
                                         getAccessBlock(anotheruser, true,
587
                true, false, false, false), null);
588
            newdocid = generateDocid();
589
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
590

    
591
            m.logout();
592

    
593
            // write a document with no access rules from a
594
            // different userid which refers to this document
595
            m.login(anotheruser, anotherpassword);
596
            testdocument = getTestEmlDoc("Testing insert", null, null,
597
                                         "ecogrid://knb/" + onlineDocid + ".1",
598
                                         null, null, null, null, null, null);
599
            newdocid = generateDocid();
600
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
601
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
602

    
603
            m.logout();
604
            m.login(anotheruser, anotherpassword);
605

    
606
            // try to read the online data file
607
            readDocid(onlineDocid + ".1", SUCCESS, false);
608

    
609
            // write a document with access rules from a
610
            // different userid which refers to this document
611
            testdocument = getTestEmlDoc("Another insert", null, null,
612
                                         "ecogrid://knb/" + onlineDocid + ".1",
613
                                         null, getAccessBlock(anotheruser, true,
614
                false, false, false, true),
615
                                         null, null, null, null);
616
            newdocid = generateDocid();
617
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
618

    
619
            m.logout();
620

    
621
            /////////Case 8./////////////////////
622
            // upload an online document
623
            onlineDocid = generateDocid();
624
            m.login(username, password);
625
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
626

    
627
            testdocument = getTestEmlDoc("Testing insert", null, null,
628
                                         "ecogrid://knb/" + onlineDocid + ".1",
629
                                         null, null, null, null,
630
                                         getAccessBlock(anotheruser, true,
631
                false, true, false, false), null);
632
            newdocid = generateDocid();
633
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
634

    
635
            m.logout();
636

    
637
            // write a document with no access rules from a
638
            // different userid which refers to this document
639
            m.login(anotheruser, anotherpassword);
640

    
641
            testdocument = getTestEmlDoc("Testing insert", null, null,
642
                                         "ecogrid://knb/" + onlineDocid + ".1",
643
                                         null, null, null, null, null, null);
644
            newdocid = generateDocid();
645
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
646
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
647

    
648
            m.logout();
649
            m.login(anotheruser, anotherpassword);
650

    
651
            // try to read the online data file
652
            readDocid(onlineDocid + ".1", FAILURE, true);
653

    
654
            // write a document with access rules from a
655
            // different userid which refers to this document
656
            testdocument = getTestEmlDoc("Another insert", null, null,
657
                                         "ecogrid://knb/" + onlineDocid + ".1",
658
                                         null, getAccessBlock(anotheruser, true,
659
                false, false, false, true),
660
                                         null, null, null, null);
661
            newdocid = generateDocid();
662
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
663

    
664
            m.logout();
665

    
666
            /////////Case 9./////////////////////
667
            // upload an online document
668
            onlineDocid = generateDocid();
669
            m.login(username, password);
670
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
671

    
672
            testdocument = getTestEmlDoc("Testing insert", null, null,
673
                                         "ecogrid://knb/" + onlineDocid + ".1",
674
                                         null, null, null, null,
675
                                         getAccessBlock(anotheruser, true,
676
                false, false, true, false), null);
677
            newdocid = generateDocid();
678
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
679

    
680
            m.logout();
681

    
682
            // write a document with no access rules from a
683
            // different userid which refers to this document
684
            m.login(anotheruser, anotherpassword);
685
            testdocument = getTestEmlDoc("Testing insert", null, null,
686
                                         "ecogrid://knb/" + onlineDocid + ".1",
687
                                         null, null, null, null, null, null);
688
            newdocid = generateDocid();
689
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
690
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
691

    
692
            m.logout();
693
            m.login(anotheruser, anotherpassword);
694

    
695
            // try to read the online data file
696
            readDocid(onlineDocid + ".1", FAILURE, true);
697

    
698
            // write a document with access rules from a
699
            // different userid which refers to this document
700
            testdocument = getTestEmlDoc("Another insert", null, null,
701
                                         "ecogrid://knb/" + onlineDocid + ".1",
702
                                         null, getAccessBlock(anotheruser, true,
703
                false, false, true, false),
704
                                         null, null, null, null);
705
            newdocid = generateDocid();
706
            //ERRRRRRRRRRRRRRRROR
707
            //insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
708

    
709
            /////////Case 10./////////////////////
710
            // upload an online document
711
            onlineDocid = generateDocid();
712
            m.login(username, password);
713
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
714

    
715
            testdocument = getTestEmlDoc("Testing insert", null, null,
716
                                         "ecogrid://knb/" + onlineDocid + ".1",
717
                                         null, null, null, null,
718
                                         getAccessBlock(anotheruser, true,
719
                false, false, false, true), null);
720
            newdocid = generateDocid();
721
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
722

    
723
            m.logout();
724

    
725
            // write a document with no access rules from a
726
            // different userid which refers to this document
727
            m.login(anotheruser, anotherpassword);
728
            testdocument = getTestEmlDoc("Testing insert", null, null,
729
                                         "ecogrid://knb/" + onlineDocid + ".1",
730
                                         null, null, null, null, null, null);
731
            newdocid = generateDocid();
732
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
733
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
734

    
735
            m.logout();
736
            m.login(anotheruser, anotherpassword);
737

    
738
            // try to read the online data file
739
            readDocid(onlineDocid + ".1", SUCCESS, false);
740

    
741
            // write a document with access rules from a
742
            // different userid which refers to this document
743
            testdocument = getTestEmlDoc("Another insert", null, null,
744
                                         "ecogrid://knb/" + onlineDocid + ".1",
745
                                         null, getAccessBlock(anotheruser, true,
746
                true, false, false, false),
747
                                         null, null, null, null);
748
            newdocid = generateDocid();
749
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
750

    
751
            m.logout();
752

    
753
        }
754
        catch (MetacatAuthException mae) {
755
            fail("Authorization failed:\n" + mae.getMessage());
756
        }
757
        catch (MetacatInaccessibleException mie) {
758
            fail("Metacat Inaccessible:\n" + mie.getMessage());
759
        }
760
        catch (Exception e) {
761
            fail("General exception:\n" + e.getMessage());
762
        }
763
    }
764

    
765

    
766

    
767
    /**
768
     * Run an initial test that always passes to check that the test
769
     * harness is working.
770
     */
771
    public void initialize() {
772
        assertTrue(1 == 1);
773
    }
774

    
775
    /**
776
     * Test the login() function with valid credentials
777
     */
778
    public void login() {
779
        // Try a valid login
780
        try {
781
            String response = m.login(username, password);
782
            System.err.println("Login response: " + response);
783
            assertTrue(response != null);
784
            assertTrue(response.indexOf("<login>") != -1);
785
            String sessionId = m.getSessionId();
786
            System.err.println("Session ID: " + m.getSessionId());
787
            assertTrue(sessionId != null);
788
            assertTrue(response.indexOf(m.getSessionId()) != -1);
789
        }
790
        catch (MetacatAuthException mae) {
791
            fail("Authorization failed:\n" + mae.getMessage());
792
        }
793
        catch (MetacatInaccessibleException mie) {
794
            fail("Metacat Inaccessible:\n" + mie.getMessage());
795
        }
796
    }
797

    
798
    /**
799
     * Test the insert() function with a known document
800
     */
801
    public void insert() {
802
        try {
803
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
804
                                         null, null, null, null,
805
                                         null, null, null, null);
806
            String identifier = newdocid + ".1";
807
            m.login(username, password);
808
            String response = m.insert(identifier,
809
                                       new StringReader(testdocument), null);
810
            assertTrue(response.indexOf("<success>") != -1);
811
            assertTrue(response.indexOf(identifier) != -1);
812
            System.err.println(response);
813
        }
814
        catch (MetacatAuthException mae) {
815
            fail("Authorization failed:\n" + mae.getMessage());
816
        }
817
        catch (MetacatInaccessibleException mie) {
818
            fail("Metacat Inaccessible:\n" + mie.getMessage());
819
        }
820
        catch (InsufficientKarmaException ike) {
821
            assertTrue(1 == 1);
822
            fail("Insufficient karma:\n" + ike.getMessage());
823
        }
824
        catch (MetacatException me) {
825
            fail("Metacat Error:\n" + me.getMessage());
826
        }
827
        catch (Exception e) {
828
            fail("General exception:\n" + e.getMessage());
829
        }
830
    }
831

    
832
    /**
833
     * The read() function
834
     */
835
    public void read() {
836
        try {
837
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1, null,
838
                                         null, null, null, null, null, null, null);
839

    
840
            m.login(username, password);
841
            Reader r = m.read(newdocid + ".1");
842
            String doc = IOUtil.getAsString(r, true);
843
            assertTrue(doc.equals(testdocument));
844
        }
845
        catch (MetacatAuthException mae) {
846
            fail("Authorization failed:\n" + mae.getMessage());
847
        }
848
        catch (MetacatInaccessibleException mie) {
849
            fail("Metacat Inaccessible:\n" + mie.getMessage());
850
        }
851
        catch (Exception e) {
852
            fail("General exception:\n" + e.getMessage());
853
        }
854
    }
855

    
856
    /**
857
     * Test the update() function with a known document
858
     */
859
    public void update() {
860
        try {
861
            testdocument = getTestEmlDoc("Testing update", testEmlInlineBlock1, null,
862
                                         null, null, null, null, null, null, null);
863
            String identifier = newdocid + ".2";
864
            m.login(username, password);
865
            String response = m.update(identifier,
866
                                       new StringReader(testdocument), null);
867
            assertTrue(response.indexOf("<success>") != -1);
868
            assertTrue(response.indexOf(identifier) != -1);
869
            System.err.println(response);
870

    
871
        }
872
        catch (MetacatAuthException mae) {
873
            fail("Authorization failed:\n" + mae.getMessage());
874
        }
875
        catch (MetacatInaccessibleException mie) {
876
            fail("Metacat Inaccessible:\n" + mie.getMessage());
877
        }
878
        catch (InsufficientKarmaException ike) {
879
            fail("Insufficient karma:\n" + ike.getMessage());
880
        }
881
        catch (MetacatException me) {
882
            fail("Metacat Error:\n" + me.getMessage());
883
        }
884
        catch (Exception e) {
885
            fail("General exception:\n" + e.getMessage());
886
        }
887
    }
888

    
889
    /**
890
     * Test the delete() function with a known document
891
     */
892
    public void delete() {
893
        try {
894
            String identifier = newdocid + ".2";
895
            m.login(username, password);
896
            String response = m.delete(identifier);
897
            assertTrue(response.indexOf("<success>") != -1);
898
            System.err.println(response);
899

    
900
        }
901
        catch (MetacatAuthException mae) {
902
            fail("Authorization failed:\n" + mae.getMessage());
903
        }
904
        catch (MetacatInaccessibleException mie) {
905
            fail("Metacat Inaccessible:\n" + mie.getMessage());
906
        }
907
        catch (InsufficientKarmaException ike) {
908
            fail("Insufficient karma:\n" + ike.getMessage());
909
        }
910
        catch (MetacatException me) {
911
            fail("Metacat Error:\n" + me.getMessage());
912
        }
913
        catch (Exception e) {
914
            fail("General exception:\n" + e.getMessage());
915
        }
916
    }
917

    
918
    /** *********
919
     * Test the case when no access is specified and owner is logged in
920
     * No online or inline data is involved
921
     */
922
    public void documentTest() {
923
        try {
924
            newdocid = generateDocid();
925

    
926
            // login
927
            m.login(username, password);
928

    
929
            // insert a document
930
            testdocument = getTestEmlDoc("Testing insert", null,
931
                                         null, null,
932
                                         null, null, null, null, null, null);
933
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
934
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
935

    
936
            // update the document
937
            testdocument = getTestEmlDoc("Testing update", null, null, null,
938
                                         null, null, null, null, null, null);
939
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
940
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
941

    
942
            /////////////////////////////
943
            // update the document access control - read only
944
            testdocument = getTestEmlDoc("Testing update access block",
945
                                         null, null, null,
946
                                         null, getAccessBlock(anotheruser, true,
947
                true, false, false, false),
948
                                         null, null, null, null);
949
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
950
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
951

    
952
            // check if the user mentioned is able to read the document
953
            m.logout();
954
            m.login(anotheruser, anotherpassword);
955
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
956

    
957
            // should not be able to update the document
958
            testdocument = getTestEmlDoc("Testing update from another user",
959
                                         null, null, null,
960
                                         null, getAccessBlock(anotheruser, true,
961
                true, false, false, false),
962
                                         null, null, null, null);
963
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
964

    
965
            // or the permissions
966
            testdocument = getTestEmlDoc(
967
                "Testing perm update from another user",
968
                null, null, null,
969
                null, getAccessBlock(anotheruser, true,
970
                                     false, false, false, true),
971
                null, null, null, null);
972
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
973
            m.logout();
974
            m.login(username, password);
975

    
976
            ///////////////////////////////////
977
            // update the document access control - write only
978
            testdocument = getTestEmlDoc("Testing update access block",
979
                                         null, null, null,
980
                                         null, getAccessBlock(anotheruser, true,
981
                false, true, false, false),
982
                                         null, null, null, null);
983
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
984
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
985
            //System.out.println(testdocument);
986
            // check if the user mentioned is able to read the document
987
            m.logout();
988
            m.login(anotheruser, anotherpassword);
989
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, FAILURE, true);
990

    
991
            // should be able to update the document
992
            testdocument = getTestEmlDoc("Testing update access block",
993
                                         null, null, null,
994
                                         null, getAccessBlock(anotheruser, true,
995
                false, true, false, false),
996
                                         null, null, null, null);
997
            //System.out.println(testdocument);
998
            // ERRRRRRRRRRRRROR
999
            //updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
1000

    
1001
            // but not the permissions
1002
            testdocument = getTestEmlDoc("Testing update from another user",
1003
                                         null, null, null,
1004
                                         null, getAccessBlock(anotheruser, true,
1005
                false, true, false, true),
1006
                                         null, null, null, null);
1007
            updateDocid(newdocid + ".5", testdocument, FAILURE, true);
1008

    
1009
            m.logout();
1010
            m.login(username, password);
1011

    
1012
            /////////////////////////////////
1013
            // update the document access control - change permissions only
1014
            testdocument = getTestEmlDoc("Testing update access block",
1015
                                         null, null, null,
1016
                                         null, getAccessBlock(anotheruser, true,
1017
                false, false, true, false),
1018
                                         null, null, null, null);
1019
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1020
            readDocidWhichEqualsDoc(newdocid + ".5", testdocument, SUCCESS, false);
1021

    
1022
            // check if the user mentioned is able to read the document
1023
            m.logout();
1024
            m.login(anotheruser, anotherpassword);
1025
            readDocidWhichEqualsDoc(newdocid + ".5", testdocument, FAILURE, true);
1026

    
1027
            // should not be able to update the document
1028
            testdocument = getTestEmlDoc("Testing update from another user",
1029
                                         null, null, null,
1030
                                         null, getAccessBlock(anotheruser, true,
1031
                false, false, true, false),
1032
                                         null, null, null, null);
1033
            updateDocid(newdocid + ".6", testdocument, FAILURE, true);
1034

    
1035
            // but can chg the permissions
1036
            testdocument = getTestEmlDoc("Testing update from another user",
1037
                                         null, null, null,
1038
                                         null, getAccessBlock(anotheruser, true,
1039
                false, false, false, true),
1040
                                         null, null, null, null);
1041
            // ERRRRRRRROR
1042
            //updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
1043

    
1044
            m.logout();
1045
            m.login(username, password);
1046

    
1047
            /////////////////////////////////
1048
            // update the document access control - all
1049
            testdocument = getTestEmlDoc("Testing update access block",
1050
                                         null, null, null,
1051
                                         null, getAccessBlock(anotheruser, true,
1052
                false, false, false, true),
1053
                                         null, null, null, null);
1054
            updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
1055
            readDocidWhichEqualsDoc(newdocid + ".7", testdocument, SUCCESS, false);
1056

    
1057
            // check if the user mentioned is able to read the document
1058
            m.logout();
1059
            m.login(anotheruser, anotherpassword);
1060
            readDocidWhichEqualsDoc(newdocid + ".7", testdocument, SUCCESS, false);
1061

    
1062
            // should not be able to update the document
1063
            testdocument = getTestEmlDoc("Testing update from another user",
1064
                                         null, null, null,
1065
                                         null, getAccessBlock(anotheruser, true,
1066
                false, false, false, true),
1067
                                         null, null, null, null);
1068
            updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
1069
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1070

    
1071
            // but can chg the permissions
1072
            testdocument = getTestEmlDoc("Testing update from another user",
1073
                                         null, null, null,
1074
                                         null, getAccessBlock(anotheruser, true,
1075
                true, false, false, false),
1076
                                         null, null, null, null);
1077
            updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
1078
            readDocidWhichEqualsDoc(newdocid + ".9", testdocument, SUCCESS, false);
1079

    
1080
            m.logout();
1081

    
1082
            // delete the document
1083
            m.login(username, password);
1084
            deleteDocid(newdocid + ".9", SUCCESS);
1085
            m.logout();
1086
        }
1087
        catch (MetacatAuthException mae) {
1088
            fail("Authorization failed:\n" + mae.getMessage());
1089
        }
1090
        catch (MetacatInaccessibleException mie) {
1091
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1092
        }
1093
        catch (Exception e) {
1094
            fail("General exception:\n" + e.getMessage());
1095
        }
1096

    
1097
    }
1098

    
1099
    /** *********
1100
     * Test the case when no access is specified and owner is logged in
1101
     * Only inline data is involved
1102
     */
1103
    public void documentWithInlineDataTest() {
1104
        try {
1105
            newdocid = generateDocid();
1106

    
1107
            // login
1108
            m.login(username, password);
1109

    
1110
            // insert a document
1111
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1112
                                         null, null, null, null,
1113
                                         null, null, null, null);
1114
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1115
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
1116

    
1117
            // update the document
1118
            testdocument = getTestEmlDoc("Testing update", testEmlInlineBlock1,
1119
                                         null, null, null, null,
1120
                                         null, null, null, null);
1121
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1122
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
1123

    
1124
            // update by modifying inline data
1125
            testdocument = getTestEmlDoc("Testing update inline",
1126
                                         testEmlInlineBlock2,
1127
                                         null, null, null, null,
1128
                                         null, null, null, null);
1129
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1130
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, true);
1131

    
1132
            // update by removing inline data
1133
            testdocument = getTestEmlDoc("Testing update inline",
1134
                                         null,
1135
                                         null, null, null, null,
1136
                                         null, null, null, null);
1137
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
1138
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, true);
1139

    
1140
            // update by introducing inline data
1141
            testdocument = getTestEmlDoc("Testing update inline",
1142
                                         testEmlInlineBlock1,
1143
                                         null, null, null, null,
1144
                                         null, null, null, null);
1145
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1146
            readDocidWhichEqualsDoc(newdocid + ".5", testdocument, SUCCESS, true);
1147

    
1148
            //  read document and check if it is same as the document which was written
1149
            //  the second time
1150

    
1151

    
1152
            /////////////////////////////
1153
            // update the document access control - read only
1154
            testdocument = getTestEmlDoc("Testing update access block",
1155
                                         testEmlInlineBlock2,
1156
                                         null, null, null,
1157
                                         getAccessBlock(anotheruser, true,
1158
                true, false, false, false),
1159
                                         null, null, null, null);
1160
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
1161
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, true);
1162

    
1163
            // check if the user mentioned is able to read the document
1164
            m.logout();
1165
            m.login(anotheruser, anotherpassword);
1166
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, true);
1167

    
1168
            // should not be able to update the document or the inline data
1169
            testdocument = getTestEmlDoc("Testing update from another user",
1170
                                         testEmlInlineBlock1, null, null, null,
1171
                                         getAccessBlock(anotheruser, true,
1172
                true, false, false, false),
1173
                                         null, null, null, null);
1174
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
1175

    
1176
            // or the permissions
1177
            testdocument = getTestEmlDoc("Testing update access block",
1178
                                         testEmlInlineBlock2,
1179
                                         null, null, null, null,
1180
                                         null, null, null, null);
1181
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
1182

    
1183
            m.logout();
1184
            m.login(username, password);
1185

    
1186
            /////////////////////////////
1187
            // update the document access control - write only
1188
            testdocument = getTestEmlDoc("Testing update access block",
1189
                                         testEmlInlineBlock2,
1190
                                         null, null, null,
1191
                                         getAccessBlock(anotheruser, true,
1192
                false, true, false, false),
1193
                                         null, null, null, null);
1194
            updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
1195
            readDocidWhichEqualsDoc(newdocid + ".7", testdocument, SUCCESS, false);
1196

    
1197
            // check if the user mentioned is able to read the document
1198
            m.logout();
1199
            m.login(anotheruser, anotherpassword);
1200
            readDocidWhichEqualsDoc(newdocid + ".7", testdocument, FAILURE, true);
1201

    
1202
            // should be able to update the document or the inline data
1203
            testdocument = getTestEmlDoc("Testing update from another user",
1204
                                         testEmlInlineBlock1, null, null, null,
1205
                                         getAccessBlock(anotheruser, true,
1206
                false, true, false, false),
1207
                                         null, null, null, null);
1208
            // ERRRRROR
1209
            //updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
1210

    
1211
            // but not the permissions
1212
            testdocument = getTestEmlDoc("Testing update access block",
1213
                                         testEmlInlineBlock2,
1214
                                         null, null, null, null,
1215
                                         null, null, null, null);
1216
            updateDocid(newdocid + ".9", testdocument, FAILURE, true);
1217

    
1218
            m.logout();
1219
            m.login(username, password);
1220

    
1221
            /////////////////////////////
1222
            // update the document access control - change permission only
1223
            testdocument = getTestEmlDoc("Testing update access block",
1224
                                         testEmlInlineBlock2,
1225
                                         null, null, null,
1226
                                         getAccessBlock(anotheruser, true,
1227
                false, false, true, false),
1228
                                         null, null, null, null);
1229
            updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
1230
            readDocidWhichEqualsDoc(newdocid + ".9", testdocument, SUCCESS, false);
1231

    
1232
            // check if the user mentioned is able to read the document
1233
            m.logout();
1234
            m.login(anotheruser, anotherpassword);
1235
            readDocidWhichEqualsDoc(newdocid + ".9", testdocument, FAILURE, true);
1236

    
1237
            // should be able to update the document or the inline data
1238
            testdocument = getTestEmlDoc("Testing update from another user",
1239
                                         testEmlInlineBlock1, null, null, null,
1240
                                         getAccessBlock(anotheruser, true,
1241
                false, false, true, false),
1242
                                         null, null, null, null);
1243
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
1244

    
1245
            // or the permissions
1246
            testdocument = getTestEmlDoc("Testing update access block",
1247
                                         testEmlInlineBlock2,
1248
                                         null, null, null, null,
1249
                                         null, null, null, null);
1250
            // ERRROR
1251
            //updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
1252

    
1253
            m.logout();
1254
            m.login(username, password);
1255

    
1256
            /////////////////////////////
1257
            // update the document access control - change permission only
1258
            testdocument = getTestEmlDoc("Testing update access block",
1259
                                         testEmlInlineBlock2,
1260
                                         null, null, null,
1261
                                         getAccessBlock(anotheruser, true,
1262
                false, false, false, true),
1263
                                         null, null, null, null);
1264
            updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
1265
            readDocidWhichEqualsDoc(newdocid + ".11", testdocument, SUCCESS, false);
1266

    
1267
            // check if the user mentioned is able to read the document
1268
            m.logout();
1269
            m.login(anotheruser, anotherpassword);
1270
            readDocidWhichEqualsDoc(newdocid + ".11", testdocument, SUCCESS, false);
1271

    
1272
            // should be able to update the document or the inline data
1273
            testdocument = getTestEmlDoc("Testing update from another user",
1274
                                         testEmlInlineBlock1, null, null, null,
1275
                                         getAccessBlock(anotheruser, true,
1276
                false, false, false, true),
1277
                                         null, null, null, null);
1278
            updateDocid(newdocid + ".12", testdocument, SUCCESS, false);
1279

    
1280
            // or the permissions
1281
            testdocument = getTestEmlDoc("Testing update access block",
1282
                                         testEmlInlineBlock2,
1283
                                         null, null, null, null,
1284
                                         null, null, null, null);
1285
            updateDocid(newdocid + ".13", testdocument, SUCCESS, false);
1286

    
1287
            m.logout();
1288
            m.login(username, password);
1289

    
1290
            // delete the document
1291
            deleteDocid(newdocid + ".13", SUCCESS);
1292
            m.logout();
1293
        }
1294
        catch (MetacatAuthException mae) {
1295
            fail("Authorization failed:\n" + mae.getMessage());
1296
        }
1297
        catch (MetacatInaccessibleException mie) {
1298
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1299
        }
1300
        catch (Exception e) {
1301
            fail("General exception:\n" + e.getMessage());
1302
        }
1303

    
1304
    }
1305

    
1306
    /** *********
1307
     * Test the case when no access is specified and owner is logged in
1308
     * Only online data is involved
1309
     */
1310
    public void documentWithOnlineDataTest() {
1311
        try {
1312
            newdocid = generateDocid();
1313

    
1314
            // login
1315
            m.login(username, password);
1316

    
1317
            // upload online document
1318
            onlineDocid = generateDocid();
1319
            uploadDocid(onlineDocid + ".1", onlinetestdatafile2, SUCCESS, false);
1320

    
1321
            // try to access the online document from other userid
1322
            m.logout();
1323
            m.login(anotheruser, anotherpassword);
1324
            readDocid(onlineDocid + ".1", FAILURE, true);
1325
            // **************** ERRRRRRRRRROR
1326
            //uploadDocid(onlineDocid + ".2", onlinetestdatafile2, FAILURE, true);
1327

    
1328
            m.logout();
1329
            m.login(username, password);
1330

    
1331
            // insert a document
1332
            testdocument = getTestEmlDoc("Testing insert", null, null,
1333
                                         "ecogrid://knb/" + onlineDocid + ".1",
1334
                                         null, null, null, null, null, null);
1335
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1336
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1337

    
1338
            // update the document
1339
            testdocument = getTestEmlDoc("Testing update", null, null,
1340
                                         "ecogrid://knb/" + onlineDocid + ".1",
1341
                                         null, null, null, null, null, null);
1342
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1343
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
1344

    
1345
            // update the online data
1346
            uploadDocid(onlineDocid + ".2", onlinetestdatafile2, SUCCESS, false);
1347

    
1348
            //  read document and check if it is same as the document which was written
1349
            //  the second time
1350

    
1351
            /////////////////////////////
1352
            // update the document access control - read only
1353
            testdocument = getTestEmlDoc("Testing update access block", null, null,
1354
                                         "ecogrid://knb/" + onlineDocid + ".2",
1355
                                         null, getAccessBlock(anotheruser, true,
1356
                true, false, false, false),
1357
                                         null, null, null, null);
1358
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1359
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
1360

    
1361
            // check if the user mentioned is able to read the document
1362
            // and online data
1363
            m.logout();
1364
            m.login(anotheruser, anotherpassword);
1365
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
1366
            // should be able to read both versions of data
1367
            readDocid(onlineDocid + ".1", SUCCESS, false);
1368
            readDocid(onlineDocid + ".2", SUCCESS, false);
1369

    
1370
            // should not be able to update the document
1371
            testdocument = getTestEmlDoc("Testing update from another user",
1372
                                         null, null,
1373
                                         "ecogrid://knb/" + onlineDocid + ".22",
1374
                                         null, getAccessBlock(anotheruser, true,
1375
                true, false, false, false),
1376
                                         null, null, null, null);
1377
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
1378
            // or the permissions
1379
            testdocument = getTestEmlDoc("Testing update from another user",
1380
                                         null, null,
1381
                                         "ecogrid://knb/" + onlineDocid + ".2",
1382
                                         null, null,
1383
                                         null, null, null, null);
1384
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
1385
            // another test - but this is not access related
1386
            // hence i am commenting this out.
1387
            // uploadDocid(onlineDocid + ".2", onlinetestdatafile2, FAILURE, true);
1388

    
1389
            m.logout();
1390
            m.login(username, password);
1391

    
1392
            /////////////////////////////
1393
            // update the document access control - write only
1394
            testdocument = getTestEmlDoc("Testing update access block", null, null,
1395
                                         "ecogrid://knb/" + onlineDocid + ".2",
1396
                                         null, getAccessBlock(anotheruser, true,
1397
                false, true, false, false),
1398
                                         null, null, null, null);
1399
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
1400
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
1401

    
1402
            // check if the user mentioned is able to read the document
1403
            // and online data
1404
            m.logout();
1405
            m.login(anotheruser, anotherpassword);
1406
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, true);
1407
            // should be able to read both versions of data
1408
            readDocid(onlineDocid + ".1", FAILURE, true);
1409
            readDocid(onlineDocid + ".2", FAILURE, true);
1410

    
1411
            // should be able to update the document
1412
            testdocument = getTestEmlDoc("Testing update from another user",
1413
                                         null, null,
1414
                                         "ecogrid://knb/" + onlineDocid + ".22",
1415
                                         null, getAccessBlock(anotheruser, true,
1416
                false, true, false, false),
1417
                                         null, null, null, null);
1418
            //Errrrrrrror
1419
            //updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1420
            // but not the permissions
1421
            testdocument = getTestEmlDoc("Testing update from another user",
1422
                                         null, null,
1423
                                         "ecogrid://knb/" + onlineDocid + ".2",
1424
                                         null, null,
1425
                                         null, null, null, null);
1426
            updateDocid(newdocid + ".6", testdocument, FAILURE, true);
1427
            // another test - but this is not access related
1428
            // hence i am commenting this out.
1429
            // uploadDocid(onlineDocid + ".2", onlinetestdatafile2, FAILURE, true);
1430

    
1431
            m.logout();
1432
            m.login(username, password);
1433

    
1434
            /////////////////////////////
1435
            // update the document access control - change permission only
1436
            testdocument = getTestEmlDoc("Testing update access block", null, null,
1437
                                         "ecogrid://knb/" + onlineDocid + ".2",
1438
                                         null, getAccessBlock(anotheruser, true,
1439
                false, false, true, false),
1440
                                         null, null, null, null);
1441
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
1442
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, false);
1443

    
1444
            // check if the user mentioned is able to read the document
1445
            // and online data
1446
            m.logout();
1447
            m.login(anotheruser, anotherpassword);
1448
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, true);
1449
            // should be able to read both versions of data
1450
            readDocid(onlineDocid + ".1", FAILURE, true);
1451
            readDocid(onlineDocid + ".2", FAILURE, true);
1452

    
1453
            // should not be able to update the document
1454
            testdocument = getTestEmlDoc("Testing update from another user",
1455
                                         null, null,
1456
                                         "ecogrid://knb/" + onlineDocid + ".22",
1457
                                         null, getAccessBlock(anotheruser, true,
1458
                false, false, true, false),
1459
                                         null, null, null, null);
1460
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
1461
            // but the permissions
1462
            testdocument = getTestEmlDoc("Testing update from another user",
1463
                                         null, null,
1464
                                         "ecogrid://knb/" + onlineDocid + ".2",
1465
                                         null, null,
1466
                                         null, null, null, null);
1467
            // Errrrrrrror
1468
            //updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
1469

    
1470
            m.logout();
1471
            m.login(username, password);
1472

    
1473
            /////////////////////////////
1474
            // update the document access control - all
1475
            testdocument = getTestEmlDoc("Testing update access block", null, null,
1476
                                         "ecogrid://knb/" + onlineDocid + ".2",
1477
                                         null, getAccessBlock(anotheruser, true,
1478
                false, false, false, true),
1479
                                         null, null, null, null);
1480
            updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
1481
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1482

    
1483
            // check if the user mentioned is able to read the document
1484
            // and online data
1485
            m.logout();
1486
            m.login(anotheruser, anotherpassword);
1487
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1488
            // should be able to read both versions of data
1489
            readDocid(onlineDocid + ".1", SUCCESS, false);
1490
            readDocid(onlineDocid + ".2", SUCCESS, false);
1491

    
1492
            // should be able to update the document
1493
            testdocument = getTestEmlDoc("Testing update from another user",
1494
                                         null, null,
1495
                                         "ecogrid://knb/" + onlineDocid + ".22",
1496
                                         null, getAccessBlock(anotheruser, true,
1497
                false, false, false, true),
1498
                                         null, null, null, null);
1499
            updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
1500
            // and the permissions
1501
            testdocument = getTestEmlDoc("Testing update from another user",
1502
                                         null, null,
1503
                                         "ecogrid://knb/" + onlineDocid + ".2",
1504
                                         null, null,
1505
                                         null, null, null, null);
1506
            updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
1507

    
1508
            m.logout();
1509
            m.login(username, password);
1510

    
1511
            // delete the document
1512
            deleteDocid(newdocid + ".10", SUCCESS);
1513
            m.logout();
1514
        }
1515
        catch (MetacatAuthException mae) {
1516
            fail("Authorization failed:\n" + mae.getMessage());
1517
        }
1518
        catch (MetacatInaccessibleException mie) {
1519
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1520
        }
1521
        catch (Exception e) {
1522
            fail("General exception:\n" + e.getMessage());
1523
        }
1524

    
1525
    }
1526

    
1527
    /** *********
1528
     * Test the case when no access is specified and another user is logged in
1529
     * Cases being checked:
1530
     * 1. the user tries to read the document - Failure.
1531
     * 2. the user tries to update the document - Failure.
1532
     * 3. the user tries to update the inline data - Failure.
1533
     * 4. the user tries to update the online data file - Failure.
1534
     * 5. the user tries to update the access rules for the document - Failure.
1535
     * 6. the user tries to update the access rules for the inline data - Failure.
1536
     * 7. the user tries to update the access rules for the online data - Failure.
1537
     * 8. the user tries to delete the document - Failure.
1538
     */
1539
    public void noAccessSpecified_Random() {
1540
        try {
1541
            newdocid = generateDocid();
1542
            String identifier = newdocid + ".1";
1543
            // login
1544
            m.login(username, password);
1545

    
1546
            // insert a document - get the docid
1547
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1548
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS,
1549
                                    true);
1550

    
1551
            // logoutand login as other user
1552
            m.logout();
1553
            m.login(anotheruser, anotherpassword);
1554

    
1555
            // read the document
1556
            readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE, false);
1557

    
1558
            // update the document
1559
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1560

    
1561
            // update the inline data
1562
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1563

    
1564
            // update the online data
1565
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1566

    
1567
            // update the document access control
1568
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1569

    
1570
            // update the document access control for inline data
1571
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1572

    
1573
            // update the document access control for online data
1574
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1575

    
1576
            // delete the document
1577
            deleteDocid(newdocid + ".2", SUCCESS);
1578
            m.logout();
1579
        }
1580
        catch (MetacatAuthException mae) {
1581
            fail("Authorization failed:\n" + mae.getMessage());
1582
        }
1583
        catch (MetacatInaccessibleException mie) {
1584
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1585
        }
1586
        catch (MetacatException me) {
1587
            fail("Metacat Error:\n" + me.getMessage());
1588
        }
1589
        catch (Exception e) {
1590
            fail("General exception:\n" + e.getMessage());
1591
        }
1592

    
1593
    }
1594

    
1595
    /** *********
1596
     * Test the case when no access is specified and public is logged in
1597
     * Cases being checked:
1598
     * 1. the owner tries to read the document - Failure.
1599
     * 2. the owner tries to update the document - Failure.
1600
     * 3. the owner tries to update the inline data - Failure.
1601
     * 4. the owner tries to update the online data file - Failure.
1602
     * 5. the owner tries to update the access rules for the document - Failure.
1603
     * 6. the owner tries to update the access rules for the inline data - Failure.
1604
     * 7. the owner tries to update the access rules for the online data - Failure.
1605
     * 8. the owner tries to delete the document - Failure.
1606
     */
1607
    public void noAccessSpecified_Public() {
1608
        try {
1609
            newdocid = generateDocid();
1610
            String identifier = newdocid + ".1";
1611
            // login
1612
            m.login(username, password);
1613

    
1614
            // insert a document - get the docid
1615
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1616
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS,
1617
                                    false);
1618

    
1619
            // logoutand login as other user
1620
            m.logout();
1621

    
1622
            // read the document
1623
            readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE, true);
1624

    
1625
            // update the document
1626
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1627

    
1628
            // update the inline data
1629
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1630

    
1631
            // update the online data
1632
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1633

    
1634
            // update the document access control
1635
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1636

    
1637
            // update the document access control for inline data
1638
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1639

    
1640
            // update the document access control for online data
1641
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1642

    
1643
            // delete the document
1644
            deleteDocid(newdocid + ".2", SUCCESS);
1645
            m.logout();
1646
        }
1647
        catch (MetacatAuthException mae) {
1648
            fail("Authorization failed:\n" + mae.getMessage());
1649
        }
1650
        catch (MetacatInaccessibleException mie) {
1651
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1652
        }
1653
        catch (MetacatException me) {
1654
            fail("Metacat Error:\n" + me.getMessage());
1655
        }
1656
        catch (Exception e) {
1657
            fail("General exception:\n" + e.getMessage());
1658
        }
1659

    
1660
    }
1661

    
1662
    /**
1663
     * Insert a document into metacat. The expected result is passed as result
1664
     */
1665

    
1666
    private String insertDocid(String docid, String docText, boolean result,
1667
                               boolean expectKarmaException) {
1668
        String response = null;
1669
        try {
1670
            response = m.insert(docid,
1671
                                new StringReader(testdocument), null);
1672
            if (result) {
1673
                assertTrue( (response.indexOf("<success>") != -1));
1674
                assertTrue(response.indexOf(docid) != -1);
1675
            }
1676
            else {
1677
                assertTrue( (response.indexOf("<success>") == -1));
1678
            }
1679
            System.err.println(response);
1680
        }
1681
        catch (MetacatInaccessibleException mie) {
1682
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1683
        }
1684
        catch (InsufficientKarmaException ike) {
1685
            if (!expectKarmaException) {
1686
                fail("Insufficient karma:\n" + ike.getMessage());
1687
            }
1688
        }
1689
        catch (MetacatException me) {
1690
            fail("Metacat Error:\n" + me.getMessage());
1691
        }
1692
        catch (Exception e) {
1693
            fail("General exception:\n" + e.getMessage());
1694
        }
1695
        return response;
1696
    }
1697

    
1698
    /**
1699
     * Insert a document into metacat. The expected result is passed as result
1700
     */
1701

    
1702
    private String uploadDocid(String docid, String filePath, boolean result,
1703
                               boolean expectedKarmaException) {
1704
        String response = null;
1705
        try {
1706
            response = m.upload(docid, new File(filePath));
1707
            if (result) {
1708
                assertTrue( (response.indexOf("<success>") != -1));
1709
                assertTrue(response.indexOf(docid) != -1);
1710
            }
1711
            else {
1712
                assertTrue( (response.indexOf("<success>") == -1));
1713
            }
1714
            System.err.println("respose from metacat: " + response);
1715
        }
1716
        catch (MetacatInaccessibleException mie) {
1717
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1718
        }
1719
        catch (InsufficientKarmaException ike) {
1720
            if (!expectedKarmaException) {
1721
                fail("Insufficient karma:\n" + ike.getMessage());
1722
            }
1723
        }
1724
        catch (MetacatException me) {
1725
            fail("Metacat Error:\n" + me.getMessage());
1726
        }
1727
        catch (Exception e) {
1728
            fail("General exception:\n" + e.getMessage());
1729
        }
1730
        return response;
1731
    }
1732

    
1733
    /**
1734
     * Update a document in metacat. The expected result is passed as result
1735
     */
1736
    private String updateDocid(String docid, String docText, boolean result,
1737
                               boolean expectedKarmaFailure) {
1738
        String response = null;
1739
        try {
1740
            response = m.update(docid,
1741
                                new StringReader(testdocument), null);
1742

    
1743
            if (result) {
1744
                assertTrue( (response.indexOf("<success>") != -1));
1745
                assertTrue(response.indexOf(docid) != -1);
1746
            }
1747
            else {
1748
                assertTrue( (response.indexOf("<success>") == -1));
1749
            }
1750
            System.err.println(response);
1751
        }
1752
        catch (MetacatInaccessibleException mie) {
1753
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1754
        }
1755
        catch (InsufficientKarmaException ike) {
1756
            if (!expectedKarmaFailure) {
1757
                fail("Insufficient karma:\n" + ike.getMessage());
1758
            }
1759
        }
1760
        catch (MetacatException me) {
1761
            if (! (expectedKarmaFailure &&
1762
                   (me.getMessage().indexOf(
1763
                "User try to update a access module which it doesn't have \"ALL\" permission") !=
1764
                    -1))) {
1765
                fail("Metacat Error:\n" + me.getMessage());
1766
            }
1767
        }
1768
        catch (Exception e) {
1769
            fail("General exception:\n" + e.getMessage());
1770
        }
1771

    
1772
        return response;
1773
    }
1774

    
1775
    /**
1776
     * Delete a document into metacat. The expected result is passed as result
1777
     */
1778
    private void deleteDocid(String docid, boolean result) {
1779
        try {
1780
            String response = m.delete(docid);
1781
            if (result) {
1782
                assertTrue(response.indexOf("<success>") != -1);
1783
            }
1784
            else {
1785
                assertTrue(response.indexOf("<success>") == -1);
1786
            }
1787
            System.err.println(response);
1788
        }
1789
        catch (MetacatInaccessibleException mie) {
1790
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1791
        }
1792
        catch (InsufficientKarmaException ike) {
1793
            fail("Insufficient karma:\n" + ike.getMessage());
1794
        }
1795
        catch (MetacatException me) {
1796
            fail("Metacat Error:\n" + me.getMessage());
1797
        }
1798
        catch (Exception e) {
1799
            fail("General exception:\n" + e.getMessage());
1800
        }
1801
    }
1802

    
1803
    /**
1804
     * Read a document from metacat. The expected result is passed as result
1805
     */
1806
    private void readDocid(String docid, boolean result,
1807
                           boolean expextedKarmaFailure) {
1808
        try {
1809
            Reader r = m.read(docid);
1810
            String response = IOUtil.getAsString(r, true);
1811

    
1812
            if (!result) {
1813
                assertTrue(response.indexOf("<success>") == -1);
1814
            }
1815
            // System.err.println(response);
1816
        }
1817
        catch (MetacatInaccessibleException mie) {
1818
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1819
        }
1820
        catch (InsufficientKarmaException ike) {
1821
            if (!expextedKarmaFailure) {
1822
                fail("Insufficient karma:\n" + ike.getMessage());
1823
            }
1824
        }
1825
        catch (MetacatException me) {
1826
            fail("Metacat Error:\n" + me.getMessage());
1827
        }
1828
        catch (Exception e) {
1829
            fail("General exception:\n" + e.getMessage());
1830
        }
1831
    }
1832

    
1833
    /**
1834
     * Read a document from metacat and check if it is equal to a given string.
1835
     * The expected result is passed as result
1836
     */
1837

    
1838
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1839
                                         boolean result,
1840
                                         boolean expextedKarmaFailure) {
1841
        try {
1842
            Reader r = m.read(docid);
1843
            String doc = IOUtil.getAsString(r, true);
1844
            if (result) {
1845

    
1846
                if (!testDoc.equals(doc)) {
1847
                    System.out.println("doc    :" + doc);
1848
                    System.out.println("testDoc:" + testDoc);
1849
                }
1850

    
1851
                assertTrue(testDoc.equals(doc));
1852
            }
1853
            else {
1854
                assertTrue(doc.indexOf("<error>") != -1);
1855
            }
1856
        }
1857
        catch (MetacatInaccessibleException mie) {
1858
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1859
        }
1860
        catch (InsufficientKarmaException ike) {
1861
            if (!expextedKarmaFailure) {
1862
                fail("Insufficient karma:\n" + ike.getMessage());
1863
            }
1864
        }
1865
        catch (MetacatException me) {
1866
            fail("Metacat Error:\n" + me.getMessage());
1867
        }
1868
        catch (Exception e) {
1869
            fail("General exception:\n" + e.getMessage());
1870
        }
1871

    
1872
    }
1873

    
1874
    /**
1875
     * Create a hopefully unique docid for testing insert and update. Does
1876
     * not include the 'revision' part of the id.
1877
     *
1878
     * @return a String docid based on the current date and time
1879
     */
1880
    private String generateDocid() {
1881
        StringBuffer docid = new StringBuffer(prefix);
1882
        docid.append(".");
1883

    
1884
        // Create a calendar to get the date formatted properly
1885
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1886
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1887
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1888
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1889
                       2 * 60 * 60 * 1000);
1890
        Calendar calendar = new GregorianCalendar(pdt);
1891
        Date trialTime = new Date();
1892
        calendar.setTime(trialTime);
1893
        docid.append(calendar.get(Calendar.YEAR));
1894
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1895
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1896
        docid.append(calendar.get(Calendar.MINUTE));
1897
        docid.append(calendar.get(Calendar.SECOND));
1898

    
1899
        return docid.toString();
1900
    }
1901
}
(1-1/9)