Project

General

Profile

« Previous | Next » 

Revision 4080

Added by daigle about 16 years ago

Merge 1.9 changes into Head

View differences:

MetaCatServletTest.java
27 27
package edu.ucsb.nceas.metacattest;
28 28

  
29 29
import edu.ucsb.nceas.metacat.*;
30
import edu.ucsb.nceas.metacat.service.PropertyService;
31
import edu.ucsb.nceas.metacat.util.SystemUtil;
30 32
import edu.ucsb.nceas.utilities.HttpMessage;
31
import edu.ucsb.nceas.utilities.Options;
32
//import edu.ucsb.nceas.morpho.framework.*;
33
import edu.ucsb.nceas.utilities.Options; //import edu.ucsb.nceas.morpho.framework.*;
34
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
33 35
import junit.framework.Test;
34 36
import junit.framework.TestCase;
35 37
import junit.framework.TestResult;
......
41 43
import java.net.*;
42 44
import java.util.*;
43 45

  
44

  
45 46
/**
46 47
 * A JUnit test for testing Step class processing
47 48
 */
48
public class MetaCatServletTest extends TestCase
49
{
49
public class MetaCatServletTest extends TestCase {
50
	private static String metacatURL;
51
	private String serialNumber;
52
	private static final Log log = LogFactory
53
			.getLog("edu.ucsb.nceas.metacattest.MetaCatServletTest");
50 54
	/* 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
  private String metacatURL=MetaCatUtil.getOption("junittesturl");
63
  private String serialNumber;
64
  private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.MetaCatServletTest");
65
  
66
  
67
  /**
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
  }
55
	static {
56
		try {
57
			metacatURL = SystemUtil.getServletURL();
58
			Options.initialize(new File("build/tests/metacat.properties"));
59
		} catch (Exception e) {
60
			System.err.println("Exception in initialize option in MetacatServletNetTest "
61
					+ e.getMessage());
62
		}
63
	}
76 64

  
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
 }
65
	/**
66
	 * Constructor to build the test
67
	 *
68
	 * @param name the name of the test method
69
	 */
70
	public MetaCatServletTest(String name) {
71
		super(name);
72
	}
94 73

  
95
  /**
96
   * Release any objects after tests are complete
97
   */
98
  public void tearDown()
99
  {
100
  }
74
	/**
75
	 * Constructor to build the test
76
	 *
77
	 * @param name the name of the test method
78
	 */
79
	public MetaCatServletTest(String name, String serial) {
80
		super(name);
81
		serialNumber = serial;
82
	}
101 83

  
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
    suite.addTest(new MetaCatServletTest("initialize"));
113
    suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
114
    suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
115
    suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
116
    suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
117
    suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
118
    //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
    number = Math.random()*100000;
124
    serial = Integer.toString(((new Double(number)).intValue()));
125
    log.debug("serial: "+serial);
126
    suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
127
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", 
128
                                                        serial));
129
    suite.addTest(new MetaCatServletTest("testUpdateXMLDocument",serial));
130
        
131
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", 
132
                                                        serial));
133
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", 
134
                                                        serial));
135
    
136
    suite.addTest(new MetaCatServletTest("testDeleteXMLDocument",serial));
137
    
138
    //insert invalid xml document
139
    number = Math.random()*100000;
140
    serial = Integer.toString(((new Double(number)).intValue()));
141
    suite.addTest(new MetaCatServletTest("testInsertInvalidateXMLDocument",
142
                                                                  serial));    
143
    //insert non well formed document
144
    number = Math.random()*100000;
145
    serial = Integer.toString(((new Double(number)).intValue())); 
146
    suite.addTest(new MetaCatServletTest("testInsertNonWellFormedXMLDocument",
147
                                                            serial));
148
    
149
    suite.addTest(new MetaCatServletTest("testLogOut"));                   
150
                              
151
    return suite;
152
  }
153
  
84
	/**
85
	 * Establish a testing framework by initializing appropriate objects
86
	 */
87
	public void setUp() {
154 88

  
89
	}
155 90

  
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
    assertTrue(1 == 1);
163
  }
91
	/**
92
	 * Release any objects after tests are complete
93
	 */
94
	public void tearDown() {
95
	}
164 96

  
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
    assertTrue(logIn(user,passwd));
173
    //assertTrue( withProtocol.getProtocol().equals("http"));
174
  }
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
    assertTrue(!logIn(user,passwd));
184
    
185
  }
186
  
187
  /**
188
   * Test the login to lter succesfully
189
   */
190
  public void testLterReferralLogin()
191
  {
192
    String user=MetaCatUtil.getOption("lteruser");
193
    String passwd=MetaCatUtil.getOption("lterpassword");
194
    assertTrue(logIn(user,passwd));
195
    
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
    assertTrue(!logIn(user,passwd));
206
    //assertTrue( withProtocol.getProtocol().equals("http"));
207
  }
208
  
209
 /**
210
   * Test the login to pisco succesfully
211
   */
212
   public void testPiscoReferralLogin()
213
  {
214
	String user=MetaCatUtil.getOption("piscouser");
215
	String passwd=MetaCatUtil.getOption("piscopassword");
216
    assertTrue(logIn(user,passwd));
217
    //assertTrue( withProtocol.getProtocol().equals("http"));
218
  }
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
    assertTrue(!logIn(user,passwd));
228
    //assertTrue( withProtocol.getProtocol().equals("http"));
229
  }
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
    log.debug("insert docid: "+name);
239
    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
    log.debug("xml document: "+content);
255
     assertTrue(handleXMLDocument(content, name, "insert"));
256
   
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
    log.debug("insert docid: "+name);
268
    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
    log.debug("xml document: "+content);
281
     assertTrue(handleXMLDocument(content, name, "insert"));
282
  }
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
    log.debug("insert non well-formed docid: "+name);
293
    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
    log.debug("xml document: "+content);
306
     assertTrue(!handleXMLDocument(content, name, "insert"));
307
  } 
97
	/**
98
	 * Create a suite of tests to be run together
99
	 */
100
	public static Test suite() {
101
		double number = 0;
102
		String serial = null;
308 103

  
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
    assertTrue(handleReadAction(name, "xml"));
317
   
318
  } 
104
		TestSuite suite = new TestSuite();
105
		suite.addTest(new MetaCatServletTest("initialize"));
106
		suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
107
		suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
108
		suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
109
		suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
110
		suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
111
		//Should put a login successfully at the end of login test
112
		//So insert or update can have cookie.
113
		suite.addTest(new MetaCatServletTest("testNCEASLogin"));
319 114

  
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
    assertTrue(handleReadAction(name, "html"));
328
   
329
  }
115
		//create random number for docid, so it can void repeat
116
		number = Math.random() * 100000;
117
		serial = Integer.toString(((new Double(number)).intValue()));
118
		log.debug("serial: " + serial);
119
		suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
120
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", serial));
121
		suite.addTest(new MetaCatServletTest("testUpdateXMLDocument", serial));
330 122

  
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
    assertTrue(handleReadAction(name, "zip"));
340
   
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
    log.debug("update docid: "+name);
351
    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
    log.debug("xml document: "+content);
367
     assertTrue(handleXMLDocument(content, name, "update"));
368
   
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
    log.debug("delete docid: "+name);
379
    assertTrue(handleDeleteFile(name));
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);
123
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", serial));
124
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", serial));
407 125

  
408
    // Now contact metacat
409
    String response = getMetacatString(prop);
410
    log.debug("Login Message: "+response);
411
    boolean connected = false;
412
    if (response.indexOf("<login>") != -1) {
413
      connected = true;
414
    } else {
415
      
416
      connected = false;
417
    }
126
		suite.addTest(new MetaCatServletTest("testDeleteXMLDocument", serial));
418 127

  
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
    log.debug("Logout Message: "+response);
438
    HttpMessage.setCookie(null);
439
     
440
    if (response.indexOf("<logout>") != -1) 
441
    {
442
      disConnected = true;
443
    } 
444
    else 
445
    {
446
      disConnected = false;
447
    }
128
		//insert invalid xml document
129
		number = Math.random() * 100000;
130
		serial = Integer.toString(((new Double(number)).intValue()));
131
		suite.addTest(new MetaCatServletTest("testInsertInvalidateXMLDocument", serial));
132
		//insert non well formed document
133
		number = Math.random() * 100000;
134
		serial = Integer.toString(((new Double(number)).intValue()));
135
		suite
136
				.addTest(new MetaCatServletTest("testInsertNonWellFormedXMLDocument",
137
						serial));
448 138

  
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
    log.debug("Insert or Update Message: "+message);
504
    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
    log.debug("Delete Message: "+message);
532
    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;
139
		suite.addTest(new MetaCatServletTest("testLogOut"));
555 140

  
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
  }
141
		return suite;
142
	}
606 143

  
607
 
144
	/**
145
	 * Run an initial test that always passes to check that the test
146
	 * harness is working.
147
	 */
148
	public void initialize() {
149
		assertTrue(1 == 1);
150
	}
608 151

  
152
	/**
153
	 * Test the login to neceas succesfully
154
	 */
155
	public void testNCEASLogin() {
156
		String user = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
157
		String passwd = "123456";
158
		assertTrue(logIn(user, passwd));
159
		//assertTrue( withProtocol.getProtocol().equals("http"));
160
	}
161

  
162
	/**
163
	 * Test the login to neceas failed
164
	 */
165
	public void testNCEASLoginFail() {
166
		String user = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
167
		String passwd = "12345678";
168
		assertTrue(!logIn(user, passwd));
169

  
170
	}
171

  
172
	/**
173
	 * Test the login to lter succesfully
174
	 */
175
	public void testLterReferralLogin() {
176
		String user = null;
177
		String passwd = null;
178
		try {
179
			user = PropertyService.getProperty("lteruser");
180
			passwd = PropertyService.getProperty("lterpassword");
181
		} catch (PropertyNotFoundException pnfe) {
182
			fail("Could not find property: " + pnfe.getMessage());
183
		}
184
		assertTrue(logIn(user, passwd));
185

  
186
	}
187

  
188
	/**
189
	 * Test the login to lter failed
190
	 */
191
	public void testLterReferralLoginFail() {
192
		String user = "uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
193
		String passwd = "qVyGpveb";
194
		assertTrue(!logIn(user, passwd));
195
		//assertTrue( withProtocol.getProtocol().equals("http"));
196
	}
197

  
198
	/**
199
	 * Test the login to pisco succesfully
200
	 */
201
	public void testPiscoReferralLogin() {
202
		String user = null;
203
		String passwd = null;
204
		try {
205
			user = PropertyService.getProperty("piscouser");
206
			passwd = PropertyService.getProperty("piscopassword");
207
		} catch (PropertyNotFoundException pnfe) {
208
			fail("Could not find property: " + pnfe.getMessage());
209
		}
210
		assertTrue(logIn(user, passwd));
211
		// assertTrue( withProtocol.getProtocol().equals("http"));
212
	}
213

  
214
	/**
215
	 * Test the login to pisco failed
216
	 */
217
	public void testPiscoReferralLoginFail() {
218
		String user = "uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
219
		String passwd = "hello";
220
		assertTrue(!logIn(user, passwd));
221
		//assertTrue( withProtocol.getProtocol().equals("http"));
222
	}
223

  
224
	/**
225
	 * Test insert a xml document successfully
226
	 */
227
	public void testInsertXMLDocument() {
228
		String name = null;
229
		try {
230
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
231
					+ PropertyService.getProperty("accNumSeparator") + "1";
232
		} catch (PropertyNotFoundException pnfe) {
233
			fail("Could not find property: " + pnfe.getMessage());
234
		}
235
		log.debug("insert docid: " + name);
236
		String content = "<?xml version=\"1.0\"?>"
237
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
238
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
239
				+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
240
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
241
				+ "</identifier>" + "<allow>"
242
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
243
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
244
				+ "<principal>public</principal>" + "<permission>read</permission>"
245
				+ "</allow>" + "</acl>";
246
		log.debug("xml document: " + content);
247
		assertTrue(handleXMLDocument(content, name, "insert"));
248

  
249
	}
250

  
251
	/**
252
	 * Test insert a invalidate xml document successfully
253
	 * In the String, there is no <!Doctype ... Public/System/>
254
	 */
255
	public void testInsertInvalidateXMLDocument() {
256
		String name = null;
257
		try {
258
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
259
				+ PropertyService.getProperty("accNumSeparator") + "1";
260
		} catch (PropertyNotFoundException pnfe) {
261
			fail("Could not find property: " + pnfe.getMessage());
262
		}
263
		log.debug("insert docid: " + name);
264
		String content = "<?xml version=\"1.0\"?>"
265
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
266
				+ "</identifier>" + "<allow>"
267
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
268
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
269
				+ "<principal>public</principal>" + "<permission>read</permission>"
270
				+ "</allow>" + "</acl>";
271
		log.debug("xml document: " + content);
272
		assertTrue(handleXMLDocument(content, name, "insert"));
273
	}
274

  
275
	/**
276
	 * Test insert a non well-formed xml document successfully
277
	 * There is no </acl> in this string
278
	 */
279
	public void testInsertNonWellFormedXMLDocument() {
280
		String name = null;
281
		try {
282
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
283
				+ PropertyService.getProperty("accNumSeparator") + "1";
284
		} catch (PropertyNotFoundException pnfe) {
285
			fail("Could not find property: " + pnfe.getMessage());
286
		}
287
		log.debug("insert non well-formed docid: " + name);
288
		String content = "<?xml version=\"1.0\"?>"
289
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
290
				+ "</identifier>" + "<allow>"
291
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
292
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
293
				+ "<principal>public</principal>" + "<permission>read</permission>"
294
				+ "</allow>";
295

  
296
		log.debug("xml document: " + content);
297
		assertTrue(!handleXMLDocument(content, name, "insert"));
298
	}
299

  
300
	/**
301
	 * Test read a xml document  in xml format successfully
302
	 */
303
	public void testReadXMLDocumentXMLFormat() {
304
		String name = null;
305
		try {
306
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
307
				+ PropertyService.getProperty("accNumSeparator") + "1";
308
		} catch (PropertyNotFoundException pnfe) {
309
			fail("Could not find property: " + pnfe.getMessage());
310
		}
311
		assertTrue(handleReadAction(name, "xml"));
312

  
313
	}
314

  
315
	/**
316
	 * Test read a xml document  in html format successfully
317
	 */
318
	public void testReadXMLDocumentHTMLFormat() {
319
		String name = null;
320
		try {
321
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
322
				+ PropertyService.getProperty("accNumSeparator") + "1";
323
		} catch (PropertyNotFoundException pnfe) {
324
			fail("Could not find property: " + pnfe.getMessage());
325
		}	
326
		assertTrue(handleReadAction(name, "html"));
327

  
328
	}
329

  
330
	/**
331
	 * Test read a xml document  in zip format successfully
332
	 */
333
	public void testReadXMLDocumentZipFormat() {
334
		String name = null;
335
		try {
336
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
337
				+ PropertyService.getProperty("accNumSeparator") + "1";
338
		} catch (PropertyNotFoundException pnfe) {
339
			fail("Could not find property: " + pnfe.getMessage());
340
		}	
341
		assertTrue(handleReadAction(name, "zip"));
342

  
343
	}
344

  
345
	/**
346
	 * Test insert a xml document successfully
347
	 */
348
	public void testUpdateXMLDocument() {
349
		String name = null;
350
		try {
351
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
352
				+ PropertyService.getProperty("accNumSeparator") + "2";
353
		} catch (PropertyNotFoundException pnfe) {
354
			fail("Could not find property: " + pnfe.getMessage());
355
		}		
356
		log.debug("update docid: " + name);
357
		String content = "<?xml version=\"1.0\"?>"
358
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
359
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
360
				+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
361
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
362
				+ "</identifier>" + "<allow>"
363
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
364
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
365
				+ "<principal>public</principal>" + "<permission>read</permission>"
366
				+ "</allow>" + "</acl>";
367
		log.debug("xml document: " + content);
368
		assertTrue(handleXMLDocument(content, name, "update"));
369

  
370
	}
371

  
372
	/**
373
	 * Test delete a xml document successfully
374
	 */
375
	public void testDeleteXMLDocument() {
376
		String name = null;
377
		try {
378
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
379
				+ PropertyService.getProperty("accNumSeparator") + "2";
380
		} catch (PropertyNotFoundException pnfe) {
381
			fail("Could not find property: " + pnfe.getMessage());
382
		}
383
		log.debug("delete docid: " + name);
384
		assertTrue(handleDeleteFile(name));
385

  
386
	}
387

  
388
	/**
389
	 * Test logout action
390
	 */
391
	public void testLogOut() {
392

  
393
		assertTrue(handleLogOut());
394

  
395
	}
396

  
397
	/**
398
	 * Method to hanld login action
399
	 *
400
	 * @param usrerName, the DN name of the test method
401
	 * @param passWord, the passwd of the user
402
	 */
403

  
404
	public boolean logIn(String userName, String passWord) {
405
		Properties prop = new Properties();
406
		prop.put("action", "login");
407
		prop.put("qformat", "xml");
408
		prop.put("username", userName);
409
		prop.put("password", passWord);
410

  
411
		// Now contact metacat
412
		String response = getMetacatString(prop);
413
		log.debug("Login Message: " + response);
414
		boolean connected = false;
415
		if (response.indexOf("<login>") != -1) {
416
			connected = true;
417
		} else {
418

  
419
			connected = false;
420
		}
421

  
422
		return connected;
423
	}
424

  
425
	/**
426
	 * Method to hanld logout action
427
	 *
428
	 * @param usrerName, the DN name of the test method
429
	 * @param passWord, the passwd of the user
430
	 */
431

  
432
	public boolean handleLogOut() {
433
		boolean disConnected = false;
434
		Properties prop = new Properties();
435
		prop.put("action", "logout");
436
		prop.put("qformat", "xml");
437

  
438
		String response = getMetacatString(prop);
439
		log.debug("Logout Message: " + response);
440
		HttpMessage.setCookie(null);
441

  
442
		if (response.indexOf("<logout>") != -1) {
443
			disConnected = true;
444
		} else {
445
			disConnected = false;
446
		}
447

  
448
		return disConnected;
449
	}
450

  
451
	/**
452
	 * Method to hanld read both xml and data file
453
	 *
454
	 * @param docid, the docid of the document want to read
455
	 * @param qformat, the format of document user want to get
456
	 */
457
	public boolean handleReadAction(String docid, String qformat) {
458
		Properties prop = new Properties();
459
		String message = "";
460
		prop.put("action", "read");
461
		prop.put("qformat", qformat);
462
		prop.put("docid", docid);
463

  
464
		message = getMetacatString(prop);
465
		message = message.trim();
466
		if (message == null || message.equals("") || message.indexOf("<error>") != -1) {//there was an error
467

  
468
			return false;
469
		} else {//successfully
470
			return true;
471
		}
472

  
473
	}
474

  
475
	/**
476
	 * Method to hanld inset or update xml document
477
	 *
478
	 * @param xmlDocument, the content of xml qformat
479
	 * @param docid, the docid of the document
480
	 * @param action, insert or update
481
	 */
482
	public boolean handleXMLDocument(String xmlDocument, String docid, String action)
483

  
484
	{ //-attempt to write file to metacat
485
		String access = "no";
486
		StringBuffer fileText = new StringBuffer();
487
		StringBuffer messageBuf = new StringBuffer();
488
		String accessFileId = null;
489
		Properties prop = new Properties();
490
		prop.put("action", action);
491
		prop.put("public", access); //This is the old way of controlling access
492
		prop.put("doctext", xmlDocument);
493
		prop.put("docid", docid);
494

  
495
		String message = getMetacatString(prop);
496
		log.debug("Insert or Update Message: " + message);
497
		if (message.indexOf("<error>") != -1) {//there was an error
498

  
499
			return false;
500
		} else if (message.indexOf("<success>") != -1) {//the operation worked
501
			//write the file to the cache and return the file object
502
			return true;
503

  
504
		} else {//something weird happened.
505
			return false;
506
		}
507

  
508
	}
509

  
510
	public boolean handleDeleteFile(String name) {
511

  
512
		Properties prop = new Properties();
513
		prop.put("action", "delete");
514
		prop.put("docid", name);
515

  
516
		String message = getMetacatString(prop);
517
		log.debug("Delete Message: " + message);
518
		if (message.indexOf("<error>") != -1) {//there was an error
519

  
520
			return false;
521
		} else if (message.indexOf("<success>") != -1) {//the operation worked
522
			//write the file to the cache and return the file object
523
			return true;
524

  
525
		} else {//something weird happened.
526
			return false;
527
		}
528
	}
529

  
530
	public String getMetacatString(Properties prop) {
531
		String response = null;
532

  
533
		// Now contact metacat and send the request
534
		try {
535
			InputStreamReader returnStream = new InputStreamReader(
536
					getMetacatInputStream(prop));
537
			StringWriter sw = new StringWriter();
538
			int len;
539
			char[] characters = new char[512];
540
			while ((len = returnStream.read(characters, 0, 512)) != -1) {
541
				sw.write(characters, 0, len);
542
			}
543
			returnStream.close();
544
			response = sw.toString();
545
			sw.close();
546
		} catch (Exception e) {
547
			return null;
548
		}
549

  
550
		return response;
551
	}
552

  
553
	/**
554
	 * Send a request to Metacat
555
	 *
556
	 * @param prop the properties to be sent to Metacat
557
	 * @return InputStream as returned by Metacat
558
	 */
559
	public InputStream getMetacatInputStream(Properties prop) {
560
		InputStream returnStream = null;
561
		// Now contact metacat and send the request
562
		try {
563

  
564
			URL url = new URL(metacatURL);
565
			HttpMessage msg = new HttpMessage(url);
566
			returnStream = msg.sendPostMessage(prop);
567
			return returnStream;
568
		} catch (Exception e) {
569
			e.printStackTrace(System.err);
570

  
571
		}
572
		return returnStream;
573

  
574
	}
575

  
609 576
}

Also available in: Unified diff