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-07-15 10:16:06 -0700 (Tue, 15 Jul 2008) $'
9
 * '$Revision: 4126 $'
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.metacat.client.InsufficientKarmaException;
37
import edu.ucsb.nceas.metacat.client.Metacat;
38
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
39
import edu.ucsb.nceas.metacat.client.MetacatException;
40
import edu.ucsb.nceas.metacat.client.MetacatFactory;
41
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
42
import edu.ucsb.nceas.metacat.service.PropertyService;
43
import edu.ucsb.nceas.metacat.util.SystemUtil;
44
import edu.ucsb.nceas.utilities.IOUtil;
45
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
46
import junit.framework.Test;
47
import junit.framework.TestCase;
48
import junit.framework.TestSuite;
49
import java.io.File;
50

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

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

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

    
82
    private Metacat m;
83

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

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

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

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

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

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

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

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

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

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

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

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

    
175
        return accessBlock;
176

    
177
    }
178

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

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

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

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

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

    
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>onlineEntity1</describes>";
244
            testDocument += onlineAccessBlock1;
245
            testDocument += "</additionalMetadata>";
246
        }
247

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

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

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

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

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

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

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

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

    
305
        return suite;
306
    }
307

    
308

    
309

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

    
318

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

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

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

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

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

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

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

    
431

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

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

    
449
            m.logout();
450

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

    
463

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

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

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

    
498
            // login as another user
499
            m.login(anotheruser, anotherpassword);
500

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

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

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

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

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

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

    
540
            // login as another user
541
            m.login(anotheruser, anotherpassword);
542

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

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

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

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

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

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

    
582
            // login as another user
583
            m.login(anotheruser, anotherpassword);
584

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

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

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

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

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

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

    
626
            // login as another user
627
            m.login(anotheruser, anotherpassword);
628

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

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

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

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

    
651
            m.logout();
652

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

    
665

    
666

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

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

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

    
701
            // login as another user
702
            m.login(anotheruser, anotherpassword);
703

    
704
            // try to read the online data
705
            readDocid(onlineDocid + ".1", SUCCESS, false);
706

    
707
            // try to upload another data with updated id
708
            uploadDocid(onlineDocid + ".2",
709
                        onlinetestdatafile2, FAILURE, true);
710

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

    
722
            // delete the document
723
            deleteDocid(onlineDocid + ".1", FAILURE, true);
724
            m.logout();
725

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

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

    
743
            // login as another user
744
            m.login(anotheruser, anotherpassword);
745

    
746
            // try to read the online data
747
            readDocid(onlineDocid + ".1", FAILURE, true);
748

    
749
            // try to upload another data with updated id
750
            uploadDocid(onlineDocid + ".2",
751
                        onlinetestdatafile2, SUCCESS, false);
752

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

    
764
            // delete the document
765
            deleteDocid(onlineDocid + ".2", FAILURE, true);
766
            m.logout();
767

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

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

    
785
            // login as another user
786
            m.login(anotheruser, anotherpassword);
787

    
788
            // try to read the online data
789
            readDocid(onlineDocid + ".1", FAILURE, true);
790

    
791
            // try to upload another data with updated id
792
            uploadDocid(onlineDocid + ".2",
793
                        onlinetestdatafile2, FAILURE, true);
794

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

    
808
            // delete the document
809
            deleteDocid(onlineDocid + ".1", FAILURE, true);
810
            m.logout();
811

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

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

    
829
            // login as another user
830
            m.login(anotheruser, anotherpassword);
831

    
832
            // try to read the online data
833
            readDocid(onlineDocid + ".1", SUCCESS, false);
834

    
835
            // try to upload another data with updated id
836
            uploadDocid(onlineDocid + ".2",
837
                        onlinetestdatafile2, SUCCESS, false);
838

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

    
850
            m.logout();
851
            // delete the document
852
            deleteDocid(onlineDocid + ".1", FAILURE, false);
853

    
854
            m.logout();
855

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

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

    
880
            // upload online data
881
            onlineDocid = generateDocid();
882
            m.login(username, password);
883

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

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

    
901
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
902
            m.logout();
903

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

    
916

    
917
    /** *********
918
     * Checking the following cases:
919
     * -> when a document is added with no online data - it is updated (has
920
     *    access)  - then data is added - and a url to it is added and docid
921
     *    is updated - then the access is updated in document
922
     *    does it result in rules being applied on the data
923
     *    (Andrea Chadden was having problem is a similar case)
924
     * -> when online data with document refering to it is uploaded with read
925
     *    access for document and no access for docid and vice versa
926
     */
927
    public void onlineDataCasesTest_6() {
928
        try {
929

    
930
            // insert a document
931
            m.login(username, password);
932
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
933
                                         null, null, null,
934
                                         null, getAccessBlock(anotheruser, true,
935
                                         true, false, false, false), null, null,
936
                                         null, null);
937
            newdocid = generateDocid();
938
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
939
            m.logout();
940

    
941

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

    
952

    
953
            // upload data and update the document
954
            onlineDocid = generateDocid();
955
            m.login(username, password);
956
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
957
            m.logout();
958

    
959
            // try to read the online data
960
            m.login(anotheruser, anotherpassword);
961
            readDocid(onlineDocid + ".1", FAILURE, true);
962
            m.logout();
963

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

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

    
987
            // try to read the online data
988
            m.login(anotheruser, anotherpassword);
989
            readDocid(newdocid + ".4", SUCCESS, false);
990
            readDocid(onlineDocid + ".1", FAILURE, true);
991
            m.logout();
992

    
993

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

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

    
1022

    
1023
    /**
1024
     * Insert a document into metacat. The expected result is passed as result
1025
     */
1026

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

    
1064
    /**
1065
     * Insert a document into metacat. The expected result is passed as result
1066
     */
1067

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

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

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

    
1143
        return response;
1144
    }
1145

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

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

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

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

    
1218
    /**
1219
     * Read a document from metacat and check if it is equal to a given string.
1220
     * The expected result is passed as result
1221
     */
1222

    
1223
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1224
                                         boolean result,
1225
                                         boolean expextedKarmaFailure) {
1226
        try {
1227
            Reader r = m.read(docid);
1228
            String doc = IOUtil.getAsString(r, true);
1229
            if (result) {
1230

    
1231
                if (!testDoc.equals(doc)) {
1232
                    System.out.println("doc    :" + doc);
1233
                    System.out.println("testDoc:" + testDoc);
1234
                }
1235

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

    
1257
    }
1258

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

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