Project

General

Profile

« Previous | Next » 

Revision 9966

Added by Jing Tao over 8 years ago

Add more test cases.

View differences:

test/edu/ucsb/nceas/metacattest/MetacatValidationAlgorithmTest.java
23 23
 */
24 24
package edu.ucsb.nceas.metacattest;
25 25

  
26
import java.io.ByteArrayInputStream;
26 27
import java.io.File;
28
import java.io.InputStream;
27 29
import java.io.StringReader;
28 30

  
29 31
import junit.framework.Test;
......
31 33

  
32 34
import org.xml.sax.*;
33 35

  
36
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
34 37
import edu.ucsb.nceas.MCTestCase;
35 38
import edu.ucsb.nceas.metacat.AccessionNumber;
36 39
import edu.ucsb.nceas.metacat.AccessionNumberException;
......
39 42
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
40 43
import edu.ucsb.nceas.metacat.client.MetacatException;
41 44
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
45
import edu.ucsb.nceas.metacat.dataone.CNodeService;
46
import edu.ucsb.nceas.metacat.dataone.MNodeService;
42 47

  
43 48
import org.apache.commons.io.FileUtils;
49
import org.dataone.service.types.v1.Identifier;
50
import org.dataone.service.types.v1.ObjectFormatIdentifier;
51
import org.dataone.service.types.v1.Session;
52
import org.dataone.service.types.v2.SystemMetadata;
44 53

  
45 54

  
46
public class MetacatValidationAlgorithmTest extends MCTestCase {
55
/**
56
 * This test tests some scenarios during Metacat inserting:
57
 * dtd, schema with namespace, schema without namespace and no-dtd/schema.
58
 * @author tao
59
 *
60
 */
61
public class MetacatValidationAlgorithmTest extends D1NodeServiceTest {
47 62
    
63
    private static final String UNREGISTERED_SCHEMA_XML_INSTANCE="<?xml version=\"1.0\"?> "+
64
           "<note:note xmlns:note=\"http://www.w3schools.com\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.w3schools.com https://www.loc.gov/ead/ead.xsd\"> "+
65
           "<to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note:note>";
66
    
67
    private static final String UNREGISTERED_NONAMESPACE_SCHEMA_XML_INSTANCE = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> "+                                                                                                                                                                                                                                                                         
68
                              "<metadata xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.fgdc.gov/metadata/foo.xsd\">"+
69
                               "<idinfo><citation><citeinfo><origin>National Center for Earth Surface Dynamics</origin><pubdate>20160811</pubdate><title>Shuttle Radar Topography Mission (SRTM)</title>"+
70
                               "<onlink>http://doi.org/10.5967/M09W0CGK</onlink></citeinfo></citation></idinfo></metadata>";
48 71
    public MetacatValidationAlgorithmTest (String name) {
49 72
        super(name);
50 73
    }
......
59 82
        suite.addTest(new MetacatValidationAlgorithmTest("testRegisteredDTDInvalidDucoment"));
60 83
        suite.addTest(new MetacatValidationAlgorithmTest("testUnRegisteredDTDDucoment"));
61 84
        suite.addTest(new MetacatValidationAlgorithmTest("testValidationUnneededDucoment"));
85
        suite.addTest(new MetacatValidationAlgorithmTest("testUnregisteredNamespaceSchema"));
86
        suite.addTest(new MetacatValidationAlgorithmTest("testEML201"));
87
        suite.addTest(new MetacatValidationAlgorithmTest("testEML211"));
88
        suite.addTest(new MetacatValidationAlgorithmTest("testRegisteredNamespaceOrFormatIdSchema"));
89
        suite.addTest(new MetacatValidationAlgorithmTest("testUnregisteredNoNamespaceSchema"));
90
        suite.addTest(new MetacatValidationAlgorithmTest("testRegisteredNoNamespaceSchema"));
62 91
        return suite;
63 92
    }
64 93
    /**
......
154 183
        }
155 184
    }
156 185
    
186
    /**
187
     * Insert a test documents which uses an unregistered namespace
188
     */
189
    public void testUnregisteredNamespaceSchema() throws Exception {
190
        ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
191
        formatId.setValue("http://www.cuahsi.org/waterML/1.1/");
192
        //Metacat API
193
        String docid = generateDocumentId() + ".1";
194
        try {
195
            m.login(username, password);
196
            String response = m.insert(docid, new StringReader(UNREGISTERED_SCHEMA_XML_INSTANCE), null);
197
            fail("We shouldn't get there since the above statement should throw an exception.");
198
        } catch (MetacatException e) {
199
            assertTrue(e.getMessage().contains("<error>"));
200
            assertTrue(e.getMessage().contains("http://www.w3schools.com"));
201
            assertTrue(e.getMessage().contains("not registered"));
202
        } 
203
        Thread.sleep(200);
204
        //DaaONEAPI - MN.create
205
        try {
206
            Session session = getTestSession();
207
            Identifier guid = new Identifier();
208
            guid.setValue("testCreate." + System.currentTimeMillis());
209
            InputStream object = new ByteArrayInputStream(UNREGISTERED_SCHEMA_XML_INSTANCE.getBytes("UTF-8"));
210
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
211
            sysmeta.setFormatId(formatId);
212
            Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
213
        } catch (Exception e) {
214
            assertTrue(e.getMessage().contains("<error>"));
215
            assertTrue(e.getMessage().contains("http://www.w3schools.com"));
216
            assertTrue(e.getMessage().contains("not registered"));
217
        } 
218
        
219
        //DaaONEAPI - CN.create
220
        Thread.sleep(200);
221
        try {
222
            Session session = getCNSession();
223
            Identifier guid = new Identifier();
224
            guid.setValue("testCreate." + System.currentTimeMillis());
225
            InputStream object = new ByteArrayInputStream(UNREGISTERED_SCHEMA_XML_INSTANCE.getBytes("UTF-8"));
226
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
227
            sysmeta.setFormatId(formatId);
228
            Identifier pid = CNodeService.getInstance(request).create(session, guid, object, sysmeta);
229
        } catch(Exception e) {
230
            assertTrue(e.getMessage().contains("<error>"));
231
            assertTrue(e.getMessage().contains("http://www.w3schools.com"));
232
            assertTrue(e.getMessage().contains("not registered"));
233
        }
234
    }
235
    
236
    
237
    /**
238
     * Test to insert an eml 201 document
239
     * @throws Exception
240
     */
241
    public void testEML201 () throws Exception{
242
        String title = "it is a test";
243
        String emlVersion = EML2_0_1;
244
        String eml200 = getTestEmlDoc(title, emlVersion);
245
        //System.out.println("the eml document is \n"+eml200);
246
        ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
247
        formatId.setValue("eml://ecoinformatics.org/eml-2.0.1");
248
        //Metacat API
249
        String docid = generateDocumentId() + ".1";
250
        try {
251
            m.login(username, password);
252
            String response = m.insert(docid, new StringReader(eml200), null);
253
            assertTrue(response.contains(docid));
254
        } catch (MetacatException e) {
255
           fail("The test failed since "+e.getMessage());
256
        } 
257
        Thread.sleep(200);
258
        //DaaONEAPI - MN.create
259
        try {
260
            Session session = getTestSession();
261
            Identifier guid = new Identifier();
262
            guid.setValue("testCreate." + System.currentTimeMillis());
263
            InputStream object = new ByteArrayInputStream(eml200.getBytes("UTF-8"));
264
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
265
            sysmeta.setFormatId(formatId);
266
            Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
267
            assertTrue(pid.getValue().equals(guid.getValue()));
268
        } catch (Exception e) {
269
            fail("The test failed since "+e.getMessage());
270
        } 
271
        
272
        //DaaONEAPI - CN.create
273
        Thread.sleep(200);
274
        try {
275
            Session session = getCNSession();
276
            Identifier guid = new Identifier();
277
            guid.setValue("testCreate." + System.currentTimeMillis());
278
            InputStream object = new ByteArrayInputStream(eml200.getBytes("UTF-8"));
279
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
280
            sysmeta.setFormatId(formatId);
281
            Identifier pid = CNodeService.getInstance(request).create(session, guid, object, sysmeta);
282
            assertTrue(pid.getValue().equals(guid.getValue()));
283
        } catch(Exception e) {
284
            fail("The test failed since "+e.getMessage());
285
        }
286
        
287
        //insert a invalid eml 201
288
        String invalidEml2 = eml200.replaceAll("access", "access1");
289
        //System.out.println(""+invalidEml2);
290
        docid = generateDocumentId() + ".1";
291
        try {
292
            m.login(username, password);
293
            String response = m.insert(docid, new StringReader(invalidEml2), null);
294
            fail("We can't get here since the document is invalid.");
295
        } catch (MetacatException e) {
296
            assertTrue(e.getMessage().contains("<error>"));
297
            assertTrue(e.getMessage().contains("access1"));
298
        } 
299
    }
300
    
301
    /**
302
     * Test to insert an eml 211 document
303
     * @throws Exception
304
     */
305
    public void testEML211 () throws Exception{
306
        String title = "it is a test";
307
        String emlVersion = EML2_1_1;
308
        String eml211 = getTestEmlDoc(title, emlVersion);
309
        System.out.println("the eml document is \n"+eml211);
310
        ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
311
        formatId.setValue("eml://ecoinformatics.org/eml-2.1.1");
312
        //Metacat API
313
        String docid = generateDocumentId() + ".1";
314
        try {
315
            m.login(username, password);
316
            String response = m.insert(docid, new StringReader(eml211), null);
317
            assertTrue(response.contains(docid));
318
        } catch (MetacatException e) {
319
           fail("The test failed since "+e.getMessage());
320
        } 
321
        Thread.sleep(200);
322
        //DaaONEAPI - MN.create
323
        try {
324
            Session session = getTestSession();
325
            Identifier guid = new Identifier();
326
            guid.setValue("testCreate." + System.currentTimeMillis());
327
            InputStream object = new ByteArrayInputStream(eml211.getBytes("UTF-8"));
328
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
329
            sysmeta.setFormatId(formatId);
330
            Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
331
            assertTrue(pid.getValue().equals(guid.getValue()));
332
        } catch (Exception e) {
333
            fail("The test failed since "+e.getMessage());
334
        } 
335
        
336
        //DaaONEAPI - CN.create
337
        Thread.sleep(200);
338
        try {
339
            Session session = getCNSession();
340
            Identifier guid = new Identifier();
341
            guid.setValue("testCreate." + System.currentTimeMillis());
342
            InputStream object = new ByteArrayInputStream(eml211.getBytes("UTF-8"));
343
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
344
            sysmeta.setFormatId(formatId);
345
            Identifier pid = CNodeService.getInstance(request).create(session, guid, object, sysmeta);
346
            assertTrue(pid.getValue().equals(guid.getValue()));
347
        } catch(Exception e) {
348
            fail("The test failed since "+e.getMessage());
349
        }
350
        
351
        //insert a invalid eml 201
352
        String invalidEml2 = eml211.replaceAll("access", "access1");
353
        //System.out.println(""+invalidEml2);
354
        docid = generateDocumentId() + ".1";
355
        try {
356
            m.login(username, password);
357
            String response = m.insert(docid, new StringReader(invalidEml2), null);
358
            fail("We can't get here since the document is invalid.");
359
        } catch (MetacatException e) {
360
            assertTrue(e.getMessage().contains("<error>"));
361
            assertTrue(e.getMessage().contains("access1"));
362
        } 
363
    }
364
    
365
    /**
366
     * Test to insert a non-eml document but its namespace is registered.
367
     * @throws Exception
368
     */
369
    public void testRegisteredNamespaceOrFormatIdSchema() throws Exception {
370
        String xml = FileUtils.readFileToString(new File("./test/dryad-metadata-profile-sample.xml"), "UTF-8");
371
        ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
372
        formatId.setValue("http://datadryad.org/profile/v3.1");
373
        //Metacat API
374
        String docid = generateDocumentId() + ".1";
375
        try {
376
            m.login(username, password);
377
            String response = m.insert(docid, new StringReader(xml), null);
378
            assertTrue(response.contains(docid));
379
        } catch (MetacatException e) {
380
           fail("The test failed since "+e.getMessage());
381
        } 
382
        Thread.sleep(200);
383
        //DaaONEAPI - MN.create
384
        try {
385
            Session session = getTestSession();
386
            Identifier guid = new Identifier();
387
            guid.setValue("testCreate." + System.currentTimeMillis());
388
            InputStream object = new ByteArrayInputStream(xml.getBytes("UTF-8"));
389
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
390
            sysmeta.setFormatId(formatId);
391
            Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
392
            assertTrue(pid.getValue().equals(guid.getValue()));
393
        } catch (Exception e) {
394
            fail("The test failed since "+e.getMessage());
395
        } 
396
        
397
        //DaaONEAPI - CN.create
398
        Thread.sleep(200);
399
        try {
400
            Session session = getCNSession();
401
            Identifier guid = new Identifier();
402
            guid.setValue("testCreate." + System.currentTimeMillis());
403
            InputStream object = new ByteArrayInputStream(xml.getBytes("UTF-8"));
404
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
405
            sysmeta.setFormatId(formatId);
406
            Identifier pid = CNodeService.getInstance(request).create(session, guid, object, sysmeta);
407
            assertTrue(pid.getValue().equals(guid.getValue()));
408
        } catch(Exception e) {
409
            fail("The test failed since "+e.getMessage());
410
        }
411
        
412
        //insert a invalid dryad
413
        String invalidXml = FileUtils.readFileToString(new File("./test/dryad-metadata-profile-invalid.xml"), "UTF-8");
414
        //System.out.println(""+invalidEml2);
415
        docid = generateDocumentId() + ".1";
416
        try {
417
            m.login(username, password);
418
            String response = m.insert(docid, new StringReader(invalidXml), null);
419
            fail("We can't get here since the document is invalid.");
420
        } catch (MetacatException e) {
421
            assertTrue(e.getMessage().contains("bad"));
422
            assertTrue(e.getMessage().contains("<error>"));
423
        } 
424
        
425
    }
426
    
427
    public void testUnregisteredNoNamespaceSchema() throws Exception {
428
        ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
429
        formatId.setValue("www.fgdc.gov/foo/");
430
        String xml = UNREGISTERED_NONAMESPACE_SCHEMA_XML_INSTANCE;
431
        //Metacat API
432
        String docid = generateDocumentId() + ".1";
433
        try {
434
            m.login(username, password);
435
            String response = m.insert(docid, new StringReader(xml), null);
436
            fail("We shouldn't get there since the above statement should throw an exception.");
437
        } catch (MetacatException e) {
438
            assertTrue(e.getMessage().contains("<error>"));
439
            assertTrue(e.getMessage().contains("http://www.fgdc.gov/metadata/foo.xsd"));
440
            assertTrue(e.getMessage().contains("not registered"));
441
        } 
442
        Thread.sleep(200);
443
        //DaaONEAPI - MN.create
444
        try {
445
            Session session = getTestSession();
446
            Identifier guid = new Identifier();
447
            guid.setValue("testCreate." + System.currentTimeMillis());
448
            InputStream object = new ByteArrayInputStream(UNREGISTERED_NONAMESPACE_SCHEMA_XML_INSTANCE.getBytes("UTF-8"));
449
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
450
            sysmeta.setFormatId(formatId);
451
            Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
452
        } catch (Exception e) {
453
            assertTrue(e.getMessage().contains("<error>"));
454
            assertTrue(e.getMessage().contains("www.fgdc.gov/foo/"));
455
            assertTrue(e.getMessage().contains("http://www.fgdc.gov/metadata/foo.xsd"));
456
            assertTrue(e.getMessage().contains("not registered"));
457
        } 
458
        
459
        //DaaONEAPI - CN.create
460
        Thread.sleep(200);
461
        try {
462
            Session session = getCNSession();
463
            Identifier guid = new Identifier();
464
            guid.setValue("testCreate." + System.currentTimeMillis());
465
            InputStream object = new ByteArrayInputStream(UNREGISTERED_NONAMESPACE_SCHEMA_XML_INSTANCE.getBytes("UTF-8"));
466
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
467
            sysmeta.setFormatId(formatId);
468
            Identifier pid = CNodeService.getInstance(request).create(session, guid, object, sysmeta);
469
        } catch(Exception e) {
470
            assertTrue(e.getMessage().contains("<error>"));
471
            assertTrue(e.getMessage().contains("www.fgdc.gov/foo/"));
472
            assertTrue(e.getMessage().contains("http://www.fgdc.gov/metadata/foo.xsd"));
473
            assertTrue(e.getMessage().contains("not registered"));
474
        }
475
    }
476
    
477
    public void testRegisteredNoNamespaceSchema() throws Exception {
478
        String xml = FileUtils.readFileToString(new File("./test/fgdc.xml"), "UTF-8");
479
        ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
480
        formatId.setValue("FGDC-STD-001-1998");
481
        //Metacat API
482
        String docid = generateDocumentId() + ".1";
483
        try {
484
            m.login(username, password);
485
            String response = m.insert(docid, new StringReader(xml), null);
486
            assertTrue(response.contains(docid));
487
        } catch (MetacatException e) {
488
           fail("The test failed since "+e.getMessage());
489
        } 
490
        Thread.sleep(200);
491
        //DaaONEAPI - MN.create
492
        try {
493
            Session session = getTestSession();
494
            Identifier guid = new Identifier();
495
            guid.setValue("testCreate." + System.currentTimeMillis());
496
            InputStream object = new ByteArrayInputStream(xml.getBytes("UTF-8"));
497
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
498
            sysmeta.setFormatId(formatId);
499
            Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
500
            assertTrue(pid.getValue().equals(guid.getValue()));
501
        } catch (Exception e) {
502
            fail("The test failed since "+e.getMessage());
503
        } 
504
        
505
        //DaaONEAPI - CN.create
506
        Thread.sleep(200);
507
        try {
508
            Session session = getCNSession();
509
            Identifier guid = new Identifier();
510
            guid.setValue("testCreate." + System.currentTimeMillis());
511
            InputStream object = new ByteArrayInputStream(xml.getBytes("UTF-8"));
512
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
513
            sysmeta.setFormatId(formatId);
514
            Identifier pid = CNodeService.getInstance(request).create(session, guid, object, sysmeta);
515
            assertTrue(pid.getValue().equals(guid.getValue()));
516
        } catch(Exception e) {
517
            fail("The test failed since "+e.getMessage());
518
        }
519
        
520
        //insert a invalid fgdc
521
        String invalidXml = xml.replace("metadata", "metadata1");
522
        //System.out.println(""+invalidEml2);
523
        docid = generateDocumentId() + ".1";
524
        try {
525
            m.login(username, password);
526
            String response = m.insert(docid, new StringReader(invalidXml), null);
527
            fail("We can't get here since the document is invalid.");
528
        } catch (MetacatException e) {
529
            assertTrue(e.getMessage().contains("metadata1"));
530
            assertTrue(e.getMessage().contains("<error>"));
531
        } 
532
    }
157 533

  
158 534
}

Also available in: Unified diff