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-10-27 17:51:48 -0700 (Thu, 27 Oct 2011) $'
|
9
|
* '$Revision: 6561 $'
|
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.math.BigInteger;
|
35
|
import java.security.MessageDigest;
|
36
|
import java.security.NoSuchAlgorithmException;
|
37
|
import java.util.Date;
|
38
|
|
39
|
import org.apache.wicket.protocol.http.MockHttpServletRequest;
|
40
|
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
|
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.Identifier;
|
48
|
import org.dataone.service.types.v1.NodeReference;
|
49
|
import org.dataone.service.types.v1.ObjectFormatList;
|
50
|
import org.dataone.service.types.v1.Permission;
|
51
|
import org.dataone.service.types.v1.Session;
|
52
|
import org.dataone.service.types.v1.Subject;
|
53
|
import org.dataone.service.types.v1.SystemMetadata;
|
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
|
protected MockHttpServletRequest request;
|
65
|
|
66
|
/**
|
67
|
* constructor for the test
|
68
|
*/
|
69
|
public D1NodeServiceTest(String name) {
|
70
|
super(name);
|
71
|
// set up the fake request (for logging)
|
72
|
request = new MockHttpServletRequest(null, null, null);
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Establish a testing framework by initializing appropriate objects
|
77
|
*/
|
78
|
public void setUp() throws Exception {
|
79
|
super.setUp();
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Release any objects after tests are complete
|
84
|
*/
|
85
|
public void tearDown() {}
|
86
|
|
87
|
/**
|
88
|
* constructs a "fake" session with a test subject
|
89
|
* @return
|
90
|
*/
|
91
|
public Session getTestSession() {
|
92
|
Session session = new Session();
|
93
|
Subject subject = new Subject();
|
94
|
subject.setValue("cn=test,dc=dataone,dc=org");
|
95
|
session.setSubject(subject);
|
96
|
return session;
|
97
|
}
|
98
|
|
99
|
|
100
|
|
101
|
|
102
|
/**
|
103
|
* Run an initial test that always passes to check that the test harness is
|
104
|
* working.
|
105
|
*/
|
106
|
public void initialize()
|
107
|
{
|
108
|
printTestHeader("initialize");
|
109
|
assertTrue(1 == 1);
|
110
|
}
|
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
|
|
131
|
/**
|
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
|
* create system metadata with a specified id
|
148
|
*/
|
149
|
public SystemMetadata createSystemMetadata(Identifier id, Subject owner, InputStream object)
|
150
|
throws Exception
|
151
|
{
|
152
|
SystemMetadata sm = new SystemMetadata();
|
153
|
sm.setSerialVersion(BigInteger.valueOf(1));
|
154
|
// set the id
|
155
|
sm.setIdentifier(id);
|
156
|
sm.setFormatId(ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFormatId());
|
157
|
// create the checksum
|
158
|
String checksumS = "test";
|
159
|
if (object != null) {
|
160
|
checksumS = checksum(object);
|
161
|
}
|
162
|
String ca = "MD5";
|
163
|
Checksum checksum = new Checksum();
|
164
|
checksum.setValue(checksumS);
|
165
|
checksum.setAlgorithm(ca);
|
166
|
sm.setChecksum(checksum);
|
167
|
// set the size
|
168
|
sm.setSize(new BigInteger("0"));
|
169
|
sm.setSubmitter(owner);
|
170
|
sm.setRightsHolder(owner);
|
171
|
sm.setDateUploaded(new Date());
|
172
|
sm.setDateSysMetadataModified(new Date());
|
173
|
NodeReference nr = new NodeReference();
|
174
|
nr.setValue("metacat");
|
175
|
sm.setOriginMemberNode(nr);
|
176
|
sm.setAuthoritativeMemberNode(nr);
|
177
|
// set the access to public read
|
178
|
AccessPolicy accessPolicy = new AccessPolicy();
|
179
|
AccessRule allow = new AccessRule();
|
180
|
allow.addPermission(Permission.READ);
|
181
|
Subject publicSubject = new Subject();
|
182
|
publicSubject.setValue(Constants.SUBJECT_PUBLIC);
|
183
|
allow.addSubject(publicSubject);
|
184
|
accessPolicy.addAllow(allow);
|
185
|
sm.setAccessPolicy(accessPolicy);
|
186
|
|
187
|
return sm;
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* For fresh Metacat installations without the Object Format List
|
192
|
* we insert the default version from d1_common.jar
|
193
|
*/
|
194
|
protected void setUpFormats() {
|
195
|
try {
|
196
|
Metacat m = MetacatFactory.createMetacatConnection(metacatUrl);
|
197
|
m.login(username, password);
|
198
|
// check if it exists already
|
199
|
InputStream is = null;
|
200
|
try {
|
201
|
is = m.read(ObjectFormatService.OBJECT_FORMAT_DOCID);
|
202
|
} catch (Exception e) {
|
203
|
// probably missing the doc
|
204
|
}
|
205
|
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()));
|
210
|
m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null);
|
211
|
}
|
212
|
m.logout();
|
213
|
} catch (Exception e) {
|
214
|
// any number of things could go wrong
|
215
|
e.printStackTrace();
|
216
|
}
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* print a header to start each test
|
221
|
*/
|
222
|
protected void printTestHeader(String testName)
|
223
|
{
|
224
|
System.out.println();
|
225
|
System.out.println("*************** " + testName + " ***************");
|
226
|
}
|
227
|
|
228
|
}
|