Project

General

Profile

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: tao $'
8
 *     '$Date: 2015-07-30 17:33:52 -0700 (Thu, 30 Jul 2015) $'
9
 * '$Revision: 9273 $'
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.IOException;
29
import java.io.InputStream;
30
import java.io.InputStreamReader;
31
import java.io.Reader;
32
import java.math.BigInteger;
33
import java.util.Date;
34
import java.util.List;
35

    
36
import junit.framework.Test;
37
import junit.framework.TestSuite;
38

    
39
import org.apache.wicket.protocol.http.mock.MockHttpServletRequest;
40
import org.dataone.client.D1Node;
41
import org.dataone.client.NodeLocator;
42
import org.dataone.client.exception.ClientSideException;
43
import org.dataone.client.v2.CNode;
44
import org.dataone.client.v2.itk.D1Client;
45
import org.dataone.client.v2.formats.ObjectFormatCache;
46
import org.dataone.configuration.Settings;
47
import org.dataone.service.types.v1.AccessPolicy;
48
import org.dataone.service.types.v1.AccessRule;
49
import org.dataone.service.types.v1.Checksum;
50
import org.dataone.service.types.v1.Identifier;
51
import org.dataone.service.types.v2.Node;
52
import org.dataone.service.types.v2.ObjectFormatList;
53
import org.dataone.service.types.v1.NodeReference;
54
import org.dataone.service.types.v1.NodeType;
55
import org.dataone.service.types.v1.Permission;
56
import org.dataone.service.types.v1.Session;
57
import org.dataone.service.types.v1.Subject;
58
import org.dataone.service.types.v2.SystemMetadata;
59
import org.dataone.service.types.v1.util.ChecksumUtil;
60
import org.dataone.service.types.v2.util.ObjectFormatServiceImpl;
61
import org.dataone.service.util.Constants;
62
import org.dataone.service.util.TypeMarshaller;
63

    
64
import edu.ucsb.nceas.MCTestCase;
65
import edu.ucsb.nceas.metacat.client.Metacat;
66
import edu.ucsb.nceas.metacat.client.MetacatFactory;
67

    
68
/**
69
 * A JUnit superclass for testing the dataone Node implementations
70
 */
71
public class D1NodeServiceTest extends MCTestCase {   
72
    
73
    protected MockHttpServletRequest request;
74

    
75
	/**
76
    * constructor for the test
77
    */
78
    public D1NodeServiceTest(String name) {
79
        super(name);
80
        // set up the fake request (for logging)
81
        request = new MockHttpServletRequest(null, null, null);
82
    }
83
    
84
    public static Test suite() 
85
    {
86
        TestSuite suite = new TestSuite();
87
        suite.addTest(new D1NodeServiceTest("initialize"));
88
        return suite;
89
    }
90
    
91
    /**
92
	 * Establish a testing framework by initializing appropriate objects
93
	 */
94
    public void setUp() throws Exception {
95
    	super.setUp();
96
		NodeLocator nodeLocator = new NodeLocator() {
97
			@Override
98
			public D1Node getCNode() throws ClientSideException {
99
			    D1Node node = null;
100
			    try {
101
			        node = new MockCNode();
102
			    } catch (IOException e) {
103
			        throw new ClientSideException(e.getMessage());
104
			    }
105
				return node;
106
			}
107
		};
108
		D1Client.setNodeLocator(nodeLocator );
109
    	
110
    }
111

    
112
	/**
113
	 * Release any objects after tests are complete
114
	 */
115
	public void tearDown() {
116
		// set back to force it to use defaults
117
		D1Client.setNodeLocator(null);
118
	}
119
	
120
	/**
121
	 * constructs a "fake" session with a test subject
122
	 * @return
123
	 */
124
	public Session getTestSession() throws Exception {
125
		Session session = new Session();
126
        Subject subject = new Subject();
127
        subject.setValue("cn=test,dc=dataone,dc=org");
128
        session.setSubject(subject);
129
        return session;
130
	}
131
	
132
	/**
133
	 * constructs a "fake" session with the MN subject
134
	 * @return
135
	 */
136
	public Session getMNSession() throws Exception {
137
		Session session = new Session();
138
        Subject subject = MNodeService.getInstance(request).getCapabilities().getSubject(0);
139
        session.setSubject(subject);
140
        return session;
141
	}
142

    
143
	public Session getCNSession() throws Exception {
144
		Session session = new Session();
145
		Subject subject = null;
146
		CNode cn = D1Client.getCN();
147
		List<Node> nodes = cn.listNodes().getNodeList();
148

    
149
		// find the first CN in the node list
150
		for (Node node : nodes) {
151
			if (node.getType().equals(NodeType.CN)) {
152
				subject = node.getSubject(0);
153
				break;
154
			}
155
		}
156
		session.setSubject(subject);
157
		return session;
158

    
159
	}
160
	
161
	public Session getAnotherSession() throws Exception {
162
	    Session session = new Session();
163
        Subject subject = new Subject();
164
        subject.setValue("cn=test2,dc=dataone,dc=org");
165
        session.setSubject(subject);
166
        return session;
167
	    
168
	}
169
	
170
	/**
171
	 * Run an initial test that always passes to check that the test harness is
172
	 * working.
173
	 */
174
	public void initialize() 
175
	{
176
	    printTestHeader("initialize");
177
		assertTrue(1 == 1);
178
	}
179
	
180
	/**
181
	 * create system metadata with a specified id
182
	 */
183
	public SystemMetadata createSystemMetadata(Identifier id, Subject owner, InputStream object)
184
	  throws Exception
185
	{
186
	    SystemMetadata sm = new SystemMetadata();
187
	    sm.setSerialVersion(BigInteger.valueOf(1));
188
        // set the id
189
        sm.setIdentifier(id);
190
        sm.setFormatId(ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFormatId());
191
        // create the checksum
192
        Checksum checksum = new Checksum();
193
        String ca = "MD5";
194
        checksum.setValue("test");
195
        checksum.setAlgorithm(ca);
196
        // actually generate one
197
        if (object != null) {
198
            checksum = ChecksumUtil.checksum(object, ca);
199
        }
200
        sm.setChecksum(checksum);
201
        // set the size
202
        sm.setSize(new BigInteger("0"));
203
        sm.setSubmitter(owner);
204
        sm.setRightsHolder(owner);
205
        sm.setDateUploaded(new Date());
206
        sm.setDateSysMetadataModified(new Date());
207
        String currentNodeId = Settings.getConfiguration().getString("dataone.nodeId");
208
        if(currentNodeId == null || currentNodeId.trim().equals("")) {
209
            throw new Exception("there should be value in the dataone.nodeId in the metacat.properties file.");
210
        }
211
        NodeReference nr = new NodeReference();
212
        nr.setValue(currentNodeId);
213
        sm.setOriginMemberNode(nr);
214
        sm.setAuthoritativeMemberNode(nr);
215
		// set the access to public read
216
        AccessPolicy accessPolicy = new AccessPolicy();
217
        AccessRule allow = new AccessRule();
218
        allow.addPermission(Permission.READ);
219
        Subject publicSubject = new Subject();
220
        publicSubject.setValue(Constants.SUBJECT_PUBLIC);
221
		allow.addSubject(publicSubject);
222
		accessPolicy.addAllow(allow);
223
        sm.setAccessPolicy(accessPolicy);
224
        
225
        return sm;
226
	}
227
	
228
	/**
229
	 * For fresh Metacat installations without the Object Format List
230
	 * we insert the default version from d1_common.jar
231
	 */
232
	protected void setUpFormats() {
233
		try {
234
			Metacat m = MetacatFactory.createMetacatConnection(metacatUrl);
235
			m.login(username, password);
236
			// check if it exists already
237
			InputStream is = null;
238
			try {
239
				is = m.read(ObjectFormatService.OBJECT_FORMAT_DOCID);
240
			} catch (Exception e) {
241
				// probably missing the doc
242
			}
243
			
244
			if (is != null) {
245
				// check for v2 OFL
246
				try {
247
					ObjectFormatList ofl = TypeMarshaller.unmarshalTypeFromStream(ObjectFormatList.class, is);
248
				} catch (ClassCastException cce) {
249
					// need to update it
250
					InputStream formats = ObjectFormatServiceImpl.getInstance().getObjectFormatFile();
251
					Reader xmlDocument = new InputStreamReader(formats);
252
					int rev = m.getNewestDocRevision(ObjectFormatService.OBJECT_FORMAT_DOCID);
253
					rev++;
254
					m.update(ObjectFormatService.OBJECT_FORMAT_DOCID + "." + rev, xmlDocument, null);
255
				}
256
				
257
			}
258
			else {
259
				// get the default from d1_common
260
				InputStream formats = ObjectFormatServiceImpl.getInstance().getObjectFormatFile();
261
				Reader xmlDocument = new InputStreamReader(formats);
262
				m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null);
263
			}
264
			m.logout();
265
		} catch (Exception e) {
266
			// any number of things could go wrong
267
			e.printStackTrace();
268
		}
269
	}
270

    
271
	/**
272
	 * print a header to start each test
273
	 */
274
	protected void printTestHeader(String testName)
275
	{
276
	    System.out.println();
277
	    System.out.println("*************** " + testName + " ***************");
278
	}
279
 
280
}
(2-2/8)