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-05-01 11:37:26 -0700 (Fri, 01 May 2015) $'
9
 * '$Revision: 9187 $'
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() == NodeType.CN) {
152
				subject = node.getSubject(0);
153
				break;
154
			}
155
		}
156
		session.setSubject(subject);
157
		return session;
158

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

    
262
	/**
263
	 * print a header to start each test
264
	 */
265
	protected void printTestHeader(String testName)
266
	{
267
	    System.out.println();
268
	    System.out.println("*************** " + testName + " ***************");
269
	}
270
 
271
}
(2-2/7)