Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: tao $'
8
 *     '$Date: 2008-05-15 16:59:09 -0700 (Thu, 15 May 2008) $'
9
 * '$Revision: 3868 $'
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
import java.util.Vector;
36

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

    
51
/**
52
 * A JUnit test for testing Access Control in Metacat
53
 */
54
public class AccessControlTest extends TestCase {
55
	 /* Initialize Options*/
56
    static
57
    {
58
  	  try
59
  	  {
60
  		  Options.initialize(new File("build/tests/metacat.properties"));
61
  		  MetaCatUtil.pathsForIndexing 
62
  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
63
  	  }
64
  	  catch(Exception e)
65
  	  {
66
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
67
  	  }
68
    }
69

    
70
    private String metacatUrl = MetaCatUtil.getOption("metacatUrl");
71
    private String username = MetaCatUtil.getOption("mcuser");
72
    private String password = MetaCatUtil.getOption("mcpassword");
73
    private String anotheruser = MetaCatUtil.getOption("mcanotheruser");
74
    private String anotherpassword = MetaCatUtil.getOption("mcanotherpassword");
75
    private String prefix = "test";
76
    private String newdocid = null;
77
    private String testdocument = "";
78
    private String onlineDocid;
79
    private String onlinetestdatafile1 = "test/onlineDataFile1";
80
    private String onlinetestdatafile2 = "test/onlineDataFile2";
81
    private static final String ALLOWFIRST = "allowFirst";
82
    private static final String DENYFIRST = "denyFirst";
83

    
84
    private Metacat m;
85

    
86
    private boolean SUCCESS = true;
87
    private boolean FAILURE = false;
88

    
89
    /**
90
     * These variables are for eml-2.0.1 only. For other eml versions,
91
     * this function might have to modified
92
     */
93

    
94
    private String testEml_201_Header =
95
        "<?xml version=\"1.0\"?><eml:eml" +
96
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
97
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
98
        " packageId=\"eml.1.1\" system=\"knb\"" +
99
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
100
        " scope=\"system\">";
101

    
102
    private String testEml_200_Header =
103
    "<?xml version=\"1.0\"?><eml:eml" +
104
    " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\"" +
105
    " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
106
    " packageId=\"eml.1.1\" system=\"knb\"" +
107
    " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\"" +
108
    " scope=\"system\">";
109

    
110
    private String testEmlCreatorBlock =
111
        "<creator scope=\"document\">                                       " +
112
        " <individualName>                                                  " +
113
        "    <surName>Smith</surName>                                       " +
114
        " </individualName>                                                 " +
115
        "</creator>                                                         ";
116

    
117
    private String testEmlContactBlock =
118
        "<contact scope=\"document\">                                       " +
119
        " <individualName>                                                  " +
120
        "    <surName>Jackson</surName>                                     " +
121
        " </individualName>                                                 " +
122
        "</contact>                                                         ";
123

    
124
    private String testEmlInlineBlock1 =
125
        "<inline>                                                           " +
126
        "  <admin>                                                          " +
127
        "    <contact>                                                      " +
128
        "      <name>Operator</name>                                        " +
129
        "      <institution>PSI</institution>                               " +
130
        "    </contact>                                                     " +
131
        "  </admin>                                                         " +
132
        "</inline>                                                          ";
133

    
134
    private String testEmlInlineBlock2 =
135
        "<inline>                                                           " +
136
        "  <instrument>                                                     " +
137
        "    <instName>LCQ</instName>                                       " +
138
        "    <source type=\"ESI\"></source>                                 " +
139
        "    <detector type=\"EM\"></detector>                              " +
140
        "  </instrument>                                                    " +
141
        "</inline>                                                          ";
142

    
143
    /*
144
     * Retrus an access block base on params passed and the defaul perm order - allow first
145
     */
146
    private String getAccessBlock(String principal, boolean grantAccess,
147
                              boolean read, boolean write,
148
                           boolean changePermission, boolean all)
149
    {
150
    	return getAccessBlock(principal,  grantAccess,
151
                read, write,
152
                changePermission, all, ALLOWFIRST);
153
    }
154
    /**
155
     * This function returns an access block based on the params passed
156
     */
157
    private String getAccessBlock(String principal, boolean grantAccess,
158
                                  boolean read, boolean write,
159
                                  boolean changePermission, boolean all, String permOrder) {
160
        String accessBlock = "<access " +
161
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
162
            " order=\""+
163
            permOrder +"\""+
164
            " scope=\"document\""  +
165
            ">";
166

    
167
        accessBlock += generateOneAccessRule(principal,  grantAccess,
168
                                  read, write, changePermission, all);
169
        accessBlock += "</access>";
170

    
171
        return accessBlock;
172

    
173
    }
174
    
175
    /*
176
     * Gets eml access block base on given acccess rules and perm order
177
     */
178
    private String  getAccessBlock(Vector accessRules, String permOrder)
179
    {
180
    	String accessBlock = "<access " +
181
        "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
182
        " order=\""+
183
        permOrder +"\""+
184
        " scope=\"document\""  +
185
        ">";
186
    	// adding rules
187
    	if (accessRules != null  && !accessRules.isEmpty())
188
    	{
189
    		for (int i=0; i<accessRules.size(); i++)
190
    		{
191
    			String rule = (String)accessRules.elementAt(i);
192
    			accessBlock += rule;
193
    			
194
    		}		
195
    	}
196
    	accessBlock += "</access>";
197
    	return accessBlock;
198
    }
199
    
200
    /*
201
     * Generates a access rule for given parameter. Note this xml portion doesn't include
202
     * <access></access> 
203
     */
204
    private String generateOneAccessRule(String principal, boolean grantAccess,
205
            boolean read, boolean write, boolean changePermission, boolean all)
206
    {
207
    	 String accessBlock = "";
208

    
209
	     if (grantAccess) {
210
	         accessBlock = "<allow>";
211
	     }
212
	     else {
213
	         accessBlock = "<deny>";
214
	     }
215
	
216
	     accessBlock = accessBlock + "<principal>" + principal + "</principal>";
217
	
218
	     if (all) {
219
	         accessBlock += "<permission>all</permission>";
220
	     }
221
	     else {
222
	         if (read) {
223
	             accessBlock += "<permission>read</permission>";
224
	         }
225
	         if (write) {
226
	             accessBlock += "<permission>write</permission>";
227
	         }
228
	         if (changePermission) {
229
	             accessBlock += "<permission>changePermission</permission>";
230
	         }
231
	     }
232
	
233
	     if (grantAccess) {
234
	         accessBlock += "</allow>";
235
	     }
236
	     else {
237
	         accessBlock += "</deny>";
238
	     }
239
	     return accessBlock;
240

    
241
    }
242

    
243
    /**
244
     * This function returns a valid eml document with no access rules
245
     * This function is for eml-2.0.1 only. For other eml versions,
246
     * this function might have to modified
247
     */
248
    private String getTestEmlDoc(String title, String inlineData1,
249
                                 String inlineData2, String onlineUrl1,
250
                                 String onlineUrl2, String docAccessBlock,
251
                                 String inlineAccessBlock1,
252
                                 String inlineAccessBlock2,
253
                                 String onlineAccessBlock1,
254
                                 String onlineAccessBlock2) {
255

    
256
        String testDocument = "";
257
        testDocument = testDocument + testEml_201_Header +
258
            "<dataset scope=\"document\"><title>" + title + "</title>" +
259
            testEmlCreatorBlock;
260

    
261
        if (inlineData1 != null) {
262
            testDocument = testDocument
263
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
264
                + inlineData1 + "</distribution>";
265
        }
266
        if (inlineData2 != null) {
267
            testDocument = testDocument
268
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
269
                + inlineData2 + "</distribution>";
270
        }
271
        if (onlineUrl1 != null) {
272
            testDocument = testDocument
273
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
274
                + "<online><url function=\"download\">"
275
                + onlineUrl1 + "</url></online></distribution>";
276
        }
277
        if (onlineUrl2 != null) {
278
            testDocument = testDocument +
279
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
280
                + "<online><url function=\"download\">"
281
                + onlineUrl2 + "</url></online></distribution>";
282
        }
283
        testDocument += testEmlContactBlock;
284

    
285
        if (docAccessBlock != null) {
286
            testDocument += docAccessBlock;
287
        }
288

    
289
        testDocument += "</dataset>";
290

    
291
        if (inlineAccessBlock1 != null) {
292
            testDocument += "<additionalMetadata>";
293
            testDocument += "<describes>inlineEntity1</describes>";
294
            testDocument += inlineAccessBlock1;
295
            testDocument += "</additionalMetadata>";
296
        }
297

    
298
        if (inlineAccessBlock2 != null) {
299
            testDocument += "<additionalMetadata>";
300
            testDocument += "<describes>inlineEntity2</describes>";
301
            testDocument += inlineAccessBlock2;
302
            testDocument += "</additionalMetadata>";
303
        }
304

    
305
        if (onlineAccessBlock1 != null) {
306
            testDocument += "<additionalMetadata>";
307
            testDocument += "<describes>onlineEntity1</describes>";
308
            testDocument += onlineAccessBlock1;
309
            testDocument += "</additionalMetadata>";
310
        }
311

    
312
        if (onlineAccessBlock2 != null) {
313
            testDocument += "<additionalMetadata>";
314
            testDocument += "<describes>onlineEntity2</describes>";
315
            testDocument += onlineAccessBlock2;
316
            testDocument += "</additionalMetadata>";
317
        }
318

    
319
        testDocument += "</eml:eml>";
320

    
321
        //System.out.println("Returning following document" + testDocument);
322
        return testDocument;
323
    }
324

    
325
    /**
326
     * Constructor to build the test
327
     *
328
     * @param name the name of the test method
329
     */
330
    public AccessControlTest(String name) {
331
        super(name);
332
        newdocid = generateDocid();
333
    }
334

    
335
    /**
336
     * Establish a testing framework by initializing appropriate objects
337
     */
338
    public void setUp() {
339
        try {
340
            System.err.println("Test Metacat: " + metacatUrl);
341
            m = MetacatFactory.createMetacatConnection(metacatUrl);
342
        }
343
        catch (MetacatInaccessibleException mie) {
344
            System.err.println("Metacat is: " + metacatUrl);
345
            fail("Metacat connection failed." + mie.getMessage());
346
        }
347
    }
348

    
349
    /**
350
     * Release any objects after tests are complete
351
     */
352
    public void tearDown() {
353
    }
354

    
355
    /**
356
     * Create a suite of tests to be run together
357
     */
358
    public static Test suite() {
359
        TestSuite suite = new TestSuite();
360
        suite.addTest(new AccessControlTest("initialize"));
361
        // Test basic functions
362
        suite.addTest(new AccessControlTest("documentTest"));
363
        suite.addTest(new AccessControlTest("AccessControlTestForPublic"));
364
        suite.addTest(new AccessControlTest("testAllowFirst"));
365
        suite.addTest(new AccessControlTest("testDenyFirst"));
366

    
367
        return suite;
368
    }
369

    
370
    /**
371
     * Run an initial test that always passes to check that the test
372
     * harness is working.
373
     */
374
    public void initialize() {
375
        assertTrue(1 == 1);
376
    }
377
    
378
    
379
    /**
380
     * Tests when permission order is allowFirst, the combination of allow and deny rules  affect 
381
     * user to read, update and delete a document. Here are test cases
382
     *1.An user inserts a document with access  rules (allowFirst) - allow READ rule for another user,
383
     *   deny  READ rule for public. 
384
     *      Another user reads this document - failure
385
     *      Another user updates this document(except access part) -failure
386
     *      Another user updates this document(access part) -failure
387
     *      Another user deletes this document - failure
388
     *2. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
389
     *    deny READ and WRITE rule for public. 
390
     *       Another user reads this document - failure
391
     *       Another user updates this document(except access part) -failure
392
     *       Another user updates this document(access part) -failure
393
     *       Another user deletes this document - failure
394
     *3. The user updates this documents with access rules (allowFirst) - allow ALL rule for another user,
395
     *     deny ALL rule for public. 
396
     *         Another user reads this document - failure
397
     *         Another user updates this document(except access part) -failure
398
     *         Another user updates this document(access part) -failure
399
     *         Another user deletes this document - failure
400
     *4. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
401
     *    deny  WRITE rule for public. 
402
     *       Another user reads this document - success
403
     *       Another user updates this document(except access part) -failure
404
     *       Another user updates this document(access part) -failure
405
     *       Another user deletes this document - failure
406
     *5. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
407
     *    deny READ rule for public. 
408
     *       Another user reads this document - failure
409
     *       Another user updates this document(except access part) - success
410
     *       Another user updates this document(access part) -failure
411
     *       Another user deletes this document - failure
412
     *6. The user updates this documents with access rules (allowFirst) - allow READ rule for another user,
413
     *     deny READ rule for a group (which another user is in the group)
414
     *         Another user reads this document - failure
415
     *         Another user updates this document(except access part) -failure
416
     *         Another user updates this document(access part) -failure
417
     *         Another user deletes this document - failure
418
     *7. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
419
     *      deny READ and WRITE rule for a group (which another user is in the group)
420
     *         Another user reads this document - failure
421
     *         Another user updates this document(except access part) -failure
422
     *         Another user updates this document(access part) -failure
423
     *         Another user deletes this document - failure
424
     *8. The user updates this documents with access rules (allowFirst) - allow ALL rule for another user,
425
     *     deny ALL rule for a group (which another user is in the group)
426
     *          Another user reads this document - failure
427
     *          Another user updates this document(except access part) -failure
428
     *          Another user updates this document(access part) -failure
429
     *          Another user deletes this document - failure
430
     *9. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
431
     *     deny WRITE rule for a group (which another user is in the group)
432
     *          Another user reads this document - success
433
     *          Another user updates this document(except access part) -failure
434
     *          Another user updates this document(access part) -failure
435
     *          Another user deletes this document - failure
436
     *10. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
437
     *     deny READ rule for a group (which another user is in the group)
438
     *          Another user reads this document - failure
439
     *          Another user updates this document(except access part) - success
440
     *          Another user updates this document(access part) -failure
441
     *          Another user deletes this document - failure
442
     */
443
    public void testAllowFirst()
444
    {
445
    	try {
446
	    	newdocid = generateDocid();	        
447
	        //====1 inserts a document with access  rules (allowFirst) - allow READ rule for another user,
448
	        // deny  READ rule for public.
449
	        String accessRule1 = generateOneAccessRule(anotheruser, true,
450
                    true, false, false, false);
451
	        String accessRule2 = generateOneAccessRule("public", false,
452
                    true, false, false, false);
453
	        Vector accessRules = new Vector();
454
	        accessRules.add(accessRule1);
455
	        accessRules.add(accessRule2);
456
	        String access = getAccessBlock(accessRules, ALLOWFIRST);
457
	        System.out.println("the access part is "+access);
458
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
459
                    null, null, null,
460
                    null, access ,
461
                    null, null, null, null);
462
	        // login
463
	        m.login(username, password);
464
	        insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
465
	        m.logout();
466
	        //login as another user
467
	        m.login(anotheruser, anotherpassword);
468
	        //fails to read this document
469
	        readDocidWhichEqualsDoc(newdocid + ".1", testdocument, FAILURE, true);
470
	        //fails to update this document
471
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
472
            //fails to update access part
473
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
474
                    null, null, null,
475
                    null,  getAccessBlock(anotheruser, true,
476
                            true, false, false, false),
477
                    null, null, null, null);
478
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
479
            //fails to delete the document
480
            deleteDocid(newdocid + ".1", FAILURE, true);
481
            //logout
482
            m.logout();
483
            
484
            //====2 inserts a document with access  rules (allowFirst) - allow READ and WRITE rule for another user,
485
	        // deny  READ and WRITE rule for public.
486
	        accessRule1 = generateOneAccessRule(anotheruser, true,
487
                    true, true, false, false);
488
	        accessRule2 = generateOneAccessRule("public", false,
489
                    true, true, false, false);
490
	        accessRules = new Vector();
491
	        accessRules.add(accessRule1);
492
	        accessRules.add(accessRule2);
493
	        access = getAccessBlock(accessRules, ALLOWFIRST);
494
	        System.out.println("the access part is "+access);
495
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
496
                    null, null, null,
497
                    null, access ,
498
                    null, null, null, null);
499
	        // login
500
	        m.login(username, password);
501
	        updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
502
	        m.logout();
503
	        //login as another user
504
	        m.login(anotheruser, anotherpassword);
505
	        //fails to read this document
506
	        readDocidWhichEqualsDoc(newdocid + ".2", testdocument, FAILURE, true);
507
	        //fails to update this document
508
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
509
            //fails to update access part
510
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
511
                    null, null, null,
512
                    null,  getAccessBlock(anotheruser, true,
513
                            true, false, false, false),
514
                    null, null, null, null);
515
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
516
            //fails to delete the document
517
            deleteDocid(newdocid + ".2", FAILURE, true);
518
            //logout
519
            m.logout();
520
            
521
             //====3 inserts a document with access  rules (allowFirst) - allow ALL rule for another user,
522
	        // deny  ALL rule for public.
523
	        accessRule1 = generateOneAccessRule(anotheruser, true,
524
                    true, true, true, true);
525
	        accessRule2 = generateOneAccessRule("public", false,
526
                    true, true, true, true);
527
	        accessRules = new Vector();
528
	        accessRules.add(accessRule1);
529
	        accessRules.add(accessRule2);
530
	        access = getAccessBlock(accessRules, ALLOWFIRST);
531
	        System.out.println("the access part is "+access);
532
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
533
                    null, null, null,
534
                    null, access ,
535
                    null, null, null, null);
536
	        // login
537
	        m.login(username, password);
538
	        updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
539
	        m.logout();
540
	        //login as another user
541
	        m.login(anotheruser, anotherpassword);
542
	        //fails to read this document
543
	        readDocidWhichEqualsDoc(newdocid + ".3", testdocument, FAILURE, true);
544
	        //fails to update this document
545
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
546
            //fails to update access part
547
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
548
                    null, null, null,
549
                    null,  getAccessBlock(anotheruser, true,
550
                            true, false, false, false),
551
                    null, null, null, null);
552
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
553
            //fails to delete the document
554
            deleteDocid(newdocid + ".3", FAILURE, true);
555
            //logout
556
            m.logout();
557
            
558
            
559
            
560
            
561
            
562
             //  ====4 The user updates this documents with access rules (allowFirst) - allow READ and WRITE 
563
            //rule for another user, deny  WRITE rule for public. 
564
	        accessRule1 = generateOneAccessRule(anotheruser, true,
565
                    true, true, false, false);
566
	        accessRule2 = generateOneAccessRule("public", false, false, true, false, false);
567
	        accessRules = new Vector();
568
	        accessRules.add(accessRule1);
569
	        accessRules.add(accessRule2);
570
	        access = getAccessBlock(accessRules, ALLOWFIRST);
571
	        System.out.println("the access part is "+access);
572
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
573
                    null, null, null,
574
                    null, access ,
575
                    null, null, null, null);
576
	        // login
577
	        m.login(username, password);
578
	        updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
579
	        m.logout();
580
	        //login as another user
581
	        m.login(anotheruser, anotherpassword);
582
	        //fails to read this document
583
	        readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
584
	        //fails to update this document
585
            updateDocid(newdocid + ".5", testdocument, FAILURE, true);
586
            //fails to update access part
587
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
588
                    null, null, null,
589
                    null,  getAccessBlock(anotheruser, true,
590
                            true, false, false, false),
591
                    null, null, null, null);
592
            updateDocid(newdocid + ".5", testdocument, FAILURE, true);
593
            //fails to delete the document
594
            deleteDocid(newdocid + ".4", FAILURE, true);
595
            //logout
596
            m.logout();
597
	        
598
            
599
            //  ====5. The user updates this documents with access rules (allowFirst) - allow READ and WRITE
600
            //rule for another user, deny READ rule for public. 
601
	        accessRule1 = generateOneAccessRule(anotheruser, true,
602
                    true, true, false, false);
603
	        accessRule2 = generateOneAccessRule("public", false, true, false, false, false);
604
	        accessRules = new Vector();
605
	        accessRules.add(accessRule1);
606
	        accessRules.add(accessRule2);
607
	        access = getAccessBlock(accessRules, ALLOWFIRST);
608
	        System.out.println("the access part is "+access);
609
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
610
                    null, null, null,
611
                    null, access ,
612
                    null, null, null, null);
613
	        // login
614
	        m.login(username, password);
615
	        updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
616
	        m.logout();
617
	        //login as another user
618
	        m.login(anotheruser, anotherpassword);
619
	        //fails to read this document
620
	        readDocidWhichEqualsDoc(newdocid + ".5", testdocument, FAILURE, true);
621
	        //fails to update this document
622
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
623
            //fails to update access part
624
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
625
                    null, null, null,
626
                    null,  getAccessBlock(anotheruser, true,
627
                            true, false, false, false),
628
                    null, null, null, null);
629
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
630
            //fails to delete the document
631
            deleteDocid(newdocid + ".6", FAILURE, true);
632
            //logout
633
            m.logout();
634
            
635
            
636
            
637
             //   ====6 inserts a document with access  rules (allowFirst) - allow READ rule for another user,
638
	        // deny  READ rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and another user is 
639
            // in this group).
640
	        accessRule1 = generateOneAccessRule(anotheruser, true,
641
                    true, false, false, false);
642
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
643
                   true, false, false, false);
644
	        accessRules = new Vector();
645
	        accessRules.add(accessRule1);
646
	        accessRules.add(accessRule2);
647
	        access = getAccessBlock(accessRules, ALLOWFIRST);
648
	        System.out.println("the access part is "+access);
649
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
650
                    null, null, null,
651
                    null, access ,
652
                    null, null, null, null);
653
	        // login
654
	        m.login(username, password);
655
	        updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
656
	        m.logout();
657
	        //login as another user
658
	        m.login(anotheruser, anotherpassword);
659
	        //fails to read this document
660
	        readDocidWhichEqualsDoc(newdocid + ".7", testdocument, FAILURE, true);
661
	        //fails to update this document
662
            updateDocid(newdocid + ".8", testdocument, FAILURE, true);
663
            //fails to update access part
664
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
665
                    null, null, null,
666
                    null,  getAccessBlock(anotheruser, true,
667
                            true, false, false, false),
668
                    null, null, null, null);
669
            updateDocid(newdocid + ".8", testdocument, FAILURE, true);
670
            //fails to delete the document
671
            deleteDocid(newdocid + ".7", FAILURE, true);
672
            //logout
673
            m.logout();
674
            
675
            //====7 inserts a document with access  rules (allowFirst) - allow READ and WRITE rule for another 
676
            //user, deny  READ and WRITE rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and
677
            // the other user is in this group)
678
	        accessRule1 = generateOneAccessRule(anotheruser, true,
679
                    true, true, false, false);
680
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
681
                    true, true, false, false);
682
	        accessRules = new Vector();
683
	        accessRules.add(accessRule1);
684
	        accessRules.add(accessRule2);
685
	        access = getAccessBlock(accessRules, ALLOWFIRST);
686
	        System.out.println("the access part is "+access);
687
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
688
                    null, null, null,
689
                    null, access ,
690
                    null, null, null, null);
691
	        // login
692
	        m.login(username, password);
693
	        updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
694
	        m.logout();
695
	        //login as another user
696
	        m.login(anotheruser, anotherpassword);
697
	        //fails to read this document
698
	        readDocidWhichEqualsDoc(newdocid + ".8", testdocument, FAILURE, true);
699
	        //fails to update this document
700
            updateDocid(newdocid + ".9", testdocument, FAILURE, true);
701
            //fails to update access part
702
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
703
                    null, null, null,
704
                    null,  getAccessBlock(anotheruser, true,
705
                            true, false, false, false),
706
                    null, null, null, null);
707
            updateDocid(newdocid + ".9", testdocument, FAILURE, true);
708
            //fails to delete the document
709
            deleteDocid(newdocid + ".8", FAILURE, true);
710
            //logout
711
            m.logout();
712
            
713
             //====8 inserts a document with access  rules (allowFirst) - allow ALL rule for another user,
714
	        // deny  ALL rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and the other user
715
            // is in this group)
716
	        accessRule1 = generateOneAccessRule(anotheruser, true,
717
                    true, true, true, true);
718
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
719
                    true, true, true, true);
720
	        accessRules = new Vector();
721
	        accessRules.add(accessRule1);
722
	        accessRules.add(accessRule2);
723
	        access = getAccessBlock(accessRules, ALLOWFIRST);
724
	        System.out.println("the access part is "+access);
725
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
726
                    null, null, null,
727
                    null, access ,
728
                    null, null, null, null);
729
	        // login
730
	        m.login(username, password);
731
	        updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
732
	        m.logout();
733
	        //login as another user
734
	        m.login(anotheruser, anotherpassword);
735
	        //fails to read this document
736
	        readDocidWhichEqualsDoc(newdocid + ".9", testdocument, FAILURE, true);
737
	        //fails to update this document
738
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
739
            //fails to update access part
740
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
741
                    null, null, null,
742
                    null,  getAccessBlock(anotheruser, true,
743
                            true, false, false, false),
744
                    null, null, null, null);
745
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
746
            //fails to delete the document
747
            deleteDocid(newdocid + ".9", FAILURE, true);
748
            //logout
749
            m.logout();
750
            
751
            
752
            
753
            
754
            
755
             //  ====9 The user updates this documents with access rules (allowFirst) - allow READ and WRITE 
756
            //rule for another user, deny  WRITE rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org
757
            // and another user is in this group). 
758
	        accessRule1 = generateOneAccessRule(anotheruser, true,
759
                    true, true, false, false);
760
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false, false, true, false, false);
761
	        accessRules = new Vector();
762
	        accessRules.add(accessRule1);
763
	        accessRules.add(accessRule2);
764
	        access = getAccessBlock(accessRules, ALLOWFIRST);
765
	        System.out.println("the access part is "+access);
766
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
767
                    null, null, null,
768
                    null, access ,
769
                    null, null, null, null);
770
	        // login
771
	        m.login(username, password);
772
	        updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
773
	        m.logout();
774
	        //login as another user
775
	        m.login(anotheruser, anotherpassword);
776
	        //succeed to read this document
777
	        readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
778
	        //fails to update this document
779
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
780
            //fails to update access part
781
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
782
                    null, null, null,
783
                    null,  getAccessBlock(anotheruser, true,
784
                            true, false, false, false),
785
                    null, null, null, null);
786
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
787
            //fails to delete the document
788
            deleteDocid(newdocid + ".10", FAILURE, true);
789
            //logout
790
            m.logout();
791
	        
792
            
793
            //  ====10. The user updates this documents with access rules (allowFirst) - allow READ and WRITE
794
            //rule for another user, deny READ rule for a group(cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and
795
            //another user is in this group). 
796
	        accessRule1 = generateOneAccessRule(anotheruser, true,
797
                    true, true, false, false);
798
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
799
                    true, false, false, false);
800
	        accessRules = new Vector();
801
	        accessRules.add(accessRule1);
802
	        accessRules.add(accessRule2);
803
	        access = getAccessBlock(accessRules, ALLOWFIRST);
804
	        System.out.println("the access part is "+access);
805
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
806
                    null, null, null,
807
                    null, access ,
808
                    null, null, null, null);
809
	        // login
810
	        m.login(username, password);
811
	        updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
812
	        m.logout();
813
	        //login as another user
814
	        m.login(anotheruser, anotherpassword);
815
	        //fails to read this document
816
	        readDocidWhichEqualsDoc(newdocid + ".11", testdocument, FAILURE, true);
817
	        //succeed to update this document
818
            updateDocid(newdocid + ".12", testdocument, SUCCESS, false);
819
            //fails to update access part
820
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
821
                    null, null, null,
822
                    null,  getAccessBlock(anotheruser, true,
823
                            true, false, false, false),
824
                    null, null, null, null);
825
            updateDocid(newdocid + ".13", testdocument, FAILURE, true);
826
            //fails to delete the document
827
            deleteDocid(newdocid + ".12", FAILURE, true);
828
            //logout
829
            m.logout();
830
	    }
831
	    catch (MetacatAuthException mae) {
832
	        fail("Authorization failed:\n" + mae.getMessage());
833
	    }
834
	    catch (MetacatInaccessibleException mie) {
835
	        fail("Metacat Inaccessible:\n" + mie.getMessage());
836
	    }
837
	    catch (Exception e) {
838
	        fail("General exception:\n" + e.getMessage());
839
	    }
840
    }
841
    
842
    /**
843
     * Tests when permission order is denyFirst, the combination of allow and deny rules  affect 
844
     * user to read, update and delete a document. Here are test cases
845
     *1.An user inserts a document with access  rules (denyFirst) - allow READ rule for another user,
846
     *   deny  READ rule for public. 
847
     *      Another user reads this document - success
848
     *      Another user updates this document(except access part) -failure
849
     *      Another user updates this document(access part) -failure
850
     *      Another user deletes this document - failure
851
     *2. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
852
     *    deny READ and WRITE rule for public. 
853
     *       Another user reads this document - success
854
     *       Another user updates this document(except access part) -success
855
     *       Another user updates this document(access part) -failure
856
     *       Another user deletes this document - failure
857
     *3. The user updates this documents with access rules (denyFirst) - allow ALL rule for another user,
858
     *     deny ALL rule for public. 
859
     *         Another user reads this document - success
860
     *         Another user updates this document(except access part) -success
861
     *         Another user updates this document(access part) -success
862
     *         Another user deletes this document - success
863
     *4. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
864
     *    deny  WRITE rule for public. 
865
     *       Another user reads this document - success
866
     *       Another user updates this document(except access part) -success
867
     *       Another user updates this document(access part) -failure
868
     *       Another user deletes this document - failure
869
     *5. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
870
     *    deny READ rule for public. 
871
     *       Another user reads this document - success
872
     *       Another user updates this document(except access part) - success
873
     *       Another user updates this document(access part) -failure
874
     *       Another user deletes this document - failure
875
     *6. The user updates this documents with access rules (denyFirst) - allow READ rule for another user,
876
     *     deny READ rule for a group (which another user is in the group)
877
     *         Another user reads this document - success
878
     *         Another user updates this document(except access part) -failure
879
     *         Another user updates this document(access part) -failure
880
     *         Another user deletes this document - failure
881
     *7. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
882
     *      deny READ and WRITE rule for a group (which another user is in the group)
883
     *         Another user reads this document - success
884
     *         Another user updates this document(except access part) - success
885
     *         Another user updates this document(access part) -failure
886
     *         Another user deletes this document - failure
887
     *8. The user updates this documents with access rules (denyFirst) - allow ALL rule for another user,
888
     *     deny ALL rule for a group (which another user is in the group)
889
     *          Another user reads this document - success
890
     *          Another user updates this document(except access part) - success
891
     *          Another user updates this document(access part) - success
892
     *          Another user deletes this document - success
893
     *9. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
894
     *     deny WRITE rule for a group (which another user is in the group)
895
     *          Another user reads this document - success
896
     *          Another user updates this document(except access part) - success
897
     *          Another user updates this document(access part) - failure
898
     *          Another user deletes this document - failure
899
     *10. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
900
     *     deny READ rule for a group (which another user is in the group)
901
     *          Another user reads this document - success
902
     *          Another user updates this document(except access part) - success
903
     *          Another user updates this document(access part) -failure
904
     *          Another user deletes this document - failure
905
     */
906
    public void testDenyFirst()
907
    {
908
    	try {
909
	    	newdocid = generateDocid();	        
910
	        //====1 inserts a document with access  rules (denyFirst) - allow READ rule for another user,
911
	        // deny  READ rule for public.
912
	        String accessRule1 = generateOneAccessRule(anotheruser, true,
913
                    true, false, false, false);
914
	        String accessRule2 = generateOneAccessRule("public", false,
915
                    true, false, false, false);
916
	        Vector accessRules = new Vector();
917
	        accessRules.add(accessRule1);
918
	        accessRules.add(accessRule2);
919
	        String access = getAccessBlock(accessRules, DENYFIRST);
920
	        System.out.println("the access part is "+access);
921
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
922
                    null, null, null,
923
                    null, access ,
924
                    null, null, null, null);
925
	        // login
926
	        m.login(username, password);
927
	        insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
928
	        m.logout();
929
	        //login as another user
930
	        m.login(anotheruser, anotherpassword);
931
	        //succeeds to read this document
932
	        readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS, false);
933
	        //fails to update this document
934
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
935
            //fails to update access part
936
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
937
                    null, null, null,
938
                    null,  getAccessBlock(anotheruser, true,
939
                            true, false, false, false),
940
                    null, null, null, null);
941
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
942
            //fails to delete the document
943
            deleteDocid(newdocid + ".1", FAILURE, true);
944
            //logout
945
            m.logout();
946
            
947
            //====2 inserts a document with access  rules (denyFirst) - allow READ and WRITE rule for another user,
948
	        // deny  READ and WRITE rule for public.
949
	        accessRule1 = generateOneAccessRule(anotheruser, true,
950
                    true, true, false, false);
951
	        accessRule2 = generateOneAccessRule("public", false,
952
                    true, true, false, false);
953
	        accessRules = new Vector();
954
	        accessRules.add(accessRule1);
955
	        accessRules.add(accessRule2);
956
	        access = getAccessBlock(accessRules, DENYFIRST);
957
	        System.out.println("the access part is "+access);
958
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
959
                    null, null, null,
960
                    null, access ,
961
                    null, null, null, null);
962
	        // login
963
	        m.login(username, password);
964
	        updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
965
	        m.logout();
966
	        //login as another user
967
	        m.login(anotheruser, anotherpassword);
968
	        //succeeds to read this document
969
	        readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS, false);
970
	        //succeeds to update this document
971
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
972
            //fails to update access part
973
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
974
                    null, null, null,
975
                    null,  getAccessBlock(anotheruser, true,
976
                            true, false, false, false),
977
                    null, null, null, null);
978
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
979
            //fails to delete the document
980
            deleteDocid(newdocid + ".3", FAILURE, true);
981
            //logout
982
            m.logout();
983
            
984
             //====3 inserts a document with access  rules (denyFirst) - allow ALL rule for another user,
985
	        // deny  ALL rule for public.
986
	        accessRule1 = generateOneAccessRule(anotheruser, true,
987
                    true, true, true, true);
988
	        accessRule2 = generateOneAccessRule("public", false,
989
                    true, true, true, true);
990
	        accessRules = new Vector();
991
	        accessRules.add(accessRule1);
992
	        accessRules.add(accessRule2);
993
	        access = getAccessBlock(accessRules, DENYFIRST);
994
	        System.out.println("the access part is "+access);
995
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
996
                    null, null, null,
997
                    null, access ,
998
                    null, null, null, null);
999
	        // login
1000
	        m.login(username, password);
1001
	        updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
1002
	        m.logout();
1003
	        //login as another user
1004
	        m.login(anotheruser, anotherpassword);
1005
	        //succeeds to read this document
1006
	        readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
1007
	        //succeeds to update this document
1008
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1009
            //succeed to update access part
1010
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1011
                    null, null, null,
1012
                    null,  getAccessBlock(anotheruser, true, true, true, true, true),
1013
                    null, null, null, null);
1014
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
1015
            //succeeds to delete the document
1016
            deleteDocid(newdocid + ".6", SUCCESS, false);
1017
            //logout
1018
            m.logout();
1019
            
1020
            
1021
            
1022
            
1023
            newdocid = generateDocid();
1024
             //  ====4 The user updates this documents with access rules (denyFirst) - allow READ and WRITE 
1025
            //rule for another user, deny  WRITE rule for public. 
1026
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1027
                    true, true, false, false);
1028
	        accessRule2 = generateOneAccessRule("public", false, false, true, false, false);
1029
	        accessRules = new Vector();
1030
	        accessRules.add(accessRule1);
1031
	        accessRules.add(accessRule2);
1032
	        access = getAccessBlock(accessRules, DENYFIRST);
1033
	        System.out.println("the access part is "+access);
1034
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1035
                    null, null, null,
1036
                    null, access ,
1037
                    null, null, null, null);
1038
	        // login
1039
	        m.login(username, password);
1040
	        insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1041
	        m.logout();
1042
	        //login as another user
1043
	        m.login(anotheruser, anotherpassword);
1044
	        //succeeds to read this document
1045
	        readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS, false);
1046
	        //succeeds to update this document
1047
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1048
            //fails to update access part
1049
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1050
                    null, null, null,
1051
                    null,  getAccessBlock(anotheruser, true,
1052
                            true, false, false, false),
1053
                    null, null, null, null);
1054
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
1055
            //fails to delete the document
1056
            deleteDocid(newdocid + ".2", FAILURE, true);
1057
            //logout
1058
            m.logout();
1059
	        
1060
            
1061
            //  ====5. The user updates this documents with access rules (allowFirst) - allow READ and WRITE
1062
            //rule for another user, deny READ rule for public. 
1063
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1064
                    true, true, false, false);
1065
	        accessRule2 = generateOneAccessRule("public", false, true, false, false, false);
1066
	        accessRules = new Vector();
1067
	        accessRules.add(accessRule1);
1068
	        accessRules.add(accessRule2);
1069
	        access = getAccessBlock(accessRules, DENYFIRST);
1070
	        System.out.println("the access part is "+access);
1071
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1072
                    null, null, null,
1073
                    null, access ,
1074
                    null, null, null, null);
1075
	        // login
1076
	        m.login(username, password);
1077
	        updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
1078
	        m.logout();
1079
	        //login as another user
1080
	        m.login(anotheruser, anotherpassword);
1081
	        //succeeds to read this document
1082
	        readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
1083
	        //succeed to update this document
1084
            updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
1085
            //fails to update access part
1086
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1087
                    null, null, null,
1088
                    null,  getAccessBlock(anotheruser, true,
1089
                            true, false, false, false),
1090
                    null, null, null, null);
1091
            updateDocid(newdocid + ".12", testdocument, FAILURE, true);
1092
            //fails to delete the document
1093
            deleteDocid(newdocid + ".12", FAILURE, true);
1094
            //logout
1095
            m.logout();
1096
            
1097
            
1098
            
1099
             //   ====6 inserts a document with access  rules (denyFirst) - allow READ rule for another user,
1100
	        // deny  READ rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and another user is 
1101
            // in this group).
1102
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1103
                    true, false, false, false);
1104
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
1105
                   true, false, false, false);
1106
	        accessRules = new Vector();
1107
	        accessRules.add(accessRule1);
1108
	        accessRules.add(accessRule2);
1109
	        access = getAccessBlock(accessRules, DENYFIRST);
1110
	        System.out.println("the access part is "+access);
1111
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1112
                    null, null, null,
1113
                    null, access ,
1114
                    null, null, null, null);
1115
	        // login
1116
	        m.login(username, password);
1117
	        updateDocid(newdocid + ".13", testdocument, SUCCESS, false);
1118
	        m.logout();
1119
	        //login as another user
1120
	        m.login(anotheruser, anotherpassword);
1121
	        //succeeds to read this document
1122
	        readDocidWhichEqualsDoc(newdocid + ".13", testdocument, SUCCESS, false);
1123
	        //fails to update this document
1124
            updateDocid(newdocid + ".14", testdocument, FAILURE, true);
1125
            //fails to update access part
1126
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1127
                    null, null, null,
1128
                    null,  getAccessBlock(anotheruser, true,
1129
                            true, false, false, false),
1130
                    null, null, null, null);
1131
            updateDocid(newdocid + ".14", testdocument, FAILURE, true);
1132
            //fails to delete the document
1133
            deleteDocid(newdocid + ".13", FAILURE, true);
1134
            //logout
1135
            m.logout();
1136
            
1137
            //====7 inserts a document with access  rules (allowFirst) - allow READ and WRITE rule for another 
1138
            //user, deny  READ and WRITE rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and
1139
            // the other user is in this group)
1140
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1141
                    true, true, false, false);
1142
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
1143
                    true, true, false, false);
1144
	        accessRules = new Vector();
1145
	        accessRules.add(accessRule1);
1146
	        accessRules.add(accessRule2);
1147
	        access = getAccessBlock(accessRules, DENYFIRST);
1148
	        System.out.println("the access part is "+access);
1149
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1150
                    null, null, null,
1151
                    null, access ,
1152
                    null, null, null, null);
1153
	        // login
1154
	        m.login(username, password);
1155
	        updateDocid(newdocid + ".14", testdocument, SUCCESS, false);
1156
	        m.logout();
1157
	        //login as another user
1158
	        m.login(anotheruser, anotherpassword);
1159
	        //succeeds to read this document
1160
	        readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
1161
	        //succeeds to update this document
1162
            updateDocid(newdocid + ".15", testdocument, SUCCESS, false);
1163
            //fails to update access part
1164
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1165
                    null, null, null,
1166
                    null,  getAccessBlock(anotheruser, true,
1167
                            true, false, false, false),
1168
                    null, null, null, null);
1169
            updateDocid(newdocid + ".16", testdocument, FAILURE, true);
1170
            //fails to delete the document
1171
            deleteDocid(newdocid + ".15", FAILURE, true);
1172
            //logout
1173
            m.logout();
1174
            
1175
             //====8 inserts a document with access  rules (denyFirst) - allow ALL rule for another user,
1176
	        // deny  ALL rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and the other user
1177
            // is in this group)
1178
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1179
                    true, true, true, true);
1180
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
1181
                    true, true, true, true);
1182
	        accessRules = new Vector();
1183
	        accessRules.add(accessRule1);
1184
	        accessRules.add(accessRule2);
1185
	        access = getAccessBlock(accessRules, DENYFIRST);
1186
	        System.out.println("the access part is "+access);
1187
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1188
                    null, null, null,
1189
                    null, access ,
1190
                    null, null, null, null);
1191
	        // login
1192
	        m.login(username, password);
1193
	        updateDocid(newdocid + ".16", testdocument, SUCCESS, false);
1194
	        m.logout();
1195
	        //login as another user
1196
	        m.login(anotheruser, anotherpassword);
1197
	        //succeeds to read this document
1198
	        readDocidWhichEqualsDoc(newdocid + ".16", testdocument, SUCCESS, false);
1199
	        //succeeds to update this document
1200
            updateDocid(newdocid + ".17", testdocument, SUCCESS, false);
1201
            //succeeds to update access part
1202
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1203
                    null, null, null,
1204
                    null,  getAccessBlock(anotheruser, true,
1205
                            true, true, true, true),
1206
                    null, null, null, null);
1207
            updateDocid(newdocid + ".18", testdocument, SUCCESS, false);
1208
            //succeeds to delete the document
1209
            deleteDocid(newdocid + ".18", SUCCESS, false);
1210
            //logout
1211
            m.logout();
1212
            
1213
            
1214
            
1215
            
1216
            newdocid = generateDocid();
1217
             //  ====9 The user updates this documents with access rules (denyFirst) - allow READ and WRITE 
1218
            //rule for another user, deny  WRITE rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org
1219
            // and another user is in this group). 
1220
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1221
                    true, true, false, false);
1222
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false, false, true, false, false);
1223
	        accessRules = new Vector();
1224
	        accessRules.add(accessRule1);
1225
	        accessRules.add(accessRule2);
1226
	        access = getAccessBlock(accessRules, DENYFIRST);
1227
	        System.out.println("the access part is "+access);
1228
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1229
                    null, null, null,
1230
                    null, access ,
1231
                    null, null, null, null);
1232
	        // login
1233
	        m.login(username, password);
1234
	        insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1235
	        m.logout();
1236
	        //login as another user
1237
	        m.login(anotheruser, anotherpassword);
1238
	        //succeed to read this document
1239
	        readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS, false);
1240
	        //succeeds to update this document
1241
            updateDocid(newdocid + ".20", testdocument, SUCCESS, false);
1242
            //fails to update access part
1243
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1244
                    null, null, null,
1245
                    null,  getAccessBlock(anotheruser, true,
1246
                            true, false, false, false),
1247
                    null, null, null, null);
1248
            updateDocid(newdocid + ".21", testdocument, FAILURE, true);
1249
            //fails to delete the document
1250
            deleteDocid(newdocid + ".20", FAILURE, true);
1251
            //logout
1252
            m.logout();
1253
	        
1254
            
1255
            //  ====10. The user updates this documents with access rules (denyFirst) - allow READ and WRITE
1256
            //rule for another user, deny READ rule for a group(cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and
1257
            //another user is in this group). 
1258
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1259
                    true, true, false, false);
1260
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
1261
                    true, false, false, false);
1262
	        accessRules = new Vector();
1263
	        accessRules.add(accessRule1);
1264
	        accessRules.add(accessRule2);
1265
	        access = getAccessBlock(accessRules, DENYFIRST);
1266
	        System.out.println("the access part is "+access);
1267
	        testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1268
                    null, null, null,
1269
                    null, access ,
1270
                    null, null, null, null);
1271
	        // login
1272
	        m.login(username, password);
1273
	        updateDocid(newdocid + ".21", testdocument, SUCCESS, false);
1274
	        m.logout();
1275
	        //login as another user
1276
	        m.login(anotheruser, anotherpassword);
1277
	        //succeeds to read this document
1278
	        readDocidWhichEqualsDoc(newdocid + ".21", testdocument, SUCCESS, false);
1279
	        //succeeds to update this document
1280
            updateDocid(newdocid + ".22", testdocument, SUCCESS, false);
1281
            //fails to update access part
1282
            testdocument = getTestEmlDoc("Testing user can not read, write and delete a document",
1283
                    null, null, null,
1284
                    null,  getAccessBlock(anotheruser, true,
1285
                            true, false, false, false),
1286
                    null, null, null, null);
1287
            updateDocid(newdocid + ".23", testdocument, FAILURE, true);
1288
            //fails to delete the document
1289
            deleteDocid(newdocid + ".22", FAILURE, true);
1290
            //logout
1291
            m.logout();
1292
	    }
1293
	    catch (MetacatAuthException mae) {
1294
	        fail("Authorization failed:\n" + mae.getMessage());
1295
	    }
1296
	    catch (MetacatInaccessibleException mie) {
1297
	        fail("Metacat Inaccessible:\n" + mie.getMessage());
1298
	    }
1299
	    catch (Exception e) {
1300
	        fail("General exception:\n" + e.getMessage());
1301
	    }
1302
    }
1303

    
1304
    /** *********
1305
     * Test the case when no access is specified and owner is logged in
1306
     * No online or inline data is involved
1307
     * -> an user inserts a document and is able to read it, update it,
1308
     *    set permissions on it and delete it
1309
     * -> another user is not able to do anything with the document
1310
     *    when no access is specified for that user
1311
     * -> test what all the other user can do when read only, write only and
1312
     *    change permissions only permissions are specified
1313
     *
1314
     */
1315
    public void documentTest() {
1316
        try {
1317

    
1318
            newdocid = generateDocid();
1319

    
1320
            // login
1321
            m.login(username, password);
1322

    
1323
            // insert a 2.0.0 document
1324
            testdocument = testEml_200_Header +
1325
                "<dataset scope=\"document\"><title>submitting eml2000</title>" +
1326
            testEmlCreatorBlock + testEmlContactBlock + "</dataset></eml:eml>";
1327

    
1328
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1329

    
1330
            // read the document
1331
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1332

    
1333
            // update it with 2.0.1 document
1334
            testdocument = getTestEmlDoc("Updating eml200 with eml201",
1335
                                         null, null, null, null,
1336
                                         null, null, null, null, null);
1337
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1338
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1339
            m.logout();
1340

    
1341

    
1342
            newdocid = generateDocid();
1343

    
1344
            // login
1345
            m.login(username, password);
1346

    
1347
            // insert a document
1348
            testdocument = getTestEmlDoc("Testing insert", null,
1349
                                         null, null,
1350
                                         null, null, null, null, null, null);
1351
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1352

    
1353
            // read the document
1354
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1355

    
1356
            // update the document
1357
            testdocument = getTestEmlDoc("Testing update", null, null, null,
1358
                                         null, null, null, null, null, null);
1359
            Thread.sleep(10000);
1360
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1361
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1362

    
1363
            /////////////////////////////
1364
            // check what the another user can do
1365
            m.logout();
1366
            m.login(anotheruser, anotherpassword);
1367

    
1368
            // check if the user mentioned is able to read/update/delete the document
1369
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, FAILURE, true);
1370
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
1371
            deleteDocid(newdocid + ".2", FAILURE, true);
1372

    
1373
            /////////////////////////////
1374
            // update the document access control - read only
1375
            m.logout();
1376
            m.login(username, password);
1377

    
1378
            testdocument = getTestEmlDoc("Testing update access block",
1379
                                         null, null, null,
1380
                                         null, getAccessBlock(anotheruser, true,
1381
                                         true, false, false, false),
1382
                                         null, null, null, null);
1383
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1384
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
1385

    
1386
            // check if the user mentioned is able to read the document
1387
            m.logout();
1388
            m.login(anotheruser, anotherpassword);
1389
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
1390

    
1391
            // should not be able to update the document
1392
            testdocument = getTestEmlDoc("Testing update from another user",
1393
                                         null, null, null,
1394
                                         null, getAccessBlock(anotheruser, true,
1395
                                         true, false, false, false),
1396
                                         null, null, null, null);
1397
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
1398

    
1399
            // or the permissions
1400
            testdocument = getTestEmlDoc("Testing update access block",
1401
                                         null, null, null,
1402
                                         null, getAccessBlock(anotheruser, true,
1403
                                         false, false, false, true),
1404
                                         null, null, null, null);
1405
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
1406

    
1407
            // or delete the document
1408
            deleteDocid(newdocid + ".3", FAILURE, true);
1409

    
1410
            m.logout();
1411
            m.login(username, password);
1412

    
1413
            ///////////////////////////////////
1414
            // update the document access control - write only
1415
            testdocument = getTestEmlDoc("Testing update access block",
1416
                                         null, null, null,
1417
                                         null, getAccessBlock(anotheruser, true,
1418
                                         false, true, false, false),
1419
                                         null, null, null, null);
1420
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
1421
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
1422
            //System.out.println(testdocument);
1423
            // check if the user mentioned is able to read the document
1424
            m.logout();
1425
            m.login(anotheruser, anotherpassword);
1426
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, FAILURE, true);
1427

    
1428
            // should be able to update the document
1429

    
1430
            //System.out.println(testdocument);
1431
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1432

    
1433
            // but not the permissions
1434
            testdocument = getTestEmlDoc("Testing update access block",
1435
                                         null, null, null,
1436
                                         null, getAccessBlock(anotheruser, true,
1437
                                         false, true, false, true),
1438
                                         null, null, null, null);
1439
            updateDocid(newdocid + ".6", testdocument, FAILURE, true);
1440

    
1441
            // try to delete the document
1442
            deleteDocid(newdocid + ".5", FAILURE, true);
1443
            //deleteDocid(newdocid + ".4", FAILURE, true);
1444

    
1445
            m.logout();
1446
            m.login(username, password);
1447

    
1448
            /////////////////////////////////
1449
            // update the document access control - change permissions only
1450
            testdocument = getTestEmlDoc("Testing update access block",
1451
                                         null, null, null,
1452
                                         null, getAccessBlock(anotheruser, true,
1453
                false, false, true, false),
1454
                                         null, null, null, null);
1455
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
1456
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, false);
1457

    
1458
            // check if the user mentioned is able to read the document
1459
            m.logout();
1460
            m.login(anotheruser, anotherpassword);
1461
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, FAILURE, true);
1462

    
1463
            // should not be able to update the document
1464
            testdocument = getTestEmlDoc("Testing update from another user",
1465
                                         null, null, null,
1466
                                         null, getAccessBlock(anotheruser, true,
1467
                false, false, true, false),
1468
                                         null, null, null, null);
1469
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
1470

    
1471
            // but can chg the permissions
1472
            testdocument = getTestEmlDoc("Testing update access block",
1473
                                         null, null, null,
1474
                                         null, getAccessBlock(anotheruser, true,
1475
                                         false, false, false, true),
1476
                                         null, null, null, null);
1477
            // ERRRRRRRROR
1478
            //updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
1479

    
1480
            // try to delete the document
1481
            //deleteDocid(newdocid + ".7", FAILURE, true);
1482
            deleteDocid(newdocid + ".6", FAILURE, true);
1483

    
1484
            m.logout();
1485
            m.login(username, password);
1486

    
1487

    
1488
            /////////////////////////////////
1489
            // update the document access control - read & write
1490
            testdocument = getTestEmlDoc("Testing update access block",
1491
                                         null, null, null,
1492
                                         null, getAccessBlock(anotheruser, true,
1493
                                         true, true, false, false),
1494
                                         null, null, null, null);
1495
            updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
1496
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1497

    
1498
            // check if the user mentioned is able to read the document
1499
            m.logout();
1500
            m.login(anotheruser, anotherpassword);
1501
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1502

    
1503
            // should be able to update the document
1504
            updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
1505

    
1506
            // but cant chg the permissions
1507
            testdocument = getTestEmlDoc("Testing update access block",
1508
                                         null, null, null,
1509
                                         null, getAccessBlock(anotheruser, true,
1510
                                         false, false, false, true),
1511
                                         null, null, null, null);
1512
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
1513

    
1514
            // try to delete the document
1515
            deleteDocid(newdocid + ".9", FAILURE, true);
1516

    
1517
            m.logout();
1518
            m.login(username, password);
1519

    
1520

    
1521
            /////////////////////////////////
1522
            // update the document access control - read & change permissions
1523
            testdocument = getTestEmlDoc("Testing update access block",
1524
                                         null, null, null,
1525
                                         null, getAccessBlock(anotheruser, true,
1526
                                         true, false, true, false),
1527
                                         null, null, null, null);
1528
            updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
1529
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
1530

    
1531
            // check if the user mentioned is able to read the document
1532
            m.logout();
1533
            m.login(anotheruser, anotherpassword);
1534
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
1535
            // should not be able to update the document
1536
            testdocument = getTestEmlDoc("Testing update from another user",
1537
                                         null, null, null,
1538
                                         null, getAccessBlock(anotheruser, true,
1539
                                         false, false, true, false),
1540
                                         null, null, null, null);
1541
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
1542

    
1543
            // but can chg the permissions
1544
            testdocument = getTestEmlDoc("Testing update access block",
1545
                                         null, null, null,
1546
                                         null, getAccessBlock(anotheruser, true,
1547
                                         false, false, false, true),
1548
                                         null, null, null, null);
1549
            //updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
1550

    
1551
            // try to delete the document
1552
            // deleteDocid(newdocid + ".11", FAILURE, true);
1553
            deleteDocid(newdocid + ".10", FAILURE, true);
1554

    
1555
            m.logout();
1556
            m.login(username, password);
1557

    
1558

    
1559

    
1560
            /////////////////////////////////
1561
            // update the document access control - read & change permissions
1562
            testdocument = getTestEmlDoc("Testing update access block",
1563
                                         null, null, null,
1564
                                         null, getAccessBlock(anotheruser, true,
1565
                                         true, false, true, false),
1566
                                         null, null, null, null);
1567
            updateDocid(newdocid + ".12", testdocument, SUCCESS, false);
1568
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument, SUCCESS, false);
1569

    
1570
            // check if the user mentioned is able to read the document
1571
            m.logout();
1572
            m.login(anotheruser, anotherpassword);
1573
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument,  SUCCESS, false);
1574

    
1575
            // should not be able to update the document
1576
            testdocument = getTestEmlDoc("Testing update from another user",
1577
                                         null, null, null,
1578
                                         null, getAccessBlock(anotheruser, true,
1579
                                         false, false, true, false),
1580
                                         null, null, null, null);
1581
            updateDocid(newdocid + ".13", testdocument, FAILURE, true);
1582
            // but can chg the permissions
1583
            testdocument = getTestEmlDoc("Testing update from another user",
1584
                                         null, null, null,
1585
                                         null, getAccessBlock(anotheruser, true,
1586
                                         false, false, false, true),
1587
                                         null, null, null, null);
1588
            // ERRRRRRRRRRRRRRRR
1589
            // updateDocid(newdocid + ".13", testdocument, SUCCESS, false);
1590

    
1591
            // try to delete the document
1592
            //deleteDocid(newdocid + ".13", FAILURE, true);
1593
            deleteDocid(newdocid + ".12", FAILURE, true);
1594

    
1595
            m.logout();
1596
            m.login(username, password);
1597

    
1598

    
1599
            /////////////////////////////////
1600
            // update the document access control - R, W, CP
1601
            testdocument = getTestEmlDoc("Testing update access block",
1602
                                         null, null, null,
1603
                                         null, getAccessBlock(anotheruser, true,
1604
                                         true, true, true, false),
1605
                                         null, null, null, null);
1606
            updateDocid(newdocid + ".14", testdocument, SUCCESS, false);
1607
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
1608

    
1609
            // check if the user mentioned is able to read the document
1610
            m.logout();
1611
            m.login(anotheruser, anotherpassword);
1612
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
1613

    
1614
            // should not be able to update the document
1615
            testdocument = getTestEmlDoc("Testing update from another user",
1616
                                         null, null, null,
1617
                                         null, getAccessBlock(anotheruser, true,
1618
                false, false, false, true),
1619
                                         null, null, null, null);
1620
            updateDocid(newdocid + ".15", testdocument, SUCCESS, false);
1621
            readDocidWhichEqualsDoc(newdocid + ".15", testdocument, SUCCESS, false);
1622

    
1623
            // but can chg the permissions
1624
            testdocument = getTestEmlDoc("Testing update from another user",
1625
                                         null, null, null,
1626
                                         null, getAccessBlock(anotheruser, true,
1627
                                         true, false, false, false),
1628
                                         null, null, null, null);
1629
            updateDocid(newdocid + ".16", testdocument, SUCCESS, false);
1630
            readDocidWhichEqualsDoc(newdocid + ".16", testdocument, SUCCESS, false);
1631

    
1632
            // try to delete the document
1633
            deleteDocid(newdocid + ".16", FAILURE, true);
1634

    
1635
            m.logout();
1636
            m.login(username, password);
1637

    
1638
            /////////////////////////////////
1639
            // update the document access control - all
1640
            testdocument = getTestEmlDoc("Testing update access block",
1641
                                         null, null, null,
1642
                                         null, getAccessBlock(anotheruser, true,
1643
                                         false, false, false, true),
1644
                                         null, null, null, null);
1645
            updateDocid(newdocid + ".17", testdocument, SUCCESS, false);
1646
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
1647

    
1648
            // check if the user mentioned is able to read the document
1649
            m.logout();
1650
            m.login(anotheruser, anotherpassword);
1651
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
1652

    
1653
            // should not be able to update the document
1654
            testdocument = getTestEmlDoc("Testing update from another user",
1655
                                         null, null, null,
1656
                                         null, getAccessBlock(anotheruser, true,
1657
                                         false, false, false, true),
1658
                                         null, null, null, null);
1659
            updateDocid(newdocid + ".18", testdocument, SUCCESS, false);
1660
            readDocidWhichEqualsDoc(newdocid + ".18", testdocument, SUCCESS, false);
1661

    
1662
            // but can chg the permissions
1663
            testdocument = getTestEmlDoc("Testing update from another user",
1664
                                         null, null, null,
1665
                                         null, getAccessBlock(anotheruser, true,
1666
                                         true, false, false, false),
1667
                                         null, null, null, null);
1668
            updateDocid(newdocid + ".19", testdocument, SUCCESS, false);
1669
            readDocidWhichEqualsDoc(newdocid + ".19", testdocument, SUCCESS, false);
1670

    
1671
            // try to delete the document
1672
            deleteDocid(newdocid + ".19", FAILURE, true);
1673

    
1674
            m.logout();
1675

    
1676
            // delete the document
1677
            m.login(username, password);
1678
            deleteDocid(newdocid + ".19", SUCCESS, false);
1679
            m.logout();
1680
        }
1681
        catch (MetacatAuthException mae) {
1682
            fail("Authorization failed:\n" + mae.getMessage());
1683
        }
1684
        catch (MetacatInaccessibleException mie) {
1685
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1686
        }
1687
        catch (Exception e) {
1688
            fail("General exception:\n" + e.getMessage());
1689
        }
1690

    
1691
    }
1692

    
1693

    
1694
   /** *********
1695
     * Test the case when no access is specified and public is logged in
1696
     * Cases being checked:
1697
     * 1. public tries to read the document - Failure.
1698
     * 2. public tries to update the document - Failure.
1699
     * 3. public tries to update the inline data - Failure.
1700
     * 4. public tries to update the online data file - Failure.
1701
     * 5. public tries to update the access rules for the document - Failure.
1702
     * 6. public tries to update the access rules for the inline data - Failure.
1703
     * 7. public tries to update the access rules for the online data - Failure.
1704
     * 8. public tries to delete the document - Failure.
1705
     */
1706
    public void AccessControlTestForPublic() {
1707
        try {
1708

    
1709
            String accessBlock = getAccessBlock(anotheruser, true,
1710
                                         true, false, false, false);
1711
            newdocid = generateDocid();
1712

    
1713
            // login
1714
            m.login(username, password);
1715

    
1716
            onlineDocid = generateDocid();
1717
            uploadDocid(onlineDocid + ".1",onlinetestdatafile1, SUCCESS, false);
1718

    
1719
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1720
                                     null,"ecogrid://knb/" + onlineDocid + ".1",
1721
                                     null, null, null, null, null, null);
1722

    
1723

    
1724
            // insert a document - get the docid
1725
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1726
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS,
1727
                                    false);
1728

    
1729
            // logoutand login as other user
1730
            m.logout();
1731

    
1732
            // read the document
1733
            readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE, true);
1734

    
1735
            // update the document
1736
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1737

    
1738
            // update the inline data
1739
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock2,
1740
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1741
                         null, null, null, null, null, null);
1742
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1743

    
1744
            // update the online data
1745
            uploadDocid(onlineDocid + ".2", onlinetestdatafile1, FAILURE, false);
1746

    
1747
            // update the document access control
1748
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1749
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1750
                         null, accessBlock, null, null, null, null);
1751
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1752

    
1753
            // update the document access control for inline data
1754
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1755
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1756
                         null, null, accessBlock, null, null, null);
1757
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1758

    
1759
            // update the document access control for online data
1760
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1761
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1762
                         null, null, null, null, accessBlock, null);
1763
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1764

    
1765
            // delete the document
1766
            deleteDocid(newdocid + ".2", FAILURE, true);
1767
            m.logout();
1768
        }
1769
        catch (MetacatAuthException mae) {
1770
            fail("Authorization failed:\n" + mae.getMessage());
1771
        }
1772
        catch (MetacatInaccessibleException mie) {
1773
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1774
        }
1775
        catch (MetacatException me) {
1776
            fail("Metacat Error:\n" + me.getMessage());
1777
        }
1778
        catch (Exception e) {
1779
            fail("General exception:\n" + e.getMessage());
1780
        }
1781

    
1782
    }
1783

    
1784
    /**
1785
     * Insert a document into metacat. The expected result is passed as result
1786
     */
1787

    
1788
    private String insertDocid(String docid, String docText, boolean result,
1789
                               boolean expectKarmaException) {
1790
        String response = null;
1791
        try {
1792
            response = m.insert(docid,
1793
                                new StringReader(testdocument), null);
1794
            if (result) {
1795
                assertTrue( (response.indexOf("<success>") != -1));
1796
                assertTrue(response.indexOf(docid) != -1);
1797
            }
1798
            else {
1799
                assertTrue( (response.indexOf("<success>") == -1));
1800
            }
1801
            System.err.println(response);
1802
        }
1803
        catch (MetacatInaccessibleException mie) {
1804
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1805
        }
1806
        catch (InsufficientKarmaException ike) {
1807
            if (!expectKarmaException) {
1808
                fail("Insufficient karma:\n" + ike.getMessage());
1809
            }
1810
        }
1811
        catch (MetacatException me) {
1812
            fail("Metacat Error:\n" + me.getMessage());
1813
        }
1814
        catch (Exception e) {
1815
            fail("General exception:\n" + e.getMessage());
1816
        }
1817
        return response;
1818
    }
1819

    
1820
    /**
1821
     * Insert a document into metacat. The expected result is passed as result
1822
     */
1823

    
1824
    private String uploadDocid(String docid, String filePath, boolean result,
1825
                               boolean expectedKarmaException) {
1826
        String response = null;
1827
        try {
1828
            response = m.upload(docid, new File(filePath));
1829
            if (result) {
1830
                assertTrue( (response.indexOf("<success>") != -1));
1831
                assertTrue(response.indexOf(docid) != -1);
1832
            }
1833
            else {
1834
                assertTrue( (response.indexOf("<success>") == -1));
1835
            }
1836
            System.err.println("respose from metacat: " + response);
1837
        }
1838
        catch (MetacatInaccessibleException mie) {
1839
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1840
        }
1841
        catch (InsufficientKarmaException ike) {
1842
            if (!expectedKarmaException) {
1843
                fail("Insufficient karma:\n" + ike.getMessage());
1844
            }
1845
        }
1846
        catch (MetacatException me) {
1847
            if (result) {
1848
                fail("Metacat Error:\n" + me.getMessage());
1849
            } else {
1850
                System.err.println("Metacat Error:\n" + me.getMessage());
1851
            }
1852
        }
1853
        catch (Exception e) {
1854
            fail("General exception:\n" + e.getMessage());
1855
        }
1856
        return response;
1857
    }
1858

    
1859
    /**
1860
     * Update a document in metacat. The expected result is passed as result
1861
     */
1862
    private String updateDocid(String docid, String docText, boolean result,
1863
                               boolean expectedKarmaFailure) {
1864
        String response = null;
1865
        try {
1866
            response = m.update(docid,
1867
                                new StringReader(testdocument), null);
1868

    
1869
            if (result) {
1870
                assertTrue( (response.indexOf("<success>") != -1));
1871
                assertTrue(response.indexOf(docid) != -1);
1872
            }
1873
            else {
1874
                assertTrue( (response.indexOf("<success>") == -1));
1875
            }
1876
            System.err.println(response);
1877
        }
1878
        catch (MetacatInaccessibleException mie) {
1879
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1880
        }
1881
        catch (InsufficientKarmaException ike) {
1882
            if (!expectedKarmaFailure) {
1883
                fail("Insufficient karma:\n" + ike.getMessage());
1884
            }
1885
        }
1886
        catch (MetacatException me) {
1887
            if (result) {
1888
                fail("Metacat Error:\n" + me.getMessage());
1889
            } else {
1890
                System.err.println("Metacat Error:\n" + me.getMessage());
1891
            }
1892
        }
1893
        catch (Exception e) {
1894
            fail("General exception:\n" + e.getMessage());
1895
        }
1896

    
1897
        return response;
1898
    }
1899

    
1900
    /**
1901
     * Delete a document into metacat. The expected result is passed as result
1902
     */
1903
    private void deleteDocid(String docid, boolean result,
1904
                             boolean expextedKarmaFailure) {
1905
        try {
1906
        	Thread.sleep(5000);
1907
            String response = m.delete(docid);
1908
            if (result) {
1909
                assertTrue(response.indexOf("<success>") != -1);
1910
            }
1911
            else {
1912
                assertTrue(response.indexOf("<success>") == -1);
1913
            }
1914
            System.err.println(response);
1915
        }
1916
        catch (MetacatInaccessibleException mie) {
1917
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1918
        }
1919
        catch (InsufficientKarmaException ike) {
1920
            if(!expextedKarmaFailure){
1921
                fail("Insufficient karma:\n" + ike.getMessage());
1922
            }
1923
        }
1924
        catch (MetacatException me) {
1925
            if (result) {
1926
                fail("Metacat Error:\n" + me.getMessage());
1927
            } else {
1928
                System.err.println("Metacat Error:\n" + me.getMessage());
1929
            }
1930
        }
1931
        catch (Exception e) {
1932
            fail("General exception:\n" + e.getMessage());
1933
        }
1934
    }
1935

    
1936
    /**
1937
     * Read a document from metacat. The expected result is passed as result
1938
     */
1939
    private void readDocid(String docid, boolean result,
1940
                           boolean expextedKarmaFailure) {
1941
        try {
1942
            Reader r = m.read(docid);
1943
            String response = IOUtil.getAsString(r, true);
1944

    
1945
            if (!result) {
1946
                assertTrue(response.indexOf("<success>") == -1);
1947
            }
1948
            // System.err.println(response);
1949
        }
1950
        catch (MetacatInaccessibleException mie) {
1951
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1952
        }
1953
        catch (InsufficientKarmaException ike) {
1954
            if (!expextedKarmaFailure) {
1955
                fail("Insufficient karma:\n" + ike.getMessage());
1956
            }
1957
        }
1958
        catch (MetacatException me) {
1959
            fail("Metacat Error:\n" + me.getMessage());
1960
        }
1961
        catch (Exception e) {
1962
            fail("General exception:\n" + e.getMessage());
1963
        }
1964
    }
1965

    
1966
    /**
1967
     * Read a document from metacat and check if it is equal to a given string.
1968
     * The expected result is passed as result
1969
     */
1970

    
1971
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1972
                                         boolean result,
1973
                                         boolean expextedKarmaFailure) {
1974
        try {
1975
            Reader r = m.read(docid);
1976
            String doc = IOUtil.getAsString(r, true);
1977
            if (result) {
1978

    
1979
                if (!testDoc.equals(doc)) {
1980
                    System.out.println("doc       :" + doc);
1981
                    System.out.println("testDoc:" + testDoc);
1982
                }
1983

    
1984
                assertTrue(testDoc.equals(doc));
1985
            }
1986
            else {
1987
                assertTrue(doc.indexOf("<error>") != -1);
1988
            }
1989
        }
1990
        catch (MetacatInaccessibleException mie) {
1991
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1992
        }
1993
        catch (InsufficientKarmaException ike) {
1994
            if (!expextedKarmaFailure) {
1995
                fail("Insufficient karma:\n" + ike.getMessage());
1996
            }
1997
        }
1998
        catch (MetacatException me) {
1999
            fail("Metacat Error:\n" + me.getMessage());
2000
        }
2001
        catch (Exception e) {
2002
            fail("General exception:\n" + e.getMessage());
2003
        }
2004

    
2005
    }
2006

    
2007
    /**
2008
     * Create a hopefully unique docid for testing insert and update. Does
2009
     * not include the 'revision' part of the id.
2010
     *
2011
     * @return a String docid based on the current date and time
2012
     */
2013
    private String generateDocid() {
2014
        StringBuffer docid = new StringBuffer(prefix);
2015
        docid.append(".");
2016

    
2017
        // Create a calendar to get the date formatted properly
2018
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
2019
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
2020
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
2021
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
2022
                       2 * 60 * 60 * 1000);
2023
        Calendar calendar = new GregorianCalendar(pdt);
2024
        Date trialTime = new Date();
2025
        calendar.setTime(trialTime);
2026
        docid.append(calendar.get(Calendar.YEAR));
2027
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
2028
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
2029
        docid.append(calendar.get(Calendar.MINUTE));
2030
        docid.append(calendar.get(Calendar.SECOND));
2031

    
2032
        return docid.toString();
2033
    }
2034
}
(1-1/18)