Project

General

Profile

« Previous | Next » 

Revision 8631

Use D1client for communication with CN (for integration test)

View differences:

test/edu/ucsb/nceas/metacat/admin/upgrade/dataone/SyncAccessPolicyTest.java
49 49
import edu.ucsb.nceas.metacat.IdentifierManager;
50 50
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
51 51
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
52
import edu.ucsb.nceas.metacat.dataone.CNodeService;
53 52
import edu.ucsb.nceas.metacat.dataone.MNodeService;
54 53
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
55 54
import edu.ucsb.nceas.metacat.dataone.SyncAccessPolicy;
......
62 61
 */
63 62
public class SyncAccessPolicyTest extends D1NodeServiceTest {
64 63

  
65
	private CNode cn = null;
66

  
67 64
	/**
68 65
	 * Constructor to build the test
69 66
	 * 
......
82 79
		metacatConnectionNeeded = true;
83 80
		super.setUp();
84 81

  
85
		/*
86
		 * Determine the CN for the current host. This test must be run on a
87
		 * registered MN.
88
		 */
89
		try {
90
			cn = D1Client.getCN();
91
		} catch (ServiceFailure sf) {
92
			debug("Unable to get Coordinating node name for this MN");
93
			fail();
94
		}
95

  
96 82
	}
97 83

  
98 84
	/**
......
172 158
		AccessPolicy mnAccessPolicy = null;
173 159
		SystemMetadata cnSysMeta = null;
174 160
		SystemMetadata mnSysMeta = null;
161
		CNode cn = null;
175 162

  
176 163
		String response = null;
177 164
		debug("\nStarting sync access policy test");
178
		debug("Logging in with user: " + anotheruser + ", password: " + anotherpassword);
165
		debug("Logging in with user: " + anotheruser + ", password: "
166
				+ anotherpassword);
179 167
		try {
180 168
			response = m.login(anotheruser, anotherpassword);
181 169
		} catch (Exception e) {
......
183 171
			fail();
184 172
		}
185 173

  
174
		String localId = null;
186 175
		try {
187 176
			Identifier pid = null;
188 177
			pid = createTestPid();
......
192 181
			boolean found = false;
193 182
			int attempts = 0;
194 183

  
184
			/*
185
			 * Determine the CN for the current host. This test must be run on a
186
			 * registered MN.
187
			 */
188
			try {
189
				cn = D1Client.getCN();
190
			} catch (ServiceFailure sf) {
191
				debug("Unable to get Coordinating node name for this MN");
192
				fail();
193
			}
194

  
195 195
			// We have to wait until the CN has harvested the new document,
196 196
			// otherwise we
197 197
			// will just get an error of "pid not found" when we request the CN
......
206 206
				Thread.sleep(1000 * 60);
207 207
				// Get the test document from the CN
208 208
				// Get sm, access policy for requested pid from the CN
209
				// Get sm, access policy for requested pid from the CN
209 210
				try {
210
					cnSysMeta = CNodeService.getInstance(request)
211
							.getSystemMetadata(pid);
211
					cnSysMeta = cn.getSystemMetadata(pid);
212
					debug("Got SM from CN");
213

  
212 214
				} catch (Exception e) {
213 215
					debug("Error getting system metadata for pid: "
214 216
							+ pid.getValue() + " from cn: " + e.getMessage());
......
226 228
						+ " after " + attempts + " attempts");
227 229
			}
228 230

  
229
			String localId = null;
230 231
			try {
231 232
				localId = IdentifierManager.getInstance().getLocalId(
232 233
						pid.getValue());
233 234
			} catch (Exception e) {
234
				debug("Unable to retrieve localId for pid: " + pid.getValue());
235
				fail();
235
				fail("Unable to retrieve localId for pid: " + pid.getValue());
236 236
			}
237 237

  
238 238
			debug("Updating permissions of localId: " + localId + ", guid: "
......
241 241

  
242 242
			m.logout();
243 243
			response = m.login(anotheruser, anotherpassword);
244
			debug("Logging in as user: " + anotheruser);
245
			debug("Adding access for user: " + username);
244 246

  
245 247
			/* Update the docid access policy with Metacat api */
246 248
			try {
......
257 259
			debug("Response from setaccess: " + response);
258 260
			debug("Retrieving updated docid from CN to check if perms were updated...");
259 261

  
260
			/* Reread SM from MN, CN */
262
			/* Reread SM from MN */
261 263
			try {
262 264
				mnSysMeta = MNodeService.getInstance(request)
263 265
						.getSystemMetadata(pid);
264 266
				debug("Got SM from MN");
265 267
			} catch (Exception e) {
266
				debug("Error getting system metadata for new pid: "
268
				fail("Error getting system metadata for new pid: "
267 269
						+ pid.getValue() + ". Message: " + e.getMessage());
270
			}
271

  
272
			/* Check if the access policy was updated on the MN */
273
			mnAccessPolicy = mnSysMeta.getAccessPolicy();
274
			found = false;
275
			List<Subject> subjectList = null;
276
			List<AccessRule> accessRules = mnAccessPolicy.getAllowList();
277
			debug("Checking that access policy was added to MN");
278

  
279
			try {
280
				debug("Checking " + accessRules.size() + " access rules");
281
				for (AccessRule ar : accessRules) {
282
					subjectList = ar.getSubjectList();
283
					debug("Checking " + ar.sizeSubjectList()
284
							+ " subjects for this access rule");
285
					for (Subject sj : subjectList) {
286
						debug("Checking subject: " + sj.getValue());
287
						if (sj.getValue().equals(username)) {
288
							debug("user " + username + " found");
289
							found = true;
290
						}
291
						debug("MN done with subject: " + sj.getValue());
292
					}
293
				}
294
			} catch (Exception e) {
295
				debug("Error checking access policy: " + e.getMessage());
268 296
				fail();
269 297
			}
270 298

  
271
			// Get the test document from the CN 
299
			if (!found)
300
				debug("user " + username + " not found in access policy");
301

  
302
			// assertTrue(found);
303

  
304
			/* Reread SM from CN */
272 305
			try {
273
				cnSysMeta = CNodeService.getInstance(request)
274
						.getSystemMetadata(pid);
306
				cnSysMeta = cn.getSystemMetadata(pid);
275 307
				debug("Got SM from CN");
276 308
			} catch (Exception e) {
277
				debug("Error getting system metadata for pid: "
278
						+ pid.getValue() + " from cn: " + e.getMessage());
279
				fail();
309
				fail("Error getting system metadata for pid: " + pid.getValue()
310
						+ " from cn: " + e.getMessage());
280 311
			}
281 312

  
313
			debug("Done getting CN SM");
314

  
282 315
			/* Check if the access policy was updated on the MN */
283
			mnAccessPolicy = mnSysMeta.getAccessPolicy();
316
			cnAccessPolicy = cnSysMeta.getAccessPolicy();
284 317
			found = false;
285
			List<Subject> subjectList = null;
286
			List<AccessRule> accessRules = mnAccessPolicy.getAllowList();
287
			debug("Checking that access policy was added to MN");
288
//
289
//			for (AccessRule ar : accessRules) {
290
//				subjectList = ar.getSubjectList();
291
//				for (Subject sj : subjectList) {
292
//					debug("Checking subject: " + sj.getValue());
293
//					if (sj.getValue().contains(anotheruser)) {
294
//						debug("user " + anotheruser + " found");
295
//						found = true;
296
//					}
297
//					debug("Foo");
298
//				}
299
//			}
300
//
301
//			assertTrue(found);
318
			subjectList = null;
319
			accessRules = cnAccessPolicy.getAllowList();
320
			debug("Checking that access policy was added to CN");
321
			debug("Checking " + accessRules.size() + " access rules");
322

  
323
			for (AccessRule ar : accessRules) {
324
				subjectList = ar.getSubjectList();
325
				debug("Checking " + ar.sizeSubjectList()
326
						+ " subjects for this access rule");
327
				for (Subject sj : subjectList) {
328
					debug("Checking subject: " + sj.getValue());
329
					if (sj.getValue().indexOf(username) >= 0) {
330
						debug("user " + username + " found");
331
						found = true;
332
					}
333
				}
334
			}
335
			if (!found)
336
				debug("user " + username + " not found in access policy");
337
			debug("Done checking access rules for CN");
338

  
339
			assertTrue(found);
302 340
			debug("Checking privs retrieved from CN");
303 341
			debug("Getting access policy for pid: " + pid.getValue());
304 342
			cnAccessPolicy = cnSysMeta.getAccessPolicy();
......
310 348
					cnAccessPolicy));
311 349
			debug("Are access policies equal?: " + apEqual.toString());
312 350
			assert (apEqual == true);
313

  
314
			deleteDocumentId(localId, SUCCESS, true);
315
			m.logout();
316

  
317 351
		} catch (Exception e) {
318 352
			e.printStackTrace();
319 353
			debug("Error running syncAP test: " + e.getMessage());
320 354
			fail();
355
		} finally {
356
			if (localId != null)
357
				deleteDocumentId(localId, SUCCESS, true);
358
			try {
359
				m.logout();
360
			} catch (Exception e) {
361
				debug("Error logging out");
362
			}
321 363
		}
322 364

  
323 365
		debug("Done running testSyncAccessPolicy");
......
331 373
	 */
332 374
	public void testSyncEML201OnlineDataAccessPolicy() {
333 375
		String newdocid = null;
334
		String onlineDocid;
376
		String onlineDocid = null;
335 377
		String onlinetestdatafile2 = "test/onlineDataFile2";
336 378
		SystemMetadata mnSysMeta = null;
337 379
		SystemMetadata cnSysMeta = null;
380
		CNode cn = null;
338 381

  
339 382
		try {
340 383
			debug("\nRunning: testSyncEML201OnlineDataAccessPolicy");
......
377 420

  
378 421
			Identifier pid = new Identifier();
379 422
			pid.setValue(guid);
423
			try {
424
				cn = D1Client.getCN();
425
			} catch (ServiceFailure sf) {
426
				debug("Unable to get Coordinating node name for this MN");
427
				fail();
428
			}
380 429

  
381 430
			/*
382 431
			 * Wait for the cn to harvest metadata for the data object. We are
......
469 518
			debug("Are access policies equal?: " + apEqual.toString());
470 519
			assert (apEqual == true);
471 520

  
521
		} catch (MetacatAuthException mae) {
522
			fail("Authorization failed:\n" + mae.getMessage());
523
		} catch (MetacatInaccessibleException mie) {
524
			fail("Metacat Inaccessible:\n" + mie.getMessage());
525
		} catch (Exception e) {
526
			fail("General exception:\n" + e.getMessage());
527
		} finally {
472 528
			/* Delete the document */
473 529
			deleteDocumentId(newdocid + ".2", SUCCESS, true);
474 530
			deleteDocumentId(onlineDocid + ".1", SUCCESS, true);
475 531

  
476 532
			// logout
477 533
			debug("logging out");
478
			m.logout();
479
		} catch (MetacatAuthException mae) {
480
			fail("Authorization failed:\n" + mae.getMessage());
481
		} catch (MetacatInaccessibleException mie) {
482
			fail("Metacat Inaccessible:\n" + mie.getMessage());
483
		} catch (Exception e) {
484
			fail("General exception:\n" + e.getMessage());
534
			try {
535
				m.logout();
536
			} catch (Exception e) {
537
				debug("Error logging out");
538
			}
485 539
		}
540

  
486 541
		debug("Done running testSyncEML201OnlineDataAccessPolicy");
487 542
	}
488 543

  

Also available in: Unified diff