Project

General

Profile

« Previous | Next » 

Revision 1112

Added by Jing Tao over 22 years ago

Add metacat test to cvs.

View differences:

test/edu/ucsb/nceas/metacattest/MetaCatServletTest.java
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$'
10
 *     '$Date$'
11
 * '$Revision$'
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.metacatjunittests;
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()*10000;
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()*10000;
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()*10000;
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("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("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("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("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
    assert(handleDeleteFile(name));
359
   
360
  }
361
       
362
  /**
363
   * Test logout action
364
   */
365
  public void testLogOut()
366
  {
367
    
368
    assert(handleLogOut());
369
   
370
  }
371
  
372
 /**
373
   * Method to hanld login action
374
   *
375
   * @param usrerName, the DN name of the test method
376
   * @param passWord, the passwd of the user
377
   */
378
  
379
  public boolean logIn(String userName, String passWord)
380
  {
381
    Properties prop = new Properties();
382
    prop.put("action", "login");
383
    prop.put("qformat", "xml");
384
    prop.put("username", userName);
385
    prop.put("password", passWord);
386

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

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

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

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

  
587
 
588

  
589
}
0 590

  

Also available in: Unified diff