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: 2013-04-24 19:34:36 -0700 (Wed, 24 Apr 2013) $'
|
9
|
* '$Revision: 7622 $'
|
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.InputStream;
|
29
|
import java.io.InputStreamReader;
|
30
|
import java.io.Reader;
|
31
|
import java.math.BigInteger;
|
32
|
import java.util.Date;
|
33
|
import java.util.List;
|
34
|
|
35
|
import org.apache.wicket.protocol.http.mock.MockHttpServletRequest;
|
36
|
import org.dataone.client.CNode;
|
37
|
import org.dataone.client.D1Client;
|
38
|
import org.dataone.client.ObjectFormatCache;
|
39
|
import org.dataone.service.exceptions.ServiceFailure;
|
40
|
import org.dataone.service.types.v1.AccessPolicy;
|
41
|
import org.dataone.service.types.v1.AccessRule;
|
42
|
import org.dataone.service.types.v1.Checksum;
|
43
|
import org.dataone.service.types.v1.Identifier;
|
44
|
import org.dataone.service.types.v1.Node;
|
45
|
import org.dataone.service.types.v1.NodeReference;
|
46
|
import org.dataone.service.types.v1.NodeType;
|
47
|
import org.dataone.service.types.v1.Permission;
|
48
|
import org.dataone.service.types.v1.Session;
|
49
|
import org.dataone.service.types.v1.Subject;
|
50
|
import org.dataone.service.types.v1.SystemMetadata;
|
51
|
import org.dataone.service.types.v1.util.ChecksumUtil;
|
52
|
import org.dataone.service.types.v1.util.ObjectFormatServiceImpl;
|
53
|
import org.dataone.service.util.Constants;
|
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() throws Exception {
|
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
|
* constructs a "fake" session with the MN subject
|
101
|
* @return
|
102
|
*/
|
103
|
public Session getMNSession() throws Exception {
|
104
|
Session session = new Session();
|
105
|
Subject subject = MNodeService.getInstance(request).getCapabilities().getSubject(0);
|
106
|
session.setSubject(subject);
|
107
|
return session;
|
108
|
}
|
109
|
|
110
|
public Session getCNSession() throws Exception {
|
111
|
Session session = new Session();
|
112
|
Subject subject = null;
|
113
|
CNode cn = D1Client.getCN();
|
114
|
List<Node> nodes = cn.listNodes().getNodeList();
|
115
|
|
116
|
// find the first CN in the node list
|
117
|
for (Node node : nodes) {
|
118
|
if (node.getType() == NodeType.CN) {
|
119
|
subject = node.getSubject(0);
|
120
|
break;
|
121
|
}
|
122
|
}
|
123
|
session.setSubject(subject);
|
124
|
return session;
|
125
|
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* Run an initial test that always passes to check that the test harness is
|
130
|
* working.
|
131
|
*/
|
132
|
public void initialize()
|
133
|
{
|
134
|
printTestHeader("initialize");
|
135
|
assertTrue(1 == 1);
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* create system metadata with a specified id
|
140
|
*/
|
141
|
public SystemMetadata createSystemMetadata(Identifier id, Subject owner, InputStream object)
|
142
|
throws Exception
|
143
|
{
|
144
|
SystemMetadata sm = new SystemMetadata();
|
145
|
sm.setSerialVersion(BigInteger.valueOf(1));
|
146
|
// set the id
|
147
|
sm.setIdentifier(id);
|
148
|
sm.setFormatId(ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFormatId());
|
149
|
// create the checksum
|
150
|
Checksum checksum = new Checksum();
|
151
|
String ca = "MD5";
|
152
|
checksum.setValue("test");
|
153
|
checksum.setAlgorithm(ca);
|
154
|
// actually generate one
|
155
|
if (object != null) {
|
156
|
checksum = ChecksumUtil.checksum(object, ca);
|
157
|
}
|
158
|
sm.setChecksum(checksum);
|
159
|
// set the size
|
160
|
sm.setSize(new BigInteger("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.SUBJECT_PUBLIC);
|
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
|
// get the default from d1_common
|
199
|
InputStream formats = ObjectFormatServiceImpl.getInstance().getObjectFormatFile();
|
200
|
Reader xmlDocument = new InputStreamReader(formats);
|
201
|
m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null);
|
202
|
}
|
203
|
m.logout();
|
204
|
} catch (Exception e) {
|
205
|
// any number of things could go wrong
|
206
|
e.printStackTrace();
|
207
|
}
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* print a header to start each test
|
212
|
*/
|
213
|
protected void printTestHeader(String testName)
|
214
|
{
|
215
|
System.out.println();
|
216
|
System.out.println("*************** " + testName + " ***************");
|
217
|
}
|
218
|
|
219
|
}
|