Revision 4445
Added by daigle about 16 years ago
test/edu/ucsb/nceas/metacattest/SchemaRegistryTest.java | ||
---|---|---|
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: daigle $' |
|
8 |
* '$Date: 2008-09-16 13:38:32 -0700 (Tue, 16 Sep 2008) $' |
|
9 |
* '$Revision: 4355 $' |
|
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.metacattest; |
|
27 |
|
|
28 |
import java.io.FileNotFoundException; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.StringReader; |
|
31 |
import java.sql.SQLException; |
|
32 |
import java.util.Calendar; |
|
33 |
import java.util.Date; |
|
34 |
import java.util.GregorianCalendar; |
|
35 |
import java.util.HashMap; |
|
36 |
import java.util.Hashtable; |
|
37 |
import java.util.SimpleTimeZone; |
|
38 |
import java.util.TimeZone; |
|
39 |
import java.util.Vector; |
|
40 |
|
|
41 |
import edu.ucsb.nceas.MCTestCase; |
|
42 |
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException; |
|
43 |
import edu.ucsb.nceas.metacat.client.Metacat; |
|
44 |
import edu.ucsb.nceas.metacat.client.MetacatAuthException; |
|
45 |
import edu.ucsb.nceas.metacat.client.MetacatException; |
|
46 |
import edu.ucsb.nceas.metacat.client.MetacatFactory; |
|
47 |
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException; |
|
48 |
import edu.ucsb.nceas.metacat.service.PropertyService; |
|
49 |
import edu.ucsb.nceas.utilities.FileUtil; |
|
50 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
|
51 |
import junit.framework.Test; |
|
52 |
import junit.framework.TestSuite; |
|
53 |
|
|
54 |
/** |
|
55 |
* A JUnit test for testing Access Control in Metacat |
|
56 |
*/ |
|
57 |
public class SchemaRegistryTest extends MCTestCase { |
|
58 |
|
|
59 |
private static String metacatUrl; |
|
60 |
private static String metacatDeployDir; |
|
61 |
private static String username; |
|
62 |
private static String password; |
|
63 |
static { |
|
64 |
try { |
|
65 |
metacatUrl = PropertyService.getProperty("test.metacatUrl"); |
|
66 |
metacatDeployDir = PropertyService.getProperty("test.metacatDeployDir"); |
|
67 |
username = PropertyService.getProperty("test.mcUser"); |
|
68 |
password = PropertyService.getProperty("test.mcPassword"); |
|
69 |
} catch (PropertyNotFoundException pnfe) { |
|
70 |
System.err.println("Could not get property in static block: " |
|
71 |
+ pnfe.getMessage()); |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
private static String testFileLocation = null; |
|
76 |
private static String serverSchemaLocation = null; |
|
77 |
private static String badSchemaLocationXMLFile = "bad-schema-location.xml"; |
|
78 |
private static String nonConformingXMLFile = "non-conforming-xml.xml"; |
|
79 |
private static String goodSchemaXMLFile = "good-schema.xml"; |
|
80 |
private static String goodSchemaFile1 = "chapter05ord.xsd"; |
|
81 |
private static String goodSchemaFile2 = "chapter05prod.xsd"; |
|
82 |
|
|
83 |
private static String getRegisteredTestSchemaSql = |
|
84 |
"SELECT * FROM xml_catalog " + |
|
85 |
"WHERE entry_type = 'Schema' " + |
|
86 |
"AND system_id LIKE '%chapter05%'"; |
|
87 |
|
|
88 |
private static String deleteRegisteredTestSchemaSql = |
|
89 |
"DELETE FROM xml_catalog " + |
|
90 |
"WHERE entry_type = 'Schema' " + |
|
91 |
"AND system_id LIKE '%chapter05%'"; |
|
92 |
|
|
93 |
private Metacat m; |
|
94 |
|
|
95 |
/** |
|
96 |
* Constructor to build the test |
|
97 |
* |
|
98 |
* @param name |
|
99 |
* the name of the test method |
|
100 |
*/ |
|
101 |
public SchemaRegistryTest(String name) { |
|
102 |
super(name); |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* Establish a testing framework by initializing appropriate objects |
|
107 |
*/ |
|
108 |
public void setUp() { |
|
109 |
try { |
|
110 |
debug("Test Metacat: " + metacatUrl); |
|
111 |
m = MetacatFactory.createMetacatConnection(metacatUrl); |
|
112 |
} catch (MetacatInaccessibleException mie) { |
|
113 |
System.err.println("Metacat is: " + metacatUrl); |
|
114 |
fail("Metacat connection failed." + mie.getMessage()); |
|
115 |
} |
|
116 |
|
|
117 |
testFileLocation = "test/clienttestfiles/"; |
|
118 |
serverSchemaLocation = metacatDeployDir + "/schema/"; |
|
119 |
|
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* Release any objects after tests are complete |
|
124 |
*/ |
|
125 |
public void tearDown() { |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* Create a suite of tests to be run together |
|
130 |
*/ |
|
131 |
public static Test suite() { |
|
132 |
TestSuite suite = new TestSuite(); |
|
133 |
suite.addTest(new SchemaRegistryTest("initialize")); |
|
134 |
// Test basic functions |
|
135 |
suite.addTest(new SchemaRegistryTest("newGoodSchemaRegisterTest")); |
|
136 |
suite.addTest(new SchemaRegistryTest("existingSchemaRegisterTest")); |
|
137 |
suite.addTest(new SchemaRegistryTest("newNonexistantSchemaLocationRegisterTest")); |
|
138 |
suite.addTest(new SchemaRegistryTest("newGoodSchemaBadXmlRegisterTest")); |
|
139 |
suite.addTest(new SchemaRegistryTest("existingGoodSchemaBadXmlRegisterTest")); |
|
140 |
suite.addTest(new SchemaRegistryTest("schemaInDbNotOnFileSystemTest")); |
|
141 |
|
|
142 |
return suite; |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* Run an initial test that always passes to check that the test harness is |
|
147 |
* working. |
|
148 |
*/ |
|
149 |
public void initialize() { |
|
150 |
assertTrue(1 == 1); |
|
151 |
} |
|
152 |
|
|
153 |
/** |
|
154 |
* Tests adding a document that has a new valid schema and the xml conforms to the |
|
155 |
* schema. The file we use has two schemas, so we expect to see two rows in the |
|
156 |
* xml_catalog table and two schema files added to the server. |
|
157 |
*/ |
|
158 |
public void newGoodSchemaRegisterTest() { |
|
159 |
try { |
|
160 |
debug("\nRunning: newGoodSchemaRegisterTest"); |
|
161 |
|
|
162 |
String newdocid = generateDocid(); |
|
163 |
|
|
164 |
debug("Refreshing the XMLSchemaService"); |
|
165 |
httpPost(metacatUrl + "?action=refreshServices", |
|
166 |
new HashMap<String, String>()); |
|
167 |
|
|
168 |
String testDocument = getTestDocument(testFileLocation + goodSchemaXMLFile); |
|
169 |
|
|
170 |
// login |
|
171 |
debug("logging in as: username=" + username + " password=" + password); |
|
172 |
m.login(username, password); |
|
173 |
|
|
174 |
// insert document. We expect to succeed. |
|
175 |
insertDocid(newdocid + ".1", testDocument, SUCCESS, false); |
|
176 |
|
|
177 |
debug("Checking db for registered schemas"); |
|
178 |
Vector<Hashtable<String,Object>> sqlResults = |
|
179 |
dbSelect(getRegisteredTestSchemaSql, |
|
180 |
"SchemaRegistryTest.newGoodSchemaRegisterTest"); |
|
181 |
if (sqlResults.size() != 2) { |
|
182 |
fail("Expected number of test schemas in the database is 2." + |
|
183 |
" The number found was: " + sqlResults.size()); |
|
184 |
} |
|
185 |
|
|
186 |
debug("Checking server for registered schema file: " + serverSchemaLocation + goodSchemaFile1); |
|
187 |
if( FileUtil.getFileStatus(serverSchemaLocation + goodSchemaFile1) < |
|
188 |
FileUtil.EXISTS_READABLE) { |
|
189 |
fail("Could not find expected schema file on server: " + |
|
190 |
serverSchemaLocation + goodSchemaFile1); |
|
191 |
} |
|
192 |
|
|
193 |
debug("Checking server for registered schema file: " + serverSchemaLocation + goodSchemaFile2); |
|
194 |
if( FileUtil.getFileStatus(serverSchemaLocation + goodSchemaFile2) < |
|
195 |
FileUtil.EXISTS_READABLE) { |
|
196 |
fail("Could not find expected schema file on server: " + |
|
197 |
serverSchemaLocation + goodSchemaFile2); |
|
198 |
} |
|
199 |
|
|
200 |
// Clean the system up |
|
201 |
debug("Deleting test schemas from db"); |
|
202 |
dbUpdate(deleteRegisteredTestSchemaSql, |
|
203 |
"SchemaRegistryTest.newGoodSchemaRegisterTest"); |
|
204 |
|
|
205 |
debug("Deleting test schema files from server file system"); |
|
206 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile1); |
|
207 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile2); |
|
208 |
|
|
209 |
debug("Refreshing the XMLSchemaService"); |
|
210 |
httpPost(metacatUrl + "?action=refreshServices", |
|
211 |
new HashMap<String, String>()); |
|
212 |
|
|
213 |
deleteDocid(newdocid + ".1", SUCCESS, false); |
|
214 |
|
|
215 |
m.logout(); |
|
216 |
|
|
217 |
} catch (MetacatAuthException mae) { |
|
218 |
fail("Authorization failed: " + mae.getMessage()); |
|
219 |
} catch (MetacatInaccessibleException mie) { |
|
220 |
fail("Metacat Inaccessible: " + mie.getMessage()); |
|
221 |
} catch (IOException ioe) { |
|
222 |
fail("I/O Exception: " + ioe.getMessage()); |
|
223 |
} catch (SQLException sqle) { |
|
224 |
fail("SQL Exception: " + sqle.getMessage()); |
|
225 |
} catch (Exception e) { |
|
226 |
fail("General exception: " + e.getMessage()); |
|
227 |
} |
|
228 |
} |
|
229 |
|
|
230 |
/** |
|
231 |
* Tests adding a second document that has an existing valid schema and the xml |
|
232 |
* conforms to the schema. Primarily, we want to make sure that the original |
|
233 |
* two schema entries in the database are the only two and that duplicates did |
|
234 |
* not get entered. |
|
235 |
*/ |
|
236 |
public void existingSchemaRegisterTest() { |
|
237 |
try { |
|
238 |
debug("\nRunning: existingSchemaRegisterTest"); |
|
239 |
|
|
240 |
String newdocid1 = generateDocid(); |
|
241 |
|
|
242 |
debug("Refreshing the XMLSchemaService"); |
|
243 |
httpPost(metacatUrl + "?action=refreshServices", |
|
244 |
new HashMap<String, String>()); |
|
245 |
|
|
246 |
String testDocument = getTestDocument(testFileLocation + goodSchemaXMLFile); |
|
247 |
|
|
248 |
// login |
|
249 |
debug("logging in as: username=" + username + " password=" + password); |
|
250 |
m.login(username, password); |
|
251 |
|
|
252 |
// insert document. We expect to succeed. |
|
253 |
insertDocid(newdocid1 + ".1", testDocument, SUCCESS, false); |
|
254 |
|
|
255 |
// create a second doc id and insert another document |
|
256 |
String newdocid2 = generateDocid(); |
|
257 |
insertDocid(newdocid2 + ".1", testDocument, SUCCESS, false); |
|
258 |
|
|
259 |
// Check the db for registered schemas. We should find two and only |
|
260 |
// two. |
|
261 |
debug("Checking db for registered schemas"); |
|
262 |
Vector<Hashtable<String,Object>> sqlResults = |
|
263 |
dbSelect(getRegisteredTestSchemaSql, |
|
264 |
"SchemaRegistryTest.existingSchemaRegisterTest"); |
|
265 |
if (sqlResults.size() != 2) { |
|
266 |
fail("Expected number of test schemas in the database is 2." + |
|
267 |
" The number found was: " + sqlResults.size()); |
|
268 |
} |
|
269 |
|
|
270 |
// Clean the system up |
|
271 |
debug("Deleting test schemas from db"); |
|
272 |
dbUpdate(deleteRegisteredTestSchemaSql, |
|
273 |
"SchemaRegistryTest.existingSchemaRegisterTest"); |
|
274 |
|
|
275 |
debug("Deleting test schema files from server file system"); |
|
276 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile1); |
|
277 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile2); |
|
278 |
|
|
279 |
debug("Refreshing the XMLSchemaService"); |
|
280 |
httpPost(metacatUrl + "?action=refreshServices", |
|
281 |
new HashMap<String, String>()); |
|
282 |
|
|
283 |
deleteDocid(newdocid1 + ".1", SUCCESS, false); |
|
284 |
deleteDocid(newdocid2 + ".1", SUCCESS, false); |
|
285 |
|
|
286 |
m.logout(); |
|
287 |
|
|
288 |
} catch (MetacatAuthException mae) { |
|
289 |
fail("Authorization failed: " + mae.getMessage()); |
|
290 |
} catch (MetacatInaccessibleException mie) { |
|
291 |
fail("Metacat Inaccessible: " + mie.getMessage()); |
|
292 |
} catch (IOException ioe) { |
|
293 |
fail("I/O Exception: " + ioe.getMessage()); |
|
294 |
} catch (SQLException sqle) { |
|
295 |
fail("SQL Exception: " + sqle.getMessage()); |
|
296 |
} catch (Exception e) { |
|
297 |
fail("General exception: " + e.getMessage()); |
|
298 |
} |
|
299 |
} |
|
300 |
|
|
301 |
/** |
|
302 |
* Tests adding a document that has an invalid schema location. The insert |
|
303 |
* should fail and the schema should not get registered. |
|
304 |
*/ |
|
305 |
public void newNonexistantSchemaLocationRegisterTest() { |
|
306 |
try { |
|
307 |
debug("\nRunning: newNonexistantSchemaLocationRegisterTest"); |
|
308 |
|
|
309 |
String newdocid1 = generateDocid(); |
|
310 |
|
|
311 |
debug("Refreshing the XMLSchemaService"); |
|
312 |
httpPost(metacatUrl + "?action=refreshServices", |
|
313 |
new HashMap<String, String>()); |
|
314 |
|
|
315 |
String testDocument = getTestDocument(testFileLocation + badSchemaLocationXMLFile); |
|
316 |
|
|
317 |
// login |
|
318 |
debug("logging in as: username=" + username + " password=" + password); |
|
319 |
m.login(username, password); |
|
320 |
|
|
321 |
// insert document. We expect to fail with a MetacatException because the schemaLocation |
|
322 |
// does not exist. |
|
323 |
insertDocidExpectException(newdocid1 + ".1", testDocument, "Failed to read schema document"); |
|
324 |
|
|
325 |
// Check the db for registered schemas. We should find none. |
|
326 |
debug("Checking db for registered schemas"); |
|
327 |
Vector<Hashtable<String,Object>> sqlResults = |
|
328 |
dbSelect(getRegisteredTestSchemaSql, |
|
329 |
"SchemaRegistryTest.newNonexistantSchemaLocationRegisterTest"); |
|
330 |
if (sqlResults.size() != 0) { |
|
331 |
fail("Expected number of test schemas in the database is 0." + |
|
332 |
" The number found was: " + sqlResults.size()); |
|
333 |
} |
|
334 |
|
|
335 |
|
|
336 |
// check the system for the schema files. There should be none. |
|
337 |
debug("Checking server for registered schema file: " + serverSchemaLocation + goodSchemaFile1); |
|
338 |
if( FileUtil.getFileStatus(serverSchemaLocation + goodSchemaFile1) >= |
|
339 |
FileUtil.EXISTS_ONLY) { |
|
340 |
fail("Found unexpected schema file on server: " + |
|
341 |
serverSchemaLocation + goodSchemaFile1); |
|
342 |
} |
|
343 |
|
|
344 |
debug("Checking server for registered schema file: " + serverSchemaLocation + goodSchemaFile2); |
|
345 |
if( FileUtil.getFileStatus(serverSchemaLocation + goodSchemaFile2) >= |
|
346 |
FileUtil.EXISTS_ONLY) { |
|
347 |
fail("Found unexpected schema file on server: " + |
|
348 |
serverSchemaLocation + goodSchemaFile2); |
|
349 |
} |
|
350 |
|
|
351 |
// Clean the system up, just to be safe. |
|
352 |
debug("Deleting test schemas from db"); |
|
353 |
dbUpdate(deleteRegisteredTestSchemaSql, |
|
354 |
"SchemaRegistryTest.newNonexistantSchemaLocationRegisterTest"); |
|
355 |
|
|
356 |
debug("Deleting test schema files from server file system"); |
|
357 |
try { |
|
358 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile1); |
|
359 |
} catch (FileNotFoundException fnfe) { |
|
360 |
// Do nothing here. We are hoping that the file didn't exist. |
|
361 |
} |
|
362 |
|
|
363 |
try { |
|
364 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile2); |
|
365 |
} catch (FileNotFoundException fnfe) { |
|
366 |
// Do nothing here. We are hoping that the file didn't exist. |
|
367 |
} |
|
368 |
|
|
369 |
debug("Refreshing the XMLSchemaService"); |
|
370 |
httpPost(metacatUrl + "?action=refreshServices", |
|
371 |
new HashMap<String, String>()); |
|
372 |
|
|
373 |
m.logout(); |
|
374 |
|
|
375 |
} catch (MetacatAuthException mae) { |
|
376 |
fail("Authorization failed: " + mae.getMessage()); |
|
377 |
} catch (MetacatInaccessibleException mie) { |
|
378 |
fail("Metacat Inaccessible: " + mie.getMessage()); |
|
379 |
} catch (IOException ioe) { |
|
380 |
fail("I/O Exception: " + ioe.getMessage()); |
|
381 |
} catch (SQLException sqle) { |
|
382 |
fail("SQL Exception: " + sqle.getMessage()); |
|
383 |
} catch (Exception e) { |
|
384 |
fail("General exception: " + e.getMessage()); |
|
385 |
} |
|
386 |
} |
|
387 |
|
|
388 |
/** |
|
389 |
* Tests adding a document that has a valid schema location which has |
|
390 |
* already been registered in metacat, but the xml doesn't conform to the |
|
391 |
* schema. The insert should fail. |
|
392 |
*/ |
|
393 |
public void newGoodSchemaBadXmlRegisterTest() { |
|
394 |
try { |
|
395 |
debug("\nRunning: newGoodSchemaBadXmlRegisterTest"); |
|
396 |
|
|
397 |
String newdocid1 = generateDocid(); |
|
398 |
|
|
399 |
debug("Refreshing the XMLSchemaService"); |
|
400 |
httpPost(metacatUrl + "?action=refreshServices", |
|
401 |
new HashMap<String, String>()); |
|
402 |
|
|
403 |
String testDocument = getTestDocument(testFileLocation + nonConformingXMLFile); |
|
404 |
|
|
405 |
// login |
|
406 |
debug("logging in as: username=" + username + " password=" + password); |
|
407 |
m.login(username, password); |
|
408 |
|
|
409 |
// insert document. We expect to fail with a MetacatException because the schemaLocation |
|
410 |
// does not exist. |
|
411 |
insertDocidExpectException(newdocid1 + ".1", testDocument, "is not allowed to appear in element"); |
|
412 |
|
|
413 |
// Check the db for registered schemas. We should find none. |
|
414 |
debug("Checking db for registered schemas"); |
|
415 |
Vector<Hashtable<String,Object>> sqlResults = |
|
416 |
dbSelect(getRegisteredTestSchemaSql, |
|
417 |
"SchemaRegistryTest.newGoodSchemaBadXmlRegisterTest"); |
|
418 |
if (sqlResults.size() != 0) { |
|
419 |
fail("Expected number of test schemas in the database is 0." + |
|
420 |
" The number found was: " + sqlResults.size()); |
|
421 |
} |
|
422 |
|
|
423 |
// check the system for the schema files. There should be none. |
|
424 |
debug("Checking server for registered schema file: " + serverSchemaLocation + goodSchemaFile1); |
|
425 |
if( FileUtil.getFileStatus(serverSchemaLocation + goodSchemaFile1) >= |
|
426 |
FileUtil.EXISTS_ONLY) { |
|
427 |
fail("Found unexpected schema file on server: " + |
|
428 |
serverSchemaLocation + goodSchemaFile1); |
|
429 |
} |
|
430 |
|
|
431 |
debug("Checking server for registered schema file: " + serverSchemaLocation + goodSchemaFile2); |
|
432 |
if( FileUtil.getFileStatus(serverSchemaLocation + goodSchemaFile2) >= |
|
433 |
FileUtil.EXISTS_ONLY) { |
|
434 |
fail("Found unexpected schema file on server: " + |
|
435 |
serverSchemaLocation + goodSchemaFile2); |
|
436 |
} |
|
437 |
|
|
438 |
// Clean the system up, just to be safe. |
|
439 |
debug("Deleting test schemas from db"); |
|
440 |
dbUpdate(deleteRegisteredTestSchemaSql, |
|
441 |
"SchemaRegistryTest.newNonexistantSchemaLocationRegisterTest"); |
|
442 |
|
|
443 |
debug("Deleting test schema files from server file system"); |
|
444 |
try { |
|
445 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile1); |
|
446 |
} catch (FileNotFoundException fnfe) { |
|
447 |
// Do nothing here. We are hoping that the file didn't exist. |
|
448 |
} |
|
449 |
|
|
450 |
try { |
|
451 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile2); |
|
452 |
} catch (FileNotFoundException fnfe) { |
|
453 |
// Do nothing here. We are hoping that the file didn't exist. |
|
454 |
} |
|
455 |
|
|
456 |
debug("Refreshing the XMLSchemaService"); |
|
457 |
httpPost(metacatUrl + "?action=refreshServices", |
|
458 |
new HashMap<String, String>()); |
|
459 |
|
|
460 |
m.logout(); |
|
461 |
|
|
462 |
} catch (MetacatAuthException mae) { |
|
463 |
fail("Authorization failed: " + mae.getMessage()); |
|
464 |
} catch (MetacatInaccessibleException mie) { |
|
465 |
fail("Metacat Inaccessible: " + mie.getMessage()); |
|
466 |
} catch (IOException ioe) { |
|
467 |
fail("I/O Exception: " + ioe.getMessage()); |
|
468 |
} catch (SQLException sqle) { |
|
469 |
fail("SQL Exception: " + sqle.getMessage()); |
|
470 |
} catch (Exception e) { |
|
471 |
fail("General exception: " + e.getMessage()); |
|
472 |
} |
|
473 |
} |
|
474 |
|
|
475 |
/** |
|
476 |
* Tests adding a document that has a valid schema location, but the xml |
|
477 |
* doesn't conform to the schema. The insert should fail and the schema |
|
478 |
* should not get registered. |
|
479 |
*/ |
|
480 |
public void existingGoodSchemaBadXmlRegisterTest() { |
|
481 |
try { |
|
482 |
debug("\nRunning: existingGoodSchemaBadXmlRegisterTest"); |
|
483 |
|
|
484 |
debug("Refreshing the XMLSchemaService"); |
|
485 |
httpPost(metacatUrl + "?action=refreshServices", |
|
486 |
new HashMap<String, String>()); |
|
487 |
|
|
488 |
// login |
|
489 |
debug("logging in as: username=" + username + " password=" + password); |
|
490 |
m.login(username, password); |
|
491 |
|
|
492 |
// insert good document. We expect to succeed. |
|
493 |
String newdocid1 = generateDocid(); |
|
494 |
String testDocument1 = getTestDocument(testFileLocation + goodSchemaXMLFile); |
|
495 |
insertDocid(newdocid1 + ".1", testDocument1, SUCCESS, false); |
|
496 |
|
|
497 |
String newdocid2 = generateDocid(); |
|
498 |
String testDocument2 = getTestDocument(testFileLocation + nonConformingXMLFile); |
|
499 |
// attempt to insert non-conforming document. We expect to fail with a MetacatException |
|
500 |
// because the xml does not conform to the schema |
|
501 |
insertDocidExpectException(newdocid2 + ".1", testDocument2, "is not allowed to appear in element"); |
|
502 |
|
|
503 |
// Check the db for registered schemas. We should find none. |
|
504 |
debug("Checking db for registered schemas"); |
|
505 |
Vector<Hashtable<String,Object>> sqlResults = |
|
506 |
dbSelect(getRegisteredTestSchemaSql, |
|
507 |
"SchemaRegistryTest.existingGoodSchemaBadXmlRegisterTest"); |
|
508 |
if (sqlResults.size() != 2) { |
|
509 |
fail("Expected number of test schemas in the database is 2." + |
|
510 |
" The number found was: " + sqlResults.size()); |
|
511 |
} |
|
512 |
|
|
513 |
// Clean the system up. |
|
514 |
debug("Deleting test schemas from db"); |
|
515 |
dbUpdate(deleteRegisteredTestSchemaSql, |
|
516 |
"SchemaRegistryTest.newNonexistantSchemaLocationRegisterTest"); |
|
517 |
|
|
518 |
debug("Deleting test schema files from server file system"); |
|
519 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile1); |
|
520 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile2); |
|
521 |
|
|
522 |
debug("Refreshing the XMLSchemaService"); |
|
523 |
httpPost(metacatUrl + "?action=refreshServices", |
|
524 |
new HashMap<String, String>()); |
|
525 |
|
|
526 |
deleteDocid(newdocid1 + ".1", SUCCESS, false); |
|
527 |
|
|
528 |
m.logout(); |
|
529 |
|
|
530 |
} catch (MetacatAuthException mae) { |
|
531 |
fail("Authorization failed: " + mae.getMessage()); |
|
532 |
} catch (MetacatInaccessibleException mie) { |
|
533 |
fail("Metacat Inaccessible: " + mie.getMessage()); |
|
534 |
} catch (IOException ioe) { |
|
535 |
fail("I/O Exception: " + ioe.getMessage()); |
|
536 |
} catch (SQLException sqle) { |
|
537 |
fail("SQL Exception: " + sqle.getMessage()); |
|
538 |
} catch (Exception e) { |
|
539 |
fail("General exception: " + e.getMessage()); |
|
540 |
} |
|
541 |
} |
|
542 |
|
|
543 |
/** |
|
544 |
* Tests adding a second document that has an existing valid schema and the |
|
545 |
* xml conforms to the schema. Primarily, we want to make sure that the |
|
546 |
* original two schema entries in the database are the only two and that |
|
547 |
* duplicates did not get entered. |
|
548 |
*/ |
|
549 |
public void schemaInDbNotOnFileSystemTest() { |
|
550 |
try { |
|
551 |
debug("\nRunning: schemaInDbNotOnFileSystemTest"); |
|
552 |
|
|
553 |
debug("Refreshing the XMLSchemaService"); |
|
554 |
httpPost(metacatUrl + "?action=refreshServices", |
|
555 |
new HashMap<String, String>()); |
|
556 |
|
|
557 |
// login |
|
558 |
debug("logging in as: username=" + username + " password=" + password); |
|
559 |
m.login(username, password); |
|
560 |
|
|
561 |
// insert document. We expect to succeed. |
|
562 |
String newdocid1 = generateDocid(); |
|
563 |
String testDocument = getTestDocument(testFileLocation + goodSchemaXMLFile); |
|
564 |
insertDocid(newdocid1 + ".1", testDocument, SUCCESS, false); |
|
565 |
|
|
566 |
debug("Deleting test schema files from server file system"); |
|
567 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile1); |
|
568 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile2); |
|
569 |
|
|
570 |
debug("Refreshing the XMLSchemaService"); |
|
571 |
httpPost(metacatUrl + "?action=refreshServices", |
|
572 |
new HashMap<String, String>()); |
|
573 |
|
|
574 |
// create a second doc id and insert another document |
|
575 |
String newdocid2 = generateDocid(); |
|
576 |
insertDocid(newdocid2 + ".1", testDocument, SUCCESS, false); |
|
577 |
|
|
578 |
// Check the db for registered schemas. We should find two and only |
|
579 |
// two. |
|
580 |
debug("Checking db for registered schemas"); |
|
581 |
Vector<Hashtable<String,Object>> sqlResults = |
|
582 |
dbSelect(getRegisteredTestSchemaSql, |
|
583 |
"SchemaRegistryTest.schemaInDbNotOnFileSystemTest"); |
|
584 |
if (sqlResults.size() != 2) { |
|
585 |
fail("Expected number of test schemas in the database is 2." + |
|
586 |
" The number found was: " + sqlResults.size()); |
|
587 |
} |
|
588 |
|
|
589 |
debug("Checking server for registered schema file: " + serverSchemaLocation + goodSchemaFile1); |
|
590 |
if( FileUtil.getFileStatus(serverSchemaLocation + goodSchemaFile1) < |
|
591 |
FileUtil.EXISTS_READABLE) { |
|
592 |
fail("Could not find expected schema file on server: " + |
|
593 |
serverSchemaLocation + goodSchemaFile1); |
|
594 |
} |
|
595 |
|
|
596 |
debug("Checking server for registered schema file: " + serverSchemaLocation + goodSchemaFile2); |
|
597 |
if( FileUtil.getFileStatus(serverSchemaLocation + goodSchemaFile2) < |
|
598 |
FileUtil.EXISTS_READABLE) { |
|
599 |
fail("Could not find expected schema file on server: " + |
|
600 |
serverSchemaLocation + goodSchemaFile2); |
|
601 |
} |
|
602 |
|
|
603 |
// Clean the system up |
|
604 |
debug("Deleting test schemas from db"); |
|
605 |
dbUpdate(deleteRegisteredTestSchemaSql, |
|
606 |
"SchemaRegistryTest.schemaInDbNotOnFileSystemTest"); |
|
607 |
|
|
608 |
debug("Deleting test schema files from server file system"); |
|
609 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile1); |
|
610 |
FileUtil.deleteFile(serverSchemaLocation + goodSchemaFile2); |
|
611 |
|
|
612 |
debug("Refreshing the XMLSchemaService"); |
|
613 |
httpPost(metacatUrl + "?action=refreshServices", |
|
614 |
new HashMap<String, String>()); |
|
615 |
|
|
616 |
deleteDocid(newdocid1 + ".1", SUCCESS, false); |
|
617 |
deleteDocid(newdocid2 + ".1", SUCCESS, false); |
|
618 |
|
|
619 |
m.logout(); |
|
620 |
|
|
621 |
} catch (MetacatAuthException mae) { |
|
622 |
fail("Authorization failed: " + mae.getMessage()); |
|
623 |
} catch (MetacatInaccessibleException mie) { |
|
624 |
fail("Metacat Inaccessible: " + mie.getMessage()); |
|
625 |
} catch (IOException ioe) { |
|
626 |
fail("I/O Exception: " + ioe.getMessage()); |
|
627 |
} catch (SQLException sqle) { |
|
628 |
fail("SQL Exception: " + sqle.getMessage()); |
|
629 |
} catch (Exception e) { |
|
630 |
fail("General exception: " + e.getMessage()); |
|
631 |
} |
|
632 |
} |
|
633 |
|
|
634 |
/** |
|
635 |
* @param documentLocation |
|
636 |
* the path of the document to read. |
|
637 |
* @return a string holding the contents of the document with the contextUrl |
|
638 |
* token replaced by the test.contextUrl property |
|
639 |
*/ |
|
640 |
private String getTestDocument(String documentLocation) throws IOException, |
|
641 |
PropertyNotFoundException { |
|
642 |
String testDocument = FileUtil.readFileToString(documentLocation); |
|
643 |
|
|
644 |
String contextUrl = PropertyService.getProperty("test.contextUrl"); |
|
645 |
testDocument = testDocument.replaceAll("@contextUrl@", contextUrl); |
|
646 |
|
|
647 |
return testDocument; |
|
648 |
} |
|
649 |
|
|
650 |
/** |
|
651 |
* Insert a document into metacat via the metacat client. |
|
652 |
* |
|
653 |
* @param docid |
|
654 |
* the id of the document to insert |
|
655 |
* @param testdocument |
|
656 |
* the contents of the document |
|
657 |
* @param result |
|
658 |
* do we expect this to succed or fail |
|
659 |
* @param expectKarmaException |
|
660 |
* do we expect it to fail with a KarmaException |
|
661 |
* @return the response that was passed back from metacat |
|
662 |
*/ |
|
663 |
private String insertDocid(String docid, String testdocument, boolean result, |
|
664 |
boolean expectKarmaException) { |
|
665 |
debug("insertDocid(): docid=" + docid + " expectedResult=" + result |
|
666 |
+ " expectKarmaException=" + expectKarmaException); |
|
667 |
String response = null; |
|
668 |
try { |
|
669 |
response = m.insert(docid, new StringReader(testdocument), null); |
|
670 |
debug("expected result: " + result); |
|
671 |
if (result) { |
|
672 |
assertTrue(response, (response.indexOf("<success>") != -1)); |
|
673 |
assertTrue(response, response.indexOf(docid) != -1); |
|
674 |
} else { |
|
675 |
debug("in else, checking: " + (response.indexOf("<success>") == -1)); |
|
676 |
assertTrue(response, (response.indexOf("<success>") == -1)); |
|
677 |
} |
|
678 |
debug("insertDocid(): response=" + response); |
|
679 |
} catch (MetacatInaccessibleException mie) { |
|
680 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
681 |
} catch (InsufficientKarmaException ike) { |
|
682 |
if (!expectKarmaException) { |
|
683 |
fail("Insufficient karma:\n" + ike.getMessage()); |
|
684 |
} |
|
685 |
} catch (MetacatException me) { |
|
686 |
fail("Metacat Error:\n" + me.getMessage()); |
|
687 |
} catch (Exception e) { |
|
688 |
fail("General exception:\n" + e.getMessage()); |
|
689 |
} |
|
690 |
return response; |
|
691 |
} |
|
692 |
|
|
693 |
/** |
|
694 |
* Try to insert a document into metacat. This is used when we expect a |
|
695 |
* MetacatException |
|
696 |
* |
|
697 |
* @param docid |
|
698 |
* the document id we are going to attempt to insert |
|
699 |
* @param testdocument |
|
700 |
* the contents of the document we are going to attempt to insert |
|
701 |
* @param expectedException |
|
702 |
* text that should be a substring of the MetacatException |
|
703 |
* message |
|
704 |
*/ |
|
705 |
private void insertDocidExpectException(String docid, String testdocument, String expectedException) { |
|
706 |
debug("insertDocidExpectException(): docid=" + docid + " expectedException=" + expectedException); |
|
707 |
|
|
708 |
String response = null; |
|
709 |
try { |
|
710 |
response = m.insert(docid, new StringReader(testdocument), null); |
|
711 |
fail("Expecting a MetacatException. Instead, got response: " + response); |
|
712 |
} catch (MetacatInaccessibleException mie) { |
|
713 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
714 |
} catch (InsufficientKarmaException ike) { |
|
715 |
fail("Insufficient karma:\n" + ike.getMessage()); |
|
716 |
} catch (MetacatException me) { |
|
717 |
assertTrue(me.getMessage(), me.getMessage().contains(expectedException)); |
|
718 |
} catch (Exception e) { |
|
719 |
fail("General exception:\n" + e.getMessage()); |
|
720 |
} |
|
721 |
|
|
722 |
|
|
723 |
} |
|
724 |
|
|
725 |
/** |
|
726 |
* Delete a document into metacat. The expected result is passed as result |
|
727 |
*/ |
|
728 |
private void deleteDocid(String docid, boolean result, boolean expectedKarmaFailure) { |
|
729 |
debug("deleteDocid(): docid=" + docid + " expectedResult=" + result |
|
730 |
+ " expectedKarmaFailure=" + expectedKarmaFailure); |
|
731 |
try { |
|
732 |
Thread.sleep(5000); |
|
733 |
String response = m.delete(docid); |
|
734 |
if (result) { |
|
735 |
assertTrue(response, response.indexOf("<success>") != -1); |
|
736 |
} else { |
|
737 |
assertTrue(response, response.indexOf("<success>") == -1); |
|
738 |
} |
|
739 |
debug("deleteDocid(): response=" + response); |
|
740 |
} catch (MetacatInaccessibleException mie) { |
|
741 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
742 |
} catch (InsufficientKarmaException ike) { |
|
743 |
if (!expectedKarmaFailure) { |
|
744 |
fail("Insufficient karma:\n" + ike.getMessage()); |
|
745 |
} |
|
746 |
} catch (MetacatException me) { |
|
747 |
if (result) { |
|
748 |
fail("Metacat Error:\n" + me.getMessage()); |
|
749 |
} else { |
|
750 |
debug("Metacat Error:\n" + me.getMessage()); |
|
751 |
} |
|
752 |
} catch (Exception e) { |
|
753 |
fail("General exception:\n" + e.getMessage()); |
|
754 |
} |
|
755 |
} |
|
756 |
|
|
757 |
/** |
|
758 |
* Create a hopefully unique docid for testing insert and update. Does not |
|
759 |
* include the 'revision' part of the id. |
|
760 |
* |
|
761 |
* @return a String docid based on the current date and time |
|
762 |
*/ |
|
763 |
private String generateDocid() { |
|
764 |
try { |
|
765 |
Thread.sleep(1100); |
|
766 |
} catch (Exception e) { |
|
767 |
debug("Could not sleep: " + e.getMessage()); |
|
768 |
} |
|
769 |
StringBuffer docid = new StringBuffer("test"); |
|
770 |
docid.append("."); |
|
771 |
|
|
772 |
// Create a calendar to get the date formatted properly |
|
773 |
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); |
|
774 |
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]); |
|
775 |
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); |
|
776 |
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); |
|
777 |
Calendar calendar = new GregorianCalendar(pdt); |
|
778 |
Date trialTime = new Date(); |
|
779 |
calendar.setTime(trialTime); |
|
780 |
docid.append(calendar.get(Calendar.YEAR)); |
|
781 |
docid.append(calendar.get(Calendar.DAY_OF_YEAR)); |
|
782 |
docid.append(calendar.get(Calendar.HOUR_OF_DAY)); |
|
783 |
docid.append(calendar.get(Calendar.MINUTE)); |
|
784 |
docid.append(calendar.get(Calendar.SECOND)); |
|
785 |
|
|
786 |
return docid.toString(); |
|
787 |
} |
|
788 |
|
|
789 |
} |
|
0 | 790 |
Also available in: Unified diff
New test case to test xml schema registration.