Project

General

Profile

« Previous | Next » 

Revision 10044

Added by Jing Tao about 8 years ago

Add code to testArchive, testSetAccessPolicy, testSetOwner and testSetReplicationPolicy on objects whose authoritative member node is v1 or v2 MNRead.

View differences:

test/edu/ucsb/nceas/metacat/dataone/CNodeServiceTest.java
322 322
	    printTestHeader("testSetOwner");
323 323

  
324 324
	    try {
325
            //v2 mn should fail
325 326
            Session session = getCNSession();
327
            Identifier guid = new Identifier();
328
            guid.setValue("testSetOwner." + System.currentTimeMillis());
329
            InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
330
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
331
            NodeReference nr = new NodeReference();
332
            nr.setValue(MockCNode.V1MNNODEID);
333
            sysmeta.setOriginMemberNode(nr);
334
            sysmeta.setAuthoritativeMemberNode(nr);
335
            long serialVersion = 1L;
336
            // save it
337
            Identifier retGuid = CNodeService.getInstance(request).registerSystemMetadata(session, guid, sysmeta);
338
            assertEquals(guid.getValue(), retGuid.getValue());
339
            Subject rightsHolder = new Subject();
340
            rightsHolder.setValue("newUser");
341
            // set it
342
            Identifier retPid = CNodeService.getInstance(request).setRightsHolder(session, guid, rightsHolder, serialVersion);
343
            assertEquals(guid, retPid);
344
            // get it
345
            sysmeta = CNodeService.getInstance(request).getSystemMetadata(session, guid);
346
            assertNotNull(sysmeta);
347
            // check it
348
            assertTrue(rightsHolder.equals(sysmeta.getRightsHolder()));
349
            
350
        } catch(Exception e) {
351
            e.printStackTrace();
352
            fail("Unexpected error: " + e.getMessage());
353
        }
354
	    try {
355
	        //v2 mn should fail
356
            Session session = getCNSession();
326 357
			Identifier guid = new Identifier();
327 358
			guid.setValue("testSetOwner." + System.currentTimeMillis());
328 359
			InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
329 360
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
361
			NodeReference nr = new NodeReference();
362
            nr.setValue(MockCNode.V2MNNODEID);
363
            sysmeta.setOriginMemberNode(nr);
364
            sysmeta.setAuthoritativeMemberNode(nr);
330 365
			long serialVersion = 1L;
331 366
			// save it
332 367
			Identifier retGuid = CNodeService.getInstance(request).registerSystemMetadata(session, guid, sysmeta);
......
344 379
			
345 380
        } catch(Exception e) {
346 381
            e.printStackTrace();
347
            if(e instanceof ServiceFailure) {
348
                assertTrue(e.getMessage().contains("Couldn't determine the authoritative member node"));
382
            if(e instanceof NotAuthorized) {
383
                assertTrue(e.getMessage().contains("The Coordinating Node is not authorized to make systemMetadata changes"));
349 384
            } else {
350 385
                fail("Unexpected error: " + e.getMessage());
351 386
            }
......
354 389
	
355 390
	public void testSetAccessPolicy() {
356 391
	    printTestHeader("testSetAccessPolicy");
392
	    
393
	    
394
	    try {
395
            // trys to set access policy on an object whose authortiative memeber node is MNRead v2. It should fail.
396
            Session session = getCNSession();
397
            Identifier guid = new Identifier();
398
            guid.setValue("testSetAccessPolicy." + System.currentTimeMillis());
399
            InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
400
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
401
            NodeReference nr = new NodeReference();
402
            nr.setValue(MockCNode.V1MNNODEID);
403
            sysmeta.setOriginMemberNode(nr);
404
            sysmeta.setAuthoritativeMemberNode(nr);
405
            
406
            long serialVersion = 1L;
357 407

  
408
            // save it
409
            Identifier retGuid = CNodeService.getInstance(request).registerSystemMetadata(session, guid, sysmeta);
410
            assertEquals(guid.getValue(), retGuid.getValue());
411
            AccessPolicy accessPolicy = new AccessPolicy();
412
            AccessRule accessRule = new AccessRule();
413
            accessRule.addPermission(Permission.WRITE);
414
            Subject publicSubject = new Subject();
415
            publicSubject.setValue(Constants.SUBJECT_PUBLIC);
416
            accessRule.addSubject(publicSubject);
417
            accessPolicy.addAllow(accessRule);
418
            // set it
419
            boolean result = CNodeService.getInstance(request).setAccessPolicy(session, guid, accessPolicy, serialVersion );
420
            assertTrue(result);
421
            // check it
422
            result = CNodeService.getInstance(request).isAuthorized(session, guid, Permission.WRITE);
423
            assertTrue(result);
424
        } catch(Exception e) {
425
            
426
            fail("Unexpected error: " + e.getMessage());
427

  
428
        }
429

  
358 430
	    try {
431
	        // trys to set access policy on an object whose authortiative memeber node is MNRead v2. It should fail.
359 432
            Session session = getCNSession();
360 433
			Identifier guid = new Identifier();
361 434
			guid.setValue("testSetAccessPolicy." + System.currentTimeMillis());
362 435
			InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
363 436
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
364
	    long serialVersion = 1L;
437
			NodeReference nr = new NodeReference();
438
	        nr.setValue(MockCNode.V2MNNODEID);
439
	        sysmeta.setOriginMemberNode(nr);
440
			sysmeta.setAuthoritativeMemberNode(nr);
441
			
442
	        long serialVersion = 1L;
365 443

  
366 444
			// save it
367 445
			Identifier retGuid = CNodeService.getInstance(request).registerSystemMetadata(session, guid, sysmeta);
......
381 459
			assertTrue(result);
382 460
        } catch(Exception e) {
383 461
            e.printStackTrace();
384
            if(e instanceof ServiceFailure) {
385
                assertTrue(e.getMessage().contains("Couldn't determine the authoritative member node"));
462
            if(e instanceof NotAuthorized) {
463
                assertTrue(e.getMessage().contains("The Coordinating Node is not authorized to make systemMetadata changes"));
386 464
            } else {
387 465
                fail("Unexpected error: " + e.getMessage());
388 466
            }
......
426 504
	    printTestHeader("testReplicationPolicy");
427 505

  
428 506
	    try {
507
            //v2 mn should fail
429 508
            Session session = getCNSession();
509
            Identifier guid = new Identifier();
510
            guid.setValue("testReplicationPolicy." + System.currentTimeMillis());
511
            InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
512
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
513
            NodeReference nr = new NodeReference();
514
            nr.setValue(MockCNode.V1MNNODEID);
515
            sysmeta.setOriginMemberNode(nr);
516
            sysmeta.setAuthoritativeMemberNode(nr);
517
            long serialVersion = 1L;
518

  
519
            // save it
520
            Identifier retGuid = CNodeService.getInstance(request).registerSystemMetadata(session, guid, sysmeta);
521
            assertEquals(guid.getValue(), retGuid.getValue());
522
            
523
            ReplicationPolicy policy = new ReplicationPolicy();
524
            NodeReference node = new NodeReference();
525
            node.setValue("testNode");
526
            policy.addPreferredMemberNode(node );
527
            // set it
528
            boolean result = CNodeService.getInstance(request).setReplicationPolicy(session, guid, policy, serialVersion);
529
            assertTrue(result);
530
            // get it
531
            sysmeta = CNodeService.getInstance(request).getSystemMetadata(session, guid);
532
            assertNotNull(sysmeta);
533
            // check it
534
            assertEquals(policy.getPreferredMemberNode(0).getValue(), sysmeta.getReplicationPolicy().getPreferredMemberNode(0).getValue());
535
            
536
        } catch(Exception e) {
537
           
538
                fail("Unexpected error: " + e.getMessage());
539
           
540
            
541
        }
542
	    try {
543
	        //v2 mn should fail
544
            Session session = getCNSession();
430 545
			Identifier guid = new Identifier();
431 546
			guid.setValue("testReplicationPolicy." + System.currentTimeMillis());
432 547
			InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
433 548
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
434
	    long serialVersion = 1L;
549
			NodeReference nr = new NodeReference();
550
            nr.setValue(MockCNode.V2MNNODEID);
551
            sysmeta.setOriginMemberNode(nr);
552
            sysmeta.setAuthoritativeMemberNode(nr);
553
	        long serialVersion = 1L;
435 554

  
436 555
			// save it
437 556
			Identifier retGuid = CNodeService.getInstance(request).registerSystemMetadata(session, guid, sysmeta);
......
452 571
			
453 572
        } catch(Exception e) {
454 573
            e.printStackTrace();
455
            if(e instanceof ServiceFailure) {
456
                assertTrue(e.getMessage().contains("Couldn't determine the authoritative member node"));
574
            if(e instanceof NotAuthorized) {
575
                assertTrue(e.getMessage().contains("The Coordinating Node is not authorized to make systemMetadata changes"));
457 576
            } else {
458 577
                fail("Unexpected error: " + e.getMessage());
459 578
            }
......
1454 1573
  }
1455 1574
  
1456 1575
  public void testArchive() throws Exception {
1457
      Session session = getCNSession();
1458
      Identifier guid = new Identifier();
1459
      guid.setValue("testArchive." + System.currentTimeMillis());
1460
      InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
1461
      SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
1462
      Identifier pid = CNodeService.getInstance(request).create(session, guid, object, sysmeta);
1463 1576
      try {
1577
          Session session = getCNSession();
1578
          Identifier guid = new Identifier();
1579
          guid.setValue("testArchive." + System.currentTimeMillis());
1580
          InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
1581
          SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
1582
          NodeReference nr = new NodeReference();
1583
          nr.setValue(MockCNode.V1MNNODEID);
1584
          sysmeta.setOriginMemberNode(nr);
1585
          sysmeta.setAuthoritativeMemberNode(nr);
1586
          Identifier pid = CNodeService.getInstance(request).create(session, guid, object, sysmeta);
1464 1587
          CNodeService.getInstance(request).archive(session, guid);
1465 1588
      } catch (Exception e) {
1466 1589
         e.printStackTrace();
1467
         if(e instanceof ServiceFailure) {
1468
          assertTrue(e.getMessage().contains("Couldn't determine the authoritative member node"));
1469
        } else {
1470
          fail("Unexpected error: " + e.getMessage());
1471
        }
1590
         fail("Unexpected error: " + e.getMessage());
1472 1591
     }
1592
      
1593
      try {
1594
          //v2 mn should faile
1595
          Session session = getCNSession();
1596
          Identifier guid = new Identifier();
1597
          guid.setValue("testArchive." + System.currentTimeMillis());
1598
          InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
1599
          SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
1600
          NodeReference nr = new NodeReference();
1601
          nr.setValue(MockCNode.V2MNNODEID);
1602
          sysmeta.setOriginMemberNode(nr);
1603
          sysmeta.setAuthoritativeMemberNode(nr);
1604
          Identifier pid = CNodeService.getInstance(request).create(session, guid, object, sysmeta);
1605
          CNodeService.getInstance(request).archive(session, guid);
1606
      } catch (Exception e) {
1607
         e.printStackTrace();
1608
         if(e instanceof NotAuthorized) {
1609
             assertTrue(e.getMessage().contains("The Coordinating Node is not authorized to make systemMetadata changes"));
1610
         } else {
1611
             fail("Unexpected error: " + e.getMessage());
1612
         }
1613
     }
1473 1614
  }
1474 1615
  
1475 1616
  
test/edu/ucsb/nceas/metacat/dataone/MockCNode.java
37 37
import org.dataone.service.types.v1.Identifier;
38 38
import org.dataone.service.types.v1.NodeReference;
39 39
import org.dataone.service.types.v1.NodeType;
40
import org.dataone.service.types.v1.Service;
41
import org.dataone.service.types.v1.Services;
40 42
import org.dataone.service.types.v1.Session;
41 43
import org.dataone.service.types.v1.Subject;
42 44
import org.dataone.service.types.v1.SubjectInfo;
......
51 53
 * when there is a dependency on CN services
52 54
 */
53 55
public class MockCNode extends MultipartCNode {
56
    
57
    public final static String V1MNNODEID= "urn:node:test_MN-v1";
58
    public final static String V2MNNODEID= "urn:node:test_MN-v2";
54 59

  
55 60
    /**
56 61
     * See superclass for documentation
......
65 70
		NodeList list = new NodeList();
66 71
		list.addNode(getCapabilities());
67 72
		list.addNode(getTestMN());
73
		list.addNode(getTestV1MN());
74
		list.addNode(getTestV2MN());
68 75
		return list;
69 76
	}
70 77
    
......
136 143
        return null;
137 144
    }
138 145
    
146
    @Override
147
    public Node getNodeCapabilities(NodeReference nodeId) throws NotImplemented, ServiceFailure {
148
        if(nodeId != null && nodeId.getValue().equals(V1MNNODEID)) {
149
            return getTestV1MN();
150
        } else if (nodeId != null && nodeId.getValue().equals(V2MNNODEID)) {
151
            return getTestV2MN();
152
        } else {
153
            return getCapabilities();
154
        }
155
    }
156
    
139 157
    /*
140 158
     * Create a test mn in this env.
141 159
     */
......
151 169
        return node;
152 170
    }
153 171
    
172
    
173
    /*
174
     * Create a v1 mn in this env
175
     */
176
    private Node getTestV1MN() {
177
        Node node = new Node();
178
        NodeReference nodeRef = new NodeReference();
179
        nodeRef.setValue(V1MNNODEID);
180
        node.setIdentifier(nodeRef);
181
        Subject subject = new Subject();
182
        subject.setValue("cn=" + V1MNNODEID + ",dc=dataone,dc=org");
183
        node.addSubject(subject );
184
        node.setType(NodeType.MN);
185
        Service service = new Service();
186
        service.setName("MNRead");
187
        service.setVersion("V1");
188
        service.setAvailable(true);
189
        Services services = new Services();
190
        services.addService(service);
191
        node.setServices(services);
192
        return node;
193
    }
194
    
195
    /*
196
     * Create a v2 mn in this env
197
     */
198
    private Node getTestV2MN() {
199
        Node node = new Node();
200
        NodeReference nodeRef = new NodeReference();
201
        nodeRef.setValue(V2MNNODEID);
202
        node.setIdentifier(nodeRef);
203
        Subject subject = new Subject();
204
        subject.setValue("cn=" + V2MNNODEID + ",dc=dataone,dc=org");
205
        node.addSubject(subject);
206
        node.setType(NodeType.MN);
207
        Service service = new Service();
208
        service.setName("MNRead");
209
        service.setVersion("V1");
210
        service.setAvailable(true);
211
        Service service2 = new Service();
212
        service2.setName("MNRead");
213
        service2.setVersion("V2");
214
        service2.setAvailable(true);
215
        Services services = new Services();
216
        services.addService(service);
217
        services.addService(service2);
218
        node.setServices(services);
219
        return node;
220
    }
154 221
}

Also available in: Unified diff