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: cjones $'
8
 *     '$Date: 2011-04-24 19:00:41 -0700 (Sun, 24 Apr 2011) $'
9
 * '$Revision: 6043 $'
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

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

    
427
        return suite;
428
    }
429

    
430

    
431

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

    
440

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

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

    
461
            // try to read the data
462
            readDocid(onlineDocid + ".1", SUCCESS, false);
463
                                                                                                       
464
            // try to upload another data with same id
465
            uploadDocid(onlineDocid + ".1",
466
                        onlinetestdatafile2, FAILURE, false);
467

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

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

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

    
490
            m.login(username, password);
491

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

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

    
503
            m.logout();
504

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

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

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

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

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

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

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

    
566
            m.login(username, password);
567

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

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

    
579
            m.logout();
580

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

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

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

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

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

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

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

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

    
630

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

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

    
648
            m.logout();
649

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

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

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

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

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

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

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

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

    
699

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

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

    
717
            m.logout();
718

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
919
            m.logout();
920

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1121
            m.logout();
1122

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1525
            m.logout();
1526

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

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

    
1552
            // upload online data
1553
            onlineDocid = generateDocid();
1554
            m.login(username, password);
1555
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
1556

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

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

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

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

    
1639

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

    
1664

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

    
1675

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

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

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

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

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

    
1716

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

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

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

    
1769

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

    
1780

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

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

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

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

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

    
1821

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

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

    
1850

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

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

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

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

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

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

    
1971
        return response;
1972
    }
1973

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

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

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

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

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

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

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

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

    
2085
    }
2086

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

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