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
 *
8
 *   '$Author: jones $'
9
 *     '$Date: 2006-11-10 11:30:36 -0800 (Fri, 10 Nov 2006) $'
10
 * '$Revision: 3080 $'
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
package edu.ucsb.nceas.metacatnettest;
28

    
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

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

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

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

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

    
139

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

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

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

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

    
315
 
316
  /**
317
   * Test read a xml document  in zip format successfully
318
   */
319
  public void testReadXMLDocumentZipFormat()
320
  {
321
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
322
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
323
    assertTrue(handleReadAction(name, "zip"));
324
   
325
  }
326
  
327
  /**
328
   * Test insert a xml document successfully
329
   */
330
  public void testUpdateXMLDocument()
331
  {
332
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
333
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
334
    MetaCatUtil.debugMessage("update docid: "+name, 20);
335
    String content="<?xml version=\"1.0\"?>"
336
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
337
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
338
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
339
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
340
                   +"<identifier>"+name+"</identifier>"
341
                   +"<allow>"
342
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
343
                   +"<permission>all</permission>"
344
                   +"</allow>"
345
                   +"<allow>"
346
                   +"<principal>public</principal>"
347
                   +"<permission>read</permission>"
348
                   +"</allow>"
349
                   +"</acl>";
350
     MetaCatUtil.debugMessage("xml document: "+content, 55);
351
     assertTrue(handleXMLDocument(content, name, "update"));
352
   
353
  }
354
  
355
  /**
356
   * Test insert a data file successfully
357
   */
358
  public void testInertDataFile()
359
  {
360
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
361
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
362
    MetaCatUtil.debugMessage("insert data file docid: "+name, 20);
363
    MetaCatUtil.debugMessage("insert data file ", 55);
364
    File hello = new File("test/edu/ucsb/nceas/metacatnettest/hello.txt");
365
   
366
    assertTrue(insertDataFile(name, hello));
367
  }
368
  
369
  /**
370
   * Test delete a xml document successfully
371
   */
372
  public void testDeleteXMLDocument()
373
  {
374
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
375
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
376
    MetaCatUtil.debugMessage("delete docid: "+name, 20);
377
    assertTrue(handleDeleteFile(name));
378
   
379
  }
380
  
381
  
382
       
383
  /**
384
   * Test logout action
385
   */
386
  public void testLogOut()
387
  {
388
    
389
    assertTrue(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)