1 |
1783
|
jones
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Copyright: 2003 Regents of the University of California and the
|
4 |
|
|
* National Center for Ecological Analysis and Synthesis
|
5 |
|
|
* Purpose: To test the MetaCatURL class 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.metacattest.client;
|
27 |
|
|
|
28 |
2242
|
sgarg
|
import java.io.File;
|
29 |
1784
|
jones
|
import java.io.FileReader;
|
30 |
|
|
import java.io.IOException;
|
31 |
|
|
import java.io.Reader;
|
32 |
1789
|
jones
|
import java.io.StringReader;
|
33 |
1791
|
jones
|
import java.util.Calendar;
|
34 |
2242
|
sgarg
|
import java.util.Date;
|
35 |
1791
|
jones
|
import java.util.GregorianCalendar;
|
36 |
|
|
import java.util.SimpleTimeZone;
|
37 |
|
|
import java.util.TimeZone;
|
38 |
1784
|
jones
|
|
39 |
2242
|
sgarg
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
40 |
|
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
41 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
42 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
43 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
44 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
45 |
|
|
import edu.ucsb.nceas.utilities.IOUtil;
|
46 |
1783
|
jones
|
import junit.framework.Test;
|
47 |
|
|
import junit.framework.TestCase;
|
48 |
|
|
import junit.framework.TestSuite;
|
49 |
2265
|
sgarg
|
import java.io.FileInputStream;
|
50 |
1783
|
jones
|
|
51 |
|
|
/**
|
52 |
|
|
* A JUnit test for testing Step class processing
|
53 |
|
|
*/
|
54 |
|
|
public class MetacatClientTest extends TestCase
|
55 |
|
|
{
|
56 |
1828
|
jones
|
private String metacatUrl = "@systemidserver@@servlet-path@";
|
57 |
1800
|
tao
|
private String wrongMetacatUrl=
|
58 |
|
|
"http://somepalce.somewhere.com/some/servlet/metacat";
|
59 |
1783
|
jones
|
private String username = "@mcuser@";
|
60 |
|
|
private String password = "@mcpassword@";
|
61 |
1800
|
tao
|
private String anotheruser = "@mcanotheruser@";
|
62 |
|
|
private String anotherpassword = "@mcanotherpassword@";
|
63 |
1783
|
jones
|
private String failpass = "uidfnkj43987yfdn";
|
64 |
1791
|
jones
|
private String prefix = "test";
|
65 |
|
|
private String newdocid = null;
|
66 |
1784
|
jones
|
private String testfile = "test/jones.204.22.xml";
|
67 |
2242
|
sgarg
|
private String onlinetestdatafile = "test/onlineDataFile1";
|
68 |
1787
|
tao
|
private String queryFile = "test/query.xml";
|
69 |
1784
|
jones
|
private String testdocument = "";
|
70 |
1783
|
jones
|
private Metacat m;
|
71 |
|
|
|
72 |
|
|
/**
|
73 |
|
|
* Constructor to build the test
|
74 |
|
|
*
|
75 |
|
|
* @param name the name of the test method
|
76 |
|
|
*/
|
77 |
|
|
public MetacatClientTest(String name)
|
78 |
|
|
{
|
79 |
|
|
super(name);
|
80 |
1791
|
jones
|
newdocid = generateDocid();
|
81 |
1783
|
jones
|
}
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* Establish a testing framework by initializing appropriate objects
|
85 |
|
|
*/
|
86 |
|
|
public void setUp()
|
87 |
|
|
{
|
88 |
|
|
try {
|
89 |
1784
|
jones
|
FileReader fr = new FileReader(testfile);
|
90 |
1788
|
jones
|
testdocument = IOUtil.getAsString(fr, true);
|
91 |
1784
|
jones
|
} catch (IOException ioe) {
|
92 |
|
|
fail("Can't read test data to run the test: " + testfile);
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
try {
|
96 |
1828
|
jones
|
System.err.println("Test Metacat: " + metacatUrl);
|
97 |
1783
|
jones
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
98 |
|
|
} catch (MetacatInaccessibleException mie) {
|
99 |
1822
|
jones
|
System.err.println("Metacat is: " + metacatUrl);
|
100 |
1783
|
jones
|
fail("Metacat connection failed." + mie.getMessage());
|
101 |
|
|
}
|
102 |
|
|
}
|
103 |
2242
|
sgarg
|
|
104 |
1783
|
jones
|
/**
|
105 |
|
|
* Release any objects after tests are complete
|
106 |
|
|
*/
|
107 |
|
|
public void tearDown()
|
108 |
|
|
{
|
109 |
|
|
}
|
110 |
2242
|
sgarg
|
|
111 |
1783
|
jones
|
/**
|
112 |
|
|
* Create a suite of tests to be run together
|
113 |
|
|
*/
|
114 |
|
|
public static Test suite()
|
115 |
|
|
{
|
116 |
2242
|
sgarg
|
TestSuite suite = new TestSuite();
|
117 |
2265
|
sgarg
|
suite.addTest(new MetacatClientTest("initialize"));
|
118 |
|
|
suite.addTest(new MetacatClientTest("invalidLogin"));
|
119 |
|
|
suite.addTest(new MetacatClientTest("logoutAndInvalidInsert"));
|
120 |
|
|
suite.addTest(new MetacatClientTest("login"));
|
121 |
|
|
suite.addTest(new MetacatClientTest("insert"));
|
122 |
2338
|
tao
|
suite.addTest(new MetacatClientTest("getNewestDocRevision"));
|
123 |
2265
|
sgarg
|
suite.addTest(new MetacatClientTest("upload"));
|
124 |
|
|
suite.addTest(new MetacatClientTest("upload_stream"));
|
125 |
|
|
suite.addTest(new MetacatClientTest("invalidRead"));
|
126 |
|
|
suite.addTest(new MetacatClientTest("read"));
|
127 |
|
|
suite.addTest(new MetacatClientTest("query"));
|
128 |
|
|
suite.addTest(new MetacatClientTest("invalidUpdate"));
|
129 |
|
|
suite.addTest(new MetacatClientTest("update"));
|
130 |
|
|
suite.addTest(new MetacatClientTest("invalidDelete"));
|
131 |
|
|
suite.addTest(new MetacatClientTest("delete"));
|
132 |
|
|
suite.addTest(new MetacatClientTest("inaccessibleMetacat"));
|
133 |
|
|
suite.addTest(new MetacatClientTest("reuseSession"));
|
134 |
|
|
suite.addTest(new MetacatClientTest("reuseInvalidSession"));
|
135 |
|
|
return suite;
|
136 |
|
|
}
|
137 |
2242
|
sgarg
|
|
138 |
1783
|
jones
|
/**
|
139 |
|
|
* Run an initial test that always passes to check that the test
|
140 |
|
|
* harness is working.
|
141 |
|
|
*/
|
142 |
|
|
public void initialize()
|
143 |
|
|
{
|
144 |
|
|
assertTrue(1 == 1);
|
145 |
|
|
}
|
146 |
2242
|
sgarg
|
|
147 |
1783
|
jones
|
/**
|
148 |
|
|
* Test the login() function with valid credentials
|
149 |
|
|
*/
|
150 |
|
|
public void login()
|
151 |
|
|
{
|
152 |
|
|
// Try a valid login
|
153 |
|
|
try {
|
154 |
1822
|
jones
|
String response = m.login(username, password);
|
155 |
|
|
System.err.println("Login response: " + response);
|
156 |
|
|
assertTrue(response != null);
|
157 |
|
|
assertTrue(response.indexOf("<login>") != -1);
|
158 |
|
|
String sessionId = m.getSessionId();
|
159 |
|
|
System.err.println("Session ID: " + m.getSessionId());
|
160 |
1825
|
jones
|
assertTrue(sessionId != null);
|
161 |
|
|
assertTrue(response.indexOf(m.getSessionId()) != -1);
|
162 |
1783
|
jones
|
} catch (MetacatAuthException mae) {
|
163 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
164 |
|
|
} catch (MetacatInaccessibleException mie) {
|
165 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
/**
|
170 |
|
|
* Test the login() function with INVALID credentials
|
171 |
|
|
*/
|
172 |
|
|
public void invalidLogin()
|
173 |
|
|
{
|
174 |
|
|
// Try an invalid login
|
175 |
|
|
try {
|
176 |
|
|
m.login(username, failpass);
|
177 |
|
|
fail("Authorization should have failed.");
|
178 |
|
|
} catch (MetacatAuthException mae) {
|
179 |
|
|
assertTrue(1 == 1);
|
180 |
|
|
} catch (MetacatInaccessibleException mie) {
|
181 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
2242
|
sgarg
|
|
185 |
1800
|
tao
|
/**
|
186 |
|
|
* Test the logout() function. When logout, user will be public, it couldn't
|
187 |
|
|
* insert a document.
|
188 |
|
|
*/
|
189 |
|
|
public void logoutAndInvalidInsert()
|
190 |
|
|
{
|
191 |
|
|
try {
|
192 |
|
|
String identifier = newdocid + ".1";
|
193 |
|
|
m.login(username, password);
|
194 |
|
|
m.logout();
|
195 |
2242
|
sgarg
|
String response = m.insert(identifier,
|
196 |
1800
|
tao
|
new StringReader(testdocument), null);
|
197 |
1822
|
jones
|
System.err.println("Response in logout: "+response);
|
198 |
1800
|
tao
|
assertTrue(response.indexOf("<success>") == -1);
|
199 |
|
|
} catch (MetacatAuthException mae) {
|
200 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
201 |
|
|
} catch (MetacatInaccessibleException mie) {
|
202 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
203 |
|
|
} catch (InsufficientKarmaException ike) {
|
204 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
205 |
|
|
} catch (MetacatException me) {
|
206 |
2265
|
sgarg
|
if(me.getMessage().
|
207 |
|
|
indexOf("Permission denied for user public inser") == -1){
|
208 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
209 |
|
|
}
|
210 |
1800
|
tao
|
} catch (Exception e) {
|
211 |
|
|
fail("General exception:\n" + e.getMessage());
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
1784
|
jones
|
|
215 |
|
|
/**
|
216 |
|
|
* Test the read() function with a known document
|
217 |
|
|
*/
|
218 |
|
|
public void read()
|
219 |
|
|
{
|
220 |
|
|
try {
|
221 |
|
|
m.login(username, password);
|
222 |
1791
|
jones
|
Reader r = m.read(newdocid+".1");
|
223 |
1788
|
jones
|
String doc = IOUtil.getAsString(r, true);
|
224 |
1800
|
tao
|
doc = doc +"\n";
|
225 |
1784
|
jones
|
System.err.println(doc);
|
226 |
|
|
assertTrue(doc.equals(testdocument));
|
227 |
|
|
} catch (MetacatAuthException mae) {
|
228 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
229 |
|
|
} catch (MetacatInaccessibleException mie) {
|
230 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
231 |
|
|
} catch (Exception e) {
|
232 |
|
|
fail("General exception:\n" + e.getMessage());
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
2242
|
sgarg
|
|
236 |
1787
|
tao
|
/**
|
237 |
1800
|
tao
|
* A user try to read a document which it doesn't have read permission
|
238 |
|
|
*/
|
239 |
|
|
public void invalidRead()
|
240 |
|
|
{
|
241 |
|
|
try {
|
242 |
|
|
m.login(anotheruser, anotherpassword);
|
243 |
|
|
Reader r = m.read(newdocid+".1");
|
244 |
|
|
String doc = IOUtil.getAsString(r, true);
|
245 |
|
|
assertTrue(doc.indexOf("<error>") != -1);
|
246 |
|
|
System.err.println(doc);
|
247 |
|
|
} catch (MetacatAuthException mae) {
|
248 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
249 |
|
|
} catch (MetacatInaccessibleException mie) {
|
250 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
251 |
|
|
} catch (InsufficientKarmaException ike) {
|
252 |
|
|
assertTrue(1 == 1);
|
253 |
|
|
} catch (MetacatException e) {
|
254 |
|
|
fail("Metacat exception:\n" + e.getMessage());
|
255 |
|
|
} catch (IOException ioe) {
|
256 |
|
|
fail("IO exception:\n" + ioe.getMessage());
|
257 |
|
|
}
|
258 |
|
|
}
|
259 |
2242
|
sgarg
|
|
260 |
1800
|
tao
|
/**
|
261 |
1787
|
tao
|
* Test the query() function with a known document
|
262 |
|
|
*/
|
263 |
|
|
public void query()
|
264 |
|
|
{
|
265 |
|
|
try {
|
266 |
1828
|
jones
|
m.login(username,password);
|
267 |
1787
|
tao
|
FileReader fr = new FileReader(queryFile);
|
268 |
|
|
Reader r = m.query(fr);
|
269 |
1828
|
jones
|
System.err.println("Starting query...");
|
270 |
1788
|
jones
|
String result = IOUtil.getAsString(r, true);
|
271 |
1828
|
jones
|
System.err.println("Query result:\n" + result);
|
272 |
1792
|
jones
|
assertTrue(result.indexOf(newdocid+".1")!=-1);
|
273 |
1828
|
jones
|
} catch (MetacatAuthException mae) {
|
274 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
275 |
1787
|
tao
|
} catch (MetacatInaccessibleException mie) {
|
276 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
277 |
|
|
} catch (Exception e) {
|
278 |
|
|
fail("General exception:\n" + e.getMessage());
|
279 |
|
|
}
|
280 |
|
|
}
|
281 |
1789
|
jones
|
|
282 |
|
|
/**
|
283 |
|
|
* Test the insert() function with a known document
|
284 |
|
|
*/
|
285 |
|
|
public void insert()
|
286 |
|
|
{
|
287 |
|
|
try {
|
288 |
1791
|
jones
|
String identifier = newdocid + ".1";
|
289 |
1789
|
jones
|
m.login(username, password);
|
290 |
2242
|
sgarg
|
String response = m.insert(identifier,
|
291 |
1789
|
jones
|
new StringReader(testdocument), null);
|
292 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
293 |
1791
|
jones
|
assertTrue(response.indexOf(identifier) != -1);
|
294 |
|
|
System.err.println(response);
|
295 |
1789
|
jones
|
|
296 |
|
|
} catch (MetacatAuthException mae) {
|
297 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
298 |
|
|
} catch (MetacatInaccessibleException mie) {
|
299 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
300 |
|
|
} catch (InsufficientKarmaException ike) {
|
301 |
1800
|
tao
|
assertTrue(1 == 1);
|
302 |
1789
|
jones
|
fail("Insufficient karma:\n" + ike.getMessage());
|
303 |
|
|
} catch (MetacatException me) {
|
304 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
305 |
|
|
} catch (Exception e) {
|
306 |
|
|
fail("General exception:\n" + e.getMessage());
|
307 |
|
|
}
|
308 |
|
|
}
|
309 |
1791
|
jones
|
|
310 |
|
|
/**
|
311 |
2242
|
sgarg
|
* Test the upload() function with a known document
|
312 |
|
|
*/
|
313 |
|
|
public void upload()
|
314 |
|
|
{
|
315 |
|
|
try {
|
316 |
|
|
newdocid = generateDocid();
|
317 |
|
|
String identifier = newdocid + ".1";
|
318 |
|
|
m.login(username, password);
|
319 |
|
|
String response = m.upload(identifier,
|
320 |
|
|
new File(onlinetestdatafile));
|
321 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
322 |
|
|
assertTrue(response.indexOf(identifier) != -1);
|
323 |
|
|
System.err.println(response);
|
324 |
|
|
|
325 |
|
|
} catch (MetacatAuthException mae) {
|
326 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
327 |
|
|
} catch (MetacatInaccessibleException mie) {
|
328 |
|
|
mie.printStackTrace();
|
329 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
330 |
|
|
} catch (InsufficientKarmaException ike) {
|
331 |
|
|
assertTrue(1 == 1);
|
332 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
333 |
|
|
} catch (MetacatException me) {
|
334 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
335 |
|
|
} catch (Exception e) {
|
336 |
|
|
fail("General exception:\n" + e.getMessage());
|
337 |
|
|
}
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
/**
|
341 |
2265
|
sgarg
|
* Test the upload() function by passing an InputStream
|
342 |
|
|
*/
|
343 |
|
|
public void upload_stream()
|
344 |
|
|
{
|
345 |
|
|
try {
|
346 |
|
|
newdocid = generateDocid();
|
347 |
|
|
String identifier = newdocid + ".1";
|
348 |
|
|
m.login(username, password);
|
349 |
|
|
File testFile = new File(onlinetestdatafile);
|
350 |
|
|
String response = m.upload(identifier, "onlineDataFile1",
|
351 |
|
|
new FileInputStream(testFile),
|
352 |
|
|
(int) testFile.length());
|
353 |
|
|
|
354 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
355 |
|
|
assertTrue(response.indexOf(identifier) != -1);
|
356 |
|
|
System.err.println(response);
|
357 |
|
|
|
358 |
|
|
} catch (MetacatAuthException mae) {
|
359 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
360 |
|
|
} catch (MetacatInaccessibleException mie) {
|
361 |
|
|
mie.printStackTrace();
|
362 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
363 |
|
|
} catch (InsufficientKarmaException ike) {
|
364 |
|
|
assertTrue(1 == 1);
|
365 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
366 |
|
|
} catch (MetacatException me) {
|
367 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
368 |
|
|
} catch (Exception e) {
|
369 |
|
|
fail("General exception:\n" + e.getMessage());
|
370 |
|
|
}
|
371 |
|
|
}
|
372 |
|
|
|
373 |
|
|
/**
|
374 |
1800
|
tao
|
* Test the invalidUpdate() function. A user try to update a document
|
375 |
|
|
* which it doesn't have permission
|
376 |
|
|
*/
|
377 |
|
|
public void invalidUpdate()
|
378 |
|
|
{
|
379 |
|
|
try {
|
380 |
|
|
String identifier = newdocid + ".2";
|
381 |
|
|
m.login(anotheruser, anotherpassword);
|
382 |
2242
|
sgarg
|
String response = m.update(identifier,
|
383 |
1800
|
tao
|
new StringReader(testdocument), null);
|
384 |
|
|
assertTrue(response.indexOf("<success>") == -1);
|
385 |
2242
|
sgarg
|
|
386 |
1800
|
tao
|
} catch (MetacatAuthException mae) {
|
387 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
388 |
|
|
} catch (MetacatInaccessibleException mie) {
|
389 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
390 |
|
|
} catch (InsufficientKarmaException ike) {
|
391 |
|
|
assertTrue(1 == 1);
|
392 |
|
|
} catch (MetacatException me) {
|
393 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
394 |
|
|
} catch (Exception e) {
|
395 |
|
|
fail("General exception:\n" + e.getMessage());
|
396 |
|
|
}
|
397 |
|
|
}
|
398 |
2242
|
sgarg
|
|
399 |
1800
|
tao
|
/**
|
400 |
1795
|
jones
|
* Test the update() function with a known document
|
401 |
|
|
*/
|
402 |
|
|
public void update()
|
403 |
|
|
{
|
404 |
|
|
try {
|
405 |
|
|
String identifier = newdocid + ".2";
|
406 |
|
|
m.login(username, password);
|
407 |
2242
|
sgarg
|
String response = m.update(identifier,
|
408 |
1795
|
jones
|
new StringReader(testdocument), null);
|
409 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
410 |
|
|
assertTrue(response.indexOf(identifier) != -1);
|
411 |
|
|
System.err.println(response);
|
412 |
|
|
|
413 |
|
|
} catch (MetacatAuthException mae) {
|
414 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
415 |
|
|
} catch (MetacatInaccessibleException mie) {
|
416 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
417 |
|
|
} catch (InsufficientKarmaException ike) {
|
418 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
419 |
|
|
} catch (MetacatException me) {
|
420 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
421 |
|
|
} catch (Exception e) {
|
422 |
|
|
fail("General exception:\n" + e.getMessage());
|
423 |
|
|
}
|
424 |
|
|
}
|
425 |
2242
|
sgarg
|
|
426 |
1800
|
tao
|
/**
|
427 |
|
|
* A user try delete a document which it doesn't have permission
|
428 |
|
|
*/
|
429 |
|
|
public void invalidDelete()
|
430 |
|
|
{
|
431 |
|
|
try {
|
432 |
|
|
String identifier = newdocid + ".2";
|
433 |
|
|
m.login(anotheruser, anotherpassword);
|
434 |
|
|
String response = m.delete(identifier);
|
435 |
|
|
assertTrue(response.indexOf("<success>") == -1);
|
436 |
|
|
System.err.println(response);
|
437 |
1795
|
jones
|
|
438 |
1800
|
tao
|
} catch (MetacatAuthException mae) {
|
439 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
440 |
|
|
} catch (MetacatInaccessibleException mie) {
|
441 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
442 |
|
|
} catch (InsufficientKarmaException ike) {
|
443 |
|
|
assertTrue(1 == 1);
|
444 |
|
|
} catch (MetacatException me) {
|
445 |
|
|
assertTrue(1 == 1);
|
446 |
|
|
} catch (Exception e) {
|
447 |
|
|
fail("General exception:\n" + e.getMessage());
|
448 |
|
|
}
|
449 |
|
|
}
|
450 |
2242
|
sgarg
|
|
451 |
1795
|
jones
|
/**
|
452 |
|
|
* Test the delete() function with a known document
|
453 |
|
|
*/
|
454 |
|
|
public void delete()
|
455 |
|
|
{
|
456 |
|
|
try {
|
457 |
|
|
String identifier = newdocid + ".2";
|
458 |
|
|
m.login(username, password);
|
459 |
|
|
String response = m.delete(identifier);
|
460 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
461 |
|
|
System.err.println(response);
|
462 |
|
|
|
463 |
|
|
} catch (MetacatAuthException mae) {
|
464 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
465 |
|
|
} catch (MetacatInaccessibleException mie) {
|
466 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
467 |
|
|
} catch (InsufficientKarmaException ike) {
|
468 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
469 |
|
|
} catch (MetacatException me) {
|
470 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
471 |
|
|
} catch (Exception e) {
|
472 |
|
|
fail("General exception:\n" + e.getMessage());
|
473 |
|
|
}
|
474 |
|
|
}
|
475 |
2242
|
sgarg
|
|
476 |
1800
|
tao
|
/**
|
477 |
|
|
* Test the to connect a wrong metacat url. Create a new metacat with a
|
478 |
|
|
* inaccessible url. Then try to login it. If get a MetacatInaccessible
|
479 |
|
|
* exception, this is right.
|
480 |
|
|
*/
|
481 |
1826
|
jones
|
public void inaccessibleMetacat()
|
482 |
1800
|
tao
|
{
|
483 |
|
|
Metacat mWrong = null;
|
484 |
|
|
try {
|
485 |
|
|
mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
|
486 |
|
|
} catch (MetacatInaccessibleException mie) {
|
487 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
488 |
|
|
}
|
489 |
2242
|
sgarg
|
|
490 |
1800
|
tao
|
try {
|
491 |
|
|
mWrong.login(username, password);
|
492 |
|
|
} catch (MetacatInaccessibleException mie) {
|
493 |
|
|
assertTrue(1 == 1);
|
494 |
|
|
} catch (MetacatAuthException mae) {
|
495 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
496 |
|
|
}
|
497 |
|
|
}
|
498 |
1795
|
jones
|
|
499 |
|
|
/**
|
500 |
1826
|
jones
|
* Try to perform an action that requires a session to be valid without
|
501 |
|
|
* having logged in first by setting the sessionId from a previous
|
502 |
|
|
* session. Then insert a document.
|
503 |
|
|
*/
|
504 |
|
|
public void reuseSession()
|
505 |
|
|
{
|
506 |
|
|
String oldSessionId = "";
|
507 |
|
|
try {
|
508 |
|
|
Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
|
509 |
|
|
String response = mtemp.login(username, password);
|
510 |
|
|
oldSessionId = mtemp.getSessionId();
|
511 |
1828
|
jones
|
System.err.println("SessionId (mtemp): " + oldSessionId);
|
512 |
1826
|
jones
|
} catch (MetacatAuthException mae) {
|
513 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
514 |
|
|
} catch (MetacatInaccessibleException mie) {
|
515 |
|
|
System.err.println("Metacat is: " + metacatUrl);
|
516 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
517 |
|
|
}
|
518 |
|
|
|
519 |
|
|
try {
|
520 |
|
|
String identifier = generateDocid() + ".1";
|
521 |
|
|
Metacat m2 = null;
|
522 |
|
|
try {
|
523 |
|
|
m2 = MetacatFactory.createMetacatConnection(metacatUrl);
|
524 |
|
|
} catch (MetacatInaccessibleException mie) {
|
525 |
|
|
System.err.println("Metacat is: " + metacatUrl);
|
526 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
527 |
|
|
}
|
528 |
1828
|
jones
|
System.err.println("SessionId (m2): " + m2.getSessionId());
|
529 |
1826
|
jones
|
m2.setSessionId(oldSessionId);
|
530 |
1828
|
jones
|
System.err.println("SessionId (m2 after set): " + m2.getSessionId());
|
531 |
2242
|
sgarg
|
String response = m2.insert(identifier,
|
532 |
1826
|
jones
|
new StringReader(testdocument), null);
|
533 |
|
|
System.err.println("Reuse Insert response: " + response);
|
534 |
|
|
assertTrue(response.indexOf("<success>") != -1);
|
535 |
|
|
} catch (MetacatInaccessibleException mie) {
|
536 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
537 |
|
|
} catch (InsufficientKarmaException ike) {
|
538 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
539 |
|
|
} catch (MetacatException me) {
|
540 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
541 |
|
|
} catch (Exception e) {
|
542 |
|
|
fail("General exception:\n" + e.getMessage());
|
543 |
|
|
}
|
544 |
|
|
}
|
545 |
|
|
|
546 |
|
|
/**
|
547 |
1828
|
jones
|
* Try to perform an action that requires a session to be valid without
|
548 |
2242
|
sgarg
|
* having logged in first, but use an invalid session identifier.
|
549 |
1829
|
jones
|
* Then insert a document, which should fail.
|
550 |
1828
|
jones
|
*/
|
551 |
|
|
public void reuseInvalidSession()
|
552 |
|
|
{
|
553 |
1829
|
jones
|
System.err.println("Starting resuseInvalidSession test...");
|
554 |
1828
|
jones
|
String oldSessionId = "foobar";
|
555 |
|
|
try {
|
556 |
|
|
String identifier = generateDocid() + ".1";
|
557 |
1829
|
jones
|
Metacat m3 = null;
|
558 |
1828
|
jones
|
try {
|
559 |
1829
|
jones
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
560 |
1828
|
jones
|
} catch (MetacatInaccessibleException mie) {
|
561 |
|
|
System.err.println("Metacat is: " + metacatUrl);
|
562 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
563 |
|
|
}
|
564 |
1829
|
jones
|
System.err.println("SessionId (m3): " + m3.getSessionId());
|
565 |
|
|
m3.setSessionId(oldSessionId);
|
566 |
|
|
System.err.println("SessionId (m3 after set): " + m3.getSessionId());
|
567 |
|
|
System.err.println("Performing resuseInvalidSession insert: " +
|
568 |
|
|
identifier);
|
569 |
2242
|
sgarg
|
String response = m3.insert(identifier,
|
570 |
1828
|
jones
|
new StringReader(testdocument), null);
|
571 |
1829
|
jones
|
System.err.println("ReuseInvalid Insert response: " + response);
|
572 |
1828
|
jones
|
assertTrue(response.indexOf("<success>") == -1);
|
573 |
|
|
} catch (MetacatInaccessibleException mie) {
|
574 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
575 |
|
|
} catch (InsufficientKarmaException ike) {
|
576 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
577 |
|
|
} catch (MetacatException me) {
|
578 |
2265
|
sgarg
|
if(me.getMessage().
|
579 |
|
|
indexOf("Permission denied for user public inser") == -1){
|
580 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
581 |
|
|
}
|
582 |
1828
|
jones
|
} catch (Exception e) {
|
583 |
|
|
fail("General exception:\n" + e.getMessage());
|
584 |
|
|
}
|
585 |
|
|
}
|
586 |
2338
|
tao
|
|
587 |
1828
|
jones
|
/**
|
588 |
2338
|
tao
|
* Try to perform an action that requires a session to be valid without
|
589 |
|
|
* having logged in first, but use an invalid session identifier.
|
590 |
|
|
* Then insert a document, which should fail.
|
591 |
|
|
*/
|
592 |
|
|
public void getNewestDocRevision()
|
593 |
|
|
{
|
594 |
|
|
System.err.println("Starting getNewestDocRevision test...");
|
595 |
|
|
try {
|
596 |
|
|
|
597 |
|
|
Metacat m3 = null;
|
598 |
|
|
try {
|
599 |
|
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
600 |
|
|
} catch (MetacatInaccessibleException mie) {
|
601 |
|
|
System.err.println("Metacat is: " + metacatUrl);
|
602 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
603 |
|
|
}
|
604 |
|
|
|
605 |
|
|
int revision = m3.getNewestDocRevision(newdocid);
|
606 |
|
|
System.err.println("Newest revision number is: " + revision);
|
607 |
|
|
assertTrue(revision == 1);
|
608 |
|
|
} catch (MetacatException me) {
|
609 |
|
|
if(me.getMessage().
|
610 |
|
|
indexOf("Permission denied for user public inser") == -1){
|
611 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
612 |
|
|
}
|
613 |
|
|
} catch (Exception e) {
|
614 |
|
|
fail("General exception:\n" + e.getMessage());
|
615 |
|
|
}
|
616 |
|
|
}
|
617 |
|
|
|
618 |
|
|
|
619 |
|
|
/**
|
620 |
1791
|
jones
|
* Create a hopefully unique docid for testing insert and update. Does
|
621 |
|
|
* not include the 'revision' part of the id.
|
622 |
|
|
*
|
623 |
|
|
* @return a String docid based on the current date and time
|
624 |
|
|
*/
|
625 |
|
|
private String generateDocid()
|
626 |
|
|
{
|
627 |
|
|
StringBuffer docid = new StringBuffer(prefix);
|
628 |
|
|
docid.append(".");
|
629 |
2242
|
sgarg
|
|
630 |
1791
|
jones
|
// Create a calendar to get the date formatted properly
|
631 |
|
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
632 |
|
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
633 |
|
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
|
634 |
|
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
|
635 |
|
|
Calendar calendar = new GregorianCalendar(pdt);
|
636 |
|
|
Date trialTime = new Date();
|
637 |
|
|
calendar.setTime(trialTime);
|
638 |
|
|
docid.append(calendar.get(Calendar.YEAR));
|
639 |
|
|
docid.append(calendar.get(Calendar.DAY_OF_YEAR));
|
640 |
|
|
docid.append(calendar.get(Calendar.HOUR_OF_DAY));
|
641 |
|
|
docid.append(calendar.get(Calendar.MINUTE));
|
642 |
|
|
docid.append(calendar.get(Calendar.SECOND));
|
643 |
|
|
|
644 |
|
|
return docid.toString();
|
645 |
|
|
}
|
646 |
1783
|
jones
|
}
|