Project

General

Profile

1 1114 tao
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5 3080 jones
 *    Purpose: To test the MetaCatURL class by JUnit
6
 *    Authors: Jing Tao
7 1114 tao
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27 1117 tao
package edu.ucsb.nceas.metacatnettest;
28 1114 tao
29
import edu.ucsb.nceas.metacat.*;
30
import edu.ucsb.nceas.morpho.framework.*;
31
import junit.framework.Test;
32
import junit.framework.TestCase;
33
import junit.framework.TestResult;
34
import junit.framework.TestSuite;
35
36
import java.io.*;
37
import java.net.*;
38
import java.util.*;
39
40 3169 tao
import org.apache.log4j.Logger;
41 1114 tao
42 3169 tao
43 1114 tao
/**
44
 * A JUnit test for testing Step class processing
45
 */
46
public class MetaCatServletNetTest extends TestCase
47
{
48
  private String metacatURL=MetaCatUtil.getOption("junittesturl");
49
  private String serialNumber;
50 3169 tao
  private static Logger logMetacat = Logger.getLogger(DocumentImpl.class);
51 1114 tao
  /**
52
   * Constructor to build the test
53
   *
54
   * @param name the name of the test method
55
   */
56
  public MetaCatServletNetTest(String name)
57
  {
58
    super(name);
59
  }
60
61
 /**
62
   * Constructor to build the test
63
   *
64
   * @param name the name of the test method
65
   */
66
  public MetaCatServletNetTest(String name, String serial)
67
  {
68
    super(name);
69
    serialNumber=serial;
70
  }
71
  /**
72
   * Establish a testing framework by initializing appropriate objects
73
   */
74
  public void setUp()
75
 {
76
77
 }
78
79
  /**
80
   * Release any objects after tests are complete
81
   */
82
  public void tearDown()
83
  {
84
  }
85
86
  /**
87
   * Create a suite of tests to be run together
88
   */
89
  public static Test suite()
90
  {
91
    double number = 0;
92
    String serial = null;
93
94
95
    TestSuite suite = new TestSuite();
96
    suite.addTest(new MetaCatServletNetTest("initialize"));
97
    suite.addTest(new MetaCatServletNetTest("testLterReferralLogin"));
98
    suite.addTest(new MetaCatServletNetTest("testLterReferralLoginFail"));
99
    suite.addTest(new MetaCatServletNetTest("testPiscoReferralLogin"));
100
    suite.addTest(new MetaCatServletNetTest("testPiscoReferralLoginFail"));
101
    suite.addTest(new MetaCatServletNetTest("testNCEASLoginFail"));
102
    //Should put a login successfully at the end of login test
103
    //So insert or update can have cookie.
104
    suite.addTest(new MetaCatServletNetTest("testNCEASLogin"));
105
106
    //create random number for docid, so it can void repeat
107 1117 tao
    number = Math.random()*100000;
108 1114 tao
    serial = Integer.toString(((new Double(number)).intValue()));
109 3169 tao
    logMetacat.info("serial: "+serial);
110 1114 tao
    suite.addTest(new MetaCatServletNetTest("testInsertXMLDocument", serial));
111
    suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentXMLFormat",
112
                                                        serial));
113 1222 tao
    suite.addTest(new MetaCatServletNetTest("testUpdateXMLDocument",serial));
114 1114 tao
    suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentHTMLFormat",
115
                                                        serial));
116
    suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentZipFormat",
117
                                                        serial));
118 1222 tao
119 1114 tao
    suite.addTest(new MetaCatServletNetTest("testDeleteXMLDocument",serial));
120
121
    //insert invalid xml document
122 1117 tao
    number = Math.random()*100000;
123 1114 tao
    serial = Integer.toString(((new Double(number)).intValue()));
124
    suite.addTest(new MetaCatServletNetTest("testInsertInvalidateXMLDocument",
125
                                                                  serial));
126
    //insert non well formed document
127 1117 tao
    number = Math.random()*100000;
128 1114 tao
    serial = Integer.toString(((new Double(number)).intValue()));
129
   suite.addTest(new MetaCatServletNetTest("testInsertNonWellFormedXMLDocument",
130
                                                            serial));
131
    //insert data file
132 1117 tao
    number = Math.random()*100000;
133 1114 tao
    serial = Integer.toString(((new Double(number)).intValue()));
134
    suite.addTest(new MetaCatServletNetTest("testInertDataFile", serial));
135
136
    suite.addTest(new MetaCatServletNetTest("testLogOut"));
137
138
    return suite;
139
  }
140
141
142
143
  /**
144
   * Run an initial test that always passes to check that the test
145
   * harness is working.
146
   */
147
  public void initialize()
148
  {
149 1504 tao
    assertTrue(1 == 1);
150 1114 tao
  }
151
152
  /**
153
   * Test the login to neceas succesfully
154
   */
155
  public void testNCEASLogin()
156
  {
157
    String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
158
    String passwd="123456";
159 1504 tao
    assertTrue(logIn(user,passwd));
160
    //assertTrue( withProtocol.getProtocol().equals("http"));
161 1114 tao
  }
162
163
  /**
164
   * Test the login to neceas failed
165
   */
166
  public void testNCEASLoginFail()
167
  {
168
    String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
169
    String passwd="12345678";
170 1504 tao
    assertTrue(!logIn(user,passwd));
171 1114 tao
172
  }
173
174
  /**
175
   * Test the login to lter succesfully
176
   */
177
  public void testLterReferralLogin()
178
  {
179
    String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
180
    String passwd="qVyGpVeb";
181 1504 tao
    assertTrue(logIn(user,passwd));
182 1114 tao
183
  }
184
185
  /**
186
   * Test the login to lter failed
187
   */
188
  public void testLterReferralLoginFail()
189
  {
190
    String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
191
    String passwd="qVyGpveb";
192 1504 tao
    assertTrue(!logIn(user,passwd));
193
    //assertTrue( withProtocol.getProtocol().equals("http"));
194 1114 tao
  }
195
196
 /**
197
   * Test the login to pisco succesfully
198
   */
199
   public void testPiscoReferralLogin()
200
  {
201
    String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
202
    String passwd="purple";
203 1504 tao
    assertTrue(logIn(user,passwd));
204
    //assertTrue( withProtocol.getProtocol().equals("http"));
205 1114 tao
  }
206
207
 /**
208
   * Test the login to pisco failed
209
   */
210
  public void testPiscoReferralLoginFail()
211
  {
212
    String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
213
    String passwd="hello";
214 1504 tao
    assertTrue(!logIn(user,passwd));
215
    //assertTrue( withProtocol.getProtocol().equals("http"));
216 1114 tao
  }
217
218
 /**
219
   * Test insert a xml document successfully
220
   */
221
  public void testInsertXMLDocument()
222
  {
223
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
224
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
225 3169 tao
    logMetacat.info("insert docid: "+name);
226 1114 tao
    String content="<?xml version=\"1.0\"?>"
227
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
228
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
229
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
230
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
231
                   +"<identifier>"+name+"</identifier>"
232
                   +"<allow>"
233
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
234
                   +"<permission>all</permission>"
235
                   +"</allow>"
236
                   +"<allow>"
237
                   +"<principal>public</principal>"
238
                   +"<permission>read</permission>"
239
                   +"</allow>"
240
                   +"</acl>";
241 3169 tao
    logMetacat.info("xml document: "+content);
242 1504 tao
     assertTrue(handleXMLDocument(content, name, "insert"));
243 1114 tao
244
  }
245
246
  /**
247
   * Test insert a invalidate xml document successfully
248
   * In the String, there is no <!Doctype ... Public/System/>
249
   */
250
  public void testInsertInvalidateXMLDocument()
251
  {
252
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
253
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
254 3169 tao
    logMetacat.info("insert docid: "+name);
255 1114 tao
    String content="<?xml version=\"1.0\"?>"
256
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
257
                   +"<identifier>"+name+"</identifier>"
258
                   +"<allow>"
259
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
260
                   +"<permission>all</permission>"
261
                   +"</allow>"
262
                   +"<allow>"
263
                   +"<principal>public</principal>"
264
                   +"<permission>read</permission>"
265
                   +"</allow>"
266
                   +"</acl>";
267 3169 tao
    logMetacat.info("xml document: "+content);
268 1504 tao
     assertTrue(handleXMLDocument(content, name, "insert"));
269 1114 tao
  }
270
271
   /**
272
   * Test insert a non well-formed xml document successfully
273
   * There is no </acl> in this string
274
   */
275
  public void testInsertNonWellFormedXMLDocument()
276
  {
277
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
278
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
279 3169 tao
    logMetacat.info("insert non well-formed docid: "+name);
280 1114 tao
    String content="<?xml version=\"1.0\"?>"
281
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
282
                   +"<identifier>"+name+"</identifier>"
283
                   +"<allow>"
284
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
285
                   +"<permission>all</permission>"
286
                   +"</allow>"
287
                   +"<allow>"
288
                   +"<principal>public</principal>"
289
                   +"<permission>read</permission>"
290
                   +"</allow>";
291
292 3169 tao
     logMetacat.info("xml document: "+content);
293 1504 tao
     assertTrue(!handleXMLDocument(content, name, "insert"));
294 1114 tao
  }
295
296
  /**
297
   * Test read a xml document  in xml format successfully
298
   */
299
  public void testReadXMLDocumentXMLFormat()
300
  {
301
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
302
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
303 1504 tao
    assertTrue(handleReadAction(name, "xml"));
304 1114 tao
305
  }
306
307
  /**
308
   * Test read a xml document  in html format successfully
309
   */
310
  public void testReadXMLDocumentHTMLFormat()
311
  {
312
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
313
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
314 1504 tao
    assertTrue(handleReadAction(name, "html"));
315 1114 tao
316
  }
317
318
319
  /**
320
   * Test read a xml document  in zip format successfully
321
   */
322
  public void testReadXMLDocumentZipFormat()
323
  {
324
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
325
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
326 1504 tao
    assertTrue(handleReadAction(name, "zip"));
327 1114 tao
328
  }
329
330
  /**
331
   * Test insert a xml document successfully
332
   */
333
  public void testUpdateXMLDocument()
334
  {
335
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
336
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
337 3169 tao
    logMetacat.info("update docid: "+name);
338 1114 tao
    String content="<?xml version=\"1.0\"?>"
339
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
340
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
341
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
342
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
343
                   +"<identifier>"+name+"</identifier>"
344
                   +"<allow>"
345
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
346
                   +"<permission>all</permission>"
347
                   +"</allow>"
348
                   +"<allow>"
349
                   +"<principal>public</principal>"
350
                   +"<permission>read</permission>"
351
                   +"</allow>"
352
                   +"</acl>";
353 3169 tao
     logMetacat.info("xml document: "+content);
354 1504 tao
     assertTrue(handleXMLDocument(content, name, "update"));
355 1114 tao
356
  }
357
358
  /**
359
   * Test insert a data file successfully
360
   */
361
  public void testInertDataFile()
362
  {
363
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
364
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
365 3169 tao
    logMetacat.info("insert data file docid: "+name);
366
    logMetacat.info("insert data file ");
367 1117 tao
    File hello = new File("test/edu/ucsb/nceas/metacatnettest/hello.txt");
368 1114 tao
369 1504 tao
    assertTrue(insertDataFile(name, hello));
370 1114 tao
  }
371
372
  /**
373
   * Test delete a xml document successfully
374
   */
375
  public void testDeleteXMLDocument()
376
  {
377
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
378
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
379 3169 tao
    logMetacat.info("delete docid: "+name);
380 1504 tao
    assertTrue(handleDeleteFile(name));
381 1114 tao
382
  }
383
384
385
386
  /**
387
   * Test logout action
388
   */
389
  public void testLogOut()
390
  {
391
392 1504 tao
    assertTrue(handleLogOut());
393 1114 tao
394
  }
395
396
 /**
397
   * Method to hanld login action
398
   *
399
   * @param usrerName, the DN name of the test method
400
   * @param passWord, the passwd of the user
401
   */
402
403
  public boolean logIn(String userName, String passWord)
404
  {
405
    Properties prop = new Properties();
406
    prop.put("action", "login");
407
    prop.put("qformat", "xml");
408
    prop.put("username", userName);
409
    prop.put("password", passWord);
410
411
    // Now contact metacat
412
    String response = getMetacatString(prop);
413 3169 tao
    logMetacat.info("Login Message: "+response);
414 1114 tao
    boolean connected = false;
415
    if (response.indexOf("<login>") != -1) {
416
      connected = true;
417
    } else {
418
419
      connected = false;
420
    }
421
422
    return connected;
423
  }
424
425
  /**
426
   * Method to hanld logout action
427
   *
428
   * @param usrerName, the DN name of the test method
429
   * @param passWord, the passwd of the user
430
   */
431
432
  public boolean handleLogOut()
433
  {
434
    boolean disConnected =false;
435
    Properties prop = new Properties();
436
    prop.put("action", "logout");
437
    prop.put("qformat", "xml");
438
439
    String response = getMetacatString(prop);
440 3169 tao
    logMetacat.info("Logout Message: "+response);
441 1114 tao
    edu.ucsb.nceas.morpho.framework.HttpMessage.setCookie(null);
442
443
    if (response.indexOf("<logout>") != -1)
444
    {
445
      disConnected = true;
446
    }
447
    else
448
    {
449
      disConnected = false;
450
    }
451
452
    return disConnected;
453
  }
454
455
  /**
456
   * Method to hanld read both xml and data file
457
   *
458
   * @param docid, the docid of the document want to read
459
   * @param qformat, the format of document user want to get
460
   */
461
  public boolean handleReadAction(String docid, String qformat)
462
  {
463
    Properties prop = new Properties();
464
    String message ="";
465
    prop.put("action", "read");
466
    prop.put("qformat", qformat);
467
    prop.put("docid", docid);
468
469
    message = getMetacatString(prop);
470
    message = message.trim();
471
    //MetaCatUtil.debugMessage("Read Message: "+message, 30);
472
    if (message==null || message.equals("")||message.indexOf("<error>") != -1 )
473
    {//there was an error
474
475
      return false;
476
    }
477
    else
478
    {//successfully
479
      return true;
480
    }
481
482
  }
483
484
485
  /**
486
   * Method to hanld inset or update xml document
487
   *
488
   * @param xmlDocument, the content of xml qformat
489
   * @param docid, the docid of the document
490
   * @param action, insert or update
491
   */
492
  public boolean handleXMLDocument(String xmlDocument, String docid,
493
                                                              String action)
494
495
  { //-attempt to write file to metacat
496
    String access = "no";
497
    StringBuffer fileText = new StringBuffer();
498
    StringBuffer messageBuf = new StringBuffer();
499
    String accessFileId = null;
500
    Properties prop = new Properties();
501
    prop.put("action", action);
502
    prop.put("public", access);  //This is the old way of controlling access
503
    prop.put("doctext", xmlDocument);
504
    prop.put("docid", docid);
505
506
    String message = getMetacatString(prop);
507 3169 tao
    logMetacat.info("Insert or Update Message: "+message);
508 1114 tao
    if(message.indexOf("<error>") != -1)
509
    {//there was an error
510
511
      return false;
512
    }
513
    else if(message.indexOf("<success>") != -1)
514
    {//the operation worked
515
     //write the file to the cache and return the file object
516
     return true;
517
518
    }
519
    else
520
    {//something weird happened.
521
      return false;
522
    }
523
524
525
  }
526
527
   public boolean handleDeleteFile(String name)
528
  {
529
530
    Properties prop = new Properties();
531
    prop.put("action", "delete");
532
    prop.put("docid", name);
533
534
    String message = getMetacatString(prop);
535 3169 tao
    logMetacat.info("Delete Message: "+message);
536 1114 tao
    if(message.indexOf("<error>") != -1)
537
    {//there was an error
538
539
      return false;
540
    }
541
    else if(message.indexOf("<success>") != -1)
542
    {//the operation worked
543
     //write the file to the cache and return the file object
544
     return true;
545
546
    }
547
    else
548
    {//something weird happened.
549
      return false;
550
    }
551
  }
552
553
554
  /**
555
   * sends a data file to the metacat using "multipart/form-data" encoding
556
   *
557
   * @param id the id to assign to the file on metacat (e.g., knb.1.1)
558
   * @param file the file to send
559
   */
560
  public boolean insertDataFile(String id, File file)
561
  {
562
    String response = null;
563
    //Get response for calling sendDataFile function
564
    response = sendDataFile(id, file);
565
566
    if (response.indexOf("success") != -1)
567
    {
568
      //insert successfully
569
      return true;
570
    }
571
    else
572
    {
573
      return false;
574
    }
575
  }
576
577
  /**
578
   * sends a data file to the metacat using "multipart/form-data" encoding
579
   *
580
   * @param id the id to assign to the file on metacat (e.g., knb.1.1)
581
   * @param file the file to send
582
   */
583
  public String sendDataFile(String id, File file)
584
  {
585
    String response = "";
586
    InputStream returnStream = null;
587
588
    // Now contact metacat and send the request
589
    try
590
    {
591
      //FileInputStream data = new FileInputStream(file);
592
      System.setProperty("java.protocol.handler.pkgs","HTTPClient");
593
      URL url = new URL(metacatURL);
594
      edu.ucsb.nceas.morpho.framework.HttpMessage msg =
595
                        new edu.ucsb.nceas.morpho.framework.HttpMessage(url);
596
      Properties args = new Properties();
597
      args.put("action", "upload");
598
      args.put("docid", id);
599
600
      Properties dataStreams = new Properties();
601
      String filename = file.getAbsolutePath();
602
603
      dataStreams.put("datafile", filename);
604
605
606
607
      returnStream = msg.sendPostData(args, dataStreams);
608
609
      InputStreamReader returnStreamReader =
610
                        new InputStreamReader(returnStream);
611
      StringWriter sw = new StringWriter();
612
      int len;
613
      char[] characters = new char[512];
614
      while ((len = returnStreamReader.read(characters, 0, 512)) != -1)
615
      {
616
        sw.write(characters, 0, len);
617
      }
618
      returnStreamReader.close();
619
      response = sw.toString();
620
      sw.close();
621
622
    }
623
    catch(Exception e)
624
    {
625
      e.printStackTrace(System.err);
626
    }
627
    return response;
628
  }
629
630
  public String getMetacatString(Properties prop)
631
  {
632
    String response = null;
633
634
    // Now contact metacat and send the request
635
    try
636
    {
637
      InputStreamReader returnStream =
638
                        new InputStreamReader(getMetacatInputStream(prop));
639
      StringWriter sw = new StringWriter();
640
      int len;
641
      char[] characters = new char[512];
642
      while ((len = returnStream.read(characters, 0, 512)) != -1)
643
      {
644
        sw.write(characters, 0, len);
645
      }
646
      returnStream.close();
647
      response = sw.toString();
648
      sw.close();
649
    }
650
    catch(Exception e)
651
    {
652
      return null;
653
    }
654
655
    return response;
656
  }
657
658
   /**
659
   * Send a request to Metacat
660
   *
661
   * @param prop the properties to be sent to Metacat
662
   * @return InputStream as returned by Metacat
663
   */
664
  public InputStream getMetacatInputStream(Properties prop)
665
  {
666
    InputStream returnStream = null;
667
    // Now contact metacat and send the request
668
    try
669
    {
670
671
      URL url = new URL(metacatURL);
672
      edu.ucsb.nceas.morpho.framework.HttpMessage msg =
673
              new edu.ucsb.nceas.morpho.framework.HttpMessage(url);
674
      returnStream = msg.sendPostMessage(prop);
675
      return returnStream;
676
    }
677
    catch(Exception e)
678
    {
679
      e.printStackTrace(System.err);
680
681
    }
682
    return returnStream;
683
684
  }
685
686
687
688
}