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-15 08:23:03 -0700 (Wed, 15 May 2002) $'
11
 * '$Revision: 1085 $'
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
  
90
    
91
    
92
    TestSuite suite = new TestSuite();
93
    suite.addTest(new MetaCatServletTest("initialize"));
94
    suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
95
    suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
96
    suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
97
    suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
98
    suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
99
    //Should put a login successfully at the end of login test
100
    //So insert or update can have cookie.
101
    suite.addTest(new MetaCatServletTest("testNCEASLogin"));
102
    
103
    //create random number for docid, so it can void repeat
104
    double number = Math.random()*10000;
105
    String serial = Integer.toString(((new Double(number)).intValue()));
106
    MetaCatUtil.debugMessage("serial: "+serial, 1);
107
    suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
108
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", 
109
                                                        serial));
110
        
111
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", 
112
                                                        serial));
113
    suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", 
114
                                                        serial));
115
    suite.addTest(new MetaCatServletTest("testUpdateXMLDocument",serial));
116
    suite.addTest(new MetaCatServletTest("testDeleteXMLDocument",serial));
117
    
118
    suite.addTest(new MetaCatServletTest("testLogOut"));
119
                                                        
120
                         
121
                              
122
    return suite;
123
  }
124
  
125

    
126

    
127
  /**
128
   * Run an initial test that always passes to check that the test
129
   * harness is working.
130
   */
131
  public void initialize()
132
  {
133
    assert(1 == 1);
134
  }
135

    
136
  /**
137
   * Test the login to neceas succesfully
138
   */
139
  public void testNCEASLogin()
140
  {
141
    String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
142
    String passwd="123456";
143
    assert(logIn(user,passwd));
144
    //assert( withProtocol.getProtocol().equals("http"));
145
  }
146
  
147
  /**
148
   * Test the login to neceas failed
149
   */
150
  public void testNCEASLoginFail()
151
  {
152
    String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
153
    String passwd="12345678";
154
    assert(!logIn(user,passwd));
155
    
156
  }
157
  
158
  /**
159
   * Test the login to lter succesfully
160
   */
161
  public void testLterReferralLogin()
162
  {
163
    String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
164
    String passwd="qVyGpVeb";
165
    assert(logIn(user,passwd));
166
    
167
  }
168
  
169
  /**
170
   * Test the login to lter failed
171
   */
172
  public void testLterReferralLoginFail()
173
  {
174
    String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
175
    String passwd="qVyGpveb";
176
    assert(!logIn(user,passwd));
177
    //assert( withProtocol.getProtocol().equals("http"));
178
  }
179
  
180
 /**
181
   * Test the login to pisco succesfully
182
   */
183
   public void testPiscoReferralLogin()
184
  {
185
    String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
186
    String passwd="purple";
187
    assert(logIn(user,passwd));
188
    //assert( withProtocol.getProtocol().equals("http"));
189
  }
190
  
191
 /**
192
   * Test the login to pisco failed
193
   */
194
  public void testPiscoReferralLoginFail()
195
  {
196
    String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
197
    String passwd="hello";
198
    assert(!logIn(user,passwd));
199
    //assert( withProtocol.getProtocol().equals("http"));
200
  }
201
  
202
 /**
203
   * Test insert a xml document successfully
204
   */
205
  public void testInsertXMLDocument()
206
  {
207
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
208
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
209
    MetaCatUtil.debugMessage("docid: "+name, 20);
210
    String content="<?xml version=\"1.0\"?>"
211
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
212
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
213
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
214
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
215
                   +"<identifier>"+name+"</identifier>"
216
                   +"<allow>"
217
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
218
                   +"<permission>all</permission>"
219
                   +"</allow>"
220
                   +"<allow>"
221
                   +"<principal>public</principal>"
222
                   +"<permission>read</permission>"
223
                   +"</allow>"
224
                   +"</acl>";
225
     MetaCatUtil.debugMessage("xml document: "+content, 55);
226
     assert(handleXMLDocument(content, name, "insert"));
227
   
228
  }
229
  
230

    
231

    
232
  /**
233
   * Test read a xml document  in xml format successfully
234
   */
235
  public void testReadXMLDocumentXMLFormat()
236
  {
237
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
238
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
239
    assert(handleReadAction(name, "xml"));
240
   
241
  } 
242

    
243
  /**
244
   * Test read a xml document  in html format successfully
245
   */
246
  public void testReadXMLDocumentHTMLFormat()
247
  {
248
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
249
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
250
    assert(handleReadAction(name, "html"));
251
   
252
  }
253

    
254
 
255
  /**
256
   * Test read a xml document  in zip format successfully
257
   */
258
  public void testReadXMLDocumentZipFormat()
259
  {
260
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
261
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
262
    assert(handleReadAction(name, "zip"));
263
   
264
  }
265
  
266
  /**
267
   * Test insert a xml document successfully
268
   */
269
  public void testUpdateXMLDocument()
270
  {
271
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
272
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
273
    MetaCatUtil.debugMessage("docid: "+name, 20);
274
    String content="<?xml version=\"1.0\"?>"
275
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
276
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
277
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
278
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
279
                   +"<identifier>"+name+"</identifier>"
280
                   +"<allow>"
281
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
282
                   +"<permission>all</permission>"
283
                   +"</allow>"
284
                   +"<allow>"
285
                   +"<principal>public</principal>"
286
                   +"<permission>read</permission>"
287
                   +"</allow>"
288
                   +"</acl>";
289
     MetaCatUtil.debugMessage("xml document: "+content, 55);
290
     assert(handleXMLDocument(content, name, "update"));
291
   
292
  }
293
  
294
  /**
295
   * Test delete a xml document successfully
296
   */
297
  public void testDeleteXMLDocument()
298
  {
299
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
300
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
301
    assert(handleDeleteFile(name));
302
   
303
  }
304
       
305
  /**
306
   * Test logout action
307
   */
308
  public void testLogOut()
309
  {
310
    
311
    assert(handleLogOut());
312
   
313
  }
314
  
315
 /**
316
   * Method to hanld login action
317
   *
318
   * @param usrerName, the DN name of the test method
319
   * @param passWord, the passwd of the user
320
   */
321
  
322
  public boolean logIn(String userName, String passWord)
323
  {
324
    Properties prop = new Properties();
325
    prop.put("action", "login");
326
    prop.put("qformat", "xml");
327
    prop.put("username", userName);
328
    prop.put("password", passWord);
329

    
330
    // Now contact metacat
331
    String response = getMetacatString(prop);
332
    MetaCatUtil.debugMessage("Login Message: "+response, 30);
333
    boolean connected = false;
334
    if (response.indexOf("<login>") != -1) {
335
      connected = true;
336
    } else {
337
      
338
      connected = false;
339
    }
340

    
341
    return connected;
342
  }
343
  
344
  /**
345
   * Method to hanld logout action
346
   *
347
   * @param usrerName, the DN name of the test method
348
   * @param passWord, the passwd of the user
349
   */
350
  
351
  public boolean handleLogOut()
352
  {
353
    boolean disConnected =false;
354
    Properties prop = new Properties();
355
    prop.put("action", "logout");
356
    prop.put("qformat", "xml");
357
  
358
    String response = getMetacatString(prop);
359
     MetaCatUtil.debugMessage("Logout Message: "+response, 30);
360
    HttpMessage.setCookie(null);
361
     
362
    if (response.indexOf("<logout>") != -1) 
363
    {
364
      disConnected = true;
365
    } 
366
    else 
367
    {
368
      disConnected = false;
369
    }
370

    
371
    return disConnected;
372
  }
373
  
374
  /**
375
   * Method to hanld read both xml and data file
376
   *
377
   * @param docid, the docid of the document want to read
378
   * @param qformat, the format of document user want to get
379
   */
380
  public boolean handleReadAction(String docid, String qformat)
381
  {
382
    Properties prop = new Properties();
383
    String message ="";
384
    prop.put("action", "read");
385
    prop.put("qformat", qformat);
386
    prop.put("docid", docid);
387
    
388
    message = getMetacatString(prop);
389
    message = message.trim();
390
    MetaCatUtil.debugMessage("Read Message: "+message, 30);
391
    if (message==null || message.equals("")||message.indexOf("<error>") != -1 )
392
    {//there was an error
393
      
394
      return false;
395
    }
396
    else
397
    {//successfully
398
      return true;
399
    } 
400
    
401
  }
402
  
403
  
404
  /**
405
   * Method to hanld inset or update xml document
406
   *
407
   * @param xmlDocument, the content of xml qformat
408
   * @param docid, the docid of the document
409
   * @param action, insert or update
410
   */
411
  public boolean handleXMLDocument(String xmlDocument, String docid, 
412
                                                              String action)
413
 
414
  { //-attempt to write file to metacat
415
    String access = "no";
416
    StringBuffer fileText = new StringBuffer();
417
    StringBuffer messageBuf = new StringBuffer();
418
    String accessFileId = null;
419
    Properties prop = new Properties();
420
    prop.put("action", action);
421
    prop.put("public", access);  //This is the old way of controlling access
422
    prop.put("doctext", xmlDocument);
423
    prop.put("docid", docid);
424
    
425
    String message = getMetacatString(prop);
426
    MetaCatUtil.debugMessage("Insert or Update Message: "+message, 30);
427
    if(message.indexOf("<error>") != -1)
428
    {//there was an error
429
      
430
      return false;
431
    }
432
    else if(message.indexOf("<success>") != -1)
433
    {//the operation worked
434
     //write the file to the cache and return the file object
435
     return true;
436
       
437
    }
438
    else
439
    {//something weird happened.
440
      return false;
441
    } 
442
   
443
   
444
  }
445
  
446
   public boolean handleDeleteFile(String name)
447
  {
448
    
449
    Properties prop = new Properties();
450
    prop.put("action", "delete");
451
    prop.put("docid", name);
452
 
453
    String message = getMetacatString(prop);
454
    MetaCatUtil.debugMessage("Delete Message: "+message, 30);
455
    if(message.indexOf("<error>") != -1)
456
    {//there was an error
457
      
458
      return false;
459
    }
460
    else if(message.indexOf("<success>") != -1)
461
    {//the operation worked
462
     //write the file to the cache and return the file object
463
     return true;
464
       
465
    }
466
    else
467
    {//something weird happened.
468
      return false;
469
    } 
470
  }
471
  
472
   public String getMetacatString(Properties prop)
473
  {
474
    String response = null;
475

    
476
    // Now contact metacat and send the request
477
    try
478
    {
479
      InputStreamReader returnStream = 
480
                        new InputStreamReader(getMetacatInputStream(prop));
481
      StringWriter sw = new StringWriter();
482
      int len;
483
      char[] characters = new char[512];
484
      while ((len = returnStream.read(characters, 0, 512)) != -1)
485
      {
486
        sw.write(characters, 0, len);
487
      }
488
      returnStream.close();
489
      response = sw.toString();
490
      sw.close();
491
    }
492
    catch(Exception e)
493
    {
494
      return null;
495
    }
496
  
497
    return response;
498
  }
499
  
500
   /**
501
   * Send a request to Metacat
502
   *
503
   * @param prop the properties to be sent to Metacat
504
   * @return InputStream as returned by Metacat
505
   */
506
  public InputStream getMetacatInputStream(Properties prop)
507
  {
508
    InputStream returnStream = null;
509
    // Now contact metacat and send the request
510
    try
511
    {
512
     
513
      URL url = new URL(metacatURL);
514
      HttpMessage msg = new HttpMessage(url);
515
      returnStream = msg.sendPostMessage(prop);
516
      return returnStream;
517
    }
518
    catch(Exception e)
519
    {
520
      e.printStackTrace(System.err);
521
     
522
    }
523
    return returnStream;
524
   
525
  }
526

    
527
 
528

    
529
}
(1-1/2)