Project

General

Profile

1 2239 sgarg
/**
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$'
8
 *     '$Date$'
9
 * '$Revision$'
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 2244 sgarg
import java.io.File;
47 2239 sgarg
48
/**
49 2244 sgarg
 * A JUnit test for testing Access Control in Metacat
50 2239 sgarg
 */
51
public class AccessControlTest
52
    extends TestCase {
53 2244 sgarg
54 2239 sgarg
  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 2244 sgarg
  private String onlineDocid = null;
62 2239 sgarg
  private String testdocument = "";
63 2244 sgarg
  private String onlinetestdatafile1 = "test/onlineDataFile1";
64
  private String onlinetestdatafile2 = "test/onlineDataFile2";
65
66 2239 sgarg
  private Metacat m;
67 2244 sgarg
68 2239 sgarg
  private boolean SUCCESS = true;
69
  private boolean FAILURE = false;
70
71 2244 sgarg
72
73 2239 sgarg
  /**
74 2244 sgarg
   * These variables are for eml-2.0.1 only. For other eml versions,
75
   * this function might have to modified
76
   */
77
78
  private String testEmlHeader =
79
      "<?xml version=\"1.0\"?><eml:eml" +
80
      " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
81
      " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
82
      " packageId=\"eml.1.1\" system=\"knb\"" +
83
      " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
84
      " scope=\"system\">";
85
86
  private String testEmlCreatorBlock =
87
      "<creator scope=\"document\">                                       " +
88
      " <individualName>                                                  " +
89
      "    <surName>Smith</surName>                                       " +
90
      " </individualName>                                                 " +
91
      "</creator>                                                         ";
92
93
  private String testEmlContactBlock =
94
      "<contact scope=\"document\">                                       " +
95
      " <individualName>                                                  " +
96
      "    <surName>Jackson</surName>                                     " +
97
      " </individualName>                                                 " +
98
      "</contact>                                                         ";
99
100
  private String testEmlInlineBlock1 =
101
      "<inline>                                                           " +
102
      "  <admin>                                                          " +
103
      "    <contact>                                                      " +
104
      "      <name>Operator</name>                                        " +
105
      "      <institution>PSI</institution>                               " +
106
      "    </contact>                                                     " +
107
      "  </admin>                                                         " +
108
      "</inline>                                                          ";
109
110
  private String testEmlInlineBlock2 =
111
      "<inline>                                                           " +
112
      "  <instrument>                                                     " +
113
      "    <instName>LCQ</instName>                                       " +
114
      "    <source type=\"ESI\"></source>                                 " +
115
      "    <detector type=\"EM\"></detector>                              " +
116
      "  </instrument>                                                    " +
117
      "</inline>                                                          ";
118
119
  /**
120
    * This function returns a valid eml document with no access rules
121
    * This function is for eml-2.0.1 only. For other eml versions,
122
    * this function might have to modified
123
    */
124
   private String getTestEmlDoc(String title,  String inlineData1,
125
                               String inlineData2, String onlineUrl1,
126
                               String onlineUrl2){
127
    String testDocument = "";
128
    testDocument = testDocument + testEmlHeader +
129
        "<dataset scope=\"document\"><title>" + title + "</title>" +
130
        testEmlCreatorBlock;
131
132
    if(inlineData1 != null){
133
      testDocument = testDocument + "<distribution scope=\"document\">" +
134
          inlineData1 + "</distribution>";
135
    }
136
    if(inlineData2 != null){
137
      testDocument = testDocument + "<distribution scope=\"document\">" +
138
          inlineData2 + "</distribution>";
139
    }
140
    if(onlineUrl1 != null){
141
      testDocument = testDocument + "<distribution scope=\"document\">" +
142
          "<online><url function=\"download\">" + onlineUrl1 + "</url></online></distribution>";
143
    }
144
    if(onlineUrl2 != null){
145
      testDocument = testDocument + "<distribution scope=\"document\">" +
146
          "<online><url function=\"download\">" + onlineUrl2 + "</url></online></distribution>";
147
    }
148
    testDocument = testDocument + testEmlContactBlock + "</dataset></eml:eml>";
149
150
    return testDocument;
151
  }
152
153
  /**
154 2239 sgarg
   * Constructor to build the test
155
   *
156
   * @param name the name of the test method
157
   */
158
  public AccessControlTest(String name) {
159
    super(name);
160
    newdocid = generateDocid();
161
  }
162
163
  /**
164
   * Establish a testing framework by initializing appropriate objects
165
   */
166
  public void setUp() {
167
    try {
168
      System.err.println("Test Metacat: " + metacatUrl);
169
      m = MetacatFactory.createMetacatConnection(metacatUrl);
170
    }
171
    catch (MetacatInaccessibleException mie) {
172
      System.err.println("Metacat is: " + metacatUrl);
173
      fail("Metacat connection failed." + mie.getMessage());
174
    }
175
  }
176
177
  /**
178
   * Release any objects after tests are complete
179
   */
180
  public void tearDown() {
181
  }
182
183
  /**
184
   * Create a suite of tests to be run together
185
   */
186
  public static Test suite() {
187
    TestSuite suite = new TestSuite();
188
    suite.addTest(new AccessControlTest("initialize"));
189
    // Test basic functions
190 2247 sgarg
    suite.addTest(new AccessControlTest("login"));
191
    suite.addTest(new AccessControlTest("insert"));
192
    suite.addTest(new AccessControlTest("read"));
193
    suite.addTest(new AccessControlTest("update"));
194
    suite.addTest(new AccessControlTest("delete"));
195 2239 sgarg
    // Tests when no access is specified
196
    suite.addTest(new AccessControlTest("noAccessSpecified_Owner"));
197 2244 sgarg
    //suite.addTest(new AccessControlTest("noAccessSpecified_Random"));
198
    //suite.addTest(new AccessControlTest("noAccessSpecified_Public"));
199 2239 sgarg
200
    return suite;
201
  }
202
203
  /**
204
   * Run an initial test that always passes to check that the test
205
   * harness is working.
206
   */
207
  public void initialize() {
208
    assertTrue(1 == 1);
209
  }
210
211
  /**
212
   * Test the login() function with valid credentials
213
   */
214
  public void login() {
215
    // Try a valid login
216
    try {
217
      String response = m.login(username, password);
218
      System.err.println("Login response: " + response);
219
      assertTrue(response != null);
220
      assertTrue(response.indexOf("<login>") != -1);
221
      String sessionId = m.getSessionId();
222
      System.err.println("Session ID: " + m.getSessionId());
223
      assertTrue(sessionId != null);
224
      assertTrue(response.indexOf(m.getSessionId()) != -1);
225
    }
226
    catch (MetacatAuthException mae) {
227
      fail("Authorization failed:\n" + mae.getMessage());
228
    }
229
    catch (MetacatInaccessibleException mie) {
230
      fail("Metacat Inaccessible:\n" + mie.getMessage());
231
    }
232
  }
233
234
  /**
235
   * Test the insert() function with a known document
236
   */
237
  public void insert() {
238
    try {
239 2244 sgarg
      testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
240
                                   null, null, null);
241 2239 sgarg
      String identifier = newdocid + ".1";
242
      m.login(username, password);
243
      String response = m.insert(identifier,
244
                                 new StringReader(testdocument), null);
245
      assertTrue(response.indexOf("<success>") != -1);
246
      assertTrue(response.indexOf(identifier) != -1);
247
      System.err.println(response);
248
    }
249
    catch (MetacatAuthException mae) {
250
      fail("Authorization failed:\n" + mae.getMessage());
251
    }
252
    catch (MetacatInaccessibleException mie) {
253
      fail("Metacat Inaccessible:\n" + mie.getMessage());
254
    }
255
    catch (InsufficientKarmaException ike) {
256
      assertTrue(1 == 1);
257
      fail("Insufficient karma:\n" + ike.getMessage());
258
    }
259
    catch (MetacatException me) {
260
      fail("Metacat Error:\n" + me.getMessage());
261
    }
262
    catch (Exception e) {
263
      fail("General exception:\n" + e.getMessage());
264
    }
265
  }
266
267
  /**
268
   * The read() function
269
   */
270
  public void read() {
271
    try {
272 2244 sgarg
      testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1, null,
273
                                   null, null);
274 2239 sgarg
      m.login(username, password);
275
      Reader r = m.read(newdocid + ".1");
276
      String doc = IOUtil.getAsString(r, true);
277
      assertTrue(doc.equals(testdocument));
278
    }
279
    catch (MetacatAuthException mae) {
280
      fail("Authorization failed:\n" + mae.getMessage());
281
    }
282
    catch (MetacatInaccessibleException mie) {
283
      fail("Metacat Inaccessible:\n" + mie.getMessage());
284
    }
285
    catch (Exception e) {
286
      fail("General exception:\n" + e.getMessage());
287
    }
288
  }
289
290
  /**
291
   * Test the update() function with a known document
292
   */
293
  public void update() {
294
    try {
295 2244 sgarg
      testdocument = getTestEmlDoc("Testing update", testEmlInlineBlock1, null,
296
                                   null, null);
297 2239 sgarg
      String identifier = newdocid + ".2";
298
      m.login(username, password);
299
      String response = m.update(identifier,
300
                                 new StringReader(testdocument), null);
301
      assertTrue(response.indexOf("<success>") != -1);
302
      assertTrue(response.indexOf(identifier) != -1);
303
      System.err.println(response);
304
305
    }
306
    catch (MetacatAuthException mae) {
307
      fail("Authorization failed:\n" + mae.getMessage());
308
    }
309
    catch (MetacatInaccessibleException mie) {
310
      fail("Metacat Inaccessible:\n" + mie.getMessage());
311
    }
312
    catch (InsufficientKarmaException ike) {
313
      fail("Insufficient karma:\n" + ike.getMessage());
314
    }
315
    catch (MetacatException me) {
316
      fail("Metacat Error:\n" + me.getMessage());
317
    }
318
    catch (Exception e) {
319
      fail("General exception:\n" + e.getMessage());
320
    }
321
  }
322
323
  /**
324
   * Test the delete() function with a known document
325
   */
326
  public void delete() {
327
    try {
328
      String identifier = newdocid + ".2";
329
      m.login(username, password);
330
      String response = m.delete(identifier);
331
      assertTrue(response.indexOf("<success>") != -1);
332
      System.err.println(response);
333
334
    }
335
    catch (MetacatAuthException mae) {
336
      fail("Authorization failed:\n" + mae.getMessage());
337
    }
338
    catch (MetacatInaccessibleException mie) {
339
      fail("Metacat Inaccessible:\n" + mie.getMessage());
340
    }
341
    catch (InsufficientKarmaException ike) {
342
      fail("Insufficient karma:\n" + ike.getMessage());
343
    }
344
    catch (MetacatException me) {
345
      fail("Metacat Error:\n" + me.getMessage());
346
    }
347
    catch (Exception e) {
348
      fail("General exception:\n" + e.getMessage());
349
    }
350
  }
351
352 2244 sgarg
353
354
  /** *********
355 2239 sgarg
   * Test the case when no access is specified and owner is logged in
356 2244 sgarg
   * Cases being checked:
357
   * 1. the owner tries to read the document  - Success.
358
   *                                            Both document and data show up.
359
   * 2. the owner tries to update the document - Success.
360
   * 3. the owner tries to update the inline data - Success.
361
   * 4. the owner tries to update the online data file - Success.
362
   * 5. the owner tries to update the access rules for the document - Success.
363
   * 6. the owner tries to update the access rules for the inline data - Success.
364
   * 7. the owner tries to update the access rules for the online data - Success.
365
   * 8. the owner tries to delete the document - Success.
366 2239 sgarg
   */
367 2244 sgarg
  public void noAccessSpecified_Owner() {
368 2239 sgarg
    try {
369
      newdocid = generateDocid();
370 2244 sgarg
371 2239 sgarg
      // login
372
      m.login(username, password);
373
374 2244 sgarg
      // upload online document
375
      onlineDocid = generateDocid();
376
      uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS);
377
378 2239 sgarg
      // insert a document - get the docid
379 2244 sgarg
      testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
380
                                   null, "ecogrid://knb/" + onlineDocid, null);
381 2239 sgarg
      insertDocid(newdocid + ".1", testdocument, SUCCESS);
382 2244 sgarg
      readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS);
383 2239 sgarg
384
      // update the document
385 2244 sgarg
      testdocument = getTestEmlDoc("Testing update", testEmlInlineBlock1,
386
                                   null, null, null);
387 2239 sgarg
      updateDocid(newdocid + ".2", testdocument, SUCCESS);
388 2244 sgarg
      readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS);
389 2239 sgarg
390
      // update the inline data
391 2244 sgarg
      testdocument = getTestEmlDoc("Testing update inline", testEmlInlineBlock2,
392
                                   null, null, null);
393 2239 sgarg
      updateDocid(newdocid + ".3", testdocument, SUCCESS);
394 2244 sgarg
      readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS);
395 2239 sgarg
396
      // update the online data
397 2244 sgarg
      uploadDocid(onlineDocid + ".2", onlinetestdatafile2, SUCCESS);
398 2247 sgarg
      //  read document and check if it is same as the document which was written
399
      // the second time
400 2239 sgarg
401
      // update the document access control
402 2247 sgarg
      testdocument = getTestEmlDoc("Testing update inline", testEmlInlineBlock2,
403
                                   null, null, null);
404
      updateDocid(newdocid + ".5", testdocument, SUCCESS);
405
      readDocidWhichEqualsDoc(newdocid + ".5", testdocument, SUCCESS);
406 2239 sgarg
407
      // update the document access control for inline data
408 2247 sgarg
      testdocument = getTestEmlDoc("Testing update inline", testEmlInlineBlock2,
409
                                   null, null, null);
410
      updateDocid(newdocid + ".6", testdocument, SUCCESS);
411
      readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS);
412 2239 sgarg
413
      // update the document access control for online data
414 2247 sgarg
      testdocument = getTestEmlDoc("Testing update inline", testEmlInlineBlock2,
415
                                   null, null, null);
416
      updateDocid(newdocid + ".7", testdocument, SUCCESS);
417
      readDocidWhichEqualsDoc(newdocid + ".7", testdocument, SUCCESS);
418 2239 sgarg
419
      // delete the document
420 2247 sgarg
      deleteDocid(newdocid + ".3", SUCCESS);
421 2239 sgarg
      m.logout();
422
    }
423
    catch (MetacatAuthException mae) {
424
      fail("Authorization failed:\n" + mae.getMessage());
425
    }
426
    catch (MetacatInaccessibleException mie) {
427
      fail("Metacat Inaccessible:\n" + mie.getMessage());
428
    }
429
    catch (Exception e) {
430
      fail("General exception:\n" + e.getMessage());
431
    }
432
433
  }
434
435 2244 sgarg
436
437
  /** *********
438 2239 sgarg
   * Test the case when no access is specified and another user is logged in
439 2244 sgarg
   * Cases being checked:
440
   * 1. the owner tries to read the document - Failure.
441
   * 2. the owner tries to update the document - Failure.
442
   * 3. the owner tries to update the inline data - Failure.
443
   * 4. the owner tries to update the online data file - Failure.
444
   * 5. the owner tries to update the access rules for the document - Failure.
445
   * 6. the owner tries to update the access rules for the inline data - Failure.
446
   * 7. the owner tries to update the access rules for the online data - Failure.
447
   * 8. the owner tries to delete the document - Failure.
448 2239 sgarg
   */
449 2244 sgarg
  public void noAccessSpecified_Random() {
450 2239 sgarg
    try {
451
      newdocid = generateDocid();
452
      String identifier = newdocid + ".1";
453
      // login
454
      m.login(username, password);
455
456
      // insert a document - get the docid
457
      insertDocid(newdocid + ".1", testdocument, SUCCESS);
458
      readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS);
459
460
      // logoutand login as other user
461
      m.logout();
462
      m.login(anotheruser, anotherpassword);
463
464
      // read the document
465
      readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE);
466
467
      // update the document
468
      updateDocid(newdocid + ".2", testdocument, FAILURE);
469
470
      // update the inline data
471
      updateDocid(newdocid + ".2", testdocument, FAILURE);
472
473
      // update the online data
474
      updateDocid(newdocid + ".2", testdocument, FAILURE);
475
476
      // update the document access control
477
      updateDocid(newdocid + ".2", testdocument, FAILURE);
478
479
      // update the document access control for inline data
480
      updateDocid(newdocid + ".2", testdocument, FAILURE);
481
482
      // update the document access control for online data
483
      updateDocid(newdocid + ".2", testdocument, FAILURE);
484
485
      // delete the document
486
      deleteDocid(newdocid + ".2", SUCCESS);
487
      m.logout();
488
    }
489
    catch (MetacatAuthException mae) {
490
      fail("Authorization failed:\n" + mae.getMessage());
491
    }
492
    catch (MetacatInaccessibleException mie) {
493
      fail("Metacat Inaccessible:\n" + mie.getMessage());
494
    }
495
    catch (MetacatException me) {
496
      fail("Metacat Error:\n" + me.getMessage());
497
    }
498
    catch (Exception e) {
499
      fail("General exception:\n" + e.getMessage());
500
    }
501
502
  }
503
504 2244 sgarg
505
506
  /** *********
507 2239 sgarg
   * Test the case when no access is specified and public is logged in
508 2244 sgarg
   * Cases being checked:
509
   * 1. the owner tries to read the document - Failure.
510
   * 2. the owner tries to update the document - Failure.
511
   * 3. the owner tries to update the inline data - Failure.
512
   * 4. the owner tries to update the online data file - Failure.
513
   * 5. the owner tries to update the access rules for the document - Failure.
514
   * 6. the owner tries to update the access rules for the inline data - Failure.
515
   * 7. the owner tries to update the access rules for the online data - Failure.
516
   * 8. the owner tries to delete the document - Failure.
517 2239 sgarg
   */
518 2244 sgarg
  public void noAccessSpecified_Public() {
519 2239 sgarg
    try {
520
      newdocid = generateDocid();
521
      String identifier = newdocid + ".1";
522
      // login
523
      m.login(username, password);
524
525
      // insert a document - get the docid
526
      insertDocid(newdocid + ".1", testdocument, SUCCESS);
527
      readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS);
528
529
      // logoutand login as other user
530
      m.logout();
531
532
      // read the document
533
      readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE);
534
535
      // update the document
536
      updateDocid(newdocid + ".2", testdocument, FAILURE);
537
538
      // update the inline data
539
      updateDocid(newdocid + ".2", testdocument, FAILURE);
540
541
      // update the online data
542
      updateDocid(newdocid + ".2", testdocument, FAILURE);
543
544
      // update the document access control
545
      updateDocid(newdocid + ".2", testdocument, FAILURE);
546
547
      // update the document access control for inline data
548
      updateDocid(newdocid + ".2", testdocument, FAILURE);
549
550
      // update the document access control for online data
551
      updateDocid(newdocid + ".2", testdocument, FAILURE);
552
553
      // delete the document
554
      deleteDocid(newdocid + ".2", SUCCESS);
555
      m.logout();
556
    }
557
    catch (MetacatAuthException mae) {
558
      fail("Authorization failed:\n" + mae.getMessage());
559
    }
560
    catch (MetacatInaccessibleException mie) {
561
      fail("Metacat Inaccessible:\n" + mie.getMessage());
562
    }
563
    catch (MetacatException me) {
564
      fail("Metacat Error:\n" + me.getMessage());
565
    }
566
    catch (Exception e) {
567
      fail("General exception:\n" + e.getMessage());
568
    }
569
570
  }
571
572 2244 sgarg
573
  /**
574
   * Insert a document into metacat. The expected result is passed as result
575
   */
576
577 2239 sgarg
  public String insertDocid(String docid, String docText, boolean result) {
578
    String response = null;
579
    try {
580
      response = m.insert(docid,
581
                          new StringReader(testdocument), null);
582
      if (result) {
583
        assertTrue( (response.indexOf("<success>") != -1));
584
        assertTrue(response.indexOf(docid) != -1);
585
      }
586
      else {
587
        assertTrue( (response.indexOf("<success>") == -1));
588
      }
589
      System.err.println(response);
590
    }
591
    catch (MetacatInaccessibleException mie) {
592
      fail("Metacat Inaccessible:\n" + mie.getMessage());
593
    }
594
    catch (InsufficientKarmaException ike) {
595
      fail("Insufficient karma:\n" + ike.getMessage());
596
    }
597
    catch (MetacatException me) {
598
      fail("Metacat Error:\n" + me.getMessage());
599
    }
600
    catch (Exception e) {
601
      fail("General exception:\n" + e.getMessage());
602
    }
603
    return response;
604
  }
605
606 2244 sgarg
607
  /**
608
   * Insert a document into metacat. The expected result is passed as result
609
   */
610
611
  public String uploadDocid(String docid, String filePath, boolean result) {
612
    String response = null;
613
    try {
614
      response = m.upload(docid, new File(filePath));
615
616
      if (result) {
617
        assertTrue( (response.indexOf("<success>") != -1));
618
        assertTrue(response.indexOf(docid) != -1);
619
      }
620
      else {
621
        assertTrue( (response.indexOf("<success>") == -1));
622
      }
623
      System.err.println(response);
624
    }
625
    catch (MetacatInaccessibleException mie) {
626
      fail("Metacat Inaccessible:\n" + mie.getMessage());
627
    }
628
    catch (InsufficientKarmaException ike) {
629
      fail("Insufficient karma:\n" + ike.getMessage());
630
    }
631
    catch (MetacatException me) {
632
      fail("Metacat Error:\n" + me.getMessage());
633
    }
634
    catch (Exception e) {
635
      fail("General exception:\n" + e.getMessage());
636
    }
637
    return response;
638
  }
639
640
  /**
641
   * Update a document in metacat. The expected result is passed as result
642
   */
643 2239 sgarg
  public String updateDocid(String docid, String docText, boolean result) {
644
    String response = null;
645
    try {
646
      response = m.update(docid,
647 2244 sgarg
                          new StringReader(testdocument), null);
648
649 2239 sgarg
      if (result) {
650
        assertTrue( (response.indexOf("<success>") != -1));
651
        assertTrue(response.indexOf(docid) != -1);
652
      }
653
      else {
654
        assertTrue( (response.indexOf("<success>") == -1));
655
      }
656
      System.err.println(response);
657
    }
658
    catch (MetacatInaccessibleException mie) {
659
      fail("Metacat Inaccessible:\n" + mie.getMessage());
660
    }
661
    catch (InsufficientKarmaException ike) {
662
      fail("Insufficient karma:\n" + ike.getMessage());
663
    }
664
    catch (MetacatException me) {
665
      fail("Metacat Error:\n" + me.getMessage());
666
    }
667
    catch (Exception e) {
668
      fail("General exception:\n" + e.getMessage());
669
    }
670
671
    return response;
672
  }
673
674 2244 sgarg
  /**
675
   * Delete a document into metacat. The expected result is passed as result
676
   */
677 2239 sgarg
  public void deleteDocid(String docid, boolean result) {
678
    try {
679
      String response = m.delete(docid);
680
      if (result) {
681
        assertTrue(response.indexOf("<success>") != -1);
682
      }
683
      else {
684
        assertTrue(response.indexOf("<success>") == -1);
685
      }
686
      System.err.println(response);
687
    }
688
    catch (MetacatInaccessibleException mie) {
689
      fail("Metacat Inaccessible:\n" + mie.getMessage());
690
    }
691
    catch (InsufficientKarmaException ike) {
692
      fail("Insufficient karma:\n" + ike.getMessage());
693
    }
694
    catch (MetacatException me) {
695
      fail("Metacat Error:\n" + me.getMessage());
696
    }
697
    catch (Exception e) {
698
      fail("General exception:\n" + e.getMessage());
699
    }
700
  }
701
702 2244 sgarg
  /**
703
   * Read a document from metacat and check if it is equal to a given string.
704
   * The expected result is passed as result
705
   */
706
707 2239 sgarg
  public void readDocidWhichEqualsDoc(String docid, String testDoc, boolean result) {
708
    try {
709
      Reader r = m.read(docid);
710
      String doc = IOUtil.getAsString(r, true);
711
      if (result) {
712
        assertTrue(testDoc.equals(doc));
713
      } else {
714
        assertTrue(doc.indexOf("<error>") != -1);
715
      }
716
    }
717
    catch (MetacatInaccessibleException mie) {
718
      fail("Metacat Inaccessible:\n" + mie.getMessage());
719
    }
720
    catch (InsufficientKarmaException ike) {
721
      fail("Insufficient karma:\n" + ike.getMessage());
722
    }
723
    catch (MetacatException me) {
724
      fail("Metacat Error:\n" + me.getMessage());
725
    }
726
    catch (Exception e) {
727
      fail("General exception:\n" + e.getMessage());
728
    }
729
730
  }
731
732
  /**
733
   * Create a hopefully unique docid for testing insert and update. Does
734
   * not include the 'revision' part of the id.
735
   *
736
   * @return a String docid based on the current date and time
737
   */
738
  private String generateDocid() {
739
    StringBuffer docid = new StringBuffer(prefix);
740
    docid.append(".");
741
742
    // Create a calendar to get the date formatted properly
743
    String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
744
    SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
745
    pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
746
    pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
747
    Calendar calendar = new GregorianCalendar(pdt);
748
    Date trialTime = new Date();
749
    calendar.setTime(trialTime);
750
    docid.append(calendar.get(Calendar.YEAR));
751
    docid.append(calendar.get(Calendar.DAY_OF_YEAR));
752
    docid.append(calendar.get(Calendar.HOUR_OF_DAY));
753
    docid.append(calendar.get(Calendar.MINUTE));
754
    docid.append(calendar.get(Calendar.SECOND));
755
756
    return docid.toString();
757
  }
758
}