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: 2016-10-04 15:26:03 -0700 (Tue, 04 Oct 2016) $'
9
 * '$Revision: 10002 $'
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
import edu.ucsb.nceas.metacat.dataone.D1NodeService;
68

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

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

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

    
147
	public Session getCNSession() throws Exception {
148
		Session session = new Session();
149
		Subject subject = null;
150
		CNode cn = D1Client.getCN();
151
		List<Node> nodes = cn.listNodes().getNodeList();
152

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

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

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