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: leinfelder $'
8
 *     '$Date: 2014-07-23 16:19:48 -0700 (Wed, 23 Jul 2014) $'
9
 * '$Revision: 8810 $'
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.v2.CNode;
37
import org.dataone.client.v2.itk.D1Client;
38
import org.dataone.client.v2.formats.ObjectFormatCache;
39
import org.dataone.service.types.v1.AccessPolicy;
40
import org.dataone.service.types.v1.AccessRule;
41
import org.dataone.service.types.v1.Checksum;
42
import org.dataone.service.types.v1.Identifier;
43
import org.dataone.service.types.v2.Node;
44
import org.dataone.service.types.v1.NodeReference;
45
import org.dataone.service.types.v1.NodeType;
46
import org.dataone.service.types.v1.Permission;
47
import org.dataone.service.types.v1.Session;
48
import org.dataone.service.types.v1.Subject;
49
import org.dataone.service.types.v2.SystemMetadata;
50
import org.dataone.service.types.v1.util.ChecksumUtil;
51
import org.dataone.service.types.v2.util.ObjectFormatServiceImpl;
52
import org.dataone.service.util.Constants;
53

    
54
import edu.ucsb.nceas.MCTestCase;
55
import edu.ucsb.nceas.metacat.client.Metacat;
56
import edu.ucsb.nceas.metacat.client.MetacatFactory;
57

    
58
/**
59
 * A JUnit superclass for testing the dataone Node implementations
60
 */
61
public class D1NodeServiceTest extends MCTestCase {   
62
    
63
    protected MockHttpServletRequest request;
64

    
65
	/**
66
    * constructor for the test
67
    */
68
    public D1NodeServiceTest(String name) {
69
        super(name);
70
        // set up the fake request (for logging)
71
        request = new MockHttpServletRequest(null, null, null);
72
    }
73
  
74
    /**
75
	 * Establish a testing framework by initializing appropriate objects
76
	 */
77
	public void setUp() throws Exception {
78
		super.setUp();
79
	}
80

    
81
	/**
82
	 * Release any objects after tests are complete
83
	 */
84
	public void tearDown() {}
85
	
86
	/**
87
	 * constructs a "fake" session with a test subject
88
	 * @return
89
	 */
90
	public Session getTestSession() throws Exception {
91
		Session session = new Session();
92
        Subject subject = new Subject();
93
        subject.setValue("cn=test,dc=dataone,dc=org");
94
        session.setSubject(subject);
95
        return session;
96
	}
97
	
98
	/**
99
	 * constructs a "fake" session with the MN subject
100
	 * @return
101
	 */
102
	public Session getMNSession() throws Exception {
103
		Session session = new Session();
104
        Subject subject = MNodeService.getInstance(request).getCapabilities().getSubject(0);
105
        session.setSubject(subject);
106
        return session;
107
	}
108

    
109
	public Session getCNSession() throws Exception {
110
		Session session = new Session();
111
		Subject subject = null;
112
		CNode cn = D1Client.getCN();
113
		List<Node> nodes = cn.listNodes().getNodeList();
114

    
115
		// find the first CN in the node list
116
		for (Node node : nodes) {
117
			if (node.getType() == NodeType.CN) {
118
				subject = node.getSubject(0);
119
				break;
120
			}
121
		}
122
		session.setSubject(subject);
123
		return session;
124

    
125
	}
126
	
127
	/**
128
	 * Run an initial test that always passes to check that the test harness is
129
	 * working.
130
	 */
131
	public void initialize() 
132
	{
133
	    printTestHeader("initialize");
134
		assertTrue(1 == 1);
135
	}
136
	
137
	/**
138
	 * create system metadata with a specified id
139
	 */
140
	public SystemMetadata createSystemMetadata(Identifier id, Subject owner, InputStream object)
141
	  throws Exception
142
	{
143
	    SystemMetadata sm = new SystemMetadata();
144
	    sm.setSerialVersion(BigInteger.valueOf(1));
145
        // set the id
146
        sm.setIdentifier(id);
147
        sm.setFormatId(ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFormatId());
148
        // create the checksum
149
        Checksum checksum = new Checksum();
150
        String ca = "MD5";
151
        checksum.setValue("test");
152
        checksum.setAlgorithm(ca);
153
        // actually generate one
154
        if (object != null) {
155
            checksum = ChecksumUtil.checksum(object, ca);
156
        }
157
        sm.setChecksum(checksum);
158
        // set the size
159
        sm.setSize(new BigInteger("0"));
160
        sm.setSubmitter(owner);
161
        sm.setRightsHolder(owner);
162
        sm.setDateUploaded(new Date());
163
        sm.setDateSysMetadataModified(new Date());
164
        NodeReference nr = new NodeReference();
165
        nr.setValue("metacat");
166
        sm.setOriginMemberNode(nr);
167
        sm.setAuthoritativeMemberNode(nr);
168
		// set the access to public read
169
        AccessPolicy accessPolicy = new AccessPolicy();
170
        AccessRule allow = new AccessRule();
171
        allow.addPermission(Permission.READ);
172
        Subject publicSubject = new Subject();
173
        publicSubject.setValue(Constants.SUBJECT_PUBLIC);
174
		allow.addSubject(publicSubject);
175
		accessPolicy.addAllow(allow);
176
        sm.setAccessPolicy(accessPolicy);
177
        
178
        return sm;
179
	}
180
	
181
	/**
182
	 * For fresh Metacat installations without the Object Format List
183
	 * we insert the default version from d1_common.jar
184
	 */
185
	protected void setUpFormats() {
186
		try {
187
			Metacat m = MetacatFactory.createMetacatConnection(metacatUrl);
188
			m.login(username, password);
189
			// check if it exists already
190
			InputStream is = null;
191
			try {
192
				is = m.read(ObjectFormatService.OBJECT_FORMAT_DOCID);
193
			} catch (Exception e) {
194
				// probably missing the doc
195
			}
196
			if (is == null) {
197
				// get the default from d1_common
198
				InputStream formats = ObjectFormatServiceImpl.getInstance().getObjectFormatFile();
199
				Reader xmlDocument = new InputStreamReader(formats);
200
				m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null);
201
			}
202
			m.logout();
203
		} catch (Exception e) {
204
			// any number of things could go wrong
205
			e.printStackTrace();
206
		}
207
	}
208

    
209
	/**
210
	 * print a header to start each test
211
	 */
212
	protected void printTestHeader(String testName)
213
	{
214
	    System.out.println();
215
	    System.out.println("*************** " + testName + " ***************");
216
	}
217
 
218
}
(2-2/6)