Project

General

Profile

1 1112 tao
/**
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 3080 jones
 *    Authors: Jing Tao
7 1112 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 1118 tao
package edu.ucsb.nceas.metacattest;
28 1112 tao
29
import edu.ucsb.nceas.metacat.*;
30 1822 jones
import edu.ucsb.nceas.utilities.HttpMessage;
31 3567 tao
import edu.ucsb.nceas.utilities.Options;
32 1112 tao
//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 2917 jones
import org.apache.commons.logging.Log;
38
import org.apache.commons.logging.LogFactory;
39 1112 tao
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 3875 tao
	/* Initialize Options*/
51
	  static
52
	  {
53
		  try
54
		  {
55
			  Options.initialize(new File("build/tests/metacat.properties"));
56
		  }
57
		  catch(Exception e)
58
		  {
59
			  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
60
		  }
61
	  }
62 1112 tao
  private String metacatURL=MetaCatUtil.getOption("junittesturl");
63
  private String serialNumber;
64 2917 jones
  private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.MetaCatServletTest");
65
66 3875 tao
67 1112 tao
  /**
68
   * Constructor to build the test
69
   *
70
   * @param name the name of the test method
71
   */
72
  public MetaCatServletTest(String name)
73
  {
74
    super(name);
75
  }
76
77
 /**
78
   * Constructor to build the test
79
   *
80
   * @param name the name of the test method
81
   */
82
  public MetaCatServletTest(String name, String serial)
83
  {
84
    super(name);
85
    serialNumber=serial;
86
  }
87
  /**
88
   * Establish a testing framework by initializing appropriate objects
89
   */
90
  public void setUp()
91
 {
92
93
 }
94
95
  /**
96
   * Release any objects after tests are complete
97
   */
98
  public void tearDown()
99
  {
100
  }
101
102
  /**
103
   * Create a suite of tests to be run together
104
   */
105
  public static Test suite()
106
  {
107
    double number = 0;
108
    String serial = null;
109
110
111
    TestSuite suite = new TestSuite();
112 1118 tao
    suite.addTest(new MetaCatServletTest("initialize"));
113 1112 tao
    suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
114
    suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
115
    suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
116
    suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
117 1118 tao
    suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
118 1112 tao
    //Should put a login successfully at the end of login test
119
    //So insert or update can have cookie.
120
    suite.addTest(new MetaCatServletTest("testNCEASLogin"));
121
122
    //create random number for docid, so it can void repeat
123 1118 tao
    number = Math.random()*100000;
124 1112 tao
    serial = Integer.toString(((new Double(number)).intValue()));
125 2917 jones
    log.debug("serial: "+serial);
126 1112 tao
    suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
127
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat",
128
                                                        serial));
129 1222 tao
    suite.addTest(new MetaCatServletTest("testUpdateXMLDocument",serial));
130 1112 tao
131
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat",
132
                                                        serial));
133
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat",
134
                                                        serial));
135 1222 tao
136 1112 tao
    suite.addTest(new MetaCatServletTest("testDeleteXMLDocument",serial));
137
138
    //insert invalid xml document
139 1118 tao
    number = Math.random()*100000;
140 1112 tao
    serial = Integer.toString(((new Double(number)).intValue()));
141
    suite.addTest(new MetaCatServletTest("testInsertInvalidateXMLDocument",
142
                                                                  serial));
143
    //insert non well formed document
144 1118 tao
    number = Math.random()*100000;
145 1112 tao
    serial = Integer.toString(((new Double(number)).intValue()));
146
    suite.addTest(new MetaCatServletTest("testInsertNonWellFormedXMLDocument",
147 1222 tao
                                                            serial));
148
149 1112 tao
    suite.addTest(new MetaCatServletTest("testLogOut"));
150
151
    return suite;
152
  }
153
154
155
156
  /**
157
   * Run an initial test that always passes to check that the test
158
   * harness is working.
159
   */
160
  public void initialize()
161
  {
162 1505 tao
    assertTrue(1 == 1);
163 1112 tao
  }
164
165
  /**
166
   * Test the login to neceas succesfully
167
   */
168
  public void testNCEASLogin()
169
  {
170
    String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
171
    String passwd="123456";
172 1505 tao
    assertTrue(logIn(user,passwd));
173
    //assertTrue( withProtocol.getProtocol().equals("http"));
174 1112 tao
  }
175
176
  /**
177
   * Test the login to neceas failed
178
   */
179
  public void testNCEASLoginFail()
180
  {
181
    String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
182
    String passwd="12345678";
183 1505 tao
    assertTrue(!logIn(user,passwd));
184 1112 tao
185
  }
186
187
  /**
188
   * Test the login to lter succesfully
189
   */
190
  public void testLterReferralLogin()
191
  {
192 3780 daigle
    String user=MetaCatUtil.getOption("lteruser");
193
    String passwd=MetaCatUtil.getOption("lterpassword");
194 1505 tao
    assertTrue(logIn(user,passwd));
195 1112 tao
196
  }
197
198
  /**
199
   * Test the login to lter failed
200
   */
201
  public void testLterReferralLoginFail()
202
  {
203
    String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
204
    String passwd="qVyGpveb";
205 1505 tao
    assertTrue(!logIn(user,passwd));
206
    //assertTrue( withProtocol.getProtocol().equals("http"));
207 1112 tao
  }
208
209
 /**
210
   * Test the login to pisco succesfully
211
   */
212
   public void testPiscoReferralLogin()
213
  {
214 3780 daigle
	String user=MetaCatUtil.getOption("piscouser");
215
	String passwd=MetaCatUtil.getOption("piscopassword");
216 1505 tao
    assertTrue(logIn(user,passwd));
217
    //assertTrue( withProtocol.getProtocol().equals("http"));
218 1112 tao
  }
219
220
 /**
221
   * Test the login to pisco failed
222
   */
223
  public void testPiscoReferralLoginFail()
224
  {
225
    String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
226
    String passwd="hello";
227 1505 tao
    assertTrue(!logIn(user,passwd));
228
    //assertTrue( withProtocol.getProtocol().equals("http"));
229 1112 tao
  }
230
231
 /**
232
   * Test insert a xml document successfully
233
   */
234
  public void testInsertXMLDocument()
235
  {
236
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
237
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
238 2917 jones
    log.debug("insert docid: "+name);
239 1112 tao
    String content="<?xml version=\"1.0\"?>"
240
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
241
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
242
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
243
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
244
                   +"<identifier>"+name+"</identifier>"
245
                   +"<allow>"
246
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
247
                   +"<permission>all</permission>"
248
                   +"</allow>"
249
                   +"<allow>"
250
                   +"<principal>public</principal>"
251
                   +"<permission>read</permission>"
252
                   +"</allow>"
253
                   +"</acl>";
254 2917 jones
    log.debug("xml document: "+content);
255 1505 tao
     assertTrue(handleXMLDocument(content, name, "insert"));
256 1112 tao
257
  }
258
259
  /**
260
   * Test insert a invalidate xml document successfully
261
   * In the String, there is no <!Doctype ... Public/System/>
262
   */
263
  public void testInsertInvalidateXMLDocument()
264
  {
265
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
266
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
267 2917 jones
    log.debug("insert docid: "+name);
268 1112 tao
    String content="<?xml version=\"1.0\"?>"
269
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
270
                   +"<identifier>"+name+"</identifier>"
271
                   +"<allow>"
272
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
273
                   +"<permission>all</permission>"
274
                   +"</allow>"
275
                   +"<allow>"
276
                   +"<principal>public</principal>"
277
                   +"<permission>read</permission>"
278
                   +"</allow>"
279
                   +"</acl>";
280 2917 jones
    log.debug("xml document: "+content);
281 1505 tao
     assertTrue(handleXMLDocument(content, name, "insert"));
282 1112 tao
  }
283
284
   /**
285
   * Test insert a non well-formed xml document successfully
286
   * There is no </acl> in this string
287
   */
288
  public void testInsertNonWellFormedXMLDocument()
289
  {
290
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
291
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
292 2917 jones
    log.debug("insert non well-formed docid: "+name);
293 1112 tao
    String content="<?xml version=\"1.0\"?>"
294
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
295
                   +"<identifier>"+name+"</identifier>"
296
                   +"<allow>"
297
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
298
                   +"<permission>all</permission>"
299
                   +"</allow>"
300
                   +"<allow>"
301
                   +"<principal>public</principal>"
302
                   +"<permission>read</permission>"
303
                   +"</allow>";
304
305 2917 jones
    log.debug("xml document: "+content);
306 1505 tao
     assertTrue(!handleXMLDocument(content, name, "insert"));
307 1112 tao
  }
308
309
  /**
310
   * Test read a xml document  in xml format successfully
311
   */
312
  public void testReadXMLDocumentXMLFormat()
313
  {
314
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
315
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
316 1505 tao
    assertTrue(handleReadAction(name, "xml"));
317 1112 tao
318
  }
319
320
  /**
321
   * Test read a xml document  in html format successfully
322
   */
323
  public void testReadXMLDocumentHTMLFormat()
324
  {
325
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
326
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
327 1505 tao
    assertTrue(handleReadAction(name, "html"));
328 1112 tao
329
  }
330
331
332
  /**
333
   * Test read a xml document  in zip format successfully
334
   */
335
  public void testReadXMLDocumentZipFormat()
336
  {
337
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
338
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
339 1505 tao
    assertTrue(handleReadAction(name, "zip"));
340 1112 tao
341
  }
342
343
  /**
344
   * Test insert a xml document successfully
345
   */
346
  public void testUpdateXMLDocument()
347
  {
348
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
349
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
350 2917 jones
    log.debug("update docid: "+name);
351 1112 tao
    String content="<?xml version=\"1.0\"?>"
352
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
353
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
354
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
355
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
356
                   +"<identifier>"+name+"</identifier>"
357
                   +"<allow>"
358
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
359
                   +"<permission>all</permission>"
360
                   +"</allow>"
361
                   +"<allow>"
362
                   +"<principal>public</principal>"
363
                   +"<permission>read</permission>"
364
                   +"</allow>"
365
                   +"</acl>";
366 2917 jones
    log.debug("xml document: "+content);
367 1505 tao
     assertTrue(handleXMLDocument(content, name, "update"));
368 1112 tao
369
  }
370
371
  /**
372
   * Test delete a xml document successfully
373
   */
374
  public void testDeleteXMLDocument()
375
  {
376
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
377
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
378 2917 jones
    log.debug("delete docid: "+name);
379 1505 tao
    assertTrue(handleDeleteFile(name));
380 1112 tao
381
  }
382
383
  /**
384
   * Test logout action
385
   */
386
  public void testLogOut()
387
  {
388
389 1505 tao
    assertTrue(handleLogOut());
390 1112 tao
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 2917 jones
    log.debug("Login Message: "+response);
411 1112 tao
    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 2917 jones
    log.debug("Logout Message: "+response);
438 1112 tao
    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
    if (message==null || message.equals("")||message.indexOf("<error>") != -1 )
469
    {//there was an error
470
471
      return false;
472
    }
473
    else
474
    {//successfully
475
      return true;
476
    }
477
478
  }
479
480
481
  /**
482
   * Method to hanld inset or update xml document
483
   *
484
   * @param xmlDocument, the content of xml qformat
485
   * @param docid, the docid of the document
486
   * @param action, insert or update
487
   */
488
  public boolean handleXMLDocument(String xmlDocument, String docid,
489
                                                              String action)
490
491
  { //-attempt to write file to metacat
492
    String access = "no";
493
    StringBuffer fileText = new StringBuffer();
494
    StringBuffer messageBuf = new StringBuffer();
495
    String accessFileId = null;
496
    Properties prop = new Properties();
497
    prop.put("action", action);
498
    prop.put("public", access);  //This is the old way of controlling access
499
    prop.put("doctext", xmlDocument);
500
    prop.put("docid", docid);
501
502
    String message = getMetacatString(prop);
503 2917 jones
    log.debug("Insert or Update Message: "+message);
504 1112 tao
    if(message.indexOf("<error>") != -1)
505
    {//there was an error
506
507
      return false;
508
    }
509
    else if(message.indexOf("<success>") != -1)
510
    {//the operation worked
511
     //write the file to the cache and return the file object
512
     return true;
513
514
    }
515
    else
516
    {//something weird happened.
517
      return false;
518
    }
519
520
521
  }
522
523
   public boolean handleDeleteFile(String name)
524
  {
525
526
    Properties prop = new Properties();
527
    prop.put("action", "delete");
528
    prop.put("docid", name);
529
530
    String message = getMetacatString(prop);
531 2917 jones
    log.debug("Delete Message: "+message);
532 1112 tao
    if(message.indexOf("<error>") != -1)
533
    {//there was an error
534
535
      return false;
536
    }
537
    else if(message.indexOf("<success>") != -1)
538
    {//the operation worked
539
     //write the file to the cache and return the file object
540
     return true;
541
542
    }
543
    else
544
    {//something weird happened.
545
      return false;
546
    }
547
  }
548
549
550
551
552
   public String getMetacatString(Properties prop)
553
  {
554
    String response = null;
555
556
    // Now contact metacat and send the request
557
    try
558
    {
559
      InputStreamReader returnStream =
560
                        new InputStreamReader(getMetacatInputStream(prop));
561
      StringWriter sw = new StringWriter();
562
      int len;
563
      char[] characters = new char[512];
564
      while ((len = returnStream.read(characters, 0, 512)) != -1)
565
      {
566
        sw.write(characters, 0, len);
567
      }
568
      returnStream.close();
569
      response = sw.toString();
570
      sw.close();
571
    }
572
    catch(Exception e)
573
    {
574
      return null;
575
    }
576
577
    return response;
578
  }
579
580
   /**
581
   * Send a request to Metacat
582
   *
583
   * @param prop the properties to be sent to Metacat
584
   * @return InputStream as returned by Metacat
585
   */
586
  public InputStream getMetacatInputStream(Properties prop)
587
  {
588
    InputStream returnStream = null;
589
    // Now contact metacat and send the request
590
    try
591
    {
592
593
      URL url = new URL(metacatURL);
594
      HttpMessage msg = new HttpMessage(url);
595
      returnStream = msg.sendPostMessage(prop);
596
      return returnStream;
597
    }
598
    catch(Exception e)
599
    {
600
      e.printStackTrace(System.err);
601
602
    }
603
    return returnStream;
604
605
  }
606
607
608
609
}