Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2003 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
 *
7
 *   '$Author: sgarg $'
8
 *     '$Date: 2004-08-19 11:50:35 -0700 (Thu, 19 Aug 2004) $'
9
 * '$Revision: 2242 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacattest.client;
27

    
28
import java.io.File;
29
import java.io.FileReader;
30
import java.io.IOException;
31
import java.io.Reader;
32
import java.io.StringReader;
33
import java.util.Calendar;
34
import java.util.Date;
35
import java.util.GregorianCalendar;
36
import java.util.SimpleTimeZone;
37
import java.util.TimeZone;
38

    
39
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
40
import edu.ucsb.nceas.metacat.client.Metacat;
41
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
42
import edu.ucsb.nceas.metacat.client.MetacatException;
43
import edu.ucsb.nceas.metacat.client.MetacatFactory;
44
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
45
import edu.ucsb.nceas.utilities.IOUtil;
46
import junit.framework.Test;
47
import junit.framework.TestCase;
48
import junit.framework.TestSuite;
49

    
50
/**
51
 * A JUnit test for testing Step class processing
52
 */
53
public class MetacatClientTest extends TestCase
54
{
55
    private String metacatUrl = "@systemidserver@@servlet-path@";
56
    private String wrongMetacatUrl=
57
                    "http://somepalce.somewhere.com/some/servlet/metacat";
58
    private String username = "@mcuser@";
59
    private String password = "@mcpassword@";
60
    private String anotheruser = "@mcanotheruser@";
61
    private String anotherpassword = "@mcanotherpassword@";
62
    private String failpass = "uidfnkj43987yfdn";
63
    private String prefix = "test";
64
    private String newdocid = null;
65
    private String testfile = "test/jones.204.22.xml";
66
    private String onlinetestdatafile = "test/onlineDataFile1";
67
    private String queryFile = "test/query.xml";
68
    private String testdocument = "";
69
    private Metacat m;
70

    
71
    /**
72
     * Constructor to build the test
73
     *
74
     * @param name the name of the test method
75
     */
76
    public MetacatClientTest(String name)
77
    {
78
        super(name);
79
        newdocid = generateDocid();
80
    }
81

    
82
    /**
83
     * Establish a testing framework by initializing appropriate objects
84
     */
85
    public void setUp()
86
    {
87
        try {
88
            FileReader fr = new FileReader(testfile);
89
            testdocument = IOUtil.getAsString(fr, true);
90
        } catch (IOException ioe) {
91
            fail("Can't read test data to run the test: " + testfile);
92
        }
93

    
94
        try {
95
            System.err.println("Test Metacat: " + metacatUrl);
96
            m = MetacatFactory.createMetacatConnection(metacatUrl);
97
        } catch (MetacatInaccessibleException mie) {
98
            System.err.println("Metacat is: " + metacatUrl);
99
            fail("Metacat connection failed." + mie.getMessage());
100
        }
101
    }
102

    
103
    /**
104
     * Release any objects after tests are complete
105
     */
106
    public void tearDown()
107
    {
108
    }
109

    
110
    /**
111
     * Create a suite of tests to be run together
112
     */
113
    public static Test suite()
114
    {
115
      TestSuite suite = new TestSuite();
116
        suite.addTest(new MetacatClientTest("initialize"));
117
        suite.addTest(new MetacatClientTest("invalidLogin"));
118
        suite.addTest(new MetacatClientTest("logoutAndInvalidInsert"));
119
        suite.addTest(new MetacatClientTest("login"));
120
        suite.addTest(new MetacatClientTest("insert"));
121
        suite.addTest(new MetacatClientTest("upload"));
122
        suite.addTest(new MetacatClientTest("invalidRead"));
123
        suite.addTest(new MetacatClientTest("read"));
124
        suite.addTest(new MetacatClientTest("query"));
125
        suite.addTest(new MetacatClientTest("invalidUpdate"));
126
        suite.addTest(new MetacatClientTest("update"));
127
        suite.addTest(new MetacatClientTest("invalidDelete"));
128
        suite.addTest(new MetacatClientTest("delete"));
129
        suite.addTest(new MetacatClientTest("inaccessibleMetacat"));
130
        suite.addTest(new MetacatClientTest("reuseSession"));
131
        suite.addTest(new MetacatClientTest("reuseInvalidSession"));
132
        return suite;
133
    }
134

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

    
144
    /**
145
     * Test the login() function with valid credentials
146
     */
147
    public void login()
148
    {
149
        // Try a valid login
150
        try {
151
            String response = m.login(username, password);
152
            System.err.println("Login response: " + response);
153
            assertTrue(response != null);
154
            assertTrue(response.indexOf("<login>") != -1);
155
            String sessionId = m.getSessionId();
156
            System.err.println("Session ID: " + m.getSessionId());
157
            assertTrue(sessionId != null);
158
            assertTrue(response.indexOf(m.getSessionId()) != -1);
159
        } catch (MetacatAuthException mae) {
160
            fail("Authorization failed:\n" + mae.getMessage());
161
        } catch (MetacatInaccessibleException mie) {
162
            fail("Metacat Inaccessible:\n" + mie.getMessage());
163
        }
164
    }
165

    
166
    /**
167
     * Test the login() function with INVALID credentials
168
     */
169
    public void invalidLogin()
170
    {
171
        // Try an invalid login
172
        try {
173
            m.login(username, failpass);
174
            fail("Authorization should have failed.");
175
        } catch (MetacatAuthException mae) {
176
            assertTrue(1 == 1);
177
        } catch (MetacatInaccessibleException mie) {
178
            fail("Metacat Inaccessible:\n" + mie.getMessage());
179
        }
180
    }
181

    
182
    /**
183
     * Test the logout() function. When logout, user will be public, it couldn't
184
     * insert a document.
185
     */
186
    public void logoutAndInvalidInsert()
187
    {
188
       try {
189
            String identifier = newdocid + ".1";
190
            m.login(username, password);
191
            m.logout();
192
            String response = m.insert(identifier,
193
                    new StringReader(testdocument), null);
194
            System.err.println("Response in logout: "+response);
195
            assertTrue(response.indexOf("<success>") == -1);
196
        } catch (MetacatAuthException mae) {
197
            fail("Authorization failed:\n" + mae.getMessage());
198
        } catch (MetacatInaccessibleException mie) {
199
            fail("Metacat Inaccessible:\n" + mie.getMessage());
200
        } catch (InsufficientKarmaException ike) {
201
            fail("Insufficient karma:\n" + ike.getMessage());
202
        } catch (MetacatException me) {
203
            fail("Metacat Error:\n" + me.getMessage());
204
        } catch (Exception e) {
205
            fail("General exception:\n" + e.getMessage());
206
        }
207
    }
208

    
209
    /**
210
     * Test the read() function with a known document
211
     */
212
    public void read()
213
    {
214
        try {
215
            m.login(username, password);
216
            Reader r = m.read(newdocid+".1");
217
            String doc = IOUtil.getAsString(r, true);
218
            doc = doc +"\n";
219
            System.err.println(doc);
220
            assertTrue(doc.equals(testdocument));
221
        } catch (MetacatAuthException mae) {
222
            fail("Authorization failed:\n" + mae.getMessage());
223
        } catch (MetacatInaccessibleException mie) {
224
            fail("Metacat Inaccessible:\n" + mie.getMessage());
225
        } catch (Exception e) {
226
            fail("General exception:\n" + e.getMessage());
227
        }
228
    }
229

    
230
    /**
231
     * A user try to read a document which it doesn't have read permission
232
     */
233
    public void invalidRead()
234
    {
235
        try {
236
            m.login(anotheruser, anotherpassword);
237
            Reader r = m.read(newdocid+".1");
238
            String doc = IOUtil.getAsString(r, true);
239
            assertTrue(doc.indexOf("<error>") != -1);
240
            System.err.println(doc);
241
        } catch (MetacatAuthException mae) {
242
            fail("Authorization failed:\n" + mae.getMessage());
243
        } catch (MetacatInaccessibleException mie) {
244
            fail("Metacat Inaccessible:\n" + mie.getMessage());
245
        } catch (InsufficientKarmaException ike) {
246
            assertTrue(1 == 1);
247
        } catch (MetacatException e) {
248
            fail("Metacat exception:\n" + e.getMessage());
249
        } catch (IOException ioe) {
250
            fail("IO exception:\n" + ioe.getMessage());
251
        }
252
    }
253

    
254
    /**
255
     * Test the query() function with a known document
256
     */
257
    public void query()
258
    {
259
        try {
260
            m.login(username,password);
261
            FileReader fr = new FileReader(queryFile);
262
            Reader r = m.query(fr);
263
            System.err.println("Starting query...");
264
            String result = IOUtil.getAsString(r, true);
265
            System.err.println("Query result:\n" + result);
266
            assertTrue(result.indexOf(newdocid+".1")!=-1);
267
        } catch (MetacatAuthException mae) {
268
            fail("Authorization failed:\n" + mae.getMessage());
269
        } catch (MetacatInaccessibleException mie) {
270
            fail("Metacat Inaccessible:\n" + mie.getMessage());
271
        } catch (Exception e) {
272
            fail("General exception:\n" + e.getMessage());
273
        }
274
    }
275

    
276
    /**
277
     * Test the insert() function with a known document
278
     */
279
    public void insert()
280
    {
281
        try {
282
            String identifier = newdocid + ".1";
283
            m.login(username, password);
284
            String response = m.insert(identifier,
285
                    new StringReader(testdocument), null);
286
            assertTrue(response.indexOf("<success>") != -1);
287
            assertTrue(response.indexOf(identifier) != -1);
288
            System.err.println(response);
289

    
290
        } catch (MetacatAuthException mae) {
291
            fail("Authorization failed:\n" + mae.getMessage());
292
        } catch (MetacatInaccessibleException mie) {
293
            fail("Metacat Inaccessible:\n" + mie.getMessage());
294
        } catch (InsufficientKarmaException ike) {
295
            assertTrue(1 == 1);
296
            fail("Insufficient karma:\n" + ike.getMessage());
297
        } catch (MetacatException me) {
298
            fail("Metacat Error:\n" + me.getMessage());
299
        } catch (Exception e) {
300
            fail("General exception:\n" + e.getMessage());
301
        }
302
    }
303

    
304
    /**
305
     * Test the upload() function with a known document
306
     */
307
    public void upload()
308
    {
309
        try {
310
            newdocid = generateDocid();
311
            String identifier = newdocid + ".1";
312
            m.login(username, password);
313
            String response = m.upload(identifier,
314
                                     new File(onlinetestdatafile));
315
            assertTrue(response.indexOf("<success>") != -1);
316
            assertTrue(response.indexOf(identifier) != -1);
317
            System.err.println(response);
318

    
319
        } catch (MetacatAuthException mae) {
320
            fail("Authorization failed:\n" + mae.getMessage());
321
        } catch (MetacatInaccessibleException mie) {
322
          mie.printStackTrace();
323
            fail("Metacat Inaccessible:\n" + mie.getMessage());
324
        } catch (InsufficientKarmaException ike) {
325
            assertTrue(1 == 1);
326
            fail("Insufficient karma:\n" + ike.getMessage());
327
        } catch (MetacatException me) {
328
            fail("Metacat Error:\n" + me.getMessage());
329
        } catch (Exception e) {
330
            fail("General exception:\n" + e.getMessage());
331
        }
332
    }
333

    
334
    /**
335
     * Test the invalidUpdate() function. A user try to update a document
336
     * which it doesn't have permission
337
     */
338
    public void invalidUpdate()
339
    {
340
        try {
341
            String identifier = newdocid + ".2";
342
            m.login(anotheruser, anotherpassword);
343
            String response = m.update(identifier,
344
                    new StringReader(testdocument), null);
345
            assertTrue(response.indexOf("<success>") == -1);
346

    
347
        } catch (MetacatAuthException mae) {
348
            fail("Authorization failed:\n" + mae.getMessage());
349
        } catch (MetacatInaccessibleException mie) {
350
            fail("Metacat Inaccessible:\n" + mie.getMessage());
351
        } catch (InsufficientKarmaException ike) {
352
            assertTrue(1 == 1);
353
        } catch (MetacatException me) {
354
            fail("Metacat Error:\n" + me.getMessage());
355
        } catch (Exception e) {
356
            fail("General exception:\n" + e.getMessage());
357
        }
358
    }
359

    
360
    /**
361
     * Test the update() function with a known document
362
     */
363
    public void update()
364
    {
365
        try {
366
            String identifier = newdocid + ".2";
367
            m.login(username, password);
368
            String response = m.update(identifier,
369
                    new StringReader(testdocument), null);
370
            assertTrue(response.indexOf("<success>") != -1);
371
            assertTrue(response.indexOf(identifier) != -1);
372
            System.err.println(response);
373

    
374
        } catch (MetacatAuthException mae) {
375
            fail("Authorization failed:\n" + mae.getMessage());
376
        } catch (MetacatInaccessibleException mie) {
377
            fail("Metacat Inaccessible:\n" + mie.getMessage());
378
        } catch (InsufficientKarmaException ike) {
379
            fail("Insufficient karma:\n" + ike.getMessage());
380
        } catch (MetacatException me) {
381
            fail("Metacat Error:\n" + me.getMessage());
382
        } catch (Exception e) {
383
            fail("General exception:\n" + e.getMessage());
384
        }
385
    }
386

    
387
    /**
388
     * A user try delete a document which it doesn't have permission
389
     */
390
    public void invalidDelete()
391
    {
392
        try {
393
            String identifier = newdocid + ".2";
394
            m.login(anotheruser, anotherpassword);
395
            String response = m.delete(identifier);
396
            assertTrue(response.indexOf("<success>") == -1);
397
            System.err.println(response);
398

    
399
        } catch (MetacatAuthException mae) {
400
            fail("Authorization failed:\n" + mae.getMessage());
401
        } catch (MetacatInaccessibleException mie) {
402
            fail("Metacat Inaccessible:\n" + mie.getMessage());
403
        } catch (InsufficientKarmaException ike) {
404
            assertTrue(1 == 1);
405
        } catch (MetacatException me) {
406
            assertTrue(1 == 1);
407
        } catch (Exception e) {
408
            fail("General exception:\n" + e.getMessage());
409
        }
410
    }
411

    
412
    /**
413
     * Test the delete() function with a known document
414
     */
415
    public void delete()
416
    {
417
        try {
418
            String identifier = newdocid + ".2";
419
            m.login(username, password);
420
            String response = m.delete(identifier);
421
            assertTrue(response.indexOf("<success>") != -1);
422
            System.err.println(response);
423

    
424
        } catch (MetacatAuthException mae) {
425
            fail("Authorization failed:\n" + mae.getMessage());
426
        } catch (MetacatInaccessibleException mie) {
427
            fail("Metacat Inaccessible:\n" + mie.getMessage());
428
        } catch (InsufficientKarmaException ike) {
429
            fail("Insufficient karma:\n" + ike.getMessage());
430
        } catch (MetacatException me) {
431
            fail("Metacat Error:\n" + me.getMessage());
432
        } catch (Exception e) {
433
            fail("General exception:\n" + e.getMessage());
434
        }
435
    }
436

    
437
    /**
438
     * Test the to connect a wrong metacat url. Create a new metacat with a
439
     * inaccessible url. Then try to login it. If get a MetacatInaccessible
440
     * exception, this is right.
441
     */
442
    public void inaccessibleMetacat()
443
    {
444
        Metacat mWrong = null;
445
        try {
446
            mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
447
        } catch (MetacatInaccessibleException mie) {
448
            fail("Metacat Inaccessible:\n" + mie.getMessage());
449
        }
450

    
451
        try {
452
            mWrong.login(username, password);
453
        } catch (MetacatInaccessibleException mie) {
454
            assertTrue(1 == 1);
455
        } catch (MetacatAuthException mae) {
456
            fail("Authorization failed:\n" + mae.getMessage());
457
        }
458
    }
459

    
460
    /**
461
     * Try to perform an action that requires a session to be valid without
462
     * having logged in first by setting the sessionId from a previous
463
     * session.  Then insert a document.
464
     */
465
    public void reuseSession()
466
    {
467
        String oldSessionId = "";
468
        try {
469
            Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
470
            String response = mtemp.login(username, password);
471
            oldSessionId = mtemp.getSessionId();
472
            System.err.println("SessionId (mtemp): " + oldSessionId);
473
        } catch (MetacatAuthException mae) {
474
            fail("Authorization failed:\n" + mae.getMessage());
475
        } catch (MetacatInaccessibleException mie) {
476
            System.err.println("Metacat is: " + metacatUrl);
477
            fail("Metacat connection failed." + mie.getMessage());
478
        }
479

    
480
        try {
481
            String identifier = generateDocid() + ".1";
482
            Metacat m2 = null;
483
            try {
484
                m2 = MetacatFactory.createMetacatConnection(metacatUrl);
485
            } catch (MetacatInaccessibleException mie) {
486
                System.err.println("Metacat is: " + metacatUrl);
487
                fail("Metacat connection failed." + mie.getMessage());
488
            }
489
            System.err.println("SessionId (m2): " + m2.getSessionId());
490
            m2.setSessionId(oldSessionId);
491
            System.err.println("SessionId (m2 after set): " + m2.getSessionId());
492
            String response = m2.insert(identifier,
493
                    new StringReader(testdocument), null);
494
            System.err.println("Reuse Insert response: " + response);
495
            assertTrue(response.indexOf("<success>") != -1);
496
        } catch (MetacatInaccessibleException mie) {
497
            fail("Metacat Inaccessible:\n" + mie.getMessage());
498
        } catch (InsufficientKarmaException ike) {
499
            fail("Insufficient karma:\n" + ike.getMessage());
500
        } catch (MetacatException me) {
501
            fail("Metacat Error:\n" + me.getMessage());
502
        } catch (Exception e) {
503
            fail("General exception:\n" + e.getMessage());
504
        }
505
    }
506

    
507
    /**
508
     * Try to perform an action that requires a session to be valid without
509
     * having logged in first, but use an invalid session identifier.
510
     * Then insert a document, which should fail.
511
     */
512
    public void reuseInvalidSession()
513
    {
514
        System.err.println("Starting resuseInvalidSession test...");
515
        String oldSessionId = "foobar";
516
        try {
517
            String identifier = generateDocid() + ".1";
518
            Metacat m3 = null;
519
            try {
520
                m3 = MetacatFactory.createMetacatConnection(metacatUrl);
521
            } catch (MetacatInaccessibleException mie) {
522
                System.err.println("Metacat is: " + metacatUrl);
523
                fail("Metacat connection failed." + mie.getMessage());
524
            }
525
            System.err.println("SessionId (m3): " + m3.getSessionId());
526
            m3.setSessionId(oldSessionId);
527
            System.err.println("SessionId (m3 after set): " + m3.getSessionId());
528
            System.err.println("Performing resuseInvalidSession insert: " +
529
                    identifier);
530
            String response = m3.insert(identifier,
531
                    new StringReader(testdocument), null);
532
            System.err.println("ReuseInvalid Insert response: " + response);
533
            assertTrue(response.indexOf("<success>") == -1);
534
        } catch (MetacatInaccessibleException mie) {
535
            fail("Metacat Inaccessible:\n" + mie.getMessage());
536
        } catch (InsufficientKarmaException ike) {
537
            fail("Insufficient karma:\n" + ike.getMessage());
538
        } catch (MetacatException me) {
539
            fail("Metacat Error:\n" + me.getMessage());
540
        } catch (Exception e) {
541
            fail("General exception:\n" + e.getMessage());
542
        }
543
    }
544
    /**
545
     * Create a hopefully unique docid for testing insert and update. Does
546
     * not include the 'revision' part of the id.
547
     *
548
     * @return a String docid based on the current date and time
549
     */
550
    private String generateDocid()
551
    {
552
        StringBuffer docid = new StringBuffer(prefix);
553
        docid.append(".");
554

    
555
        // Create a calendar to get the date formatted properly
556
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
557
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
558
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
559
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
560
        Calendar calendar = new GregorianCalendar(pdt);
561
        Date trialTime = new Date();
562
        calendar.setTime(trialTime);
563
        docid.append(calendar.get(Calendar.YEAR));
564
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
565
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
566
        docid.append(calendar.get(Calendar.MINUTE));
567
        docid.append(calendar.get(Calendar.SECOND));
568

    
569
        return docid.toString();
570
    }
571
}
    (1-1/1)