1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2010 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: berkley $'
|
8
|
* '$Date: 2010-05-03 14:26:08 -0700 (Fri, 14 Aug 2009) $'
|
9
|
* '$Revision: 5027 $'
|
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.metacat.restservice;
|
27
|
|
28
|
import java.util.*;
|
29
|
import java.io.*;
|
30
|
|
31
|
import edu.ucsb.nceas.MCTestCase;
|
32
|
import edu.ucsb.nceas.metacat.dataone.CrudServiceTest;
|
33
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
34
|
|
35
|
import junit.framework.Test;
|
36
|
import junit.framework.TestSuite;
|
37
|
|
38
|
import org.apache.commons.io.IOUtils;
|
39
|
import org.dataone.service.exceptions.BaseException;
|
40
|
import org.dataone.service.exceptions.InvalidSystemMetadata;
|
41
|
import org.dataone.service.exceptions.ServiceFailure;
|
42
|
import org.dataone.service.types.SystemMetadata;
|
43
|
|
44
|
import org.jibx.runtime.BindingDirectory;
|
45
|
import org.jibx.runtime.IBindingFactory;
|
46
|
import org.jibx.runtime.IMarshallingContext;
|
47
|
import org.jibx.runtime.IUnmarshallingContext;
|
48
|
import org.jibx.runtime.JiBXException;
|
49
|
|
50
|
/**
|
51
|
* @author berkley
|
52
|
* class to test ResourceHandler
|
53
|
*/
|
54
|
public class ResourceHandlerTest extends MCTestCase
|
55
|
{
|
56
|
public ResourceHandlerTest(String name)
|
57
|
{
|
58
|
super(name);
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Create a suite of tests to be run together
|
63
|
*/
|
64
|
public static Test suite()
|
65
|
{
|
66
|
TestSuite suite = new TestSuite();
|
67
|
|
68
|
//suite.addTest(new ResourceHandlerTest("testFindBoundary"));
|
69
|
//suite.addTest(new ResourceHandlerTest("testProcessMMP"));
|
70
|
suite.addTest(new ResourceHandlerTest("testReplicate"));
|
71
|
return suite;
|
72
|
}
|
73
|
|
74
|
public void testReplicate()
|
75
|
{
|
76
|
|
77
|
}
|
78
|
|
79
|
public void testFindBoundary()
|
80
|
{
|
81
|
try
|
82
|
{
|
83
|
File f = new File("test/testMimeInput.txt");
|
84
|
FileInputStream fis = new FileInputStream(f);
|
85
|
String[] s = ResourceHandler.findBoundaryString(fis);
|
86
|
assertEquals(s[0], "--1288304583346");
|
87
|
//System.out.println("r: " + s[1].trim());
|
88
|
assertTrue(s[1].trim().startsWith(s[0]));
|
89
|
}
|
90
|
catch(Exception e)
|
91
|
{
|
92
|
fail("unexpected error in testFindBoundary: " + e.getMessage());
|
93
|
}
|
94
|
}
|
95
|
|
96
|
public void testProcessMMP()
|
97
|
{
|
98
|
try
|
99
|
{
|
100
|
runTestProccessMMP("test/testMimeInput.txt", "test/testMimeInput-object.txt");
|
101
|
runTestProccessMMP("test/testMimeInput2.txt", "test/testMimeInput2-object.txt");
|
102
|
runTestProccessMMP("test/testMimeInput3.txt", "test/testMimeInput3-object.txt");
|
103
|
}
|
104
|
catch(Exception e)
|
105
|
{
|
106
|
e.printStackTrace();
|
107
|
fail("Unexpected error in testProcessMMP: " + e.getMessage());
|
108
|
}
|
109
|
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* create a mime multipart message from object and sysmeta and write it to out
|
114
|
*/
|
115
|
private void createMimeMultipart(OutputStream out, InputStream object,
|
116
|
SystemMetadata sysmeta) throws IOException, BaseException
|
117
|
{
|
118
|
if (sysmeta == null) {
|
119
|
throw new InvalidSystemMetadata("1000",
|
120
|
"System metadata was null. Can't create multipart form.");
|
121
|
}
|
122
|
Date d = new Date();
|
123
|
String boundary = d.getTime() + "";
|
124
|
|
125
|
String mime = "MIME-Version:1.0\n";
|
126
|
mime += "Content-type:multipart/mixed; boundary=\"" + boundary + "\"\n";
|
127
|
boundary = "--" + boundary + "\n";
|
128
|
mime += boundary;
|
129
|
mime += "Content-Disposition: attachment; filename=systemmetadata\n\n";
|
130
|
out.write(mime.getBytes());
|
131
|
out.flush();
|
132
|
|
133
|
//write the sys meta
|
134
|
try
|
135
|
{
|
136
|
ByteArrayInputStream bais = serializeSystemMetadata(sysmeta);
|
137
|
IOUtils.copy(bais, out);
|
138
|
}
|
139
|
catch(Exception e)
|
140
|
{
|
141
|
throw new ServiceFailure("1000",
|
142
|
"Could not serialize the system metadata to multipart: "
|
143
|
+ e.getMessage());
|
144
|
}
|
145
|
|
146
|
out.write("\n".getBytes());
|
147
|
|
148
|
if (object != null)
|
149
|
{
|
150
|
out.write(boundary.getBytes());
|
151
|
out.write("Content-Disposition: attachment; filename=object\n\n".getBytes());
|
152
|
try {
|
153
|
mime += IOUtils.copy(object, out);
|
154
|
}
|
155
|
catch (IOException ioe)
|
156
|
{
|
157
|
throw new ServiceFailure("1000",
|
158
|
"Error serializing object to multipart: "
|
159
|
+ ioe.getMessage());
|
160
|
}
|
161
|
out.write("\n".getBytes());
|
162
|
}
|
163
|
|
164
|
out.write((boundary + "--").getBytes());
|
165
|
}
|
166
|
|
167
|
protected ByteArrayInputStream serializeSystemMetadata(SystemMetadata sysmeta)
|
168
|
throws Exception
|
169
|
{
|
170
|
|
171
|
ByteArrayOutputStream sysmetaOut = new ByteArrayOutputStream();
|
172
|
serializeServiceType(SystemMetadata.class, sysmeta, sysmetaOut);
|
173
|
ByteArrayInputStream sysmetaStream = new ByteArrayInputStream(
|
174
|
sysmetaOut.toByteArray());
|
175
|
return sysmetaStream;
|
176
|
}
|
177
|
|
178
|
protected void serializeServiceType(Class type, Object object,
|
179
|
OutputStream out) throws Exception {
|
180
|
IBindingFactory bfact = BindingDirectory.getFactory(type);
|
181
|
IMarshallingContext mctx = bfact.createMarshallingContext();
|
182
|
mctx.marshalDocument(object, "UTF-8", null, out);
|
183
|
}
|
184
|
|
185
|
private void runTestProccessMMP(String filename, String answerFilename)
|
186
|
throws Exception
|
187
|
{
|
188
|
/*PropertyService propServ = PropertyService.getInstance("/Users/berkley/tools/tomcat/webapps/knb/WEB-INF");
|
189
|
System.out.println("*****************************************");
|
190
|
System.out.println("running process test on file " + filename);
|
191
|
File f = new File(filename);
|
192
|
File fObject = new File(answerFilename);
|
193
|
FileInputStream fobjectis = new FileInputStream(fObject);
|
194
|
FileInputStream fis = new FileInputStream(f);
|
195
|
ResourceHandler rh = new ResourceHandler(null, null, null);
|
196
|
Hashtable<String, File> h = rh.processMMP(fis);
|
197
|
FileInputStream smFis = new FileInputStream(h.get("sysmeta"));
|
198
|
FileInputStream objFis = new FileInputStream(h.get("object"));
|
199
|
String sm = IOUtils.toString(smFis);
|
200
|
String obj = IOUtils.toString(objFis);
|
201
|
//System.out.println("sm: " + sm);
|
202
|
//System.out.println("obj: " + obj);
|
203
|
String fObjectStr = IOUtils.toString(fobjectis);
|
204
|
assertTrue(fObjectStr.trim().equals(obj.trim()));
|
205
|
assertTrue(sm.indexOf("<d1:systemMetadata") != -1);
|
206
|
assertTrue(sm.indexOf("</d1:systemMetadata>") != -1);*/
|
207
|
}
|
208
|
}
|