Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the MetaCatURL class by JUnit
6
 *    Authors: @Jing Tao@
7
 *    Release: @release@
8
 *
9
 *   '$Author: tao $'
10
 *     '$Date: 2002-05-22 20:17:27 -0700 (Wed, 22 May 2002) $'
11
 * '$Revision: 1117 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacatnettest;
29

    
30
import edu.ucsb.nceas.metacat.*;
31
import edu.ucsb.nceas.morpho.framework.*;
32
import junit.framework.Test;
33
import junit.framework.TestCase;
34
import junit.framework.TestResult;
35
import junit.framework.TestSuite;
36

    
37
import java.io.*;
38
import java.net.*;
39
import java.util.*;
40

    
41

    
42
/**
43
 * A JUnit test for testing Step class processing
44
 */
45
public class MetaCatServletNetTest extends TestCase
46
{
47
  private String metacatURL=MetaCatUtil.getOption("junittesturl");
48
  private String serialNumber;
49
  /**
50
   * Constructor to build the test
51
   *
52
   * @param name the name of the test method
53
   */
54
  public MetaCatServletNetTest(String name)
55
  {
56
    super(name);
57
  }
58

    
59
 /**
60
   * Constructor to build the test
61
   *
62
   * @param name the name of the test method
63
   */
64
  public MetaCatServletNetTest(String name, String serial)
65
  {
66
    super(name);
67
    serialNumber=serial;
68
  }
69
  /**
70
   * Establish a testing framework by initializing appropriate objects
71
   */
72
  public void setUp()
73
 {
74
    
75
 }
76

    
77
  /**
78
   * Release any objects after tests are complete
79
   */
80
  public void tearDown()
81
  {
82
  }
83

    
84
  /**
85
   * Create a suite of tests to be run together
86
   */
87
  public static Test suite()
88
  {
89
    double number = 0;
90
    String serial = null;
91
    
92
    
93
    TestSuite suite = new TestSuite();
94
    suite.addTest(new MetaCatServletNetTest("initialize"));
95
    suite.addTest(new MetaCatServletNetTest("testLterReferralLogin"));
96
    suite.addTest(new MetaCatServletNetTest("testLterReferralLoginFail"));
97
    suite.addTest(new MetaCatServletNetTest("testPiscoReferralLogin"));
98
    suite.addTest(new MetaCatServletNetTest("testPiscoReferralLoginFail"));
99
    suite.addTest(new MetaCatServletNetTest("testNCEASLoginFail"));
100
    //Should put a login successfully at the end of login test
101
    //So insert or update can have cookie.
102
    suite.addTest(new MetaCatServletNetTest("testNCEASLogin"));
103
    
104
    //create random number for docid, so it can void repeat
105
    number = Math.random()*100000;
106
    serial = Integer.toString(((new Double(number)).intValue()));
107
    MetaCatUtil.debugMessage("serial: "+serial, 1);
108
    suite.addTest(new MetaCatServletNetTest("testInsertXMLDocument", serial));
109
    suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentXMLFormat", 
110
                                                        serial));
111
        
112
    suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentHTMLFormat", 
113
                                                        serial));
114
    suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentZipFormat", 
115
                                                        serial));
116
    suite.addTest(new MetaCatServletNetTest("testUpdateXMLDocument",serial));
117
    suite.addTest(new MetaCatServletNetTest("testDeleteXMLDocument",serial));
118
    
119
    //insert invalid xml document
120
    number = Math.random()*100000;
121
    serial = Integer.toString(((new Double(number)).intValue()));
122
    suite.addTest(new MetaCatServletNetTest("testInsertInvalidateXMLDocument",
123
                                                                  serial));    
124
    //insert non well formed document
125
    number = Math.random()*100000;
126
    serial = Integer.toString(((new Double(number)).intValue())); 
127
   suite.addTest(new MetaCatServletNetTest("testInsertNonWellFormedXMLDocument",
128
                                                            serial));
129
    //insert data file  
130
    number = Math.random()*100000;
131
    serial = Integer.toString(((new Double(number)).intValue()));
132
    suite.addTest(new MetaCatServletNetTest("testInertDataFile", serial));
133
    
134
    suite.addTest(new MetaCatServletNetTest("testLogOut"));                   
135
                              
136
    return suite;
137
  }
138
  
139

    
140

    
141
  /**
142
   * Run an initial test that always passes to check that the test
143
   * harness is working.
144
   */
145
  public void initialize()
146
  {
147
    assert(1 == 1);
148
  }
149

    
150
  /**
151
   * Test the login to neceas succesfully
152
   */
153
  public void testNCEASLogin()
154
  {
155
    String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
156
    String passwd="123456";
157
    assert(logIn(user,passwd));
158
    //assert( withProtocol.getProtocol().equals("http"));
159
  }
160
  
161
  /**
162
   * Test the login to neceas failed
163
   */
164
  public void testNCEASLoginFail()
165
  {
166
    String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
167
    String passwd="12345678";
168
    assert(!logIn(user,passwd));
169
    
170
  }
171
  
172
  /**
173
   * Test the login to lter succesfully
174
   */
175
  public void testLterReferralLogin()
176
  {
177
    String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
178
    String passwd="qVyGpVeb";
179
    assert(logIn(user,passwd));
180
    
181
  }
182
  
183
  /**
184
   * Test the login to lter failed
185
   */
186
  public void testLterReferralLoginFail()
187
  {
188
    String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
189
    String passwd="qVyGpveb";
190
    assert(!logIn(user,passwd));
191
    //assert( withProtocol.getProtocol().equals("http"));
192
  }
193
  
194
 /**
195
   * Test the login to pisco succesfully
196
   */
197
   public void testPiscoReferralLogin()
198
  {
199
    String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
200
    String passwd="purple";
201
    assert(logIn(user,passwd));
202
    //assert( withProtocol.getProtocol().equals("http"));
203
  }
204
  
205
 /**
206
   * Test the login to pisco failed
207
   */
208
  public void testPiscoReferralLoginFail()
209
  {
210
    String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
211
    String passwd="hello";
212
    assert(!logIn(user,passwd));
213
    //assert( withProtocol.getProtocol().equals("http"));
214
  }
215
  
216
 /**
217
   * Test insert a xml document successfully
218
   */
219
  public void testInsertXMLDocument()
220
  {
221
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
222
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
223
    MetaCatUtil.debugMessage("docid: "+name, 20);
224
    String content="<?xml version=\"1.0\"?>"
225
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
226
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
227
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
228
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
229
                   +"<identifier>"+name+"</identifier>"
230
                   +"<allow>"
231
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
232
                   +"<permission>all</permission>"
233
                   +"</allow>"
234
                   +"<allow>"
235
                   +"<principal>public</principal>"
236
                   +"<permission>read</permission>"
237
                   +"</allow>"
238
                   +"</acl>";
239
     MetaCatUtil.debugMessage("xml document: "+content, 55);
240
     assert(handleXMLDocument(content, name, "insert"));
241
   
242
  }
243
  
244
  /**
245
   * Test insert a invalidate xml document successfully
246
   * In the String, there is no <!Doctype ... Public/System/>
247
   */
248
  public void testInsertInvalidateXMLDocument()
249
  {
250
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
251
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
252
    MetaCatUtil.debugMessage("docid: "+name, 20);
253
    String content="<?xml version=\"1.0\"?>"
254
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
255
                   +"<identifier>"+name+"</identifier>"
256
                   +"<allow>"
257
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
258
                   +"<permission>all</permission>"
259
                   +"</allow>"
260
                   +"<allow>"
261
                   +"<principal>public</principal>"
262
                   +"<permission>read</permission>"
263
                   +"</allow>"
264
                   +"</acl>";
265
     MetaCatUtil.debugMessage("xml document: "+content, 55);
266
     assert(handleXMLDocument(content, name, "insert"));
267
  }
268
  
269
   /**
270
   * Test insert a non well-formed xml document successfully
271
   * There is no </acl> in this string
272
   */
273
  public void testInsertNonWellFormedXMLDocument()
274
  {
275
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
276
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
277
    MetaCatUtil.debugMessage("docid: "+name, 20);
278
    String content="<?xml version=\"1.0\"?>"
279
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
280
                   +"<identifier>"+name+"</identifier>"
281
                   +"<allow>"
282
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
283
                   +"<permission>all</permission>"
284
                   +"</allow>"
285
                   +"<allow>"
286
                   +"<principal>public</principal>"
287
                   +"<permission>read</permission>"
288
                   +"</allow>";
289
  
290
     MetaCatUtil.debugMessage("xml document: "+content, 55);
291
     assert(!handleXMLDocument(content, name, "insert"));
292
  } 
293

    
294
  /**
295
   * Test read a xml document  in xml format successfully
296
   */
297
  public void testReadXMLDocumentXMLFormat()
298
  {
299
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
300
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
301
    assert(handleReadAction(name, "xml"));
302
   
303
  } 
304

    
305
  /**
306
   * Test read a xml document  in html format successfully
307
   */
308
  public void testReadXMLDocumentHTMLFormat()
309
  {
310
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
311
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
312
    assert(handleReadAction(name, "html"));
313
   
314
  }
315

    
316
 
317
  /**
318
   * Test read a xml document  in zip format successfully
319
   */
320
  public void testReadXMLDocumentZipFormat()
321
  {
322
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
323
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
324
    assert(handleReadAction(name, "zip"));
325
   
326
  }
327
  
328
  /**
329
   * Test insert a xml document successfully
330
   */
331
  public void testUpdateXMLDocument()
332
  {
333
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
334
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
335
    MetaCatUtil.debugMessage("docid: "+name, 20);
336
    String content="<?xml version=\"1.0\"?>"
337
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
338
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
339
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
340
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
341
                   +"<identifier>"+name+"</identifier>"
342
                   +"<allow>"
343
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
344
                   +"<permission>all</permission>"
345
                   +"</allow>"
346
                   +"<allow>"
347
                   +"<principal>public</principal>"
348
                   +"<permission>read</permission>"
349
                   +"</allow>"
350
                   +"</acl>";
351
     MetaCatUtil.debugMessage("xml document: "+content, 55);
352
     assert(handleXMLDocument(content, name, "update"));
353
   
354
  }
355
  
356
  /**
357
   * Test insert a data file successfully
358
   */
359
  public void testInertDataFile()
360
  {
361
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
362
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
363
    MetaCatUtil.debugMessage("insert data file docid: "+name, 20);
364
    MetaCatUtil.debugMessage("insert data file ", 55);
365
    File hello = new File("test/edu/ucsb/nceas/metacatnettest/hello.txt");
366
   
367
    assert(insertDataFile(name, hello));
368
  }
369
  
370
  /**
371
   * Test delete a xml document successfully
372
   */
373
  public void testDeleteXMLDocument()
374
  {
375
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
376
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
377
    assert(handleDeleteFile(name));
378
   
379
  }
380
  
381
  
382
       
383
  /**
384
   * Test logout action
385
   */
386
  public void testLogOut()
387
  {
388
    
389
    assert(handleLogOut());
390
   
391
  }
392
  
393
 /**
394
   * Method to hanld login action
395
   *
396
   * @param usrerName, the DN name of the test method
397
   * @param passWord, the passwd of the user
398
   */
399
  
400
  public boolean logIn(String userName, String passWord)
401
  {
402
    Properties prop = new Properties();
403
    prop.put("action", "login");
404
    prop.put("qformat", "xml");
405
    prop.put("username", userName);
406
    prop.put("password", passWord);
407

    
408
    // Now contact metacat
409
    String response = getMetacatString(prop);
410
    MetaCatUtil.debugMessage("Login Message: "+response, 55);
411
    boolean connected = false;
412
    if (response.indexOf("<login>") != -1) {
413
      connected = true;
414
    } else {
415
      
416
      connected = false;
417
    }
418

    
419
    return connected;
420
  }
421
  
422
  /**
423
   * Method to hanld logout action
424
   *
425
   * @param usrerName, the DN name of the test method
426
   * @param passWord, the passwd of the user
427
   */
428
  
429
  public boolean handleLogOut()
430
  {
431
    boolean disConnected =false;
432
    Properties prop = new Properties();
433
    prop.put("action", "logout");
434
    prop.put("qformat", "xml");
435
  
436
    String response = getMetacatString(prop);
437
    MetaCatUtil.debugMessage("Logout Message: "+response, 55);
438
    edu.ucsb.nceas.morpho.framework.HttpMessage.setCookie(null);
439
     
440
    if (response.indexOf("<logout>") != -1) 
441
    {
442
      disConnected = true;
443
    } 
444
    else 
445
    {
446
      disConnected = false;
447
    }
448

    
449
    return disConnected;
450
  }
451
  
452
  /**
453
   * Method to hanld read both xml and data file
454
   *
455
   * @param docid, the docid of the document want to read
456
   * @param qformat, the format of document user want to get
457
   */
458
  public boolean handleReadAction(String docid, String qformat)
459
  {
460
    Properties prop = new Properties();
461
    String message ="";
462
    prop.put("action", "read");
463
    prop.put("qformat", qformat);
464
    prop.put("docid", docid);
465
    
466
    message = getMetacatString(prop);
467
    message = message.trim();
468
    //MetaCatUtil.debugMessage("Read Message: "+message, 30);
469
    if (message==null || message.equals("")||message.indexOf("<error>") != -1 )
470
    {//there was an error
471
      
472
      return false;
473
    }
474
    else
475
    {//successfully
476
      return true;
477
    } 
478
    
479
  }
480
  
481
  
482
  /**
483
   * Method to hanld inset or update xml document
484
   *
485
   * @param xmlDocument, the content of xml qformat
486
   * @param docid, the docid of the document
487
   * @param action, insert or update
488
   */
489
  public boolean handleXMLDocument(String xmlDocument, String docid, 
490
                                                              String action)
491
 
492
  { //-attempt to write file to metacat
493
    String access = "no";
494
    StringBuffer fileText = new StringBuffer();
495
    StringBuffer messageBuf = new StringBuffer();
496
    String accessFileId = null;
497
    Properties prop = new Properties();
498
    prop.put("action", action);
499
    prop.put("public", access);  //This is the old way of controlling access
500
    prop.put("doctext", xmlDocument);
501
    prop.put("docid", docid);
502
    
503
    String message = getMetacatString(prop);
504
    MetaCatUtil.debugMessage("Insert or Update Message: "+message, 55);
505
    if(message.indexOf("<error>") != -1)
506
    {//there was an error
507
      
508
      return false;
509
    }
510
    else if(message.indexOf("<success>") != -1)
511
    {//the operation worked
512
     //write the file to the cache and return the file object
513
     return true;
514
       
515
    }
516
    else
517
    {//something weird happened.
518
      return false;
519
    } 
520
   
521
   
522
  }
523
  
524
   public boolean handleDeleteFile(String name)
525
  {
526
    
527
    Properties prop = new Properties();
528
    prop.put("action", "delete");
529
    prop.put("docid", name);
530
 
531
    String message = getMetacatString(prop);
532
    MetaCatUtil.debugMessage("Delete Message: "+message, 55);
533
    if(message.indexOf("<error>") != -1)
534
    {//there was an error
535
      
536
      return false;
537
    }
538
    else if(message.indexOf("<success>") != -1)
539
    {//the operation worked
540
     //write the file to the cache and return the file object
541
     return true;
542
       
543
    }
544
    else
545
    {//something weird happened.
546
      return false;
547
    } 
548
  }
549
  
550
  
551
  /**
552
   * sends a data file to the metacat using "multipart/form-data" encoding
553
   *
554
   * @param id the id to assign to the file on metacat (e.g., knb.1.1)
555
   * @param file the file to send
556
   */
557
  public boolean insertDataFile(String id, File file)
558
  {
559
    String response = null;
560
    //Get response for calling sendDataFile function
561
    response = sendDataFile(id, file);
562
    
563
    if (response.indexOf("success") != -1)
564
    {
565
      //insert successfully
566
      return true;
567
    }
568
    else
569
    {
570
      return false;
571
    }
572
  }
573
  
574
  /**
575
   * sends a data file to the metacat using "multipart/form-data" encoding
576
   *
577
   * @param id the id to assign to the file on metacat (e.g., knb.1.1)
578
   * @param file the file to send
579
   */
580
  public String sendDataFile(String id, File file) 
581
  {
582
    String response = "";
583
    InputStream returnStream = null;
584

    
585
    // Now contact metacat and send the request
586
    try
587
    {
588
      //FileInputStream data = new FileInputStream(file);
589
      System.setProperty("java.protocol.handler.pkgs","HTTPClient");
590
      URL url = new URL(metacatURL);
591
      edu.ucsb.nceas.morpho.framework.HttpMessage msg = 
592
                        new edu.ucsb.nceas.morpho.framework.HttpMessage(url);
593
      Properties args = new Properties();
594
      args.put("action", "upload");
595
      args.put("docid", id);
596

    
597
      Properties dataStreams = new Properties();
598
      String filename = file.getAbsolutePath();
599
   
600
      dataStreams.put("datafile", filename);
601
       
602
  
603
    
604
      returnStream = msg.sendPostData(args, dataStreams);
605
        
606
      InputStreamReader returnStreamReader = 
607
                        new InputStreamReader(returnStream);
608
      StringWriter sw = new StringWriter();
609
      int len;
610
      char[] characters = new char[512];
611
      while ((len = returnStreamReader.read(characters, 0, 512)) != -1)
612
      {
613
        sw.write(characters, 0, len);
614
      }
615
      returnStreamReader.close();
616
      response = sw.toString();
617
      sw.close();
618
   
619
    } 
620
    catch(Exception e) 
621
    {
622
      e.printStackTrace(System.err);
623
    }
624
    return response;
625
  }
626
  
627
  public String getMetacatString(Properties prop)
628
  {
629
    String response = null;
630

    
631
    // Now contact metacat and send the request
632
    try
633
    {
634
      InputStreamReader returnStream = 
635
                        new InputStreamReader(getMetacatInputStream(prop));
636
      StringWriter sw = new StringWriter();
637
      int len;
638
      char[] characters = new char[512];
639
      while ((len = returnStream.read(characters, 0, 512)) != -1)
640
      {
641
        sw.write(characters, 0, len);
642
      }
643
      returnStream.close();
644
      response = sw.toString();
645
      sw.close();
646
    }
647
    catch(Exception e)
648
    {
649
      return null;
650
    }
651
  
652
    return response;
653
  }
654
  
655
   /**
656
   * Send a request to Metacat
657
   *
658
   * @param prop the properties to be sent to Metacat
659
   * @return InputStream as returned by Metacat
660
   */
661
  public InputStream getMetacatInputStream(Properties prop)
662
  {
663
    InputStream returnStream = null;
664
    // Now contact metacat and send the request
665
    try
666
    {
667
     
668
      URL url = new URL(metacatURL);
669
      edu.ucsb.nceas.morpho.framework.HttpMessage msg = 
670
              new edu.ucsb.nceas.morpho.framework.HttpMessage(url);
671
      returnStream = msg.sendPostMessage(prop);
672
      return returnStream;
673
    }
674
    catch(Exception e)
675
    {
676
      e.printStackTrace(System.err);
677
     
678
    }
679
    return returnStream;
680
   
681
  }
682

    
683
 
684

    
685
}
(1-1/2)