Project

General

Profile

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

    
26
package edu.ucsb.nceas.metacattest;
27

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

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

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

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

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

    
81
    private Metacat m;
82

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

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

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

    
99
    private String testEmlCreatorBlock =
100
        "<creator scope=\"document\">                                       " +
101
        " <individualName>                                                  " +
102
        "    <surName>Smith</surName>                                       " +
103
        " </individualName>                                                 " +
104
        "</creator>                                                         ";
105

    
106
    private String testEmlContactBlock =
107
        "<contact scope=\"document\">                                       " +
108
        " <individualName>                                                  " +
109
        "    <surName>Jackson</surName>                                     " +
110
        " </individualName>                                                 " +
111
        "</contact>                                                         ";
112

    
113
    private String testEmlInlineBlock1 =
114
        "<inline>                                                           " +
115
        "  <admin>                                                          " +
116
        "    <contact>                                                      " +
117
        "      <name>Operator</name>                                        " +
118
        "      <institution>PSI</institution>                               " +
119
        "    </contact>                                                     " +
120
        "  </admin>                                                         " +
121
        "</inline>                                                          ";
122

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

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

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

    
149
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
150

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

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

    
174
        return accessBlock;
175

    
176
    }
177

    
178
    /**
179
     * This function returns a valid eml document with no access rules
180
     * This function is for eml-2.0.1 only. For other eml versions,
181
     * this function might have to modified
182
     */
183
    private String getTestEmlDoc(String title, String inlineData1,
184
                                 String inlineData2, String onlineUrl1,
185
                                 String onlineUrl2, String docAccessBlock,
186
                                 String inlineAccessBlock1,
187
                                 String inlineAccessBlock2,
188
                                 String onlineAccessBlock1,
189
                                 String onlineAccessBlock2) {
190

    
191
        String testDocument = "";
192
        testDocument = testDocument + testEmlHeader +
193
            "<dataset scope=\"document\"><title>" + title + "</title>" +
194
            testEmlCreatorBlock;
195

    
196
        if (inlineData1 != null) {
197
            testDocument = testDocument
198
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
199
                + inlineData1 + "</distribution>";
200
        }
201
        if (inlineData2 != null) {
202
            testDocument = testDocument
203
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
204
                + inlineData2 + "</distribution>";
205
        }
206
        if (onlineUrl1 != null) {
207
            testDocument = testDocument
208
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
209
                + "<online><url function=\"download\">"
210
                + onlineUrl1 + "</url></online></distribution>";
211
        }
212
        if (onlineUrl2 != null) {
213
            testDocument = testDocument +
214
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
215
                + "<online><url function=\"download\">"
216
                + onlineUrl2 + "</url></online></distribution>";
217
        }
218
        testDocument += testEmlContactBlock;
219

    
220
        if (docAccessBlock != null) {
221
            testDocument += docAccessBlock;
222
        }
223

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

    
226
        if (inlineAccessBlock1 != null) {
227
            testDocument += "<additionalMetadata>";
228
            testDocument += "<describes>inlineEntity1</describes>";
229
            testDocument += inlineAccessBlock1;
230
            testDocument += "</additionalMetadata>";
231
        }
232

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

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

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

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

    
256
        //System.out.println("Returning following document" + testDocument);
257
        return testDocument;
258
    }
259

    
260
    /**
261
     * Constructor to build the test
262
     *
263
     * @param name the name of the test method
264
     */
265
    public OnlineDataAccessTest(String name) {
266
        super(name);
267
        newdocid = generateDocid();
268
    }
269

    
270
    /**
271
     * Establish a testing framework by initializing appropriate objects
272
     */
273
    public void setUp() {
274
        try {
275
            System.err.println("Test Metacat: " + metacatUrl);
276
            m = MetacatFactory.createMetacatConnection(metacatUrl);
277
        }
278
        catch (MetacatInaccessibleException mie) {
279
            System.err.println("Metacat is: " + metacatUrl);
280
            fail("Metacat connection failed." + mie.getMessage());
281
        }
282
    }
283

    
284
    /**
285
     * Release any objects after tests are complete
286
     */
287
    public void tearDown() {
288
    }
289

    
290
    /**
291
     * Create a suite of tests to be run together
292
     */
293
    public static Test suite() {
294
        TestSuite suite = new TestSuite();
295
        suite.addTest(new OnlineDataAccessTest("initialize"));
296

    
297
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_1"));
298
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_2"));
299
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_3"));
300
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_4"));
301
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_5"));
302
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_6"));
303

    
304
        return suite;
305
    }
306

    
307

    
308

    
309
    /**
310
     * Run an initial test that always passes to check that the test
311
     * harness is working.
312
     */
313
    public void initialize() {
314
        assertTrue(1 == 1);
315
    }
316

    
317

    
318
    /** *********
319
     * Checking the following cases:
320
     * when only online data is uploaded by a user and
321
     * -> he tries to read it  - success
322
     * -> he tries to add same docid again  - failure
323
     * -> he tries to update it  - success
324
     * -> he tries to set permissions on it  - success
325
     * -> he tries to delete it  - success
326
     * -> he tries to read it after deleteing - failure
327
     */
328
    public void onlineDataCasesTest_1() {
329
        try {
330
			debug("\nRunning: onlineDataCasesTest_1");
331

    
332
            // upload online data
333
            onlineDocid = generateDocid();
334
            m.login(username, password);
335
            uploadDocid(onlineDocid + ".1",
336
                        onlinetestdatafile1, SUCCESS, false);
337

    
338
            // try to read the data
339
            readDocid(onlineDocid + ".1", SUCCESS, false);
340

    
341
            // try to upload another data with same id
342
            uploadDocid(onlineDocid + ".1",
343
                        onlinetestdatafile2, FAILURE, false);
344

    
345
            // try to upload another data with updated id
346
            uploadDocid(onlineDocid + ".2",
347
                        onlinetestdatafile2, SUCCESS, false);
348

    
349
            // try to set the permissions for the uploaded document
350
            // the docid given is for the online document
351
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
352
                                         null, null,
353
                                         "ecogrid://knb/" + onlineDocid + ".1",
354
                                         null, getAccessBlock(anotheruser, true,
355
                true, false, false, false),
356
                                         null, null, null, null);
357
            newdocid = generateDocid();
358
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
359
            m.logout();
360

    
361
            // check if the permissions were set properly
362
            m.login(anotheruser, anotherpassword);
363
            readDocid(onlineDocid + ".1", SUCCESS, false);
364
            readDocid(onlineDocid + ".2", SUCCESS, false);
365
            m.logout();
366

    
367
            m.login(username, password);
368

    
369
            // delete the document - able to delete .1
370
            // but not able to delete .2 as no rules 
371
	    // written to access table when a document 
372
	    // is 'uploaded'
373
            deleteDocid(onlineDocid + ".1", SUCCESS, false);
374
            deleteDocid(onlineDocid + ".2", FAILURE, true);
375

    
376
            // try to read the documents now
377
            readDocid(onlineDocid + ".1", FAILURE, true);
378
            readDocid(onlineDocid + ".2", FAILURE, true);
379

    
380
            m.logout();
381

    
382
        }
383
        catch (MetacatAuthException mae) {
384
            fail("Authorization failed:\n" + mae.getMessage());
385
        }
386
        catch (MetacatInaccessibleException mie) {
387
            fail("Metacat Inaccessible:\n" + mie.getMessage());
388
        }
389
        catch (Exception e) {
390
            fail("General exception:\n" + e.getMessage());
391
        }
392
    }
393

    
394

    
395
    /** *********
396
     * Checking the following cases:
397
     * when only online data is uploaded by a user and another user
398
     * -> tries to read it  - failure
399
     * -> tries to add same docid again  - failure
400
     * -> tries to update it  - failure
401
     * -> tries to set permissions on it  - failure
402
     * -> tries to delete it  - failure
403
     */
404
    public void onlineDataCasesTest_2() {
405
        try {
406
			debug("\nRunning: onlineDataCasesTest_2");
407

    
408
            // upload an online document
409
            onlineDocid = generateDocid();
410
            m.login(username, password);
411
            uploadDocid(onlineDocid + ".1",
412
                        onlinetestdatafile1, SUCCESS, false);
413

    
414
            uploadDocid(onlineDocid + ".2",
415
                        onlinetestdatafile2, SUCCESS, false);
416

    
417
            // login as another user
418
            m.logout();
419
            m.login(anotheruser, anotherpassword);
420

    
421
            // try to read the data
422
            readDocid(onlineDocid + ".2", FAILURE, true);
423

    
424
            // try to upload another document with same id
425
            uploadDocid(onlineDocid + ".2",
426
                        onlinetestdatafile2, FAILURE, false);
427

    
428
            // try to upload another document with updated id
429
            uploadDocid(onlineDocid + ".3",
430
                        onlinetestdatafile2, FAILURE, true);
431

    
432

    
433
            // try to set the permissions for the uploaded document
434
            // the docid given is for the online document
435
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
436
                                         null, null,
437
                                         "ecogrid://knb/" + onlineDocid + ".1",
438
                                         "ecogrid://knb/" + onlineDocid + ".2",
439
                                         getAccessBlock(anotheruser, true,
440
                                         true, false, false, false),
441
                                         null, null, null, null);
442
            newdocid = generateDocid();
443
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
444

    
445
            // delete the document - should not be able to delete .1
446
            // but should be able to delete .2
447
            deleteDocid(onlineDocid + ".1", FAILURE, true);
448
            deleteDocid(onlineDocid + ".2", FAILURE, true);
449

    
450
            m.logout();
451

    
452
        }
453
        catch (MetacatAuthException mae) {
454
            fail("Authorization failed:\n" + mae.getMessage());
455
        }
456
        catch (MetacatInaccessibleException mie) {
457
            fail("Metacat Inaccessible:\n" + mie.getMessage());
458
        }
459
        catch (Exception e) {
460
            fail("General exception:\n" + e.getMessage());
461
        }
462
    }
463

    
464

    
465
    /** *********
466
     * Checking the following cases:
467
     * when only online data is uploaded by a user with the following different
468
     * access controls in another document
469
     *   1.read
470
     *   2.write
471
     *   3.change permission
472
     *   4.all
473
     * And another user tries to do the following:
474
     * -> tries to read it
475
     * -> tries to update it
476
     * -> tries to set permissions on it
477
     * -> tries to delete it
478
     */
479
    public void onlineDataCasesTest_3() {
480
        try {
481
			debug("\nRunning: onlineDataCasesTest_3");
482

    
483
            /////////Case 1./////////////////////
484
            // upload an online document - read only
485
            onlineDocid = generateDocid();
486
            m.login(username, password);
487
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
488

    
489
            // upload a document which gives read access to the online document
490
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
491
                                         null, null,
492
                                         "ecogrid://knb/" + onlineDocid + ".1",
493
                                         null, getAccessBlock(anotheruser, true,
494
                true, false, false, false),
495
                                         null, null, null, null);
496
            newdocid = generateDocid();
497
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
498
            m.logout();
499

    
500
            // login as another user
501
            m.login(anotheruser, anotherpassword);
502

    
503
            // try to read the online data
504
            readDocid(onlineDocid + ".1", SUCCESS, false);
505

    
506
            // try to upload another data with updated id
507
            uploadDocid(onlineDocid + ".2",
508
                        onlinetestdatafile2, FAILURE, true);
509

    
510
            // try to set the permissions for the uploaded document
511
            // the docid given is for the online document
512
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
513
                                         null, null,
514
                                         "ecogrid://knb/" + onlineDocid + ".1",
515
                                         null, getAccessBlock(anotheruser, true,
516
                false, false, false, true),
517
                                         null, null, null, null);
518
            newdocid = generateDocid();
519
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
520

    
521
            // delete the document
522
            deleteDocid(onlineDocid + ".1", FAILURE, true);
523
            m.logout();
524

    
525
            /////////Case 2/////////////////////
526
            // upload an online document - write only
527
            onlineDocid = generateDocid();
528
            m.login(username, password);
529
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
530

    
531
            // upload a document which gives read access to the online document
532
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
533
                                         null, null,
534
                                         "ecogrid://knb/" + onlineDocid + ".1",
535
                                         null, getAccessBlock(anotheruser, true,
536
                false, true, false, false),
537
                                         null, null, null, null);
538
            newdocid = generateDocid();
539
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
540
            m.logout();
541

    
542
            // login as another user
543
            m.login(anotheruser, anotherpassword);
544

    
545
            // try to read the online data
546
            readDocid(onlineDocid + ".1", FAILURE, true);
547

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

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

    
563
            // delete the document
564
            deleteDocid(onlineDocid + ".2", FAILURE, true);
565
            m.logout();
566

    
567
            /////////Case 3/////////////////////
568
            // upload an online document - change permission only
569
            onlineDocid = generateDocid();
570
            m.login(username, password);
571
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
572

    
573
            // upload a document which gives read access to the online document
574
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
575
                                         null, null,
576
                                         "ecogrid://knb/" + onlineDocid + ".1",
577
                                         null, getAccessBlock(anotheruser, true,
578
                                         false, false, true, false),
579
                                         null, null, null, null);
580
            newdocid = generateDocid();
581
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
582
            m.logout();
583

    
584
            // login as another user
585
            m.login(anotheruser, anotherpassword);
586

    
587
            // try to read the online data
588
            readDocid(onlineDocid + ".1", FAILURE, true);
589

    
590
            // try to upload another data with updated id
591
            uploadDocid(onlineDocid + ".2",
592
                          onlinetestdatafile2, FAILURE, true);
593

    
594
            // try to set the permissions for the uploaded document
595
            // the docid given is for the online document
596
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
597
                                         null, null,
598
                                         "ecogrid://knb/" + onlineDocid + ".1",
599
                                         null, getAccessBlock(anotheruser, true,
600
                                         false, false, false, true),
601
                                         null, null, null, null);
602
            newdocid = generateDocid();
603
            // ERRRRRRRRRRRRRRRR
604
            // User does not have permission to update of access rules for data
605
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
606

    
607
            // delete the document
608
            deleteDocid(onlineDocid + ".1", FAILURE, true);
609
            m.logout();
610

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

    
617
            // upload a document which gives read access to the online document
618
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
619
                                         null, null,
620
                                         "ecogrid://knb/" + onlineDocid + ".1",
621
                                         null, getAccessBlock(anotheruser, true,
622
                                         false, false, false, true),
623
                                         null, null, null, null);
624
            newdocid = generateDocid();
625
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
626
            m.logout();
627

    
628
            // login as another user
629
            m.login(anotheruser, anotherpassword);
630

    
631
            // try to read the online data
632
            readDocid(onlineDocid + ".1", SUCCESS, false);
633

    
634
            // try to upload another data with updated id
635
            uploadDocid(onlineDocid + ".2",
636
                        onlinetestdatafile2, SUCCESS, false);
637

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

    
649
            m.logout();
650
            // delete the document
651
            deleteDocid(onlineDocid + ".1", FAILURE, false);
652

    
653
            m.logout();
654

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

    
667

    
668

    
669
    /** *********
670
     * Checking the following cases:
671
     * when only online data is uploaded by a user with the following different
672
     * access controls specified in addiotnal metadata in another document
673
     *   1.read
674
     *   2.write
675
     *   3.change permission
676
     *   4.all
677
     * And another user tries to do the following:
678
     * -> tries to read it
679
     * -> tries to update it
680
     * -> tries to set permissions on it
681
     * -> tries to delete it
682
     */
683
    public void onlineDataCasesTest_4() {
684
        try {
685
			debug("\nRunning: onlineDataCasesTest_4");
686

    
687
            /////////Case 1./////////////////////
688
            // upload an online document - read only
689
            onlineDocid = generateDocid();
690
            m.login(username, password);
691
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
692

    
693
            // upload a document which gives read access to the online document
694
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
695
                                         null, null,
696
                                         "ecogrid://knb/" + onlineDocid + ".1",
697
                                         null, null, null, null,
698
                                         getAccessBlock(anotheruser, true,
699
                                         true, false, false, false), null);
700
            newdocid = generateDocid();
701
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
702
            m.logout();
703

    
704
            // login as another user
705
            m.login(anotheruser, anotherpassword);
706

    
707
            // try to read the online data
708
            readDocid(onlineDocid + ".1", SUCCESS, false);
709

    
710
            // try to upload another data with updated id
711
            uploadDocid(onlineDocid + ".2",
712
                        onlinetestdatafile2, FAILURE, true);
713

    
714
            // try to set the permissions for the uploaded document
715
            // the docid given is for the online document
716
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
717
                                         null, null,
718
                                         "ecogrid://knb/" + onlineDocid + ".1",
719
                                         null, null, null, null,
720
                                         getAccessBlock(anotheruser, true,
721
                                         false, false, false, true), null);
722
            newdocid = generateDocid();
723
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
724

    
725
            // delete the document
726
            deleteDocid(onlineDocid + ".1", FAILURE, true);
727
            m.logout();
728

    
729
            /////////Case 2/////////////////////
730
            // upload an online document - write only
731
            onlineDocid = generateDocid();
732
            m.login(username, password);
733
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
734

    
735
            // upload a document which gives read access to the online document
736
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
737
                                         null, null,
738
                                         "ecogrid://knb/" + onlineDocid + ".1",
739
                                         null, null, null, null,
740
                                         getAccessBlock(anotheruser, true,
741
                                         false, true, false, false), null);
742
            newdocid = generateDocid();
743
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
744
            m.logout();
745

    
746
            // login as another user
747
            m.login(anotheruser, anotherpassword);
748

    
749
            // try to read the online data
750
            readDocid(onlineDocid + ".1", FAILURE, true);
751

    
752
            // try to upload another data with updated id
753
            uploadDocid(onlineDocid + ".2",
754
                        onlinetestdatafile2, SUCCESS, false);
755

    
756
            // try to set the permissions for the uploaded document
757
            // the docid given is for the online document
758
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
759
                                         null, null,
760
                                         "ecogrid://knb/" + onlineDocid + ".1",
761
                                         null, null, null, null,
762
                                         getAccessBlock(anotheruser, true,
763
                                         false, false, false, true), null);
764
            newdocid = generateDocid();
765
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
766

    
767
            // delete the document
768
            deleteDocid(onlineDocid + ".2", FAILURE, true);
769
            m.logout();
770

    
771
            /////////Case 3/////////////////////
772
            // upload an online document - change permission only
773
            onlineDocid = generateDocid();
774
            m.login(username, password);
775
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
776

    
777
            // upload a document which gives read access to the online document
778
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
779
                                         null, null,
780
                                         "ecogrid://knb/" + onlineDocid + ".1",
781
                                         null, null, null, null,
782
                                         getAccessBlock(anotheruser, true,
783
                                         false, false, true, false), null);
784
            newdocid = generateDocid();
785
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
786
            m.logout();
787

    
788
            // login as another user
789
            m.login(anotheruser, anotherpassword);
790

    
791
            // try to read the online data
792
            readDocid(onlineDocid + ".1", FAILURE, true);
793

    
794
            // try to upload another data with updated id
795
            uploadDocid(onlineDocid + ".2",
796
                        onlinetestdatafile2, FAILURE, true);
797

    
798
            // try to set the permissions for the uploaded document
799
            // the docid given is for the online document
800
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
801
                                         null, null,
802
                                         "ecogrid://knb/" + onlineDocid + ".1",
803
                                         null, null, null, null,
804
                                         getAccessBlock(anotheruser, true,
805
                                         false, false, false, true), null);
806
            newdocid = generateDocid();
807
            // ERRRRRRRRRRRRRRRR
808
            // User does not have permission to update of access rules for data
809
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
810

    
811
            // delete the document
812
            deleteDocid(onlineDocid + ".1", FAILURE, true);
813
            m.logout();
814

    
815
            /////////Case 4/////////////////////
816
            // upload an online document all
817
            onlineDocid = generateDocid();
818
            m.login(username, password);
819
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
820

    
821
            // upload a document which gives read access to the online document
822
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
823
                                         null, null,
824
                                         "ecogrid://knb/" + onlineDocid + ".1",
825
                                         null, null, null, null,
826
                                         getAccessBlock(anotheruser, true,
827
                                         false, false, false, true), null);
828
            newdocid = generateDocid();
829
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
830
            m.logout();
831

    
832
            // login as another user
833
            m.login(anotheruser, anotherpassword);
834

    
835
            // try to read the online data
836
            readDocid(onlineDocid + ".1", SUCCESS, false);
837

    
838
            // try to upload another data with updated id
839
            uploadDocid(onlineDocid + ".2",
840
                        onlinetestdatafile2, SUCCESS, false);
841

    
842
            // try to set the permissions for the uploaded document
843
            // the docid given is for the online document
844
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
845
                                         null, null,
846
                                         "ecogrid://knb/" + onlineDocid + ".1",
847
                                         null, null, null, null,
848
                                         getAccessBlock(anotheruser, true,
849
                                         true, false, false, false), null);
850
            newdocid = generateDocid();
851
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
852

    
853
            m.logout();
854
            // delete the document
855
            deleteDocid(onlineDocid + ".1", FAILURE, false);
856

    
857
            m.logout();
858

    
859
        }
860
        catch (MetacatAuthException mae) {
861
            fail("Authorization failed:\n" + mae.getMessage());
862
        }
863
        catch (MetacatInaccessibleException mie) {
864
            fail("Metacat Inaccessible:\n" + mie.getMessage());
865
        }
866
        catch (Exception e) {
867
            fail("General exception:\n" + e.getMessage());
868
        }
869
    }
870

    
871
    /** *********
872
     * Checking the following cases:
873
     * -> when online data with document refering to it is uploaded with
874
     *    rules in additional metadata for an wrong entity id which
875
     *    doesnt exist
876
     * -> when online data with document refering to it is uploaded with
877
     *    rules in additional metadata for an entity which doesnt
878
     *    exist - wrong url
879
     */
880
    public void onlineDataCasesTest_5() {
881
        try {       	
882
			debug("\nRunning: onlineDataCasesTest_5");
883

    
884
            // upload online data
885
            onlineDocid = generateDocid();
886
            m.login(username, password);
887

    
888
            /////////Case 1
889
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
890
                                         "ecogrid://knb/" + onlineDocid + ".1",
891
                                         null, null, null, null, null,
892
                                         getAccessBlock(anotheruser, true,
893
                                         true, false, false, false));
894
            newdocid = generateDocid();
895
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
896

    
897
            /////////Case 2
898
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
899
                                         "ecogrid://knb/" + onlineDocid + ".1",
900
                                         null, null, null, null,
901
                                         getAccessBlock(anotheruser, true,
902
                                         true, false, false, false), null);
903
            newdocid = generateDocid();
904

    
905
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
906
            m.logout();
907

    
908
        }
909
        catch (MetacatAuthException mae) {
910
            fail("Authorization failed:\n" + mae.getMessage());
911
        }
912
        catch (MetacatInaccessibleException mie) {
913
            fail("Metacat Inaccessible:\n" + mie.getMessage());
914
        }
915
        catch (Exception e) {
916
            fail("General exception:\n" + e.getMessage());
917
        }
918
    }
919

    
920

    
921
    /** *********
922
     * Checking the following cases:
923
     * -> when a document is added with no online data - it is updated (has
924
     *    access)  - then data is added - and a url to it is added and docid
925
     *    is updated - then the access is updated in document
926
     *    does it result in rules being applied on the data
927
     *    (Andrea Chadden was having problem is a similar case)
928
     * -> when online data with document refering to it is uploaded with read
929
     *    access for document and no access for docid and vice versa
930
     */
931
    public void onlineDataCasesTest_6() {
932
        try {
933
			debug("\nRunning: onlineDataCasesTest_6");
934
            // insert a document
935
            m.login(username, password);
936
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
937
                                         null, null, null,
938
                                         null, getAccessBlock(anotheruser, true,
939
                                         true, false, false, false), null, null,
940
                                         null, null);
941
            newdocid = generateDocid();
942
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
943
            m.logout();
944

    
945

    
946
            // update document
947
            m.login(username, password);
948
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing update",
949
                                         null, null, null,
950
                                         null, getAccessBlock(anotheruser, true,
951
                                         true, false, false, false), null, null,
952
                                         null, null);
953
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
954
            m.logout();
955

    
956

    
957
            // upload data and update the document
958
            onlineDocid = generateDocid();
959
            m.login(username, password);
960
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
961
            m.logout();
962

    
963
            // try to read the online data
964
            m.login(anotheruser, anotherpassword);
965
            readDocid(onlineDocid + ".1", FAILURE, true);
966
            m.logout();
967

    
968
            // upload data and update the document
969
            m.login(username, password);
970
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing update",
971
                                         null, null,
972
                                         "ecogrid://knb/" + onlineDocid + ".1",
973
                                         null, getAccessBlock(anotheruser, true,
974
                                         true, false, false, false), null, null,
975
                                         null, null);
976
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
977
            m.logout();
978

    
979
            // set read for document - no read for data
980
            m.login(username, password);
981
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
982
                                         null, null,
983
                                         "ecogrid://knb/" + onlineDocid + ".1",
984
                                         null, getAccessBlock(anotheruser, true,
985
                                         true, false, false, false), null, null,
986
                                         getAccessBlock(anotheruser, false,
987
                                         false, false, false, true), null);
988
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
989
            m.logout();
990

    
991
            // try to read the online data
992
            m.login(anotheruser, anotherpassword);
993
            readDocid(newdocid + ".4", SUCCESS, false);
994
            readDocid(onlineDocid + ".1", FAILURE, true);
995
            m.logout();
996

    
997

    
998
            // set read for document - no read for data
999
            m.login(username, password);
1000
            testdocument = getTestEmlDoc("Doing update", null, null,
1001
                                         "ecogrid://knb/" + onlineDocid + ".1",
1002
                                         null, getAccessBlock(anotheruser, false,
1003
                                         false, false, false, true), null, null,
1004
                                         getAccessBlock(anotheruser, true,
1005
                                         true, false, false, false), null);
1006
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1007
            m.logout();
1008

    
1009
            // try to read the online data
1010
            m.login(anotheruser, anotherpassword);
1011
            readDocid(newdocid + ".5", FAILURE, true);
1012
            readDocid(onlineDocid + ".1", SUCCESS, false);
1013
            m.logout();
1014
        }
1015
        catch (MetacatAuthException mae) {
1016
            fail("Authorization failed:\n" + mae.getMessage());
1017
        }
1018
        catch (MetacatInaccessibleException mie) {
1019
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1020
        }
1021
        catch (Exception e) {
1022
            fail("General exception:\n" + e.getMessage());
1023
        }
1024
    }
1025

    
1026

    
1027
    /**
1028
     * Insert a document into metacat. The expected result is passed as result
1029
     */
1030

    
1031
    private String insertDocid(String docid, String docText, boolean result,
1032
                               boolean expectKarmaException) {
1033
        String response = null;
1034
        try {
1035
            response = m.insert(docid,
1036
                                new StringReader(testdocument), null);
1037
            if (result) {
1038
                assertTrue( (response.indexOf("<success>") != -1));
1039
                assertTrue(response.indexOf(docid) != -1);
1040
            }
1041
            else {
1042
                assertTrue( (response.indexOf("<success>") == -1));
1043
            }
1044
            System.err.println(response);
1045
        }
1046
        catch (MetacatInaccessibleException mie) {
1047
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1048
        }
1049
        catch (InsufficientKarmaException ike) {
1050
            if (!expectKarmaException) {
1051
                fail("Insufficient karma:\n" + ike.getMessage());
1052
            }
1053
        }
1054
        catch (MetacatException me) {
1055
            if (result) {
1056
               fail("Metacat Error:\n" + me.getMessage());
1057
           }
1058
           else {
1059
               System.err.println("Metacat Error: " + me.getMessage());
1060
           }
1061
        }
1062
        catch (Exception e) {
1063
            fail("General exception:\n" + e.getMessage());
1064
        }
1065
        return response;
1066
    }
1067

    
1068
    /**
1069
     * Insert a document into metacat. The expected result is passed as result
1070
     */
1071

    
1072
    private String uploadDocid(String docid, String filePath, boolean result,
1073
                               boolean expectedKarmaException) {
1074
        String response = null;
1075
        try {
1076
            response = m.upload(docid, new File(filePath));
1077
            if (result) {
1078
                assertTrue( (response.indexOf("<success>") != -1));
1079
                assertTrue(response.indexOf(docid) != -1);
1080
            }
1081
            else {
1082
                assertTrue( (response.indexOf("<success>") == -1));
1083
            }
1084
            System.err.println("respose from metacat: " + response);
1085
        }
1086
        catch (MetacatInaccessibleException mie) {
1087
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1088
        }
1089
        catch (InsufficientKarmaException ike) {
1090
            if (!expectedKarmaException) {
1091
                fail("Insufficient karma:\n" + ike.getMessage());
1092
            }
1093
        }
1094
        catch (MetacatException me) {
1095
            if (result) {
1096
                fail("Metacat Error:\n" + me.getMessage());
1097
            }
1098
            else {
1099
                System.err.println("Metacat Error: " + me.getMessage());
1100
            }
1101
        }
1102
        catch (Exception e) {
1103
            fail("General exception:\n" + e.getMessage());
1104
        }
1105
        return response;
1106
    }
1107

    
1108
    /**
1109
     * Update a document in metacat. The expected result is passed as result
1110
     */
1111
    private String updateDocid(String docid, String docText, boolean result,
1112
                               boolean expectedKarmaFailure) {
1113
        String response = null;
1114
        try {
1115
            response = m.update(docid,
1116
                                new StringReader(testdocument), null);
1117

    
1118
            if (result) {
1119
                assertTrue( (response.indexOf("<success>") != -1));
1120
                assertTrue(response.indexOf(docid) != -1);
1121
            }
1122
            else {
1123
                assertTrue( (response.indexOf("<success>") == -1));
1124
            }
1125
            System.err.println(response);
1126
        }
1127
        catch (MetacatInaccessibleException mie) {
1128
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1129
        }
1130
        catch (InsufficientKarmaException ike) {
1131
            if (!expectedKarmaFailure) {
1132
                fail("Insufficient karma:\n" + ike.getMessage());
1133
            }
1134
        }
1135
        catch (MetacatException me) {
1136
            if (! (expectedKarmaFailure &&
1137
                   (me.getMessage().indexOf(
1138
                "User tried to update an access module when they don't have \"ALL\" permission") !=
1139
                    -1))) {
1140
                fail("Metacat Error:\n" + me.getMessage());
1141
            }
1142
        }
1143
        catch (Exception e) {
1144
            fail("General exception:\n" + e.getMessage());
1145
        }
1146

    
1147
        return response;
1148
    }
1149

    
1150
    /**
1151
     * Delete a document into metacat. The expected result is passed as result
1152
     */
1153
    private void deleteDocid(String docid, boolean result,
1154
                             boolean expectedKarmaFailure) {
1155
        try {
1156
            String response = m.delete(docid);
1157
            if (result) {
1158
                assertTrue(response.indexOf("<success>") != -1);
1159
            }
1160
            else {
1161
                assertTrue(response.indexOf("<success>") == -1);
1162
            }
1163
            System.err.println(response);
1164
        }
1165
        catch (MetacatInaccessibleException mie) {
1166
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1167
        }
1168
        catch (InsufficientKarmaException ike) {
1169
            if (!expectedKarmaFailure) {
1170
                    fail("Insufficient karma:\n" + ike.getMessage());
1171
                }
1172

    
1173
            }
1174
        catch (MetacatException me) {
1175
            if (result) {
1176
                fail("Metacat Error:\n" + me.getMessage());
1177
            }
1178
            else {
1179
                System.err.println("Metacat Error:\n" + me.getMessage());
1180
            }
1181
        }
1182
        catch (Exception e) {
1183
            fail("General exception:\n" + e.getMessage());
1184
        }
1185
    }
1186

    
1187
    /**
1188
     * Read a document from metacat. The expected result is passed as result
1189
     */
1190
    private void readDocid(String docid, boolean result,
1191
                           boolean expextedKarmaFailure) {
1192
        try {
1193
            Reader r = m.read(docid);
1194
            String response = IOUtil.getAsString(r, true);
1195

    
1196
            if (!result) {
1197
                assertTrue(response.indexOf("<success>") == -1);
1198
            }
1199
            // System.err.println(response);
1200
        }
1201
        catch (MetacatInaccessibleException mie) {
1202
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1203
        }
1204
        catch (InsufficientKarmaException ike) {
1205
            if (!expextedKarmaFailure) {
1206
                fail("Insufficient karma:\n" + ike.getMessage());
1207
            }
1208
        }
1209
        catch (MetacatException me) {
1210
            if (result) {
1211
                fail("Metacat Error:\n" + me.getMessage());
1212
            }
1213
            else {
1214
                System.err.println("Metacat Error:\n" + me.getMessage());
1215
            }
1216
        }
1217
        catch (Exception e) {
1218
            fail("General exception:\n" + e.getMessage());
1219
        }
1220
    }
1221

    
1222
    /**
1223
     * Read a document from metacat and check if it is equal to a given string.
1224
     * The expected result is passed as result
1225
     */
1226

    
1227
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1228
                                         boolean result,
1229
                                         boolean expextedKarmaFailure) {
1230
        try {
1231
            Reader r = m.read(docid);
1232
            String doc = IOUtil.getAsString(r, true);
1233
            if (result) {
1234

    
1235
                if (!testDoc.equals(doc)) {
1236
                    System.out.println("doc    :" + doc);
1237
                    System.out.println("testDoc:" + testDoc);
1238
                }
1239

    
1240
                assertTrue(testDoc.equals(doc));
1241
            }
1242
            else {
1243
                assertTrue(doc.indexOf("<error>") != -1);
1244
            }
1245
        }
1246
        catch (MetacatInaccessibleException mie) {
1247
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1248
        }
1249
        catch (InsufficientKarmaException ike) {
1250
            if (!expextedKarmaFailure) {
1251
                fail("Insufficient karma:\n" + ike.getMessage());
1252
            }
1253
        }
1254
        catch (MetacatException me) {
1255
            fail("Metacat Error:\n" + me.getMessage());
1256
        }
1257
        catch (Exception e) {
1258
            fail("General exception:\n" + e.getMessage());
1259
        }
1260

    
1261
    }
1262

    
1263
    /**
1264
     * Create a hopefully unique docid for testing insert and update. Does
1265
     * not include the 'revision' part of the id.
1266
     *
1267
     * @return a String docid based on the current date and time
1268
     */
1269
    private String generateDocid() {
1270
        StringBuffer docid = new StringBuffer(prefix);
1271
        docid.append(".");
1272

    
1273
        // Create a calendar to get the date formatted properly
1274
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1275
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1276
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1277
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1278
                       2 * 60 * 60 * 1000);
1279
        Calendar calendar = new GregorianCalendar(pdt);
1280
        Date trialTime = new Date();
1281
        calendar.setTime(trialTime);
1282
        docid.append(calendar.get(Calendar.YEAR));
1283
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1284
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1285
        docid.append(calendar.get(Calendar.MINUTE));
1286
        docid.append(calendar.get(Calendar.SECOND));
1287
   	    //sometimes this number is not unique, so we append a random number
1288
    	int random = (new Double(Math.random()*100)).intValue();
1289
    	docid.append(random);
1290
        
1291
        return docid.toString();
1292
    }
1293
}
(10-10/20)