Revision 1082
Added by Jing Tao over 22 years ago
junittests/edu/ucsb/nceas/metacatjunittests/MetaCatServletTest.java | ||
---|---|---|
44 | 44 |
*/ |
45 | 45 |
public class MetaCatServletTest extends TestCase |
46 | 46 |
{ |
47 |
private String metacatURL= |
|
48 |
"http://dev.nceas.ucsb.edu/tao/servlet/metacat";
|
|
47 |
private String metacatURL=MetaCatUtil.getOption("junittesturl");
|
|
48 |
private String serialNumber;
|
|
49 | 49 |
/** |
50 | 50 |
* Constructor to build the test |
51 | 51 |
* |
... | ... | |
54 | 54 |
public MetaCatServletTest(String name) |
55 | 55 |
{ |
56 | 56 |
super(name); |
57 |
System.out.println("metacatURL: "+metacatURL); |
|
57 | 58 |
} |
58 | 59 |
|
60 |
/** |
|
61 |
* Constructor to build the test |
|
62 |
* |
|
63 |
* @param name the name of the test method |
|
64 |
*/ |
|
65 |
public MetaCatServletTest(String name, String serial) |
|
66 |
{ |
|
67 |
super(name); |
|
68 |
serialNumber=serial; |
|
69 |
} |
|
59 | 70 |
/** |
60 | 71 |
* Establish a testing framework by initializing appropriate objects |
61 | 72 |
*/ |
62 | 73 |
public void setUp() |
63 |
{
|
|
74 |
{ |
|
64 | 75 |
|
65 |
}
|
|
76 |
} |
|
66 | 77 |
|
67 | 78 |
/** |
68 | 79 |
* Release any objects after tests are complete |
... | ... | |
76 | 87 |
*/ |
77 | 88 |
public static Test suite() |
78 | 89 |
{ |
90 |
|
|
91 |
|
|
92 |
|
|
79 | 93 |
TestSuite suite = new TestSuite(); |
80 | 94 |
suite.addTest(new MetaCatServletTest("initialize")); |
81 |
suite.addTest(new MetaCatServletTest("testNCEASLogin")); |
|
82 |
suite.addTest(new MetaCatServletTest("testNCEASLoginFail")); |
|
83 | 95 |
suite.addTest(new MetaCatServletTest("testLterReferralLogin")); |
84 | 96 |
suite.addTest(new MetaCatServletTest("testLterReferralLoginFail")); |
85 | 97 |
suite.addTest(new MetaCatServletTest("testPiscoReferralLogin")); |
86 | 98 |
suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail")); |
99 |
suite.addTest(new MetaCatServletTest("testNCEASLoginFail")); |
|
100 |
//Should put a login successfully at the end of login test |
|
101 |
//So insert or update can have cookie. |
|
102 |
suite.addTest(new MetaCatServletTest("testNCEASLogin")); |
|
103 |
|
|
104 |
//create random number for docid, so it can void repeat |
|
105 |
double number = Math.random()*10000; |
|
106 |
String serial = Integer.toString(((new Double(number)).intValue())); |
|
107 |
MetaCatUtil.debugMessage("serial: "+serial, 1); |
|
108 |
suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial)); |
|
109 |
suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", |
|
110 |
serial)); |
|
111 |
|
|
112 |
suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", |
|
113 |
serial)); |
|
114 |
suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", |
|
115 |
serial)); |
|
116 |
suite.addTest(new MetaCatServletTest("testUpdateXMLDocument",serial)); |
|
117 |
suite.addTest(new MetaCatServletTest("testDeleteXMLDocument",serial)); |
|
118 |
|
|
119 |
suite.addTest(new MetaCatServletTest("testLogOut")); |
|
120 |
|
|
121 |
|
|
122 |
|
|
87 | 123 |
return suite; |
88 | 124 |
} |
89 | 125 |
|
... | ... | |
99 | 135 |
} |
100 | 136 |
|
101 | 137 |
/** |
102 |
* Test if the getProtocol() function works given a url
|
|
138 |
* Test the login to neceas succesfully
|
|
103 | 139 |
*/ |
104 | 140 |
public void testNCEASLogin() |
105 | 141 |
{ |
... | ... | |
109 | 145 |
//assert( withProtocol.getProtocol().equals("http")); |
110 | 146 |
} |
111 | 147 |
|
148 |
/** |
|
149 |
* Test the login to neceas failed |
|
150 |
*/ |
|
112 | 151 |
public void testNCEASLoginFail() |
113 | 152 |
{ |
114 | 153 |
String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org"; |
115 | 154 |
String passwd="12345678"; |
116 | 155 |
assert(!logIn(user,passwd)); |
117 |
//assert( withProtocol.getProtocol().equals("http")); |
|
156 |
|
|
118 | 157 |
} |
119 |
public void testLterReferralLogin() |
|
158 |
|
|
159 |
/** |
|
160 |
* Test the login to lter succesfully |
|
161 |
*/ |
|
162 |
public void testLterReferralLogin() |
|
120 | 163 |
{ |
121 | 164 |
String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org"; |
122 | 165 |
String passwd="qVyGpVeb"; |
123 | 166 |
assert(logIn(user,passwd)); |
124 |
//assert( withProtocol.getProtocol().equals("http")); |
|
167 |
|
|
125 | 168 |
} |
126 | 169 |
|
170 |
/** |
|
171 |
* Test the login to lter failed |
|
172 |
*/ |
|
127 | 173 |
public void testLterReferralLoginFail() |
128 | 174 |
{ |
129 | 175 |
String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org"; |
... | ... | |
132 | 178 |
//assert( withProtocol.getProtocol().equals("http")); |
133 | 179 |
} |
134 | 180 |
|
181 |
/** |
|
182 |
* Test the login to pisco succesfully |
|
183 |
*/ |
|
135 | 184 |
public void testPiscoReferralLogin() |
136 | 185 |
{ |
137 | 186 |
String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org"; |
... | ... | |
140 | 189 |
//assert( withProtocol.getProtocol().equals("http")); |
141 | 190 |
} |
142 | 191 |
|
192 |
/** |
|
193 |
* Test the login to pisco failed |
|
194 |
*/ |
|
143 | 195 |
public void testPiscoReferralLoginFail() |
144 | 196 |
{ |
145 | 197 |
String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org"; |
... | ... | |
148 | 200 |
//assert( withProtocol.getProtocol().equals("http")); |
149 | 201 |
} |
150 | 202 |
|
203 |
/** |
|
204 |
* Test insert a xml document successfully |
|
205 |
*/ |
|
206 |
public void testInsertXMLDocument() |
|
207 |
{ |
|
208 |
String name = "john"+MetaCatUtil.getOption("accNumSeparator") |
|
209 |
+serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1"; |
|
210 |
MetaCatUtil.debugMessage("docid: "+name, 20); |
|
211 |
String content="<?xml version=\"1.0\"?>" |
|
212 |
+"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//" |
|
213 |
+"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb." |
|
214 |
+"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">" |
|
215 |
+"<acl authSystem=\"knb\" order=\"allowFirst\">" |
|
216 |
+"<identifier>"+name+"</identifier>" |
|
217 |
+"<allow>" |
|
218 |
+"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>" |
|
219 |
+"<permission>all</permission>" |
|
220 |
+"</allow>" |
|
221 |
+"<allow>" |
|
222 |
+"<principal>public</principal>" |
|
223 |
+"<permission>read</permission>" |
|
224 |
+"</allow>" |
|
225 |
+"</acl>"; |
|
226 |
MetaCatUtil.debugMessage("xml document: "+content, 55); |
|
227 |
assert(handleXMLDocument(content, name, "insert")); |
|
228 |
|
|
229 |
} |
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
/** |
|
234 |
* Test read a xml document in xml format successfully |
|
235 |
*/ |
|
236 |
public void testReadXMLDocumentXMLFormat() |
|
237 |
{ |
|
238 |
String name = "john"+MetaCatUtil.getOption("accNumSeparator") |
|
239 |
+serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1"; |
|
240 |
assert(handleReadAction(name, "xml")); |
|
241 |
|
|
242 |
} |
|
243 |
|
|
244 |
/** |
|
245 |
* Test read a xml document in html format successfully |
|
246 |
*/ |
|
247 |
public void testReadXMLDocumentHTMLFormat() |
|
248 |
{ |
|
249 |
String name = "john"+MetaCatUtil.getOption("accNumSeparator") |
|
250 |
+serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1"; |
|
251 |
assert(handleReadAction(name, "html")); |
|
252 |
|
|
253 |
} |
|
254 |
|
|
255 |
|
|
256 |
/** |
|
257 |
* Test read a xml document in zip format successfully |
|
258 |
*/ |
|
259 |
public void testReadXMLDocumentZipFormat() |
|
260 |
{ |
|
261 |
String name = "john"+MetaCatUtil.getOption("accNumSeparator") |
|
262 |
+serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1"; |
|
263 |
assert(handleReadAction(name, "zip")); |
|
264 |
|
|
265 |
} |
|
266 |
|
|
267 |
/** |
|
268 |
* Test insert a xml document successfully |
|
269 |
*/ |
|
270 |
public void testUpdateXMLDocument() |
|
271 |
{ |
|
272 |
String name = "john"+MetaCatUtil.getOption("accNumSeparator") |
|
273 |
+serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2"; |
|
274 |
MetaCatUtil.debugMessage("docid: "+name, 20); |
|
275 |
String content="<?xml version=\"1.0\"?>" |
|
276 |
+"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//" |
|
277 |
+"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb." |
|
278 |
+"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">" |
|
279 |
+"<acl authSystem=\"knb\" order=\"allowFirst\">" |
|
280 |
+"<identifier>"+name+"</identifier>" |
|
281 |
+"<allow>" |
|
282 |
+"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>" |
|
283 |
+"<permission>all</permission>" |
|
284 |
+"</allow>" |
|
285 |
+"<allow>" |
|
286 |
+"<principal>public</principal>" |
|
287 |
+"<permission>read</permission>" |
|
288 |
+"</allow>" |
|
289 |
+"</acl>"; |
|
290 |
MetaCatUtil.debugMessage("xml document: "+content, 55); |
|
291 |
assert(handleXMLDocument(content, name, "update")); |
|
292 |
|
|
293 |
} |
|
294 |
|
|
295 |
/** |
|
296 |
* Test delete a xml document successfully |
|
297 |
*/ |
|
298 |
public void testDeleteXMLDocument() |
|
299 |
{ |
|
300 |
String name = "john"+MetaCatUtil.getOption("accNumSeparator") |
|
301 |
+serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2"; |
|
302 |
assert(handleDeleteFile(name)); |
|
303 |
|
|
304 |
} |
|
305 |
|
|
306 |
/** |
|
307 |
* Test logout action |
|
308 |
*/ |
|
309 |
public void testLogOut() |
|
310 |
{ |
|
311 |
|
|
312 |
assert(handleLogOut()); |
|
313 |
|
|
314 |
} |
|
315 |
|
|
316 |
/** |
|
317 |
* Method to hanld login action |
|
318 |
* |
|
319 |
* @param usrerName, the DN name of the test method |
|
320 |
* @param passWord, the passwd of the user |
|
321 |
*/ |
|
322 |
|
|
151 | 323 |
public boolean logIn(String userName, String passWord) |
152 | 324 |
{ |
153 | 325 |
Properties prop = new Properties(); |
... | ... | |
169 | 341 |
return connected; |
170 | 342 |
} |
171 | 343 |
|
344 |
/** |
|
345 |
* Method to hanld logout action |
|
346 |
* |
|
347 |
* @param usrerName, the DN name of the test method |
|
348 |
* @param passWord, the passwd of the user |
|
349 |
*/ |
|
350 |
|
|
351 |
public boolean handleLogOut() |
|
352 |
{ |
|
353 |
boolean disConnected =false; |
|
354 |
Properties prop = new Properties(); |
|
355 |
prop.put("action", "logout"); |
|
356 |
prop.put("qformat", "xml"); |
|
357 |
|
|
358 |
String response = getMetacatString(prop); |
|
359 |
HttpMessage.setCookie(null); |
|
360 |
|
|
361 |
if (response.indexOf("<logout>") != -1) |
|
362 |
{ |
|
363 |
disConnected = true; |
|
364 |
} |
|
365 |
else |
|
366 |
{ |
|
367 |
disConnected = false; |
|
368 |
} |
|
369 |
|
|
370 |
return disConnected; |
|
371 |
} |
|
372 |
|
|
373 |
/** |
|
374 |
* Method to hanld read both xml and data file |
|
375 |
* |
|
376 |
* @param docid, the docid of the document want to read |
|
377 |
* @param qformat, the format of document user want to get |
|
378 |
*/ |
|
379 |
public boolean handleReadAction(String docid, String qformat) |
|
380 |
{ |
|
381 |
Properties prop = new Properties(); |
|
382 |
String message =""; |
|
383 |
prop.put("action", "read"); |
|
384 |
prop.put("qformat", qformat); |
|
385 |
prop.put("docid", docid); |
|
386 |
|
|
387 |
message = getMetacatString(prop); |
|
388 |
message = message.trim(); |
|
389 |
if (message==null || message.equals("")||message.indexOf("<error>") != -1 ) |
|
390 |
{//there was an error |
|
391 |
|
|
392 |
return false; |
|
393 |
} |
|
394 |
else |
|
395 |
{//successfully |
|
396 |
return true; |
|
397 |
} |
|
398 |
|
|
399 |
} |
|
400 |
|
|
401 |
|
|
402 |
/** |
|
403 |
* Method to hanld inset or update xml document |
|
404 |
* |
|
405 |
* @param xmlDocument, the content of xml qformat |
|
406 |
* @param docid, the docid of the document |
|
407 |
* @param action, insert or update |
|
408 |
*/ |
|
409 |
public boolean handleXMLDocument(String xmlDocument, String docid, |
|
410 |
String action) |
|
411 |
|
|
412 |
{ //-attempt to write file to metacat |
|
413 |
String access = "no"; |
|
414 |
StringBuffer fileText = new StringBuffer(); |
|
415 |
StringBuffer messageBuf = new StringBuffer(); |
|
416 |
String accessFileId = null; |
|
417 |
Properties prop = new Properties(); |
|
418 |
prop.put("action", action); |
|
419 |
prop.put("public", access); //This is the old way of controlling access |
|
420 |
prop.put("doctext", xmlDocument); |
|
421 |
prop.put("docid", docid); |
|
422 |
|
|
423 |
String message = getMetacatString(prop); |
|
424 |
|
|
425 |
if(message.indexOf("<error>") != -1) |
|
426 |
{//there was an error |
|
427 |
|
|
428 |
return false; |
|
429 |
} |
|
430 |
else if(message.indexOf("<success>") != -1) |
|
431 |
{//the operation worked |
|
432 |
//write the file to the cache and return the file object |
|
433 |
return true; |
|
434 |
|
|
435 |
} |
|
436 |
else |
|
437 |
{//something weird happened. |
|
438 |
return false; |
|
439 |
} |
|
440 |
|
|
441 |
|
|
442 |
} |
|
443 |
|
|
444 |
public boolean handleDeleteFile(String name) |
|
445 |
{ |
|
446 |
|
|
447 |
Properties prop = new Properties(); |
|
448 |
prop.put("action", "delete"); |
|
449 |
prop.put("docid", name); |
|
450 |
|
|
451 |
String message = getMetacatString(prop); |
|
452 |
System.out.println("Message: "+message); |
|
453 |
if(message.indexOf("<error>") != -1) |
|
454 |
{//there was an error |
|
455 |
|
|
456 |
return false; |
|
457 |
} |
|
458 |
else if(message.indexOf("<success>") != -1) |
|
459 |
{//the operation worked |
|
460 |
//write the file to the cache and return the file object |
|
461 |
return true; |
|
462 |
|
|
463 |
} |
|
464 |
else |
|
465 |
{//something weird happened. |
|
466 |
return false; |
|
467 |
} |
|
468 |
} |
|
469 |
|
|
172 | 470 |
public String getMetacatString(Properties prop) |
173 | 471 |
{ |
174 | 472 |
String response = null; |
... | ... | |
191 | 489 |
} |
192 | 490 |
catch(Exception e) |
193 | 491 |
{ |
194 |
|
|
492 |
return null; |
|
195 | 493 |
} |
196 | 494 |
|
197 | 495 |
return response; |
Also available in: Unified diff
Add test case for inert, read, update, delete xml documents and logout action too.