Revision 6755
Added by ben leinfelder almost 13 years ago
test/edu/ucsb/nceas/metacat/dataone/D1NodeServiceTest.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
package edu.ucsb.nceas.metacat.dataone; |
27 | 27 |
|
28 |
import java.io.ByteArrayInputStream; |
|
29 |
import java.io.ByteArrayOutputStream; |
|
30 |
import java.io.IOException; |
|
31 | 28 |
import java.io.InputStream; |
32 | 29 |
import java.io.InputStreamReader; |
33 | 30 |
import java.io.Reader; |
34 | 31 |
import java.math.BigInteger; |
35 |
import java.security.MessageDigest; |
|
36 |
import java.security.NoSuchAlgorithmException; |
|
37 | 32 |
import java.util.Date; |
38 | 33 |
|
39 | 34 |
import org.apache.wicket.protocol.http.MockHttpServletRequest; |
40 | 35 |
import org.dataone.client.ObjectFormatCache; |
41 |
import org.dataone.service.util.Constants; |
|
42 |
import org.dataone.service.util.TypeMarshaller; |
|
43 |
import org.dataone.service.types.v1.util.ObjectFormatServiceImpl; |
|
44 | 36 |
import org.dataone.service.types.v1.AccessPolicy; |
45 | 37 |
import org.dataone.service.types.v1.AccessRule; |
46 | 38 |
import org.dataone.service.types.v1.Checksum; |
47 | 39 |
import org.dataone.service.types.v1.Identifier; |
48 | 40 |
import org.dataone.service.types.v1.NodeReference; |
49 |
import org.dataone.service.types.v1.ObjectFormatList; |
|
50 | 41 |
import org.dataone.service.types.v1.Permission; |
51 | 42 |
import org.dataone.service.types.v1.Session; |
52 | 43 |
import org.dataone.service.types.v1.Subject; |
53 | 44 |
import org.dataone.service.types.v1.SystemMetadata; |
45 |
import org.dataone.service.types.v1.util.ChecksumUtil; |
|
46 |
import org.dataone.service.types.v1.util.ObjectFormatServiceImpl; |
|
47 |
import org.dataone.service.util.Constants; |
|
54 | 48 |
|
55 | 49 |
import edu.ucsb.nceas.MCTestCase; |
56 | 50 |
import edu.ucsb.nceas.metacat.client.Metacat; |
... | ... | |
95 | 89 |
session.setSubject(subject); |
96 | 90 |
return session; |
97 | 91 |
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
92 |
|
|
102 | 93 |
/** |
103 | 94 |
* Run an initial test that always passes to check that the test harness is |
104 | 95 |
* working. |
... | ... | |
108 | 99 |
printTestHeader("initialize"); |
109 | 100 |
assertTrue(1 == 1); |
110 | 101 |
} |
111 |
|
|
112 |
private static String checksum(InputStream is) throws NoSuchAlgorithmException, IOException { |
|
113 |
byte[] buffer = new byte[1024]; |
|
114 |
MessageDigest complete = MessageDigest.getInstance("MD5"); |
|
115 |
int numRead; |
|
116 |
|
|
117 |
do { |
|
118 |
numRead = is.read(buffer); |
|
119 |
if (numRead > 0) { |
|
120 |
complete.update(buffer, 0, numRead); |
|
121 |
} |
|
122 |
} while (numRead != -1); |
|
123 |
|
|
124 |
// rest to beginning -- not all streams support this |
|
125 |
is.reset(); |
|
126 |
|
|
127 |
String csStr = getHex(complete.digest()); |
|
128 |
return csStr; |
|
129 |
} |
|
130 | 102 |
|
131 | 103 |
/** |
132 |
* convert a byte array to a hex string |
|
133 |
*/ |
|
134 |
private static String getHex(byte[] raw) { |
|
135 |
final String HEXES = "0123456789ABCDEF"; |
|
136 |
if (raw == null) { |
|
137 |
return null; |
|
138 |
} |
|
139 |
final StringBuilder hex = new StringBuilder(2 * raw.length); |
|
140 |
for (final byte b : raw) { |
|
141 |
hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F))); |
|
142 |
} |
|
143 |
return hex.toString(); |
|
144 |
} |
|
145 |
|
|
146 |
/** |
|
147 | 104 |
* create system metadata with a specified id |
148 | 105 |
*/ |
149 | 106 |
public SystemMetadata createSystemMetadata(Identifier id, Subject owner, InputStream object) |
... | ... | |
155 | 112 |
sm.setIdentifier(id); |
156 | 113 |
sm.setFormatId(ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFormatId()); |
157 | 114 |
// create the checksum |
158 |
String checksumS = "test"; |
|
115 |
Checksum checksum = new Checksum(); |
|
116 |
String ca = "MD5"; |
|
117 |
checksum.setValue("test"); |
|
118 |
checksum.setAlgorithm(ca); |
|
119 |
// actually generate one |
|
159 | 120 |
if (object != null) { |
160 |
checksumS = checksum(object);
|
|
121 |
checksum = ChecksumUtil.checksum(object, ca);
|
|
161 | 122 |
} |
162 |
String ca = "MD5"; |
|
163 |
Checksum checksum = new Checksum(); |
|
164 |
checksum.setValue(checksumS); |
|
165 |
checksum.setAlgorithm(ca); |
|
166 | 123 |
sm.setChecksum(checksum); |
167 | 124 |
// set the size |
168 | 125 |
sm.setSize(new BigInteger("0")); |
... | ... | |
203 | 160 |
// probably missing the doc |
204 | 161 |
} |
205 | 162 |
if (is == null) { |
206 |
ObjectFormatList formats = ObjectFormatServiceImpl.getInstance().listFormats(); |
|
207 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
208 |
TypeMarshaller.marshalTypeToOutputStream(formats, out); |
|
209 |
Reader xmlDocument = new InputStreamReader(new ByteArrayInputStream(out.toByteArray())); |
|
163 |
// get the default from d1_common |
|
164 |
InputStream formats = ObjectFormatServiceImpl.getInstance().getObjectFormatFile(); |
|
165 |
Reader xmlDocument = new InputStreamReader(formats); |
|
210 | 166 |
m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null); |
211 | 167 |
} |
212 | 168 |
m.logout(); |
Also available in: Unified diff
fix tests:
-insert default objectFormatList from d1_common (includes schemaLocation now)
-calculate checksum for the system metadata using the ChecksumUtil in d1_common