Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 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: 2012-12-12 14:38:23 -0800 (Wed, 12 Dec 2012) $'
9
 * '$Revision: 7476 $'
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.replication;
27

    
28
import java.io.InputStream;
29
import java.io.Reader;
30
import java.io.StringReader;
31
import java.net.URL;
32
import java.util.Hashtable;
33

    
34
import junit.framework.Test;
35
import junit.framework.TestSuite;
36

    
37
import org.apache.commons.io.IOUtils;
38
import org.xml.sax.InputSource;
39
import org.xml.sax.XMLReader;
40

    
41
import edu.ucsb.nceas.MCTestCase;
42
import edu.ucsb.nceas.metacat.MetaCatServlet;
43
import edu.ucsb.nceas.metacat.client.Metacat;
44
import edu.ucsb.nceas.metacat.client.MetacatFactory;
45
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
46
import edu.ucsb.nceas.metacat.properties.PropertyService;
47
import edu.ucsb.nceas.metacat.util.DocumentUtil;
48
import edu.ucsb.nceas.metacat.util.MetacatUtil;
49
import edu.ucsb.nceas.metacat.util.ReplicationUtil;
50

    
51
import edu.ucsb.nceas.utilities.access.AccessControlInterface;
52
import edu.ucsb.nceas.utilities.access.DocInfoHandler;
53

    
54

    
55
/**
56
 * A JUnit test for testing Metacat replication
57
 */
58
public class ReplicationTest
59
    extends MCTestCase {
60
    
61
    private static final long forceReplicationSleep = 1 * 60 * 1000;
62
	private String targetReplicationServer = null;
63
	private Metacat targetMetacat = null;
64
	private final static String TITLE = "Test replication";
65
    
66
	/**
67
     * Constructor to build the test
68
     *
69
     * @param name the name of the test method
70
     */
71
    public ReplicationTest(String name) {
72
        super(name);
73
    }
74

    
75
    /**
76
     * Establish a testing framework by initializing appropriate objects
77
     */
78
    public void setUp() {
79
        try {
80
        	// get the target ("B server")
81
            targetReplicationServer = PropertyService.getProperty("test.replication.targetServer");
82
            m = MetacatFactory.createMetacatConnection(metacatUrl);
83
            targetMetacat = MetacatFactory.createMetacatConnection("https://" + targetReplicationServer + "/metacat");
84

    
85
        }
86
        catch (MetacatInaccessibleException mie) {
87
            System.err.println("Metacat is: " + metacatUrl);
88
            fail("Metacat connection failed." + mie.getMessage());
89
        } catch (Exception e) {
90
        	e.printStackTrace();
91
            fail(e.getMessage());
92
        }
93
    }
94

    
95
    /**
96
     * Release any objects after tests are complete
97
     */
98
    public void tearDown() {
99
    }
100

    
101
    /**
102
     * Create a suite of tests to be run together
103
     */
104
    public static Test suite() {
105
        TestSuite suite = new TestSuite();
106
        suite.addTest(new ReplicationTest("initialize"));
107
        // Test basic functions
108
        suite.addTest(new ReplicationTest("testCertificate"));
109
        suite.addTest(new ReplicationTest("testReplicateData_AtoB"));
110
        suite.addTest(new ReplicationTest("testReplicateEML_AtoB"));
111
        suite.addTest(new ReplicationTest("testReplicateDataLocking"));
112
        suite.addTest(new ReplicationTest("testDocumentInfo"));
113
        
114
        return suite;
115
    }
116

    
117
    /**
118
     * Run an initial test that always passes to check that the test
119
     * harness is working.
120
     */
121
    public void initialize() {
122
        assertTrue(1 == 1);
123
    }
124
    
125
    public void testCertificate() {
126
    	try {
127
    	  //System.out.println("test certificate ============");
128
    		URL u = new URL("https://" + targetReplicationServer  + "/servlet/replication?server=" + MetacatUtil.getLocalReplicationServerName() + "&action=test");
129
			String test = ReplicationService.getURLContent(u);
130
			//System.out.println("the result is "+test);
131
			assertTrue(test.contains("Test successfully"));
132
			
133
    	} catch (Exception e) {
134
    		e.printStackTrace();
135
			fail(e.getMessage());
136
		}
137
    }
138
    
139
    public void testReplicateData_AtoB() {
140
    	try {
141
    		// the id
142
    		String baseDocid = DocumentUtil.generateDocumentId("replicationTest", 0);
143
    		String docid = baseDocid + "." + 1;
144
    		
145
    		// the test data
146
    		String object = "test";
147
    		
148
			// insert data locally
149
    		m.login(username, password);
150
    		m.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
151
    		
152
    		// wait for replication (forced)
153
    		Thread.sleep(forceReplicationSleep);
154
    		
155
    		// check the target for the same data
156
    		targetMetacat.login(username, password);
157
    		InputStream is = targetMetacat.read(docid);
158
    		String replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
159
    		
160
    		assertEquals(object, replicatedObject);
161
    		
162
    		// update the object
163
    		docid = baseDocid + "." + 2;
164
    		object = "test2";
165
    		m.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
166

    
167
    		// wait for replication (forced)
168
    		Thread.sleep(forceReplicationSleep);
169
    		
170
    		// check the target for the updated data
171
    		is = targetMetacat.read(docid);
172
    		replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
173
    		
174
    		assertEquals(object, replicatedObject);
175
    		
176
    		// update the access control rules
177
    		m.setAccess(
178
    				docid, 
179
    				AccessControlInterface.PUBLIC, 
180
    				AccessControlInterface.READSTRING, 
181
    				AccessControlInterface.ALLOW, 
182
    				AccessControlInterface.ALLOWFIRST);
183
    		
184
    		// wait for replication (forced)
185
    		Thread.sleep(forceReplicationSleep);
186
    		
187
    		// check the target for the same data, logout to act as public
188
    		targetMetacat.logout();
189
    		is = targetMetacat.read(docid);
190
    		replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
191
    		
192
    		assertEquals(object, replicatedObject);
193
    		
194
    		// delete the object
195
    		m.delete(docid);
196
    		
197
    		// wait for replication (forced)
198
    		Thread.sleep(forceReplicationSleep);
199
    		
200
    		// TODO: check that it is missing
201
    		// update should fail since it is "archived"
202
    		docid = baseDocid + "." + 3;
203
    		object = "test3";
204
    		try {
205
    			targetMetacat.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
206
    		} catch (Exception e) {
207
				// should fail
208
    			assertTrue(true);
209
    			return;
210
			}
211
    		// if we get here, he have failed
212
    		fail("Should not have been able to update archived data");
213
    		
214
			
215
    	} catch (Exception e) {
216
    		e.printStackTrace();
217
			fail(e.getMessage());
218
		}
219
    }
220
    
221
    public void testReplicateEML_AtoB() {
222
    	try {
223
    		// the id
224
    		String baseDocid = DocumentUtil.generateDocumentId("replicationTest", 0);
225
    		String docid = baseDocid + "." + 1;
226
    		
227
    		// the test data, no public access
228
    		String emlContent = null;
229
			emlContent = getTestEmlDoc(
230
    				TITLE, //title, 
231
    				EML2_1_0, //emlVersion, 
232
    				null, //inlineData1, 
233
    				null, //inlineData2, 
234
    				null, //onlineUrl1, 
235
    				null, //onlineUrl2, 
236
    				null, //docAccessBlock , 
237
    				null, //inlineAccessBlock1, 
238
    				null, //inlineAccessBlock2, 
239
    				null, //onlineAccessBlock1, 
240
    				null //onlineAccessBlock2
241
    				);
242
    				
243
    		StringReader xmlReader = new StringReader(emlContent);
244
    		
245
			// insert data locally
246
    		m.login(username, password);
247
    		m.insert(docid, xmlReader, null);
248
    		
249
    		// wait for replication (forced)
250
    		Thread.sleep(forceReplicationSleep);
251
    		
252
    		// check the target for the same data
253
    		targetMetacat.login(username, password);
254
    		InputStream is = targetMetacat.read(docid);
255
    		String replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
256
    		
257
    		assertEquals(emlContent, replicatedObject);
258
    		
259
    		// update the object
260
    		docid = baseDocid + "." + 2;
261
    		//emlContent = getTestEmlDoc("Test replication2", EML2_1_0);
262
    		emlContent = getTestEmlDoc(
263
    				TITLE, //title, 
264
    				EML2_1_0, //emlVersion, 
265
    				null, //inlineData1, 
266
    				null, //inlineData2, 
267
    				null, //onlineUrl1, 
268
    				null, //onlineUrl2, 
269
    				null, //docAccessBlock , 
270
    				null, //inlineAccessBlock1, 
271
    				null, //inlineAccessBlock2, 
272
    				null, //onlineAccessBlock1, 
273
    				null //onlineAccessBlock2
274
    				);
275
    		xmlReader = new StringReader(emlContent);
276
    		m.update(docid, xmlReader, null);
277

    
278
    		// wait for replication (forced)
279
    		Thread.sleep(forceReplicationSleep);
280
    		
281
    		// check the target for the updated data
282
    		is = targetMetacat.read(docid);
283
    		replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
284
    		
285
    		assertEquals(emlContent, replicatedObject);
286
    		
287
    		// update the access control rules
288
    		m.setAccess(
289
    				docid, 
290
    				AccessControlInterface.PUBLIC, 
291
    				AccessControlInterface.READSTRING, 
292
    				AccessControlInterface.ALLOW, 
293
    				AccessControlInterface.ALLOWFIRST);
294
    		
295
    		// wait for replication (forced)
296
    		Thread.sleep(forceReplicationSleep);
297
    		
298
    		// check the target for the same data, logout to act as public
299
    		targetMetacat.logout();
300
    		is = targetMetacat.read(docid);
301
    		replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
302
    		
303
    		assertEquals(emlContent, replicatedObject);
304
    		
305
    		// delete the object
306
    		m.delete(docid);
307
    		
308
    		// wait for replication (forced)
309
    		Thread.sleep(forceReplicationSleep);
310
    		
311
			// query for the docid -- should not be returned since it is archived
312
			String queryString = getTestEmlQuery(TITLE, EML2_1_0);
313
			System.out.println("queryString: " + queryString);
314
			Reader xmlQuery = new StringReader(queryString);
315
			Reader resultReader = targetMetacat.query(xmlQuery);
316
			String results = IOUtils.toString(resultReader);
317
			System.out.println("results: " + results);
318
			assertFalse(results.contains(docid));
319
			
320
    	} catch (Exception e) {
321
    		e.printStackTrace();
322
			fail(e.getMessage());
323
		}
324
    }
325

    
326
	public void testReplicateDataLocking() {
327
		try {
328
			// the id
329
			String baseDocid = DocumentUtil.generateDocumentId("replicationTest", 0);
330
			String docid = baseDocid + "." + 1;
331
			
332
			// the test data
333
			String object = "test";
334
			
335
			// insert data locally
336
			m.login(username, password);
337
			m.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
338
			
339
			// wait for replication (forced)
340
			Thread.sleep(forceReplicationSleep);
341
			
342
			// check the target for the same data
343
			targetMetacat.login(username, password);
344
			InputStream is = targetMetacat.read(docid);
345
			String replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
346
			
347
			assertEquals(object, replicatedObject);
348
			
349
			// update the object on the target
350
			docid = baseDocid + "." + 2;
351
			object = "test2";
352
			targetMetacat.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
353
	
354
			// wait for replication (forced)
355
			Thread.sleep(forceReplicationSleep);
356
			
357
			// check the original has the result
358
			is = m.read(docid);
359
			replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
360
			
361
			assertEquals(object, replicatedObject);
362
			
363
			
364
		} catch (Exception e) {
365
			e.printStackTrace();
366
			fail(e.getMessage());
367
		}
368
	}
369
	
370
	/**
371
	 * Tests the docInfo parser using reserved characters (&) in CDATA tags
372
	 */
373
	public void testDocumentInfo() {
374
		try {
375
			// the id
376
			String baseDocid = DocumentUtil.generateDocumentId("replicationTest", 0);
377
			String docid = baseDocid + "." + 1;
378
			
379
			// the test data
380
			String object = "test";
381
			String fileName = "testObject with & in filename";
382
			
383
			// insert data locally
384
			m.login(username, password);
385
			m.upload(docid, fileName, IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
386
    		
387
    		// get the docinfo string
388
    		String docInfoStr = ReplicationService.getDocumentInfo(docid);
389
    					
390
    		System.out.println("docInfoStr: " + docInfoStr);
391
    		
392
			// strip out the system metadata portion
393
		    String systemMetadataXML = ReplicationUtil.getSystemMetadataContent(docInfoStr);
394
		   	docInfoStr = ReplicationUtil.getContentWithoutSystemMetadata(docInfoStr);
395

    
396
			//dih is the parser for the docinfo xml format
397
			DocInfoHandler dih = new DocInfoHandler();
398
			XMLReader docinfoParser = ReplicationHandler.initParser(dih);
399
			docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
400
			Hashtable<String, String> docinfoHash = dih.getDocInfo();
401
			
402
			// get the docname for testing &
403
			String docName = (String) docinfoHash.get("docname");
404
    		System.out.println("docName: " + docName);
405
    		
406
			assertEquals(fileName, docName);
407

    
408
    		// test user
409
    		String user = (String) docinfoHash.get("user_owner");
410
    		System.out.println("user_owner: " + user);
411
			assertEquals(username, user);
412

    
413
		} catch (Exception e) {
414
			e.printStackTrace();
415
			fail(e.getMessage());
416
		}
417
	}
418

    
419
    
420
}
421

    
    (1-1/1)