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: jones $'
10
 *     '$Date: 2006-02-24 13:18:07 -0800 (Fri, 24 Feb 2006) $'
11
 * '$Revision: 2917 $'
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.metacattest;
29

    
30
import edu.ucsb.nceas.metacat.*;
31
import edu.ucsb.nceas.utilities.HttpMessage;
32
//import edu.ucsb.nceas.morpho.framework.*;
33
import junit.framework.Test;
34
import junit.framework.TestCase;
35
import junit.framework.TestResult;
36
import junit.framework.TestSuite;
37
import org.apache.commons.logging.Log;
38
import org.apache.commons.logging.LogFactory;
39

    
40
import java.io.*;
41
import java.net.*;
42
import java.util.*;
43

    
44

    
45
/**
46
 * A JUnit test for testing Step class processing
47
 */
48
public class MetaCatServletTest extends TestCase
49
{
50
  private String metacatURL=MetaCatUtil.getOption("junittesturl");
51
  private String serialNumber;
52
  private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.MetaCatServletTest");
53
  
54
  /**
55
   * Constructor to build the test
56
   *
57
   * @param name the name of the test method
58
   */
59
  public MetaCatServletTest(String name)
60
  {
61
    super(name);
62
  }
63

    
64
 /**
65
   * Constructor to build the test
66
   *
67
   * @param name the name of the test method
68
   */
69
  public MetaCatServletTest(String name, String serial)
70
  {
71
    super(name);
72
    serialNumber=serial;
73
  }
74
  /**
75
   * Establish a testing framework by initializing appropriate objects
76
   */
77
  public void setUp()
78
 {
79
    
80
 }
81

    
82
  /**
83
   * Release any objects after tests are complete
84
   */
85
  public void tearDown()
86
  {
87
  }
88

    
89
  /**
90
   * Create a suite of tests to be run together
91
   */
92
  public static Test suite()
93
  {
94
    double number = 0;
95
    String serial = null;
96
    
97
    
98
    TestSuite suite = new TestSuite();
99
    suite.addTest(new MetaCatServletTest("initialize"));
100
    suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
101
    suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
102
    suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
103
    suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
104
    suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
105
    //Should put a login successfully at the end of login test
106
    //So insert or update can have cookie.
107
    suite.addTest(new MetaCatServletTest("testNCEASLogin"));
108
    
109
    //create random number for docid, so it can void repeat
110
    number = Math.random()*100000;
111
    serial = Integer.toString(((new Double(number)).intValue()));
112
    log.debug("serial: "+serial);
113
    suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
114
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", 
115
                                                        serial));
116
    suite.addTest(new MetaCatServletTest("testUpdateXMLDocument",serial));
117
        
118
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", 
119
                                                        serial));
120
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", 
121
                                                        serial));
122
    
123
    suite.addTest(new MetaCatServletTest("testDeleteXMLDocument",serial));
124
    
125
    //insert invalid xml document
126
    number = Math.random()*100000;
127
    serial = Integer.toString(((new Double(number)).intValue()));
128
    suite.addTest(new MetaCatServletTest("testInsertInvalidateXMLDocument",
129
                                                                  serial));    
130
    //insert non well formed document
131
    number = Math.random()*100000;
132
    serial = Integer.toString(((new Double(number)).intValue())); 
133
    suite.addTest(new MetaCatServletTest("testInsertNonWellFormedXMLDocument",
134
                                                            serial));
135
    
136
    suite.addTest(new MetaCatServletTest("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
    assertTrue(1 == 1);
150
  }
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
    assertTrue(logIn(user,passwd));
160
    //assertTrue( withProtocol.getProtocol().equals("http"));
161
  }
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
    assertTrue(!logIn(user,passwd));
171
    
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
    assertTrue(logIn(user,passwd));
182
    
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
    assertTrue(!logIn(user,passwd));
193
    //assertTrue( withProtocol.getProtocol().equals("http"));
194
  }
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
    assertTrue(logIn(user,passwd));
204
    //assertTrue( withProtocol.getProtocol().equals("http"));
205
  }
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
    assertTrue(!logIn(user,passwd));
215
    //assertTrue( withProtocol.getProtocol().equals("http"));
216
  }
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
    log.debug("insert docid: "+name);
226
    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
    log.debug("xml document: "+content);
242
     assertTrue(handleXMLDocument(content, name, "insert"));
243
   
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
    log.debug("insert docid: "+name);
255
    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
    log.debug("xml document: "+content);
268
     assertTrue(handleXMLDocument(content, name, "insert"));
269
  }
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
    log.debug("insert non well-formed docid: "+name);
280
    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
    log.debug("xml document: "+content);
293
     assertTrue(!handleXMLDocument(content, name, "insert"));
294
  } 
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
    assertTrue(handleReadAction(name, "xml"));
304
   
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
    assertTrue(handleReadAction(name, "html"));
315
   
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
    assertTrue(handleReadAction(name, "zip"));
327
   
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
    log.debug("update docid: "+name);
338
    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
    log.debug("xml document: "+content);
354
     assertTrue(handleXMLDocument(content, name, "update"));
355
   
356
  }
357
  
358
  /**
359
   * Test delete a xml document successfully
360
   */
361
  public void testDeleteXMLDocument()
362
  {
363
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
364
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
365
    log.debug("delete docid: "+name);
366
    assertTrue(handleDeleteFile(name));
367
   
368
  }
369
       
370
  /**
371
   * Test logout action
372
   */
373
  public void testLogOut()
374
  {
375
    
376
    assertTrue(handleLogOut());
377
   
378
  }
379
  
380
 /**
381
   * Method to hanld login action
382
   *
383
   * @param usrerName, the DN name of the test method
384
   * @param passWord, the passwd of the user
385
   */
386
  
387
  public boolean logIn(String userName, String passWord)
388
  {
389
    Properties prop = new Properties();
390
    prop.put("action", "login");
391
    prop.put("qformat", "xml");
392
    prop.put("username", userName);
393
    prop.put("password", passWord);
394

    
395
    // Now contact metacat
396
    String response = getMetacatString(prop);
397
    log.debug("Login Message: "+response);
398
    boolean connected = false;
399
    if (response.indexOf("<login>") != -1) {
400
      connected = true;
401
    } else {
402
      
403
      connected = false;
404
    }
405

    
406
    return connected;
407
  }
408
  
409
  /**
410
   * Method to hanld logout action
411
   *
412
   * @param usrerName, the DN name of the test method
413
   * @param passWord, the passwd of the user
414
   */
415
  
416
  public boolean handleLogOut()
417
  {
418
    boolean disConnected =false;
419
    Properties prop = new Properties();
420
    prop.put("action", "logout");
421
    prop.put("qformat", "xml");
422
  
423
    String response = getMetacatString(prop);
424
    log.debug("Logout Message: "+response);
425
    HttpMessage.setCookie(null);
426
     
427
    if (response.indexOf("<logout>") != -1) 
428
    {
429
      disConnected = true;
430
    } 
431
    else 
432
    {
433
      disConnected = false;
434
    }
435

    
436
    return disConnected;
437
  }
438
  
439
  /**
440
   * Method to hanld read both xml and data file
441
   *
442
   * @param docid, the docid of the document want to read
443
   * @param qformat, the format of document user want to get
444
   */
445
  public boolean handleReadAction(String docid, String qformat)
446
  {
447
    Properties prop = new Properties();
448
    String message ="";
449
    prop.put("action", "read");
450
    prop.put("qformat", qformat);
451
    prop.put("docid", docid);
452
    
453
    message = getMetacatString(prop);
454
    message = message.trim();
455
    if (message==null || message.equals("")||message.indexOf("<error>") != -1 )
456
    {//there was an error
457
      
458
      return false;
459
    }
460
    else
461
    {//successfully
462
      return true;
463
    } 
464
    
465
  }
466
  
467
  
468
  /**
469
   * Method to hanld inset or update xml document
470
   *
471
   * @param xmlDocument, the content of xml qformat
472
   * @param docid, the docid of the document
473
   * @param action, insert or update
474
   */
475
  public boolean handleXMLDocument(String xmlDocument, String docid, 
476
                                                              String action)
477
 
478
  { //-attempt to write file to metacat
479
    String access = "no";
480
    StringBuffer fileText = new StringBuffer();
481
    StringBuffer messageBuf = new StringBuffer();
482
    String accessFileId = null;
483
    Properties prop = new Properties();
484
    prop.put("action", action);
485
    prop.put("public", access);  //This is the old way of controlling access
486
    prop.put("doctext", xmlDocument);
487
    prop.put("docid", docid);
488
    
489
    String message = getMetacatString(prop);
490
    log.debug("Insert or Update Message: "+message);
491
    if(message.indexOf("<error>") != -1)
492
    {//there was an error
493
      
494
      return false;
495
    }
496
    else if(message.indexOf("<success>") != -1)
497
    {//the operation worked
498
     //write the file to the cache and return the file object
499
     return true;
500
       
501
    }
502
    else
503
    {//something weird happened.
504
      return false;
505
    } 
506
   
507
   
508
  }
509
  
510
   public boolean handleDeleteFile(String name)
511
  {
512
    
513
    Properties prop = new Properties();
514
    prop.put("action", "delete");
515
    prop.put("docid", name);
516
 
517
    String message = getMetacatString(prop);
518
    log.debug("Delete Message: "+message);
519
    if(message.indexOf("<error>") != -1)
520
    {//there was an error
521
      
522
      return false;
523
    }
524
    else if(message.indexOf("<success>") != -1)
525
    {//the operation worked
526
     //write the file to the cache and return the file object
527
     return true;
528
       
529
    }
530
    else
531
    {//something weird happened.
532
      return false;
533
    } 
534
  }
535
  
536
  
537
 
538
  
539
   public String getMetacatString(Properties prop)
540
  {
541
    String response = null;
542

    
543
    // Now contact metacat and send the request
544
    try
545
    {
546
      InputStreamReader returnStream = 
547
                        new InputStreamReader(getMetacatInputStream(prop));
548
      StringWriter sw = new StringWriter();
549
      int len;
550
      char[] characters = new char[512];
551
      while ((len = returnStream.read(characters, 0, 512)) != -1)
552
      {
553
        sw.write(characters, 0, len);
554
      }
555
      returnStream.close();
556
      response = sw.toString();
557
      sw.close();
558
    }
559
    catch(Exception e)
560
    {
561
      return null;
562
    }
563
  
564
    return response;
565
  }
566
  
567
   /**
568
   * Send a request to Metacat
569
   *
570
   * @param prop the properties to be sent to Metacat
571
   * @return InputStream as returned by Metacat
572
   */
573
  public InputStream getMetacatInputStream(Properties prop)
574
  {
575
    InputStream returnStream = null;
576
    // Now contact metacat and send the request
577
    try
578
    {
579
     
580
      URL url = new URL(metacatURL);
581
      HttpMessage msg = new HttpMessage(url);
582
      returnStream = msg.sendPostMessage(prop);
583
      return returnStream;
584
    }
585
    catch(Exception e)
586
    {
587
      e.printStackTrace(System.err);
588
     
589
    }
590
    return returnStream;
591
   
592
  }
593

    
594
 
595

    
596
}
(5-5/12)