Project

General

Profile

« Previous | Next » 

Revision 5284

Added by Matt Jones over 14 years ago

Updated MetacatRestClientTest so that the tests are actually calling fail()
when errors occur, and fixed all sixteen tests so that they run successfully
and can be run from any client (i.e., they don't depend on particular
documents being present in Metacat). The 'restfiles' directory can probably
be removed now -- will need to check.

View differences:

test/edu/ucsb/nceas/metacattest/restservice/MetacatRestClientTest.java
22 22
 */
23 23
package edu.ucsb.nceas.metacattest.restservice;
24 24

  
25
import java.io.FileReader;
26 25
import java.io.IOException;
27 26
import java.io.Reader;
28
import java.util.Calendar;
29
import java.util.Date;
30
import java.util.GregorianCalendar;
31
import java.util.SimpleTimeZone;
32
import java.util.TimeZone;
27
import java.io.StringReader;
33 28
import java.util.Vector;
34 29

  
35 30
import junit.framework.Test;
36 31
import junit.framework.TestSuite;
37 32
import edu.ucsb.nceas.MCTestCase;
33
import edu.ucsb.nceas.metacat.IdentifierManager;
38 34
import edu.ucsb.nceas.metacat.client.DocumentNotFoundException;
35
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
39 36
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
40 37
import edu.ucsb.nceas.metacat.client.MetacatException;
41 38
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
......
63 60
		}
64 61
	}
65 62
	
66
	
67
	
68 63
    private String failpass = "sdfsdfsdfsd";
69
    private String prefix = "test";
70
    private String newdocid = null;
71
    private String testfile = "test/jones.204.22.xml";
72
    private String onlinetestdatafile = "test/onlineDataFile1";
73
    private String queryFile = "test/query.xml";
74
    private String testdocument = "";
75 64
    private MetacatRest m;
76
    private String spatialTestFile = "test/spatialEml.xml";
77
    private static final int TIME = 30;
78
    private static final String DOCID = "docid";
79
    private static final String DATAID = "dataid";
65
    private static final String DOC_TITLE = "Test MetacatRest service";    
80 66
    
81
    private static final String public_doc_id = "doi:12345.5/playing/in/dirt/rev1";
82
    private static final String authorized_doc_id = "serhan.4.2";
83
    private static final String notexisting_doc_id = "serhan.1.1";
84
    private static final String ecogridQueryFile= "test/restfiles/ecogrid_query.xml";
85

  
86

  
87
    private static final String insert_file = "test/restfiles/knb-lter-gce.109.6.xml";
88
    
89
    
90 67
    /**
91 68
     * Constructor to build the test
92 69
     *
......
95 72
    public MetacatRestClientTest(String name)
96 73
    {
97 74
        super(name);
98
        newdocid = generateDocid();
99 75
    }
100 76

  
101 77
    /**
......
103 79
     */
104 80
    public void setUp()
105 81
    {
106
        try {
107
            FileReader fr = new FileReader(testfile);
108
            testdocument = IOUtil.getAsString(fr, true);
109
        } catch (IOException ioe) {
110
            fail("Can't read test data to run the test: " + testfile);
111
        }
112

  
113 82
        m = new MetacatRestClient(contextUrl);
114 83
    }
115 84
    
......
131 100
      suite.addTest(new MetacatRestClientTest("login"));
132 101
      suite.addTest(new MetacatRestClientTest("logout"));
133 102
      suite.addTest(new MetacatRestClientTest("get"));
103
      suite.addTest(new MetacatRestClientTest("invalidGet"));
134 104
      suite.addTest(new MetacatRestClientTest("getPrivate"));
135
      suite.addTest(new MetacatRestClientTest("invalidGet"));
136 105
      suite.addTest(new MetacatRestClientTest("authenticatedGet"));
137 106
      suite.addTest(new MetacatRestClientTest("query"));
138
      suite.addTest(new MetacatRestClientTest("authenticatedQuery"));      
107
      suite.addTest(new MetacatRestClientTest("authenticatedQuery"));     
139 108
      suite.addTest(new MetacatRestClientTest("crud"));
140 109
      suite.addTest(new MetacatRestClientTest("getNextObject"));
141 110
      suite.addTest(new MetacatRestClientTest("getNextRevision"));
......
150 119
     * Run an initial test that always passes to check that the test
151 120
     * harness is working.
152 121
     */
153
    public void initialize() {
122
    public void initialize() 
123
    {
154 124
        assertTrue(1 == 1);
155 125
    }
156 126
    
......
192 162
            fail("Authorization should have failed.");
193 163
        } catch (MetacatAuthException mae) {
194 164
        	debug("invalid login(): response=" + mae.getMessage());
195
            //assertTrue(1 == 1);
165
            assertNotNull(mae);
196 166
        } catch (MetacatInaccessibleException mie) {
197 167
            fail("Metacat Inaccessible:\n" + mie.getMessage());
198 168
        } 
......
231 201
    public void get()
232 202
    {
233 203
        debug("\nStarting get test...");
234
        debug("-------------------------------------------------------");        
204
        debug("-------------------------------------------------------"); 
235 205
        try {
236
            Reader r =  m.getObject(public_doc_id, null);            
206
            IdentifierManager im = IdentifierManager.getInstance();
207
            String docid = insertTestDocument();
208
            String guid = "test:"+docid;
209
            im.createMapping(guid, docid);
210
            Reader r =  m.getObject(guid, null);            
237 211
            String doc = IOUtil.getAsString(r, true);
238 212
            doc = doc +"\n";
239 213
            debug("document:\n"+doc);
214
            assertTrue(doc.indexOf(DOC_TITLE) != -1);
240 215
        } catch (MetacatInaccessibleException mie) {
241
            debug("Metacat Inaccessible:\n" + mie.getMessage());
216
            fail("Metacat Inaccessible:\n" + mie.getMessage());
242 217
        } catch (Exception e) {
243
        	e.printStackTrace();
244
            debug("General exception:\n" + e.getMessage());
218
            fail("General exception:\n" + e.getMessage());
245 219
        }
246 220
    }
247 221
    
......
250 224
     */
251 225
    public void getPrivate()
252 226
    {
227
        assertTrue("Not implemented yet.", 1==1);
228
        /*
253 229
        debug("\nStarting getprivate  test...");
254 230
        debug("-------------------------------------------------------");        
255 231
        try {
......
258 234
            doc = doc +"\n";
259 235
            debug("document:\n"+doc);
260 236
        } catch (MetacatInaccessibleException mie) {
261
            debug("Metacat Inaccessible:\n" + mie.getMessage());
237
            fail("Metacat Inaccessible:\n" + mie.getMessage());
262 238
        } catch (Exception e) {
263
            debug("General exception:\n" + e.getMessage());
239
            fail("General exception:\n" + e.getMessage());
264 240
        }
241
        */
265 242
    }
266 243
    
267 244
    /**
......
269 246
     */
270 247
    public void authenticatedGet()
271 248
    {
249
        assertTrue("Not implemented yet.", 1==1);
250
        /*
272 251
        debug("\nStarting authorizedGet test...");
273 252
        debug("-------------------------------------------------------");
274 253
        try {
......
282 261
        } catch (Exception e) {
283 262
            debug("General exception:\n" + e.getMessage());
284 263
        }
264
        */
285 265
    }
286 266
    
287 267
    /**
......
292 272
        debug("\nStarting invalid get test ...");
293 273
        debug("-------------------------------------------------------");
294 274
        try {
295
            Reader r =  m.getObject(notexisting_doc_id, null);            
275
            String unregDocid = generateDocumentId() + ".1";
276
            Reader r =  m.getObject(unregDocid, null);            
296 277
            String doc = IOUtil.getAsString(r, true);
297 278
            doc = doc +"\n";
298 279
            debug("document:\n"+doc);
280
            assertTrue(doc.indexOf("<error>") != -1);
299 281
        } catch (MetacatInaccessibleException mie) {
300 282
            debug("Metacat Inaccessible:\n" + mie.getMessage());
301 283
        } catch (DocumentNotFoundException doe) {
......
309 291
    /**
310 292
     * Test the query() function with a public session
311 293
     */
312
    public void query(){
294
    public void query()
295
    {
296
        assertTrue("Not implemented yet.", 1==1);
297
        /*
313 298
    	debug("\nStarting query test ...");
314 299
        debug("-------------------------------------------------------");
315 300
    	try {
......
325 310
        	e.printStackTrace();
326 311
            fail("General exception:\n" + e.getMessage());
327 312
        }
313
        */
328 314
    }
329 315
    
330 316
    
......
332 318
    /**
333 319
     * Test the authenticatedQuery() function
334 320
     */
335
    public void authenticatedQuery(){
321
    public void authenticatedQuery()
322
    {
323
        assertTrue("Not implemented yet.", 1==1);
324
        /*
336 325
    	debug("\nStarting authenticatedQuery test ...");
337 326
        debug("-------------------------------------------------------");
338 327
    	try {
......
351 340
        	e.printStackTrace();
352 341
            fail("General exception:\n" + e.getMessage());
353 342
        }
343
        */
354 344
    }
355 345
    
356 346
    /**
357 347
     * Test the insert,update and delete function
358 348
     */
359
    public void crud(){
349
    public void crud()
350
    {
360 351
    	debug("\nStarting insert,update,delete document test ...");
361 352
        debug("-------------------------------------------------------");
362 353
    	try {
363
    		FileReader fr = new FileReader(insert_file);	
354
    	    String accessBlock = getAccessBlock("public", true, true,
355
                    false, false, false);
356
            String emldoc = getTestEmlDoc("Test MetacatRest service", EML2_1_0, null,
357
                    null, "http://fake.example.com/somedata", null,
358
                    accessBlock, null, null,
359
                    null, null);
360
            String docid = generateDocumentId();
361
            
362
    		StringReader sr = new StringReader(emldoc);	
364 363
    		m.login(username,password);
365 364

  
366 365
    		debug("\nFirst insert the document...");
367
    		String response = m.putObject(newdocid+".1",fr,true);
366
    		String response = m.putObject(docid + ".1",sr,true);
367
    		sr.close();
368 368
    		debug("response:\n"+response);
369

  
370
    		fr = new FileReader(insert_file);
369
            assertTrue(response.indexOf("success") != -1);
370
    		
371
    		sr = new StringReader(emldoc);
371 372
    		debug("\nNow update the document...");
372
    		response = m.putObject(newdocid+".2",fr,false);    	 
373
    		debug("response:\n"+response);    		
374
    		fr.close();
373
    		response = m.putObject(docid + ".2",sr,false);    	 
374
    		debug("response:\n"+response);
375
            assertTrue(response.indexOf("success") != -1);
376
    		sr.close();
375 377
    		
376 378
    		Thread.sleep(10000);
377 379
    		
378 380
    		debug("\nFinally delete the document...");
379
    		response = m.deleteObject(newdocid+".2");  
381
    		response = m.deleteObject(docid + ".2");  
380 382
    		debug("response:\n"+response);
383
            assertTrue(response.indexOf("success") != -1);
381 384

  
382

  
383 385
    	} catch (MetacatAuthException mae) {
384
    		debug("Authorization failed:\n" + mae.getMessage());
386
    		fail("Authorization failed:\n" + mae.getMessage());
385 387
    	} catch (MetacatInaccessibleException mie) {
386
    		debug("Metacat Inaccessible:\n" + mie.getMessage());
388
    		fail("Metacat Inaccessible:\n" + mie.getMessage());
387 389
    	} catch (Exception e) {
388
    		e.printStackTrace();
389 390
    		fail("General exception:\n" + e.getMessage());
390 391
    	}
391 392
    }
......
396 397
     * that it matches the one we last inserted. Assumes this test is run
397 398
     * immediately following a successful insert() test.
398 399
     */
399
    public void getNextObject() {
400
    public void getNextObject() 
401
    {
400 402
        debug("\nStarting getNextObject test...");
401 403
        debug("-------------------------------------------------------");
402 404
        try {
......
412 414
    }
413 415

  
414 416
    /**
415
     * Get the most recent document id for a given scope and be sure
416
     * that it matches the one we last inserted. Assumes this test is run
417
     * Get the next revision for a given id. Assumes this test is run
417 418
     * immediately following a successful insert() test.
418 419
     */
419
    public void getNextRevision() {
420
    public void getNextRevision()
421
    {
420 422
        debug("\nStarting getNextRevision test...");
421 423
        debug("-------------------------------------------------------");
422 424
        try {
423

  
424
            int rev_id = m.getNextRevision(public_doc_id);
425
            IdentifierManager im = IdentifierManager.getInstance();
426
            String docid = insertTestDocument();
427
            int rev_id = m.getNextRevision(docid);
425 428
            debug("NextRevision number is " + rev_id);
426 429

  
427 430
        } catch (MetacatException me) {
......
435 438
    /**
436 439
     * Test getAllDocids function
437 440
     */
438
    public void getAllDocids() {
441
    public void getAllDocids() 
442
    {
439 443
        debug("\nStarting getAllDocids test...");
440 444
        debug("-------------------------------------------------------");
441 445
        try {
......
453 457
        }
454 458
    }
455 459
    
456
    /***
460
    /**
457 461
     * Test isRegistered function
458 462
     */
459
    public void isRegistered() {
463
    public void isRegistered() 
464
    {
460 465
        debug("\nStarting isRegistered test...");
461 466
        debug("-------------------------------------------------------");
462 467
        try {
463
            boolean registered = m.isRegistered(public_doc_id);
464
            debug("isRegistered(): " + public_doc_id+" is "+(registered?"":"not ")+"registered"); 
465
            registered = m.isRegistered(newdocid);
466
            debug("isRegistered(): " + newdocid+" is "+(registered?"":"not ")+"registered");
468
            IdentifierManager im = IdentifierManager.getInstance();
469
            String docid = insertTestDocument();
470
            boolean registered = m.isRegistered(docid);
471
            debug("isRegistered(): " + docid + ".1" +" is "+(registered?"":"not ")+"registered");
472
            assertTrue(registered);
473
            String unregDocid = generateDocumentId() + ".1";
474
            registered = m.isRegistered(unregDocid);
475
            debug("isRegistered(): " + unregDocid+" is "+(registered?"":"not ")+"registered");
476
            assertFalse(registered);
467 477
        } catch (MetacatException me) {
468 478
            fail("Metacat Error:\n" + me.getMessage());
469 479
        } catch (Exception e) {
......
471 481
        }
472 482
    }
473 483
    
474
    /***
484
    /**
475 485
     * Test addLSID function
476 486
     */
477
    public void addLSID() {
487
    public void addLSID() 
488
    {
478 489
    	  debug("\nStarting addLSID test...");
479 490
    	  debug("-------------------------------------------------------");
480 491
          try {
481
        	  String response =  m.addLSID(public_doc_id);
492
              String unregDocid = generateDocumentId() + ".1";
493
        	  String response =  m.addLSID(unregDocid);
482 494
        	  debug("response:\n"+response);
483 495
          } catch (MetacatException me) {
484 496
              fail("Metacat Error:\n" + me.getMessage());
......
486 498
              fail("General exception:\n" + e.getMessage());
487 499
          }
488 500
    }
489
    	
490
    	
491
    /**
492
     * Create a hopefully unique docid for testing insert and update. Does
493
     * not include the 'revision' part of the id.
494
     *
495
     * @return a String docid based on the current date and time
496
     */
497
    private String generateDocid()
501
    
502
    /** Insert a test document, returning the docid that was used. */
503
    private String insertTestDocument()
498 504
    {
499
    	StringBuffer docid = new StringBuffer(prefix);
500
    	docid.append(".");
501

  
502
    	// Create a calendar to get the date formatted properly
503
    	String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
504
    	SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
505
    	pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
506
    	pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
507
    	Calendar calendar = new GregorianCalendar(pdt);
508
    	Date trialTime = new Date();
509
    	calendar.setTime(trialTime);
510

  
511
    	int time = 0; 
512

  
513
    	docid.append(calendar.get(Calendar.YEAR));
514

  
515
    	time = calendar.get(Calendar.DAY_OF_YEAR);
516
    	if(time < 10){
517
    		docid.append("0");
518
    		docid.append("0");
519
    		docid.append(time);
520
    	} else if(time < 100) {
521
    		docid.append("0");
522
    		docid.append(time);
523
    	} else {
524
    		docid.append(time);
525
    	}
526

  
527
    	time = calendar.get(Calendar.HOUR_OF_DAY);
528
    	if(time < 10){
529
    		docid.append("0");
530
    		docid.append(time);
531
    	} else {
532
    		docid.append(time);
533
    	}
534

  
535
    	time = calendar.get(Calendar.MINUTE);
536
    	if(time < 10){
537
    		docid.append("0");
538
    		docid.append(time);
539
    	} else {
540
    		docid.append(time);
541
    	}
542

  
543
    	time = calendar.get(Calendar.SECOND);
544
    	if(time < 10){
545
    		docid.append("0");
546
    		docid.append(time);
547
    	} else {
548
    		docid.append(time);
549
    	}
550

  
551
    	//sometimes this number is not unique, so we append a random number
552
    	int random = (new Double(Math.random()*100)).intValue();
553
    	docid.append(random);
554
    	return docid.toString();
505
        String accessBlock = getAccessBlock("public", true, true,
506
                false, false, false);
507
        String emldoc = getTestEmlDoc(DOC_TITLE, EML2_1_0, null,
508
                null, "http://fake.example.com/somedata", null,
509
                accessBlock, null, null,
510
                null, null);
511
        String docid = generateDocumentId() + ".1";
512
        StringReader sr = new StringReader(emldoc);        
513
        String response;
514
        try {
515
            m.login(username,password);
516
            response = m.putObject(docid,sr,true);
517
            debug("response:\n"+response);
518
            assertTrue(response.indexOf("success") != -1);
519
        } catch (InsufficientKarmaException e) {
520
            fail(e.getMessage());
521
        } catch (MetacatException e) {
522
            fail(e.getMessage());
523
        } catch (IOException e) {
524
            fail(e.getMessage());
525
        } catch (MetacatInaccessibleException e) {
526
            fail(e.getMessage());
527
        } catch (MetacatAuthException e) {
528
            fail(e.getMessage());
529
        }
530
        return docid;
555 531
    }
556 532
}

Also available in: Unified diff