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-26 17:30:57 -0700 (Thu, 26 Aug 2004) $'
9
 * '$Revision: 2256 $'
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 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("Testing insert", null, null,
336
                                         "ecogrid://knb/" + onlineDocid + ".1",
337
                                         null, getAccessBlock(anotheruser, true,
338
                true, false, false, false),
339
                                         null, null, null, null);
340
            newdocid = generateDocid();
341
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
342
            m.logout();
343

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

    
350
            m.login(username, password);
351

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

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

    
361
            m.logout();
362

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

    
375

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

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

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

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

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

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

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

    
413

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

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

    
430
            m.logout();
431

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

    
444

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

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

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

    
478
            // login as another user
479
            m.login(anotheruser, anotherpassword);
480

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

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

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

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

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

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

    
519
            // login as another user
520
            m.login(anotheruser, anotherpassword);
521

    
522
            // try to read the online data
523
            readDocid(onlineDocid + ".1", FAILURE, true);
524

    
525
            // try to upload another data with updated id
526
            uploadDocid(onlineDocid + ".2",
527
                        onlinetestdatafile2, SUCCESS, false);
528

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

    
539
            // delete the document
540
            deleteDocid(onlineDocid + ".2", FAILURE, true);
541
            m.logout();
542

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

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

    
559
            // login as another user
560
            m.login(anotheruser, anotherpassword);
561

    
562
            // try to read the online data
563
            readDocid(onlineDocid + ".1", FAILURE, true);
564

    
565
            // try to upload another data with updated id
566
            // ERRRRRRRRRRRRRRRRRRRRR
567
            // uploadDocid(onlineDocid + ".2",
568
            //           onlinetestdatafile2, FAILURE, true);
569

    
570
            // try to set the permissions for the uploaded document
571
            // the docid given is for the online document
572
            testdocument = getTestEmlDoc("Testing insert", null, null,
573
                                         "ecogrid://knb/" + onlineDocid + ".1",
574
                                         null, getAccessBlock(anotheruser, true,
575
                                         false, false, false, true),
576
                                         null, null, null, null);
577
            newdocid = generateDocid();
578
            // ERRRRRRRRRRRRRRRR
579
            //insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
580

    
581
            // delete the document
582
            deleteDocid(onlineDocid + ".1", FAILURE, true);
583
            m.logout();
584

    
585
            /////////Case 4/////////////////////
586
            // upload an online document all
587
            onlineDocid = generateDocid();
588
            m.login(username, password);
589
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
590

    
591
            // upload a document which gives read access to the online document
592
            testdocument = getTestEmlDoc("Another insert", null, null,
593
                                         "ecogrid://knb/" + onlineDocid + ".1",
594
                                         null, getAccessBlock(anotheruser, true,
595
                                         false, false, false, true),
596
                                         null, null, null, null);
597
            newdocid = generateDocid();
598
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
599
            m.logout();
600

    
601
            // login as another user
602
            m.login(anotheruser, anotherpassword);
603

    
604
            // try to read the online data
605
            readDocid(onlineDocid + ".1", SUCCESS, false);
606

    
607
            // try to upload another data with updated id
608
            uploadDocid(onlineDocid + ".2",
609
                        onlinetestdatafile2, SUCCESS, false);
610

    
611
            // try to set the permissions for the uploaded document
612
            // the docid given is for the online document
613
            testdocument = getTestEmlDoc("Testing insert", null, null,
614
                                         "ecogrid://knb/" + onlineDocid + ".1",
615
                                         null, getAccessBlock(anotheruser, true,
616
                                         true, false, false, false),
617
                                         null, null, null, null);
618
            newdocid = generateDocid();
619
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
620

    
621
            m.logout();
622
            // delete the document
623
            deleteDocid(onlineDocid + ".1", FAILURE, false);
624

    
625
            m.logout();
626

    
627
        }
628
        catch (MetacatAuthException mae) {
629
            fail("Authorization failed:\n" + mae.getMessage());
630
        }
631
        catch (MetacatInaccessibleException mie) {
632
            fail("Metacat Inaccessible:\n" + mie.getMessage());
633
        }
634
        catch (Exception e) {
635
            fail("General exception:\n" + e.getMessage());
636
        }
637
    }
638

    
639

    
640

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

    
658
            /////////Case 1./////////////////////
659
            // upload an online document - read only
660
            onlineDocid = generateDocid();
661
            m.login(username, password);
662
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
663

    
664
            // upload a document which gives read access to the online document
665
            testdocument = getTestEmlDoc("Another insert", null, null,
666
                                         "ecogrid://knb/" + onlineDocid + ".1",
667
                                         null, null, null, null,
668
                                         getAccessBlock(anotheruser, true,
669
                                         true, false, false, false), null);
670
            newdocid = generateDocid();
671
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
672
            m.logout();
673

    
674
            // login as another user
675
            m.login(anotheruser, anotherpassword);
676

    
677
            // try to read the online data
678
            readDocid(onlineDocid + ".1", SUCCESS, false);
679

    
680
            // try to upload another data with updated id
681
            // ERRRRRRRRRRRRRRRRRRRRRR
682
            //uploadDocid(onlineDocid + ".2",
683
            //            onlinetestdatafile2, FAILURE, true);
684

    
685
            // try to set the permissions for the uploaded document
686
            // the docid given is for the online document
687
            testdocument = getTestEmlDoc("Testing insert", null, null,
688
                                         "ecogrid://knb/" + onlineDocid + ".1",
689
                                         null, null, null, null,
690
                                         getAccessBlock(anotheruser, true,
691
                                         false, false, false, true), null);
692
            newdocid = generateDocid();
693
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
694

    
695
            // delete the document
696
            deleteDocid(onlineDocid + ".1", FAILURE, true);
697
            m.logout();
698

    
699
            /////////Case 2/////////////////////
700
            // upload an online document - write only
701
            onlineDocid = generateDocid();
702
            m.login(username, password);
703
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
704

    
705
            // upload a document which gives read access to the online document
706
            testdocument = getTestEmlDoc("Another insert", null, null,
707
                                         "ecogrid://knb/" + onlineDocid + ".1",
708
                                         null, null, null, null,
709
                                         getAccessBlock(anotheruser, true,
710
                                         false, true, false, false), null);
711
            newdocid = generateDocid();
712
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
713
            m.logout();
714

    
715
            // login as another user
716
            m.login(anotheruser, anotherpassword);
717

    
718
            // try to read the online data
719
            readDocid(onlineDocid + ".1", FAILURE, true);
720

    
721
            // try to upload another data with updated id
722
            uploadDocid(onlineDocid + ".2",
723
                        onlinetestdatafile2, SUCCESS, false);
724

    
725
            // try to set the permissions for the uploaded document
726
            // the docid given is for the online document
727
            testdocument = getTestEmlDoc("Testing insert", null, null,
728
                                         "ecogrid://knb/" + onlineDocid + ".1",
729
                                         null, null, null, null,
730
                                         getAccessBlock(anotheruser, true,
731
                                         false, false, false, true), null);
732
            newdocid = generateDocid();
733
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
734

    
735
            // delete the document
736
            deleteDocid(onlineDocid + ".2", FAILURE, true);
737
            m.logout();
738

    
739
            /////////Case 3/////////////////////
740
            // upload an online document - change permission only
741
            onlineDocid = generateDocid();
742
            m.login(username, password);
743
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
744

    
745
            // upload a document which gives read access to the online document
746
            testdocument = getTestEmlDoc("Another insert", null, null,
747
                                         "ecogrid://knb/" + onlineDocid + ".1",
748
                                         null, null, null, null,
749
                                         getAccessBlock(anotheruser, true,
750
                                         false, false, true, false), null);
751
            newdocid = generateDocid();
752
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
753
            m.logout();
754

    
755
            // login as another user
756
            m.login(anotheruser, anotherpassword);
757

    
758
            // try to read the online data
759
            readDocid(onlineDocid + ".1", FAILURE, true);
760

    
761
            // try to upload another data with updated id
762
            // ERRRRRRRRRRRRRRRRRRRRR
763
            // uploadDocid(onlineDocid + ".2",
764
            //           onlinetestdatafile2, FAILURE, true);
765

    
766
            // try to set the permissions for the uploaded document
767
            // the docid given is for the online document
768
            testdocument = getTestEmlDoc("Testing insert", null, null,
769
                                         "ecogrid://knb/" + onlineDocid + ".1",
770
                                         null, null, null, null,
771
                                         getAccessBlock(anotheruser, true,
772
                                         false, false, false, true), null);
773
            newdocid = generateDocid();
774
            // ERRRRRRRRRRRRRRRR
775
            //insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
776

    
777
            // delete the document
778
            deleteDocid(onlineDocid + ".1", FAILURE, true);
779
            m.logout();
780

    
781
            /////////Case 4/////////////////////
782
            // upload an online document all
783
            onlineDocid = generateDocid();
784
            m.login(username, password);
785
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
786

    
787
            // upload a document which gives read access to the online document
788
            testdocument = getTestEmlDoc("Another insert", null, null,
789
                                         "ecogrid://knb/" + onlineDocid + ".1",
790
                                         null, null, null, null,
791
                                         getAccessBlock(anotheruser, true,
792
                                         false, false, false, true), null);
793
            newdocid = generateDocid();
794
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
795
            m.logout();
796

    
797
            // login as another user
798
            m.login(anotheruser, anotherpassword);
799

    
800
            // try to read the online data
801
            readDocid(onlineDocid + ".1", SUCCESS, false);
802

    
803
            // try to upload another data with updated id
804
            uploadDocid(onlineDocid + ".2",
805
                        onlinetestdatafile2, SUCCESS, false);
806

    
807
            // try to set the permissions for the uploaded document
808
            // the docid given is for the online document
809
            testdocument = getTestEmlDoc("Testing insert", null, null,
810
                                         "ecogrid://knb/" + onlineDocid + ".1",
811
                                         null, null, null, null,
812
                                         getAccessBlock(anotheruser, true,
813
                                         true, false, false, false), null);
814
            newdocid = generateDocid();
815
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
816

    
817
            m.logout();
818
            // delete the document
819
            deleteDocid(onlineDocid + ".1", FAILURE, false);
820

    
821
            m.logout();
822

    
823
        }
824
        catch (MetacatAuthException mae) {
825
            fail("Authorization failed:\n" + mae.getMessage());
826
        }
827
        catch (MetacatInaccessibleException mie) {
828
            fail("Metacat Inaccessible:\n" + mie.getMessage());
829
        }
830
        catch (Exception e) {
831
            fail("General exception:\n" + e.getMessage());
832
        }
833
    }
834

    
835
    /** *********
836
     * Checking the following cases:
837
     * -> when online data with document refering to it is uploaded with
838
     *    rules in additional metadata for an wrong entity id which
839
     *    doesnt exist
840
     * -> when online data with document refering to it is uploaded with
841
     *    rules in additional metadata for an entity which doesnt
842
     *    exist - wrong url
843
     */
844
    public void onlineDataCasesTest_5() {
845
        try {
846

    
847
            // upload online data
848
            onlineDocid = generateDocid();
849
            m.login(username, password);
850

    
851
            /////////Case 1
852
            testdocument = getTestEmlDoc("Testing insert", null, null,
853
                                         "ecogrid://knb/" + onlineDocid + ".1",
854
                                         null, null, null, null, null,
855
                                         getAccessBlock(anotheruser, true,
856
                                         true, false, false, false));
857
            newdocid = generateDocid();
858
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
859

    
860
            /////////Case 2
861
            testdocument = getTestEmlDoc("Testing insert", null, null,
862
                                         "ecogrid://knb/" + onlineDocid + ".1",
863
                                         null, null, null, null,
864
                                         getAccessBlock(anotheruser, true,
865
                                         true, false, false, false), null);
866
            newdocid = generateDocid();
867

    
868
            // ERRRRRRRRRRRRRRRR - what is the right behaviour??????
869
            //insertDocid(newdocid + ".1", testdocument, FAILURE, false);
870
            m.logout();
871

    
872
        }
873
        catch (MetacatAuthException mae) {
874
            fail("Authorization failed:\n" + mae.getMessage());
875
        }
876
        catch (MetacatInaccessibleException mie) {
877
            fail("Metacat Inaccessible:\n" + mie.getMessage());
878
        }
879
        catch (Exception e) {
880
            fail("General exception:\n" + e.getMessage());
881
        }
882
    }
883

    
884

    
885
    /** *********
886
     * Checking the following cases:
887
     * -> when a document is added with no online data - it is updated (has
888
     *    access)  - then data is added - and a url to it is added and docid
889
     *    is updated - then the access is updated in document
890
     *    does it result in rules being applied on the data
891
     *    (Andrea Chadden was having problem is a similar case)
892
     * -> when online data with document refering to it is uploaded with read
893
     *    access for document and no access for docid and vice versa
894
     */
895
    public void onlineDataCasesTest_6() {
896
        try {
897

    
898
            // insert a document
899
            m.login(username, password);
900
            testdocument = getTestEmlDoc("Doing insert", null, null, null,
901
                                         null, getAccessBlock(anotheruser, true,
902
                                         true, false, false, false), null, null,
903
                                         null, null);
904
            newdocid = generateDocid();
905
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
906
            m.logout();
907

    
908

    
909
            // update document
910
            m.login(username, password);
911
            testdocument = getTestEmlDoc("Doing update", null, null, null,
912
                                         null, getAccessBlock(anotheruser, true,
913
                                         true, false, false, false), null, null,
914
                                         null, null);
915
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
916
            m.logout();
917

    
918

    
919
            // upload data and update the document
920
            onlineDocid = generateDocid();
921
            m.login(username, password);
922
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
923
            m.logout();
924

    
925
            // try to read the online data
926
            m.login(anotheruser, anotherpassword);
927
            readDocid(onlineDocid + ".1", FAILURE, true);
928
            m.logout();
929

    
930
            // upload data and update the document
931
            m.login(username, password);
932
            testdocument = getTestEmlDoc("Doing update", null, null,
933
                                         "ecogrid://knb/" + onlineDocid + ".1",
934
                                         null, getAccessBlock(anotheruser, true,
935
                                         true, false, false, false), null, null,
936
                                         null, null);
937
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
938
            m.logout();
939

    
940
            // set read for document - no read for data
941
            m.login(username, password);
942
            testdocument = getTestEmlDoc("Doing update", null, null,
943
                                         "ecogrid://knb/" + onlineDocid + ".1",
944
                                         null, getAccessBlock(anotheruser, true,
945
                                         true, false, false, false), null, null,
946
                                         getAccessBlock(anotheruser, false,
947
                                         false, false, false, true), null);
948
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
949
            m.logout();
950

    
951
            // try to read the online data
952
            m.login(anotheruser, anotherpassword);
953
            readDocid(newdocid + ".4", SUCCESS, false);
954
            readDocid(onlineDocid + ".1", FAILURE, true);
955
            m.logout();
956

    
957

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

    
969
            // try to read the online data
970
            m.login(anotheruser, anotherpassword);
971
            readDocid(newdocid + ".5", FAILURE, true);
972
            readDocid(onlineDocid + ".1", SUCCESS, false);
973
            m.logout();
974
        }
975
        catch (MetacatAuthException mae) {
976
            fail("Authorization failed:\n" + mae.getMessage());
977
        }
978
        catch (MetacatInaccessibleException mie) {
979
            fail("Metacat Inaccessible:\n" + mie.getMessage());
980
        }
981
        catch (Exception e) {
982
            fail("General exception:\n" + e.getMessage());
983
        }
984
    }
985

    
986

    
987
    /**
988
     * Insert a document into metacat. The expected result is passed as result
989
     */
990

    
991
    private String insertDocid(String docid, String docText, boolean result,
992
                               boolean expectKarmaException) {
993
        String response = null;
994
        try {
995
            response = m.insert(docid,
996
                                new StringReader(testdocument), null);
997
            if (result) {
998
                assertTrue( (response.indexOf("<success>") != -1));
999
                assertTrue(response.indexOf(docid) != -1);
1000
            }
1001
            else {
1002
                assertTrue( (response.indexOf("<success>") == -1));
1003
            }
1004
            System.err.println(response);
1005
        }
1006
        catch (MetacatInaccessibleException mie) {
1007
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1008
        }
1009
        catch (InsufficientKarmaException ike) {
1010
            if (!expectKarmaException) {
1011
                fail("Insufficient karma:\n" + ike.getMessage());
1012
            }
1013
        }
1014
        catch (MetacatException me) {
1015
            if (result) {
1016
               fail("Metacat Error:\n" + me.getMessage());
1017
           }
1018
           else {
1019
               System.err.println("Metacat Error: " + me.getMessage());
1020
           }
1021
        }
1022
        catch (Exception e) {
1023
            fail("General exception:\n" + e.getMessage());
1024
        }
1025
        return response;
1026
    }
1027

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

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

    
1068
    /**
1069
     * Update a document in metacat. The expected result is passed as result
1070
     */
1071
    private String updateDocid(String docid, String docText, boolean result,
1072
                               boolean expectedKarmaFailure) {
1073
        String response = null;
1074
        try {
1075
            response = m.update(docid,
1076
                                new StringReader(testdocument), null);
1077

    
1078
            if (result) {
1079
                assertTrue( (response.indexOf("<success>") != -1));
1080
                assertTrue(response.indexOf(docid) != -1);
1081
            }
1082
            else {
1083
                assertTrue( (response.indexOf("<success>") == -1));
1084
            }
1085
            System.err.println(response);
1086
        }
1087
        catch (MetacatInaccessibleException mie) {
1088
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1089
        }
1090
        catch (InsufficientKarmaException ike) {
1091
            if (!expectedKarmaFailure) {
1092
                fail("Insufficient karma:\n" + ike.getMessage());
1093
            }
1094
        }
1095
        catch (MetacatException me) {
1096
            if (! (expectedKarmaFailure &&
1097
                   (me.getMessage().indexOf(
1098
                "User try to update a access module which it doesn't have \"ALL\" permission") !=
1099
                    -1))) {
1100
                fail("Metacat Error:\n" + me.getMessage());
1101
            }
1102
        }
1103
        catch (Exception e) {
1104
            fail("General exception:\n" + e.getMessage());
1105
        }
1106

    
1107
        return response;
1108
    }
1109

    
1110
    /**
1111
     * Delete a document into metacat. The expected result is passed as result
1112
     */
1113
    private void deleteDocid(String docid, boolean result,
1114
                             boolean expectedKarmaFailure) {
1115
        try {
1116
            String response = m.delete(docid);
1117
            if (result) {
1118
                assertTrue(response.indexOf("<success>") != -1);
1119
            }
1120
            else {
1121
                assertTrue(response.indexOf("<success>") == -1);
1122
            }
1123
            System.err.println(response);
1124
        }
1125
        catch (MetacatInaccessibleException mie) {
1126
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1127
        }
1128
        catch (InsufficientKarmaException ike) {
1129
            if (!expectedKarmaFailure) {
1130
                    fail("Insufficient karma:\n" + ike.getMessage());
1131
                }
1132

    
1133
            }
1134
        catch (MetacatException me) {
1135
            if (result) {
1136
                fail("Metacat Error:\n" + me.getMessage());
1137
            }
1138
            else {
1139
                System.err.println("Metacat Error:\n" + me.getMessage());
1140
            }
1141
        }
1142
        catch (Exception e) {
1143
            fail("General exception:\n" + e.getMessage());
1144
        }
1145
    }
1146

    
1147
    /**
1148
     * Read a document from metacat. The expected result is passed as result
1149
     */
1150
    private void readDocid(String docid, boolean result,
1151
                           boolean expextedKarmaFailure) {
1152
        try {
1153
            Reader r = m.read(docid);
1154
            String response = IOUtil.getAsString(r, true);
1155

    
1156
            if (!result) {
1157
                assertTrue(response.indexOf("<success>") == -1);
1158
            }
1159
            // System.err.println(response);
1160
        }
1161
        catch (MetacatInaccessibleException mie) {
1162
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1163
        }
1164
        catch (InsufficientKarmaException ike) {
1165
            if (!expextedKarmaFailure) {
1166
                fail("Insufficient karma:\n" + ike.getMessage());
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 and check if it is equal to a given string.
1184
     * The expected result is passed as result
1185
     */
1186

    
1187
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1188
                                         boolean result,
1189
                                         boolean expextedKarmaFailure) {
1190
        try {
1191
            Reader r = m.read(docid);
1192
            String doc = IOUtil.getAsString(r, true);
1193
            if (result) {
1194

    
1195
                if (!testDoc.equals(doc)) {
1196
                    System.out.println("doc    :" + doc);
1197
                    System.out.println("testDoc:" + testDoc);
1198
                }
1199

    
1200
                assertTrue(testDoc.equals(doc));
1201
            }
1202
            else {
1203
                assertTrue(doc.indexOf("<error>") != -1);
1204
            }
1205
        }
1206
        catch (MetacatInaccessibleException mie) {
1207
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1208
        }
1209
        catch (InsufficientKarmaException ike) {
1210
            if (!expextedKarmaFailure) {
1211
                fail("Insufficient karma:\n" + ike.getMessage());
1212
            }
1213
        }
1214
        catch (MetacatException me) {
1215
            fail("Metacat Error:\n" + me.getMessage());
1216
        }
1217
        catch (Exception e) {
1218
            fail("General exception:\n" + e.getMessage());
1219
        }
1220

    
1221
    }
1222

    
1223
    /**
1224
     * Create a hopefully unique docid for testing insert and update. Does
1225
     * not include the 'revision' part of the id.
1226
     *
1227
     * @return a String docid based on the current date and time
1228
     */
1229
    private String generateDocid() {
1230
        StringBuffer docid = new StringBuffer(prefix);
1231
        docid.append(".");
1232

    
1233
        // Create a calendar to get the date formatted properly
1234
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1235
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1236
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1237
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1238
                       2 * 60 * 60 * 1000);
1239
        Calendar calendar = new GregorianCalendar(pdt);
1240
        Date trialTime = new Date();
1241
        calendar.setTime(trialTime);
1242
        docid.append(calendar.get(Calendar.YEAR));
1243
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1244
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1245
        docid.append(calendar.get(Calendar.MINUTE));
1246
        docid.append(calendar.get(Calendar.SECOND));
1247

    
1248
        return docid.toString();
1249
    }
1250
}
(5-5/9)