Project

General

Profile

1 2440 sgarg
/**
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.metacattest;
27
28 5750 berkley
import java.io.InputStream;
29 5110 daigle
import java.io.InputStreamReader;
30 2440 sgarg
import java.io.Reader;
31
import java.io.StringReader;
32
import java.util.Calendar;
33
import java.util.Date;
34
import java.util.GregorianCalendar;
35
import java.util.SimpleTimeZone;
36
import java.util.TimeZone;
37
38 4145 daigle
import edu.ucsb.nceas.MCTestCase;
39 2440 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 5035 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
46 5752 leinfelder
import edu.ucsb.nceas.utilities.FileUtil;
47 2440 sgarg
import edu.ucsb.nceas.utilities.IOUtil;
48 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
49 2440 sgarg
import junit.framework.Test;
50
import junit.framework.TestSuite;
51
import java.io.File;
52
53 5750 berkley
import org.apache.commons.io.IOUtils;
54
55 2440 sgarg
/**
56
 * A JUnit test for testing Metacat when Non Ascii Characters are inserted
57
 */
58
public class NonAsciiCharacterTest
59 4145 daigle
    extends MCTestCase {
60 4080 daigle
61
    private static String metacatUrl;
62
    private static String username;
63
	private static String password;
64
	private static String anotheruser;
65
	private static String anotherpassword;
66
	static {
67
		try {
68 4231 daigle
		    metacatUrl = PropertyService.getProperty("test.metacatUrl");
69
			username = PropertyService.getProperty("test.mcUser");
70
			password = PropertyService.getProperty("test.mcPassword");
71
			anotheruser = PropertyService.getProperty("test.mcAnotherUser");
72
			anotherpassword = PropertyService.getProperty("test.mcAnotherPassword");
73 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
74
			System.err.println("Could not get property in static block: "
75
					+ pnfe.getMessage());
76
		}
77
	}
78
79 2440 sgarg
    private String prefix = "test";
80
    private String testdocument = "";
81
82
    private Metacat m;
83
84
    /**
85
     * These variables are for eml-2.0.1 only. For other eml versions,
86
     * this function might have to modified
87
     */
88
89 4356 daigle
    private String testEml_201_Header =
90 2440 sgarg
        "<?xml version=\"1.0\"?><eml:eml" +
91
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
92
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
93
        " packageId=\"eml.1.1\" system=\"knb\"" +
94
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
95
        " scope=\"system\">";
96 4356 daigle
97
    private String testEml_210_Header =
98
        "<?xml version=\"1.0\"?><eml:eml" +
99
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.0\"" +
100
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
101
        " packageId=\"eml.1.1\" system=\"knb\"" +
102
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.0 eml.xsd\"" +
103
        " scope=\"system\">";
104 2440 sgarg
105
    private String testEmlCreatorBlock =
106
        "<creator scope=\"document\">                                       " +
107
        " <individualName>                                                  " +
108
        "    <surName>Smith</surName>                                       " +
109
        " </individualName>                                                 " +
110
        "</creator>                                                         ";
111
112
    private String testEmlContactBlock =
113
        "<contact scope=\"document\">                                       " +
114
        " <individualName>                                                  " +
115
        "    <surName>Jackson</surName>                                     " +
116
        " </individualName>                                                 " +
117
        "</contact>                                                         ";
118
119
    /**
120
     * This function returns an access block based on the params passed
121
     */
122 4509 daigle
    protected String getAccessBlock(String principal, boolean grantAccess,
123 2440 sgarg
                                  boolean read, boolean write,
124
                                  boolean changePermission, boolean all) {
125
        String accessBlock = "<access " +
126
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
127
            " order=\"allowFirst\" scope=\"document\">";
128
129
        if (grantAccess) {
130
            accessBlock += "<allow>";
131
        }
132
        else {
133
            accessBlock += "<deny>";
134
        }
135
136
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
137
138
        if (all) {
139
            accessBlock += "<permission>all</permission>";
140
        }
141
        else {
142
            if (read) {
143
                accessBlock += "<permission>read</permission>";
144
            }
145
            if (write) {
146
                accessBlock += "<permission>write</permission>";
147
            }
148
            if (changePermission) {
149
                accessBlock += "<permission>changePermission</permission>";
150
            }
151
        }
152
153
        if (grantAccess) {
154
            accessBlock += "</allow>";
155
        }
156
        else {
157
            accessBlock += "</deny>";
158
        }
159
        accessBlock += "</access>";
160
161
        return accessBlock;
162
163
    }
164
165
    /**
166
     * This function returns a valid eml document with no access rules
167
     * This function is for eml-2.0.1 only. For other eml versions,
168
     * this function might have to modified
169
     */
170 4356 daigle
    private String getTestEmlDoc(String title, String emlVersion) {
171 2440 sgarg
172
        String testDocument = "";
173 4356 daigle
174
        String header;
175
		if (emlVersion == EML2_0_1) {
176
			header = testEml_201_Header;
177 5712 leinfelder
		} else if (emlVersion == EML2_1_0) {
178 4356 daigle
			header = testEml_210_Header;
179 5712 leinfelder
		} else { // if (emlVersion == EML2_1_1) {
180
			header = testEml_211_Header;
181 4356 daigle
		}
182
183 4478 daigle
        testDocument += header;
184
185
        // if this is an EML 2.1.0 or later document, the document level access is
186
        // before the dataset element.
187 5709 leinfelder
        if (emlVersion == EML2_1_0 || emlVersion == EML2_1_1) {
188 4478 daigle
        	testDocument += getAccessBlock("public", true, true, false, false, false);
189
        }
190
        testDocument += "<dataset scope=\"document\"><title>" + title + "</title>"
191
				+ testEmlCreatorBlock;
192 2440 sgarg
193
        testDocument += testEmlContactBlock;
194 4478 daigle
195
        // if this is an EML 2.0.1 or earlier document, the document level access is
196
        // inside the dataset element.
197
        if (emlVersion != EML2_1_0) {
198
        	testDocument += getAccessBlock("public", true, true, false, false, false);
199
        }
200 2440 sgarg
        testDocument += "</dataset>";
201
        testDocument += "</eml:eml>";
202
203
        return testDocument;
204
    }
205 4356 daigle
206
    /**
207
     * Returns an xml squery that searches for the doc id in the
208 5752 leinfelder
     * title of documents. This function is for eml-2.0.1+ only. For
209 4356 daigle
     * other eml versions, this function might have to modified.
210
     */
211 5752 leinfelder
    private String getTestEmlQuery(String titlePart, String emlVersion) {
212 2440 sgarg
213 4356 daigle
    	String docType;
214
    	if (emlVersion.equals(EML2_0_1)) {
215
    		docType = "eml://ecoinformatics.org/eml-2.0.1";
216 5712 leinfelder
    	} else if (emlVersion.equals(EML2_1_0)) {
217 4356 daigle
    		docType = "eml://ecoinformatics.org/eml-2.1.0";
218 5712 leinfelder
    	} else { //if (emlVersion.equals(EML2_1_1)) {
219
    		docType = "eml://ecoinformatics.org/eml-2.1.1";
220 4356 daigle
    	}
221
222
        String sQuery = "";
223
        sQuery =
224
        	"<pathquery version=\"1.0\">" +
225
        		"<meta_file_id>unspecified</meta_file_id>" +
226
        		"<querytitle>unspecified</querytitle>" +
227
        		"<returnfield>dataset/title</returnfield>" +
228
        		"<returndoctype>" + docType + "</returndoctype>" +
229
        		"<querygroup operator=\"UNION\">" +
230
        			"<queryterm casesensitive=\"false\" searchmode=\"contains\">" +
231 5752 leinfelder
        				"<value>" + titlePart + "</value>" +
232 4356 daigle
        				"<pathexpr>dataset/title</pathexpr>" +
233
        			"</queryterm>" +
234
        		"</querygroup>" +
235
        	"</pathquery>";
236
237
        return sQuery;
238
    }
239
240 2440 sgarg
    /**
241
     * Constructor to build the test
242
     *
243
     * @param name the name of the test method
244
     */
245
    public NonAsciiCharacterTest(String name) {
246
        super(name);
247
    }
248
249
    /**
250
     * Establish a testing framework by initializing appropriate objects
251
     */
252
    public void setUp() {
253
        try {
254
            System.err.println("Test Metacat: " + metacatUrl);
255
            m = MetacatFactory.createMetacatConnection(metacatUrl);
256
        }
257
        catch (MetacatInaccessibleException mie) {
258
            System.err.println("Metacat is: " + metacatUrl);
259
            fail("Metacat connection failed." + mie.getMessage());
260
        }
261
    }
262
263
    /**
264
     * Release any objects after tests are complete
265
     */
266
    public void tearDown() {
267
    }
268
269
    /**
270
     * Create a suite of tests to be run together
271
     */
272
    public static Test suite() {
273
        TestSuite suite = new TestSuite();
274
        suite.addTest(new NonAsciiCharacterTest("initialize"));
275
        // Test basic functions
276 5744 berkley
        /*suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters201Test"));
277 4356 daigle
        suite.addTest(new NonAsciiCharacterTest("invalidXMLCharacters210Test"));
278
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat201Test"));
279
        suite.addTest(new NonAsciiCharacterTest("symbolEncodedFormat210Test"));
280
        suite.addTest(new NonAsciiCharacterTest("quote201Test"));
281
        suite.addTest(new NonAsciiCharacterTest("quote210Test"));
282
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat201Test"));
283 5744 berkley
        suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat210Test"));*/
284 5752 leinfelder
        //suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter201Test"));
285 5744 berkley
        //suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter210Test"));
286 5752 leinfelder
        suite.addTest(new NonAsciiCharacterTest("unicodeCharacterTest"));
287 2440 sgarg
288 5752 leinfelder
289 2440 sgarg
        return suite;
290
    }
291
292
    /**
293
     * Run an initial test that always passes to check that the test
294
     * harness is working.
295
     */
296
    public void initialize() {
297
        assertTrue(1 == 1);
298
    }
299
300 4356 daigle
    /**
301
     * Test inserting an EML 2.0.1 document with > & <
302 2440 sgarg
     * should fail because this means an invalid xml document is being inserted
303
     */
304 4356 daigle
    public void invalidXMLCharacters201Test() {
305
    	debug("\nRunning: invalidXMLCharacters201Test");
306 2440 sgarg
        try {
307 4356 daigle
            String newdocid = generateDocid();
308 2440 sgarg
            m.login(username, password);
309 4356 daigle
            testdocument = getTestEmlDoc("Checking > & < in doc: " + newdocid, EML2_0_1);
310 2440 sgarg
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
311
            m.logout();
312
        }
313
        catch (MetacatAuthException mae) {
314
            fail("Authorization failed:\n" + mae.getMessage());
315
        }
316
        catch (MetacatInaccessibleException mie) {
317
            fail("Metacat Inaccessible:\n" + mie.getMessage());
318
        }
319
        catch (Exception e) {
320
            fail("General exception:\n" + e.getMessage());
321
        }
322
    }
323 4356 daigle
324
    /**
325
     * Test inserting an EML 2.1.0 document with > & <
326 2440 sgarg
     * should fail because this means an invalid xml document is being inserted
327
     */
328 4356 daigle
    public void invalidXMLCharacters210Test() {
329
    	debug("\nRunning: invalidXMLCharacters210Test");
330 2440 sgarg
        try {
331 4356 daigle
            String newdocid = generateDocid();
332 2440 sgarg
            m.login(username, password);
333 4356 daigle
            testdocument = getTestEmlDoc("Checking > & < in doc: " + newdocid, EML2_1_0);
334
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
335
            m.logout();
336
        }
337
        catch (MetacatAuthException mae) {
338
            fail("Authorization failed:\n" + mae.getMessage());
339
        }
340
        catch (MetacatInaccessibleException mie) {
341
            fail("Metacat Inaccessible:\n" + mie.getMessage());
342
        }
343
        catch (Exception e) {
344
            fail("General exception:\n" + e.getMessage());
345
        }
346
    }
347
348
    /**
349
     * Test inserting and reading an EML 2.0.1 document with &gt; &amp; &lt;
350
     * Read should succeed since the same document should be read
351
     * back from disk that was submitted. Query should succeed as
352
     * well because the characters in this test are not changed in
353
     * the database.
354
     */
355
    public void symbolEncodedFormat201Test() {
356
    	debug("\nRunning: symbolEncodedFormat201Test");
357
        try {
358
            String newdocid = generateDocid();
359
            m.login(username, password);
360
361
            String testTitle =
362
            	"Checking &gt; &lt; &quot; &apos; &amp; in doc: " + newdocid  + ".1";
363
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
364
            insertDocid(newdocid  + ".1", testdocument, SUCCESS, false);
365
366
            // this tests reading the document back from disk
367 2448 sgarg
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
368 4356 daigle
369
            // this tests searching for the document in the database
370
            Thread.sleep(3000);
371
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_0_1, SUCCESS);
372
373 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
374
375 2440 sgarg
            m.logout();
376
        }
377
        catch (MetacatAuthException mae) {
378
            fail("Authorization failed:\n" + mae.getMessage());
379
        }
380
        catch (MetacatInaccessibleException mie) {
381
            fail("Metacat Inaccessible:\n" + mie.getMessage());
382
        }
383
        catch (Exception e) {
384
            fail("General exception:\n" + e.getMessage());
385
        }
386
    }
387 4356 daigle
388
    /**
389
     * Test inserting and reading an EML 2.1.0 document with &gt; &amp; &lt;
390
     * Read should succeed since the same document should be read
391
     * back from disk that was submitted. Query should succeed as
392
     * well because the characters in this test are not changed in
393
     * the database.
394
     */
395
    public void symbolEncodedFormat210Test() {
396
    	debug("\nRunning: symbolEncodedFormat210Test");
397
        try {
398
            String newdocid = generateDocid();
399
            m.login(username, password);
400
401
            String testTitle =
402
            	"Checking &gt; &lt; &quot; &apos; &amp; in doc: " + newdocid + ".1";
403
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
404
            insertDocid(newdocid  + ".1", testdocument, SUCCESS, false);
405
406
            // this tests reading the document back from disk
407
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
408
409
            // this tests searching for the document in the database
410
            Thread.sleep(3000);
411
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_1_0, SUCCESS);
412
413 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
414
415 4356 daigle
            m.logout();
416
        }
417
        catch (MetacatAuthException mae) {
418
            fail("Authorization failed:\n" + mae.getMessage());
419
        }
420
        catch (MetacatInaccessibleException mie) {
421
            fail("Metacat Inaccessible:\n" + mie.getMessage());
422
        }
423
        catch (Exception e) {
424
            fail("General exception:\n" + e.getMessage());
425
        }
426
    }
427 2440 sgarg
428 4356 daigle
    /**
429
     * Test inserting and reading an EML 2.0.1 document with single quote and double quote.
430
     * Read should succeed since the same document should be read back from disk
431
     * that was submitted.  Query should succeed because we look for the converted
432
     * character (&apos; and &quot;).
433 2440 sgarg
     */
434 4356 daigle
    public void quote201Test() {
435
    	debug("\nRunning: quote201Test");
436 2440 sgarg
        try {
437 4356 daigle
            String newdocid = generateDocid();
438 2440 sgarg
            m.login(username, password);
439 4356 daigle
440
            String testTitle = "Checking ' ` \" in doc: " + newdocid  + ".1";
441
            String convertedTestTitle = "Checking &apos; ` &quot; in doc: " + newdocid  + ".1";
442
443
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
444 3760 tao
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
445 4356 daigle
446
            // this tests reading the document back from disk
447 2440 sgarg
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
448 4356 daigle
449
            // this tests searching for the document in the database
450
            Thread.sleep(3000);
451
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_0_1, SUCCESS);
452
453 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
454
455 2440 sgarg
            m.logout();
456
        }
457
        catch (MetacatAuthException mae) {
458
            fail("Authorization failed:\n" + mae.getMessage());
459
        }
460
        catch (MetacatInaccessibleException mie) {
461
            fail("Metacat Inaccessible:\n" + mie.getMessage());
462
        }
463
        catch (Exception e) {
464
            fail("General exception:\n" + e.getMessage());
465
        }
466
    }
467 4356 daigle
468
    /**
469
     * Test inserting and reading an EML 2.1.0 document with single quote and double quote.
470
     * Read should succeed since the same document should be read back from disk
471
     * that was submitted.  Query shoud succeed because we look for the converted
472
     * character (&apos; and &quot;).
473
     */
474
    public void quote210Test() {
475
    	debug("\nRunning: quote210Test");
476
        try {
477
            String newdocid = generateDocid();
478
            m.login(username, password);
479
480
            String testTitle = "Checking ' ` \" in doc: " + newdocid  + ".1";
481
            String convertedTestTitle = "Checking &apos; ` &quot; in doc: " + newdocid  + ".1";
482
483
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
484
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
485
486
            // this tests reading the document back from disk
487
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
488
489
            // this tests searching for the document in the database
490
            Thread.sleep(3000);
491
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_1_0, SUCCESS);
492
493 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
494
495 4356 daigle
            m.logout();
496
        }
497
        catch (MetacatAuthException mae) {
498
            fail("Authorization failed:\n" + mae.getMessage());
499
        }
500
        catch (MetacatInaccessibleException mie) {
501
            fail("Metacat Inaccessible:\n" + mie.getMessage());
502
        }
503
        catch (Exception e) {
504
            fail("General exception:\n" + e.getMessage());
505
        }
506
    }
507 2440 sgarg
508 4356 daigle
    /**
509
     * Test inserting and reading an EML 2.0.1 document with the code representation
510
     * of a micro sign (&#181). Read should succeed since the same document should be
511
     * read back from disk that was submitted.  Query should succeed because we look
512 5752 leinfelder
     * for the converted character (µ).
513 4356 daigle
     */
514
    public void numericCharacterReferenceFormat201Test() {
515
    	debug("\nRunning: numericCharacterReferenceFormat201Test");
516
        try {
517
            String newdocid = generateDocid();
518
            m.login(username, password);
519
520
            String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
521 5752 leinfelder
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
522 4356 daigle
523
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
524
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
525 2440 sgarg
526 4356 daigle
            // this tests reading the document back from disk
527
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
528
529
            // this tests searching for the document in the database
530
            Thread.sleep(3000);
531
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_0_1, SUCCESS);
532 4407 daigle
533
            deleteDocid(newdocid  + ".1", SUCCESS, false);
534 4356 daigle
535
            m.logout();
536
        }
537
        catch (MetacatAuthException mae) {
538
            fail("Authorization failed:\n" + mae.getMessage());
539
        }
540
        catch (MetacatInaccessibleException mie) {
541
            fail("Metacat Inaccessible:\n" + mie.getMessage());
542
        }
543
        catch (Exception e) {
544
            fail("General exception:\n" + e.getMessage());
545
        }
546
    }
547 2440 sgarg
548 4356 daigle
    /**
549
     * Test inserting and reading an EML 2.1.0 document with the code representation
550
     * of a micro sign (&#181). Read should succeed since the same document should be
551
     * read back from disk that was submitted.  Query should succeed because we look
552 5752 leinfelder
     * for the converted character (µ).
553 2440 sgarg
     */
554 4356 daigle
    public void numericCharacterReferenceFormat210Test() {
555
    	debug("\nRunning: numericCharacterReferenceFormat210Test");
556 2440 sgarg
        try {
557 4356 daigle
            String newdocid = generateDocid();
558 2440 sgarg
            m.login(username, password);
559 4356 daigle
560
            String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
561 5752 leinfelder
            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
562 4356 daigle
563
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
564 3760 tao
            insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
565 4356 daigle
566
            // this tests reading the document back from disk
567 2440 sgarg
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
568 4356 daigle
569
            // this tests searching for the document in the database
570
            Thread.sleep(3000);
571
            queryDocWhichHasTitle(newdocid  + ".1", convertedTestTitle, EML2_1_0, SUCCESS);
572
573 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
574
575 2440 sgarg
            m.logout();
576
        }
577
        catch (MetacatAuthException mae) {
578
            fail("Authorization failed:\n" + mae.getMessage());
579
        }
580
        catch (MetacatInaccessibleException mie) {
581
            fail("Metacat Inaccessible:\n" + mie.getMessage());
582
        }
583
        catch (Exception e) {
584
            fail("General exception:\n" + e.getMessage());
585
        }
586
    }
587
588 4356 daigle
    /**
589 5752 leinfelder
     * Test inserting and reading an EML 2.0.1 document with the micro sign (µ).
590 4356 daigle
     * Read should succeed since the same document should be read back from disk
591
     * that was submitted.  Query should succeed because we look for the same
592 5752 leinfelder
     * character (µ).
593 2448 sgarg
     */
594 4356 daigle
    public void nonLatinUnicodeCharacter201Test() {
595
    	debug("\nRunning: nonLatinUnicodeCharacter201Test");
596 2448 sgarg
        try {
597 4356 daigle
            String newdocid = generateDocid();
598 2448 sgarg
            m.login(username, password);
599 4356 daigle
600
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
601
602
            testdocument = getTestEmlDoc(testTitle, EML2_0_1);
603 5752 leinfelder
604
            debug("original test document:	" + testdocument);
605
606 2448 sgarg
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
607 4356 daigle
608
            // this tests reading the document back from disk
609 2448 sgarg
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
610 4356 daigle
611
            // this tests searching for the document in the database
612
            Thread.sleep(3000);
613
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_0_1, SUCCESS);
614
615 5750 berkley
            //deleteDocid(newdocid  + ".1", SUCCESS, false);
616 4407 daigle
617 2448 sgarg
            m.logout();
618
        }
619
        catch (MetacatAuthException mae) {
620
            fail("Authorization failed:\n" + mae.getMessage());
621
        }
622
        catch (MetacatInaccessibleException mie) {
623
            fail("Metacat Inaccessible:\n" + mie.getMessage());
624
        }
625
        catch (Exception e) {
626
            fail("General exception:\n" + e.getMessage());
627
        }
628
    }
629 4356 daigle
630
    /**
631 5744 berkley
     * Test inserting and reading an EML 2.1.0 document with the micro sign (µ).
632 4356 daigle
     * Read should succeed since the same document should be read back from disk
633
     * that was submitted.  Query should succeed because we look for the same
634 5744 berkley
     * character (µ).
635 4356 daigle
     */
636
    public void nonLatinUnicodeCharacter210Test() {
637
    	debug("\nRunning: nonLatinUnicodeCharacter210Test");
638
        try {
639
            String newdocid = generateDocid();
640
            m.login(username, password);
641
642 5752 leinfelder
            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
643 4356 daigle
644
            testdocument = getTestEmlDoc(testTitle, EML2_1_0);
645
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
646 2448 sgarg
647 4356 daigle
            // this tests reading the document back from disk
648
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
649
650
            // this tests searching for the document in the database
651
            Thread.sleep(3000);
652
            queryDocWhichHasTitle(newdocid  + ".1", testTitle, EML2_1_0, SUCCESS);
653
654 4407 daigle
            deleteDocid(newdocid  + ".1", SUCCESS, false);
655
656 4356 daigle
            m.logout();
657
        }
658
        catch (MetacatAuthException mae) {
659
            fail("Authorization failed:\n" + mae.getMessage());
660
        }
661
        catch (MetacatInaccessibleException mie) {
662
            fail("Metacat Inaccessible:\n" + mie.getMessage());
663
        }
664
        catch (Exception e) {
665
            fail("General exception:\n" + e.getMessage());
666
        }
667
    }
668 5752 leinfelder
669
    /**
670
     * Test inserting and reading an EML 2.1.0 with Chinese
671
     */
672
    public void unicodeCharacterTest() {
673
    	debug("\nRunning: unicodeCharacterTest");
674
        try {
675
676
        	String filePath = "test/clienttestfiles/unicodeEML.xml";
677
            String testTitle = "測試中的數據包 (Test Chinese data package) _DOCID_";
678
            String newdocid = generateDocid() + ".1";
679
            testdocument = FileUtil.readFileToString(filePath, "UTF-8");
680
681
            // include the docid
682
            testdocument = testdocument.replaceAll("_DOCID_", newdocid);
683
            testTitle = testTitle.replaceAll("_DOCID_", newdocid);
684
685
            // login
686
            m.login(username, password);
687
688
            // insert
689
            insertDocid(newdocid, testdocument, SUCCESS, false);
690 4356 daigle
691 5752 leinfelder
            // this tests reading the document back from disk
692
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
693
694
            // this tests searching for the document in the database
695
            Thread.sleep(3000);
696
            queryDocWhichHasTitle(testTitle, testTitle, EML2_1_0, SUCCESS);
697
698
            // clean up
699
            //deleteDocid(newdocid, SUCCESS, false);
700
701
            m.logout();
702
        }
703
        catch (MetacatAuthException mae) {
704
            fail("Authorization failed:\n" + mae.getMessage());
705
        }
706
        catch (MetacatInaccessibleException mie) {
707
            fail("Metacat Inaccessible:\n" + mie.getMessage());
708
        }
709
        catch (Exception e) {
710
            fail("General exception:\n" + e.getMessage());
711
        }
712
    }
713
714 2440 sgarg
    /**
715
     * Insert a document into metacat. The expected result is passed as result
716
     */
717
    private String insertDocid(String docid, String docText, boolean result,
718
                               boolean expectMetacatException) {
719
        String response = null;
720
        try {
721 5752 leinfelder
722
        	debug("doctext: " + docText);
723
724 2440 sgarg
            response = m.insert(docid,
725
                                new StringReader(docText), null);
726
            System.err.println(response);
727
            if (result) {
728
                assertTrue( (response.indexOf("<success>") != -1));
729
                assertTrue(response.indexOf(docid) != -1);
730
            }
731
            else {
732
                assertTrue( (response.indexOf("<success>") == -1));
733
            }
734
        }
735
        catch (MetacatInaccessibleException mie) {
736
            fail("Metacat Inaccessible:\n" + mie.getMessage());
737
        }
738
        catch (InsufficientKarmaException ike) {
739
                fail("Insufficient karma:\n" + ike.getMessage());
740
        }
741
        catch (MetacatException me) {
742
            if (!expectMetacatException) {
743
                fail("Metacat Error:\n" + me.getMessage());
744
            }
745
        }
746
        catch (Exception e) {
747
            fail("General exception:\n" + e.getMessage());
748
        }
749
        return response;
750
    }
751
752
    /**
753
     * Insert a document into metacat. The expected result is passed as result
754
     */
755
    private String uploadDocid(String docid, String filePath, boolean result,
756
                               boolean expectedKarmaException) {
757
        String response = null;
758
        try {
759
            response = m.upload(docid, new File(filePath));
760
            if (result) {
761
                assertTrue( (response.indexOf("<success>") != -1));
762
                assertTrue(response.indexOf(docid) != -1);
763
            }
764
            else {
765
                assertTrue( (response.indexOf("<success>") == -1));
766
            }
767
            System.err.println("respose from metacat: " + response);
768
        }
769
        catch (MetacatInaccessibleException mie) {
770
            fail("Metacat Inaccessible:\n" + mie.getMessage());
771
        }
772
        catch (InsufficientKarmaException ike) {
773
            if (!expectedKarmaException) {
774
                fail("Insufficient karma:\n" + ike.getMessage());
775
            }
776
        }
777
        catch (MetacatException me) {
778
            if (result) {
779
                fail("Metacat Error:\n" + me.getMessage());
780
            } else {
781
                System.err.println("Metacat Error:\n" + me.getMessage());
782
            }
783
        }
784
        catch (Exception e) {
785
            fail("General exception:\n" + e.getMessage());
786
        }
787
        return response;
788
    }
789
790
    /**
791
     * Update a document in metacat. The expected result is passed as result
792
     */
793
    private String updateDocid(String docid, String docText, boolean result,
794
                               boolean expectedKarmaFailure) {
795
        String response = null;
796
        try {
797
            response = m.update(docid,
798
                                new StringReader(testdocument), null);
799
800
            if (result) {
801
                assertTrue( (response.indexOf("<success>") != -1));
802
                assertTrue(response.indexOf(docid) != -1);
803
            }
804
            else {
805
                assertTrue( (response.indexOf("<success>") == -1));
806
            }
807
            System.err.println(response);
808
        }
809
        catch (MetacatInaccessibleException mie) {
810
            fail("Metacat Inaccessible:\n" + mie.getMessage());
811
        }
812
        catch (InsufficientKarmaException ike) {
813
            if (!expectedKarmaFailure) {
814
                fail("Insufficient karma:\n" + ike.getMessage());
815
            }
816
        }
817
        catch (MetacatException me) {
818
            if (result) {
819
                fail("Metacat Error:\n" + me.getMessage());
820
            } else {
821
                System.err.println("Metacat Error:\n" + me.getMessage());
822
            }
823
        }
824
        catch (Exception e) {
825
            fail("General exception:\n" + e.getMessage());
826
        }
827
828
        return response;
829
    }
830
831
    /**
832 4356 daigle
     * Delete a document from metacat. The expected result is passed as result
833 2440 sgarg
     */
834
    private void deleteDocid(String docid, boolean result,
835
                             boolean expextedKarmaFailure) {
836
        try {
837
            String response = m.delete(docid);
838
            if (result) {
839
                assertTrue(response.indexOf("<success>") != -1);
840
            }
841
            else {
842
                assertTrue(response.indexOf("<success>") == -1);
843
            }
844
            System.err.println(response);
845
        }
846
        catch (MetacatInaccessibleException mie) {
847
            fail("Metacat Inaccessible:\n" + mie.getMessage());
848
        }
849
        catch (InsufficientKarmaException ike) {
850
            if(!expextedKarmaFailure){
851
                fail("Insufficient karma:\n" + ike.getMessage());
852
            }
853
        }
854
        catch (MetacatException me) {
855
            if (result) {
856
                fail("Metacat Error:\n" + me.getMessage());
857
            } else {
858
                System.err.println("Metacat Error:\n" + me.getMessage());
859
            }
860
        }
861
        catch (Exception e) {
862
            fail("General exception:\n" + e.getMessage());
863
        }
864
    }
865
866
    /**
867
     * Read a document from metacat. The expected result is passed as result
868
     */
869
    private void readDocid(String docid, boolean result,
870
                           boolean expextedKarmaFailure) {
871
        try {
872 5110 daigle
            Reader r = new InputStreamReader(m.read(docid));
873 2440 sgarg
            String response = IOUtil.getAsString(r, true);
874
875
            if (!result) {
876
                assertTrue(response.indexOf("<success>") == -1);
877
            }
878
            // System.err.println(response);
879
        }
880
        catch (MetacatInaccessibleException mie) {
881
            fail("Metacat Inaccessible:\n" + mie.getMessage());
882
        }
883
        catch (InsufficientKarmaException ike) {
884
            if (!expextedKarmaFailure) {
885
                fail("Insufficient karma:\n" + ike.getMessage());
886
            }
887
        }
888
        catch (MetacatException me) {
889
            fail("Metacat Error:\n" + me.getMessage());
890
        }
891
        catch (Exception e) {
892
            fail("General exception:\n" + e.getMessage());
893
        }
894
    }
895
896
    /**
897
     * Read a document from metacat and check if it is equal to a given string.
898
     * The expected result is passed as result
899
     */
900
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
901
                                         boolean result,
902 4356 daigle
                                         boolean expectedKarmaFailure) {
903 2440 sgarg
        try {
904 5750 berkley
            Reader r = new InputStreamReader(m.read(docid), "UTF-8");
905
            //InputStream is = m.read(docid);
906 2440 sgarg
            String doc = IOUtil.getAsString(r, true);
907 5750 berkley
            //String doc = IOUtils.toString(is);
908
909 2440 sgarg
            if (result) {
910
911
                if (!testDoc.equals(doc)) {
912 4360 daigle
                    debug("doc    :" + doc);
913
                    debug("testDoc:" + testDoc);
914 2440 sgarg
                }
915
916
                assertTrue(testDoc.equals(doc));
917
            }
918
            else {
919
                assertTrue(doc.indexOf("<error>") != -1);
920
            }
921
        }
922
        catch (MetacatInaccessibleException mie) {
923
            fail("Metacat Inaccessible:\n" + mie.getMessage());
924
        }
925
        catch (InsufficientKarmaException ike) {
926 4356 daigle
            if (!expectedKarmaFailure) {
927 2440 sgarg
                fail("Insufficient karma:\n" + ike.getMessage());
928
            }
929
        }
930
        catch (MetacatException me) {
931
            fail("Metacat Error:\n" + me.getMessage());
932
        }
933
        catch (Exception e) {
934
            fail("General exception:\n" + e.getMessage());
935
        }
936
937
    }
938 4356 daigle
939
    /**
940 5752 leinfelder
     * Query a document by looking for a part of the title in the title element.
941 4356 daigle
     * Then check if the testTitle exists in the doc.
942 5752 leinfelder
     * @param titlePart the part of the title of the doc to look for
943 4356 daigle
     * @param testTitle the title containing special characters
944
     * @param result are we expecting SUCCESS or FAILURE
945
     * @param expextedKarmaFailure
946
     */
947 5752 leinfelder
    private void queryDocWhichHasTitle(String titlePart, String testTitle,
948 4356 daigle
                                         String emlVersion, boolean result) {
949
        try {
950 5752 leinfelder
            String sQuery = getTestEmlQuery(titlePart, emlVersion);
951 4356 daigle
        	Reader queryReader = new StringReader(sQuery);
952
            Reader resultReader = m.query(queryReader);
953
            String queryResult = IOUtil.getAsString(resultReader, true);
954
            if (result) {
955
                if (!queryResult.contains(testTitle)) {
956 4360 daigle
                    debug("queryResult: " + queryResult);
957
                    debug("does not contain title: " + testTitle);
958 4356 daigle
                }
959 2440 sgarg
960 4356 daigle
                assertTrue(queryResult.contains(testTitle));
961
            }
962
            else {
963
                assertTrue(queryResult.indexOf("<error>") != -1);
964
            }
965
        }
966
        catch (MetacatInaccessibleException mie) {
967
            fail("Metacat Inaccessible:\n" + mie.getMessage());
968
        }
969
        catch (Exception e) {
970
            fail("General exception:\n" + e.getMessage());
971
        }
972
973
    }
974
975 2440 sgarg
    /**
976
     * Create a hopefully unique docid for testing insert and update. Does
977
     * not include the 'revision' part of the id.
978
     *
979
     * @return a String docid based on the current date and time
980
     */
981
    private String generateDocid() {
982
        StringBuffer docid = new StringBuffer(prefix);
983
        docid.append(".");
984
985
        // Create a calendar to get the date formatted properly
986
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
987
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
988
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
989
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
990
                       2 * 60 * 60 * 1000);
991
        Calendar calendar = new GregorianCalendar(pdt);
992
        Date trialTime = new Date();
993
        calendar.setTime(trialTime);
994
        docid.append(calendar.get(Calendar.YEAR));
995
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
996
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
997
        docid.append(calendar.get(Calendar.MINUTE));
998
        docid.append(calendar.get(Calendar.SECOND));
999 3584 tao
   	    //sometimes this number is not unique, so we append a random number
1000
    	int random = (new Double(Math.random()*100)).intValue();
1001
    	docid.append(random);
1002 2440 sgarg
        return docid.toString();
1003
    }
1004
}