Project

General

Profile

« Previous | Next » 

Revision 6295

add methods for testing CNodeService impl. Still some failing tests, but whittling them down

View differences:

test/edu/ucsb/nceas/metacat/dataone/CNodeServiceTest.java
25 25

  
26 26
package edu.ucsb.nceas.metacat.dataone;
27 27

  
28
import static org.junit.Assert.assertTrue;
29
import static org.junit.Assert.fail;
30

  
28
import java.io.ByteArrayInputStream;
29
import java.io.InputStream;
31 30
import java.util.Date;
32 31

  
33 32
import junit.framework.Test;
34 33
import junit.framework.TestSuite;
35 34

  
36 35
import org.dataone.client.ObjectFormatCache;
36
import org.dataone.service.Constants;
37 37
import org.dataone.service.exceptions.InsufficientResources;
38 38
import org.dataone.service.exceptions.InvalidRequest;
39 39
import org.dataone.service.exceptions.NotFound;
40 40
import org.dataone.service.exceptions.NotImplemented;
41 41
import org.dataone.service.exceptions.ServiceFailure;
42
import org.dataone.service.types.AccessPolicy;
43
import org.dataone.service.types.AccessRule;
42 44
import org.dataone.service.types.Checksum;
43 45
import org.dataone.service.types.ChecksumAlgorithm;
44 46
import org.dataone.service.types.Identifier;
......
46 48
import org.dataone.service.types.ObjectFormat;
47 49
import org.dataone.service.types.ObjectFormatIdentifier;
48 50
import org.dataone.service.types.ObjectFormatList;
51
import org.dataone.service.types.ObjectInfo;
52
import org.dataone.service.types.ObjectList;
53
import org.dataone.service.types.ObjectLocationList;
54
import org.dataone.service.types.Permission;
55
import org.dataone.service.types.Replica;
56
import org.dataone.service.types.ReplicationPolicy;
57
import org.dataone.service.types.ReplicationStatus;
49 58
import org.dataone.service.types.Session;
50 59
import org.dataone.service.types.Subject;
51 60
import org.dataone.service.types.SystemMetadata;
......
88 97
		TestSuite suite = new TestSuite();
89 98
		suite.addTest(new CNodeServiceTest("initialize"));
90 99
		
100
		suite.addTest(new CNodeServiceTest("testAssertRelation"));
101
		suite.addTest(new CNodeServiceTest("testChecksum"));
102
		suite.addTest(new CNodeServiceTest("testCreate"));
103
		suite.addTest(new CNodeServiceTest("testGetFormat"));
91 104
		suite.addTest(new CNodeServiceTest("testListFormats"));
92
		suite.addTest(new CNodeServiceTest("testGetFormat"));
105
		suite.addTest(new CNodeServiceTest("testListNodes"));
106
		suite.addTest(new CNodeServiceTest("testObjectFormatNotFoundException"));
93 107
		suite.addTest(new CNodeServiceTest("testRegisterSystemMetadata"));
108
		suite.addTest(new CNodeServiceTest("testReplicationPolicy"));
109
		suite.addTest(new CNodeServiceTest("testReplicationStatus"));
110
		suite.addTest(new CNodeServiceTest("testReserveIdentifier"));
111
		suite.addTest(new CNodeServiceTest("testResolve"));
112
		suite.addTest(new CNodeServiceTest("testSearch"));
113
		suite.addTest(new CNodeServiceTest("testSetOwner"));
94 114
	
95 115
		return suite;
96 116
	}
97 117
	
98 118
	/**
119
	 * constructs a "fake" session with a test subject
120
	 * @return
121
	 */
122
	private Session getTestSession() {
123
		Session session = new Session();
124
        Subject subject = new Subject();
125
        subject.setValue("cn=test,dc=dataone,dc=org");
126
        session.setSubject(subject);
127
        return session;
128
	}
129
	
130
	/**
99 131
	 * test for registering standalone system metadata
100 132
	 */
101
	public void testRegisterSystemMetadata()
102
	{
133
	public void testRegisterSystemMetadata() {
103 134
	    printTestHeader("testRegisterSystemMetadata");
104 135

  
105 136
	    try {
106
            Session session = new Session();
107
            Subject subject = new Subject();
108
            subject.setValue("cn=test,dc=dataone,dc=org");
109
            session.setSubject(subject);
137
            Session session = getTestSession();
110 138
			Identifier guid = new Identifier();
111 139
			guid.setValue("testRegisterSystemMetadata." + System.currentTimeMillis());
112
			SystemMetadata sysmeta = createSystemMetadata(guid, subject);
113
			CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
140
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
141
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
142
			assertTrue(result);
143
        } catch(Exception e) {
144
            fail("Unexpected error: " + e.getMessage());
114 145
        }
115
        catch(Exception e)
116
        {
117
            fail("Unexpected error in testDescribe: " + e.getMessage());
146
	}
147
	
148
	public void testCreate() {
149
	    printTestHeader("testCreate");
150

  
151
	    try {
152
            Session session = getTestSession();
153
			Identifier guid = new Identifier();
154
			guid.setValue("testCreate." + System.currentTimeMillis());
155
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
156
			InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
157
			Identifier pid = CNodeService.getInstance().create(session, guid, object, sysmeta);
158
			assertEquals(guid, pid);
159
        } catch(Exception e) {
160
        	e.printStackTrace();
161
            fail("Unexpected error: " + e.getMessage());
118 162
        }
119 163
	}
120 164
	
165
	public void testAssertRelation() {
166
	    printTestHeader("testAssertRelation");
167

  
168
	    try {
169
            Session session = getTestSession();
170
			Identifier guid = new Identifier();
171
			guid.setValue("testAssertRelation." + System.currentTimeMillis());
172
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
173
			Identifier describePid = new Identifier();
174
			describePid.setValue("describePid." + System.currentTimeMillis());
175
			sysmeta.addDescribe(describePid);
176
			// save it
177
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
178
			assertTrue(result);
179
			// save the other
180
			SystemMetadata describeSysmeta = createSystemMetadata(describePid, session.getSubject());
181
			result = CNodeService.getInstance().registerSystemMetadata(session, describePid, describeSysmeta);
182
			assertTrue(result);
183
			// check it
184
			result = CNodeService.getInstance().assertRelation(session, guid, "describes", describePid);
185
			assertTrue(result);
186
        } catch(Exception e) {
187
        	e.printStackTrace();
188
            fail("Unexpected error: " + e.getMessage());
189
        }
190
	}
191
	
192
	public void testChecksum() {
193
	    printTestHeader("testChecksum");
194

  
195
	    try {
196
            Session session = getTestSession();
197
			Identifier guid = new Identifier();
198
			guid.setValue("testChecksum." + System.currentTimeMillis());
199
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
200
			// save it
201
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
202
			assertTrue(result);
203
			// check it
204
			Checksum checksum = CNodeService.getInstance().getChecksum(session, guid);
205
			assertEquals(sysmeta.getChecksum().getValue(), checksum.getValue());
206
        } catch(Exception e) {
207
            fail("Unexpected error: " + e.getMessage());
208
        }
209
	}
210
	
211
	public void testListNodes() {
212
	    printTestHeader("testListNodes");
213

  
214
	    try {
215
	    	CNodeService.getInstance().listNodes();
216
        } catch(NotImplemented e) {
217
        	// expecting not implemented
218
            assertTrue(true);
219
        } catch(Exception e) {
220
            fail("Unexpected error: " + e.getMessage());
221
        }
222
	}
223
	
224
	public void testReserveIdentifier() {
225
	    printTestHeader("testReserveIdentifier");
226

  
227
	    try {
228
            Session session = getTestSession();
229
			Identifier guid = new Identifier();
230
			guid.setValue("testReserveIdentifier." + System.currentTimeMillis());
231
			// reserve it
232
			Identifier retPid = CNodeService.getInstance().reserveIdentifier(session, guid, null, null);
233
			assertEquals(guid, retPid);
234
        } catch(Exception e) {
235
        	e.printStackTrace();
236
            fail("Unexpected error: " + e.getMessage());
237
        }
238
	}
239
	
240
	public void testResolve() {
241
	    printTestHeader("testResolve");
242

  
243
	    try {
244
            Session session = getTestSession();
245
			Identifier guid = new Identifier();
246
			guid.setValue("testResolve." + System.currentTimeMillis());
247
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
248
			Replica replica = new Replica();
249
			NodeReference replicaMemberNode = new NodeReference();
250
			replicaMemberNode.setValue("testNode");
251
			replica.setReplicaMemberNode(replicaMemberNode);
252
			replica.setReplicationStatus(ReplicationStatus.QUEUED);
253
			replica.setReplicaVerified(new Date());
254
			sysmeta.addReplica(replica );
255
			// save it
256
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
257
			assertTrue(result);
258
			// resolve it
259
			ObjectLocationList locationList = CNodeService.getInstance().resolve(session, guid);
260
			// check it
261
			assertEquals(replicaMemberNode, locationList.getObjectLocation(0).getNodeIdentifier());
262
			
263
        } catch(Exception e) {
264
        	e.printStackTrace();
265
            fail("Unexpected error: " + e.getMessage());
266
        }
267
	}
268
	
269
	public void testSearch() {
270
	    printTestHeader("testSearch");
271

  
272
	    try {
273
            Session session = getTestSession();
274
			Identifier guid = new Identifier();
275
			guid.setValue("testSearch." + System.currentTimeMillis());
276
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
277
			
278
			// save it
279
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
280
			assertTrue(result);
281
			// search for it
282
			ObjectList objectList = CNodeService.getInstance().search(session, null, null);
283
			// check it
284
			assertNotNull(objectList);
285
			result = false;
286
			for (ObjectInfo objectInfo: objectList.getObjectInfoList()) {
287
				Identifier pid = objectInfo.getIdentifier();
288
				if (pid.getValue().equals(guid.getValue())) {
289
					result = true;
290
					break;
291
				}
292
			}
293
			assertTrue(result);
294
        } catch(Exception e) {
295
            fail("Unexpected error: " + e.getMessage());
296
        }
297
	}
298
	
299
	public void testSetOwner() {
300
	    printTestHeader("testSetOwner");
301

  
302
	    try {
303
            Session session = getTestSession();
304
			Identifier guid = new Identifier();
305
			guid.setValue("testSetOwner." + System.currentTimeMillis());
306
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
307
			// save it
308
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
309
			assertTrue(result);
310
			Subject rightsHolder = new Subject();
311
			rightsHolder.setValue("newUser");
312
			// set it
313
			Identifier retPid = CNodeService.getInstance().setOwner(session, guid, rightsHolder);
314
			assertEquals(guid, retPid);
315
			// get it
316
			sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
317
			assertNotNull(sysmeta);
318
			// check it
319
			assertEquals(rightsHolder.getValue(), sysmeta.getRightsHolder().getValue());
320
			
321
        } catch(Exception e) {
322
            fail("Unexpected error: " + e.getMessage());
323
        }
324
	}
325
	
326
	public void testReplicationPolicy() {
327
	    printTestHeader("testReplicationPolicy");
328

  
329
	    try {
330
            Session session = getTestSession();
331
			Identifier guid = new Identifier();
332
			guid.setValue("testReplicationPolicy." + System.currentTimeMillis());
333
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
334
			// save it
335
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
336
			assertTrue(result);
337
			
338
			ReplicationPolicy policy = new ReplicationPolicy();
339
			NodeReference node = new NodeReference();
340
			node.setValue("testNode");
341
			policy.addPreferredMemberNode(node );
342
			// set it
343
			result = CNodeService.getInstance().setReplicationPolicy(session, guid, policy);
344
			assertTrue(result);
345
			// get it
346
			sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
347
			assertNotNull(sysmeta);
348
			// check it
349
			assertEquals(policy.getPreferredMemberNode(0).getValue(), sysmeta.getReplicationPolicy().getPreferredMemberNode(0).getValue());
350
			
351
        } catch(Exception e) {
352
            fail("Unexpected error: " + e.getMessage());
353
        }
354
	}
355
	
356
	public void testReplicationStatus() {
357
	    printTestHeader("testReplicationStatus");
358

  
359
	    try {
360
            Session session = getTestSession();
361
			Identifier guid = new Identifier();
362
			guid.setValue("testReplicationStatus." + System.currentTimeMillis());
363
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
364
			Replica replica = new Replica();
365
			NodeReference replicaMemberNode = new NodeReference();
366
			replicaMemberNode.setValue("testNode");
367
			replica.setReplicationStatus(ReplicationStatus.REQUESTED);
368
			replica.setReplicaMemberNode(replicaMemberNode);
369
			replica.setReplicaVerified(new Date());
370
			sysmeta.addReplica(replica );
371
			// save it
372
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
373
			assertTrue(result);
374
			// set it
375
			ReplicationStatus status = ReplicationStatus.COMPLETED;
376
			result = CNodeService.getInstance().setReplicationStatus(session, guid, status);
377
			assertTrue(result);
378
			// get it
379
			sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
380
			assertNotNull(sysmeta);
381
			// check it
382
			assertEquals(status, sysmeta.getReplica(0).getReplicationStatus());
383
			
384
        } catch(Exception e) {
385
            fail("Unexpected error: " + e.getMessage());
386
        }
387
	}
388
	
121 389
	/**
122 390
	 * Run an initial test that always passes to check that the test harness is
123 391
	 * working.
......
135 403
	  throws Exception
136 404
	{
137 405
	    SystemMetadata sm = new SystemMetadata();
138
        //set the id
406
        // set the id
139 407
        sm.setIdentifier(id);
140 408
        sm.setObjectFormat(ObjectFormatCache.getInstance().getFormat("application/octet-stream"));
141
        //create the checksum
409
        // create the checksum
142 410
        String checksumS = "test";
143 411
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
144 412
        Checksum checksum = new Checksum();
145 413
        checksum.setValue(checksumS);
146 414
        checksum.setAlgorithm(ca);
147 415
        sm.setChecksum(checksum);
148
        //set the size
416
        // set the size
149 417
        sm.setSize(0);
150 418
        sm.setSubmitter(owner);
151 419
        sm.setRightsHolder(owner);
......
155 423
        nr.setValue("metacat");
156 424
        sm.setOriginMemberNode(nr);
157 425
        sm.setAuthoritativeMemberNode(nr);
426
		// set the access to public read
427
        AccessPolicy accessPolicy = new AccessPolicy();
428
        AccessRule allow = new AccessRule();
429
        allow.addPermission(Permission.READ);
430
        Subject publicSubject = new Subject();
431
        publicSubject.setValue(Constants.PUBLIC_SUBJECT);
432
		allow.addSubject(publicSubject);
433
		accessPolicy.addAllow(allow);
434
        sm.setAccessPolicy(accessPolicy);
435
        
158 436
        return sm;
159 437
	}
160 438

  

Also available in: Unified diff