Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: leinfelder $'
8
 *     '$Date: 2014-07-23 16:19:48 -0700 (Wed, 23 Jul 2014) $'
9
 * '$Revision: 8810 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacattest;
27

    
28
import java.io.InputStream;
29
import java.math.BigInteger;
30

    
31
import junit.framework.Test;
32
import junit.framework.TestSuite;
33

    
34
import org.apache.commons.io.IOUtils;
35
import org.dataone.service.exceptions.ServiceFailure;
36
import org.dataone.service.types.v1.Identifier;
37
import org.dataone.service.types.v1.ObjectFormatIdentifier;
38
import org.dataone.service.types.v1.Session;
39
import org.dataone.service.types.v2.SystemMetadata;
40

    
41
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
42
import edu.ucsb.nceas.metacat.dataone.MNodeService;
43

    
44
/**
45
 * A JUnit test for testing Dryad documents
46
 */
47
public class DryadTest
48
    extends D1NodeServiceTest {
49
    
50
    private static final String DRYAD_TEST_DOC = "test/dryad-metadata-profile-sample.xml";
51
    
52
    private static final String DRYAD_TEST_DOC_INVALID = "test/dryad-metadata-profile-invalid.xml";
53

    
54
    /**
55
     * Constructor to build the test
56
     *
57
     * @param name the name of the test method
58
     */
59
    public DryadTest(String name) {
60
        super(name);
61
    }
62

    
63
    /**
64
     * Create a suite of tests to be run together
65
     */
66
    public static Test suite() {
67
        TestSuite suite = new TestSuite();
68
        suite.addTest(new DryadTest("initialize"));
69
        // Test basic functions
70
        suite.addTest(new DryadTest("d1InsertDoc"));
71
        suite.addTest(new DryadTest("d1InsertInvalidDoc"));
72

    
73
        return suite;
74
    }
75

    
76
    /**
77
     * Run an initial test that always passes to check that the test
78
     * harness is working.
79
     */
80
    public void initialize() {
81
        assertTrue(1 == 1);
82
    }
83
    
84
    /**
85
     * Insert test doc using D1 API
86
     */
87
    public void d1InsertDoc() {
88
		try {
89
	    	String docid = this.generateDocumentId();
90
			String documentContents = this.getTestDocFromFile(DRYAD_TEST_DOC);
91
			Session session = getTestSession();
92
			Identifier pid = new Identifier();
93
			pid.setValue(docid);
94
			SystemMetadata sysmeta = this.createSystemMetadata(pid, session.getSubject(), IOUtils.toInputStream(documentContents, "UTF-8"));
95
			ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
96
			formatId.setValue("http://datadryad.org/profile/v3.1");
97
			sysmeta.setFormatId(formatId);
98
			sysmeta.setSize(BigInteger.valueOf(IOUtils.toByteArray(documentContents).length));
99
			MNodeService.getInstance(request).create(session, pid, IOUtils.toInputStream(documentContents, "UTF-8"), sysmeta);
100
			InputStream results = MNodeService.getInstance(request).get(session, pid);
101
			String resultString = IOUtils.toString(results);
102
			assertEquals(documentContents, resultString);
103
		} catch (Exception e) {
104
			e.printStackTrace();
105
			fail(e.getMessage());
106
		}
107
    	
108
    }
109
    
110
    /**
111
     * Insert test doc using D1 API
112
     */
113
    public void d1InsertInvalidDoc() {
114
		try {
115
	    	String docid = this.generateDocumentId();
116
			String documentContents = this.getTestDocFromFile(DRYAD_TEST_DOC_INVALID);
117
			Session session = getTestSession();
118
			Identifier pid = new Identifier();
119
			pid.setValue(docid);
120
			SystemMetadata sysmeta = this.createSystemMetadata(pid, session.getSubject(), IOUtils.toInputStream(documentContents, "UTF-8"));
121
			ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
122
			formatId.setValue("http://datadryad.org/profile/v3.1");
123
			sysmeta.setFormatId(formatId);
124
			sysmeta.setSize(BigInteger.valueOf(IOUtils.toByteArray(documentContents).length));
125
			try {
126
				MNodeService.getInstance(request).create(session, pid, IOUtils.toInputStream(documentContents, "UTF-8"), sysmeta);
127
			} catch (Exception expectedException) {
128

    
129
				// expected exception
130
				assertTrue(expectedException instanceof ServiceFailure);
131
				return;
132
			}
133
			fail("Should not allow inserting invalid Dryad content");
134
		} catch (Exception e) {
135
			e.printStackTrace();
136
			fail(e.getMessage());
137
		}
138
    	
139
    }
140
    
141
}
(5-5/30)