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: 2016-05-18 09:01:29 -0700 (Wed, 18 May 2016) $'
9
 * '$Revision: 9780 $'
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

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

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

    
74
    private String prefix = "test";
75
    private String newdocid = null;
76
    private String testdocument = "";
77

    
78
    private Metacat m;
79

    
80
    private String testEml_200_Header =
81
        "<?xml version=\"1.0\"?><eml:eml" +
82
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\"" +
83
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
84
        " packageId=\"eml.1.1\" system=\"knb\"" +
85
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\"" +
86
        " scope=\"system\">";
87
    
88
    private String testEml_201_Header =
89
        "<?xml version=\"1.0\"?><eml:eml" +
90
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
91
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
92
        " packageId=\"eml.1.1\" system=\"knb\"" +
93
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
94
        " scope=\"system\">";
95
    
96
    private String testEml_210_Header =
97
        "<?xml version=\"1.0\"?><eml:eml" +
98
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.0\"" +
99
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
100
        " packageId=\"eml.1.1\" system=\"knb\"" +
101
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.0 eml.xsd\"" +
102
        " scope=\"system\">";
103

    
104
    private String testEmlCreatorBlock =
105
        "<creator scope=\"document\">                                       " +
106
        " <individualName>                                                  " +
107
        "    <surName>Smith</surName>                                       " +
108
        " </individualName>                                                 " +
109
        "</creator>                                                         ";
110

    
111
    private String testEmlContactBlock =
112
        "<contact scope=\"document\">                                       " +
113
        " <individualName>                                                  " +
114
        "    <surName>Jackson</surName>                                     " +
115
        " </individualName>                                                 " +
116
        "</contact>                                                         ";
117

    
118
    private String testEmlInlineBlock1 =
119
        "  <admin>                                                          " +
120
        "    <contact>                                                      " +
121
        "      <name>Operator</name>                                        " +
122
        "      <institution>PSI</institution>                               " +
123
        "    </contact>                                                     " +
124
        "  </admin>                                                         ";
125

    
126
    private String testEmlInlineBlock2 =
127
        "  <instrument>                                                     " +
128
        "    <instName>LCQ</instName>                                       " +
129
        "    <source type=\"ESI\"></source>                                 " +
130
        "    <detector type=\"EM\"></detector>                              " +
131
        "  </instrument>                                                    ";
132

    
133

    
134
    /**
135
     * This function returns an access block based on the params passed
136
     */
137
    protected String getAccessBlock(String principal, boolean grantAccess,
138
                                  boolean read, boolean write,
139
                                  boolean changePermission, boolean all) {
140
        String accessBlock = "<access " +
141
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
142
            " order=\"allowFirst\" scope=\"document\">";
143

    
144
        if (grantAccess) {
145
            accessBlock += "<allow>";
146
        }
147
        else {
148
            accessBlock += "<deny>";
149
        }
150

    
151
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
152

    
153
        if (all) {
154
            accessBlock += "<permission>all</permission>";
155
        }
156
        else {
157
            if (read) {
158
                accessBlock += "<permission>read</permission>";
159
            }
160
            if (write) {
161
                accessBlock += "<permission>write</permission>";
162
            }
163
            if (changePermission) {
164
                accessBlock += "<permission>changePermission</permission>";
165
            }
166
        }
167

    
168
        if (grantAccess) {
169
            accessBlock += "</allow>";
170
        }
171
        else {
172
            accessBlock += "</deny>";
173
        }
174
        accessBlock += "</access>";
175

    
176
        return accessBlock;
177

    
178
    }
179

    
180
    /**
181
	 * This function returns a valid eml 2.0 1 document
182
	 */
183
	private String get201TestEmlDoc(String title, String inlineData1, String inlineData2,
184
			String onlineUrl1, String onlineUrl2, String docAccessBlock,
185
			String inlineAccessBlock1, String inlineAccessBlock2,
186
			String onlineAccessBlock1, String onlineAccessBlock2) {
187

    
188
		String testDocument = testEml_201_Header;
189

    
190
		testDocument += "<dataset scope=\"document\"><title>" + title + "</title>"
191
				+ testEmlCreatorBlock;
192

    
193
		// The inline/online level access block sits at the same level as the 
194
		// inline element.
195
		if (inlineData1 != null) {
196
			testDocument += "<distribution scope=\"document\" id=\"inlineEntity1\">"
197
					+ "<inline>" + inlineData1 + "</inline></distribution>";
198
		}
199
		if (inlineData2 != null) {
200
			testDocument = testDocument
201
					+ "<distribution scope=\"document\" id=\"inlineEntity2\">"
202
					+ "<inline>" + inlineData2 + "</inline></distribution>";
203
		}
204
		if (onlineUrl1 != null) {
205
			testDocument = testDocument
206
					+ "<distribution scope=\"document\" id=\"InlineEntity1\">"
207
					+ "<inline><url function=\"download\">" + onlineUrl1
208
					+ "</url></inline></distribution>";
209
		}
210
		if (onlineUrl2 != null) {
211
			testDocument = testDocument
212
					+ "<distribution scope=\"document\" id=\"InlineEntity2\">"
213
					+ "<inline><url function=\"download\">" + onlineUrl2
214
					+ "</url></inline></distribution>";
215
		}
216
		testDocument += testEmlContactBlock;
217
		
218
		// The document level access block sits inside the dataset element.
219
		if (docAccessBlock != null) {
220
			testDocument += docAccessBlock;
221
		}
222

    
223
		testDocument += "</dataset>";
224

    
225
		// The inline access blocks live in additionalMetadata elements at 
226
		// the same level as the dataset.
227
		if (inlineAccessBlock1 != null) {
228
			testDocument += "<additionalMetadata>";
229
			testDocument += "<describes>inlineEntity1</describes>";
230
			testDocument += inlineAccessBlock1;
231
			testDocument += "</additionalMetadata>";
232
		}
233

    
234
		if (inlineAccessBlock2 != null) {
235
			testDocument += "<additionalMetadata>";
236
			testDocument += "<describes>inlineEntity2</describes>";
237
			testDocument += inlineAccessBlock2;
238
			testDocument += "</additionalMetadata>";
239
		}
240

    
241
		if (onlineAccessBlock1 != null) {
242
			testDocument += "<additionalMetadata>";
243
			testDocument += "<describes>InlineEntity1</describes>";
244
			testDocument += onlineAccessBlock1;
245
			testDocument += "</additionalMetadata>";
246
		}
247

    
248
		if (onlineAccessBlock2 != null) {
249
			testDocument += "<additionalMetadata>";
250
			testDocument += "<describes>InlineEntity2</describes>";
251
			testDocument += onlineAccessBlock2;
252
			testDocument += "</additionalMetadata>";
253
		}
254

    
255
		testDocument += "</eml:eml>";
256

    
257
		debug("get201TestEmlDoc returning following document: " + testDocument);
258
		return testDocument;
259
	}
260
    
261
    /**
262
	 * This function returns a valid eml document
263
	 */
264
    private String get210TestEmlDoc(String title, String inlineData1,
265
                                 String inlineData2, String onlineUrl1,
266
                                 String onlineUrl2, String docAccessBlock,
267
                                 String inlineAccessBlock1,
268
                                 String inlineAccessBlock2,
269
                                 String onlineAccessBlock1,
270
                                 String onlineAccessBlock2) {
271

    
272
    	String testDocument = testEml_210_Header;
273
		
274
		// the document level access block sits at the same level and 
275
		// before the dataset element.
276
		if (docAccessBlock != null) {
277
			testDocument += docAccessBlock;
278
		}
279
		
280
        testDocument += "<dataset scope=\"document\"><title>" + title + "</title>"
281
				+ testEmlCreatorBlock  + testEmlContactBlock 
282
				+ "<dataTable>" 
283
				+ "  <entityName>Test Data</entityName>"
284
				+ "  <physical>" 
285
				+ "    <objectName>2.1.0 test physical</objectName>"
286
				+ "    <size unit=\"bytes\">1</size>"
287
				+ "    <characterEncoding>ASCII</characterEncoding>"
288
				+ "    <dataFormat>"
289
				+ "      <textFormat>"
290
				+ "        <numHeaderLines>1</numHeaderLines>"
291
				+ "        <attributeOrientation>column</attributeOrientation>"
292
				+ "        <simpleDelimited>"
293
				+ "          <fieldDelimiter>,</fieldDelimiter>"
294
				+ "        </simpleDelimited>"
295
				+ "      </textFormat>"
296
				+ "    </dataFormat>";
297

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

    
358
		debug("get210TestEmlDoc returning following document: " + testDocument);
359
		return testDocument;
360
	}
361

    
362
    /**
363
	 * Constructor to build the test
364
	 * 
365
	 * @param name
366
	 *            the name of the test method
367
	 */
368
    public InlineDataAccessTest(String name) {
369
        super(name);
370
        newdocid = generateDocid();
371
    }
372

    
373
    /**
374
     * Establish a testing framework by initializing appropriate objects
375
     */
376
    public void setUp() {
377
        try {
378
            System.err.println("Test Metacat: " + metacatUrl);
379
            m = MetacatFactory.createMetacatConnection(metacatUrl);
380
        }
381
        catch (MetacatInaccessibleException mie) {
382
            System.err.println("Metacat is: " + metacatUrl);
383
            fail("Metacat connection failed." + mie.getMessage());
384
        }
385
    }
386

    
387
    /**
388
     * Release any objects after tests are complete
389
     */
390
    public void tearDown() {
391
    }
392

    
393
    /**
394
     * Create a suite of tests to be run together
395
     */
396
    public static Test suite() {
397
        TestSuite suite = new TestSuite();
398
        suite.addTest(new InlineDataAccessTest("initialize"));
399

    
400
        suite.addTest(new InlineDataAccessTest("inlineData201CasesTest_1"));
401
        suite.addTest(new InlineDataAccessTest("inlineData210CasesTest_1"));
402
        suite.addTest(new InlineDataAccessTest("inlineData201CasesTest_2"));
403
        suite.addTest(new InlineDataAccessTest("inlineData210CasesTest_2"));
404
        suite.addTest(new InlineDataAccessTest("inlineData201CasesTest_3"));
405
        suite.addTest(new InlineDataAccessTest("inlineData210CasesTest_3"));
406
        suite.addTest(new InlineDataAccessTest("inlineData201CasesTest_4"));
407
        suite.addTest(new InlineDataAccessTest("inlineData210CasesTest_4"));
408
        suite.addTest(new InlineDataAccessTest("inlineData201CasesTest_5"));
409
        suite.addTest(new InlineDataAccessTest("inlineData210CasesTest_5"));
410
        suite.addTest(new InlineDataAccessTest("inlineData201CasesTest_6"));
411
        suite.addTest(new InlineDataAccessTest("inlineData210CasesTest_6"));
412

    
413
        return suite;
414
    }
415

    
416

    
417
    /**
418
     * Run an initial test that always passes to check that the test
419
     * harness is working.
420
     */
421
    public void initialize() {
422
        assertTrue(1 == 1);
423
    }
424

    
425
    /**
426
     * For EML 2.0.1, checking the following cases:
427
     * when only Inline data is uploaded by a user and
428
     * -> he tries to read it  - success
429
     * -> he tries to add same docid again  - failure
430
     * -> he tries to update it  - success
431
     * -> he removes it and adds it again - success
432
     * -> he tries to delete it  - success
433
     * -> he tries to read it after deleteing - failure
434
     */
435
    public void inlineData201CasesTest_1() {
436
		debug("\nRunning: inlineData201CasesTest_1()");
437
		try {
438
			newdocid = generateDocid();
439
			m.login(username, password);
440

    
441
			// insert a document
442
			testdocument = get201TestEmlDoc("Testing insert", testEmlInlineBlock1,
443
					null, null, null, null, null, null, null, null);
444

    
445
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
446
			readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
447

    
448
			// insert same document again
449
			insertDocid(newdocid + ".1", testdocument, FAILURE, false);
450

    
451
			// update by changing inline data
452
			testdocument = get201TestEmlDoc("Testing update inline",
453
					testEmlInlineBlock2, null, null, null, null, null, null, null, null);
454
			Thread.sleep(2000);
455
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
456
			readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS, true);
457

    
458
			// update by removing inline data
459
			testdocument = get201TestEmlDoc("Testing update inline", null, null,
460
					null, null, null, null, null, null, null);
461
			Thread.sleep(2000);
462
			updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
463
			readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, true);
464

    
465
			// update by introducing inline data
466
			testdocument = get201TestEmlDoc("Testing update inline",
467
					testEmlInlineBlock1, null, null, null, null, null, null, null, null);
468
			Thread.sleep(2000);
469
			updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
470
			readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, true);
471

    
472
			// read inline data only
473
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, SUCCESS,
474
					false);
475

    
476
			// delete the inline data
477
			// sleep needed only in case of inline data - jing said that
478
			// somehow the thread writing data to xml_index takes too much time
479
			// when used with inline data. hence if delete is requested too soon
480
			// database gives an error of xml_index records left with FK to
481
			// xml_document record which is going to be deleted.
482
			Thread.sleep(10000);
483

    
484
			deleteDocid(newdocid + ".4", SUCCESS, false);
485
			readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
486
			m.logout();
487
		} catch (MetacatAuthException mae) {
488
			fail("Authorization failed:\n" + mae.getMessage());
489
		} catch (MetacatInaccessibleException mie) {
490
			fail("Metacat Inaccessible:\n" + mie.getMessage());
491
		} catch (Exception e) {
492
			fail("General exception:\n" + e.getMessage());
493
		}
494
	}
495
    
496
    /**
497
	 * For EML 2.1.0, checking the following cases: when only Inline data is
498
	 * uploaded by a user and -> he tries to read it - success -> he tries to
499
	 * add same docid again - failure -> he tries to update it - success -> he
500
	 * removes it and adds it again - success -> he tries to delete it - success ->
501
	 * he tries to read it after deleteing - failure
502
	 */
503
    public void inlineData210CasesTest_1() {
504
		debug("\nRunning: inlineData210CasesTest_1()");
505
		try {
506
			newdocid = generateDocid();
507
			m.login(username, password);
508

    
509
			// insert a document
510
			testdocument = get210TestEmlDoc("Testing insert", testEmlInlineBlock1,
511
					null, null, null, null, null, null, null, null);
512

    
513
			debug("testdocument: " + testdocument);
514
			
515
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
516
			readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
517

    
518
			// insert same document again
519
			insertDocid(newdocid + ".1", testdocument, FAILURE, false);
520

    
521
			// update by changing inline data
522
			testdocument = get210TestEmlDoc("Testing update inline",
523
					testEmlInlineBlock2, null, null, null, null, null, null, null, null);
524
			Thread.sleep(2000);
525
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
526
			readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS, true);
527

    
528
			// update by removing inline data
529
			testdocument = get210TestEmlDoc("Testing update inline", null, null,
530
					null, null, null, null, null, null, null);
531
			Thread.sleep(2000);
532
			updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
533
			readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, true);
534

    
535
			// update by introducing inline data
536
			testdocument = get210TestEmlDoc("Testing update inline",
537
					testEmlInlineBlock1, null, null, null, null, null, null, null, null);
538
			Thread.sleep(2000);
539
			updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
540
			readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, true);
541

    
542
			// read inline data only
543
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, SUCCESS,
544
					false);
545

    
546
			// delete the inline data
547
			// sleep needed only in case of inline data - jing said that
548
			// somehow the thread writing data to xml_index takes too much time
549
			// when used with inline data. hence if delete is requested too soon
550
			// database gives an error of xml_index records left with FK to
551
			// xml_document record which is going to be deleted.
552
			Thread.sleep(10000);
553

    
554
			deleteDocid(newdocid + ".4", SUCCESS, false);
555
			readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
556
			m.logout();
557
		} catch (MetacatAuthException mae) {
558
			fail("Authorization failed:\n" + mae.getMessage());
559
		} catch (MetacatInaccessibleException mie) {
560
			fail("Metacat Inaccessible:\n" + mie.getMessage());
561
		} catch (Exception e) {
562
			fail("General exception:\n" + e.getMessage());
563
		}
564
	}
565

    
566
    /***************************************************************************
567
	 * For EML 2.0.1, checking the following cases: when only inline data is 
568
	 * uploaded by a user and another user -> tries to read it - failure -> 
569
	 * tries to read inline data only - failure -> tries to update - failure -> 
570
	 * tries to delete it - failure
571
	 */
572
    public void inlineData201CasesTest_2() {
573
		debug("\nRunning: inlineData201CasesTest_2()");
574
		try {
575
			newdocid = generateDocid();
576
			m.login(username, password);
577

    
578
			// insert a document
579
			testdocument = get201TestEmlDoc("Testing insert", testEmlInlineBlock1,
580
					null, null, null, null, null, null, null, null);
581

    
582
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
583
			readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
584

    
585
			// login as another user
586
			m.logout();
587
			m.login(anotheruser, anotherpassword);
588

    
589
			// try to read document or data only
590
			readDocidWhichEqualsDoc(newdocid, testdocument, FAILURE, true);
591
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
592
					true);
593

    
594
			// try to update the document
595
			testdocument = get201TestEmlDoc("Testing insert", testEmlInlineBlock1,
596
					null, null, null, null, null, null, null, null);
597
			Thread.sleep(2000);
598
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
599

    
600
			// try to delete the document
601
			deleteDocid(newdocid + ".1", FAILURE, true);
602
			m.logout();
603

    
604
			// delete the document
605
			m.login(username, password);
606
			deleteDocid(newdocid + ".1", SUCCESS, false);
607
			m.logout();
608

    
609
		} catch (MetacatAuthException mae) {
610
			fail("Authorization failed:\n" + mae.getMessage());
611
		} catch (MetacatInaccessibleException mie) {
612
			fail("Metacat Inaccessible:\n" + mie.getMessage());
613
		} catch (Exception e) {
614
			fail("General exception:\n" + e.getMessage());
615
		}
616
	}
617
    
618
    /***************************************************************************
619
	 * For EML 2.1.0, checking the following cases: when only inline data is 
620
	 * uploaded by a user and another user -> tries to read it - failure -> 
621
	 * tries to read inline data only - failure -> tries to update - failure -> 
622
	 * tries to delete it - failure
623
	 */
624
    public void inlineData210CasesTest_2() {
625
		debug("\nRunning: inlineData210CasesTest_2()");
626
		try {
627
			newdocid = generateDocid();
628
			m.login(username, password);
629

    
630
			// insert a document
631
			testdocument = get210TestEmlDoc("Testing insert", testEmlInlineBlock1,
632
					null, null, null, null, null, null, null, null);
633

    
634
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
635
			readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
636

    
637
			// login as another user
638
			m.logout();
639
			m.login(anotheruser, anotherpassword);
640

    
641
			// try to read document or data only
642
			readDocidWhichEqualsDoc(newdocid, testdocument, FAILURE, true);
643
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
644
					true);
645

    
646
			// try to update the document
647
			testdocument = get210TestEmlDoc("Testing insert", testEmlInlineBlock1,
648
					null, null, null, null, null, null, null, null);
649
			Thread.sleep(2000);
650
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
651

    
652
			// try to delete the document
653
			deleteDocid(newdocid + ".1", FAILURE, true);
654
			m.logout();
655

    
656
			// delete the document
657
			m.login(username, password);
658
			deleteDocid(newdocid + ".1", SUCCESS, false);
659
			m.logout();
660

    
661
		} catch (MetacatAuthException mae) {
662
			fail("Authorization failed:\n" + mae.getMessage());
663
		} catch (MetacatInaccessibleException mie) {
664
			fail("Metacat Inaccessible:\n" + mie.getMessage());
665
		} catch (Exception e) {
666
			fail("General exception:\n" + e.getMessage());
667
		}
668
	}
669

    
670
    /***************************************************************************
671
	 * For EML 2.0.1, checking the following cases: 
672
	 * when only inline data is uploaded by a user with the following different 
673
	 * access controls in another document 1.read 2.write 3.change permission 
674
	 * 4.all And another user tries to do the following: -> tries to read it -> 
675
	 * tries to update it -> tries to set permissions on it -> tries to delete it
676
	 */
677
    public void inlineData201CasesTest_3() {
678
		debug("\nRunning: inlineData201CasesTest_3()");
679
		try {
680

    
681
			// ///////Case 1./////////////////////
682
			// insert an inline document - read only
683
			m.login(username, password);
684
			newdocid = generateDocid();
685

    
686
			// insert a document which gives read access to the inline document
687
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
688
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
689
							true, true, false, false, false), null, null, null, null);
690
			
691
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
692
			m.logout();
693

    
694
			// login as another user
695
			m.login(anotheruser, anotherpassword);
696

    
697
			// try to read the document and the inline data
698
			readDocid(newdocid + ".1", SUCCESS, false);
699
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, SUCCESS,
700
					false);
701

    
702
			// try to update the inline data
703
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing update",
704
					testEmlInlineBlock2, null, null, null, getAccessBlock(anotheruser,
705
							true, true, false, false, false), null, null, null, null);
706
			Thread.sleep(2000);
707
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
708

    
709
			// try to set the permissions for the inline data
710
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
711
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
712
							true, false, false, false, true), null, null, null, null);
713
			Thread.sleep(2000);
714
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
715

    
716
			// try to delete the document
717
			deleteDocid(newdocid + ".1", FAILURE, true);
718
			m.logout();
719

    
720
			// delete the document
721
			m.login(username, password);
722
			deleteDocid(newdocid + ".1", SUCCESS, false);
723
			m.logout();
724

    
725
			// ///////Case 2./////////////////////
726
			// insert an inline document - write only
727
			m.login(username, password);
728
			newdocid = generateDocid();
729

    
730
			// insert a document which gives read access to the inline document
731
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
732
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
733
							true, false, true, false, false), null, null, null, null);
734
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
735
			m.logout();
736

    
737
			// login as another user
738
			m.login(anotheruser, anotherpassword);
739

    
740
			// try to read the document and the inline data
741
			readDocid(newdocid + ".1", FAILURE, true);
742
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
743
					true);
744

    
745
			// try to update the inline data
746
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing update",
747
					testEmlInlineBlock2, null, null, null, getAccessBlock(anotheruser,
748
							true, false, true, false, false), null, null, null, null);
749
			Thread.sleep(2000);
750
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
751

    
752
			// try to set the permissions for the inline data
753
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
754
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
755
							true, false, false, false, true), null, null, null, null);
756
			Thread.sleep(2000);
757
			updateDocid(newdocid + ".3", testdocument, FAILURE, true);
758

    
759
			// try to delete the document
760
			deleteDocid(newdocid + ".2", FAILURE, true);
761
			m.logout();
762

    
763
			// delete the document
764
			m.login(username, password);
765
			deleteDocid(newdocid + ".2", SUCCESS, false);
766
			m.logout();
767

    
768
			// ///////Case 3./////////////////////
769
			// insert an inline document - change permission only
770
			m.login(username, password);
771
			newdocid = generateDocid();
772

    
773
			// insert a document which gives read access to the inline document
774
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
775
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
776
							true, false, false, true, false), null, null, null, null);
777
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
778
			m.logout();
779

    
780
			// login as another user
781
			m.login(anotheruser, anotherpassword);
782

    
783
			// try to read the document and the inline data
784
			readDocid(newdocid + ".1", FAILURE, true);
785
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, SUCCESS,
786
					false);
787

    
788
			// try to update the inline data
789
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing update",
790
					testEmlInlineBlock2, null, null, null, getAccessBlock(anotheruser,
791
							true, false, false, true, false), null, null, null, null);
792
			Thread.sleep(2000);
793
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
794

    
795
			// try to set the permissions for the inline data
796
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
797
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
798
							true, false, false, false, true), null, null, null, null);
799
			// ERRRRRRRRRRRR
800
			// updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
801

    
802
			// try to delete the document
803
			//deleteDocid(newdocid + ".2", FAILURE, true);
804
			//m.logout();
805

    
806
			// delete the document
807
			m.login(username, password);
808
			deleteDocid(newdocid + ".2", SUCCESS, false);
809
			m.logout();
810

    
811
			// ///////Case 4./////////////////////
812
			// insert an inline document - change permission only
813
			m.login(username, password);
814
			newdocid = generateDocid();
815

    
816
			// insert a document which gives read access to the inline document
817
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
818
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
819
							true, false, false, false, true), null, null, null, null);
820
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
821
			m.logout();
822

    
823
			// login as another user
824
			m.login(anotheruser, anotherpassword);
825

    
826
			// try to read the document and the inline data
827
			readDocid(newdocid + ".1", SUCCESS, false);
828
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, SUCCESS,
829
					false);
830

    
831
			// try to update the inline data
832
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing update",
833
					testEmlInlineBlock2, null, null, null, getAccessBlock(anotheruser,
834
							true, false, false, false, true), null, null, null, null);
835
			Thread.sleep(2000);
836
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
837

    
838
			// try to set the permissions for the inline data
839
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
840
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
841
							true, true, true, true, false), null, null, null, null);
842
			Thread.sleep(2000);
843
			updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
844

    
845
			// try to delete the document
846
			// sleep needed only in case of inline data - jing said that
847
			// somehow the thread writing data to xml_index takes too much time
848
			// when used with inline data. hence if delete is requested too soon
849
			// database gives an error of xml_index records left with FK to
850
			// xml_document record which is going to be deleted.
851
			Thread.sleep(10000);
852

    
853
			deleteDocid(newdocid + ".3", SUCCESS, false);
854
			m.logout();
855

    
856
		} catch (MetacatAuthException mae) {
857
			fail("Authorization failed:\n" + mae.getMessage());
858
		} catch (MetacatInaccessibleException mie) {
859
			fail("Metacat Inaccessible:\n" + mie.getMessage());
860
		} catch (Exception e) {
861
			fail("General exception:\n" + e.getMessage());
862
		}
863
	}
864
    
865
    /***************************************************************************
866
	 * For EML 2.1.0, checking the following cases: 
867
	 * when only inline data is uploaded by a user with the following different 
868
	 * access controls in another document 1.read 2.write 3.change permission 
869
	 * 4.all And another user tries to do the following: -> tries to read it -> 
870
	 * tries to update it -> tries to set permissions on it -> tries to delete it
871
	 */
872
    public void inlineData210CasesTest_3() {
873
		debug("\nRunning: inlineData210CasesTest_3()");
874
		try {
875

    
876
			// ///////Case 1./////////////////////
877
			// insert an inline document - read only
878
			debug("Case 1:");
879
			m.login(username, password);
880
			newdocid = generateDocid();
881

    
882
			// insert a document which gives read access to the inline document
883
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
884
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
885
							true, true, false, false, false), null, null, null, null);
886
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
887
			m.logout();
888

    
889
			// login as another user
890
			m.login(anotheruser, anotherpassword);
891

    
892
			// try to read the document and the inline data
893
			readDocid(newdocid + ".1", SUCCESS, false);
894
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, SUCCESS,
895
					false);
896

    
897
			// try to update the inline data
898
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing update",
899
					testEmlInlineBlock2, null, null, null, getAccessBlock(anotheruser,
900
							true, true, false, false, false), null, null, null, null);
901
			Thread.sleep(2000);
902
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
903

    
904
			// try to set the permissions for the inline data
905
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
906
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
907
							true, false, false, false, true), null, null, null, null);
908
			Thread.sleep(2000);
909
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
910

    
911
			// try to delete the document
912
			deleteDocid(newdocid + ".1", FAILURE, true);
913
			m.logout();
914

    
915
			// delete the document
916
			m.login(username, password);
917
			deleteDocid(newdocid + ".1", SUCCESS, false);
918
			m.logout();
919

    
920
			// ///////Case 2./////////////////////
921
			// insert an inline document - write only
922
			debug("Case 2:");
923
			m.login(username, password);
924
			newdocid = generateDocid();
925

    
926
			// insert a document which gives read access to the inline document
927
			debug("Inserting doc: " + newdocid + ".1, which allows document level" 
928
					+ " write access for " + anotheruser + " and expect SUCCESS");
929
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
930
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
931
							true, false, true, false, false), null, null, null, null);
932
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
933
			m.logout();
934

    
935
			// login as another user
936
			m.login(anotheruser, anotherpassword);
937

    
938
			// try to read the document and the inline data
939
			debug("Try to read:" + newdocid + ".1 as user: " + anotheruser 
940
					+ " and expect FAILURE since write access does not imply read access");
941
			readDocid(newdocid + ".1", FAILURE, true);
942
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
943
					true);
944

    
945
			// try to update the inline data
946
			debug("Updating doc: " + newdocid + ".2, with new inline data." 
947
					+ " as: " + anotheruser + " and expect SUCCESS");
948
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing update",
949
					testEmlInlineBlock2, null, null, null, getAccessBlock(anotheruser,
950
							true, false, true, false, false), null, null, null, null);
951
			Thread.sleep(2000);
952
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
953

    
954
			// try to set the permissions for the inline data
955
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
956
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
957
							true, false, false, false, true), null, null, null, null);
958
			Thread.sleep(2000);
959
			updateDocid(newdocid + ".3", testdocument, FAILURE, true);
960

    
961
			// try to delete the document
962
			deleteDocid(newdocid + ".2", FAILURE, true);
963
			m.logout();
964

    
965
			// delete the document
966
			m.login(username, password);
967
			deleteDocid(newdocid + ".2", SUCCESS, false);
968
			m.logout();
969

    
970
			// ///////Case 3./////////////////////
971
			// insert an inline document - change permission only
972
			debug("Case 3:");
973
			m.login(username, password);
974
			newdocid = generateDocid();
975

    
976
			// insert a document which gives read access to the inline document
977
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
978
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
979
							true, false, false, true, false), null, null, null, null);
980
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
981
			m.logout();
982

    
983
			// login as another user
984
			m.login(anotheruser, anotherpassword);
985

    
986
			// try to read the document and the inline data
987
			readDocid(newdocid + ".1", FAILURE, true);
988
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
989
					true);
990

    
991
			// try to update the inline data
992
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing update",
993
					testEmlInlineBlock2, null, null, null, getAccessBlock(anotheruser,
994
							true, false, false, true, false), null, null, null, null);
995
			Thread.sleep(2000);
996
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
997

    
998
			// try to set the permissions for the inline data
999
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1000
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
1001
							true, false, false, false, true), null, null, null, null);
1002
			// ERRRRRRRRRRRR
1003
			// updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1004

    
1005
			// try to delete the document
1006
			//deleteDocid(newdocid + ".2", FAILURE, true);
1007
			//m.logout();
1008

    
1009
			// delete the document
1010
			m.login(username, password);
1011
			deleteDocid(newdocid + ".2", SUCCESS, false);
1012
			m.logout();
1013

    
1014
			// ///////Case 4./////////////////////
1015
			// insert an inline document - change permission only
1016
			debug("Case 4:");
1017
			m.login(username, password);
1018
			newdocid = generateDocid();
1019

    
1020
			// insert a document which gives read access to the inline document
1021
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1022
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
1023
							true, false, false, false, true), null, null, null, null);
1024
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1025
			m.logout();
1026

    
1027
			// login as another user
1028
			m.login(anotheruser, anotherpassword);
1029

    
1030
			// try to read the document and the inline data
1031
			readDocid(newdocid + ".1", SUCCESS, false);
1032
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, SUCCESS,
1033
					false);
1034

    
1035
			// try to update the inline data
1036
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing update",
1037
					testEmlInlineBlock2, null, null, null, getAccessBlock(anotheruser,
1038
							true, false, false, false, true), null, null, null, null);
1039
			Thread.sleep(2000);
1040
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1041

    
1042
			// try to set the permissions for the inline data
1043
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1044
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
1045
							true, true, true, true, false), null, null, null, null);
1046
			Thread.sleep(2000);
1047
			updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1048

    
1049
			// try to delete the document
1050
			// sleep needed only in case of inline data - jing said that
1051
			// somehow the thread writing data to xml_index takes too much time
1052
			// when used with inline data. hence if delete is requested too soon
1053
			// database gives an error of xml_index records left with FK to
1054
			// xml_document record which is going to be deleted.
1055
			Thread.sleep(10000);
1056

    
1057
			deleteDocid(newdocid + ".3", SUCCESS, false);
1058
			m.logout();
1059

    
1060
		} catch (MetacatAuthException mae) {
1061
			fail("Authorization failed:\n" + mae.getMessage());
1062
		} catch (MetacatInaccessibleException mie) {
1063
			fail("Metacat Inaccessible:\n" + mie.getMessage());
1064
		} catch (Exception e) {
1065
			fail("General exception:\n" + e.getMessage());
1066
		}
1067
	}
1068

    
1069
    /***************************************************************************
1070
	 * For EML 2.0.1, checking the following cases: when only Inline data is 
1071
	 * uploaded by a user with the following different access controls specified 
1072
	 * in addiotnal metadata in another document 1.read 2.write 3.change permission 
1073
	 * 4.all And another user tries to do the following: -> tries to read it -> 
1074
	 * tries to update it -> tries to set permissions on it -> tries to delete it
1075
	 */
1076
    public void inlineData201CasesTest_4() {
1077
		debug("\nRunning: inlineData201CasesTest_4()");
1078
		try {
1079
			// ///////Case 1./////////////////////
1080
			// insert an inline document - read only
1081
			m.login(username, password);
1082
			newdocid = generateDocid();
1083

    
1084
			// insert a document which gives read access to the inline document
1085
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1086
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1087
							anotheruser, true, true, false, false, false), null, null,
1088
					null);
1089
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1090
			m.logout();
1091

    
1092
			// login as another user
1093
			m.login(anotheruser, anotherpassword);
1094

    
1095
			// try to read the document and the inline data
1096
			readDocid(newdocid + ".1", FAILURE, true);
1097
			// ERRRRRRRRRRRRRRR
1098
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
1099
					true);
1100

    
1101
			// try to update the inline data
1102
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing update",
1103
					testEmlInlineBlock2, null, null, null, null, getAccessBlock(
1104
							anotheruser, true, true, false, false, false), null, null,
1105
					null);
1106
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
1107

    
1108
			// try to set the permissions for the inline data
1109
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1110
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1111
							anotheruser, true, false, false, false, true), null, null,
1112
					null);
1113
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
1114

    
1115
			// try to delete the document
1116
			deleteDocid(newdocid + ".1", FAILURE, true);
1117
			m.logout();
1118

    
1119
			// delete the document
1120
			m.login(username, password);
1121
			 try
1122
	      {
1123
	        Thread.sleep(5000);
1124
	      }
1125
	      catch(Exception e)
1126
	      {
1127
	        
1128
	      }
1129
			deleteDocid(newdocid + ".1", SUCCESS, false);
1130
			m.logout();
1131

    
1132
			// ///////Case 2./////////////////////
1133
			// insert an inline document - write only
1134
			m.login(username, password);
1135
			newdocid = generateDocid();
1136

    
1137
			// insert a document which gives read access to the inline document
1138
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1139
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1140
							anotheruser, true, false, true, false, false), null, null,
1141
					null);
1142
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1143
			m.logout();
1144

    
1145
			// login as another user
1146
			m.login(anotheruser, anotherpassword);
1147

    
1148
			// try to read the document and the inline data
1149
			readDocid(newdocid + ".1", FAILURE, true);
1150
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
1151
					true);
1152

    
1153
			// try to update the inline data
1154
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing update",
1155
					testEmlInlineBlock2, null, null, null, null, getAccessBlock(
1156
							anotheruser, true, false, true, false, false), null, null,
1157
					null);
1158
			// ERRRRRRRRRRRRRRRRRRRRRRRR
1159
			// updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1160

    
1161
			// try to set the permissions for the inline data
1162
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1163
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1164
							anotheruser, true, false, false, false, true), null, null,
1165
					null);
1166
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
1167

    
1168
			// try to delete the document
1169
			deleteDocid(newdocid + ".1", FAILURE, true);
1170
			m.logout();
1171

    
1172
			// delete the document
1173
			m.login(username, password);
1174
			try
1175
			{
1176
			  Thread.sleep(5000);
1177
			}
1178
			catch(Exception e)
1179
			{
1180
			  
1181
			}
1182
			deleteDocid(newdocid + ".1", SUCCESS, false);
1183
			m.logout();
1184

    
1185
			// ///////Case 3./////////////////////
1186
			// insert an inline document - change permission only
1187
			m.login(username, password);
1188
			newdocid = generateDocid();
1189

    
1190
			// insert a document which gives read access to the inline document
1191
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1192
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1193
							anotheruser, true, false, false, true, false), null, null,
1194
					null);
1195
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1196
			m.logout();
1197

    
1198
			// login as another user
1199
			m.login(anotheruser, anotherpassword);
1200

    
1201
			// try to read the document and the inline data
1202
			readDocid(newdocid + ".1", FAILURE, true);
1203
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
1204
					true);
1205

    
1206
			// try to update the inline data
1207
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing update",
1208
					testEmlInlineBlock2, null, null, null, null, getAccessBlock(
1209
							anotheruser, true, false, false, true, false), null, null,
1210
					null);
1211
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
1212

    
1213
			// try to set the permissions for the inline data
1214
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1215
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1216
							anotheruser, true, false, false, false, true), null, null,
1217
					null);
1218
			// ERRRRRRRRRRRR
1219
			// updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1220

    
1221
			// try to delete the document
1222
			deleteDocid(newdocid + ".1", FAILURE, true);
1223
			m.logout();
1224

    
1225
			// delete the document
1226
			m.login(username, password);
1227
			try
1228
      {
1229
        Thread.sleep(5000);
1230
      }
1231
      catch(Exception e)
1232
      {
1233
        
1234
      }
1235
			deleteDocid(newdocid + ".1", SUCCESS, false);
1236
			m.logout();
1237

    
1238
			// ///////Case 4./////////////////////
1239
			// insert an inline document - change permission only
1240
			m.login(username, password);
1241
			newdocid = generateDocid();
1242

    
1243
			// insert a document which gives read access to the inline document
1244
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1245
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1246
							anotheruser, true, false, false, false, true), null, null,
1247
					null);
1248
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1249
			m.logout();
1250

    
1251
			// login as another user
1252
			m.login(anotheruser, anotherpassword);
1253

    
1254
			// try to read the document and the inline data
1255
			readDocid(newdocid + ".1", FAILURE, true);
1256
			// ERRRRRRRRRRRRRRR
1257
			// readInlineDataWhichEqualsDoc(newdocid + ".1.1",
1258
			// testEmlInlineBlock1, SUCCESS, false);
1259

    
1260
			// try to update the inline data
1261
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing update",
1262
					testEmlInlineBlock2, null, null, null, null, getAccessBlock(
1263
							anotheruser, true, false, false, false, true), null, null,
1264
					null);
1265
			// ERRRRRR
1266
			// updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1267

    
1268
			// try to set the permissions for the inline data
1269
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1270
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1271
							anotheruser, true, true, true, true, false), null, null, null);
1272
			// ERRRRRRRRRR
1273
			// updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1274

    
1275
			// try to delete the document
1276
			deleteDocid(newdocid + ".3", FAILURE, true);
1277
			m.logout();
1278

    
1279
			// delete the document
1280
			m.login(username, password);
1281
			try
1282
      {
1283
        Thread.sleep(5000);
1284
      }
1285
      catch(Exception e)
1286
      {
1287
        
1288
      }
1289
			deleteDocid(newdocid + ".1", SUCCESS, false);
1290
			m.logout();
1291

    
1292
		} catch (MetacatAuthException mae) {
1293
			fail("Authorization failed:\n" + mae.getMessage());
1294
		} catch (MetacatInaccessibleException mie) {
1295
			fail("Metacat Inaccessible:\n" + mie.getMessage());
1296
		} catch (Exception e) {
1297
			fail("General exception:\n" + e.getMessage());
1298
		}
1299
	}
1300
    
1301
    /***************************************************************************
1302
	 * For EML 2.1.0, checking the following cases: when only Inline data is 
1303
	 * uploaded by a user with the following different access controls specified 
1304
	 * in addiotnal metadata in another document 1.read 2.write 3.change permission 
1305
	 * 4.all And another user tries to do the following: -> tries to read it -> 
1306
	 * tries to update it -> tries to set permissions on it -> tries to delete it
1307
	 */
1308
    public void inlineData210CasesTest_4() {
1309
		debug("\nRunning: inlineData210CasesTest_4()");
1310
		try {
1311
			// ///////Case 1./////////////////////
1312
			// insert an inline document - read only
1313
			m.login(username, password);
1314
			newdocid = generateDocid();
1315

    
1316
			// insert a document which gives read access to the inline document
1317
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1318
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1319
							anotheruser, true, true, false, false, false), null, null,
1320
					null);
1321
			debug("testdocument: " + testdocument);
1322
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1323
			m.logout();
1324

    
1325
			// login as another user
1326
			m.login(anotheruser, anotherpassword);
1327

    
1328
			// try to read the document and the inline data
1329
			readDocid(newdocid + ".1", FAILURE, true);
1330
			// ERRRRRRRRRRRRRRR
1331
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
1332
					true);
1333

    
1334
			// try to update the inline data
1335
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing update",
1336
					testEmlInlineBlock2, null, null, null, null, getAccessBlock(
1337
							anotheruser, true, true, false, false, false), null, null,
1338
					null);
1339
			Thread.sleep(2000);
1340
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
1341

    
1342
			// try to set the permissions for the inline data
1343
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1344
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1345
							anotheruser, true, false, false, false, true), null, null,
1346
					null);
1347
			Thread.sleep(2000);
1348
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
1349

    
1350
			// try to delete the document
1351
			deleteDocid(newdocid + ".1", FAILURE, true);
1352
			m.logout();
1353

    
1354
			// delete the document
1355
			m.login(username, password);
1356
			deleteDocid(newdocid + ".1", SUCCESS, false);
1357
			m.logout();
1358

    
1359
			// ///////Case 2./////////////////////
1360
			// insert an inline document - write only
1361
			m.login(username, password);
1362
			newdocid = generateDocid();
1363

    
1364
			// insert a document which gives read access to the inline document
1365
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1366
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1367
							anotheruser, true, false, true, false, false), null, null,
1368
					null);
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 document and the inline data
1376
			readDocid(newdocid + ".1", FAILURE, true);
1377
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
1378
					true);
1379

    
1380
			// try to update the inline data
1381
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing update",
1382
					testEmlInlineBlock2, null, null, null, null, getAccessBlock(
1383
							anotheruser, true, false, true, false, false), null, null,
1384
					null);
1385
			// ERRRRRRRRRRRRRRRRRRRRRRRR
1386
			// updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1387

    
1388
			// try to set the permissions for the inline data
1389
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1390
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1391
							anotheruser, true, false, false, false, true), null, null,
1392
					null);
1393
			Thread.sleep(2000);
1394
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
1395

    
1396
			// try to delete the document
1397
			deleteDocid(newdocid + ".1", FAILURE, true);
1398
			m.logout();
1399

    
1400
			// delete the document
1401
			m.login(username, password);
1402
			deleteDocid(newdocid + ".1", SUCCESS, false);
1403
			m.logout();
1404

    
1405
			// ///////Case 3./////////////////////
1406
			// insert an inline document - change permission only
1407
			m.login(username, password);
1408
			newdocid = generateDocid();
1409

    
1410
			// insert a document which gives read access to the inline document
1411
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1412
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1413
							anotheruser, true, false, false, true, false), null, null,
1414
					null);
1415
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1416
			m.logout();
1417

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

    
1421
			// try to read the document and the inline data
1422
			readDocid(newdocid + ".1", FAILURE, true);
1423
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
1424
					true);
1425

    
1426
			// try to update the inline data
1427
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing update",
1428
					testEmlInlineBlock2, null, null, null, null, getAccessBlock(
1429
							anotheruser, true, false, false, true, false), null, null,
1430
					null);
1431
			Thread.sleep(2000);
1432
			updateDocid(newdocid + ".2", testdocument, FAILURE, true);
1433

    
1434
			// try to set the permissions for the inline data
1435
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1436
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1437
							anotheruser, true, false, false, false, true), null, null,
1438
					null);
1439
			// ERRRRRRRRRRRR
1440
			// updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1441

    
1442
			// try to delete the document
1443
			deleteDocid(newdocid + ".1", FAILURE, true);
1444
			m.logout();
1445

    
1446
			// delete the document
1447
			m.login(username, password);
1448
			try
1449
      {
1450
        Thread.sleep(5000);
1451
      }
1452
      catch(Exception e)
1453
      {
1454
        
1455
      }
1456
			deleteDocid(newdocid + ".1", SUCCESS, false);
1457
			m.logout();
1458

    
1459
			// ///////Case 4./////////////////////
1460
			// insert an inline document - change permission only
1461
			m.login(username, password);
1462
			newdocid = generateDocid();
1463

    
1464
			// insert a document which gives read access to the inline document
1465
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1466
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1467
							anotheruser, true, false, false, false, true), null, null,
1468
					null);
1469
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1470
			m.logout();
1471

    
1472
			// login as another user
1473
			m.login(anotheruser, anotherpassword);
1474

    
1475
			// try to read the document and the inline data
1476
			readDocid(newdocid + ".1", FAILURE, true);
1477
			// ERRRRRRRRRRRRRRR
1478
			// readInlineDataWhichEqualsDoc(newdocid + ".1.1",
1479
			// testEmlInlineBlock1, SUCCESS, false);
1480

    
1481
			// try to update the inline data
1482
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing update",
1483
					testEmlInlineBlock2, null, null, null, null, getAccessBlock(
1484
							anotheruser, true, false, false, false, true), null, null,
1485
					null);
1486
			// ERRRRRR
1487
			// updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1488

    
1489
			// try to set the permissions for the inline data
1490
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1491
					testEmlInlineBlock1, null, null, null, null, getAccessBlock(
1492
							anotheruser, true, true, true, true, false), null, null, null);
1493
			// ERRRRRRRRRR
1494
			// updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1495

    
1496
			// try to delete the document
1497
			deleteDocid(newdocid + ".3", FAILURE, true);
1498
			m.logout();
1499

    
1500
			// delete the document
1501
			m.login(username, password);
1502
			try
1503
      {
1504
        Thread.sleep(5000);
1505
      }
1506
      catch(Exception e)
1507
      {
1508
        
1509
      }
1510
			deleteDocid(newdocid + ".1", SUCCESS, false);
1511
			m.logout();
1512

    
1513
		} catch (MetacatAuthException mae) {
1514
			fail("Authorization failed:\n" + mae.getMessage());
1515
		} catch (MetacatInaccessibleException mie) {
1516
			fail("Metacat Inaccessible:\n" + mie.getMessage());
1517
		} catch (Exception e) {
1518
			fail("General exception:\n" + e.getMessage());
1519
		}
1520
	}
1521

    
1522
    /***************************************************************************
1523
	 * For EML 2.0.1, checking the following cases: -> when no inline data is 
1524
	 * specified in the document but rules are specified in additional metadata -> 
1525
	 * when a user has RW permission for inline data, can he delete it -> when 
1526
	 * inline data with document refering to it is uploaded with read access for 
1527
	 * metadata and no access for data
1528
	 */
1529
    public void inlineData201CasesTest_5() {
1530
		debug("\nRunning: inlineData201CasesTest_5()");
1531
		try {
1532

    
1533
			m.login(username, password);
1534

    
1535
			/////////Case 1
1536
			debug("Case 1:");
1537
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1538
					null, null, null, null, null, getAccessBlock(anotheruser, true, true,
1539
							false, false, false), null, null, null);
1540
			newdocid = generateDocid();
1541

    
1542
			// try to insert the wrong document.  This document is wrong because
1543
			// it has an access block but no distribution.
1544
			insertDocid(newdocid + ".1", testdocument, FAILURE, false);
1545
			m.logout();
1546

    
1547
			/////////Case 2
1548
			debug("Case 2:");
1549
			m.login(username, password);
1550
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1551
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
1552
							true, true, true, false, false), null, null, null, null);
1553
			newdocid = generateDocid();
1554
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1555
			m.logout();
1556

    
1557
			m.login(anotheruser, anotherpassword);
1558
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1559
					null, null, null, null, getAccessBlock(anotheruser, true, true, true,
1560
							false, false), null, null, null, null);
1561
			/// ERRRRRRRRRRRR
1562
			Thread.sleep(2000);
1563
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1564

    
1565
			/////////Case 3
1566
			debug("Case 3:");
1567
			// insert a document
1568
			m.login(username, password);
1569
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1570
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
1571
							true, false, false, false, true), getAccessBlock(anotheruser,
1572
							false, false, false, false, true), null, null, null);
1573
			newdocid = generateDocid();
1574
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1575
			m.logout();
1576

    
1577
			// try to read the Inline data
1578
			m.login(anotheruser, anotherpassword);
1579
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1580
					"", null, null, null, getAccessBlock(anotheruser, true, false, false,
1581
							false, true), getAccessBlock(anotheruser, false, false,
1582
							false, false, true), null, null, null);
1583
			readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS, false);
1584

    
1585
			// try to update the rules for inline data
1586
			// / ERRRRRRRRRRRR it lets you do that
1587
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1588
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
1589
							true, false, false, false, true), getAccessBlock(anotheruser,
1590
							true, false, false, false, true), null, null, null);
1591
			Thread.sleep(2000);
1592
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1593
			readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS, false);
1594

    
1595
			m.logout();
1596
		} catch (MetacatAuthException mae) {
1597
			fail("Authorization failed:\n" + mae.getMessage());
1598
		} catch (MetacatInaccessibleException mie) {
1599
			fail("Metacat Inaccessible:\n" + mie.getMessage());
1600
		} catch (Exception e) {
1601
			fail("General exception:\n" + e.getMessage());
1602
		}
1603
	}
1604
    
1605
    /***************************************************************************
1606
	 * For EML 2.1.0, checking the following cases: -> when no inline data is 
1607
	 * specified in the document but rules are specified in additional metadata -> 
1608
	 * when a user has RW permission for inline data, can he delete it -> when 
1609
	 * inline data with document refering to it is uploaded with read access for 
1610
	 * metadata and no access for data
1611
	 */
1612
    public void inlineData210CasesTest_5() {
1613
		debug("\nRunning: inlineData210CasesTest_5()");
1614
		try {
1615

    
1616
			/////////Case 2
1617
			debug("Case 2:");
1618
			m.login(username, password);
1619
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1620
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
1621
							true, true, true, false, false), null, null, null, null);
1622
			newdocid = generateDocid();
1623
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1624
			m.logout();
1625

    
1626
			m.login(anotheruser, anotherpassword);
1627
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1628
					null, null, null, null, getAccessBlock(anotheruser, true, true, true,
1629
							false, false), null, null, null, null);
1630
			/// ERRRRRRRRRRRR
1631
			Thread.sleep(2000);
1632
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1633

    
1634
			/////////Case 3
1635
			debug("Case 3:");
1636
			// insert a document
1637
			m.login(username, password);
1638
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1639
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
1640
							true, false, false, false, true), getAccessBlock(anotheruser,
1641
							false, false, false, false, true), null, null, null);
1642
			newdocid = generateDocid();
1643
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1644
			m.logout();
1645

    
1646
			// try to read the Inline data
1647
			m.login(anotheruser, anotherpassword);
1648
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1649
					"", null, null, null, getAccessBlock(anotheruser, true, false, false,
1650
							false, true), getAccessBlock(anotheruser, false, false,
1651
							false, false, true), null, null, null);
1652
			readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS, false);
1653

    
1654
			// try to update the rules for inline data
1655
			// shouldn't succeed
1656
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1657
					testEmlInlineBlock1, null, null, null, getAccessBlock(anotheruser,
1658
							true, false, false, false, true), getAccessBlock(anotheruser,
1659
							true, false, false, false, true), null, null, null);
1660
			debug("inserting this: " + testdocument);
1661
			Thread.sleep(2000);
1662
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1663
			readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS, false);
1664

    
1665
			m.logout();
1666
		} catch (MetacatAuthException mae) {
1667
			fail("Authorization failed:\n" + mae.getMessage());
1668
		} catch (MetacatInaccessibleException mie) {
1669
			fail("Metacat Inaccessible:\n" + mie.getMessage());
1670
		} catch (Exception e) {
1671
			fail("General exception:\n" + e.getMessage());
1672
		}
1673
	}
1674

    
1675
    /***************************************************************************
1676
	 * for EML 2.0.1, checking the following cases: -> when inline data is 
1677
	 * inserted and updated, do access rules apply to the old document
1678
	 */
1679
      public void inlineData201CasesTest_6() {
1680
		debug("\nRunning: inlineData201CasesTest_6()");
1681
		try {
1682

    
1683
			// insert the document
1684
			m.login(username, password);
1685
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing insert",
1686
					testEmlInlineBlock1, testEmlInlineBlock2, null, null, getAccessBlock(
1687
							anotheruser, true, true, true, false, false), getAccessBlock(
1688
							anotheruser, false, false, false, false, true),
1689
					getAccessBlock(anotheruser, false, false, false, false, true), null,
1690
					null);
1691
			newdocid = generateDocid();
1692
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1693

    
1694
			m.logout();
1695

    
1696
			// update the document
1697
			m.login(anotheruser, anotherpassword);
1698
			testdocument = get201TestEmlDoc("InlineDataAccessTest: Doing update",
1699
					testEmlInlineBlock2, testEmlInlineBlock1, null, null, getAccessBlock(
1700
							anotheruser, true, true, true, false, false), getAccessBlock(
1701
							anotheruser, false, false, false, false, true),
1702
					getAccessBlock(anotheruser, false, false, false, false, true), null,
1703
					null);
1704
			Thread.sleep(2000);
1705
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1706

    
1707
			// try reading the inline document
1708
			readInlineDataWhichEqualsDoc(newdocid + ".2.1", testEmlInlineBlock1, FAILURE,
1709
					true);
1710

    
1711
			System.out.print("Trying to read " + newdocid + ".1.1");
1712
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
1713
					true);
1714

    
1715
			m.logout();
1716
		} catch (MetacatAuthException mae) {
1717
			fail("Authorization failed:\n" + mae.getMessage());
1718
		} catch (MetacatInaccessibleException mie) {
1719
			fail("Metacat Inaccessible:\n" + mie.getMessage());
1720
		} catch (Exception e) {
1721
			fail("General exception:\n" + e.getMessage());
1722
		}
1723
	}
1724
      
1725
    /***************************************************************************
1726
	 * for EML 2.1.0, checking the following cases: -> when inline data is
1727
	 * inserted and updated, do access rules apply to the old document
1728
	 */
1729
	public void inlineData210CasesTest_6() {
1730
		debug("\nRunning: inlineData210CasesTest_6()");
1731
		try {
1732

    
1733
			// insert the document
1734
			m.login(username, password);
1735
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing insert",
1736
					testEmlInlineBlock1, testEmlInlineBlock2, null, null, getAccessBlock(
1737
							anotheruser, true, true, true, false, false), getAccessBlock(
1738
							anotheruser, false, false, false, false, true),
1739
					getAccessBlock(anotheruser, false, false, false, false, true), null,
1740
					null);
1741
			newdocid = generateDocid();
1742
			insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1743

    
1744
			m.logout();
1745

    
1746
			// update the document
1747
			m.login(anotheruser, anotherpassword);
1748
			testdocument = get210TestEmlDoc("InlineDataAccessTest: Doing update",
1749
					testEmlInlineBlock2, testEmlInlineBlock1, null, null, getAccessBlock(
1750
							anotheruser, true, true, true, false, false), getAccessBlock(
1751
							anotheruser, false, false, false, false, true),
1752
					getAccessBlock(anotheruser, false, false, false, false, true), null,
1753
					null);
1754
			Thread.sleep(2000);
1755
			updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1756

    
1757
			// try reading the inline document
1758
			readInlineDataWhichEqualsDoc(newdocid + ".2.1", testEmlInlineBlock1, FAILURE,
1759
					true);
1760

    
1761
			System.out.print("Trying to read " + newdocid + ".1.1");
1762
			readInlineDataWhichEqualsDoc(newdocid + ".1.1", testEmlInlineBlock1, FAILURE,
1763
					true);
1764

    
1765
			m.logout();
1766
		} catch (MetacatAuthException mae) {
1767
			fail("Authorization failed:\n" + mae.getMessage());
1768
		} catch (MetacatInaccessibleException mie) {
1769
			fail("Metacat Inaccessible:\n" + mie.getMessage());
1770
		} catch (Exception e) {
1771
			fail("General exception:\n" + e.getMessage());
1772
		}
1773
	}
1774

    
1775

    
1776
    /**
1777
	 * Insert a document into metacat. The expected result is passed as result
1778
	 */
1779

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

    
1817
    /**
1818
     * Insert a document into metacat. The expected result is passed as result
1819
     */
1820

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

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

    
1867
            if (result) {
1868
                assertTrue( (response.indexOf("<success>") != -1));
1869
                assertTrue(response.indexOf(docid) != -1);
1870
            }
1871
            else {
1872
                assertTrue( (response.indexOf("<success>") == -1));
1873
            }
1874
            System.err.println(response);
1875
        }
1876
        catch (MetacatInaccessibleException mie) {
1877
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1878
        }
1879
        catch (InsufficientKarmaException ike) {
1880
            if (!expectedKarmaFailure) {
1881
                fail("Insufficient karma:\n" + ike.getMessage());
1882
            }
1883
        }
1884
        catch (MetacatException me) {
1885
            if (! (expectedKarmaFailure &&
1886
                   (me.getMessage().indexOf(
1887
                "User tried to update an access module when they don't have \"ALL\" permission") !=
1888
                    -1))) {
1889
                fail("Metacat Error:\n" + me.getMessage());
1890
            }
1891
        }
1892
        catch (Exception e) {
1893
            fail("General exception:\n" + e.getMessage());
1894
        }
1895

    
1896
        return response;
1897
    }
1898

    
1899
    /**
1900
     * Delete a document into metacat. The expected result is passed as result
1901
     */
1902
    private void deleteDocid(String docid, boolean result,
1903
                             boolean expectedKarmaFailure) {
1904
        try {
1905
            String response = m.delete(docid);
1906
            if (result) {
1907
                assertTrue(response.indexOf("<success>") != -1);
1908
            }
1909
            else {
1910
                assertTrue(response.indexOf("<success>") == -1);
1911
            }
1912
            System.err.println(response);
1913
        }
1914
        catch (MetacatInaccessibleException mie) {
1915
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1916
        }
1917
        catch (InsufficientKarmaException ike) {
1918
            if (!expectedKarmaFailure) {
1919
                fail("Insufficient karma:\n" + ike.getMessage());
1920
            }
1921

    
1922
        }
1923
        catch (MetacatException me) {
1924
            if (result) {
1925
                fail("Metacat Error:\n" + me.getMessage());
1926
            }
1927
            else {
1928
                System.err.println("Metacat Error:\n" + me.getMessage());
1929
            }
1930
        }
1931
        catch (Exception e) {
1932
            fail("General exception:\n" + e.getMessage());
1933
        }
1934
    }
1935

    
1936
    /**
1937
     * Read inline data from metacat. The expected result is passed as result
1938
     */
1939
    private void readInlineDataWhichEqualsDoc(String docid, String testDoc,
1940
                                              boolean result,
1941
                                              boolean expextedKarmaFailure) {
1942
        try {
1943
        	debug("before read, docid: " + docid);
1944
            Reader r = new InputStreamReader(m.readInlineData(docid));
1945
        	debug("after read, docid: " + docid);
1946
            String doc = IOUtil.getAsString(r, true);
1947
        	debug("after get as string, doc: " + doc);
1948

    
1949
            if (result) {
1950
                if (!testDoc.equals(doc)) {
1951
                    System.out.println("doc    :" + doc);
1952
                    System.out.println("testDoc:" + testDoc);
1953
                }
1954

    
1955
                assertTrue(testDoc.equals(doc));
1956
            }
1957
            else {
1958
                System.out.println("doc    :" + doc);
1959
                System.out.println("testDoc:" + testDoc);
1960
                assertTrue(doc.indexOf("<error>") != -1);
1961
            }
1962
        }
1963
        catch (MetacatInaccessibleException mie) {
1964
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1965
        }
1966
        catch (InsufficientKarmaException ike) {
1967
            if (!expextedKarmaFailure) {
1968
                fail("Insufficient karma:\n" + ike.getMessage());
1969
            }
1970
        }
1971
        catch (MetacatException me) {
1972
            if (result) {
1973
                fail("Metacat Error:\n" + me.getMessage());
1974
            }
1975
            else {
1976
                System.err.println("Metacat Error:\n" + me.getMessage());
1977
            }
1978
        }
1979
        catch (Exception e) {
1980
            fail("General exception:\n" + e.getMessage());
1981
        }
1982
    }
1983

    
1984
    /**
1985
     * Read a document from metacat. The expected result is passed as result
1986
     */
1987
    private void readDocid(String docid, boolean result,
1988
                           boolean expextedKarmaFailure) {
1989
        try {
1990
            Reader r = new InputStreamReader(m.read(docid));
1991
            String response = IOUtil.getAsString(r, true);
1992

    
1993
            if (!result) {
1994
                assertTrue(response.indexOf("<success>") == -1);
1995
            }
1996
            System.err.println(response);
1997
        }
1998
        catch (MetacatInaccessibleException mie) {
1999
            fail("Metacat Inaccessible:\n" + mie.getMessage());
2000
        }
2001
        catch (InsufficientKarmaException ike) {
2002
            if (!expextedKarmaFailure) {
2003
                fail("Insufficient karma:\n" + ike.getMessage());
2004
            }
2005
        }
2006
        catch (MetacatException me) {
2007
            if (result) {
2008
                fail("Metacat Error:\n" + me.getMessage());
2009
            }
2010
            else {
2011
                System.err.println("Metacat Error:\n" + me.getMessage());
2012
            }
2013
        }
2014
        catch (Exception e) {
2015
            fail("General exception:\n" + e.getMessage());
2016
        }
2017
    }
2018

    
2019
    /**
2020
     * Read a document from metacat and check if it is equal to a given string.
2021
     * The expected result is passed as result
2022
     */
2023

    
2024
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
2025
                                         boolean result,
2026
                                         boolean expextedKarmaFailure) {
2027
        try {
2028
            Reader r = new InputStreamReader(m.read(docid));
2029
            String doc = IOUtil.getAsString(r, true);
2030
            if (result) {
2031

    
2032
                if (!testDoc.equals(doc)) {
2033
					System.out.println("doc ***********************");
2034
					System.out.println(doc);
2035
					System.out.println("end doc ***********************");
2036
					System.out.println("testDoc ***********************");
2037
					System.out.println(testDoc);
2038
					System.out.println("end testDoc ***********************");
2039
                }
2040

    
2041
                assertTrue(testDoc.equals(doc));
2042
            }
2043
            else {
2044
                assertTrue(doc.indexOf("<error>") != -1);
2045
            }
2046
        }
2047
        catch (MetacatInaccessibleException mie) {
2048
            fail("Metacat Inaccessible:\n" + mie.getMessage());
2049
        }
2050
        catch (InsufficientKarmaException ike) {
2051
        	if (result) {
2052
                fail("Metacat Error:\n" + ike.getMessage());
2053
            }
2054
            else {
2055
                System.out.println("Metacat Error:\n" + ike.getMessage());
2056
            }
2057
        }
2058
        catch (MetacatException me) {
2059
            if (result) {
2060
                fail("Metacat Error:\n" + me.getMessage());
2061
            }
2062
            else {
2063
                System.out.println("Metacat Error:\n" + me.getMessage());
2064
            }
2065
        }
2066
        catch (Exception e) {
2067
            fail("General exception:\n" + e.getMessage());
2068
        }
2069

    
2070
    }
2071

    
2072
    /**
2073
     * Create a hopefully unique docid for testing insert and update. Does
2074
     * not include the 'revision' part of the id.
2075
     *
2076
     * @return a String docid based on the current date and time
2077
     */
2078
    private String generateDocid() {
2079
        StringBuffer docid = new StringBuffer(prefix);
2080
        docid.append(".");
2081

    
2082
        try {
2083
        Thread.sleep(1100);
2084
        } catch (InterruptedException ie) {
2085
        	System.out.println("Warning: generateDocid() could not sleep 1100 ms");
2086
        }
2087
        // Create a calendar to get the date formatted properly
2088
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
2089
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
2090
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
2091
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
2092
                       2 * 60 * 60 * 1000);
2093
        Calendar calendar = new GregorianCalendar(pdt);
2094
        Date trialTime = new Date();
2095
        calendar.setTime(trialTime);
2096
        docid.append(calendar.get(Calendar.YEAR));
2097
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
2098
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
2099
        docid.append(calendar.get(Calendar.MINUTE));
2100
        docid.append(calendar.get(Calendar.SECOND));
2101

    
2102
        return docid.toString();
2103
    }
2104
}
(9-9/30)