Revision 6324
Added by ben leinfelder over 13 years ago
test/edu/ucsb/nceas/metacat/dataone/MNodeServiceTest.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
package edu.ucsb.nceas.metacat.dataone; |
27 | 27 |
|
28 |
import static org.junit.Assert.*; |
|
29 |
|
|
30 | 28 |
import java.io.ByteArrayInputStream; |
31 | 29 |
import java.io.InputStream; |
32 | 30 |
import java.io.StringBufferInputStream; |
... | ... | |
34 | 32 |
import java.security.MessageDigest; |
35 | 33 |
import java.util.Calendar; |
36 | 34 |
import java.util.Date; |
37 |
import java.util.List; |
|
38 | 35 |
|
39 | 36 |
import junit.framework.Test; |
40 | 37 |
import junit.framework.TestSuite; |
41 | 38 |
|
42 |
import org.dataone.client.ObjectFormatCache;
|
|
39 |
import org.apache.commons.io.IOUtils;
|
|
43 | 40 |
import org.dataone.service.Constants; |
44 | 41 |
import org.dataone.service.exceptions.IdentifierNotUnique; |
45 | 42 |
import org.dataone.service.exceptions.InsufficientResources; |
... | ... | |
54 | 51 |
import org.dataone.service.types.AccessPolicy; |
55 | 52 |
import org.dataone.service.types.AccessRule; |
56 | 53 |
import org.dataone.service.types.Checksum; |
57 |
import org.dataone.service.types.ChecksumAlgorithm; |
|
58 | 54 |
import org.dataone.service.types.DescribeResponse; |
59 | 55 |
import org.dataone.service.types.Event; |
60 | 56 |
import org.dataone.service.types.Identifier; |
61 | 57 |
import org.dataone.service.types.Log; |
62 |
import org.dataone.service.types.NodeReference; |
|
63 | 58 |
import org.dataone.service.types.ObjectFormat; |
64 | 59 |
import org.dataone.service.types.ObjectList; |
65 | 60 |
import org.dataone.service.types.Permission; |
66 | 61 |
import org.dataone.service.types.Session; |
67 | 62 |
import org.dataone.service.types.Subject; |
68 | 63 |
import org.dataone.service.types.SystemMetadata; |
69 |
import org.dataone.service.types.util.ServiceTypeUtil; |
|
70 | 64 |
import org.junit.After; |
71 | 65 |
import org.junit.Before; |
72 | 66 |
|
73 |
import edu.ucsb.nceas.MCTestCase; |
|
74 |
|
|
75 | 67 |
/** |
76 | 68 |
* A JUnit test to exercise the Metacat Member Node service implementation. |
77 | 69 |
* This also tests a few of the D1NodeService superclass methods |
... | ... | |
79 | 71 |
* @author cjones |
80 | 72 |
* |
81 | 73 |
*/ |
82 |
public class MNodeServiceTest extends MCTestCase {
|
|
74 |
public class MNodeServiceTest extends D1NodeServiceTest {
|
|
83 | 75 |
|
84 | 76 |
/** |
85 | 77 |
* Set up the test fixtures |
... | ... | |
94 | 86 |
|
95 | 87 |
/** |
96 | 88 |
* Remove the test fixtures |
97 |
* |
|
98 |
* @throws Exception |
|
99 | 89 |
*/ |
100 | 90 |
@After |
101 |
public void tearDown() throws Exception {
|
|
91 |
public void tearDown() { |
|
102 | 92 |
} |
103 | 93 |
|
104 | 94 |
/** |
... | ... | |
163 | 153 |
Session session = getTestSession(); |
164 | 154 |
Identifier guid = new Identifier(); |
165 | 155 |
guid.setValue("testGet." + System.currentTimeMillis()); |
166 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
167 | 156 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
157 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
168 | 158 |
Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta); |
169 | 159 |
InputStream result = MNodeService.getInstance().get(session, guid); |
170 |
assertEquals(result, object); |
|
160 |
// go back to beginning of original stream |
|
161 |
object.reset(); |
|
162 |
// check |
|
163 |
assertTrue(object.available() > 0); |
|
164 |
assertTrue(result.available() > 0); |
|
165 |
assertTrue(IOUtils.contentEquals(result, object)); |
|
171 | 166 |
|
172 | 167 |
} catch (UnsupportedEncodingException e) { |
173 | 168 |
e.printStackTrace(); |
... | ... | |
231 | 226 |
Session session = getTestSession(); |
232 | 227 |
Identifier guid = new Identifier(); |
233 | 228 |
guid.setValue("testGetSystemMetadata." + System.currentTimeMillis()); |
234 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
235 | 229 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
230 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
236 | 231 |
Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta); |
237 | 232 |
SystemMetadata newsysmeta = MNodeService.getInstance().getSystemMetadata(session, pid); |
238 |
assertEquals(newsysmeta, sysmeta);
|
|
233 |
assertEquals(newsysmeta.getIdentifier().getValue(), sysmeta.getIdentifier().getValue());
|
|
239 | 234 |
|
240 | 235 |
} catch (UnsupportedEncodingException e) { |
241 | 236 |
e.printStackTrace(); |
... | ... | |
299 | 294 |
Session session = getTestSession(); |
300 | 295 |
Identifier guid = new Identifier(); |
301 | 296 |
guid.setValue("testCreate." + System.currentTimeMillis()); |
302 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
303 | 297 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
298 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
304 | 299 |
Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta); |
305 | 300 |
assertEquals(guid, pid); |
306 | 301 |
} catch (UnsupportedEncodingException e) { |
... | ... | |
361 | 356 |
Session session = getTestSession(); |
362 | 357 |
Identifier guid = new Identifier(); |
363 | 358 |
guid.setValue("testDelete." + System.currentTimeMillis()); |
364 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
365 | 359 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
360 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
366 | 361 |
Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta); |
367 | 362 |
Identifier deletedPid = MNodeService.getInstance().delete(session, pid); |
368 | 363 |
assertEquals(pid, deletedPid); |
... | ... | |
370 | 365 |
} catch (UnsupportedEncodingException e) { |
371 | 366 |
e.printStackTrace(); |
372 | 367 |
|
373 |
} catch (InvalidToken e) {
|
|
368 |
} catch (Exception e) {
|
|
374 | 369 |
e.printStackTrace(); |
375 |
|
|
376 |
} catch (ServiceFailure e) { |
|
377 |
e.printStackTrace(); |
|
378 | 370 |
fail("Unexpected error: " + e.getMessage()); |
379 | 371 |
|
380 |
} catch (NotAuthorized e) { |
|
381 |
e.printStackTrace(); |
|
382 |
fail("Unexpected error: " + e.getMessage()); |
|
383 |
|
|
384 |
} catch (IdentifierNotUnique e) { |
|
385 |
e.printStackTrace(); |
|
386 |
fail("Unexpected error: " + e.getMessage()); |
|
387 |
|
|
388 |
} catch (UnsupportedType e) { |
|
389 |
e.printStackTrace(); |
|
390 |
fail("Unexpected error: " + e.getMessage()); |
|
391 |
|
|
392 |
} catch (InsufficientResources e) { |
|
393 |
e.printStackTrace(); |
|
394 |
fail("Unexpected error: " + e.getMessage()); |
|
395 |
|
|
396 |
} catch (InvalidSystemMetadata e) { |
|
397 |
e.printStackTrace(); |
|
398 |
fail("Unexpected error: " + e.getMessage()); |
|
399 |
|
|
400 |
} catch (NotImplemented e) { |
|
401 |
e.printStackTrace(); |
|
402 |
fail("Unexpected error: " + e.getMessage()); |
|
403 |
|
|
404 |
} catch (InvalidRequest e) { |
|
405 |
e.printStackTrace(); |
|
406 |
fail("Unexpected error: " + e.getMessage()); |
|
407 |
|
|
408 |
} catch (NotFound e) { |
|
409 |
e.printStackTrace(); |
|
410 |
fail("Unexpected error: " + e.getMessage()); |
|
411 |
|
|
412 |
} |
|
372 |
} |
|
413 | 373 |
|
414 | 374 |
} |
415 | 375 |
|
... | ... | |
423 | 383 |
Session session = getTestSession(); |
424 | 384 |
Identifier guid = new Identifier(); |
425 | 385 |
guid.setValue("testUpdate." + System.currentTimeMillis()); |
426 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
427 | 386 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
387 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
428 | 388 |
Identifier newPid = new Identifier(); |
429 | 389 |
newPid.setValue("testUpdate." + System.currentTimeMillis()); |
430 | 390 |
Identifier pid = |
... | ... | |
433 | 393 |
|
434 | 394 |
Identifier updatedPid = |
435 | 395 |
MNodeService.getInstance().update(session, pid, object, newPid, sysmeta); |
436 |
assertEquals(updatedPid, newPid);
|
|
396 |
assertEquals(updatedPid.getValue(), newPid.getValue());
|
|
437 | 397 |
|
438 | 398 |
} catch (UnsupportedEncodingException e) { |
439 | 399 |
e.printStackTrace(); |
... | ... | |
496 | 456 |
Session session = getTestSession(); |
497 | 457 |
Identifier guid = new Identifier(); |
498 | 458 |
guid.setValue("testGetSystemMetadata." + System.currentTimeMillis()); |
499 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
500 | 459 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
460 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
501 | 461 |
Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta); |
502 | 462 |
DescribeResponse describeResponse = MNodeService.getInstance().describe(session, pid); |
503 |
assertEquals(describeResponse.getDataONE_Checksum(), sysmeta.getChecksum());
|
|
504 |
assertEquals(describeResponse.getDataONE_ObjectFormat(), sysmeta.getObjectFormat());
|
|
463 |
assertEquals(describeResponse.getDataONE_Checksum().getValue(), sysmeta.getChecksum().getValue());
|
|
464 |
assertEquals(describeResponse.getDataONE_ObjectFormat().getFormatName(), sysmeta.getObjectFormat().getFormatName());
|
|
505 | 465 |
|
506 | 466 |
} catch (UnsupportedEncodingException e) { |
507 | 467 |
e.printStackTrace(); |
... | ... | |
564 | 524 |
Session session = getTestSession(); |
565 | 525 |
Identifier guid = new Identifier(); |
566 | 526 |
guid.setValue("testGetChecksum." + System.currentTimeMillis()); |
567 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
568 | 527 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
528 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
569 | 529 |
Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta); |
570 | 530 |
Checksum checksum = MNodeService.getInstance().getChecksum(session, pid, "MD5"); |
571 | 531 |
assertEquals(checksum.getValue(), sysmeta.getChecksum().getValue()); |
... | ... | |
727 | 687 |
fromDate = calendar.getTime(); |
728 | 688 |
Date toDate = new Date(); |
729 | 689 |
Event event = Event.CREATE; |
730 |
int start = 2;
|
|
731 |
int count = 500;
|
|
690 |
int start = 0;
|
|
691 |
int count = 1;
|
|
732 | 692 |
|
733 | 693 |
try { |
734 | 694 |
log = MNodeService.getInstance().getLogRecords(session, fromDate, toDate, |
735 | 695 |
event, start, count); |
736 | 696 |
|
737 | 697 |
assertNotNull(log); |
738 |
if ( log != null ) { |
|
739 |
assertTrue(log.getCount() == 500); |
|
740 |
assertTrue(log.getStart() == 1); |
|
741 |
assertTrue(log.getTotal() >= 1); |
|
698 |
assertTrue(log.getCount() == count); |
|
699 |
assertTrue(log.getStart() == start); |
|
700 |
assertTrue(log.getTotal() >= 1); |
|
742 | 701 |
|
743 |
} |
|
744 | 702 |
} catch (InvalidToken e) { |
745 | 703 |
e.printStackTrace(); |
746 | 704 |
fail("Unexpected error: " + e.getMessage()); |
... | ... | |
777 | 735 |
Session session = getTestSession(); |
778 | 736 |
Identifier guid = new Identifier(); |
779 | 737 |
guid.setValue("testSetAccess." + System.currentTimeMillis()); |
780 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
781 | 738 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
739 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
782 | 740 |
Identifier pid = |
783 | 741 |
MNodeService.getInstance().create(session, guid, object, sysmeta); |
784 | 742 |
// set the access |
... | ... | |
855 | 813 |
Session session = getTestSession(); |
856 | 814 |
Identifier guid = new Identifier(); |
857 | 815 |
guid.setValue("testIsAuthorized." + System.currentTimeMillis()); |
858 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
859 | 816 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
817 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
860 | 818 |
Identifier pid = |
861 | 819 |
MNodeService.getInstance().create(session, guid, object, sysmeta); |
862 | 820 |
boolean isAuthorized = |
... | ... | |
910 | 868 |
} |
911 | 869 |
} |
912 | 870 |
|
913 |
/** |
|
914 |
* print a header to start each test |
|
915 |
*/ |
|
916 |
private void printTestHeader(String testName) |
|
917 |
{ |
|
918 |
System.out.println(); |
|
919 |
System.out.println("*************** " + testName + " ***************"); |
|
920 |
} |
|
921 | 871 |
|
922 |
/** |
|
923 |
* produce an md5 checksum for item |
|
924 |
*/ |
|
925 |
private String checksum(String item) |
|
926 |
throws Exception { |
|
927 |
StringBufferInputStream fis = new StringBufferInputStream(item); |
|
928 |
|
|
929 |
byte[] buffer = new byte[1024]; |
|
930 |
MessageDigest complete = MessageDigest.getInstance("MD5"); |
|
931 |
int numRead; |
|
932 |
|
|
933 |
do { |
|
934 |
numRead = fis.read(buffer); |
|
935 |
if (numRead > 0) { |
|
936 |
complete.update(buffer, 0, numRead); |
|
937 |
} |
|
938 |
} while (numRead != -1); |
|
939 |
|
|
940 |
return getHex(complete.digest()); |
|
941 |
} |
|
942 | 872 |
|
943 |
/** |
|
944 |
* convert a byte array to a hex string |
|
945 |
*/ |
|
946 |
private static String getHex( byte [] raw ) { |
|
947 |
final String HEXES = "0123456789ABCDEF"; |
|
948 |
if ( raw == null ) { |
|
949 |
return null; |
|
950 |
} |
|
951 |
final StringBuilder hex = new StringBuilder( 2 * raw.length ); |
|
952 |
for ( final byte b : raw ) { |
|
953 |
hex.append(HEXES.charAt((b & 0xF0) >> 4)) |
|
954 |
.append(HEXES.charAt((b & 0x0F))); |
|
955 |
} |
|
956 |
return hex.toString(); |
|
957 |
} |
|
958 | 873 |
|
959 |
/** |
|
960 |
* constructs a "fake" session with a test subject |
|
961 |
* @return |
|
962 |
*/ |
|
963 |
private Session getTestSession() { |
|
964 |
Session session = new Session(); |
|
965 |
Subject subject = new Subject(); |
|
966 |
subject.setValue("cn=test,dc=dataone,dc=org"); |
|
967 |
session.setSubject(subject); |
|
968 |
return session; |
|
969 |
} |
|
970 |
|
|
971 |
/** |
|
972 |
* create system metadata with a specified id |
|
973 |
* @throws NotFound |
|
974 |
*/ |
|
975 |
private SystemMetadata createSystemMetadata(Identifier id, Subject owner) |
|
976 |
throws NotFound { |
|
977 |
SystemMetadata sm = new SystemMetadata(); |
|
978 |
// set the id |
|
979 |
sm.setIdentifier(id); |
|
980 |
sm.setObjectFormat(ObjectFormatCache.getInstance().getFormat("application/octet-stream")); |
|
981 |
// create the checksum |
|
982 |
String checksumS = "test"; |
|
983 |
ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5"); |
|
984 |
Checksum checksum = new Checksum(); |
|
985 |
checksum.setValue(checksumS); |
|
986 |
checksum.setAlgorithm(ca); |
|
987 |
sm.setChecksum(checksum); |
|
988 |
// set the size |
|
989 |
sm.setSize(0); |
|
990 |
sm.setSubmitter(owner); |
|
991 |
sm.setRightsHolder(owner); |
|
992 |
sm.setDateUploaded(new Date()); |
|
993 |
sm.setDateSysMetadataModified(new Date()); |
|
994 |
NodeReference nr = new NodeReference(); |
|
995 |
nr.setValue("metacat"); |
|
996 |
sm.setOriginMemberNode(nr); |
|
997 |
sm.setAuthoritativeMemberNode(nr); |
|
998 |
|
|
999 |
AccessPolicy accessPolicy = new AccessPolicy(); |
|
1000 |
|
|
1001 |
// allow delete for the owner (is this needed?) |
|
1002 |
AccessRule deleteRule = new AccessRule(); |
|
1003 |
deleteRule.addPermission(Permission.CHANGE_PERMISSION); |
|
1004 |
deleteRule.addSubject(owner); |
|
1005 |
deleteRule.addPermission(Permission.READ); |
|
1006 |
|
|
1007 |
// set the access to public read |
|
1008 |
AccessRule allow = new AccessRule(); |
|
1009 |
Subject publicSubject = new Subject(); |
|
1010 |
publicSubject.setValue(Constants.PUBLIC_SUBJECT); |
|
1011 |
allow.addSubject(publicSubject); |
|
1012 |
accessPolicy.addAllow(allow); |
|
1013 |
sm.setAccessPolicy(accessPolicy); |
|
1014 |
|
|
1015 |
return sm; |
|
1016 |
} |
|
1017 |
|
|
1018 | 874 |
} |
test/edu/ucsb/nceas/metacat/dataone/CNodeServiceTest.java | ||
---|---|---|
26 | 26 |
package edu.ucsb.nceas.metacat.dataone; |
27 | 27 |
|
28 | 28 |
import java.io.ByteArrayInputStream; |
29 |
import java.io.ByteArrayOutputStream; |
|
30 | 29 |
import java.io.InputStream; |
31 |
import java.io.InputStreamReader; |
|
32 |
import java.io.Reader; |
|
33 | 30 |
import java.text.SimpleDateFormat; |
34 | 31 |
import java.util.Date; |
35 | 32 |
|
... | ... | |
37 | 34 |
import junit.framework.TestSuite; |
38 | 35 |
|
39 | 36 |
import org.apache.commons.io.IOUtils; |
40 |
import org.dataone.client.ObjectFormatCache; |
|
41 | 37 |
import org.dataone.service.Constants; |
42 | 38 |
import org.dataone.service.exceptions.InsufficientResources; |
43 | 39 |
import org.dataone.service.exceptions.InvalidRequest; |
... | ... | |
45 | 41 |
import org.dataone.service.exceptions.NotFound; |
46 | 42 |
import org.dataone.service.exceptions.NotImplemented; |
47 | 43 |
import org.dataone.service.exceptions.ServiceFailure; |
48 |
import org.dataone.service.impl.ObjectFormatServiceImpl; |
|
49 | 44 |
import org.dataone.service.types.AccessPolicy; |
50 | 45 |
import org.dataone.service.types.AccessRule; |
51 | 46 |
import org.dataone.service.types.Checksum; |
52 |
import org.dataone.service.types.ChecksumAlgorithm; |
|
53 | 47 |
import org.dataone.service.types.Event; |
54 | 48 |
import org.dataone.service.types.Identifier; |
55 | 49 |
import org.dataone.service.types.Log; |
... | ... | |
66 | 60 |
import org.dataone.service.types.Session; |
67 | 61 |
import org.dataone.service.types.Subject; |
68 | 62 |
import org.dataone.service.types.SystemMetadata; |
69 |
import org.dataone.service.types.util.ServiceTypeUtil; |
|
70 | 63 |
|
71 |
import edu.ucsb.nceas.MCTestCase; |
|
72 |
import edu.ucsb.nceas.metacat.client.Metacat; |
|
73 |
import edu.ucsb.nceas.metacat.client.MetacatFactory; |
|
74 |
|
|
75 | 64 |
/** |
76 | 65 |
* A JUnit test for testing the dataone CNCore implementation |
77 | 66 |
*/ |
78 |
public class CNodeServiceTest extends MCTestCase {
|
|
67 |
public class CNodeServiceTest extends D1NodeServiceTest {
|
|
79 | 68 |
|
80 | 69 |
/** |
81 | 70 |
* constructor for the test |
... | ... | |
84 | 73 |
{ |
85 | 74 |
super(name); |
86 | 75 |
} |
87 |
|
|
88 |
/** |
|
89 |
* Establish a testing framework by initializing appropriate objects |
|
90 |
*/ |
|
91 |
public void setUp() throws Exception |
|
92 |
{ |
|
93 |
super.setUp(); |
|
94 |
} |
|
95 | 76 |
|
96 | 77 |
/** |
97 |
* Release any objects after tests are complete |
|
98 |
*/ |
|
99 |
public void tearDown() |
|
100 |
{ |
|
101 |
} |
|
102 |
|
|
103 |
/** |
|
104 | 78 |
* Create a suite of tests to be run together |
105 | 79 |
*/ |
106 | 80 |
public static Test suite() |
... | ... | |
130 | 104 |
return suite; |
131 | 105 |
} |
132 | 106 |
|
133 |
/** |
|
134 |
* constructs a "fake" session with a test subject |
|
135 |
* @return |
|
136 |
*/ |
|
137 |
private Session getTestSession() { |
|
138 |
Session session = new Session(); |
|
139 |
Subject subject = new Subject(); |
|
140 |
subject.setValue("cn=test,dc=dataone,dc=org"); |
|
141 |
session.setSubject(subject); |
|
142 |
return session; |
|
143 |
} |
|
144 | 107 |
|
145 | 108 |
/** |
146 | 109 |
* test for registering standalone system metadata |
... | ... | |
152 | 115 |
Session session = getTestSession(); |
153 | 116 |
Identifier guid = new Identifier(); |
154 | 117 |
guid.setValue("testRegisterSystemMetadata." + System.currentTimeMillis()); |
155 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
118 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
|
156 | 119 |
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta); |
157 | 120 |
assertTrue(result); |
158 | 121 |
} catch(Exception e) { |
... | ... | |
170 | 133 |
Session session = getTestSession(); |
171 | 134 |
Identifier guid = new Identifier(); |
172 | 135 |
guid.setValue("testGetSystemMetadata." + System.currentTimeMillis()); |
173 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
136 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
|
174 | 137 |
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta); |
175 | 138 |
assertTrue(result); |
176 | 139 |
// get it |
... | ... | |
213 | 176 |
Session session = getTestSession(); |
214 | 177 |
Identifier guid = new Identifier(); |
215 | 178 |
guid.setValue("testCreate." + System.currentTimeMillis()); |
216 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
217 | 179 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
180 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
218 | 181 |
Identifier pid = CNodeService.getInstance().create(session, guid, object, sysmeta); |
219 | 182 |
assertEquals(guid, pid); |
220 | 183 |
} catch(Exception e) { |
... | ... | |
230 | 193 |
Session session = getTestSession(); |
231 | 194 |
Identifier guid = new Identifier(); |
232 | 195 |
guid.setValue("testGet." + System.currentTimeMillis()); |
233 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
234 | 196 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
197 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
235 | 198 |
Identifier pid = CNodeService.getInstance().create(session, guid, object, sysmeta); |
236 | 199 |
assertEquals(guid, pid); |
237 | 200 |
// get it |
... | ... | |
251 | 214 |
Session session = getTestSession(); |
252 | 215 |
Identifier guid = new Identifier(); |
253 | 216 |
guid.setValue("testAssertRelation." + System.currentTimeMillis()); |
254 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
217 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
|
255 | 218 |
Identifier describePid = new Identifier(); |
256 | 219 |
describePid.setValue("describePid." + System.currentTimeMillis()); |
257 | 220 |
sysmeta.addDescribe(describePid); |
... | ... | |
259 | 222 |
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta); |
260 | 223 |
assertTrue(result); |
261 | 224 |
// save the other |
262 |
SystemMetadata describeSysmeta = createSystemMetadata(describePid, session.getSubject()); |
|
225 |
SystemMetadata describeSysmeta = createSystemMetadata(describePid, session.getSubject(), null);
|
|
263 | 226 |
result = CNodeService.getInstance().registerSystemMetadata(session, describePid, describeSysmeta); |
264 | 227 |
assertTrue(result); |
265 | 228 |
// check it |
... | ... | |
278 | 241 |
Session session = getTestSession(); |
279 | 242 |
Identifier guid = new Identifier(); |
280 | 243 |
guid.setValue("testChecksum." + System.currentTimeMillis()); |
281 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
244 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
|
282 | 245 |
// save it |
283 | 246 |
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta); |
284 | 247 |
assertTrue(result); |
... | ... | |
326 | 289 |
Session session = getTestSession(); |
327 | 290 |
Identifier guid = new Identifier(); |
328 | 291 |
guid.setValue("testSearch." + System.currentTimeMillis()); |
329 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
292 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
|
330 | 293 |
|
331 | 294 |
// save it |
332 | 295 |
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta); |
... | ... | |
356 | 319 |
Session session = getTestSession(); |
357 | 320 |
Identifier guid = new Identifier(); |
358 | 321 |
guid.setValue("testSetOwner." + System.currentTimeMillis()); |
359 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
322 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
|
360 | 323 |
// save it |
361 | 324 |
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta); |
362 | 325 |
assertTrue(result); |
... | ... | |
383 | 346 |
Session session = getTestSession(); |
384 | 347 |
Identifier guid = new Identifier(); |
385 | 348 |
guid.setValue("testSetAccessPolicy." + System.currentTimeMillis()); |
386 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
349 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
|
387 | 350 |
// save it |
388 | 351 |
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta); |
389 | 352 |
assertTrue(result); |
... | ... | |
412 | 375 |
Session session = getTestSession(); |
413 | 376 |
Identifier guid = new Identifier(); |
414 | 377 |
guid.setValue("testIsAuthorized." + System.currentTimeMillis()); |
415 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
378 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
|
416 | 379 |
// save it |
417 | 380 |
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta); |
418 | 381 |
assertTrue(result); |
... | ... | |
444 | 407 |
Session session = getTestSession(); |
445 | 408 |
Identifier guid = new Identifier(); |
446 | 409 |
guid.setValue("testReplicationPolicy." + System.currentTimeMillis()); |
447 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
410 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
|
448 | 411 |
// save it |
449 | 412 |
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta); |
450 | 413 |
assertTrue(result); |
... | ... | |
474 | 437 |
Session session = getTestSession(); |
475 | 438 |
Identifier guid = new Identifier(); |
476 | 439 |
guid.setValue("testReplicationStatus." + System.currentTimeMillis()); |
477 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject()); |
|
440 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
|
478 | 441 |
Replica replica = new Replica(); |
479 | 442 |
NodeReference replicaMemberNode = new NodeReference(); |
480 | 443 |
replicaMemberNode.setValue("testNode"); |
... | ... | |
509 | 472 |
printTestHeader("initialize"); |
510 | 473 |
assertTrue(1 == 1); |
511 | 474 |
} |
512 |
|
|
513 |
/** |
|
514 |
* create system metadata with a specified id |
|
515 |
*/ |
|
516 |
private SystemMetadata createSystemMetadata(Identifier id, Subject owner) |
|
517 |
throws Exception |
|
518 |
{ |
|
519 |
SystemMetadata sm = new SystemMetadata(); |
|
520 |
// set the id |
|
521 |
sm.setIdentifier(id); |
|
522 |
sm.setObjectFormat(ObjectFormatCache.getInstance().getFormat("application/octet-stream")); |
|
523 |
// create the checksum |
|
524 |
String checksumS = "test"; |
|
525 |
ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5"); |
|
526 |
Checksum checksum = new Checksum(); |
|
527 |
checksum.setValue(checksumS); |
|
528 |
checksum.setAlgorithm(ca); |
|
529 |
sm.setChecksum(checksum); |
|
530 |
// set the size |
|
531 |
sm.setSize(0); |
|
532 |
sm.setSubmitter(owner); |
|
533 |
sm.setRightsHolder(owner); |
|
534 |
sm.setDateUploaded(new Date()); |
|
535 |
sm.setDateSysMetadataModified(new Date()); |
|
536 |
NodeReference nr = new NodeReference(); |
|
537 |
nr.setValue("metacat"); |
|
538 |
sm.setOriginMemberNode(nr); |
|
539 |
sm.setAuthoritativeMemberNode(nr); |
|
540 |
// set the access to public read |
|
541 |
AccessPolicy accessPolicy = new AccessPolicy(); |
|
542 |
AccessRule allow = new AccessRule(); |
|
543 |
allow.addPermission(Permission.READ); |
|
544 |
Subject publicSubject = new Subject(); |
|
545 |
publicSubject.setValue(Constants.PUBLIC_SUBJECT); |
|
546 |
allow.addSubject(publicSubject); |
|
547 |
accessPolicy.addAllow(allow); |
|
548 |
sm.setAccessPolicy(accessPolicy); |
|
549 |
|
|
550 |
return sm; |
|
551 |
} |
|
552 | 475 |
|
553 |
/** |
|
554 |
* For fresh Metacat installations without the Object Format List |
|
555 |
* we insert the default version from d1_common.jar |
|
556 |
*/ |
|
557 |
private void setUpFormats() { |
|
558 |
try { |
|
559 |
Metacat m = MetacatFactory.createMetacatConnection(metacatUrl); |
|
560 |
m.login(username, password); |
|
561 |
// check if it exists already |
|
562 |
InputStream is = null; |
|
563 |
try { |
|
564 |
is = m.read(ObjectFormatService.OBJECT_FORMAT_DOCID); |
|
565 |
} catch (Exception e) { |
|
566 |
// probably missing the doc |
|
567 |
} |
|
568 |
if (is == null) { |
|
569 |
ObjectFormatList formats = ObjectFormatServiceImpl.getInstance().listFormats(); |
|
570 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
571 |
ServiceTypeUtil.serializeServiceType(ObjectFormatList.class, formats, out); |
|
572 |
Reader xmlDocument = new InputStreamReader(new ByteArrayInputStream(out.toByteArray())); |
|
573 |
m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null); |
|
574 |
} |
|
575 |
m.logout(); |
|
576 |
} catch (Exception e) { |
|
577 |
// any number of things could go wrong |
|
578 |
e.printStackTrace(); |
|
579 |
} |
|
580 |
} |
|
581 | 476 |
|
582 | 477 |
/** |
583 | 478 |
* test to list the object formats registered in metacat |
... | ... | |
682 | 577 |
} |
683 | 578 |
|
684 | 579 |
} |
685 |
|
|
686 |
/** |
|
687 |
* print a header to start each test |
|
688 |
*/ |
|
689 |
private void printTestHeader(String testName) |
|
690 |
{ |
|
691 |
System.out.println(); |
|
692 |
System.out.println("*************** " + testName + " ***************"); |
|
693 |
} |
|
694 | 580 |
|
695 | 581 |
} |
test/edu/ucsb/nceas/metacat/dataone/D1NodeServiceTest.java | ||
---|---|---|
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$' |
|
8 |
* '$Date$' |
|
9 |
* '$Revision$' |
|
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.ByteArrayOutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.io.InputStreamReader; |
|
33 |
import java.io.Reader; |
|
34 |
import java.security.MessageDigest; |
|
35 |
import java.security.NoSuchAlgorithmException; |
|
36 |
import java.util.Date; |
|
37 |
|
|
38 |
import org.apache.commons.io.IOUtils; |
|
39 |
import org.dataone.client.ObjectFormatCache; |
|
40 |
import org.dataone.service.Constants; |
|
41 |
import org.dataone.service.impl.ObjectFormatServiceImpl; |
|
42 |
import org.dataone.service.types.AccessPolicy; |
|
43 |
import org.dataone.service.types.AccessRule; |
|
44 |
import org.dataone.service.types.Checksum; |
|
45 |
import org.dataone.service.types.ChecksumAlgorithm; |
|
46 |
import org.dataone.service.types.Identifier; |
|
47 |
import org.dataone.service.types.NodeReference; |
|
48 |
import org.dataone.service.types.ObjectFormatList; |
|
49 |
import org.dataone.service.types.Permission; |
|
50 |
import org.dataone.service.types.Session; |
|
51 |
import org.dataone.service.types.Subject; |
|
52 |
import org.dataone.service.types.SystemMetadata; |
|
53 |
import org.dataone.service.types.util.ServiceTypeUtil; |
|
54 |
|
|
55 |
import edu.ucsb.nceas.MCTestCase; |
|
56 |
import edu.ucsb.nceas.metacat.client.Metacat; |
|
57 |
import edu.ucsb.nceas.metacat.client.MetacatFactory; |
|
58 |
|
|
59 |
/** |
|
60 |
* A JUnit superclass for testing the dataone Node implementations |
|
61 |
*/ |
|
62 |
public class D1NodeServiceTest extends MCTestCase { |
|
63 |
|
|
64 |
/** |
|
65 |
* constructor for the test |
|
66 |
*/ |
|
67 |
public D1NodeServiceTest(String name) { |
|
68 |
super(name); |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* Establish a testing framework by initializing appropriate objects |
|
73 |
*/ |
|
74 |
public void setUp() throws Exception { |
|
75 |
super.setUp(); |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* Release any objects after tests are complete |
|
80 |
*/ |
|
81 |
public void tearDown() {} |
|
82 |
|
|
83 |
/** |
|
84 |
* constructs a "fake" session with a test subject |
|
85 |
* @return |
|
86 |
*/ |
|
87 |
protected Session getTestSession() { |
|
88 |
Session session = new Session(); |
|
89 |
Subject subject = new Subject(); |
|
90 |
subject.setValue("cn=test,dc=dataone,dc=org"); |
|
91 |
session.setSubject(subject); |
|
92 |
return session; |
|
93 |
} |
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
/** |
|
99 |
* Run an initial test that always passes to check that the test harness is |
|
100 |
* working. |
|
101 |
*/ |
|
102 |
public void initialize() |
|
103 |
{ |
|
104 |
printTestHeader("initialize"); |
|
105 |
assertTrue(1 == 1); |
|
106 |
} |
|
107 |
|
|
108 |
private static String checksum(InputStream is) throws NoSuchAlgorithmException, IOException { |
|
109 |
byte[] buffer = new byte[1024]; |
|
110 |
MessageDigest complete = MessageDigest.getInstance("MD5"); |
|
111 |
int numRead; |
|
112 |
|
|
113 |
do { |
|
114 |
numRead = is.read(buffer); |
|
115 |
if (numRead > 0) { |
|
116 |
complete.update(buffer, 0, numRead); |
|
117 |
} |
|
118 |
} while (numRead != -1); |
|
119 |
|
|
120 |
// rest to beginning -- not all streams support this |
|
121 |
is.reset(); |
|
122 |
|
|
123 |
String csStr = getHex(complete.digest()); |
|
124 |
return csStr; |
|
125 |
} |
|
126 |
|
|
127 |
/** |
|
128 |
* convert a byte array to a hex string |
|
129 |
*/ |
|
130 |
private static String getHex(byte[] raw) { |
|
131 |
final String HEXES = "0123456789ABCDEF"; |
|
132 |
if (raw == null) { |
|
133 |
return null; |
|
134 |
} |
|
135 |
final StringBuilder hex = new StringBuilder(2 * raw.length); |
|
136 |
for (final byte b : raw) { |
|
137 |
hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F))); |
|
138 |
} |
|
139 |
return hex.toString(); |
|
140 |
} |
|
141 |
|
|
142 |
/** |
|
143 |
* create system metadata with a specified id |
|
144 |
*/ |
|
145 |
protected SystemMetadata createSystemMetadata(Identifier id, Subject owner, InputStream object) |
|
146 |
throws Exception |
|
147 |
{ |
|
148 |
SystemMetadata sm = new SystemMetadata(); |
|
149 |
// set the id |
|
150 |
sm.setIdentifier(id); |
|
151 |
sm.setObjectFormat(ObjectFormatCache.getInstance().getFormat("application/octet-stream")); |
|
152 |
// create the checksum |
|
153 |
String checksumS = checksum(object); |
|
154 |
ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5"); |
|
155 |
Checksum checksum = new Checksum(); |
|
156 |
checksum.setValue(checksumS); |
|
157 |
checksum.setAlgorithm(ca); |
|
158 |
sm.setChecksum(checksum); |
|
159 |
// set the size |
|
160 |
sm.setSize(0); |
|
161 |
sm.setSubmitter(owner); |
|
162 |
sm.setRightsHolder(owner); |
|
163 |
sm.setDateUploaded(new Date()); |
|
164 |
sm.setDateSysMetadataModified(new Date()); |
|
165 |
NodeReference nr = new NodeReference(); |
|
166 |
nr.setValue("metacat"); |
|
167 |
sm.setOriginMemberNode(nr); |
|
168 |
sm.setAuthoritativeMemberNode(nr); |
|
169 |
// set the access to public read |
|
170 |
AccessPolicy accessPolicy = new AccessPolicy(); |
|
171 |
AccessRule allow = new AccessRule(); |
|
172 |
allow.addPermission(Permission.READ); |
|
173 |
Subject publicSubject = new Subject(); |
|
174 |
publicSubject.setValue(Constants.PUBLIC_SUBJECT); |
|
175 |
allow.addSubject(publicSubject); |
|
176 |
accessPolicy.addAllow(allow); |
|
177 |
sm.setAccessPolicy(accessPolicy); |
|
178 |
|
|
179 |
return sm; |
|
180 |
} |
|
181 |
|
|
182 |
/** |
|
183 |
* For fresh Metacat installations without the Object Format List |
|
184 |
* we insert the default version from d1_common.jar |
|
185 |
*/ |
|
186 |
protected void setUpFormats() { |
|
187 |
try { |
|
188 |
Metacat m = MetacatFactory.createMetacatConnection(metacatUrl); |
|
189 |
m.login(username, password); |
|
190 |
// check if it exists already |
|
191 |
InputStream is = null; |
|
192 |
try { |
|
193 |
is = m.read(ObjectFormatService.OBJECT_FORMAT_DOCID); |
|
194 |
} catch (Exception e) { |
|
195 |
// probably missing the doc |
|
196 |
} |
|
197 |
if (is == null) { |
|
198 |
ObjectFormatList formats = ObjectFormatServiceImpl.getInstance().listFormats(); |
|
199 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
200 |
ServiceTypeUtil.serializeServiceType(ObjectFormatList.class, formats, out); |
|
201 |
Reader xmlDocument = new InputStreamReader(new ByteArrayInputStream(out.toByteArray())); |
|
202 |
m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null); |
|
203 |
} |
|
204 |
m.logout(); |
|
205 |
} catch (Exception e) { |
|
206 |
// any number of things could go wrong |
|
207 |
e.printStackTrace(); |
|
208 |
} |
|
209 |
} |
|
210 |
|
|
211 |
/** |
|
212 |
* print a header to start each test |
|
213 |
*/ |
|
214 |
protected void printTestHeader(String testName) |
|
215 |
{ |
|
216 |
System.out.println(); |
|
217 |
System.out.println("*************** " + testName + " ***************"); |
|
218 |
} |
|
219 |
|
|
220 |
} |
|
0 | 221 |
Also available in: Unified diff
refactor to use common superclass D1NodeServiceTest for shared methods