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: 2011-12-15 17:32:39 -0800 (Thu, 15 Dec 2011) $'
9
 * '$Revision: 6790 $'
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

    
33
import org.apache.commons.io.IOUtils;
34

    
35
import junit.framework.Test;
36
import junit.framework.TestSuite;
37
import edu.ucsb.nceas.MCTestCase;
38
import edu.ucsb.nceas.metacat.MetaCatServlet;
39
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
40
import edu.ucsb.nceas.metacat.client.DocumentNotFoundException;
41
import edu.ucsb.nceas.metacat.client.Metacat;
42
import edu.ucsb.nceas.metacat.client.MetacatFactory;
43
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
44
import edu.ucsb.nceas.metacat.properties.PropertyService;
45
import edu.ucsb.nceas.metacat.util.DocumentUtil;
46
import edu.ucsb.nceas.metacat.util.MetacatUtil;
47

    
48
/**
49
 * A JUnit test for testing Metacat replication
50
 */
51
public class ReplicationTest
52
    extends MCTestCase {
53
    
54
    private static final long forceReplicationSleep = 1 * 60 * 1000;
55
	private String targetReplicationServer = null;
56
	private Metacat targetMetacat = null;
57
    
58
	/**
59
     * Constructor to build the test
60
     *
61
     * @param name the name of the test method
62
     */
63
    public ReplicationTest(String name) {
64
        super(name);
65
    }
66

    
67
    /**
68
     * Establish a testing framework by initializing appropriate objects
69
     */
70
    public void setUp() {
71
        try {
72
        	// get the target ("B server")
73
            targetReplicationServer = PropertyService.getProperty("test.replication.targetServer");
74
            m = MetacatFactory.createMetacatConnection(metacatUrl);
75
            targetMetacat = MetacatFactory.createMetacatConnection("https://" + targetReplicationServer + "/metacat");
76

    
77
        }
78
        catch (MetacatInaccessibleException mie) {
79
            System.err.println("Metacat is: " + metacatUrl);
80
            fail("Metacat connection failed." + mie.getMessage());
81
        } catch (Exception e) {
82
        	e.printStackTrace();
83
            fail(e.getMessage());
84
        }
85
    }
86

    
87
    /**
88
     * Release any objects after tests are complete
89
     */
90
    public void tearDown() {
91
    }
92

    
93
    /**
94
     * Create a suite of tests to be run together
95
     */
96
    public static Test suite() {
97
        TestSuite suite = new TestSuite();
98
        suite.addTest(new ReplicationTest("initialize"));
99
        // Test basic functions
100
        suite.addTest(new ReplicationTest("testCertificate"));
101
        suite.addTest(new ReplicationTest("testReplicateData_AtoB"));
102
        suite.addTest(new ReplicationTest("testReplicateEML_AtoB"));
103
        suite.addTest(new ReplicationTest("testReplicateDataLocking"));
104

    
105
        return suite;
106
    }
107

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

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

    
267
    		// wait for replication (forced)
268
    		Thread.sleep(forceReplicationSleep);
269
    		
270
    		// check the target for the updated data
271
    		is = targetMetacat.read(docid);
272
    		replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
273
    		
274
    		assertEquals(emlContent, replicatedObject);
275
    		
276
    		// update the access control rules
277
    		m.setAccess(
278
    				docid, 
279
    				AccessControlInterface.PUBLIC, 
280
    				AccessControlInterface.READSTRING, 
281
    				AccessControlInterface.ALLOW, 
282
    				AccessControlInterface.ALLOWFIRST);
283
    		
284
    		// wait for replication (forced)
285
    		Thread.sleep(forceReplicationSleep);
286
    		
287
    		// check the target for the same data, logout to act as public
288
    		targetMetacat.logout();
289
    		is = targetMetacat.read(docid);
290
    		replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
291
    		
292
    		assertEquals(emlContent, replicatedObject);
293
    		
294
    		// delete the object
295
    		m.delete(docid);
296
    		
297
    		// wait for replication (forced)
298
    		Thread.sleep(forceReplicationSleep);
299
    		
300
			// query for the docid -- should not be returned since it is archived
301
			String queryString = getTestEmlQuery(docid, EML2_1_0);
302
			Reader xmlQuery = new StringReader(queryString);
303
			Reader resultReader = targetMetacat.query(xmlQuery);
304
			String results = IOUtils.toString(resultReader);
305
			assertFalse(results.contains(docid));
306
			
307
    	} catch (Exception e) {
308
    		e.printStackTrace();
309
			fail(e.getMessage());
310
		}
311
    }
312

    
313
	public void testReplicateDataLocking() {
314
		try {
315
			// the id
316
			String baseDocid = DocumentUtil.generateDocumentId("replicationTest", 0);
317
			String docid = baseDocid + "." + 1;
318
			
319
			// the test data
320
			String object = "test";
321
			
322
			// insert data locally
323
			m.login(username, password);
324
			m.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
325
			
326
			// wait for replication (forced)
327
			Thread.sleep(forceReplicationSleep);
328
			
329
			// check the target for the same data
330
			targetMetacat.login(username, password);
331
			InputStream is = targetMetacat.read(docid);
332
			String replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
333
			
334
			assertEquals(object, replicatedObject);
335
			
336
			// update the object on the target
337
			docid = baseDocid + "." + 2;
338
			object = "test2";
339
			targetMetacat.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
340
	
341
			// wait for replication (forced)
342
			Thread.sleep(forceReplicationSleep);
343
			
344
			// check the original has the result
345
			is = m.read(docid);
346
			replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
347
			
348
			assertEquals(object, replicatedObject);
349
			
350
			// delete the object
351
			targetMetacat.delete(docid);
352
			
353
			// wait for replication (forced)
354
			Thread.sleep(forceReplicationSleep);
355
			
356
			// check that it is missing
357
			replicatedObject = null;
358
			try {
359
	    		is = m.read(docid);
360
				replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
361
			} catch (DocumentNotFoundException dnfe) {
362
				// expect this
363
				assertTrue(true);
364
			}
365
			assertNull(replicatedObject);
366
			
367
			
368
		} catch (Exception e) {
369
			e.printStackTrace();
370
			fail(e.getMessage());
371
		}
372
	}
373

    
374
    
375
}
376

    
    (1-1/1)