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: jones $'
8
 *     '$Date: 2003-09-19 17:01:59 -0700 (Fri, 19 Sep 2003) $'
9
 * '$Revision: 1826 $'
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 edu.ucsb.nceas.metacat.client.*;
29
import edu.ucsb.nceas.utilities.HttpMessage;
30
import edu.ucsb.nceas.utilities.IOUtil;
31

    
32
import java.io.FileReader;
33
import java.io.InputStreamReader;
34
import java.io.IOException;
35
import java.io.Reader;
36
import java.io.StringReader;
37
import java.io.StringWriter;
38
import java.text.DateFormat;
39
import java.util.Calendar;
40
import java.util.GregorianCalendar;
41
import java.util.Date;
42
import java.util.SimpleTimeZone;
43
import java.util.TimeZone;
44

    
45
import junit.framework.Test;
46
import junit.framework.TestCase;
47
import junit.framework.TestResult;
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 = 
56
         "http://dev.nceas.ucsb.edu/tao/servlet/metacat";
57
        //"http://knb.ecoinformatics.org/knb/servlet/metacat";
58
    private String wrongMetacatUrl=
59
                    "http://somepalce.somewhere.com/some/servlet/metacat";
60
    private String username = "@mcuser@";
61
    private String password = "@mcpassword@";
62
    private String anotheruser = "@mcanotheruser@";
63
    private String anotherpassword = "@mcanotherpassword@";
64
    private String failpass = "uidfnkj43987yfdn";
65
    private String prefix = "test";
66
    private String newdocid = null;
67
    private String testfile = "test/jones.204.22.xml";
68
    private String queryFile = "test/query.xml";
69
    private String testdocument = "";
70
    
71
    private Metacat m;
72

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

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

    
96
        try {
97
            m = MetacatFactory.createMetacatConnection(metacatUrl);
98
        } catch (MetacatInaccessibleException mie) {
99
            System.err.println("Metacat is: " + metacatUrl);
100
            fail("Metacat connection failed." + mie.getMessage());
101
        }
102
    }
103
  
104
    /**
105
     * Release any objects after tests are complete
106
     */
107
    public void tearDown()
108
    {
109
    }
110
  
111
    /**
112
     * Create a suite of tests to be run together
113
     */
114
    public static Test suite()
115
    {
116
        TestSuite suite = new TestSuite();
117
        suite.addTest(new MetacatClientTest("initialize"));
118
        suite.addTest(new MetacatClientTest("invalidLogin"));
119
        suite.addTest(new MetacatClientTest("logoutAndInvalidInsert"));
120
        suite.addTest(new MetacatClientTest("login"));
121
        suite.addTest(new MetacatClientTest("insert"));
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
        return suite;
132
    }
133
  
134
    /**
135
     * Run an initial test that always passes to check that the test
136
     * harness is working.
137
     */
138
    public void initialize()
139
    {
140
        assertTrue(1 == 1);
141
    }
142
  
143
    /**
144
     * Test the login() function with valid credentials
145
     */
146
    public void login()
147
    {
148
        // Try a valid login
149
        try {
150
            String response = m.login(username, password);
151
            System.err.println("Login response: " + response);
152
            assertTrue(response != null);
153
            assertTrue(response.indexOf("<login>") != -1);
154
            String sessionId = m.getSessionId();
155
            System.err.println("Session ID: " + m.getSessionId());
156
            assertTrue(sessionId != null);
157
            assertTrue(response.indexOf(m.getSessionId()) != -1);
158
        } catch (MetacatAuthException mae) {
159
            fail("Authorization failed:\n" + mae.getMessage());
160
        } catch (MetacatInaccessibleException mie) {
161
            fail("Metacat Inaccessible:\n" + mie.getMessage());
162
        }
163
    }
164

    
165
    /**
166
     * Test the login() function with INVALID credentials
167
     */
168
    public void invalidLogin()
169
    {
170
        // Try an invalid login
171
        try {
172
            m.login(username, failpass);
173
            fail("Authorization should have failed.");
174
        } catch (MetacatAuthException mae) {
175
            assertTrue(1 == 1);
176
        } catch (MetacatInaccessibleException mie) {
177
            fail("Metacat Inaccessible:\n" + mie.getMessage());
178
        }
179
    }
180
    
181
    /**
182
     * Test the logout() function. When logout, user will be public, it couldn't
183
     * insert a document.
184
     */
185
    public void logoutAndInvalidInsert()
186
    {
187
       try {
188
            String identifier = newdocid + ".1";
189
            m.login(username, password);
190
            m.logout();
191
            String response = m.insert(identifier, 
192
                    new StringReader(testdocument), null);
193
            System.err.println("Response in logout: "+response);
194
            assertTrue(response.indexOf("<success>") == -1);
195
        } catch (MetacatAuthException mae) {
196
            fail("Authorization failed:\n" + mae.getMessage());
197
        } catch (MetacatInaccessibleException mie) {
198
            fail("Metacat Inaccessible:\n" + mie.getMessage());
199
        } catch (InsufficientKarmaException ike) {
200
            fail("Insufficient karma:\n" + ike.getMessage());
201
        } catch (MetacatException me) {
202
            fail("Metacat Error:\n" + me.getMessage());
203
        } catch (Exception e) {
204
            fail("General exception:\n" + e.getMessage());
205
        }
206
    }
207

    
208
    /**
209
     * Test the read() function with a known document
210
     */
211
    public void read()
212
    {
213
        try {
214
            m.login(username, password);
215
            Reader r = m.read(newdocid+".1");
216
            String doc = IOUtil.getAsString(r, true);
217
            doc = doc +"\n";
218
            System.err.println(doc);
219
            assertTrue(doc.equals(testdocument));
220
        } catch (MetacatAuthException mae) {
221
            fail("Authorization failed:\n" + mae.getMessage());
222
        } catch (MetacatInaccessibleException mie) {
223
            fail("Metacat Inaccessible:\n" + mie.getMessage());
224
        } catch (Exception e) {
225
            fail("General exception:\n" + e.getMessage());
226
        }
227
    }
228
    
229
    /**
230
     * A user try to read a document which it doesn't have read permission
231
     */
232
    public void invalidRead()
233
    {
234
        try {
235
            m.login(anotheruser, anotherpassword);
236
            Reader r = m.read(newdocid+".1");
237
            String doc = IOUtil.getAsString(r, true);
238
            assertTrue(doc.indexOf("<error>") != -1);
239
            System.err.println(doc);
240
        } catch (MetacatAuthException mae) {
241
            fail("Authorization failed:\n" + mae.getMessage());
242
        } catch (MetacatInaccessibleException mie) {
243
            fail("Metacat Inaccessible:\n" + mie.getMessage());
244
        } catch (InsufficientKarmaException ike) {
245
            assertTrue(1 == 1);
246
        } catch (MetacatException e) {
247
            fail("Metacat exception:\n" + e.getMessage());
248
        } catch (IOException ioe) {
249
            fail("IO exception:\n" + ioe.getMessage());
250
        }
251
    }
252
    
253
    /**
254
     * Test the query() function with a known document
255
     */
256
    public void query()
257
    {
258
        try {
259
            FileReader fr = new FileReader(queryFile);
260
            Reader r = m.query(fr);
261
            String result = IOUtil.getAsString(r, true);
262
            assertTrue(result.indexOf(newdocid+".1")!=-1);
263
        } catch (MetacatInaccessibleException mie) {
264
            fail("Metacat Inaccessible:\n" + mie.getMessage());
265
        } catch (Exception e) {
266
            fail("General exception:\n" + e.getMessage());
267
        }
268
    }
269

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

    
284
        } catch (MetacatAuthException mae) {
285
            fail("Authorization failed:\n" + mae.getMessage());
286
        } catch (MetacatInaccessibleException mie) {
287
            fail("Metacat Inaccessible:\n" + mie.getMessage());
288
        } catch (InsufficientKarmaException ike) {
289
            assertTrue(1 == 1);
290
            fail("Insufficient karma:\n" + ike.getMessage());
291
        } catch (MetacatException me) {
292
            fail("Metacat Error:\n" + me.getMessage());
293
        } catch (Exception e) {
294
            fail("General exception:\n" + e.getMessage());
295
        }
296
    }
297

    
298
    /**
299
     * Test the invalidUpdate() function. A user try to update a document
300
     * which it doesn't have permission
301
     */
302
    public void invalidUpdate()
303
    {
304
        try {
305
            String identifier = newdocid + ".2";
306
            m.login(anotheruser, anotherpassword);
307
            String response = m.update(identifier, 
308
                    new StringReader(testdocument), null);
309
            assertTrue(response.indexOf("<success>") == -1);
310
          
311
        } catch (MetacatAuthException mae) {
312
            fail("Authorization failed:\n" + mae.getMessage());
313
        } catch (MetacatInaccessibleException mie) {
314
            fail("Metacat Inaccessible:\n" + mie.getMessage());
315
        } catch (InsufficientKarmaException ike) {
316
            assertTrue(1 == 1);
317
        } catch (MetacatException me) {
318
            fail("Metacat Error:\n" + me.getMessage());
319
        } catch (Exception e) {
320
            fail("General exception:\n" + e.getMessage());
321
        }
322
    }
323
    
324
    /**
325
     * Test the update() function with a known document
326
     */
327
    public void update()
328
    {
329
        try {
330
            String identifier = newdocid + ".2";
331
            m.login(username, password);
332
            String response = m.update(identifier, 
333
                    new StringReader(testdocument), null);
334
            assertTrue(response.indexOf("<success>") != -1);
335
            assertTrue(response.indexOf(identifier) != -1);
336
            System.err.println(response);
337

    
338
        } catch (MetacatAuthException mae) {
339
            fail("Authorization failed:\n" + mae.getMessage());
340
        } catch (MetacatInaccessibleException mie) {
341
            fail("Metacat Inaccessible:\n" + mie.getMessage());
342
        } catch (InsufficientKarmaException ike) {
343
            fail("Insufficient karma:\n" + ike.getMessage());
344
        } catch (MetacatException me) {
345
            fail("Metacat Error:\n" + me.getMessage());
346
        } catch (Exception e) {
347
            fail("General exception:\n" + e.getMessage());
348
        }
349
    }
350
    
351
    /**
352
     * A user try delete a document which it doesn't have permission
353
     */
354
    public void invalidDelete()
355
    {
356
        try {
357
            String identifier = newdocid + ".2";
358
            m.login(anotheruser, anotherpassword);
359
            String response = m.delete(identifier);
360
            assertTrue(response.indexOf("<success>") == -1);
361
            System.err.println(response);
362

    
363
        } catch (MetacatAuthException mae) {
364
            fail("Authorization failed:\n" + mae.getMessage());
365
        } catch (MetacatInaccessibleException mie) {
366
            fail("Metacat Inaccessible:\n" + mie.getMessage());
367
        } catch (InsufficientKarmaException ike) {
368
            assertTrue(1 == 1);
369
        } catch (MetacatException me) {
370
            assertTrue(1 == 1);
371
        } catch (Exception e) {
372
            fail("General exception:\n" + e.getMessage());
373
        }
374
    }
375
    
376
    /**
377
     * Test the delete() function with a known document
378
     */
379
    public void delete()
380
    {
381
        try {
382
            String identifier = newdocid + ".2";
383
            m.login(username, password);
384
            String response = m.delete(identifier);
385
            assertTrue(response.indexOf("<success>") != -1);
386
            System.err.println(response);
387

    
388
        } catch (MetacatAuthException mae) {
389
            fail("Authorization failed:\n" + mae.getMessage());
390
        } catch (MetacatInaccessibleException mie) {
391
            fail("Metacat Inaccessible:\n" + mie.getMessage());
392
        } catch (InsufficientKarmaException ike) {
393
            fail("Insufficient karma:\n" + ike.getMessage());
394
        } catch (MetacatException me) {
395
            fail("Metacat Error:\n" + me.getMessage());
396
        } catch (Exception e) {
397
            fail("General exception:\n" + e.getMessage());
398
        }
399
    }
400
    
401
    /**
402
     * Test the to connect a wrong metacat url. Create a new metacat with a
403
     * inaccessible url. Then try to login it. If get a MetacatInaccessible
404
     * exception, this is right.
405
     */
406
    public void inaccessibleMetacat()
407
    {
408
        Metacat mWrong = null;
409
        try {
410
            mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
411
        } catch (MetacatInaccessibleException mie) {
412
            fail("Metacat Inaccessible:\n" + mie.getMessage());
413
        }
414
        
415
        try {
416
            mWrong.login(username, password);
417
        } catch (MetacatInaccessibleException mie) {
418
            assertTrue(1 == 1);
419
        } catch (MetacatAuthException mae) {
420
            fail("Authorization failed:\n" + mae.getMessage());
421
        }
422
    }
423

    
424
    /**
425
     * Try to perform an action that requires a session to be valid without
426
     * having logged in first by setting the sessionId from a previous
427
     * session.  Then insert a document.
428
     */
429
    public void reuseSession()
430
    {
431
        String oldSessionId = "";
432
        try {
433
            Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
434
            String response = mtemp.login(username, password);
435
            oldSessionId = mtemp.getSessionId();
436
        } catch (MetacatAuthException mae) {
437
            fail("Authorization failed:\n" + mae.getMessage());
438
        } catch (MetacatInaccessibleException mie) {
439
            System.err.println("Metacat is: " + metacatUrl);
440
            fail("Metacat connection failed." + mie.getMessage());
441
        }
442

    
443
        try {
444
            String identifier = generateDocid() + ".1";
445
            Metacat m2 = null;
446
            try {
447
                m2 = MetacatFactory.createMetacatConnection(metacatUrl);
448
            } catch (MetacatInaccessibleException mie) {
449
                System.err.println("Metacat is: " + metacatUrl);
450
                fail("Metacat connection failed." + mie.getMessage());
451
            }
452
            m2.setSessionId(oldSessionId);
453
            String response = m2.insert(identifier, 
454
                    new StringReader(testdocument), null);
455
            System.err.println("Reuse Insert response: " + response);
456
            assertTrue(response.indexOf("<success>") != -1);
457
        } catch (MetacatInaccessibleException mie) {
458
            fail("Metacat Inaccessible:\n" + mie.getMessage());
459
        } catch (InsufficientKarmaException ike) {
460
            fail("Insufficient karma:\n" + ike.getMessage());
461
        } catch (MetacatException me) {
462
            fail("Metacat Error:\n" + me.getMessage());
463
        } catch (Exception e) {
464
            fail("General exception:\n" + e.getMessage());
465
        }
466
    }
467

    
468
    /**
469
     * Create a hopefully unique docid for testing insert and update. Does
470
     * not include the 'revision' part of the id.
471
     *
472
     * @return a String docid based on the current date and time
473
     */
474
    private String generateDocid()
475
    {
476
        StringBuffer docid = new StringBuffer(prefix);
477
        docid.append(".");
478
        
479
        // Create a calendar to get the date formatted properly
480
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
481
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
482
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
483
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
484
        Calendar calendar = new GregorianCalendar(pdt);
485
        Date trialTime = new Date();
486
        calendar.setTime(trialTime);
487
        docid.append(calendar.get(Calendar.YEAR));
488
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
489
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
490
        docid.append(calendar.get(Calendar.MINUTE));
491
        docid.append(calendar.get(Calendar.SECOND));
492

    
493
        return docid.toString();
494
    }
495
}
    (1-1/1)