Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2010 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: leinfelder $'
8
 *     '$Date: 2011-07-08 14:08:47 -0700 (Fri, 08 Jul 2011) $'
9
 * '$Revision: 6324 $'
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.metacat.dataone;
27

    
28
import java.io.ByteArrayInputStream;
29
import java.io.InputStream;
30
import java.text.SimpleDateFormat;
31
import java.util.Date;
32

    
33
import junit.framework.Test;
34
import junit.framework.TestSuite;
35

    
36
import org.apache.commons.io.IOUtils;
37
import org.dataone.service.Constants;
38
import org.dataone.service.exceptions.InsufficientResources;
39
import org.dataone.service.exceptions.InvalidRequest;
40
import org.dataone.service.exceptions.NotAuthorized;
41
import org.dataone.service.exceptions.NotFound;
42
import org.dataone.service.exceptions.NotImplemented;
43
import org.dataone.service.exceptions.ServiceFailure;
44
import org.dataone.service.types.AccessPolicy;
45
import org.dataone.service.types.AccessRule;
46
import org.dataone.service.types.Checksum;
47
import org.dataone.service.types.Event;
48
import org.dataone.service.types.Identifier;
49
import org.dataone.service.types.Log;
50
import org.dataone.service.types.NodeReference;
51
import org.dataone.service.types.ObjectFormat;
52
import org.dataone.service.types.ObjectFormatIdentifier;
53
import org.dataone.service.types.ObjectFormatList;
54
import org.dataone.service.types.ObjectInfo;
55
import org.dataone.service.types.ObjectList;
56
import org.dataone.service.types.Permission;
57
import org.dataone.service.types.Replica;
58
import org.dataone.service.types.ReplicationPolicy;
59
import org.dataone.service.types.ReplicationStatus;
60
import org.dataone.service.types.Session;
61
import org.dataone.service.types.Subject;
62
import org.dataone.service.types.SystemMetadata;
63

    
64
/**
65
 * A JUnit test for testing the dataone CNCore implementation
66
 */
67
public class CNodeServiceTest extends D1NodeServiceTest {   
68
    
69
    /**
70
    * constructor for the test
71
    */
72
    public CNodeServiceTest(String name)
73
    {
74
        super(name);
75
    }
76

    
77
	/**
78
	 * Create a suite of tests to be run together
79
	 */
80
	public static Test suite() 
81
	{
82
		TestSuite suite = new TestSuite();
83
		suite.addTest(new CNodeServiceTest("initialize"));
84
		
85
		suite.addTest(new CNodeServiceTest("testAssertRelation"));
86
		suite.addTest(new CNodeServiceTest("testChecksum"));
87
		suite.addTest(new CNodeServiceTest("testCreate"));
88
		suite.addTest(new CNodeServiceTest("testGet"));
89
		suite.addTest(new CNodeServiceTest("testGetFormat"));
90
		suite.addTest(new CNodeServiceTest("testGetLogRecords"));
91
		suite.addTest(new CNodeServiceTest("testGetSystemMetadata"));
92
		suite.addTest(new CNodeServiceTest("testIsAuthorized"));
93
		suite.addTest(new CNodeServiceTest("testListFormats"));
94
		suite.addTest(new CNodeServiceTest("testListNodes"));
95
		suite.addTest(new CNodeServiceTest("testObjectFormatNotFoundException"));
96
		suite.addTest(new CNodeServiceTest("testRegisterSystemMetadata"));
97
		suite.addTest(new CNodeServiceTest("testReplicationPolicy"));
98
		suite.addTest(new CNodeServiceTest("testReplicationStatus"));
99
		suite.addTest(new CNodeServiceTest("testReserveIdentifier"));
100
		suite.addTest(new CNodeServiceTest("testSearch"));
101
		suite.addTest(new CNodeServiceTest("testSetAccessPolicy"));
102
		suite.addTest(new CNodeServiceTest("testSetOwner"));
103
	
104
		return suite;
105
	}
106
	
107
	
108
	/**
109
	 * test for registering standalone system metadata
110
	 */
111
	public void testRegisterSystemMetadata() {
112
	    printTestHeader("testRegisterSystemMetadata");
113

    
114
	    try {
115
            Session session = getTestSession();
116
			Identifier guid = new Identifier();
117
			guid.setValue("testRegisterSystemMetadata." + System.currentTimeMillis());
118
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
119
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
120
			assertTrue(result);
121
        } catch(Exception e) {
122
            fail("Unexpected error: " + e.getMessage());
123
        }
124
	}
125
	
126
	/**
127
	 * test for getting system metadata
128
	 */
129
	public void testGetSystemMetadata() {
130
	    printTestHeader("testGetSystemMetadata");
131

    
132
	    try {
133
            Session session = getTestSession();
134
			Identifier guid = new Identifier();
135
			guid.setValue("testGetSystemMetadata." + System.currentTimeMillis());
136
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
137
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
138
			assertTrue(result);
139
			// get it
140
			SystemMetadata retSysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
141
			// check it
142
			assertEquals(sysmeta.getIdentifier().getValue(), retSysmeta.getIdentifier().getValue());
143
        } catch(Exception e) {
144
            fail("Unexpected error: " + e.getMessage());
145
        }
146
	}
147
	
148
	public void testGetLogRecords() {
149
	    printTestHeader("testGetLogRecords");
150
	    try {
151

    
152
		    Session session = getTestSession();
153
		    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
154
		    Date fromDate = sdf.parse("2010-01-01");
155
		    Date toDate = new Date();
156
		    Event event = Event.CREATE;
157
		    int start = 0;
158
		    int count = 1;
159
	    
160
		    Log log = CNodeService.getInstance().getLogRecords(session, fromDate, toDate, 
161
		    	event, start, count);
162
		    assertNotNull(log);
163
		    assertTrue(log.getCount() == count);
164
		    assertTrue(log.getStart() == start);
165
		    assertTrue(log.getTotal() > 0);
166
	    } catch (Exception e) {
167
		    e.printStackTrace();
168
		    fail("Unexpected error: " + e.getMessage());
169
	    } 
170
	}
171
	
172
	public void testCreate() {
173
	    printTestHeader("testCreate");
174

    
175
	    try {
176
            Session session = getTestSession();
177
			Identifier guid = new Identifier();
178
			guid.setValue("testCreate." + System.currentTimeMillis());
179
			InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
180
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
181
			Identifier pid = CNodeService.getInstance().create(session, guid, object, sysmeta);
182
			assertEquals(guid, pid);
183
        } catch(Exception e) {
184
        	e.printStackTrace();
185
            fail("Unexpected error: " + e.getMessage());
186
        }
187
	}
188
	
189
	public void testGet() {
190
	    printTestHeader("testGet");
191

    
192
	    try {
193
            Session session = getTestSession();
194
			Identifier guid = new Identifier();
195
			guid.setValue("testGet." + System.currentTimeMillis());
196
			InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
197
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
198
			Identifier pid = CNodeService.getInstance().create(session, guid, object, sysmeta);
199
			assertEquals(guid, pid);
200
			// get it
201
			InputStream retObject = CNodeService.getInstance().get(session, pid);
202
			// check it
203
			assertTrue(IOUtils.contentEquals(object, retObject));
204
        } catch(Exception e) {
205
        	e.printStackTrace();
206
            fail("Unexpected error: " + e.getMessage());
207
        }
208
	}
209
	
210
	public void testAssertRelation() {
211
	    printTestHeader("testAssertRelation");
212

    
213
	    try {
214
            Session session = getTestSession();
215
			Identifier guid = new Identifier();
216
			guid.setValue("testAssertRelation." + System.currentTimeMillis());
217
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
218
			Identifier describePid = new Identifier();
219
			describePid.setValue("describePid." + System.currentTimeMillis());
220
			sysmeta.addDescribe(describePid);
221
			// save it
222
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
223
			assertTrue(result);
224
			// save the other
225
			SystemMetadata describeSysmeta = createSystemMetadata(describePid, session.getSubject(), null);
226
			result = CNodeService.getInstance().registerSystemMetadata(session, describePid, describeSysmeta);
227
			assertTrue(result);
228
			// check it
229
			result = CNodeService.getInstance().assertRelation(session, guid, "describes", describePid);
230
			assertTrue(result);
231
        } catch(Exception e) {
232
        	e.printStackTrace();
233
            fail("Unexpected error: " + e.getMessage());
234
        }
235
	}
236
	
237
	public void testChecksum() {
238
	    printTestHeader("testChecksum");
239

    
240
	    try {
241
            Session session = getTestSession();
242
			Identifier guid = new Identifier();
243
			guid.setValue("testChecksum." + System.currentTimeMillis());
244
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
245
			// save it
246
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
247
			assertTrue(result);
248
			// check it
249
			Checksum checksum = CNodeService.getInstance().getChecksum(session, guid);
250
			assertEquals(sysmeta.getChecksum().getValue(), checksum.getValue());
251
        } catch(Exception e) {
252
            fail("Unexpected error: " + e.getMessage());
253
        }
254
	}
255
	
256
	public void testListNodes() {
257
	    printTestHeader("testListNodes");
258

    
259
	    try {
260
	    	CNodeService.getInstance().listNodes();
261
        } catch(NotImplemented e) {
262
        	// expecting not implemented
263
            assertTrue(true);
264
        } catch(Exception e) {
265
            fail("Unexpected error: " + e.getMessage());
266
        }
267
	}
268
	
269
	public void testReserveIdentifier() {
270
	    printTestHeader("testReserveIdentifier");
271

    
272
	    try {
273
            Session session = getTestSession();
274
			Identifier guid = new Identifier();
275
			guid.setValue("testReserveIdentifier." + System.currentTimeMillis());
276
			// reserve it
277
			Identifier retPid = CNodeService.getInstance().reserveIdentifier(session, guid, null, null);
278
			assertEquals(guid, retPid);
279
        } catch(Exception e) {
280
        	e.printStackTrace();
281
            fail("Unexpected error: " + e.getMessage());
282
        }
283
	}
284
	
285
	public void testSearch() {
286
	    printTestHeader("testSearch");
287

    
288
	    try {
289
            Session session = getTestSession();
290
			Identifier guid = new Identifier();
291
			guid.setValue("testSearch." + System.currentTimeMillis());
292
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
293
			
294
			// save it
295
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
296
			assertTrue(result);
297
			// search for it
298
			ObjectList objectList = CNodeService.getInstance().search(session, null, null);
299
			// check it
300
			assertNotNull(objectList);
301
			result = false;
302
			for (ObjectInfo objectInfo: objectList.getObjectInfoList()) {
303
				Identifier pid = objectInfo.getIdentifier();
304
				if (pid.getValue().equals(guid.getValue())) {
305
					result = true;
306
					break;
307
				}
308
			}
309
			assertTrue(result);
310
        } catch(Exception e) {
311
            fail("Unexpected error: " + e.getMessage());
312
        }
313
	}
314
	
315
	public void testSetOwner() {
316
	    printTestHeader("testSetOwner");
317

    
318
	    try {
319
            Session session = getTestSession();
320
			Identifier guid = new Identifier();
321
			guid.setValue("testSetOwner." + System.currentTimeMillis());
322
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
323
			// save it
324
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
325
			assertTrue(result);
326
			Subject rightsHolder = new Subject();
327
			rightsHolder.setValue("newUser");
328
			// set it
329
			Identifier retPid = CNodeService.getInstance().setOwner(session, guid, rightsHolder);
330
			assertEquals(guid, retPid);
331
			// get it
332
			sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
333
			assertNotNull(sysmeta);
334
			// check it
335
			assertEquals(rightsHolder.getValue(), sysmeta.getRightsHolder().getValue());
336
			
337
        } catch(Exception e) {
338
            fail("Unexpected error: " + e.getMessage());
339
        }
340
	}
341
	
342
	public void testSetAccessPolicy() {
343
	    printTestHeader("testSetAccessPolicy");
344

    
345
	    try {
346
            Session session = getTestSession();
347
			Identifier guid = new Identifier();
348
			guid.setValue("testSetAccessPolicy." + System.currentTimeMillis());
349
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
350
			// save it
351
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
352
			assertTrue(result);
353
			AccessPolicy accessPolicy = new AccessPolicy();
354
			AccessRule accessRule = new AccessRule();
355
			accessRule.addPermission(Permission.WRITE);
356
			Subject publicSubject = new Subject();
357
			publicSubject.setValue(Constants.PUBLIC_SUBJECT);
358
			accessRule.addSubject(publicSubject);
359
			accessPolicy.addAllow(accessRule);
360
			// set it
361
			result = CNodeService.getInstance().setAccessPolicy(session, guid, accessPolicy );
362
			assertTrue(result);
363
			// check it
364
			result = CNodeService.getInstance().isAuthorized(session, guid, Permission.WRITE);
365
			assertTrue(result);
366
        } catch(Exception e) {
367
            fail("Unexpected error: " + e.getMessage());
368
        }
369
	}
370
	
371
	public void testIsAuthorized() {
372
	    printTestHeader("testIsAuthorized");
373

    
374
	    try {
375
            Session session = getTestSession();
376
			Identifier guid = new Identifier();
377
			guid.setValue("testIsAuthorized." + System.currentTimeMillis());
378
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
379
			// save it
380
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
381
			assertTrue(result);
382
			// check it
383
			Subject publicSubject = new Subject();
384
			publicSubject.setValue(Constants.PUBLIC_SUBJECT);
385
			session.setSubject(publicSubject);
386
			// public read
387
			result = CNodeService.getInstance().isAuthorized(session, guid, Permission.READ);
388
			assertTrue(result);
389
			// not public write
390
			try {
391
				result = false;
392
				result = CNodeService.getInstance().isAuthorized(session, guid, Permission.WRITE);
393
				fail("Public WRITE should be denied");
394
			} catch (NotAuthorized nae) {
395
				result = true;
396
			}
397
			assertTrue(result);
398
        } catch(Exception e) {
399
            fail("Unexpected error: " + e.getMessage());
400
        }
401
	}
402
	
403
	public void testReplicationPolicy() {
404
	    printTestHeader("testReplicationPolicy");
405

    
406
	    try {
407
            Session session = getTestSession();
408
			Identifier guid = new Identifier();
409
			guid.setValue("testReplicationPolicy." + System.currentTimeMillis());
410
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
411
			// save it
412
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
413
			assertTrue(result);
414
			
415
			ReplicationPolicy policy = new ReplicationPolicy();
416
			NodeReference node = new NodeReference();
417
			node.setValue("testNode");
418
			policy.addPreferredMemberNode(node );
419
			// set it
420
			result = CNodeService.getInstance().setReplicationPolicy(session, guid, policy);
421
			assertTrue(result);
422
			// get it
423
			sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
424
			assertNotNull(sysmeta);
425
			// check it
426
			assertEquals(policy.getPreferredMemberNode(0).getValue(), sysmeta.getReplicationPolicy().getPreferredMemberNode(0).getValue());
427
			
428
        } catch(Exception e) {
429
            fail("Unexpected error: " + e.getMessage());
430
        }
431
	}
432
	
433
	public void testReplicationStatus() {
434
	    printTestHeader("testReplicationStatus");
435

    
436
	    try {
437
            Session session = getTestSession();
438
			Identifier guid = new Identifier();
439
			guid.setValue("testReplicationStatus." + System.currentTimeMillis());
440
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
441
			Replica replica = new Replica();
442
			NodeReference replicaMemberNode = new NodeReference();
443
			replicaMemberNode.setValue("testNode");
444
			replica.setReplicationStatus(ReplicationStatus.REQUESTED);
445
			replica.setReplicaMemberNode(replicaMemberNode);
446
			replica.setReplicaVerified(new Date());
447
			sysmeta.addReplica(replica );
448
			// save it
449
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
450
			assertTrue(result);
451
			// set it
452
			ReplicationStatus status = ReplicationStatus.COMPLETED;
453
			result = CNodeService.getInstance().setReplicationStatus(session, guid, status);
454
			assertTrue(result);
455
			// get it
456
			sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
457
			assertNotNull(sysmeta);
458
			// check it
459
			assertEquals(status, sysmeta.getReplica(0).getReplicationStatus());
460
			
461
        } catch(Exception e) {
462
            fail("Unexpected error: " + e.getMessage());
463
        }
464
	}
465
	
466
	/**
467
	 * Run an initial test that always passes to check that the test harness is
468
	 * working.
469
	 */
470
	public void initialize() 
471
	{
472
	    printTestHeader("initialize");
473
		assertTrue(1 == 1);
474
	}
475
	
476

    
477
	/**
478
	 * test to list the object formats registered in metacat
479
	 */
480
	public void testListFormats() {
481
		
482
    printTestHeader("testListFormats");
483
    
484
    // make sure we are set up
485
    setUpFormats();
486
    
487
    // there should be at least 59 formats in the list
488
  	int formatsCount = 59;
489
  	ObjectFormatList objectFormatList;
490
  	
491
  	try {
492
	    objectFormatList = CNodeService.getInstance().listFormats();
493
	  	assertTrue(objectFormatList.getTotal() >= formatsCount);
494
  	
495
  	} catch (InvalidRequest e) {
496
  		fail("Could not get the object format list: " + e.getMessage());
497
    
498
  	} catch (ServiceFailure e) {
499
  		fail("Could not get the object format list: " + e.getMessage());
500

    
501
    } catch (NotFound e) {
502
  		fail("Could not get the object format list: " + e.getMessage());
503

    
504
    } catch (InsufficientResources e) {
505
  		fail("Could not get the object format list: " + e.getMessage());
506

    
507
    } catch (NotImplemented e) {
508
  		fail("Could not get the object format list: " + e.getMessage());
509

    
510
    }
511
    
512
	}
513
	
514
  /**
515
   * Test getting a single object format from the registered list
516
   */
517
  public void testGetFormat() {
518
  	
519
    printTestHeader("testGetFormat");
520

    
521
    // make sure we are set up
522
    setUpFormats();
523
    
524
    String knownFormat = "text/plain";
525
    ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
526
    fmtid.setValue(knownFormat);
527
  	
528
    try {
529
	    
530
			String result = 
531
				CNodeService.getInstance().getFormat(fmtid).getFmtid().getValue();
532
	  	System.out.println("Expected result: " + knownFormat);
533
	  	System.out.println("Found    result: " + result);
534
	  	assertTrue(result.equals(knownFormat));
535
  
536
    } catch (NullPointerException npe) {	  
537
	    fail("The returned format was null: " + npe.getMessage());
538
    
539
    } catch (NotFound nfe) {     
540
    	fail("The format " + knownFormat + " was not found: " + nfe.getMessage());
541
    	
542
    } catch (InvalidRequest ire) {
543
    	fail("The format " + knownFormat + " was not found: " + ire.getMessage());
544

    
545
    } catch (ServiceFailure sfe) {
546
    	fail("The format " + knownFormat + " was not found: " + sfe.getMessage());
547

    
548
    } catch (InsufficientResources ise) {
549
    	fail("The format " + knownFormat + " was not found: " + ise.getMessage());
550
 
551
    } catch (NotImplemented nie) {
552
    	fail("The getFormat() method has not been implemented: " + nie.getMessage());
553

    
554
    }
555
  	
556
  }
557
	
558
  /**
559
   * Test getting a non-existent object format, returning NotFound
560
   */
561
  public void testObjectFormatNotFoundException() {
562
  
563
    printTestHeader("testObjectFormatNotFoundException");
564

    
565
    ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
566
  	String badFormat = "text/bad-format";
567
  	fmtid.setValue(badFormat);
568
  	
569
  	try {
570
  		
571
	    ObjectFormat objectFormat = 
572
	    	CNodeService.getInstance().getFormat(fmtid);
573
      
574
  	} catch (Exception e) {
575
	    
576
  		assertTrue(e instanceof NotFound);
577
  	}
578
  	
579
  }
580
 
581
}
(1-1/6)