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: sgarg $'
8
 *     '$Date: 2004-08-31 15:47:18 -0700 (Tue, 31 Aug 2004) $'
9
 * '$Revision: 2260 $'
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.utilities.IOUtil;
43
import junit.framework.Test;
44
import junit.framework.TestCase;
45
import junit.framework.TestSuite;
46
import java.io.File;
47

    
48
/**
49
 * A JUnit test for testing Access Control for online data in Metacat
50
 */
51
public class OnlineDataAccessTest
52
    extends TestCase {
53

    
54
    private String metacatUrl = "@systemidserver@@servlet-path@";
55
    private String username = "@mcuser@";
56
    private String password = "@mcpassword@";
57
    private String anotheruser = "@mcanotheruser@";
58
    private String anotherpassword = "@mcanotherpassword@";
59
    private String prefix = "test";
60
    private String newdocid = null;
61
    private String onlineDocid = null;
62
    private String testdocument = "";
63
    private String onlinetestdatafile1 = "test/onlineDataFile1";
64
    private String onlinetestdatafile2 = "test/onlineDataFile2";
65

    
66
    private Metacat m;
67

    
68
    private boolean SUCCESS = true;
69
    private boolean FAILURE = false;
70

    
71
    /**
72
     * These variables are for eml-2.0.1 only. For other eml versions,
73
     * this function might have to modified
74
     */
75

    
76
    private String testEmlHeader =
77
        "<?xml version=\"1.0\"?><eml:eml" +
78
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
79
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
80
        " packageId=\"eml.1.1\" system=\"knb\"" +
81
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
82
        " scope=\"system\">";
83

    
84
    private String testEmlCreatorBlock =
85
        "<creator scope=\"document\">                                       " +
86
        " <individualName>                                                  " +
87
        "    <surName>Smith</surName>                                       " +
88
        " </individualName>                                                 " +
89
        "</creator>                                                         ";
90

    
91
    private String testEmlContactBlock =
92
        "<contact scope=\"document\">                                       " +
93
        " <individualName>                                                  " +
94
        "    <surName>Jackson</surName>                                     " +
95
        " </individualName>                                                 " +
96
        "</contact>                                                         ";
97

    
98
    private String testEmlInlineBlock1 =
99
        "<inline>                                                           " +
100
        "  <admin>                                                          " +
101
        "    <contact>                                                      " +
102
        "      <name>Operator</name>                                        " +
103
        "      <institution>PSI</institution>                               " +
104
        "    </contact>                                                     " +
105
        "  </admin>                                                         " +
106
        "</inline>                                                          ";
107

    
108
    private String testEmlInlineBlock2 =
109
        "<inline>                                                           " +
110
        "  <instrument>                                                     " +
111
        "    <instName>LCQ</instName>                                       " +
112
        "    <source type=\"ESI\"></source>                                 " +
113
        "    <detector type=\"EM\"></detector>                              " +
114
        "  </instrument>                                                    " +
115
        "</inline>                                                          ";
116

    
117
    /**
118
     * This function returns an access block based on the params passed
119
     */
120
    private String getAccessBlock(String principal, boolean grantAccess,
121
                                  boolean read, boolean write,
122
                                  boolean changePermission, boolean all) {
123
        String accessBlock = "<access " +
124
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
125
            " order=\"allowFirst\" scope=\"document\">";
126

    
127
        if (grantAccess) {
128
            accessBlock += "<allow>";
129
        }
130
        else {
131
            accessBlock += "<deny>";
132
        }
133

    
134
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
135

    
136
        if (all) {
137
            accessBlock += "<permission>all</permission>";
138
        }
139
        else {
140
            if (read) {
141
                accessBlock += "<permission>read</permission>";
142
            }
143
            if (write) {
144
                accessBlock += "<permission>write</permission>";
145
            }
146
            if (changePermission) {
147
                accessBlock += "<permission>changePermission</permission>";
148
            }
149
        }
150

    
151
        if (grantAccess) {
152
            accessBlock += "</allow>";
153
        }
154
        else {
155
            accessBlock += "</deny>";
156
        }
157
        accessBlock += "</access>";
158

    
159
        return accessBlock;
160

    
161
    }
162

    
163
    /**
164
     * This function returns a valid eml document with no access rules
165
     * This function is for eml-2.0.1 only. For other eml versions,
166
     * this function might have to modified
167
     */
168
    private String getTestEmlDoc(String title, String inlineData1,
169
                                 String inlineData2, String onlineUrl1,
170
                                 String onlineUrl2, String docAccessBlock,
171
                                 String inlineAccessBlock1,
172
                                 String inlineAccessBlock2,
173
                                 String onlineAccessBlock1,
174
                                 String onlineAccessBlock2) {
175

    
176
        String testDocument = "";
177
        testDocument = testDocument + testEmlHeader +
178
            "<dataset scope=\"document\"><title>" + title + "</title>" +
179
            testEmlCreatorBlock;
180

    
181
        if (inlineData1 != null) {
182
            testDocument = testDocument
183
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
184
                + inlineData1 + "</distribution>";
185
        }
186
        if (inlineData2 != null) {
187
            testDocument = testDocument
188
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
189
                + inlineData2 + "</distribution>";
190
        }
191
        if (onlineUrl1 != null) {
192
            testDocument = testDocument
193
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
194
                + "<online><url function=\"download\">"
195
                + onlineUrl1 + "</url></online></distribution>";
196
        }
197
        if (onlineUrl2 != null) {
198
            testDocument = testDocument +
199
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
200
                + "<online><url function=\"download\">"
201
                + onlineUrl2 + "</url></online></distribution>";
202
        }
203
        testDocument += testEmlContactBlock;
204

    
205
        if (docAccessBlock != null) {
206
            testDocument += docAccessBlock;
207
        }
208

    
209
        testDocument += "</dataset>";
210

    
211
        if (inlineAccessBlock1 != null) {
212
            testDocument += "<additionalMetadata>";
213
            testDocument += "<describes>inlineEntity1</describes>";
214
            testDocument += inlineAccessBlock1;
215
            testDocument += "</additionalMetadata>";
216
        }
217

    
218
        if (inlineAccessBlock2 != null) {
219
            testDocument += "<additionalMetadata>";
220
            testDocument += "<describes>inlineEntity2</describes>";
221
            testDocument += inlineAccessBlock2;
222
            testDocument += "</additionalMetadata>";
223
        }
224

    
225
        if (onlineAccessBlock1 != null) {
226
            testDocument += "<additionalMetadata>";
227
            testDocument += "<describes>onlineEntity1</describes>";
228
            testDocument += onlineAccessBlock1;
229
            testDocument += "</additionalMetadata>";
230
        }
231

    
232
        if (onlineAccessBlock2 != null) {
233
            testDocument += "<additionalMetadata>";
234
            testDocument += "<describes>onlineEntity2</describes>";
235
            testDocument += onlineAccessBlock2;
236
            testDocument += "</additionalMetadata>";
237
        }
238

    
239
        testDocument += "</eml:eml>";
240

    
241
        //System.out.println("Returning following document" + testDocument);
242
        return testDocument;
243
    }
244

    
245
    /**
246
     * Constructor to build the test
247
     *
248
     * @param name the name of the test method
249
     */
250
    public OnlineDataAccessTest(String name) {
251
        super(name);
252
        newdocid = generateDocid();
253
    }
254

    
255
    /**
256
     * Establish a testing framework by initializing appropriate objects
257
     */
258
    public void setUp() {
259
        try {
260
            System.err.println("Test Metacat: " + metacatUrl);
261
            m = MetacatFactory.createMetacatConnection(metacatUrl);
262
        }
263
        catch (MetacatInaccessibleException mie) {
264
            System.err.println("Metacat is: " + metacatUrl);
265
            fail("Metacat connection failed." + mie.getMessage());
266
        }
267
    }
268

    
269
    /**
270
     * Release any objects after tests are complete
271
     */
272
    public void tearDown() {
273
    }
274

    
275
    /**
276
     * Create a suite of tests to be run together
277
     */
278
    public static Test suite() {
279
        TestSuite suite = new TestSuite();
280
        suite.addTest(new OnlineDataAccessTest("initialize"));
281

    
282
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_1"));
283
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_2"));
284
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_3"));
285
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_4"));
286
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_5"));
287
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_6"));
288

    
289
        return suite;
290
    }
291

    
292

    
293

    
294
    /**
295
     * Run an initial test that always passes to check that the test
296
     * harness is working.
297
     */
298
    public void initialize() {
299
        assertTrue(1 == 1);
300
    }
301

    
302

    
303
    /** *********
304
     * Checking the following cases:
305
     * when only online data is uploaded by a user and
306
     * -> he tries to read it  - success
307
     * -> he tries to add same docid again  - failure
308
     * -> he tries to update it  - success
309
     * -> he tries to set permissions on it  - success
310
     * -> he tries to delete it  - success
311
     * -> he tries to read it after deleteing - failure
312
     */
313
    public void onlineDataCasesTest_1() {
314
        try {
315

    
316
            // upload online data
317
            onlineDocid = generateDocid();
318
            m.login(username, password);
319
            uploadDocid(onlineDocid + ".1",
320
                        onlinetestdatafile1, SUCCESS, false);
321

    
322
            // try to read the data
323
            readDocid(onlineDocid + ".1", SUCCESS, false);
324

    
325
            // try to upload another data with same id
326
            uploadDocid(onlineDocid + ".1",
327
                        onlinetestdatafile2, FAILURE, false);
328

    
329
            // try to upload another data with updated id
330
            uploadDocid(onlineDocid + ".2",
331
                        onlinetestdatafile2, SUCCESS, false);
332

    
333
            // try to set the permissions for the uploaded document
334
            // the docid given is for the online document
335
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
336
                                         null, null,
337
                                         "ecogrid://knb/" + onlineDocid + ".1",
338
                                         null, getAccessBlock(anotheruser, true,
339
                true, false, false, false),
340
                                         null, null, null, null);
341
            newdocid = generateDocid();
342
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
343
            m.logout();
344

    
345
            // check if the permissions were set properly
346
            m.login(anotheruser, anotherpassword);
347
            readDocid(onlineDocid + ".1", SUCCESS, false);
348
            readDocid(onlineDocid + ".2", SUCCESS, false);
349
            m.logout();
350

    
351
            m.login(username, password);
352

    
353
            // delete the document - should not be able to delete .1
354
            // but should be able to delete .2
355
            deleteDocid(onlineDocid + ".1", FAILURE, false);
356
            deleteDocid(onlineDocid + ".2", SUCCESS, false);
357

    
358
            // try to read the documents now
359
            readDocid(onlineDocid + ".1", FAILURE, false);
360
            readDocid(onlineDocid + ".2", FAILURE, false);
361

    
362
            m.logout();
363

    
364
        }
365
        catch (MetacatAuthException mae) {
366
            fail("Authorization failed:\n" + mae.getMessage());
367
        }
368
        catch (MetacatInaccessibleException mie) {
369
            fail("Metacat Inaccessible:\n" + mie.getMessage());
370
        }
371
        catch (Exception e) {
372
            fail("General exception:\n" + e.getMessage());
373
        }
374
    }
375

    
376

    
377
    /** *********
378
     * Checking the following cases:
379
     * when only online data is uploaded by a user and another user
380
     * -> tries to read it  - failure
381
     * -> tries to add same docid again  - failure
382
     * -> tries to update it  - failure
383
     * -> tries to set permissions on it  - failure
384
     * -> tries to delete it  - failure
385
     */
386
    public void onlineDataCasesTest_2() {
387
        try {
388

    
389
            // upload an online document
390
            onlineDocid = generateDocid();
391
            m.login(username, password);
392
            uploadDocid(onlineDocid + ".1",
393
                        onlinetestdatafile1, SUCCESS, false);
394

    
395
            uploadDocid(onlineDocid + ".2",
396
                        onlinetestdatafile2, SUCCESS, false);
397

    
398
            // login as another user
399
            m.logout();
400
            m.login(anotheruser, anotherpassword);
401

    
402
            // try to read the data
403
            readDocid(onlineDocid + ".2", FAILURE, true);
404

    
405
            // try to upload another document with same id
406
            uploadDocid(onlineDocid + ".2",
407
                        onlinetestdatafile2, FAILURE, false);
408

    
409
            // try to upload another document with updated id
410
            uploadDocid(onlineDocid + ".3",
411
                        onlinetestdatafile2, FAILURE, true);
412

    
413

    
414
            // try to set the permissions for the uploaded document
415
            // the docid given is for the online document
416
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
417
                                         null, null,
418
                                         "ecogrid://knb/" + onlineDocid + ".1",
419
                                         "ecogrid://knb/" + onlineDocid + ".2",
420
                                         getAccessBlock(anotheruser, true,
421
                                         true, false, false, false),
422
                                         null, null, null, null);
423
            newdocid = generateDocid();
424
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
425

    
426
            // delete the document - should not be able to delete .1
427
            // but should be able to delete .2
428
            deleteDocid(onlineDocid + ".1", FAILURE, false);
429
            deleteDocid(onlineDocid + ".2", FAILURE, true);
430

    
431
            m.logout();
432

    
433
        }
434
        catch (MetacatAuthException mae) {
435
            fail("Authorization failed:\n" + mae.getMessage());
436
        }
437
        catch (MetacatInaccessibleException mie) {
438
            fail("Metacat Inaccessible:\n" + mie.getMessage());
439
        }
440
        catch (Exception e) {
441
            fail("General exception:\n" + e.getMessage());
442
        }
443
    }
444

    
445

    
446
    /** *********
447
     * Checking the following cases:
448
     * when only online data is uploaded by a user with the following different
449
     * access controls in another document
450
     *   1.read
451
     *   2.write
452
     *   3.change permission
453
     *   4.all
454
     * And another user tries to do the following:
455
     * -> tries to read it
456
     * -> tries to update it
457
     * -> tries to set permissions on it
458
     * -> tries to delete it
459
     */
460
    public void onlineDataCasesTest_3() {
461
        try {
462

    
463
            /////////Case 1./////////////////////
464
            // upload an online document - read only
465
            onlineDocid = generateDocid();
466
            m.login(username, password);
467
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
468

    
469
            // upload a document which gives read access to the online document
470
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
471
                                         null, null,
472
                                         "ecogrid://knb/" + onlineDocid + ".1",
473
                                         null, getAccessBlock(anotheruser, true,
474
                true, false, false, false),
475
                                         null, null, null, null);
476
            newdocid = generateDocid();
477
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
478
            m.logout();
479

    
480
            // login as another user
481
            m.login(anotheruser, anotherpassword);
482

    
483
            // try to read the online data
484
            readDocid(onlineDocid + ".1", SUCCESS, false);
485

    
486
            // try to upload another data with updated id
487
            uploadDocid(onlineDocid + ".2",
488
                        onlinetestdatafile2, FAILURE, true);
489

    
490
            // try to set the permissions for the uploaded document
491
            // the docid given is for the online document
492
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
493
                                         null, null,
494
                                         "ecogrid://knb/" + onlineDocid + ".1",
495
                                         null, getAccessBlock(anotheruser, true,
496
                false, false, false, true),
497
                                         null, null, null, null);
498
            newdocid = generateDocid();
499
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
500

    
501
            // delete the document
502
            deleteDocid(onlineDocid + ".1", FAILURE, true);
503
            m.logout();
504

    
505
            /////////Case 2/////////////////////
506
            // upload an online document - write only
507
            onlineDocid = generateDocid();
508
            m.login(username, password);
509
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
510

    
511
            // upload a document which gives read access to the online document
512
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
513
                                         null, null,
514
                                         "ecogrid://knb/" + onlineDocid + ".1",
515
                                         null, getAccessBlock(anotheruser, true,
516
                false, true, false, false),
517
                                         null, null, null, null);
518
            newdocid = generateDocid();
519
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
520
            m.logout();
521

    
522
            // login as another user
523
            m.login(anotheruser, anotherpassword);
524

    
525
            // try to read the online data
526
            readDocid(onlineDocid + ".1", FAILURE, true);
527

    
528
            // try to upload another data with updated id
529
            uploadDocid(onlineDocid + ".2",
530
                        onlinetestdatafile2, SUCCESS, false);
531

    
532
            // try to set the permissions for the uploaded document
533
            // the docid given is for the online document
534
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
535
                                         null, null,
536
                                         "ecogrid://knb/" + onlineDocid + ".1",
537
                                         null, getAccessBlock(anotheruser, true,
538
                                         false, false, false, true),
539
                                         null, null, null, null);
540
            newdocid = generateDocid();
541
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
542

    
543
            // delete the document
544
            deleteDocid(onlineDocid + ".2", FAILURE, true);
545
            m.logout();
546

    
547
            /////////Case 3/////////////////////
548
            // upload an online document - change permission only
549
            onlineDocid = generateDocid();
550
            m.login(username, password);
551
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
552

    
553
            // upload a document which gives read access to the online document
554
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
555
                                         null, null,
556
                                         "ecogrid://knb/" + onlineDocid + ".1",
557
                                         null, getAccessBlock(anotheruser, true,
558
                                         false, false, true, false),
559
                                         null, null, null, null);
560
            newdocid = generateDocid();
561
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
562
            m.logout();
563

    
564
            // login as another user
565
            m.login(anotheruser, anotherpassword);
566

    
567
            // try to read the online data
568
            readDocid(onlineDocid + ".1", FAILURE, true);
569

    
570
            // try to upload another data with updated id
571
            uploadDocid(onlineDocid + ".2",
572
                          onlinetestdatafile2, FAILURE, true);
573

    
574
            // try to set the permissions for the uploaded document
575
            // the docid given is for the online document
576
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
577
                                         null, null,
578
                                         "ecogrid://knb/" + onlineDocid + ".1",
579
                                         null, getAccessBlock(anotheruser, true,
580
                                         false, false, false, true),
581
                                         null, null, null, null);
582
            newdocid = generateDocid();
583
            // ERRRRRRRRRRRRRRRR
584
            // User does not have permission to update of access rules for data
585
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
586

    
587
            // delete the document
588
            deleteDocid(onlineDocid + ".1", FAILURE, true);
589
            m.logout();
590

    
591
            /////////Case 4/////////////////////
592
            // upload an online document all
593
            onlineDocid = generateDocid();
594
            m.login(username, password);
595
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
596

    
597
            // upload a document which gives read access to the online document
598
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
599
                                         null, null,
600
                                         "ecogrid://knb/" + onlineDocid + ".1",
601
                                         null, getAccessBlock(anotheruser, true,
602
                                         false, false, false, true),
603
                                         null, null, null, null);
604
            newdocid = generateDocid();
605
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
606
            m.logout();
607

    
608
            // login as another user
609
            m.login(anotheruser, anotherpassword);
610

    
611
            // try to read the online data
612
            readDocid(onlineDocid + ".1", SUCCESS, false);
613

    
614
            // try to upload another data with updated id
615
            uploadDocid(onlineDocid + ".2",
616
                        onlinetestdatafile2, SUCCESS, false);
617

    
618
            // try to set the permissions for the uploaded document
619
            // the docid given is for the online document
620
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
621
                                         null, null,
622
                                         "ecogrid://knb/" + onlineDocid + ".1",
623
                                         null, getAccessBlock(anotheruser, true,
624
                                         true, false, false, false),
625
                                         null, null, null, null);
626
            newdocid = generateDocid();
627
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
628

    
629
            m.logout();
630
            // delete the document
631
            deleteDocid(onlineDocid + ".1", FAILURE, false);
632

    
633
            m.logout();
634

    
635
        }
636
        catch (MetacatAuthException mae) {
637
            fail("Authorization failed:\n" + mae.getMessage());
638
        }
639
        catch (MetacatInaccessibleException mie) {
640
            fail("Metacat Inaccessible:\n" + mie.getMessage());
641
        }
642
        catch (Exception e) {
643
            fail("General exception:\n" + e.getMessage());
644
        }
645
    }
646

    
647

    
648

    
649
    /** *********
650
     * Checking the following cases:
651
     * when only online data is uploaded by a user with the following different
652
     * access controls specified in addiotnal metadata in another document
653
     *   1.read
654
     *   2.write
655
     *   3.change permission
656
     *   4.all
657
     * And another user tries to do the following:
658
     * -> tries to read it
659
     * -> tries to update it
660
     * -> tries to set permissions on it
661
     * -> tries to delete it
662
     */
663
    public void onlineDataCasesTest_4() {
664
        try {
665

    
666
            /////////Case 1./////////////////////
667
            // upload an online document - read only
668
            onlineDocid = generateDocid();
669
            m.login(username, password);
670
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
671

    
672
            // upload a document which gives read access to the online document
673
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
674
                                         null, null,
675
                                         "ecogrid://knb/" + onlineDocid + ".1",
676
                                         null, null, null, null,
677
                                         getAccessBlock(anotheruser, true,
678
                                         true, false, false, false), null);
679
            newdocid = generateDocid();
680
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
681
            m.logout();
682

    
683
            // login as another user
684
            m.login(anotheruser, anotherpassword);
685

    
686
            // try to read the online data
687
            readDocid(onlineDocid + ".1", SUCCESS, false);
688

    
689
            // try to upload another data with updated id
690
            uploadDocid(onlineDocid + ".2",
691
                        onlinetestdatafile2, FAILURE, true);
692

    
693
            // try to set the permissions for the uploaded document
694
            // the docid given is for the online document
695
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
696
                                         null, null,
697
                                         "ecogrid://knb/" + onlineDocid + ".1",
698
                                         null, null, null, null,
699
                                         getAccessBlock(anotheruser, true,
700
                                         false, false, false, true), null);
701
            newdocid = generateDocid();
702
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
703

    
704
            // delete the document
705
            deleteDocid(onlineDocid + ".1", FAILURE, true);
706
            m.logout();
707

    
708
            /////////Case 2/////////////////////
709
            // upload an online document - write only
710
            onlineDocid = generateDocid();
711
            m.login(username, password);
712
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
713

    
714
            // upload a document which gives read access to the online document
715
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
716
                                         null, null,
717
                                         "ecogrid://knb/" + onlineDocid + ".1",
718
                                         null, null, null, null,
719
                                         getAccessBlock(anotheruser, true,
720
                                         false, true, false, false), null);
721
            newdocid = generateDocid();
722
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
723
            m.logout();
724

    
725
            // login as another user
726
            m.login(anotheruser, anotherpassword);
727

    
728
            // try to read the online data
729
            readDocid(onlineDocid + ".1", FAILURE, true);
730

    
731
            // try to upload another data with updated id
732
            uploadDocid(onlineDocid + ".2",
733
                        onlinetestdatafile2, SUCCESS, false);
734

    
735
            // try to set the permissions for the uploaded document
736
            // the docid given is for the online document
737
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
738
                                         null, null,
739
                                         "ecogrid://knb/" + onlineDocid + ".1",
740
                                         null, null, null, null,
741
                                         getAccessBlock(anotheruser, true,
742
                                         false, false, false, true), null);
743
            newdocid = generateDocid();
744
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
745

    
746
            // delete the document
747
            deleteDocid(onlineDocid + ".2", FAILURE, true);
748
            m.logout();
749

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

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

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

    
770
            // try to read the online data
771
            readDocid(onlineDocid + ".1", FAILURE, true);
772

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

    
777
            // try to set the permissions for the uploaded document
778
            // the docid given is for the online document
779
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
780
                                         null, null,
781
                                         "ecogrid://knb/" + onlineDocid + ".1",
782
                                         null, null, null, null,
783
                                         getAccessBlock(anotheruser, true,
784
                                         false, false, false, true), null);
785
            newdocid = generateDocid();
786
            // ERRRRRRRRRRRRRRRR
787
            // User does not have permission to update of access rules for data
788
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
789

    
790
            // delete the document
791
            deleteDocid(onlineDocid + ".1", FAILURE, true);
792
            m.logout();
793

    
794
            /////////Case 4/////////////////////
795
            // upload an online document all
796
            onlineDocid = generateDocid();
797
            m.login(username, password);
798
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
799

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

    
811
            // login as another user
812
            m.login(anotheruser, anotherpassword);
813

    
814
            // try to read the online data
815
            readDocid(onlineDocid + ".1", SUCCESS, false);
816

    
817
            // try to upload another data with updated id
818
            uploadDocid(onlineDocid + ".2",
819
                        onlinetestdatafile2, SUCCESS, false);
820

    
821
            // try to set the permissions for the uploaded document
822
            // the docid given is for the online document
823
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
824
                                         null, null,
825
                                         "ecogrid://knb/" + onlineDocid + ".1",
826
                                         null, null, null, null,
827
                                         getAccessBlock(anotheruser, true,
828
                                         true, false, false, false), null);
829
            newdocid = generateDocid();
830
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
831

    
832
            m.logout();
833
            // delete the document
834
            deleteDocid(onlineDocid + ".1", FAILURE, false);
835

    
836
            m.logout();
837

    
838
        }
839
        catch (MetacatAuthException mae) {
840
            fail("Authorization failed:\n" + mae.getMessage());
841
        }
842
        catch (MetacatInaccessibleException mie) {
843
            fail("Metacat Inaccessible:\n" + mie.getMessage());
844
        }
845
        catch (Exception e) {
846
            fail("General exception:\n" + e.getMessage());
847
        }
848
    }
849

    
850
    /** *********
851
     * Checking the following cases:
852
     * -> when online data with document refering to it is uploaded with
853
     *    rules in additional metadata for an wrong entity id which
854
     *    doesnt exist
855
     * -> when online data with document refering to it is uploaded with
856
     *    rules in additional metadata for an entity which doesnt
857
     *    exist - wrong url
858
     */
859
    public void onlineDataCasesTest_5() {
860
        try {
861

    
862
            // upload online data
863
            onlineDocid = generateDocid();
864
            m.login(username, password);
865

    
866
            /////////Case 1
867
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
868
                                         "ecogrid://knb/" + onlineDocid + ".1",
869
                                         null, null, null, null, null,
870
                                         getAccessBlock(anotheruser, true,
871
                                         true, false, false, false));
872
            newdocid = generateDocid();
873
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
874

    
875
            /////////Case 2
876
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
877
                                         "ecogrid://knb/" + onlineDocid + ".1",
878
                                         null, null, null, null,
879
                                         getAccessBlock(anotheruser, true,
880
                                         true, false, false, false), null);
881
            newdocid = generateDocid();
882

    
883
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
884
            m.logout();
885

    
886
        }
887
        catch (MetacatAuthException mae) {
888
            fail("Authorization failed:\n" + mae.getMessage());
889
        }
890
        catch (MetacatInaccessibleException mie) {
891
            fail("Metacat Inaccessible:\n" + mie.getMessage());
892
        }
893
        catch (Exception e) {
894
            fail("General exception:\n" + e.getMessage());
895
        }
896
    }
897

    
898

    
899
    /** *********
900
     * Checking the following cases:
901
     * -> when a document is added with no online data - it is updated (has
902
     *    access)  - then data is added - and a url to it is added and docid
903
     *    is updated - then the access is updated in document
904
     *    does it result in rules being applied on the data
905
     *    (Andrea Chadden was having problem is a similar case)
906
     * -> when online data with document refering to it is uploaded with read
907
     *    access for document and no access for docid and vice versa
908
     */
909
    public void onlineDataCasesTest_6() {
910
        try {
911

    
912
            // insert a document
913
            m.login(username, password);
914
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
915
                                         null, null, null,
916
                                         null, getAccessBlock(anotheruser, true,
917
                                         true, false, false, false), null, null,
918
                                         null, null);
919
            newdocid = generateDocid();
920
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
921
            m.logout();
922

    
923

    
924
            // update document
925
            m.login(username, password);
926
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing update",
927
                                         null, null, null,
928
                                         null, getAccessBlock(anotheruser, true,
929
                                         true, false, false, false), null, null,
930
                                         null, null);
931
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
932
            m.logout();
933

    
934

    
935
            // upload data and update the document
936
            onlineDocid = generateDocid();
937
            m.login(username, password);
938
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
939
            m.logout();
940

    
941
            // try to read the online data
942
            m.login(anotheruser, anotherpassword);
943
            readDocid(onlineDocid + ".1", FAILURE, true);
944
            m.logout();
945

    
946
            // upload data and update the document
947
            m.login(username, password);
948
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing update",
949
                                         null, null,
950
                                         "ecogrid://knb/" + onlineDocid + ".1",
951
                                         null, getAccessBlock(anotheruser, true,
952
                                         true, false, false, false), null, null,
953
                                         null, null);
954
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
955
            m.logout();
956

    
957
            // set read for document - no read for data
958
            m.login(username, password);
959
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
960
                                         null, null,
961
                                         "ecogrid://knb/" + onlineDocid + ".1",
962
                                         null, getAccessBlock(anotheruser, true,
963
                                         true, false, false, false), null, null,
964
                                         getAccessBlock(anotheruser, false,
965
                                         false, false, false, true), null);
966
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
967
            m.logout();
968

    
969
            // try to read the online data
970
            m.login(anotheruser, anotherpassword);
971
            readDocid(newdocid + ".4", SUCCESS, false);
972
            readDocid(onlineDocid + ".1", FAILURE, true);
973
            m.logout();
974

    
975

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

    
987
            // try to read the online data
988
            m.login(anotheruser, anotherpassword);
989
            readDocid(newdocid + ".5", FAILURE, true);
990
            readDocid(onlineDocid + ".1", SUCCESS, false);
991
            m.logout();
992
        }
993
        catch (MetacatAuthException mae) {
994
            fail("Authorization failed:\n" + mae.getMessage());
995
        }
996
        catch (MetacatInaccessibleException mie) {
997
            fail("Metacat Inaccessible:\n" + mie.getMessage());
998
        }
999
        catch (Exception e) {
1000
            fail("General exception:\n" + e.getMessage());
1001
        }
1002
    }
1003

    
1004

    
1005
    /**
1006
     * Insert a document into metacat. The expected result is passed as result
1007
     */
1008

    
1009
    private String insertDocid(String docid, String docText, boolean result,
1010
                               boolean expectKarmaException) {
1011
        String response = null;
1012
        try {
1013
            response = m.insert(docid,
1014
                                new StringReader(testdocument), null);
1015
            if (result) {
1016
                assertTrue( (response.indexOf("<success>") != -1));
1017
                assertTrue(response.indexOf(docid) != -1);
1018
            }
1019
            else {
1020
                assertTrue( (response.indexOf("<success>") == -1));
1021
            }
1022
            System.err.println(response);
1023
        }
1024
        catch (MetacatInaccessibleException mie) {
1025
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1026
        }
1027
        catch (InsufficientKarmaException ike) {
1028
            if (!expectKarmaException) {
1029
                fail("Insufficient karma:\n" + ike.getMessage());
1030
            }
1031
        }
1032
        catch (MetacatException me) {
1033
            if (result) {
1034
               fail("Metacat Error:\n" + me.getMessage());
1035
           }
1036
           else {
1037
               System.err.println("Metacat Error: " + me.getMessage());
1038
           }
1039
        }
1040
        catch (Exception e) {
1041
            fail("General exception:\n" + e.getMessage());
1042
        }
1043
        return response;
1044
    }
1045

    
1046
    /**
1047
     * Insert a document into metacat. The expected result is passed as result
1048
     */
1049

    
1050
    private String uploadDocid(String docid, String filePath, boolean result,
1051
                               boolean expectedKarmaException) {
1052
        String response = null;
1053
        try {
1054
            response = m.upload(docid, new File(filePath));
1055
            if (result) {
1056
                assertTrue( (response.indexOf("<success>") != -1));
1057
                assertTrue(response.indexOf(docid) != -1);
1058
            }
1059
            else {
1060
                assertTrue( (response.indexOf("<success>") == -1));
1061
            }
1062
            System.err.println("respose from metacat: " + response);
1063
        }
1064
        catch (MetacatInaccessibleException mie) {
1065
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1066
        }
1067
        catch (InsufficientKarmaException ike) {
1068
            if (!expectedKarmaException) {
1069
                fail("Insufficient karma:\n" + ike.getMessage());
1070
            }
1071
        }
1072
        catch (MetacatException me) {
1073
            if (result) {
1074
                fail("Metacat Error:\n" + me.getMessage());
1075
            }
1076
            else {
1077
                System.err.println("Metacat Error: " + me.getMessage());
1078
            }
1079
        }
1080
        catch (Exception e) {
1081
            fail("General exception:\n" + e.getMessage());
1082
        }
1083
        return response;
1084
    }
1085

    
1086
    /**
1087
     * Update a document in metacat. The expected result is passed as result
1088
     */
1089
    private String updateDocid(String docid, String docText, boolean result,
1090
                               boolean expectedKarmaFailure) {
1091
        String response = null;
1092
        try {
1093
            response = m.update(docid,
1094
                                new StringReader(testdocument), null);
1095

    
1096
            if (result) {
1097
                assertTrue( (response.indexOf("<success>") != -1));
1098
                assertTrue(response.indexOf(docid) != -1);
1099
            }
1100
            else {
1101
                assertTrue( (response.indexOf("<success>") == -1));
1102
            }
1103
            System.err.println(response);
1104
        }
1105
        catch (MetacatInaccessibleException mie) {
1106
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1107
        }
1108
        catch (InsufficientKarmaException ike) {
1109
            if (!expectedKarmaFailure) {
1110
                fail("Insufficient karma:\n" + ike.getMessage());
1111
            }
1112
        }
1113
        catch (MetacatException me) {
1114
            if (! (expectedKarmaFailure &&
1115
                   (me.getMessage().indexOf(
1116
                "User try to update a access module which it doesn't have \"ALL\" permission") !=
1117
                    -1))) {
1118
                fail("Metacat Error:\n" + me.getMessage());
1119
            }
1120
        }
1121
        catch (Exception e) {
1122
            fail("General exception:\n" + e.getMessage());
1123
        }
1124

    
1125
        return response;
1126
    }
1127

    
1128
    /**
1129
     * Delete a document into metacat. The expected result is passed as result
1130
     */
1131
    private void deleteDocid(String docid, boolean result,
1132
                             boolean expectedKarmaFailure) {
1133
        try {
1134
            String response = m.delete(docid);
1135
            if (result) {
1136
                assertTrue(response.indexOf("<success>") != -1);
1137
            }
1138
            else {
1139
                assertTrue(response.indexOf("<success>") == -1);
1140
            }
1141
            System.err.println(response);
1142
        }
1143
        catch (MetacatInaccessibleException mie) {
1144
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1145
        }
1146
        catch (InsufficientKarmaException ike) {
1147
            if (!expectedKarmaFailure) {
1148
                    fail("Insufficient karma:\n" + ike.getMessage());
1149
                }
1150

    
1151
            }
1152
        catch (MetacatException me) {
1153
            if (result) {
1154
                fail("Metacat Error:\n" + me.getMessage());
1155
            }
1156
            else {
1157
                System.err.println("Metacat Error:\n" + me.getMessage());
1158
            }
1159
        }
1160
        catch (Exception e) {
1161
            fail("General exception:\n" + e.getMessage());
1162
        }
1163
    }
1164

    
1165
    /**
1166
     * Read a document from metacat. The expected result is passed as result
1167
     */
1168
    private void readDocid(String docid, boolean result,
1169
                           boolean expextedKarmaFailure) {
1170
        try {
1171
            Reader r = m.read(docid);
1172
            String response = IOUtil.getAsString(r, true);
1173

    
1174
            if (!result) {
1175
                assertTrue(response.indexOf("<success>") == -1);
1176
            }
1177
            // System.err.println(response);
1178
        }
1179
        catch (MetacatInaccessibleException mie) {
1180
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1181
        }
1182
        catch (InsufficientKarmaException ike) {
1183
            if (!expextedKarmaFailure) {
1184
                fail("Insufficient karma:\n" + ike.getMessage());
1185
            }
1186
        }
1187
        catch (MetacatException me) {
1188
            if (result) {
1189
                fail("Metacat Error:\n" + me.getMessage());
1190
            }
1191
            else {
1192
                System.err.println("Metacat Error:\n" + me.getMessage());
1193
            }
1194
        }
1195
        catch (Exception e) {
1196
            fail("General exception:\n" + e.getMessage());
1197
        }
1198
    }
1199

    
1200
    /**
1201
     * Read a document from metacat and check if it is equal to a given string.
1202
     * The expected result is passed as result
1203
     */
1204

    
1205
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1206
                                         boolean result,
1207
                                         boolean expextedKarmaFailure) {
1208
        try {
1209
            Reader r = m.read(docid);
1210
            String doc = IOUtil.getAsString(r, true);
1211
            if (result) {
1212

    
1213
                if (!testDoc.equals(doc)) {
1214
                    System.out.println("doc    :" + doc);
1215
                    System.out.println("testDoc:" + testDoc);
1216
                }
1217

    
1218
                assertTrue(testDoc.equals(doc));
1219
            }
1220
            else {
1221
                assertTrue(doc.indexOf("<error>") != -1);
1222
            }
1223
        }
1224
        catch (MetacatInaccessibleException mie) {
1225
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1226
        }
1227
        catch (InsufficientKarmaException ike) {
1228
            if (!expextedKarmaFailure) {
1229
                fail("Insufficient karma:\n" + ike.getMessage());
1230
            }
1231
        }
1232
        catch (MetacatException me) {
1233
            fail("Metacat Error:\n" + me.getMessage());
1234
        }
1235
        catch (Exception e) {
1236
            fail("General exception:\n" + e.getMessage());
1237
        }
1238

    
1239
    }
1240

    
1241
    /**
1242
     * Create a hopefully unique docid for testing insert and update. Does
1243
     * not include the 'revision' part of the id.
1244
     *
1245
     * @return a String docid based on the current date and time
1246
     */
1247
    private String generateDocid() {
1248
        StringBuffer docid = new StringBuffer(prefix);
1249
        docid.append(".");
1250

    
1251
        // Create a calendar to get the date formatted properly
1252
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1253
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1254
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1255
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1256
                       2 * 60 * 60 * 1000);
1257
        Calendar calendar = new GregorianCalendar(pdt);
1258
        Date trialTime = new Date();
1259
        calendar.setTime(trialTime);
1260
        docid.append(calendar.get(Calendar.YEAR));
1261
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1262
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1263
        docid.append(calendar.get(Calendar.MINUTE));
1264
        docid.append(calendar.get(Calendar.SECOND));
1265

    
1266
        return docid.toString();
1267
    }
1268
}
(7-7/11)