Project

General

Profile

1 1050 tao
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the MetaCatURL class by JUnit
6
 *    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 1082 tao
  private String metacatURL=MetaCatUtil.getOption("junittesturl");
48
  private String serialNumber;
49 1050 tao
  /**
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 1082 tao
    System.out.println("metacatURL: "+metacatURL);
58 1050 tao
  }
59
60 1082 tao
 /**
61
   * Constructor to build the test
62
   *
63
   * @param name the name of the test method
64
   */
65
  public MetaCatServletTest(String name, String serial)
66
  {
67
    super(name);
68
    serialNumber=serial;
69
  }
70 1050 tao
  /**
71
   * Establish a testing framework by initializing appropriate objects
72
   */
73
  public void setUp()
74 1082 tao
 {
75 1050 tao
76 1082 tao
 }
77 1050 tao
78
  /**
79
   * Release any objects after tests are complete
80
   */
81
  public void tearDown()
82
  {
83
  }
84
85
  /**
86
   * Create a suite of tests to be run together
87
   */
88
  public static Test suite()
89
  {
90 1082 tao
91
92
93 1050 tao
    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 1082 tao
    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
    double number = Math.random()*10000;
106
    String 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
    suite.addTest(new MetaCatServletTest("testLogOut"));
120
121
122
123 1050 tao
    return suite;
124
  }
125
126
127
128
  /**
129
   * Run an initial test that always passes to check that the test
130
   * harness is working.
131
   */
132
  public void initialize()
133
  {
134
    assert(1 == 1);
135
  }
136
137
  /**
138 1082 tao
   * Test the login to neceas succesfully
139 1050 tao
   */
140
  public void testNCEASLogin()
141
  {
142
    String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
143
    String passwd="123456";
144
    assert(logIn(user,passwd));
145
    //assert( withProtocol.getProtocol().equals("http"));
146
  }
147
148 1082 tao
  /**
149
   * Test the login to neceas failed
150
   */
151 1050 tao
  public void testNCEASLoginFail()
152
  {
153
    String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
154
    String passwd="12345678";
155
    assert(!logIn(user,passwd));
156 1082 tao
157 1050 tao
  }
158 1082 tao
159
  /**
160
   * Test the login to lter succesfully
161
   */
162
  public void testLterReferralLogin()
163 1050 tao
  {
164
    String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
165
    String passwd="qVyGpVeb";
166
    assert(logIn(user,passwd));
167 1082 tao
168 1050 tao
  }
169
170 1082 tao
  /**
171
   * Test the login to lter failed
172
   */
173 1050 tao
  public void testLterReferralLoginFail()
174
  {
175
    String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
176
    String passwd="qVyGpveb";
177
    assert(!logIn(user,passwd));
178
    //assert( withProtocol.getProtocol().equals("http"));
179
  }
180
181 1082 tao
 /**
182
   * Test the login to pisco succesfully
183
   */
184 1050 tao
   public void testPiscoReferralLogin()
185
  {
186
    String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
187
    String passwd="purple";
188
    assert(logIn(user,passwd));
189
    //assert( withProtocol.getProtocol().equals("http"));
190
  }
191
192 1082 tao
 /**
193
   * Test the login to pisco failed
194
   */
195 1050 tao
  public void testPiscoReferralLoginFail()
196
  {
197
    String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
198
    String passwd="hello";
199
    assert(!logIn(user,passwd));
200
    //assert( withProtocol.getProtocol().equals("http"));
201
  }
202
203 1082 tao
 /**
204
   * Test insert a xml document successfully
205
   */
206
  public void testInsertXMLDocument()
207
  {
208
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
209
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
210
    MetaCatUtil.debugMessage("docid: "+name, 20);
211
    String content="<?xml version=\"1.0\"?>"
212
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
213
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
214
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
215
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
216
                   +"<identifier>"+name+"</identifier>"
217
                   +"<allow>"
218
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
219
                   +"<permission>all</permission>"
220
                   +"</allow>"
221
                   +"<allow>"
222
                   +"<principal>public</principal>"
223
                   +"<permission>read</permission>"
224
                   +"</allow>"
225
                   +"</acl>";
226
     MetaCatUtil.debugMessage("xml document: "+content, 55);
227
     assert(handleXMLDocument(content, name, "insert"));
228
229
  }
230
231
232
233
  /**
234
   * Test read a xml document  in xml format successfully
235
   */
236
  public void testReadXMLDocumentXMLFormat()
237
  {
238
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
239
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
240
    assert(handleReadAction(name, "xml"));
241
242
  }
243
244
  /**
245
   * Test read a xml document  in html format successfully
246
   */
247
  public void testReadXMLDocumentHTMLFormat()
248
  {
249
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
250
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
251
    assert(handleReadAction(name, "html"));
252
253
  }
254
255
256
  /**
257
   * Test read a xml document  in zip format successfully
258
   */
259
  public void testReadXMLDocumentZipFormat()
260
  {
261
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
262
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
263
    assert(handleReadAction(name, "zip"));
264
265
  }
266
267
  /**
268
   * Test insert a xml document successfully
269
   */
270
  public void testUpdateXMLDocument()
271
  {
272
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
273
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
274
    MetaCatUtil.debugMessage("docid: "+name, 20);
275
    String content="<?xml version=\"1.0\"?>"
276
                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
277
                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
278
                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
279
                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
280
                   +"<identifier>"+name+"</identifier>"
281
                   +"<allow>"
282
             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
283
                   +"<permission>all</permission>"
284
                   +"</allow>"
285
                   +"<allow>"
286
                   +"<principal>public</principal>"
287
                   +"<permission>read</permission>"
288
                   +"</allow>"
289
                   +"</acl>";
290
     MetaCatUtil.debugMessage("xml document: "+content, 55);
291
     assert(handleXMLDocument(content, name, "update"));
292
293
  }
294
295
  /**
296
   * Test delete a xml document successfully
297
   */
298
  public void testDeleteXMLDocument()
299
  {
300
    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
301
                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
302
    assert(handleDeleteFile(name));
303
304
  }
305
306
  /**
307
   * Test logout action
308
   */
309
  public void testLogOut()
310
  {
311
312
    assert(handleLogOut());
313
314
  }
315
316
 /**
317
   * Method to hanld login action
318
   *
319
   * @param usrerName, the DN name of the test method
320
   * @param passWord, the passwd of the user
321
   */
322
323 1050 tao
  public boolean logIn(String userName, String passWord)
324
  {
325
    Properties prop = new Properties();
326
    prop.put("action", "login");
327
    prop.put("qformat", "xml");
328
    prop.put("username", userName);
329
    prop.put("password", passWord);
330
331
    // Now contact metacat
332
    String response = getMetacatString(prop);
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 1082 tao
  /**
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
    HttpMessage.setCookie(null);
360
361
    if (response.indexOf("<logout>") != -1)
362
    {
363
      disConnected = true;
364
    }
365
    else
366
    {
367
      disConnected = false;
368
    }
369
370
    return disConnected;
371
  }
372
373
  /**
374
   * Method to hanld read both xml and data file
375
   *
376
   * @param docid, the docid of the document want to read
377
   * @param qformat, the format of document user want to get
378
   */
379
  public boolean handleReadAction(String docid, String qformat)
380
  {
381
    Properties prop = new Properties();
382
    String message ="";
383
    prop.put("action", "read");
384
    prop.put("qformat", qformat);
385
    prop.put("docid", docid);
386
387
    message = getMetacatString(prop);
388
    message = message.trim();
389
    if (message==null || message.equals("")||message.indexOf("<error>") != -1 )
390
    {//there was an error
391
392
      return false;
393
    }
394
    else
395
    {//successfully
396
      return true;
397
    }
398
399
  }
400
401
402
  /**
403
   * Method to hanld inset or update xml document
404
   *
405
   * @param xmlDocument, the content of xml qformat
406
   * @param docid, the docid of the document
407
   * @param action, insert or update
408
   */
409
  public boolean handleXMLDocument(String xmlDocument, String docid,
410
                                                              String action)
411
412
  { //-attempt to write file to metacat
413
    String access = "no";
414
    StringBuffer fileText = new StringBuffer();
415
    StringBuffer messageBuf = new StringBuffer();
416
    String accessFileId = null;
417
    Properties prop = new Properties();
418
    prop.put("action", action);
419
    prop.put("public", access);  //This is the old way of controlling access
420
    prop.put("doctext", xmlDocument);
421
    prop.put("docid", docid);
422
423
    String message = getMetacatString(prop);
424
425
    if(message.indexOf("<error>") != -1)
426
    {//there was an error
427
428
      return false;
429
    }
430
    else if(message.indexOf("<success>") != -1)
431
    {//the operation worked
432
     //write the file to the cache and return the file object
433
     return true;
434
435
    }
436
    else
437
    {//something weird happened.
438
      return false;
439
    }
440
441
442
  }
443
444
   public boolean handleDeleteFile(String name)
445
  {
446
447
    Properties prop = new Properties();
448
    prop.put("action", "delete");
449
    prop.put("docid", name);
450
451
    String message = getMetacatString(prop);
452
    System.out.println("Message: "+message);
453
    if(message.indexOf("<error>") != -1)
454
    {//there was an error
455
456
      return false;
457
    }
458
    else if(message.indexOf("<success>") != -1)
459
    {//the operation worked
460
     //write the file to the cache and return the file object
461
     return true;
462
463
    }
464
    else
465
    {//something weird happened.
466
      return false;
467
    }
468
  }
469
470 1050 tao
   public String getMetacatString(Properties prop)
471
  {
472
    String response = null;
473
474
    // Now contact metacat and send the request
475
    try
476
    {
477
      InputStreamReader returnStream =
478
                        new InputStreamReader(getMetacatInputStream(prop));
479
      StringWriter sw = new StringWriter();
480
      int len;
481
      char[] characters = new char[512];
482
      while ((len = returnStream.read(characters, 0, 512)) != -1)
483
      {
484
        sw.write(characters, 0, len);
485
      }
486
      returnStream.close();
487
      response = sw.toString();
488
      sw.close();
489
    }
490
    catch(Exception e)
491
    {
492 1082 tao
      return null;
493 1050 tao
    }
494
495
    return response;
496
  }
497
498
   /**
499
   * Send a request to Metacat
500
   *
501
   * @param prop the properties to be sent to Metacat
502
   * @return InputStream as returned by Metacat
503
   */
504
  public InputStream getMetacatInputStream(Properties prop)
505
  {
506
    InputStream returnStream = null;
507
    // Now contact metacat and send the request
508
    try
509
    {
510
511
      URL url = new URL(metacatURL);
512
      HttpMessage msg = new HttpMessage(url);
513
      returnStream = msg.sendPostMessage(prop);
514
      return returnStream;
515
    }
516
    catch(Exception e)
517
    {
518
      e.printStackTrace(System.err);
519
520
    }
521
    return returnStream;
522
523
  }
524
525
526
527
}