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: leinfelder $'
8
 *     '$Date: 2011-12-08 11:27:45 -0800 (Thu, 08 Dec 2011) $'
9
 * '$Revision: 6751 $'
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.InputStreamReader;
29
import java.io.Reader;
30
import java.io.StringReader;
31
import java.util.Calendar;
32
import java.util.Date;
33
import java.util.GregorianCalendar;
34
import java.util.SimpleTimeZone;
35
import java.util.TimeZone;
36

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

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

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

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

    
82
    private Metacat m;
83

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

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

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

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

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

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

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

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

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

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

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

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

    
191
        return accessBlock;
192

    
193
    }
194

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
408
    /**
409
     * Create a suite of tests to be run together
410
     */
411
    public static Test suite() {
412
        TestSuite suite = new TestSuite();
413
        suite.addTest(new OnlineDataAccessTest("initialize"));
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
        return suite;
426
    }
427

    
428

    
429

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

    
438

    
439
    /**
440
     * Checking the following cases on 2.0.1 version documents:
441
     * when only online data is uploaded by a user and
442
     * -> he tries to read it  - success
443
     * -> he tries to add same docid again  - failure
444
     * -> he tries to update it  - success
445
     * -> he tries to set permissions on it  - success
446
     * -> he tries to delete it  - success
447
     * -> he tries to read it after deleteing - failure
448
     */
449
    public void onlineData201CasesTest_1() {
450
        try {
451
        	debug("\nRunning: onlineData201CasesTest_1");
452
        	
453
        	// upload online data
454
        	onlineDocid = generateDocid();
455
        	m.login(username, password);
456
        	debug("\nUpload online data with id: "+ onlineDocid + ".1");
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
        	debug("\nUpload of (different) online data with same id: "+ onlineDocid + ".1");
465
        	uploadDocid(onlineDocid + ".1",
466
        			onlinetestdatafile2, FAILURE, false);
467

    
468
        	// try to upload another data with updated id
469
        	debug("\nUpload of data with updated id: "+ onlineDocid + ".2");
470

    
471
        	uploadDocid(onlineDocid + ".2",
472
        			onlinetestdatafile2, SUCCESS, false);
473

    
474
        	// try to set the permissions for the uploaded document
475
        	// the docid given is for the online document
476
        	testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert",
477
        			null, null,
478
        			"ecogrid://knb/" + onlineDocid + ".2",
479
        			null, getAccessBlock(anotheruser, true,
480
        					true, false, false, false),
481
        					null, null, null, null);
482
        	newdocid = generateDocid();
483
        	debug("\nInsert of document with id: "+ newdocid + ".1");
484
        	insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
485
        	m.logout();
486

    
487
        	// check if the permissions were set properly, permission is per-revision
488
        	m.login(anotheruser, anotherpassword);
489
        	readDocid(onlineDocid + ".1", FAILURE, true);
490
        	readDocid(onlineDocid + ".2", SUCCESS, false);
491
        	m.logout();
492

    
493
        	m.login(username, password);
494

    
495
        	// delete the document - able to delete .1
496
        	// but not able to delete .2 as no rules 
497
        	// written to access table when a document 
498
        	// is 'uploaded'
499
        	debug("\nDeleting .1 and .2 version of document with ids: "+ onlineDocid);
500
        	deleteDocid(onlineDocid + ".1", SUCCESS, false);
501
        	deleteDocid(onlineDocid + ".2", FAILURE, true);
502

    
503
        	// try to read the documents now
504
        	readDocid(onlineDocid + ".1", FAILURE, true);
505
        	readDocid(onlineDocid + ".2", FAILURE, true);
506

    
507
        	m.logout();
508

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

    
535
            // upload online data
536
            onlineDocid = generateDocid();
537
            m.login(username, password);
538
            uploadDocid(onlineDocid + ".1",
539
                        onlinetestdatafile1, SUCCESS, false);
540

    
541
            // try to read the data
542
            readDocid(onlineDocid + ".1", SUCCESS, false);
543

    
544
            // try to upload another data with same id
545
            uploadDocid(onlineDocid + ".1",
546
                        onlinetestdatafile2, FAILURE, false);
547

    
548
            // try to upload another data with updated id
549
            uploadDocid(onlineDocid + ".2",
550
                        onlinetestdatafile2, SUCCESS, false);
551

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

    
564
            // check if the permissions were set properly
565
            m.login(anotheruser, anotherpassword);
566
            readDocid(onlineDocid + ".1", FAILURE, true);
567
            readDocid(onlineDocid + ".2", SUCCESS, false);
568
            m.logout();
569

    
570
            m.login(username, password);
571

    
572
            // delete the document - able to delete .1
573
            // but not able to delete .2 as no rules 
574
	        // written to access table when a document 
575
	        // is 'uploaded'
576
            deleteDocid(onlineDocid + ".1", SUCCESS, false);
577
            deleteDocid(onlineDocid + ".2", FAILURE, true);
578

    
579
            // try to read the documents now
580
            readDocid(onlineDocid + ".1", FAILURE, true);
581
            readDocid(onlineDocid + ".2", FAILURE, true);
582

    
583
            m.logout();
584

    
585
        }
586
        catch (MetacatAuthException mae) {
587
            fail("Authorization failed:\n" + mae.getMessage());
588
        }
589
        catch (MetacatInaccessibleException mie) {
590
            fail("Metacat Inaccessible:\n" + mie.getMessage());
591
        }
592
        catch (Exception e) {
593
            fail("General exception:\n" + e.getMessage());
594
        }
595
    }
596

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

    
610
            // upload an online document
611
            onlineDocid = generateDocid();
612
            m.login(username, password);
613
            uploadDocid(onlineDocid + ".1",
614
                        onlinetestdatafile1, SUCCESS, false);
615

    
616
            uploadDocid(onlineDocid + ".2",
617
                        onlinetestdatafile2, SUCCESS, false);
618

    
619
            // login as another user
620
            m.logout();
621
            m.login(anotheruser, anotherpassword);
622

    
623
            // try to read the data
624
            readDocid(onlineDocid + ".2", FAILURE, true);
625

    
626
            // try to upload another document with same id
627
            uploadDocid(onlineDocid + ".2",
628
                        onlinetestdatafile2, FAILURE, false);
629

    
630
            // try to upload another document with updated id
631
            uploadDocid(onlineDocid + ".3",
632
                        onlinetestdatafile2, FAILURE, true);
633

    
634

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

    
647
            // delete the document - should not be able to delete .1
648
            // but should be able to delete .2
649
            deleteDocid(onlineDocid + ".1", FAILURE, true);
650
            deleteDocid(onlineDocid + ".2", FAILURE, true);
651

    
652
            m.logout();
653

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

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

    
679
            // upload an online document
680
            onlineDocid = generateDocid();
681
            m.login(username, password);
682
            uploadDocid(onlineDocid + ".1",
683
                        onlinetestdatafile1, SUCCESS, false);
684

    
685
            uploadDocid(onlineDocid + ".2",
686
                        onlinetestdatafile2, SUCCESS, false);
687

    
688
            // login as another user
689
            m.logout();
690
            m.login(anotheruser, anotherpassword);
691

    
692
            // try to read the data
693
            readDocid(onlineDocid + ".2", FAILURE, true);
694

    
695
            // try to upload another document with same id
696
            uploadDocid(onlineDocid + ".2",
697
                        onlinetestdatafile2, FAILURE, false);
698

    
699
            // try to upload another document with updated id
700
            uploadDocid(onlineDocid + ".3",
701
                        onlinetestdatafile2, FAILURE, true);
702

    
703

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

    
716
            // delete the document - should not be able to delete .1
717
            // but should be able to delete .2
718
            deleteDocid(onlineDocid + ".1", FAILURE, true);
719
            deleteDocid(onlineDocid + ".2", FAILURE, true);
720

    
721
            m.logout();
722

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

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

    
753
            /////////Case 1./////////////////////
754
            // upload an online document - read only
755
            onlineDocid = generateDocid();
756
            m.login(username, password);
757
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
758

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

    
770
            // login as another user
771
            m.login(anotheruser, anotherpassword);
772

    
773
            // try to read the online data
774
            readDocid(onlineDocid + ".1", SUCCESS, false);
775

    
776
            // try to upload another data with updated id
777
            uploadDocid(onlineDocid + ".2",
778
                        onlinetestdatafile2, FAILURE, true);
779

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

    
791
            // delete the document
792
            deleteDocid(onlineDocid + ".1", FAILURE, true);
793
            m.logout();
794

    
795
            /////////Case 2/////////////////////
796
            // upload an online document - write only
797
            onlineDocid = generateDocid();
798
            m.login(username, password);
799
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
800

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

    
812
            // login as another user
813
            m.login(anotheruser, anotherpassword);
814

    
815
            // try to read the online data
816
            readDocid(onlineDocid + ".1", FAILURE, true);
817

    
818
            // try to upload another data with updated id
819
            uploadDocid(onlineDocid + ".2",
820
                        onlinetestdatafile2, SUCCESS, false);
821

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

    
833
            // delete the document
834
            deleteDocid(onlineDocid + ".2", FAILURE, true);
835
            m.logout();
836

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

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

    
854
            // login as another user
855
            m.login(anotheruser, anotherpassword);
856

    
857
            // try to read the online data
858
            readDocid(onlineDocid + ".1", FAILURE, true);
859

    
860
            // try to upload another data with updated id
861
            uploadDocid(onlineDocid + ".2",
862
                          onlinetestdatafile2, FAILURE, true);
863

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

    
877
            // delete the document
878
            deleteDocid(onlineDocid + ".1", FAILURE, true);
879
            m.logout();
880

    
881
            /////////Case 4/////////////////////
882
            // upload an online document all
883
            onlineDocid = generateDocid();
884
            m.login(username, password);
885
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
886

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

    
898
            // login as another user
899
            m.login(anotheruser, anotherpassword);
900

    
901
            // try to read the online data
902
            readDocid(onlineDocid + ".1", SUCCESS, false);
903

    
904
            // try to upload another data with updated id
905
            uploadDocid(onlineDocid + ".2",
906
                        onlinetestdatafile2, SUCCESS, false);
907

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

    
919
            m.logout();
920
            // delete the document
921
            deleteDocid(onlineDocid + ".1", FAILURE, false);
922

    
923
            m.logout();
924

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

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

    
955
            /////////Case 1./////////////////////
956
            // upload an online document - read only
957
            onlineDocid = generateDocid();
958
            m.login(username, password);
959
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
960

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

    
972
            // login as another user
973
            m.login(anotheruser, anotherpassword);
974

    
975
            // try to read the online data
976
            readDocid(onlineDocid + ".1", SUCCESS, false);
977

    
978
            // try to upload another data with updated id
979
            uploadDocid(onlineDocid + ".2",
980
                        onlinetestdatafile2, FAILURE, true);
981

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

    
993
            // delete the document
994
            deleteDocid(onlineDocid + ".1", FAILURE, true);
995
            m.logout();
996

    
997
            /////////Case 2/////////////////////
998
            // upload an online document - write only
999
            onlineDocid = generateDocid();
1000
            m.login(username, password);
1001
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1002

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

    
1014
            // login as another user
1015
            m.login(anotheruser, anotherpassword);
1016

    
1017
            // try to read the online data
1018
            readDocid(onlineDocid + ".1", FAILURE, true);
1019

    
1020
            // try to upload another data with updated id
1021
            uploadDocid(onlineDocid + ".2",
1022
                        onlinetestdatafile2, SUCCESS, false);
1023

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

    
1035
            // delete the document
1036
            deleteDocid(onlineDocid + ".2", FAILURE, true);
1037
            m.logout();
1038

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

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

    
1056
            // login as another user
1057
            m.login(anotheruser, anotherpassword);
1058

    
1059
            // try to read the online data
1060
            readDocid(onlineDocid + ".1", FAILURE, true);
1061

    
1062
            // try to upload another data with updated id
1063
            uploadDocid(onlineDocid + ".2",
1064
                          onlinetestdatafile2, FAILURE, true);
1065

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

    
1079
            // delete the document
1080
            deleteDocid(onlineDocid + ".1", FAILURE, true);
1081
            m.logout();
1082

    
1083
            /////////Case 4/////////////////////
1084
            // upload an online document all
1085
            onlineDocid = generateDocid();
1086
            m.login(username, password);
1087
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1088

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

    
1100
            // login as another user
1101
            m.login(anotheruser, anotherpassword);
1102

    
1103
            // try to read the online data
1104
            readDocid(onlineDocid + ".1", SUCCESS, false);
1105

    
1106
            // try to upload another data with updated id
1107
            uploadDocid(onlineDocid + ".2",
1108
                        onlinetestdatafile2, SUCCESS, false);
1109

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

    
1121
            m.logout();
1122
            // delete the document
1123
            deleteDocid(onlineDocid + ".1", FAILURE, false);
1124

    
1125
            m.logout();
1126

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

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

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

    
1199
            /////////Case 2/////////////////////
1200
            // upload an online document - write only
1201
            onlineDocid = generateDocid();
1202
            m.login(username, password);
1203
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1204

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

    
1216
            // login as another user
1217
            m.login(anotheruser, anotherpassword);
1218

    
1219
            // try to read the online data
1220
            readDocid(onlineDocid + ".1", FAILURE, true);
1221

    
1222
            // try to upload another data with updated id
1223
            uploadDocid(onlineDocid + ".2",
1224
                        onlinetestdatafile2, SUCCESS, false);
1225

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

    
1237
            // delete the document
1238
            deleteDocid(onlineDocid + ".2", FAILURE, true);
1239
            m.logout();
1240

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

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

    
1359
            /////////Case 1./////////////////////
1360
            // upload an online document - read only
1361
            onlineDocid = generateDocid();
1362
            m.login(username, password);
1363
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1364

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

    
1376
            // login as another user
1377
            m.login(anotheruser, anotherpassword);
1378

    
1379
            // try to read the online data
1380
            readDocid(onlineDocid + ".1", SUCCESS, false);
1381

    
1382
            // try to upload another data with updated id
1383
            uploadDocid(onlineDocid + ".2",
1384
                        onlinetestdatafile2, FAILURE, true);
1385

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

    
1397
            // delete the document
1398
            deleteDocid(onlineDocid + ".1", FAILURE, true);
1399
            m.logout();
1400

    
1401
            /////////Case 2/////////////////////
1402
            // upload an online document - write only
1403
            onlineDocid = generateDocid();
1404
            m.login(username, password);
1405
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1406

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

    
1418
            // login as another user
1419
            m.login(anotheruser, anotherpassword);
1420

    
1421
            // try to read the online data
1422
            readDocid(onlineDocid + ".1", FAILURE, true);
1423

    
1424
            // try to upload another data with updated id
1425
            uploadDocid(onlineDocid + ".2",
1426
                        onlinetestdatafile2, SUCCESS, false);
1427

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

    
1439
            // delete the document
1440
            deleteDocid(onlineDocid + ".2", FAILURE, true);
1441
            m.logout();
1442

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

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

    
1460
            // login as another user
1461
            m.login(anotheruser, anotherpassword);
1462

    
1463
            // try to read the online data
1464
            readDocid(onlineDocid + ".1", FAILURE, true);
1465

    
1466
            // try to upload another data with updated id
1467
            uploadDocid(onlineDocid + ".2",
1468
                        onlinetestdatafile2, FAILURE, true);
1469

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

    
1483
            // delete the document
1484
            deleteDocid(onlineDocid + ".1", FAILURE, true);
1485
            m.logout();
1486

    
1487
            /////////Case 4/////////////////////
1488
            // upload an online document all
1489
            onlineDocid = generateDocid();
1490
            m.login(username, password);
1491
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1492

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

    
1504
            // login as another user
1505
            m.login(anotheruser, anotherpassword);
1506

    
1507
            // try to read the online data
1508
            readDocid(onlineDocid + ".1", SUCCESS, false);
1509

    
1510
            // try to upload another data with updated id
1511
            uploadDocid(onlineDocid + ".2",
1512
                        onlinetestdatafile2, SUCCESS, false);
1513

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

    
1525
            m.logout();
1526
            // delete the document
1527
            deleteDocid(onlineDocid + ".1", FAILURE, false);
1528

    
1529
            m.logout();
1530

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

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

    
1556
            // upload online data
1557
            onlineDocid = generateDocid();
1558
            m.login(username, password);
1559
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1560

    
1561
            /////////Case 1
1562
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
1563
                                         "ecogrid://knb/" + onlineDocid + ".1",
1564
                                         null, null, null, null, null,
1565
                                         getAccessBlock(anotheruser, true,
1566
                                         true, false, false, false));
1567
            newdocid = generateDocid();
1568
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
1569

    
1570
            /////////Case 2
1571
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
1572
                                         "ecogrid://knb/" + onlineDocid + ".1",
1573
                                         null, null, null, null,
1574
                                         getAccessBlock(anotheruser, true,
1575
                                         true, false, false, false), null);
1576
            newdocid = generateDocid();
1577

    
1578
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1579
            m.logout();
1580

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

    
1643

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

    
1668

    
1669
            // update document
1670
            m.login(username, password);
1671
            testdocument = get201TestEmlDoc("OnlineDataAccessTest: Doing update",
1672
                                         null, null, null,
1673
                                         null, getAccessBlock(anotheruser, true,
1674
                                         true, false, false, false), null, null,
1675
                                         null, null);
1676
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1677
            m.logout();
1678

    
1679

    
1680
            // upload data and update the document
1681
            onlineDocid = generateDocid();
1682
            m.login(username, password);
1683
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1684
            m.logout();
1685

    
1686
            // try to read the online data
1687
            m.login(anotheruser, anotherpassword);
1688
            readDocid(onlineDocid + ".1", FAILURE, true);
1689
            m.logout();
1690

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

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

    
1714
            // try to read the online data
1715
            m.login(anotheruser, anotherpassword);
1716
            readDocid(newdocid + ".4", SUCCESS, false);
1717
            readDocid(onlineDocid + ".1", FAILURE, true);
1718
            m.logout();
1719

    
1720

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

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

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

    
1773

    
1774
            // update document
1775
            m.login(username, password);
1776
            testdocument = get210TestEmlDoc("OnlineDataAccessTest: Doing update",
1777
                                         null, null, null,
1778
                                         null, getAccessBlock(anotheruser, true,
1779
                                         true, false, false, false), null, null,
1780
                                         null, null);
1781
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1782
            m.logout();
1783

    
1784

    
1785
            // upload data and update the document
1786
            onlineDocid = generateDocid();
1787
            m.login(username, password);
1788
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1789
            m.logout();
1790

    
1791
            // try to read the online data
1792
            m.login(anotheruser, anotherpassword);
1793
            readDocid(onlineDocid + ".1", FAILURE, true);
1794
            m.logout();
1795

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

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

    
1819
            // try to read the online data
1820
            m.login(anotheruser, anotherpassword);
1821
            readDocid(newdocid + ".4", SUCCESS, false);
1822
            readDocid(onlineDocid + ".1", FAILURE, true);
1823
            m.logout();
1824

    
1825

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

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

    
1854

    
1855
    /**
1856
     * Insert a document into metacat. The expected result is passed as result
1857
     */
1858

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

    
1897
    /**
1898
     * Insert a document into metacat. The expected result is passed as result
1899
     */
1900

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

    
1937
    /**
1938
     * Update a document in metacat. The expected result is passed as result
1939
     */
1940
    private String updateDocid(String docid, String docText, boolean result,
1941
                               boolean expectedKarmaFailure) {
1942
        String response = null;
1943
        try {
1944
            response = m.update(docid,
1945
                                new StringReader(testdocument), null);
1946

    
1947
            if (result) {
1948
                assertTrue( (response.indexOf("<success>") != -1));
1949
                assertTrue(response.indexOf(docid) != -1);
1950
            }
1951
            else {
1952
                assertTrue( (response.indexOf("<success>") == -1));
1953
            }
1954
            System.err.println(response);
1955
        }
1956
        catch (MetacatInaccessibleException mie) {
1957
            fail("Metacat Inaccessible (in updateDocid):\n" + mie.getMessage());
1958
        }
1959
        catch (InsufficientKarmaException ike) {
1960
            if (!expectedKarmaFailure) {
1961
                fail("Insufficient karma (in updateDocid):\n" + ike.getMessage());
1962
            }
1963
        }
1964
        catch (MetacatException me) {
1965
            if (! (expectedKarmaFailure &&
1966
                   (me.getMessage().indexOf(
1967
                "User tried to update an access module when they don't have \"ALL\" permission") !=
1968
                    -1))) {
1969
                fail("Metacat Error (in updateDocid):\n" + me.getMessage());
1970
            }
1971
        }
1972
        catch (Exception e) {
1973
            fail("General exception (in updateDocid):\n" + e.getMessage());
1974
        }
1975

    
1976
        return response;
1977
    }
1978

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

    
2002
            }
2003
        catch (MetacatException me) {
2004
            if (result) {
2005
                fail("Metacat Error (in deleteDocid):\n" + me.getMessage());
2006
            }
2007
            else {
2008
                System.err.println("Expected Metacat Error (in deleteDocid):\n" + me.getMessage());
2009
            }
2010
        }
2011
        catch (Exception e) {
2012
            fail("General exception (in deleteDocid):\n" + e.getMessage());
2013
        }
2014
    }
2015

    
2016
    /**
2017
     * Read a document from metacat. The expected result is passed as result
2018
     */
2019
    private void readDocid(String docid, boolean result,
2020
                           boolean expextedKarmaFailure) {
2021
        try {
2022
            Reader r = new InputStreamReader(m.read(docid));
2023
            String response = IOUtil.getAsString(r, true);
2024

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

    
2051
    /**
2052
     * Read a document from metacat and check if it is equal to a given string.
2053
     * The expected result is passed as result
2054
     */
2055

    
2056
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
2057
                                         boolean result,
2058
                                         boolean expextedKarmaFailure) {
2059
        try {
2060
            Reader r = new InputStreamReader(m.read(docid));
2061
            String doc = IOUtil.getAsString(r, true);
2062
            if (result) {
2063

    
2064
                if (!testDoc.equals(doc)) {
2065
                    System.out.println("doc    :" + doc);
2066
                    System.out.println("testDoc:" + testDoc);
2067
                }
2068

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

    
2090
    }
2091

    
2092
    /**
2093
     * Create a hopefully unique docid for testing insert and update. Does
2094
     * not include the 'revision' part of the id.
2095
     *
2096
     * @return a String docid based on the current date and time
2097
     */
2098
    private String generateDocid() {
2099
        StringBuffer docid = new StringBuffer(prefix);
2100
        docid.append(".");
2101

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