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-12-08 14:06:54 -0800 (Thu, 08 Dec 2011) $'
|
9
|
* '$Revision: 6755 $'
|
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
|
|
34
|
import org.apache.wicket.protocol.http.MockHttpServletRequest;
|
35
|
import org.dataone.client.ObjectFormatCache;
|
36
|
import org.dataone.service.types.v1.AccessPolicy;
|
37
|
import org.dataone.service.types.v1.AccessRule;
|
38
|
import org.dataone.service.types.v1.Checksum;
|
39
|
import org.dataone.service.types.v1.Identifier;
|
40
|
import org.dataone.service.types.v1.NodeReference;
|
41
|
import org.dataone.service.types.v1.Permission;
|
42
|
import org.dataone.service.types.v1.Session;
|
43
|
import org.dataone.service.types.v1.Subject;
|
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;
|
48
|
|
49
|
import edu.ucsb.nceas.MCTestCase;
|
50
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
51
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
52
|
|
53
|
/**
|
54
|
* A JUnit superclass for testing the dataone Node implementations
|
55
|
*/
|
56
|
public class D1NodeServiceTest extends MCTestCase {
|
57
|
|
58
|
protected MockHttpServletRequest request;
|
59
|
|
60
|
/**
|
61
|
* constructor for the test
|
62
|
*/
|
63
|
public D1NodeServiceTest(String name) {
|
64
|
super(name);
|
65
|
// set up the fake request (for logging)
|
66
|
request = new MockHttpServletRequest(null, null, null);
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Establish a testing framework by initializing appropriate objects
|
71
|
*/
|
72
|
public void setUp() throws Exception {
|
73
|
super.setUp();
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Release any objects after tests are complete
|
78
|
*/
|
79
|
public void tearDown() {}
|
80
|
|
81
|
/**
|
82
|
* constructs a "fake" session with a test subject
|
83
|
* @return
|
84
|
*/
|
85
|
public Session getTestSession() throws Exception {
|
86
|
Session session = new Session();
|
87
|
Subject subject = new Subject();
|
88
|
subject.setValue("cn=test,dc=dataone,dc=org");
|
89
|
session.setSubject(subject);
|
90
|
return session;
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Run an initial test that always passes to check that the test harness is
|
95
|
* working.
|
96
|
*/
|
97
|
public void initialize()
|
98
|
{
|
99
|
printTestHeader("initialize");
|
100
|
assertTrue(1 == 1);
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* create system metadata with a specified id
|
105
|
*/
|
106
|
public SystemMetadata createSystemMetadata(Identifier id, Subject owner, InputStream object)
|
107
|
throws Exception
|
108
|
{
|
109
|
SystemMetadata sm = new SystemMetadata();
|
110
|
sm.setSerialVersion(BigInteger.valueOf(1));
|
111
|
// set the id
|
112
|
sm.setIdentifier(id);
|
113
|
sm.setFormatId(ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFormatId());
|
114
|
// create the checksum
|
115
|
Checksum checksum = new Checksum();
|
116
|
String ca = "MD5";
|
117
|
checksum.setValue("test");
|
118
|
checksum.setAlgorithm(ca);
|
119
|
// actually generate one
|
120
|
if (object != null) {
|
121
|
checksum = ChecksumUtil.checksum(object, ca);
|
122
|
}
|
123
|
sm.setChecksum(checksum);
|
124
|
// set the size
|
125
|
sm.setSize(new BigInteger("0"));
|
126
|
sm.setSubmitter(owner);
|
127
|
sm.setRightsHolder(owner);
|
128
|
sm.setDateUploaded(new Date());
|
129
|
sm.setDateSysMetadataModified(new Date());
|
130
|
NodeReference nr = new NodeReference();
|
131
|
nr.setValue("metacat");
|
132
|
sm.setOriginMemberNode(nr);
|
133
|
sm.setAuthoritativeMemberNode(nr);
|
134
|
// set the access to public read
|
135
|
AccessPolicy accessPolicy = new AccessPolicy();
|
136
|
AccessRule allow = new AccessRule();
|
137
|
allow.addPermission(Permission.READ);
|
138
|
Subject publicSubject = new Subject();
|
139
|
publicSubject.setValue(Constants.SUBJECT_PUBLIC);
|
140
|
allow.addSubject(publicSubject);
|
141
|
accessPolicy.addAllow(allow);
|
142
|
sm.setAccessPolicy(accessPolicy);
|
143
|
|
144
|
return sm;
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* For fresh Metacat installations without the Object Format List
|
149
|
* we insert the default version from d1_common.jar
|
150
|
*/
|
151
|
protected void setUpFormats() {
|
152
|
try {
|
153
|
Metacat m = MetacatFactory.createMetacatConnection(metacatUrl);
|
154
|
m.login(username, password);
|
155
|
// check if it exists already
|
156
|
InputStream is = null;
|
157
|
try {
|
158
|
is = m.read(ObjectFormatService.OBJECT_FORMAT_DOCID);
|
159
|
} catch (Exception e) {
|
160
|
// probably missing the doc
|
161
|
}
|
162
|
if (is == null) {
|
163
|
// get the default from d1_common
|
164
|
InputStream formats = ObjectFormatServiceImpl.getInstance().getObjectFormatFile();
|
165
|
Reader xmlDocument = new InputStreamReader(formats);
|
166
|
m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null);
|
167
|
}
|
168
|
m.logout();
|
169
|
} catch (Exception e) {
|
170
|
// any number of things could go wrong
|
171
|
e.printStackTrace();
|
172
|
}
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* print a header to start each test
|
177
|
*/
|
178
|
protected void printTestHeader(String testName)
|
179
|
{
|
180
|
System.out.println();
|
181
|
System.out.println("*************** " + testName + " ***************");
|
182
|
}
|
183
|
|
184
|
}
|