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-08-13 08:59:35 -0700 (Wed, 13 Aug 2008) $'
9
 * '$Revision: 4231 $'
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
    private 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

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

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

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

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

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

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

    
366
            m.login(username, password);
367

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

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

    
379
            m.logout();
380

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

    
393

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

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

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

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

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

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

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

    
430

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

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

    
448
            m.logout();
449

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

    
462

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
650
            m.logout();
651

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

    
664

    
665

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
853
            m.logout();
854

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

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

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

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

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

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

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

    
915

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

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

    
940

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

    
951

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

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

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

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

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

    
992

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

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

    
1021

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

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

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

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

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

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

    
1142
        return response;
1143
    }
1144

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

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

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

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

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

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

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

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

    
1256
    }
1257

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

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