Project

General

Profile

1 6620 leinfelder
/**
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$'
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.replication;
27
28 6770 leinfelder
import java.io.InputStream;
29 6784 leinfelder
import java.io.StringReader;
30 6620 leinfelder
import java.net.URL;
31
32 6770 leinfelder
import org.apache.commons.io.IOUtils;
33
34 6620 leinfelder
import junit.framework.Test;
35
import junit.framework.TestSuite;
36
import edu.ucsb.nceas.MCTestCase;
37 6770 leinfelder
import edu.ucsb.nceas.metacat.MetaCatServlet;
38 6778 leinfelder
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
39 6788 leinfelder
import edu.ucsb.nceas.metacat.client.DocumentNotFoundException;
40 6770 leinfelder
import edu.ucsb.nceas.metacat.client.Metacat;
41 6620 leinfelder
import edu.ucsb.nceas.metacat.client.MetacatFactory;
42
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
43
import edu.ucsb.nceas.metacat.properties.PropertyService;
44 6770 leinfelder
import edu.ucsb.nceas.metacat.util.DocumentUtil;
45 6620 leinfelder
import edu.ucsb.nceas.metacat.util.MetacatUtil;
46
47
/**
48
 * A JUnit test for testing Metacat replication
49
 */
50
public class ReplicationTest
51
    extends MCTestCase {
52
53 6776 leinfelder
    private static final long forceReplicationSleep = 1 * 60 * 1000;
54 6770 leinfelder
	private String targetReplicationServer = null;
55
	private Metacat targetMetacat = null;
56 6620 leinfelder
57
	/**
58
     * Constructor to build the test
59
     *
60
     * @param name the name of the test method
61
     */
62
    public ReplicationTest(String name) {
63
        super(name);
64
    }
65
66
    /**
67
     * Establish a testing framework by initializing appropriate objects
68
     */
69
    public void setUp() {
70
        try {
71 6770 leinfelder
        	// get the target ("B server")
72
            targetReplicationServer = PropertyService.getProperty("test.replication.targetServer");
73 6620 leinfelder
            m = MetacatFactory.createMetacatConnection(metacatUrl);
74 6771 leinfelder
            targetMetacat = MetacatFactory.createMetacatConnection("https://" + targetReplicationServer + "/metacat");
75 6770 leinfelder
76 6620 leinfelder
        }
77
        catch (MetacatInaccessibleException mie) {
78
            System.err.println("Metacat is: " + metacatUrl);
79
            fail("Metacat connection failed." + mie.getMessage());
80
        } catch (Exception e) {
81
        	e.printStackTrace();
82
            fail(e.getMessage());
83
        }
84
    }
85
86
    /**
87
     * Release any objects after tests are complete
88
     */
89
    public void tearDown() {
90
    }
91
92
    /**
93
     * Create a suite of tests to be run together
94
     */
95
    public static Test suite() {
96
        TestSuite suite = new TestSuite();
97
        suite.addTest(new ReplicationTest("initialize"));
98
        // Test basic functions
99
        suite.addTest(new ReplicationTest("testCertificate"));
100 6770 leinfelder
        suite.addTest(new ReplicationTest("testReplicateData_AtoB"));
101 6785 leinfelder
        suite.addTest(new ReplicationTest("testReplicateEML_AtoB"));
102 6789 leinfelder
        suite.addTest(new ReplicationTest("testReplicateDataLocking"));
103 6770 leinfelder
104 6620 leinfelder
        return suite;
105
    }
106
107
    /**
108
     * Run an initial test that always passes to check that the test
109
     * harness is working.
110
     */
111
    public void initialize() {
112
        assertTrue(1 == 1);
113
    }
114
115
    public void testCertificate() {
116
    	try {
117 6771 leinfelder
    		URL u = new URL("https://" + targetReplicationServer  + "/servlet/replication?server=" + MetacatUtil.getLocalReplicationServerName() + "&action=test");
118 6620 leinfelder
			String test = ReplicationService.getURLContent(u);
119
			assertTrue(test.contains("Test successfully"));
120
121
    	} catch (Exception e) {
122
    		e.printStackTrace();
123
			fail(e.getMessage());
124
		}
125
    }
126 6770 leinfelder
127
    public void testReplicateData_AtoB() {
128
    	try {
129
    		// the id
130 6778 leinfelder
    		String baseDocid = DocumentUtil.generateDocumentId("replicationTest", 0);
131
    		String docid = baseDocid + "." + 1;
132 6770 leinfelder
133
    		// the test data
134
    		String object = "test";
135
136
			// insert data locally
137
    		m.login(username, password);
138
    		m.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
139
140
    		// wait for replication (forced)
141
    		Thread.sleep(forceReplicationSleep);
142
143
    		// check the target for the same data
144
    		targetMetacat.login(username, password);
145
    		InputStream is = targetMetacat.read(docid);
146
    		String replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
147
148
    		assertEquals(object, replicatedObject);
149 6778 leinfelder
150
    		// update the object
151
    		docid = baseDocid + "." + 2;
152
    		object = "test2";
153
    		m.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
154
155
    		// wait for replication (forced)
156
    		Thread.sleep(forceReplicationSleep);
157
158
    		// check the target for the updated data
159
    		is = targetMetacat.read(docid);
160
    		replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
161
162
    		assertEquals(object, replicatedObject);
163
164
    		// update the access control rules
165
    		m.setAccess(
166
    				docid,
167
    				AccessControlInterface.PUBLIC,
168
    				AccessControlInterface.READSTRING,
169
    				AccessControlInterface.ALLOW,
170
    				AccessControlInterface.ALLOWFIRST);
171
172
    		// wait for replication (forced)
173
    		Thread.sleep(forceReplicationSleep);
174
175
    		// check the target for the same data, logout to act as public
176
    		targetMetacat.logout();
177
    		is = targetMetacat.read(docid);
178
    		replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
179
180
    		assertEquals(object, replicatedObject);
181 6788 leinfelder
182
    		// delete the object
183
    		m.delete(docid);
184
185
    		// wait for replication (forced)
186
    		Thread.sleep(forceReplicationSleep);
187
188
    		// check that it is missing
189
			replicatedObject = null;
190
    		try {
191
        		is = targetMetacat.read(docid);
192
    			replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
193
    		} catch (DocumentNotFoundException dnfe) {
194
    			// expect this
195
    			assertTrue(true);
196
    		}
197
    		assertNull(replicatedObject);
198
199 6770 leinfelder
200
    	} catch (Exception e) {
201
    		e.printStackTrace();
202
			fail(e.getMessage());
203
		}
204
    }
205
206 6784 leinfelder
    public void testReplicateEML_AtoB() {
207
    	try {
208
    		// the id
209
    		String baseDocid = DocumentUtil.generateDocumentId("replicationTest", 0);
210
    		String docid = baseDocid + "." + 1;
211
212
    		// the test data, no public access
213
    		String emlContent = null;
214
			emlContent = getTestEmlDoc(
215
    				"Test replication", //title,
216
    				EML2_1_0, //emlVersion,
217
    				null, //inlineData1,
218
    				null, //inlineData2,
219
    				null, //onlineUrl1,
220
    				null, //onlineUrl2,
221
    				null, //docAccessBlock ,
222
    				null, //inlineAccessBlock1,
223
    				null, //inlineAccessBlock2,
224
    				null, //onlineAccessBlock1,
225
    				null //onlineAccessBlock2
226
    				);
227
228
    		StringReader xmlReader = new StringReader(emlContent);
229
230
			// insert data locally
231
    		m.login(username, password);
232
    		m.insert(docid, xmlReader, null);
233
234
    		// wait for replication (forced)
235
    		Thread.sleep(forceReplicationSleep);
236
237
    		// check the target for the same data
238
    		targetMetacat.login(username, password);
239
    		InputStream is = targetMetacat.read(docid);
240
    		String replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
241
242
    		assertEquals(emlContent, replicatedObject);
243
244
    		// update the object
245
    		docid = baseDocid + "." + 2;
246
    		//emlContent = getTestEmlDoc("Test replication2", EML2_1_0);
247
    		emlContent = getTestEmlDoc(
248
    				"Test replication", //title,
249
    				EML2_1_0, //emlVersion,
250
    				null, //inlineData1,
251
    				null, //inlineData2,
252
    				null, //onlineUrl1,
253
    				null, //onlineUrl2,
254
    				null, //docAccessBlock ,
255
    				null, //inlineAccessBlock1,
256
    				null, //inlineAccessBlock2,
257
    				null, //onlineAccessBlock1,
258
    				null //onlineAccessBlock2
259
    				);
260
    		xmlReader = new StringReader(emlContent);
261
    		m.update(docid, xmlReader, null);
262
263
    		// wait for replication (forced)
264
    		Thread.sleep(forceReplicationSleep);
265
266
    		// check the target for the updated data
267
    		is = targetMetacat.read(docid);
268
    		replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
269
270
    		assertEquals(emlContent, replicatedObject);
271
272
    		// update the access control rules
273
    		m.setAccess(
274
    				docid,
275
    				AccessControlInterface.PUBLIC,
276
    				AccessControlInterface.READSTRING,
277
    				AccessControlInterface.ALLOW,
278
    				AccessControlInterface.ALLOWFIRST);
279
280
    		// wait for replication (forced)
281
    		Thread.sleep(forceReplicationSleep);
282
283
    		// check the target for the same data, logout to act as public
284
    		targetMetacat.logout();
285
    		is = targetMetacat.read(docid);
286
    		replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
287
288
    		assertEquals(emlContent, replicatedObject);
289 6770 leinfelder
290 6788 leinfelder
    		// delete the object
291
    		m.delete(docid);
292 6770 leinfelder
293
    		// wait for replication (forced)
294
    		Thread.sleep(forceReplicationSleep);
295
296 6788 leinfelder
    		// check that it is missing
297
			replicatedObject = null;
298
    		try {
299
        		is = targetMetacat.read(docid);
300
    			replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
301
    		} catch (DocumentNotFoundException dnfe) {
302
    			// expect this
303
    			assertTrue(true);
304
    		}
305
    		assertNull(replicatedObject);
306 6770 leinfelder
307
    	} catch (Exception e) {
308
    		e.printStackTrace();
309
			fail(e.getMessage());
310
		}
311
    }
312 6620 leinfelder
313 6789 leinfelder
	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 6620 leinfelder
375
}