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-23 08:46:37 -0700 (Thu, 23 May 2002) $'
11
 * '$Revision: 1120 $'
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.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 MetaCatServletTest 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 MetaCatServletTest(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 MetaCatServletTest(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 MetaCatServletTest("initialize"));
95
    suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
96
    suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
97
    suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
98
    suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
99
    suite.addTest(new MetaCatServletTest("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 MetaCatServletTest("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 MetaCatServletTest("testInsertXMLDocument", serial));
109
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", 
110
                                                        serial));
111
        
112
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", 
113
                                                        serial));
114
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", 
115
                                                        serial));
116
    suite.addTest(new MetaCatServletTest("testUpdateXMLDocument",serial));
117
    suite.addTest(new MetaCatServletTest("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 MetaCatServletTest("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 MetaCatServletTest("testInsertNonWellFormedXMLDocument",
128
                                                            serial));                                               
129
    suite.addTest(new MetaCatServletTest("testLogOut"));                   
130
                              
131
    return suite;
132
  }
133
  
134

    
135

    
136
  /**
137
   * Run an initial test that always passes to check that the test
138
   * harness is working.
139
   */
140
  public void initialize()
141
  {
142
    assert(1 == 1);
143
  }
144

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

    
289
  /**
290
   * Test read a xml document  in xml format successfully
291
   */
292
  public void testReadXMLDocumentXMLFormat()
293
  {
294
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
295
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
296
    assert(handleReadAction(name, "xml"));
297
   
298
  } 
299

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

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

    
388
    // Now contact metacat
389
    String response = getMetacatString(prop);
390
    MetaCatUtil.debugMessage("Login Message: "+response, 55);
391
    boolean connected = false;
392
    if (response.indexOf("<login>") != -1) {
393
      connected = true;
394
    } else {
395
      
396
      connected = false;
397
    }
398

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

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

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

    
588
 
589

    
590
}
(1-1/2)