Project

General

Profile

« Previous | Next » 

Revision 8611

Test syncing of access policies when updated with legacy metacat api

View differences:

test/edu/ucsb/nceas/metacat/admin/upgrade/dataone/SyncAccessPolicyTest.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2014 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: slaughter $'
8
 *     '$Date: $'
9
 * '$Revision:$'
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.admin.upgrade.dataone;
27

  
28
import java.io.ByteArrayInputStream;
29
import java.io.File;
30
import java.io.InputStream;
31
import java.io.StringReader;
32
import java.io.UnsupportedEncodingException;
33
import java.util.Hashtable;
34

  
35
import org.apache.http.client.HttpClient;
36
import org.apache.http.impl.client.DefaultHttpClient;
37
import org.dataone.client.CNode;
38
import org.dataone.client.D1Client;
39
import org.dataone.service.exceptions.IdentifierNotUnique;
40
import org.dataone.service.exceptions.InsufficientResources;
41
import org.dataone.service.exceptions.InvalidRequest;
42
import org.dataone.service.exceptions.InvalidSystemMetadata;
43
import org.dataone.service.exceptions.InvalidToken;
44
import org.dataone.service.exceptions.NotAuthorized;
45
import org.dataone.service.exceptions.NotImplemented;
46
import org.dataone.service.exceptions.ServiceFailure;
47
import org.dataone.service.exceptions.UnsupportedType;
48
import org.dataone.service.types.v1.AccessPolicy;
49
import org.dataone.service.types.v1.AccessRule;
50
import org.dataone.service.types.v1.Identifier;
51
import org.dataone.service.types.v1.Permission;
52
import org.dataone.service.types.v1.Session;
53
import org.dataone.service.types.v1.Subject;
54
import org.dataone.service.types.v1.SystemMetadata;
55
import org.dataone.service.util.Constants;
56
import org.junit.Before;
57

  
58
import edu.ucsb.nceas.MCTestCase;
59
import edu.ucsb.nceas.metacat.IdentifierManager;
60
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
61
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
62
import edu.ucsb.nceas.metacat.client.Metacat;
63
import edu.ucsb.nceas.metacat.client.MetacatFactory;
64
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
65
import edu.ucsb.nceas.metacat.dataone.CNodeServiceTest;
66
import edu.ucsb.nceas.metacat.dataone.MNodeService;
67
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
68
import edu.ucsb.nceas.metacat.dataone.SyncAccessPolicy;
69
import edu.ucsb.nceas.metacat.properties.PropertyService;
70
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
71
import edu.ucsb.nceas.metacat.util.RequestUtil;
72
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
73
import junit.framework.Test;
74
import junit.framework.TestSuite;
75

  
76
/**
77
 * A JUnit test for testing syncing access policies between MN -> CN after local
78
 * update by metacat services
79
 */
80
public class SyncAccessPolicyTest extends D1NodeServiceTest {
81

  
82
//	private static String username;
83
//	private static String password;
84
//	private static String anotheruser;
85
//	private static String anotherPassword;
86
//
87
//	static {
88
//		try {
89
//
90
//			username = PropertyService.getProperty("test.mcUser");
91
//			password = PropertyService.getProperty("test.mcPassword");
92
//			anotheruser = PropertyService.getProperty("test.mcAnotherUser");
93
//			anotherpassword = PropertyService
94
//					.getProperty("test.mcAnotherPassword");
95
//
96
//		} catch (PropertyNotFoundException pnfe) {
97
//			System.err.println("Could not get property in static block: "
98
//					+ pnfe.getMessage());
99
//		}
100
//	}
101

  
102
	private Metacat m;
103

  
104
	/**
105
	 * Constructor to build the test
106
	 * 
107
	 * @param name
108
	 *            the name of the test method
109
	 */
110
	public SyncAccessPolicyTest(String name) {
111
		super(name);
112
	}
113

  
114
	/**
115
	 * Establish a testing framework by initializing appropriate objects
116
	 */
117
	@Before
118
	public void setUp() throws Exception {
119
		// super.setUp();
120

  
121
		try {
122
			debug("Test Metacat: " + metacatUrl);
123
			m = MetacatFactory.createMetacatConnection(metacatUrl);
124
		} catch (MetacatInaccessibleException mie) {
125
			System.err.println("Metacat is: " + metacatUrl);
126
			fail("Metacat connection failed." + mie.getMessage());
127
		}
128
	}
129

  
130
	/**
131
	 * Release any objects after tests are complete
132
	 */
133
	public void tearDown() {
134
	}
135

  
136
	/**
137
	 * Create a suite of tests to be run together
138
	 */
139
	public static Test suite() {
140
		TestSuite suite = new TestSuite();
141
		suite.addTest(new SyncAccessPolicyTest("initialize"));
142
		suite.addTest(new SyncAccessPolicyTest("testSyncAccessPolicy"));
143

  
144
		return suite;
145
	}
146

  
147
	/**
148
	 * Run an initial test that always passes to check that the test harness is
149
	 * working.
150
	 */
151
	public void initialize() {
152
		assertTrue(1 == 1);
153
	}
154

  
155
	/**
156
	 * Test object creation
157
	 */
158
	public Identifier createTestPid() {
159
		printTestHeader("testCreate");
160
		Identifier pid = null;
161
		try {
162
			Session session = getTestSession();
163
			Identifier guid = new Identifier();
164
			guid.setValue("testSyncAP." + System.currentTimeMillis());
165
			InputStream object = new ByteArrayInputStream(
166
					"test".getBytes("UTF-8"));
167
			SystemMetadata sysmeta = createSystemMetadata(guid,
168
					session.getSubject(), object);
169

  
170
			AccessPolicy accessPolicy = sysmeta.getAccessPolicy();
171
			AccessRule allow = new AccessRule();
172
			allow.addPermission(Permission.CHANGE_PERMISSION);
173
			Subject publicSubject = new Subject();
174
			publicSubject.setValue(Constants.SUBJECT_PUBLIC);
175
			allow.addSubject(publicSubject);
176
			accessPolicy.addAllow(allow);
177
			sysmeta.setAccessPolicy(accessPolicy);
178

  
179
			pid = MNodeService.getInstance(request).create(session, guid,
180
					object, sysmeta);
181
		} catch (Exception e) {
182
			e.printStackTrace();
183
			debug("Error creating pid: " + e.getMessage());
184
			fail();
185
		}
186
		return pid;
187
	}
188

  
189
	public void testSyncAccessPolicy() {
190

  
191
		AccessPolicy cnAccessPolicy = null;
192
		AccessPolicy mnAccessPolicy = null;
193
		SystemMetadata cnSysMeta = null;
194
		SystemMetadata mnSysMeta = null;
195
		String resultXML = null;
196

  
197
		debug("Logging in with user: " + username);
198
		String response = null;
199

  
200
		try {
201
			debug("\nStarting sync access policy test");
202

  
203
			Identifier pid = null;
204
			// create the systemMetadata the normal way
205

  
206
			pid = createTestPid();
207
			assertNotNull(pid);
208

  
209
			debug("Inserted new document: " + pid.getValue());
210
			try {
211
				// Get sm, access policy for requested localId
212
				mnSysMeta = IdentifierManager.getInstance().getSystemMetadata(
213
						pid.getValue());
214
			} catch (Exception e) {
215
				debug("Error getting system metadata for new pid: "
216
						+ pid.getValue() + ". Message: " + e.getMessage());
217
				fail();
218
			}
219

  
220
			CNode cn = null;
221

  
222
			try {
223
				cn = D1Client.getCN();
224
			} catch (ServiceFailure sf) {
225
				debug("Unable to get Coordinating node name for this MN");
226
				fail();
227
			}
228

  
229
			boolean found = false;
230
			int attempts = 0;
231

  
232
			// We have to wait until the CN has harvested the new document,
233
			// otherwise we
234
			// will just get an error of "pid not found" when we request the CN
235
			// to
236
			// sync the access policies
237
			// (which is triggered by the Metacat setaccess call).
238

  
239
			debug("Checking for new docid on CN...");
240
			found = false;
241
			for (int i = 0; i < 6; i++) {
242
				attempts = i;
243
				Thread.sleep(1000 * 60);
244
				// Get the test document from the CN
245
				// Get sm, access policy for requested pid from the CN
246
				try {
247
					cnSysMeta = cn.getSystemMetadata(pid);
248
				} catch (Exception e) {
249
					debug("Error getting system metadata for pid: "
250
							+ pid.getValue() + " from cn: " + e.getMessage());
251
					debug("Will request pid from CN again...");
252
					continue;
253
				}
254

  
255
				found = true;
256
				debug("Document " + pid.getValue() + " has been read from CN.");
257
				break;
258
			}
259

  
260
			if (!found) {
261
				fail("Error, cannot read system metadata for pid: " + pid
262
						+ " after " + attempts + " attempts");
263
			}
264

  
265
			Hashtable<String, String[]> fieldValuePairs = new Hashtable<String, String[]>();
266
			fieldValuePairs = new Hashtable<String, String[]>();
267

  
268
			String localId = null;
269
			try {
270
				localId = IdentifierManager.getInstance().getLocalId(
271
						pid.getValue());
272
			} catch (Exception e) {
273
				debug("Unable to retrieve localId for pid: " + pid.getValue());
274
				fail();
275
			}
276
			
277
			debug("Logging in with user: " + anotheruser + ", password: " + anotherpassword);
278
			try {
279
				response = m.login(anotheruser, anotherpassword);
280
				debug("Login response: " + response);
281
			} catch (Exception e) {
282
				debug("Unable to login: " + response);
283
				fail();
284
			}
285
			
286
			debug("Updating permissions of localId: " + localId + ", guid: "
287
					+ pid.getValue());
288

  
289
			// Now update the access policy on the local metacat using the
290
			// metacat api
291
			fieldValuePairs = new Hashtable<String, String[]>();
292
			fieldValuePairs.put("action", new String[] { "setaccess" });
293
			fieldValuePairs.put("docid", new String[] { localId });
294
			fieldValuePairs.put("principal", new String[] { username });
295
			fieldValuePairs.put("permission", new String[] { "read" });
296
			fieldValuePairs.put("permType", new String[] { "allow" });
297
			fieldValuePairs.put("permOrder", new String[] { "allowFirst" });
298
			debug("Updating access perms for docid: " + localId);
299
			try {
300
				resultXML = RequestUtil.get(metacatUrl, fieldValuePairs);
301
			} catch (Exception e) {
302
				debug("Error setting permissions on docid: " + localId);
303
				fail();
304
			}
305

  
306
			if (!resultXML.contains("success")) {
307
				debug("Unable to change access policy on MN, response: " + resultXML);
308
				fail();
309
			}
310

  
311
			debug("Retrieving updated docid from CN to check if perms were updated...");
312

  
313
			// Get the test document from the CN
314
			// Get sm, access policy for requested pid from the CN
315
			try {
316
				cnSysMeta = cn.getSystemMetadata(pid);
317
			} catch (Exception e) {
318
				debug("Error getting system metadata for pid: "
319
						+ pid.getValue() + " from cn: " + e.getMessage());
320
				fail();
321
			}
322

  
323
			debug("Checking privs retrieved from CN");
324
			// Check if privs for the new user showed up on the CN
325
			mnAccessPolicy = mnSysMeta.getAccessPolicy();
326
			debug("Getting access policy for pid: " + pid.getValue());
327
			cnAccessPolicy = cnSysMeta.getAccessPolicy();
328
			debug("Diffing access policies (MN,CN) for pid: " + pid.getValue());
329
			SyncAccessPolicy syncAP = new SyncAccessPolicy();
330
			debug("Comparing access policies...");
331
			assert (syncAP.isEqual(mnAccessPolicy, cnAccessPolicy));
332

  
333
			m.logout();
334

  
335
		} catch (Exception e) {
336
			e.printStackTrace();
337
			fail("Error running syncAP test: " + e.getMessage());
338
		}
339

  
340
		debug("Done running testSyncAccessPolicy");
341
	}
342
}

Also available in: Unified diff