Project

General

Profile

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.io.ByteArrayInputStream;
29
import java.io.ByteArrayOutputStream;
30
import java.io.File;
31
import java.io.FileInputStream;
32
import java.io.IOException;
33
import java.io.InputStream;
34
import java.io.OutputStream;
35
import java.util.Date;
36

    
37
import junit.framework.Test;
38
import junit.framework.TestSuite;
39

    
40
import org.apache.commons.io.IOUtils;
41
import org.dataone.service.exceptions.BaseException;
42
import org.dataone.service.exceptions.InvalidSystemMetadata;
43
import org.dataone.service.exceptions.ServiceFailure;
44
import org.dataone.service.types.v1.SystemMetadata;
45
import org.jibx.runtime.BindingDirectory;
46
import org.jibx.runtime.IBindingFactory;
47
import org.jibx.runtime.IMarshallingContext;
48

    
49
import edu.ucsb.nceas.MCTestCase;
50

    
51
/**
52
 * @author berkley
53
 * class to test ResourceHandler
54
 */
55
public class ResourceHandlerTest extends MCTestCase
56
{
57
    public ResourceHandlerTest(String name)
58
    {
59
        super(name);
60
    }
61

    
62
    /**
63
     * Create a suite of tests to be run together
64
     */
65
    public static Test suite() 
66
    {
67
        TestSuite suite = new TestSuite();
68
        
69
        //suite.addTest(new ResourceHandlerTest("testFindBoundary"));        
70
        //suite.addTest(new ResourceHandlerTest("testProcessMMP"));
71
        suite.addTest(new ResourceHandlerTest("testReplicate"));
72
        return suite;
73
    }
74
    
75
    public void testReplicate()
76
    {
77
        
78
    }
79
    
80
    public void testFindBoundary()
81
    {
82
        try
83
        {
84
            File f = new File("test/testMimeInput.txt");
85
            FileInputStream fis = new FileInputStream(f);
86
            String[] s = ResourceHandler.findBoundaryString(fis);
87
            assertEquals(s[0], "--1288304583346");
88
            //System.out.println("r: " + s[1].trim());
89
            assertTrue(s[1].trim().startsWith(s[0]));
90
        }
91
        catch(Exception e)
92
        {
93
            fail("unexpected error in testFindBoundary: " + e.getMessage());
94
        }
95
    }
96
    
97
    public void testProcessMMP()
98
    {
99
        try
100
        {
101
            runTestProccessMMP("test/testMimeInput.txt", "test/testMimeInput-object.txt");
102
            runTestProccessMMP("test/testMimeInput2.txt", "test/testMimeInput2-object.txt");
103
            runTestProccessMMP("test/testMimeInput3.txt", "test/testMimeInput3-object.txt");
104
        }
105
        catch(Exception e)
106
        {
107
            e.printStackTrace();
108
            fail("Unexpected error in testProcessMMP: " + e.getMessage());
109
        }
110
        
111
    }
112
    
113
    /**
114
     * create a mime multipart message from object and sysmeta and write it to out
115
     */
116
    private void createMimeMultipart(OutputStream out, InputStream object,
117
            SystemMetadata sysmeta) throws IOException, BaseException
118
    {
119
        if (sysmeta == null) {
120
            throw new InvalidSystemMetadata("1000",
121
                    "System metadata was null.  Can't create multipart form.");
122
        }
123
        Date d = new Date();
124
        String boundary = d.getTime() + "";
125

    
126
        String mime = "MIME-Version:1.0\n";
127
        mime += "Content-type:multipart/mixed; boundary=\"" + boundary + "\"\n";
128
        boundary = "--" + boundary + "\n";
129
        mime += boundary;
130
        mime += "Content-Disposition: attachment; filename=systemmetadata\n\n";
131
        out.write(mime.getBytes());
132
        out.flush();
133
        
134
        //write the sys meta
135
        try
136
        {
137
            ByteArrayInputStream bais = serializeSystemMetadata(sysmeta);
138
            IOUtils.copy(bais, out);
139
        }
140
        catch(Exception e)
141
        {
142
            throw new ServiceFailure("1000",
143
                    "Could not serialize the system metadata to multipart: "
144
                            + e.getMessage());
145
        }
146
        
147
        out.write("\n".getBytes()); 
148

    
149
        if (object != null) 
150
        {    
151
            out.write(boundary.getBytes());
152
            out.write("Content-Disposition: attachment; filename=object\n\n".getBytes());
153
            try {
154
                mime += IOUtils.copy(object, out);
155
            } 
156
            catch (IOException ioe) 
157
            {
158
                throw new ServiceFailure("1000",
159
                        "Error serializing object to multipart: "
160
                                + ioe.getMessage());
161
            }
162
            out.write("\n".getBytes());
163
        }
164

    
165
        out.write((boundary + "--").getBytes());        
166
    }
167
    
168
    protected ByteArrayInputStream serializeSystemMetadata(SystemMetadata sysmeta)
169
        throws Exception 
170
    {
171

    
172
        ByteArrayOutputStream sysmetaOut = new ByteArrayOutputStream();
173
        serializeServiceType(SystemMetadata.class, sysmeta, sysmetaOut);
174
        ByteArrayInputStream sysmetaStream = new ByteArrayInputStream(
175
                sysmetaOut.toByteArray());
176
        return sysmetaStream;
177
    }
178
    
179
    protected void serializeServiceType(Class type, Object object,
180
            OutputStream out) throws Exception {
181
        IBindingFactory bfact = BindingDirectory.getFactory(type);
182
        IMarshallingContext mctx = bfact.createMarshallingContext();
183
        mctx.marshalDocument(object, "UTF-8", null, out);
184
    }
185
    
186
    private void runTestProccessMMP(String filename, String answerFilename)
187
        throws Exception
188
    {
189
        /*PropertyService propServ = PropertyService.getInstance("/Users/berkley/tools/tomcat/webapps/knb/WEB-INF");
190
        System.out.println("*****************************************");
191
        System.out.println("running process test on file " + filename);
192
        File f = new File(filename);
193
        File fObject = new File(answerFilename);
194
        FileInputStream fobjectis = new FileInputStream(fObject);
195
        FileInputStream fis = new FileInputStream(f);
196
        ResourceHandler rh = new ResourceHandler(null, null, null);
197
        Hashtable<String, File> h = rh.processMMP(fis);
198
        FileInputStream smFis = new FileInputStream(h.get("sysmeta"));
199
        FileInputStream objFis = new FileInputStream(h.get("object"));
200
        String sm = IOUtils.toString(smFis);
201
        String obj = IOUtils.toString(objFis);
202
        //System.out.println("sm: " + sm);
203
        //System.out.println("obj: " + obj);
204
        String fObjectStr = IOUtils.toString(fobjectis);
205
        assertTrue(fObjectStr.trim().equals(obj.trim()));
206
        assertTrue(sm.indexOf("<d1:systemMetadata") != -1);
207
        assertTrue(sm.indexOf("</d1:systemMetadata>") != -1);*/
208
    }
209
}
    (1-1/1)