Project

General

Profile

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

    
21
import java.io.ByteArrayInputStream;
22
import java.io.FileInputStream;
23
import java.io.InputStream;
24

    
25
import junit.framework.Test;
26
import junit.framework.TestSuite;
27

    
28
import org.dataone.service.types.v1.Identifier;
29
import org.dataone.service.types.v1.ObjectFormatIdentifier;
30
import org.dataone.service.types.v1.Session;
31
import org.dataone.service.types.v1.SystemMetadata;
32

    
33
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
34
import edu.ucsb.nceas.metacat.dataone.MNodeService;
35

    
36
public class DatapackageSummarizerTest extends D1NodeServiceTest {
37

    
38
	
39
    private static final String ANNOTATION_TEST_DOC = "test/eml-sample-annotation.xml";
40

    
41
	/**
42
	 * constructor for the test
43
	 */
44
	public DatapackageSummarizerTest(String name) {
45
		super(name);
46
	}
47

    
48
	/**
49
	 * Establish a testing framework by initializing appropriate objects
50
	 */
51
	public void setUp() throws Exception {
52
		super.setUp();
53
	}
54

    
55
	/**
56
	 * Release any objects after tests are complete
57
	 */
58
	public void tearDown() {
59
	}
60

    
61
	/**
62
	 * Create a suite of tests to be run together
63
	 */
64
	public static Test suite() {
65
		TestSuite suite = new TestSuite();
66
		suite.addTest(new DatapackageSummarizerTest("testGenerateAnnotation"));
67
//		suite.addTest(new DatapackageSummarizerTest("testGenerateRandomAnnotation"));
68
		return suite;
69
	}
70
	
71
	/**
72
	 * Generate a single annotation based exclusively on the metadata
73
	 * @throws Exception
74
	 */
75
	public void testGenerateAnnotation() throws Exception {
76
		this.testGenerateAnnotation_base(false);
77
	}
78
	
79
	/**
80
	 * Generate a bunch of random annotations
81
	 * @throws Exception
82
	 */
83
	public void testGenerateRandomAnnotation() throws Exception {
84
		for (int i = 0; i < 5; i++) {
85
			this.testGenerateAnnotation_base(true);
86
		}
87
	}
88

    
89
	private void testGenerateAnnotation_base(boolean randomize) throws Exception {
90
		Identifier metadataPid = new Identifier();
91
		metadataPid.setValue("testAnnotation.eml." + System.currentTimeMillis());
92
		Session session = getTestSession();
93
		try {
94
			InputStream object = new ByteArrayInputStream(this.getTestDocFromFile(ANNOTATION_TEST_DOC).getBytes("UTF-8"));
95
			SystemMetadata sysmeta = createSystemMetadata(metadataPid, session.getSubject(), object);
96
			ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
97
			formatId.setValue("eml://ecoinformatics.org/eml-2.0.0");
98
			sysmeta.setFormatId(formatId);
99
			Identifier pid = MNodeService.getInstance(request).create(session, metadataPid, object, sysmeta);
100
			assertEquals(metadataPid.getValue(), pid.getValue());
101
		} catch (Exception e) {
102
			e.printStackTrace();
103
			fail("Could not add metadata test file: " + e.getMessage());
104
		}
105

    
106
		// generate the annotation for the metadata
107
		DatapackageSummarizer ds = new DatapackageSummarizer();
108
		ds.randomize = randomize;
109
		String rdfContent = ds.generateAnnotation(metadataPid);
110
		
111
		// save the annotation
112
		Identifier annotationPid = new Identifier();
113
		annotationPid.setValue("http://annotation/" + metadataPid.getValue());
114
		try {
115
			InputStream object = new ByteArrayInputStream(rdfContent.getBytes("UTF-8"));
116
			SystemMetadata sysmeta = createSystemMetadata(annotationPid, session.getSubject(), object);
117
			ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
118
			formatId.setValue("http://www.w3.org/TR/rdf-syntax-grammar");
119
			sysmeta.setFormatId(formatId);
120
			Identifier pid = MNodeService.getInstance(request).create(session, annotationPid, object, sysmeta);
121
			assertEquals(annotationPid.getValue(), pid.getValue());
122
		} catch (Exception e) {
123
			e.printStackTrace();
124
			fail("Could not add annotation test file: " + e.getMessage());
125
		}
126
		
127
		// check that it was parsed?
128
	}
129

    
130
}
    (1-1/1)