Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: daigle $'
8
 *     '$Date: 2008-12-17 14:18:21 -0800 (Wed, 17 Dec 2008) $'
9
 * '$Revision: 4680 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacattest;
27

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

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

    
50
/**
51
 * A JUnit test for testing Access Control for online data in Metacat
52
 */
53
public class OnlineDataAccessTest
54
    extends MCTestCase {
55

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

    
74
    private String prefix = "test";
75
    private String newdocid = null;
76
    private String onlineDocid = null;
77
    private String testdocument = "";
78
    private String onlinetestdatafile1 = "test/onlineDataFile1";
79
    private String onlinetestdatafile2 = "test/onlineDataFile2";
80

    
81
    private Metacat m;
82

    
83
    private boolean SUCCESS = true;
84
    private boolean FAILURE = false;
85

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

    
91
    private String testEml_200_Header =
92
        "<?xml version=\"1.0\"?><eml:eml" +
93
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\"" +
94
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
95
        " packageId=\"eml.1.1\" system=\"knb\"" +
96
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\"" +
97
        " scope=\"system\">";
98
    
99
    private String testEml_201_Header =
100
        "<?xml version=\"1.0\"?><eml:eml" +
101
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
102
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
103
        " packageId=\"eml.1.1\" system=\"knb\"" +
104
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
105
        " scope=\"system\">";
106
    
107
    private String testEml_210_Header =
108
        "<?xml version=\"1.0\"?><eml:eml" +
109
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.0\"" +
110
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
111
        " packageId=\"eml.1.1\" system=\"knb\"" +
112
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.0 eml.xsd\"" +
113
        " scope=\"system\">";
114

    
115
    private String testEmlCreatorBlock =
116
        "<creator scope=\"document\">                                       " +
117
        " <individualName>                                                  " +
118
        "    <surName>Smith</surName>                                       " +
119
        " </individualName>                                                 " +
120
        "</creator>                                                         ";
121

    
122
    private String testEmlContactBlock =
123
        "<contact scope=\"document\">                                       " +
124
        " <individualName>                                                  " +
125
        "    <surName>Jackson</surName>                                     " +
126
        " </individualName>                                                 " +
127
        "</contact>                                                         ";
128

    
129
    private String testEmlInlineBlock1 =
130
        "<inline>                                                           " +
131
        "  <admin>                                                          " +
132
        "    <contact>                                                      " +
133
        "      <name>Operator</name>                                        " +
134
        "      <institution>PSI</institution>                               " +
135
        "    </contact>                                                     " +
136
        "  </admin>                                                         " +
137
        "</inline>                                                          ";
138

    
139
    private String testEmlInlineBlock2 =
140
        "<inline>                                                           " +
141
        "  <instrument>                                                     " +
142
        "    <instName>LCQ</instName>                                       " +
143
        "    <source type=\"ESI\"></source>                                 " +
144
        "    <detector type=\"EM\"></detector>                              " +
145
        "  </instrument>                                                    " +
146
        "</inline>                                                          ";
147

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

    
158
        if (grantAccess) {
159
            accessBlock += "<allow>";
160
        }
161
        else {
162
            accessBlock += "<deny>";
163
        }
164

    
165
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
166

    
167
        if (all) {
168
            accessBlock += "<permission>all</permission>";
169
        }
170
        else {
171
            if (read) {
172
                accessBlock += "<permission>read</permission>";
173
            }
174
            if (write) {
175
                accessBlock += "<permission>write</permission>";
176
            }
177
            if (changePermission) {
178
                accessBlock += "<permission>changePermission</permission>";
179
            }
180
        }
181

    
182
        if (grantAccess) {
183
            accessBlock += "</allow>";
184
        }
185
        else {
186
            accessBlock += "</deny>";
187
        }
188
        accessBlock += "</access>";
189

    
190
        return accessBlock;
191

    
192
    }
193

    
194
    /**
195
     * This function returns a valid eml document with no access rules
196
     * This function is for eml-2.0.1 only. For other eml versions,
197
     * this function might have to modified
198
     */
199
    private String get201TestEmlDoc(String title, String inlineData1,
200
                                 String inlineData2, String onlineUrl1,
201
                                 String onlineUrl2, String docAccessBlock,
202
                                 String inlineAccessBlock1,
203
                                 String inlineAccessBlock2,
204
                                 String onlineAccessBlock1,
205
                                 String onlineAccessBlock2) {
206

    
207
        String testDocument = "";
208
        testDocument = testDocument + testEml_201_Header +
209
            "<dataset scope=\"document\"><title>" + title + "</title>" +
210
            testEmlCreatorBlock;
211

    
212
        if (inlineData1 != null) {
213
            testDocument = testDocument
214
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
215
                + inlineData1 + "</distribution>";
216
        }
217
        if (inlineData2 != null) {
218
            testDocument = testDocument
219
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
220
                + inlineData2 + "</distribution>";
221
        }
222
        if (onlineUrl1 != null) {
223
            testDocument = testDocument
224
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
225
                + "<online><url function=\"download\">"
226
                + onlineUrl1 + "</url></online></distribution>";
227
        }
228
        if (onlineUrl2 != null) {
229
            testDocument = testDocument +
230
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
231
                + "<online><url function=\"download\">"
232
                + onlineUrl2 + "</url></online></distribution>";
233
        }
234
        testDocument += testEmlContactBlock;
235

    
236
        if (docAccessBlock != null) {
237
            testDocument += docAccessBlock;
238
        }
239

    
240
        testDocument += "</dataset>";
241

    
242
        if (inlineAccessBlock1 != null) {
243
            testDocument += "<additionalMetadata>";
244
            testDocument += "<describes>inlineEntity1</describes>";
245
            testDocument += inlineAccessBlock1;
246
            testDocument += "</additionalMetadata>";
247
        }
248

    
249
        if (inlineAccessBlock2 != null) {
250
            testDocument += "<additionalMetadata>";
251
            testDocument += "<describes>inlineEntity2</describes>";
252
            testDocument += inlineAccessBlock2;
253
            testDocument += "</additionalMetadata>";
254
        }
255

    
256
        if (onlineAccessBlock1 != null) {
257
            testDocument += "<additionalMetadata>";
258
            testDocument += "<describes>onlineEntity1</describes>";
259
            testDocument += onlineAccessBlock1;
260
            testDocument += "</additionalMetadata>";
261
        }
262

    
263
        if (onlineAccessBlock2 != null) {
264
            testDocument += "<additionalMetadata>";
265
            testDocument += "<describes>onlineEntity2</describes>";
266
            testDocument += onlineAccessBlock2;
267
            testDocument += "</additionalMetadata>";
268
        }
269

    
270
        testDocument += "</eml:eml>";
271

    
272
		debug("Returning following document: " + testDocument);
273
        return testDocument;
274
    }
275
    
276
    /**
277
	 * This function returns a valid eml document
278
	 */
279
    private String get210TestEmlDoc(String title, String inlineData1,
280
                                 String inlineData2, String onlineUrl1,
281
                                 String onlineUrl2, String docAccessBlock,
282
                                 String inlineAccessBlock1,
283
                                 String inlineAccessBlock2,
284
                                 String onlineAccessBlock1,
285
                                 String onlineAccessBlock2) {
286

    
287
    	String testDocument = testEml_210_Header;
288
		
289
		// the document level access block sits at the same level and 
290
		// before the dataset element.
291
		if (docAccessBlock != null) {
292
			testDocument += docAccessBlock;
293
		}
294
		
295
        testDocument += "<dataset scope=\"document\"><title>" + title + "</title>"
296
				+ testEmlCreatorBlock  + testEmlContactBlock 
297
				+ "<dataTable>" 
298
				+ "  <entityName>Test Data</entityName>"
299
				+ "  <physical>" 
300
				+ "    <objectName>2.1.0 test physical</objectName>"
301
				+ "    <size unit=\"bytes\">1</size>"
302
				+ "    <characterEncoding>ASCII</characterEncoding>"
303
				+ "    <dataFormat>"
304
				+ "      <textFormat>"
305
				+ "        <numHeaderLines>1</numHeaderLines>"
306
				+ "        <attributeOrientation>column</attributeOrientation>"
307
				+ "        <simpleDelimited>"
308
				+ "          <fieldDelimiter>,</fieldDelimiter>"
309
				+ "        </simpleDelimited>"
310
				+ "      </textFormat>"
311
				+ "    </dataFormat>";
312

    
313
		// The inline/online level access block sits at the same level as the 
314
        // inline element.
315
		if (inlineData1 != null) {
316
			testDocument += "<distribution><inline>" + inlineData1 + "</inline>";
317
			if (inlineAccessBlock1 != null) {
318
				testDocument += inlineAccessBlock1;
319
			}
320
			testDocument += "</distribution>";
321
		}
322
		if (inlineData2 != null) {
323
			testDocument += "<distribution><inline>" + inlineData2 + "</inline>";
324
			if (inlineAccessBlock2 != null) {
325
				testDocument += inlineAccessBlock2;
326
			}
327
			testDocument += "</distribution>";
328
		}
329
		if (onlineUrl1 != null) {
330
			testDocument = testDocument
331
					+ "<distribution><online><url function=\"download\">" + onlineUrl1
332
					+ "</url></online>";
333
			if (onlineAccessBlock1 != null) {
334
				testDocument += onlineAccessBlock1;
335
			}
336
			testDocument += "</distribution>";
337
		}
338
		if (onlineUrl2 != null) {
339
			testDocument = testDocument
340
					+ "<distribution><online><url function=\"download\">" + onlineUrl2
341
					+ "</url></online>";
342
			if (onlineAccessBlock2 != null) {
343
				testDocument += onlineAccessBlock2;
344
			}
345
			testDocument += "</distribution>";
346
		}
347
		testDocument += 
348
			  "  </physical>" 
349
			+ "  <attributeList>"
350
			+ "    <attribute>"
351
			+ "      <attributeName>rain</attributeName>"
352
			+ "      <attributeLabel>Surface Rainfall</attributeLabel>"
353
			+ "      <attributeDefinition>The amount of rainfall on the sampling unit."
354
			+ "      </attributeDefinition>"
355
			+ "      <storageType>float</storageType>"
356
			+ "      <storageType typeSystem=\"http://java.sun.com/docs/books/jls/second_edition/html\">double</storageType>"
357
			+ "      <measurementScale>"
358
			+ "        <interval>"
359
			+ "          <unit><standardUnit>millimeter</standardUnit></unit>"
360
			+ "          <precision>0.5</precision>"
361
			+ "          <numericDomain id=\"nd.1\">"
362
			+ "            <numberType>real</numberType>"
363
			+ "            <bounds>"
364
			+ "              <minimum exclusive=\"false\">0</minimum>"
365
			+ "            </bounds>"
366
			+ "          </numericDomain>"
367
			+ "        </interval>"
368
			+ "      </measurementScale>"
369
			+ "    </attribute>"
370
			+ "  </attributeList>"
371
			+ "</dataTable></dataset></eml:eml>";
372

    
373
		debug("Returning following document: " + testDocument);
374
		return testDocument;
375
	}
376

    
377
    /**
378
     * Constructor to build the test
379
     *
380
     * @param name the name of the test method
381
     */
382
    public OnlineDataAccessTest(String name) {
383
        super(name);
384
        newdocid = generateDocid();
385
    }
386

    
387
    /**
388
     * Establish a testing framework by initializing appropriate objects
389
     */
390
    public void setUp() {
391
        try {
392
            System.err.println("Test Metacat: " + metacatUrl);
393
            m = MetacatFactory.createMetacatConnection(metacatUrl);
394
        }
395
        catch (MetacatInaccessibleException mie) {
396
            System.err.println("Metacat is: " + metacatUrl);
397
            fail("Metacat connection failed." + mie.getMessage());
398
        }
399
    }
400

    
401
    /**
402
     * Release any objects after tests are complete
403
     */
404
    public void tearDown() {
405
    }
406

    
407
    /**
408
     * Create a suite of tests to be run together
409
     */
410
    public static Test suite() {
411
        TestSuite suite = new TestSuite();
412
        suite.addTest(new OnlineDataAccessTest("initialize"));
413

    
414
        suite.addTest(new OnlineDataAccessTest("onlineData201CasesTest_1"));
415
        suite.addTest(new OnlineDataAccessTest("onlineData210CasesTest_1"));
416
        suite.addTest(new OnlineDataAccessTest("onlineData201CasesTest_2"));
417
        suite.addTest(new OnlineDataAccessTest("onlineData210CasesTest_2"));
418
        suite.addTest(new OnlineDataAccessTest("onlineData201CasesTest_3"));
419
        suite.addTest(new OnlineDataAccessTest("onlineData210CasesTest_3"));
420
        suite.addTest(new OnlineDataAccessTest("onlineData201CasesTest_4"));
421
        suite.addTest(new OnlineDataAccessTest("onlineData210CasesTest_4"));
422
        suite.addTest(new OnlineDataAccessTest("onlineData201CasesTest_5"));
423
        suite.addTest(new OnlineDataAccessTest("onlineData201CasesTest_6"));
424
        suite.addTest(new OnlineDataAccessTest("onlineData210CasesTest_6"));
425

    
426
        return suite;
427
    }
428

    
429

    
430

    
431
    /**
432
     * Run an initial test that always passes to check that the test
433
     * harness is working.
434
     */
435
    public void initialize() {
436
        assertTrue(1 == 1);
437
    }
438

    
439

    
440
    /**
441
     * Checking the following cases on 2.0.1 version documents:
442
     * when only online data is uploaded by a user and
443
     * -> he tries to read it  - success
444
     * -> he tries to add same docid again  - failure
445
     * -> he tries to update it  - success
446
     * -> he tries to set permissions on it  - success
447
     * -> he tries to delete it  - success
448
     * -> he tries to read it after deleteing - failure
449
     */
450
    public void onlineData201CasesTest_1() {
451
        try {
452
			debug("\nRunning: onlineData201CasesTest_1");
453

    
454
            // upload online data
455
            onlineDocid = generateDocid();
456
            m.login(username, password);
457
            uploadDocid(onlineDocid + ".1",
458
                        onlinetestdatafile1, SUCCESS, false);
459

    
460
            // try to read the data
461
            readDocid(onlineDocid + ".1", SUCCESS, false);
462

    
463
            // try to upload another data with same id
464
            uploadDocid(onlineDocid + ".1",
465
                        onlinetestdatafile2, FAILURE, false);
466

    
467
            // try to upload another data with updated id
468
            uploadDocid(onlineDocid + ".2",
469
                        onlinetestdatafile2, SUCCESS, false);
470

    
471
            // try to set the permissions for the uploaded document
472
            // the docid given is for the online document
473
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
474
                                         null, null,
475
                                         "ecogrid://knb/" + onlineDocid + ".1",
476
                                         null, getAccessBlock(anotheruser, true,
477
                true, false, false, false),
478
                                         null, null, null, null);
479
            newdocid = generateDocid();
480
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
481
            m.logout();
482

    
483
            // check if the permissions were set properly
484
            m.login(anotheruser, anotherpassword);
485
            readDocid(onlineDocid + ".1", SUCCESS, false);
486
            readDocid(onlineDocid + ".2", SUCCESS, false);
487
            m.logout();
488

    
489
            m.login(username, password);
490

    
491
            // delete the document - able to delete .1
492
            // but not able to delete .2 as no rules 
493
	    // written to access table when a document 
494
	    // is 'uploaded'
495
            deleteDocid(onlineDocid + ".1", SUCCESS, false);
496
            deleteDocid(onlineDocid + ".2", FAILURE, true);
497

    
498
            // try to read the documents now
499
            readDocid(onlineDocid + ".1", FAILURE, true);
500
            readDocid(onlineDocid + ".2", FAILURE, true);
501

    
502
            m.logout();
503

    
504
        }
505
        catch (MetacatAuthException mae) {
506
            fail("Authorization failed:\n" + mae.getMessage());
507
        }
508
        catch (MetacatInaccessibleException mie) {
509
            fail("Metacat Inaccessible:\n" + mie.getMessage());
510
        }
511
        catch (Exception e) {
512
            fail("General exception:\n" + e.getMessage());
513
        }
514
    }
515
    
516
    /**
517
     * Checking the following cases on 2.1.0 version documents:
518
     * when only online data is uploaded by a user and
519
     * -> he tries to read it  - success
520
     * -> he tries to add same docid again  - failure
521
     * -> he tries to update it  - success
522
     * -> he tries to set permissions on it  - success
523
     * -> he tries to delete it  - success
524
     * -> he tries to read it after deleteing - failure
525
     */
526
    public void onlineData210CasesTest_1() {
527
        try {
528
			debug("\nRunning: onlineData210CasesTest_1");
529

    
530
            // upload online data
531
            onlineDocid = generateDocid();
532
            m.login(username, password);
533
            uploadDocid(onlineDocid + ".1",
534
                        onlinetestdatafile1, SUCCESS, false);
535

    
536
            // try to read the data
537
            readDocid(onlineDocid + ".1", SUCCESS, false);
538

    
539
            // try to upload another data with same id
540
            uploadDocid(onlineDocid + ".1",
541
                        onlinetestdatafile2, FAILURE, false);
542

    
543
            // try to upload another data with updated id
544
            uploadDocid(onlineDocid + ".2",
545
                        onlinetestdatafile2, SUCCESS, false);
546

    
547
            // try to set the permissions for the uploaded document
548
            // the docid given is for the online document
549
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
550
                                         null, null,
551
                                         "ecogrid://knb/" + onlineDocid + ".1",
552
                                         null, getAccessBlock(anotheruser, true,
553
                true, false, false, false),
554
                                         null, null, null, null);
555
            newdocid = generateDocid();
556
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
557
            m.logout();
558

    
559
            // check if the permissions were set properly
560
            m.login(anotheruser, anotherpassword);
561
            readDocid(onlineDocid + ".1", SUCCESS, false);
562
            readDocid(onlineDocid + ".2", SUCCESS, false);
563
            m.logout();
564

    
565
            m.login(username, password);
566

    
567
            // delete the document - able to delete .1
568
            // but not able to delete .2 as no rules 
569
	    // written to access table when a document 
570
	    // is 'uploaded'
571
            deleteDocid(onlineDocid + ".1", SUCCESS, false);
572
            deleteDocid(onlineDocid + ".2", FAILURE, true);
573

    
574
            // try to read the documents now
575
            readDocid(onlineDocid + ".1", FAILURE, true);
576
            readDocid(onlineDocid + ".2", FAILURE, true);
577

    
578
            m.logout();
579

    
580
        }
581
        catch (MetacatAuthException mae) {
582
            fail("Authorization failed:\n" + mae.getMessage());
583
        }
584
        catch (MetacatInaccessibleException mie) {
585
            fail("Metacat Inaccessible:\n" + mie.getMessage());
586
        }
587
        catch (Exception e) {
588
            fail("General exception:\n" + e.getMessage());
589
        }
590
    }
591

    
592

    
593
    /**
594
     * Checking the following cases on 2.0.1 version documents:
595
     * when only online data is uploaded by a user and another user
596
     * -> tries to read it  - failure
597
     * -> tries to add same docid again  - failure
598
     * -> tries to update it  - failure
599
     * -> tries to set permissions on it  - failure
600
     * -> tries to delete it  - failure
601
     */
602
    public void onlineData201CasesTest_2() {
603
        try {
604
			debug("\nRunning: onlineData201CasesTest_2");
605

    
606
            // upload an online document
607
            onlineDocid = generateDocid();
608
            m.login(username, password);
609
            uploadDocid(onlineDocid + ".1",
610
                        onlinetestdatafile1, SUCCESS, false);
611

    
612
            uploadDocid(onlineDocid + ".2",
613
                        onlinetestdatafile2, SUCCESS, false);
614

    
615
            // login as another user
616
            m.logout();
617
            m.login(anotheruser, anotherpassword);
618

    
619
            // try to read the data
620
            readDocid(onlineDocid + ".2", FAILURE, true);
621

    
622
            // try to upload another document with same id
623
            uploadDocid(onlineDocid + ".2",
624
                        onlinetestdatafile2, FAILURE, false);
625

    
626
            // try to upload another document with updated id
627
            uploadDocid(onlineDocid + ".3",
628
                        onlinetestdatafile2, FAILURE, true);
629

    
630

    
631
            // try to set the permissions for the uploaded document
632
            // the docid given is for the online document
633
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
634
                                         null, null,
635
                                         "ecogrid://knb/" + onlineDocid + ".1",
636
                                         "ecogrid://knb/" + onlineDocid + ".2",
637
                                         getAccessBlock(anotheruser, true,
638
                                         true, false, false, false),
639
                                         null, null, null, null);
640
            newdocid = generateDocid();
641
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
642

    
643
            // delete the document - should not be able to delete .1
644
            // but should be able to delete .2
645
            deleteDocid(onlineDocid + ".1", FAILURE, true);
646
            deleteDocid(onlineDocid + ".2", FAILURE, true);
647

    
648
            m.logout();
649

    
650
        }
651
        catch (MetacatAuthException mae) {
652
            fail("Authorization failed:\n" + mae.getMessage());
653
        }
654
        catch (MetacatInaccessibleException mie) {
655
            fail("Metacat Inaccessible:\n" + mie.getMessage());
656
        }
657
        catch (Exception e) {
658
            fail("General exception:\n" + e.getMessage());
659
        }
660
    }
661

    
662
    /**
663
     * Checking the following cases on 2.1.0 version documents:
664
     * when only online data is uploaded by a user and another user
665
     * -> tries to read it  - failure
666
     * -> tries to add same docid again  - failure
667
     * -> tries to update it  - failure
668
     * -> tries to set permissions on it  - failure
669
     * -> tries to delete it  - failure
670
     */
671
    public void onlineData210CasesTest_2() {
672
        try {
673
			debug("\nRunning: onlineData201CasesTest_2");
674

    
675
            // upload an online document
676
            onlineDocid = generateDocid();
677
            m.login(username, password);
678
            uploadDocid(onlineDocid + ".1",
679
                        onlinetestdatafile1, SUCCESS, false);
680

    
681
            uploadDocid(onlineDocid + ".2",
682
                        onlinetestdatafile2, SUCCESS, false);
683

    
684
            // login as another user
685
            m.logout();
686
            m.login(anotheruser, anotherpassword);
687

    
688
            // try to read the data
689
            readDocid(onlineDocid + ".2", FAILURE, true);
690

    
691
            // try to upload another document with same id
692
            uploadDocid(onlineDocid + ".2",
693
                        onlinetestdatafile2, FAILURE, false);
694

    
695
            // try to upload another document with updated id
696
            uploadDocid(onlineDocid + ".3",
697
                        onlinetestdatafile2, FAILURE, true);
698

    
699

    
700
            // try to set the permissions for the uploaded document
701
            // the docid given is for the online document
702
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
703
                                         null, null,
704
                                         "ecogrid://knb/" + onlineDocid + ".1",
705
                                         "ecogrid://knb/" + onlineDocid + ".2",
706
                                         getAccessBlock(anotheruser, true,
707
                                         true, false, false, false),
708
                                         null, null, null, null);
709
            newdocid = generateDocid();
710
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
711

    
712
            // delete the document - should not be able to delete .1
713
            // but should be able to delete .2
714
            deleteDocid(onlineDocid + ".1", FAILURE, true);
715
            deleteDocid(onlineDocid + ".2", FAILURE, true);
716

    
717
            m.logout();
718

    
719
        }
720
        catch (MetacatAuthException mae) {
721
            fail("Authorization failed:\n" + mae.getMessage());
722
        }
723
        catch (MetacatInaccessibleException mie) {
724
            fail("Metacat Inaccessible:\n" + mie.getMessage());
725
        }
726
        catch (Exception e) {
727
            fail("General exception:\n" + e.getMessage());
728
        }
729
    }
730

    
731
    /**
732
     * Checking the following cases on 2.0.1 version documents:
733
     * when only online data is uploaded by a user with the following different
734
     * access controls in another document
735
     *   1.read
736
     *   2.write
737
     *   3.change permission
738
     *   4.all
739
     * And another user tries to do the following:
740
     * -> tries to read it
741
     * -> tries to update it
742
     * -> tries to set permissions on it
743
     * -> tries to delete it
744
     */
745
    public void onlineData201CasesTest_3() {
746
        try {
747
			debug("\nRunning: onlineData201CasesTest_3");
748

    
749
            /////////Case 1./////////////////////
750
            // upload an online document - read only
751
            onlineDocid = generateDocid();
752
            m.login(username, password);
753
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
754

    
755
            // upload a document which gives read access to the online document
756
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
757
                                         null, null,
758
                                         "ecogrid://knb/" + onlineDocid + ".1",
759
                                         null, getAccessBlock(anotheruser, true,
760
                true, false, false, false),
761
                                         null, null, null, null);
762
            newdocid = generateDocid();
763
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
764
            m.logout();
765

    
766
            // login as another user
767
            m.login(anotheruser, anotherpassword);
768

    
769
            // try to read the online data
770
            readDocid(onlineDocid + ".1", SUCCESS, false);
771

    
772
            // try to upload another data with updated id
773
            uploadDocid(onlineDocid + ".2",
774
                        onlinetestdatafile2, FAILURE, true);
775

    
776
            // try to set the permissions for the uploaded document
777
            // the docid given is for the online document
778
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
779
                                         null, null,
780
                                         "ecogrid://knb/" + onlineDocid + ".1",
781
                                         null, getAccessBlock(anotheruser, true,
782
                false, false, false, true),
783
                                         null, null, null, null);
784
            newdocid = generateDocid();
785
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
786

    
787
            // delete the document
788
            deleteDocid(onlineDocid + ".1", FAILURE, true);
789
            m.logout();
790

    
791
            /////////Case 2/////////////////////
792
            // upload an online document - write only
793
            onlineDocid = generateDocid();
794
            m.login(username, password);
795
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
796

    
797
            // upload a document which gives read access to the online document
798
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Another insert",
799
                                         null, null,
800
                                         "ecogrid://knb/" + onlineDocid + ".1",
801
                                         null, getAccessBlock(anotheruser, true,
802
                false, true, false, false),
803
                                         null, null, null, null);
804
            newdocid = generateDocid();
805
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
806
            m.logout();
807

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

    
811
            // try to read the online data
812
            readDocid(onlineDocid + ".1", FAILURE, true);
813

    
814
            // try to upload another data with updated id
815
            uploadDocid(onlineDocid + ".2",
816
                        onlinetestdatafile2, SUCCESS, false);
817

    
818
            // try to set the permissions for the uploaded document
819
            // the docid given is for the online document
820
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Another insert",
821
                                         null, null,
822
                                         "ecogrid://knb/" + onlineDocid + ".1",
823
                                         null, getAccessBlock(anotheruser, true,
824
                                         false, false, false, true),
825
                                         null, null, null, null);
826
            newdocid = generateDocid();
827
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
828

    
829
            // delete the document
830
            deleteDocid(onlineDocid + ".2", FAILURE, true);
831
            m.logout();
832

    
833
            /////////Case 3/////////////////////
834
            // upload an online document - change permission only
835
            onlineDocid = generateDocid();
836
            m.login(username, password);
837
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
838

    
839
            // upload a document which gives read access to the online document
840
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Another insert",
841
                                         null, null,
842
                                         "ecogrid://knb/" + onlineDocid + ".1",
843
                                         null, getAccessBlock(anotheruser, true,
844
                                         false, false, true, false),
845
                                         null, null, null, null);
846
            newdocid = generateDocid();
847
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
848
            m.logout();
849

    
850
            // login as another user
851
            m.login(anotheruser, anotherpassword);
852

    
853
            // try to read the online data
854
            readDocid(onlineDocid + ".1", FAILURE, true);
855

    
856
            // try to upload another data with updated id
857
            uploadDocid(onlineDocid + ".2",
858
                          onlinetestdatafile2, FAILURE, true);
859

    
860
            // try to set the permissions for the uploaded document
861
            // the docid given is for the online document
862
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
863
                                         null, null,
864
                                         "ecogrid://knb/" + onlineDocid + ".1",
865
                                         null, getAccessBlock(anotheruser, true,
866
                                         false, false, false, true),
867
                                         null, null, null, null);
868
            newdocid = generateDocid();
869
            // ERRRRRRRRRRRRRRRR
870
            // User does not have permission to update of access rules for data
871
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
872

    
873
            // delete the document
874
            deleteDocid(onlineDocid + ".1", FAILURE, true);
875
            m.logout();
876

    
877
            /////////Case 4/////////////////////
878
            // upload an online document all
879
            onlineDocid = generateDocid();
880
            m.login(username, password);
881
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
882

    
883
            // upload a document which gives read access to the online document
884
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
885
                                         null, null,
886
                                         "ecogrid://knb/" + onlineDocid + ".1",
887
                                         null, getAccessBlock(anotheruser, true,
888
                                         false, false, false, true),
889
                                         null, null, null, null);
890
            newdocid = generateDocid();
891
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
892
            m.logout();
893

    
894
            // login as another user
895
            m.login(anotheruser, anotherpassword);
896

    
897
            // try to read the online data
898
            readDocid(onlineDocid + ".1", SUCCESS, false);
899

    
900
            // try to upload another data with updated id
901
            uploadDocid(onlineDocid + ".2",
902
                        onlinetestdatafile2, SUCCESS, false);
903

    
904
            // try to set the permissions for the uploaded document
905
            // the docid given is for the online document
906
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
907
                                         null, null,
908
                                         "ecogrid://knb/" + onlineDocid + ".1",
909
                                         null, getAccessBlock(anotheruser, true,
910
                                         true, false, false, false),
911
                                         null, null, null, null);
912
            newdocid = generateDocid();
913
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
914

    
915
            m.logout();
916
            // delete the document
917
            deleteDocid(onlineDocid + ".1", FAILURE, false);
918

    
919
            m.logout();
920

    
921
        }
922
        catch (MetacatAuthException mae) {
923
            fail("Authorization failed:\n" + mae.getMessage());
924
        }
925
        catch (MetacatInaccessibleException mie) {
926
            fail("Metacat Inaccessible:\n" + mie.getMessage());
927
        }
928
        catch (Exception e) {
929
            fail("General exception:\n" + e.getMessage());
930
        }
931
    }
932

    
933
    /**
934
     * Checking the following cases on 2.1.0 version documents:
935
     * when only online data is uploaded by a user with the following different
936
     * access controls in another document
937
     *   1.read
938
     *   2.write
939
     *   3.change permission
940
     *   4.all
941
     * And another user tries to do the following:
942
     * -> tries to read it
943
     * -> tries to update it
944
     * -> tries to set permissions on it
945
     * -> tries to delete it
946
     */
947
    public void onlineData210CasesTest_3() {
948
        try {
949
			debug("\nRunning: onlineData210CasesTest_3");
950

    
951
            /////////Case 1./////////////////////
952
            // upload an online document - read only
953
            onlineDocid = generateDocid();
954
            m.login(username, password);
955
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
956

    
957
            // upload a document which gives read access to the online document
958
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
959
                                         null, null,
960
                                         "ecogrid://knb/" + onlineDocid + ".1",
961
                                         null, getAccessBlock(anotheruser, true,
962
                true, false, false, false),
963
                                         null, null, null, null);
964
            newdocid = generateDocid();
965
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
966
            m.logout();
967

    
968
            // login as another user
969
            m.login(anotheruser, anotherpassword);
970

    
971
            // try to read the online data
972
            readDocid(onlineDocid + ".1", SUCCESS, false);
973

    
974
            // try to upload another data with updated id
975
            uploadDocid(onlineDocid + ".2",
976
                        onlinetestdatafile2, FAILURE, true);
977

    
978
            // try to set the permissions for the uploaded document
979
            // the docid given is for the online document
980
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
981
                                         null, null,
982
                                         "ecogrid://knb/" + onlineDocid + ".1",
983
                                         null, getAccessBlock(anotheruser, true,
984
                false, false, false, true),
985
                                         null, null, null, null);
986
            newdocid = generateDocid();
987
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
988

    
989
            // delete the document
990
            deleteDocid(onlineDocid + ".1", FAILURE, true);
991
            m.logout();
992

    
993
            /////////Case 2/////////////////////
994
            // upload an online document - write only
995
            onlineDocid = generateDocid();
996
            m.login(username, password);
997
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
998

    
999
            // upload a document which gives read access to the online document
1000
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Another insert",
1001
                                         null, null,
1002
                                         "ecogrid://knb/" + onlineDocid + ".1",
1003
                                         null, getAccessBlock(anotheruser, true,
1004
                false, true, false, false),
1005
                                         null, null, null, null);
1006
            newdocid = generateDocid();
1007
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1008
            m.logout();
1009

    
1010
            // login as another user
1011
            m.login(anotheruser, anotherpassword);
1012

    
1013
            // try to read the online data
1014
            readDocid(onlineDocid + ".1", FAILURE, true);
1015

    
1016
            // try to upload another data with updated id
1017
            uploadDocid(onlineDocid + ".2",
1018
                        onlinetestdatafile2, SUCCESS, false);
1019

    
1020
            // try to set the permissions for the uploaded document
1021
            // the docid given is for the online document
1022
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Another insert",
1023
                                         null, null,
1024
                                         "ecogrid://knb/" + onlineDocid + ".1",
1025
                                         null, getAccessBlock(anotheruser, true,
1026
                                         false, false, false, true),
1027
                                         null, null, null, null);
1028
            newdocid = generateDocid();
1029
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
1030

    
1031
            // delete the document
1032
            deleteDocid(onlineDocid + ".2", FAILURE, true);
1033
            m.logout();
1034

    
1035
            /////////Case 3/////////////////////
1036
            // upload an online document - change permission only
1037
            onlineDocid = generateDocid();
1038
            m.login(username, password);
1039
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1040

    
1041
            // upload a document which gives read access to the online document
1042
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Another insert",
1043
                                         null, null,
1044
                                         "ecogrid://knb/" + onlineDocid + ".1",
1045
                                         null, getAccessBlock(anotheruser, true,
1046
                                         false, false, true, false),
1047
                                         null, null, null, null);
1048
            newdocid = generateDocid();
1049
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1050
            m.logout();
1051

    
1052
            // login as another user
1053
            m.login(anotheruser, anotherpassword);
1054

    
1055
            // try to read the online data
1056
            readDocid(onlineDocid + ".1", FAILURE, true);
1057

    
1058
            // try to upload another data with updated id
1059
            uploadDocid(onlineDocid + ".2",
1060
                          onlinetestdatafile2, FAILURE, true);
1061

    
1062
            // try to set the permissions for the uploaded document
1063
            // the docid given is for the online document
1064
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1065
                                         null, null,
1066
                                         "ecogrid://knb/" + onlineDocid + ".1",
1067
                                         null, getAccessBlock(anotheruser, true,
1068
                                         false, false, false, true),
1069
                                         null, null, null, null);
1070
            newdocid = generateDocid();
1071
            // ERRRRRRRRRRRRRRRR
1072
            // User does not have permission to update of access rules for data
1073
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1074

    
1075
            // delete the document
1076
            deleteDocid(onlineDocid + ".1", FAILURE, true);
1077
            m.logout();
1078

    
1079
            /////////Case 4/////////////////////
1080
            // upload an online document all
1081
            onlineDocid = generateDocid();
1082
            m.login(username, password);
1083
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1084

    
1085
            // upload a document which gives read access to the online document
1086
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1087
                                         null, null,
1088
                                         "ecogrid://knb/" + onlineDocid + ".1",
1089
                                         null, getAccessBlock(anotheruser, true,
1090
                                         false, false, false, true),
1091
                                         null, null, null, null);
1092
            newdocid = generateDocid();
1093
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1094
            m.logout();
1095

    
1096
            // login as another user
1097
            m.login(anotheruser, anotherpassword);
1098

    
1099
            // try to read the online data
1100
            readDocid(onlineDocid + ".1", SUCCESS, false);
1101

    
1102
            // try to upload another data with updated id
1103
            uploadDocid(onlineDocid + ".2",
1104
                        onlinetestdatafile2, SUCCESS, false);
1105

    
1106
            // try to set the permissions for the uploaded document
1107
            // the docid given is for the online document
1108
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1109
                                         null, null,
1110
                                         "ecogrid://knb/" + onlineDocid + ".1",
1111
                                         null, getAccessBlock(anotheruser, true,
1112
                                         true, false, false, false),
1113
                                         null, null, null, null);
1114
            newdocid = generateDocid();
1115
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1116

    
1117
            m.logout();
1118
            // delete the document
1119
            deleteDocid(onlineDocid + ".1", FAILURE, false);
1120

    
1121
            m.logout();
1122

    
1123
        }
1124
        catch (MetacatAuthException mae) {
1125
            fail("Authorization failed:\n" + mae.getMessage());
1126
        }
1127
        catch (MetacatInaccessibleException mie) {
1128
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1129
        }
1130
        catch (Exception e) {
1131
            fail("General exception:\n" + e.getMessage());
1132
        }
1133
    }
1134

    
1135
    /**
1136
     * Checking the following cases on 2.0.1 version documents:
1137
     * when only online data is uploaded by a user with the following different
1138
     * access controls specified in addiotnal metadata in another document
1139
     *   1.read
1140
     *   2.write
1141
     *   3.change permission
1142
     *   4.all
1143
     * And another user tries to do the following:
1144
     * -> tries to read it
1145
     * -> tries to update it
1146
     * -> tries to set permissions on it
1147
     * -> tries to delete it
1148
     */
1149
    public void onlineData201CasesTest_4() {
1150
        try {
1151
			debug("\nRunning: onlineData201CasesTest_4");
1152

    
1153
//            /////////Case 1./////////////////////
1154
//            // upload an online document - read only
1155
//            onlineDocid = generateDocid();
1156
//            m.login(username, password);
1157
//            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1158
//
1159
//            // upload a document which gives read access to the online document
1160
//            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
1161
//                                         null, null,
1162
//                                         "ecogrid://knb/" + onlineDocid + ".1",
1163
//                                         null, null, null, null,
1164
//                                         getAccessBlock(anotheruser, true,
1165
//                                         true, false, false, false), null);
1166
//            newdocid = generateDocid();
1167
//            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1168
//            m.logout();
1169
//
1170
//            // login as another user
1171
//            m.login(anotheruser, anotherpassword);
1172
//
1173
//            // try to read the online data
1174
//            readDocid(onlineDocid + ".1", SUCCESS, false);
1175
//
1176
//            // try to upload another data with updated id
1177
//            uploadDocid(onlineDocid + ".2",
1178
//                        onlinetestdatafile2, FAILURE, true);
1179
//
1180
//            // try to set the permissions for the uploaded document
1181
//            // the docid given is for the online document
1182
//            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
1183
//                                         null, null,
1184
//                                         "ecogrid://knb/" + onlineDocid + ".1",
1185
//                                         null, null, null, null,
1186
//                                         getAccessBlock(anotheruser, true,
1187
//                                         false, false, false, true), null);
1188
//            newdocid = generateDocid();
1189
//            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
1190
//
1191
//            // delete the document
1192
//            deleteDocid(onlineDocid + ".1", FAILURE, true);
1193
//            m.logout();
1194

    
1195
            /////////Case 2/////////////////////
1196
            // upload an online document - write only
1197
            onlineDocid = generateDocid();
1198
            m.login(username, password);
1199
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1200

    
1201
            // upload a document which gives read access to the online document
1202
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
1203
                                         null, null,
1204
                                         "ecogrid://knb/" + onlineDocid + ".1",
1205
                                         null, null, null, null,
1206
                                         getAccessBlock(anotheruser, true,
1207
                                         false, true, false, false), null);
1208
            newdocid = generateDocid();
1209
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1210
            m.logout();
1211

    
1212
            // login as another user
1213
            m.login(anotheruser, anotherpassword);
1214

    
1215
            // try to read the online data
1216
            readDocid(onlineDocid + ".1", FAILURE, true);
1217

    
1218
            // try to upload another data with updated id
1219
            uploadDocid(onlineDocid + ".2",
1220
                        onlinetestdatafile2, SUCCESS, false);
1221

    
1222
            // try to set the permissions for the uploaded document
1223
            // the docid given is for the online document
1224
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
1225
                                         null, null,
1226
                                         "ecogrid://knb/" + onlineDocid + ".1",
1227
                                         null, null, null, null,
1228
                                         getAccessBlock(anotheruser, true,
1229
                                         false, false, false, true), null);
1230
            newdocid = generateDocid();
1231
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
1232

    
1233
            // delete the document
1234
            deleteDocid(onlineDocid + ".2", FAILURE, true);
1235
            m.logout();
1236

    
1237
//            /////////Case 3/////////////////////
1238
//            // upload an online document - change permission only
1239
//            onlineDocid = generateDocid();
1240
//            m.login(username, password);
1241
//            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1242
//
1243
//            // upload a document which gives read access to the online document
1244
//            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
1245
//                                         null, null,
1246
//                                         "ecogrid://knb/" + onlineDocid + ".1",
1247
//                                         null, null, null, null,
1248
//                                         getAccessBlock(anotheruser, true,
1249
//                                         false, false, true, false), null);
1250
//            newdocid = generateDocid();
1251
//            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1252
//            m.logout();
1253
//
1254
//            // login as another user
1255
//            m.login(anotheruser, anotherpassword);
1256
//
1257
//            // try to read the online data
1258
//            readDocid(onlineDocid + ".1", FAILURE, true);
1259
//
1260
//            // try to upload another data with updated id
1261
//            uploadDocid(onlineDocid + ".2",
1262
//                        onlinetestdatafile2, FAILURE, true);
1263
//
1264
//            // try to set the permissions for the uploaded document
1265
//            // the docid given is for the online document
1266
//            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
1267
//                                         null, null,
1268
//                                         "ecogrid://knb/" + onlineDocid + ".1",
1269
//                                         null, null, null, null,
1270
//                                         getAccessBlock(anotheruser, true,
1271
//                                         false, false, false, true), null);
1272
//            newdocid = generateDocid();
1273
//            // ERRRRRRRRRRRRRRRR
1274
//            // User does not have permission to update of access rules for data
1275
//            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1276
//
1277
//            // delete the document
1278
//            deleteDocid(onlineDocid + ".1", FAILURE, true);
1279
//            m.logout();
1280
//
1281
//            /////////Case 4/////////////////////
1282
//            // upload an online document all
1283
//            onlineDocid = generateDocid();
1284
//            m.login(username, password);
1285
//            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1286
//
1287
//            // upload a document which gives read access to the online document
1288
//            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
1289
//                                         null, null,
1290
//                                         "ecogrid://knb/" + onlineDocid + ".1",
1291
//                                         null, null, null, null,
1292
//                                         getAccessBlock(anotheruser, true,
1293
//                                         false, false, false, true), null);
1294
//            newdocid = generateDocid();
1295
//            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1296
//            m.logout();
1297
//
1298
//            // login as another user
1299
//            m.login(anotheruser, anotherpassword);
1300
//
1301
//            // try to read the online data
1302
//            readDocid(onlineDocid + ".1", SUCCESS, false);
1303
//
1304
//            // try to upload another data with updated id
1305
//            uploadDocid(onlineDocid + ".2",
1306
//                        onlinetestdatafile2, SUCCESS, false);
1307
//
1308
//            // try to set the permissions for the uploaded document
1309
//            // the docid given is for the online document
1310
//            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
1311
//                                         null, null,
1312
//                                         "ecogrid://knb/" + onlineDocid + ".1",
1313
//                                         null, null, null, null,
1314
//                                         getAccessBlock(anotheruser, true,
1315
//                                         true, false, false, false), null);
1316
//            newdocid = generateDocid();
1317
//            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1318
//
1319
//            m.logout();
1320
//            // delete the document
1321
//            deleteDocid(onlineDocid + ".1", FAILURE, false);
1322
//
1323
//            m.logout();
1324

    
1325
        }
1326
        catch (MetacatAuthException mae) {
1327
            fail("Authorization failed:\n" + mae.getMessage());
1328
        }
1329
        catch (MetacatInaccessibleException mie) {
1330
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1331
        }
1332
        catch (Exception e) {
1333
            fail("General exception:\n" + e.getMessage());
1334
        }
1335
    }
1336
    
1337
    /**
1338
     * Checking the following cases on 2.1.0 version documents:
1339
     * when only online data is uploaded by a user with the following different
1340
     * access controls specified in addiotnal metadata in another document
1341
     *   1.read
1342
     *   2.write
1343
     *   3.change permission
1344
     *   4.all
1345
     * And another user tries to do the following:
1346
     * -> tries to read it
1347
     * -> tries to update it
1348
     * -> tries to set permissions on it
1349
     * -> tries to delete it
1350
     */
1351
    public void onlineData210CasesTest_4() {
1352
        try {
1353
			debug("\nRunning: onlineData210CasesTest_4");
1354

    
1355
            /////////Case 1./////////////////////
1356
            // upload an online document - read only
1357
            onlineDocid = generateDocid();
1358
            m.login(username, password);
1359
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1360

    
1361
            // upload a document which gives read access to the online document
1362
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1363
                                         null, null,
1364
                                         "ecogrid://knb/" + onlineDocid + ".1",
1365
                                         null, null, null, null,
1366
                                         getAccessBlock(anotheruser, true,
1367
                                         true, false, false, false), null);
1368
            newdocid = generateDocid();
1369
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1370
            m.logout();
1371

    
1372
            // login as another user
1373
            m.login(anotheruser, anotherpassword);
1374

    
1375
            // try to read the online data
1376
            readDocid(onlineDocid + ".1", SUCCESS, false);
1377

    
1378
            // try to upload another data with updated id
1379
            uploadDocid(onlineDocid + ".2",
1380
                        onlinetestdatafile2, FAILURE, true);
1381

    
1382
            // try to set the permissions for the uploaded document
1383
            // the docid given is for the online document
1384
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1385
                                         null, null,
1386
                                         "ecogrid://knb/" + onlineDocid + ".1",
1387
                                         null, null, null, null,
1388
                                         getAccessBlock(anotheruser, true,
1389
                                         false, false, false, true), null);
1390
            newdocid = generateDocid();
1391
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
1392

    
1393
            // delete the document
1394
            deleteDocid(onlineDocid + ".1", FAILURE, true);
1395
            m.logout();
1396

    
1397
            /////////Case 2/////////////////////
1398
            // upload an online document - write only
1399
            onlineDocid = generateDocid();
1400
            m.login(username, password);
1401
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1402

    
1403
            // upload a document which gives read access to the online document
1404
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1405
                                         null, null,
1406
                                         "ecogrid://knb/" + onlineDocid + ".1",
1407
                                         null, null, null, null,
1408
                                         getAccessBlock(anotheruser, true,
1409
                                         false, true, false, false), null);
1410
            newdocid = generateDocid();
1411
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1412
            m.logout();
1413

    
1414
            // login as another user
1415
            m.login(anotheruser, anotherpassword);
1416

    
1417
            // try to read the online data
1418
            readDocid(onlineDocid + ".1", FAILURE, true);
1419

    
1420
            // try to upload another data with updated id
1421
            uploadDocid(onlineDocid + ".2",
1422
                        onlinetestdatafile2, SUCCESS, false);
1423

    
1424
            // try to set the permissions for the uploaded document
1425
            // the docid given is for the online document
1426
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1427
                                         null, null,
1428
                                         "ecogrid://knb/" + onlineDocid + ".1",
1429
                                         null, null, null, null,
1430
                                         getAccessBlock(anotheruser, true,
1431
                                         false, false, false, true), null);
1432
            newdocid = generateDocid();
1433
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
1434

    
1435
            // delete the document
1436
            deleteDocid(onlineDocid + ".2", FAILURE, true);
1437
            m.logout();
1438

    
1439
            /////////Case 3/////////////////////
1440
            // upload an online document - change permission only
1441
            onlineDocid = generateDocid();
1442
            m.login(username, password);
1443
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1444

    
1445
            // upload a document which gives read access to the online document
1446
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1447
                                         null, null,
1448
                                         "ecogrid://knb/" + onlineDocid + ".1",
1449
                                         null, null, null, null,
1450
                                         getAccessBlock(anotheruser, true,
1451
                                         false, false, true, false), null);
1452
            newdocid = generateDocid();
1453
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1454
            m.logout();
1455

    
1456
            // login as another user
1457
            m.login(anotheruser, anotherpassword);
1458

    
1459
            // try to read the online data
1460
            readDocid(onlineDocid + ".1", FAILURE, true);
1461

    
1462
            // try to upload another data with updated id
1463
            uploadDocid(onlineDocid + ".2",
1464
                        onlinetestdatafile2, FAILURE, true);
1465

    
1466
            // try to set the permissions for the uploaded document
1467
            // the docid given is for the online document
1468
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1469
                                         null, null,
1470
                                         "ecogrid://knb/" + onlineDocid + ".1",
1471
                                         null, null, null, null,
1472
                                         getAccessBlock(anotheruser, true,
1473
                                         false, false, false, true), null);
1474
            newdocid = generateDocid();
1475
            // ERRRRRRRRRRRRRRRR
1476
            // User does not have permission to update of access rules for data
1477
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1478

    
1479
            // delete the document
1480
            deleteDocid(onlineDocid + ".1", FAILURE, true);
1481
            m.logout();
1482

    
1483
            /////////Case 4/////////////////////
1484
            // upload an online document all
1485
            onlineDocid = generateDocid();
1486
            m.login(username, password);
1487
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1488

    
1489
            // upload a document which gives read access to the online document
1490
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1491
                                         null, null,
1492
                                         "ecogrid://knb/" + onlineDocid + ".1",
1493
                                         null, null, null, null,
1494
                                         getAccessBlock(anotheruser, true,
1495
                                         false, false, false, true), null);
1496
            newdocid = generateDocid();
1497
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1498
            m.logout();
1499

    
1500
            // login as another user
1501
            m.login(anotheruser, anotherpassword);
1502

    
1503
            // try to read the online data
1504
            readDocid(onlineDocid + ".1", SUCCESS, false);
1505

    
1506
            // try to upload another data with updated id
1507
            uploadDocid(onlineDocid + ".2",
1508
                        onlinetestdatafile2, SUCCESS, false);
1509

    
1510
            // try to set the permissions for the uploaded document
1511
            // the docid given is for the online document
1512
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1513
                                         null, null,
1514
                                         "ecogrid://knb/" + onlineDocid + ".1",
1515
                                         null, null, null, null,
1516
                                         getAccessBlock(anotheruser, true,
1517
                                         true, false, false, false), null);
1518
            newdocid = generateDocid();
1519
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1520

    
1521
            m.logout();
1522
            // delete the document
1523
            deleteDocid(onlineDocid + ".1", FAILURE, false);
1524

    
1525
            m.logout();
1526

    
1527
        }
1528
        catch (MetacatAuthException mae) {
1529
            fail("Authorization failed:\n" + mae.getMessage());
1530
        }
1531
        catch (MetacatInaccessibleException mie) {
1532
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1533
        }
1534
        catch (Exception e) {
1535
            fail("General exception:\n" + e.getMessage());
1536
        }
1537
    }
1538

    
1539
    /**
1540
     * Checking the following cases on 2.0.1 version documents:
1541
     * -> when online data with document refering to it is uploaded with
1542
     *    rules in additional metadata for an wrong entity id which
1543
     *    doesnt exist
1544
     * -> when online data with document refering to it is uploaded with
1545
     *    rules in additional metadata for an entity which doesnt
1546
     *    exist - wrong url
1547
     */
1548
    public void onlineData201CasesTest_5() {
1549
        try {       	
1550
			debug("\nRunning: onlineData201CasesTest_5");
1551

    
1552
            // upload online data
1553
            onlineDocid = generateDocid();
1554
            m.login(username, password);
1555

    
1556
            /////////Case 1
1557
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
1558
                                         "ecogrid://knb/" + onlineDocid + ".1",
1559
                                         null, null, null, null, null,
1560
                                         getAccessBlock(anotheruser, true,
1561
                                         true, false, false, false));
1562
            newdocid = generateDocid();
1563
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
1564

    
1565
            /////////Case 2
1566
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
1567
                                         "ecogrid://knb/" + onlineDocid + ".1",
1568
                                         null, null, null, null,
1569
                                         getAccessBlock(anotheruser, true,
1570
                                         true, false, false, false), null);
1571
            newdocid = generateDocid();
1572

    
1573
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1574
            m.logout();
1575

    
1576
        }
1577
        catch (MetacatAuthException mae) {
1578
            fail("Authorization failed:\n" + mae.getMessage());
1579
        }
1580
        catch (MetacatInaccessibleException mie) {
1581
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1582
        }
1583
        catch (Exception e) {
1584
            fail("General exception:\n" + e.getMessage());
1585
        }
1586
    }
1587
   
1588
// MCD - Removed this test.  Access does not use additional metadata in 2.0.1  
1589
//    /**
1590
//     * Checking the following cases on 2.1.0 version documents:
1591
//     * -> when online data with document refering to it is uploaded with
1592
//     *    rules in additional metadata for an wrong entity id which
1593
//     *    doesnt exist
1594
//     * -> when online data with document refering to it is uploaded with
1595
//     *    rules in additional metadata for an entity which doesnt
1596
//     *    exist - wrong url
1597
//     */
1598
//    public void onlineData210CasesTest_5() {
1599
//        try {       	
1600
//			debug("\nRunning: onlineData210CasesTest_5");
1601
//
1602
//            // upload online data
1603
//            onlineDocid = generateDocid();
1604
//            m.login(username, password);
1605
//
1606
//            /////////Case 1
1607
//            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
1608
//                                         "ecogrid://knb/" + onlineDocid + ".1",
1609
//                                         null, null, null, null, null,
1610
//                                         getAccessBlock(anotheruser, true,
1611
//                                         true, false, false, false));
1612
//            newdocid = generateDocid();
1613
//            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
1614
//
1615
//            /////////Case 2
1616
//            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
1617
//                                         "ecogrid://knb/" + onlineDocid + ".1",
1618
//                                         null, null, null, null,
1619
//                                         getAccessBlock(anotheruser, true,
1620
//                                         true, false, false, false), null);
1621
//            newdocid = generateDocid();
1622
//
1623
//            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1624
//            m.logout();
1625
//
1626
//        }
1627
//        catch (MetacatAuthException mae) {
1628
//            fail("Authorization failed:\n" + mae.getMessage());
1629
//        }
1630
//        catch (MetacatInaccessibleException mie) {
1631
//            fail("Metacat Inaccessible:\n" + mie.getMessage());
1632
//        }
1633
//        catch (Exception e) {
1634
//            fail("General exception:\n" + e.getMessage());
1635
//        }
1636
//    }
1637

    
1638

    
1639
    /**
1640
     * Checking the following cases on 2.0.1 version documents:
1641
     * -> when a document is added with no online data - it is updated (has
1642
     *    access)  - then data is added - and a url to it is added and docid
1643
     *    is updated - then the access is updated in document
1644
     *    does it result in rules being applied on the data
1645
     *    (Andrea Chadden was having problem is a similar case)
1646
     * -> when online data with document refering to it is uploaded with read
1647
     *    access for document and no access for docid and vice versa
1648
     */
1649
    public void onlineData201CasesTest_6() {
1650
        try {
1651
			debug("\nRunning: onlineData201CasesTest_6");
1652
            // insert a document
1653
            m.login(username, password);
1654
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
1655
                                         null, null, null,
1656
                                         null, getAccessBlock(anotheruser, true,
1657
                                         true, false, false, false), null, null,
1658
                                         null, null);
1659
            newdocid = generateDocid();
1660
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1661
            m.logout();
1662

    
1663

    
1664
            // update document
1665
            m.login(username, password);
1666
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing update",
1667
                                         null, null, null,
1668
                                         null, getAccessBlock(anotheruser, true,
1669
                                         true, false, false, false), null, null,
1670
                                         null, null);
1671
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1672
            m.logout();
1673

    
1674

    
1675
            // upload data and update the document
1676
            onlineDocid = generateDocid();
1677
            m.login(username, password);
1678
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1679
            m.logout();
1680

    
1681
            // try to read the online data
1682
            m.login(anotheruser, anotherpassword);
1683
            readDocid(onlineDocid + ".1", FAILURE, true);
1684
            m.logout();
1685

    
1686
            // upload data and update the document
1687
            m.login(username, password);
1688
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing update",
1689
                                         null, null,
1690
                                         "ecogrid://knb/" + onlineDocid + ".1",
1691
                                         null, getAccessBlock(anotheruser, true,
1692
                                         true, false, false, false), null, null,
1693
                                         null, null);
1694
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1695
            m.logout();
1696

    
1697
            // set read for document - no read for data
1698
            m.login(username, password);
1699
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
1700
                                         null, null,
1701
                                         "ecogrid://knb/" + onlineDocid + ".1",
1702
                                         null, getAccessBlock(anotheruser, true,
1703
                                         true, false, false, false), null, null,
1704
                                         getAccessBlock(anotheruser, false,
1705
                                         false, false, false, true), null);
1706
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
1707
            m.logout();
1708

    
1709
            // try to read the online data
1710
            m.login(anotheruser, anotherpassword);
1711
            readDocid(newdocid + ".4", SUCCESS, false);
1712
            readDocid(onlineDocid + ".1", FAILURE, true);
1713
            m.logout();
1714

    
1715

    
1716
            // set read for document - no read for data
1717
            m.login(username, password);
1718
            testdocument = get201TestEmlDoc("Doing update", null, null,
1719
                                         "ecogrid://knb/" + onlineDocid + ".1",
1720
                                         null, getAccessBlock(anotheruser, false,
1721
                                         false, false, false, true), null, null,
1722
                                         getAccessBlock(anotheruser, true,
1723
                                         true, false, false, false), null);
1724
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1725
            m.logout();
1726

    
1727
            // try to read the online data
1728
            m.login(anotheruser, anotherpassword);
1729
            readDocid(newdocid + ".5", FAILURE, true);
1730
            readDocid(onlineDocid + ".1", SUCCESS, false);
1731
            m.logout();
1732
        }
1733
        catch (MetacatAuthException mae) {
1734
            fail("Authorization failed:\n" + mae.getMessage());
1735
        }
1736
        catch (MetacatInaccessibleException mie) {
1737
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1738
        }
1739
        catch (Exception e) {
1740
            fail("General exception:\n" + e.getMessage());
1741
        }
1742
    }
1743

    
1744
    /**
1745
     * Checking the following cases on 2.1.0 version documents:
1746
     * -> when a document is added with no online data - it is updated (has
1747
     *    access)  - then data is added - and a url to it is added and docid
1748
     *    is updated - then the access is updated in document
1749
     *    does it result in rules being applied on the data
1750
     *    (Andrea Chadden was having problem is a similar case)
1751
     * -> when online data with document refering to it is uploaded with read
1752
     *    access for document and no access for docid and vice versa
1753
     */
1754
    public void onlineData210CasesTest_6() {
1755
        try {
1756
			debug("\nRunning: onlineData210CasesTest_6");
1757
            // insert a document
1758
            m.login(username, password);
1759
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1760
                                         null, null, null,
1761
                                         null, getAccessBlock(anotheruser, true,
1762
                                         true, false, false, false), null, null,
1763
                                         null, null);
1764
            newdocid = generateDocid();
1765
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1766
            m.logout();
1767

    
1768

    
1769
            // update document
1770
            m.login(username, password);
1771
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing update",
1772
                                         null, null, null,
1773
                                         null, getAccessBlock(anotheruser, true,
1774
                                         true, false, false, false), null, null,
1775
                                         null, null);
1776
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1777
            m.logout();
1778

    
1779

    
1780
            // upload data and update the document
1781
            onlineDocid = generateDocid();
1782
            m.login(username, password);
1783
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1784
            m.logout();
1785

    
1786
            // try to read the online data
1787
            m.login(anotheruser, anotherpassword);
1788
            readDocid(onlineDocid + ".1", FAILURE, true);
1789
            m.logout();
1790

    
1791
            // upload data and update the document
1792
            m.login(username, password);
1793
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing update",
1794
                                         null, null,
1795
                                         "ecogrid://knb/" + onlineDocid + ".1",
1796
                                         null, getAccessBlock(anotheruser, true,
1797
                                         true, false, false, false), null, null,
1798
                                         null, null);
1799
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1800
            m.logout();
1801

    
1802
            // set read for document - no read for data
1803
            m.login(username, password);
1804
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing insert",
1805
                                         null, null,
1806
                                         "ecogrid://knb/" + onlineDocid + ".1",
1807
                                         null, getAccessBlock(anotheruser, true,
1808
                                         true, false, false, false), null, null,
1809
                                         getAccessBlock(anotheruser, false,
1810
                                         false, false, false, true), null);
1811
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
1812
            m.logout();
1813

    
1814
            // try to read the online data
1815
            m.login(anotheruser, anotherpassword);
1816
            readDocid(newdocid + ".4", SUCCESS, false);
1817
            readDocid(onlineDocid + ".1", FAILURE, true);
1818
            m.logout();
1819

    
1820

    
1821
            // set no read for document - read for data
1822
            m.login(username, password);
1823
            testdocument = get210TestEmlDoc("Doing update", null, null,
1824
                                         "ecogrid://knb/" + onlineDocid + ".1",
1825
                                         null, getAccessBlock(anotheruser, false,
1826
                                         false, false, false, true), null, null,
1827
                                         getAccessBlock(anotheruser, true,
1828
                                         true, false, false, false), null);
1829
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1830
            m.logout();
1831

    
1832
            // try to read the online data
1833
            m.login(anotheruser, anotherpassword);
1834
            readDocid(newdocid + ".5", FAILURE, true);
1835
            readDocid(onlineDocid + ".1", SUCCESS, false);
1836
            m.logout();
1837
        }
1838
        catch (MetacatAuthException mae) {
1839
            fail("Authorization failed:\n" + mae.getMessage());
1840
        }
1841
        catch (MetacatInaccessibleException mie) {
1842
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1843
        }
1844
        catch (Exception e) {
1845
            fail("General exception:\n" + e.getMessage());
1846
        }
1847
    }
1848

    
1849

    
1850
    /**
1851
     * Insert a document into metacat. The expected result is passed as result
1852
     */
1853

    
1854
    private String insertDocid(String docid, String docText, boolean result,
1855
                               boolean expectKarmaException) {
1856
        String response = null;
1857
        try {
1858
            response = m.insert(docid,
1859
                                new StringReader(testdocument), null);
1860
            if (result) {
1861
                assertTrue( (response.indexOf("<success>") != -1));
1862
                assertTrue(response.indexOf(docid) != -1);
1863
            }
1864
            else {
1865
                assertTrue( (response.indexOf("<success>") == -1));
1866
            }
1867
            System.err.println(response);
1868
        }
1869
        catch (MetacatInaccessibleException mie) {
1870
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1871
        }
1872
        catch (InsufficientKarmaException ike) {
1873
            if (!expectKarmaException) {
1874
                fail("Insufficient karma:\n" + ike.getMessage());
1875
            }
1876
        }
1877
        catch (MetacatException me) {
1878
            if (result) {
1879
               fail("Metacat Error:\n" + me.getMessage());
1880
           }
1881
           else {
1882
               System.err.println("Metacat Error: " + me.getMessage());
1883
           }
1884
        }
1885
        catch (Exception e) {
1886
            fail("General exception:\n" + e.getMessage());
1887
        }
1888
        return response;
1889
    }
1890

    
1891
    /**
1892
     * Insert a document into metacat. The expected result is passed as result
1893
     */
1894

    
1895
    private String uploadDocid(String docid, String filePath, boolean result,
1896
                               boolean expectedKarmaException) {
1897
        String response = null;
1898
        try {
1899
            response = m.upload(docid, new File(filePath));
1900
            if (result) {
1901
                assertTrue( (response.indexOf("<success>") != -1));
1902
                assertTrue(response.indexOf(docid) != -1);
1903
            }
1904
            else {
1905
                assertTrue( (response.indexOf("<success>") == -1));
1906
            }
1907
            System.err.println("respose from metacat: " + response);
1908
        }
1909
        catch (MetacatInaccessibleException mie) {
1910
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1911
        }
1912
        catch (InsufficientKarmaException ike) {
1913
            if (!expectedKarmaException) {
1914
                fail("Insufficient karma:\n" + ike.getMessage());
1915
            }
1916
        }
1917
        catch (MetacatException me) {
1918
            if (result) {
1919
                fail("Metacat Error:\n" + me.getMessage());
1920
            }
1921
            else {
1922
                System.err.println("Metacat Error: " + me.getMessage());
1923
            }
1924
        }
1925
        catch (Exception e) {
1926
            fail("General exception:\n" + e.getMessage());
1927
        }
1928
        return response;
1929
    }
1930

    
1931
    /**
1932
     * Update a document in metacat. The expected result is passed as result
1933
     */
1934
    private String updateDocid(String docid, String docText, boolean result,
1935
                               boolean expectedKarmaFailure) {
1936
        String response = null;
1937
        try {
1938
            response = m.update(docid,
1939
                                new StringReader(testdocument), null);
1940

    
1941
            if (result) {
1942
                assertTrue( (response.indexOf("<success>") != -1));
1943
                assertTrue(response.indexOf(docid) != -1);
1944
            }
1945
            else {
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 (!expectedKarmaFailure) {
1955
                fail("Insufficient karma:\n" + ike.getMessage());
1956
            }
1957
        }
1958
        catch (MetacatException me) {
1959
            if (! (expectedKarmaFailure &&
1960
                   (me.getMessage().indexOf(
1961
                "User tried to update an access module when they don't have \"ALL\" permission") !=
1962
                    -1))) {
1963
                fail("Metacat Error:\n" + me.getMessage());
1964
            }
1965
        }
1966
        catch (Exception e) {
1967
            fail("General exception:\n" + e.getMessage());
1968
        }
1969

    
1970
        return response;
1971
    }
1972

    
1973
    /**
1974
     * Delete a document into metacat. The expected result is passed as result
1975
     */
1976
    private void deleteDocid(String docid, boolean result,
1977
                             boolean expectedKarmaFailure) {
1978
        try {
1979
            String response = m.delete(docid);
1980
            if (result) {
1981
                assertTrue(response.indexOf("<success>") != -1);
1982
            }
1983
            else {
1984
                assertTrue(response.indexOf("<success>") == -1);
1985
            }
1986
            System.err.println(response);
1987
        }
1988
        catch (MetacatInaccessibleException mie) {
1989
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1990
        }
1991
        catch (InsufficientKarmaException ike) {
1992
            if (!expectedKarmaFailure) {
1993
                    fail("Insufficient karma:\n" + ike.getMessage());
1994
                }
1995

    
1996
            }
1997
        catch (MetacatException me) {
1998
            if (result) {
1999
                fail("Metacat Error:\n" + me.getMessage());
2000
            }
2001
            else {
2002
                System.err.println("Metacat Error:\n" + me.getMessage());
2003
            }
2004
        }
2005
        catch (Exception e) {
2006
            fail("General exception:\n" + e.getMessage());
2007
        }
2008
    }
2009

    
2010
    /**
2011
     * Read a document from metacat. The expected result is passed as result
2012
     */
2013
    private void readDocid(String docid, boolean result,
2014
                           boolean expextedKarmaFailure) {
2015
        try {
2016
            Reader r = m.read(docid);
2017
            String response = IOUtil.getAsString(r, true);
2018

    
2019
            if (!result) {
2020
                assertTrue(response.indexOf("<success>") == -1);
2021
            }
2022
            // System.err.println(response);
2023
        }
2024
        catch (MetacatInaccessibleException mie) {
2025
            fail("Metacat Inaccessible:\n" + mie.getMessage());
2026
        }
2027
        catch (InsufficientKarmaException ike) {
2028
            if (!expextedKarmaFailure) {
2029
                fail("Insufficient karma:\n" + ike.getMessage());
2030
            }
2031
        }
2032
        catch (MetacatException me) {
2033
            if (result) {
2034
                fail("Metacat Error:\n" + me.getMessage());
2035
            }
2036
            else {
2037
                System.err.println("Metacat Error:\n" + me.getMessage());
2038
            }
2039
        }
2040
        catch (Exception e) {
2041
            fail("General exception:\n" + e.getMessage());
2042
        }
2043
    }
2044

    
2045
    /**
2046
     * Read a document from metacat and check if it is equal to a given string.
2047
     * The expected result is passed as result
2048
     */
2049

    
2050
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
2051
                                         boolean result,
2052
                                         boolean expextedKarmaFailure) {
2053
        try {
2054
            Reader r = m.read(docid);
2055
            String doc = IOUtil.getAsString(r, true);
2056
            if (result) {
2057

    
2058
                if (!testDoc.equals(doc)) {
2059
                    System.out.println("doc    :" + doc);
2060
                    System.out.println("testDoc:" + testDoc);
2061
                }
2062

    
2063
                assertTrue(testDoc.equals(doc));
2064
            }
2065
            else {
2066
                assertTrue(doc.indexOf("<error>") != -1);
2067
            }
2068
        }
2069
        catch (MetacatInaccessibleException mie) {
2070
            fail("Metacat Inaccessible:\n" + mie.getMessage());
2071
        }
2072
        catch (InsufficientKarmaException ike) {
2073
            if (!expextedKarmaFailure) {
2074
                fail("Insufficient karma:\n" + ike.getMessage());
2075
            }
2076
        }
2077
        catch (MetacatException me) {
2078
            fail("Metacat Error:\n" + me.getMessage());
2079
        }
2080
        catch (Exception e) {
2081
            fail("General exception:\n" + e.getMessage());
2082
        }
2083

    
2084
    }
2085

    
2086
    /**
2087
     * Create a hopefully unique docid for testing insert and update. Does
2088
     * not include the 'revision' part of the id.
2089
     *
2090
     * @return a String docid based on the current date and time
2091
     */
2092
    private String generateDocid() {
2093
        StringBuffer docid = new StringBuffer(prefix);
2094
        docid.append(".");
2095

    
2096
        // Create a calendar to get the date formatted properly
2097
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
2098
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
2099
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
2100
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
2101
                       2 * 60 * 60 * 1000);
2102
        Calendar calendar = new GregorianCalendar(pdt);
2103
        Date trialTime = new Date();
2104
        calendar.setTime(trialTime);
2105
        docid.append(calendar.get(Calendar.YEAR));
2106
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
2107
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
2108
        docid.append(calendar.get(Calendar.MINUTE));
2109
        docid.append(calendar.get(Calendar.SECOND));
2110
   	    //sometimes this number is not unique, so we append a random number
2111
    	int random = (new Double(Math.random()*100)).intValue();
2112
    	docid.append(random);
2113
        
2114
        return docid.toString();
2115
    }
2116
}
(10-10/20)