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-02-20 10:35:08 -0800 (Thu, 20 Feb 2014) $'
9
 * '$Revision: 8622 $'
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.io.StringReader;
30
import java.math.BigInteger;
31

    
32
import org.apache.commons.io.IOUtils;
33
import org.dataone.service.types.v1.Identifier;
34
import org.dataone.service.types.v1.ObjectFormatIdentifier;
35
import org.dataone.service.types.v1.Session;
36
import org.dataone.service.types.v1.SystemMetadata;
37

    
38
import junit.framework.Test;
39
import junit.framework.TestSuite;
40
import edu.ucsb.nceas.metacat.client.MetacatFactory;
41
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
42
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
43
import edu.ucsb.nceas.metacat.dataone.MNodeService;
44

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

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

    
62
    /**
63
     * Establish a testing framework by initializing appropriate objects
64
     */
65
    public void setUp() {
66
        try {
67
            System.err.println("Test Metacat: " + metacatUrl);
68
            m = MetacatFactory.createMetacatConnection(metacatUrl);
69
        }
70
        catch (MetacatInaccessibleException mie) {
71
            System.err.println("Metacat is: " + metacatUrl);
72
            fail("Metacat connection failed." + mie.getMessage());
73
        }
74
    }
75

    
76
    /**
77
     * Release any objects after tests are complete
78
     */
79
    public void tearDown() {
80
    }
81

    
82
    /**
83
     * Create a suite of tests to be run together
84
     */
85
    public static Test suite() {
86
        TestSuite suite = new TestSuite();
87
        suite.addTest(new DryadTest("initialize"));
88
        // Test basic functions
89
        //suite.addTest(new DryadTest("insertDoc"));
90
        suite.addTest(new DryadTest("d1InsertDoc"));
91

    
92
        return suite;
93
    }
94

    
95
    /**
96
     * Run an initial test that always passes to check that the test
97
     * harness is working.
98
     */
99
    public void initialize() {
100
        assertTrue(1 == 1);
101
    }
102
    
103
    /**
104
     * Test insert of Dryad document via Metacat
105
     * Note: formatId will be generic
106
     */
107
    public void insertDoc() {
108
		try {
109
			m.login(username, password);
110
	    	String docid = this.generateDocumentId();
111
	    	docid += ".1";
112
			String documentContents = this.getTestDocFromFile(DRYAD_TEST_DOC);
113
			m.insert(docid, new StringReader(documentContents), null);
114
			InputStream results = m.read(docid);
115
			String resultString = IOUtils.toString(results);
116
			assertEquals(documentContents, resultString);
117
			m.logout();
118
		} catch (Exception e) {
119
			e.printStackTrace();
120
			fail(e.getMessage());
121
		}
122
    	
123
    }
124
    
125
    /**
126
     * Insert test doc using D1 API
127
     */
128
    public void d1InsertDoc() {
129
		try {
130
	    	String docid = this.generateDocumentId();
131
	    	docid += ".1";
132
			String documentContents = this.getTestDocFromFile(DRYAD_TEST_DOC);
133
			Session session = getTestSession();
134
			Identifier pid = new Identifier();
135
			pid.setValue(docid);
136
			SystemMetadata sysmeta = this.createSystemMetadata(pid, session.getSubject(), IOUtils.toInputStream(documentContents, "UTF-8"));
137
			ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
138
			formatId.setValue("http://datadryad.org/profile/v3.1");
139
			sysmeta.setFormatId(formatId);
140
			sysmeta.setSize(BigInteger.valueOf(IOUtils.toByteArray(documentContents).length));
141
			MNodeService.getInstance(request).create(session, pid, IOUtils.toInputStream(documentContents, "UTF-8"), sysmeta);
142
			InputStream results = MNodeService.getInstance(request).get(session, pid);
143
			String resultString = IOUtils.toString(results);
144
			assertEquals(documentContents, resultString);
145
			m.logout();
146
		} catch (Exception e) {
147
			e.printStackTrace();
148
			fail(e.getMessage());
149
		}
150
    	
151
    }
152

    
153
    
154
}
(5-5/25)