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-09-12 09:34:50 -0700 (Mon, 12 Sep 2011) $'
9
 * '$Revision: 6413 $'
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.util.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.v1.AccessPolicy;
45
import org.dataone.service.types.v1.AccessRule;
46
import org.dataone.service.types.v1.Checksum;
47
import org.dataone.service.types.v1.Event;
48
import org.dataone.service.types.v1.Identifier;
49
import org.dataone.service.types.v1.Log;
50
import org.dataone.service.types.v1.NodeReference;
51
import org.dataone.service.types.v1.ObjectFormat;
52
import org.dataone.service.types.v1.ObjectFormatIdentifier;
53
import org.dataone.service.types.v1.ObjectFormatList;
54
import org.dataone.service.types.v1.ObjectInfo;
55
import org.dataone.service.types.v1.ObjectList;
56
import org.dataone.service.types.v1.Permission;
57
import org.dataone.service.types.v1.Replica;
58
import org.dataone.service.types.v1.ReplicationPolicy;
59
import org.dataone.service.types.v1.ReplicationStatus;
60
import org.dataone.service.types.v1.Session;
61
import org.dataone.service.types.v1.Subject;
62
import org.dataone.service.types.v1.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 Identifier 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
			Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
120
			assertEquals(guid.getValue(), retGuid.getValue());
121
			return retGuid;
122
        } catch(Exception e) {
123
            fail("Unexpected error: " + e.getMessage());
124
        }
125
        return null;
126
	}
127
	
128
	/**
129
	 * test for getting system metadata
130
	 */
131
	public void testGetSystemMetadata() {
132
	    printTestHeader("testGetSystemMetadata");
133

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

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

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

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

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

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

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

    
275
	    try {
276
            Session session = getTestSession();
277
			Identifier guid = new Identifier();
278
			guid.setValue("testReserveIdentifier." + System.currentTimeMillis());
279
			// reserve it
280
			boolean result = CNodeService.getInstance().reserveIdentifier(session, guid);
281
			assertTrue(result);
282
	    } catch(NotImplemented ni) {
283
        	// this is not implemented in Metacat
284
            assertTrue(true);	
285
        } catch(Exception e) {
286
        	e.printStackTrace();
287
            fail("Unexpected error: " + e.getMessage());
288
        }
289
	}
290
	
291
	public void testSearch() {
292
	    printTestHeader("testSearch");
293

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

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

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

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

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

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

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

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

    
510
    } catch (InsufficientResources e) {
511
  		fail("Could not get the object format list: " + e.getMessage());
512

    
513
    } catch (NotImplemented e) {
514
  		fail("Could not get the object format list: " + e.getMessage());
515

    
516
    }
517
    
518
	}
519
	
520
  /**
521
   * Test getting a single object format from the registered list
522
   */
523
  public void testGetFormat() {
524
  	
525
    printTestHeader("testGetFormat");
526

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

    
551
    } catch (ServiceFailure sfe) {
552
    	fail("The format " + knownFormat + " was not found: " + sfe.getMessage());
553

    
554
    } catch (InsufficientResources ise) {
555
    	fail("The format " + knownFormat + " was not found: " + ise.getMessage());
556
 
557
    } catch (NotImplemented nie) {
558
    	fail("The getFormat() method has not been implemented: " + nie.getMessage());
559

    
560
    }
561
  	
562
  }
563
	
564
  /**
565
   * Test getting a non-existent object format, returning NotFound
566
   */
567
  public void testObjectFormatNotFoundException() {
568
  
569
    printTestHeader("testObjectFormatNotFoundException");
570

    
571
    ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
572
  	String badFormat = "text/bad-format";
573
  	fmtid.setValue(badFormat);
574
  	
575
  	try {
576
  		
577
	    ObjectFormat objectFormat = 
578
	    	CNodeService.getInstance().getFormat(fmtid);
579
      
580
  	} catch (Exception e) {
581
	    
582
  		assertTrue(e instanceof NotFound);
583
  	}
584
  	
585
  }
586
 
587
}
(1-1/6)