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