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: leinfelder $'
8
 *     '$Date: 2011-07-08 14:08:47 -0700 (Fri, 08 Jul 2011) $'
9
 * '$Revision: 6324 $'
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.dataone;
27

    
28
import java.io.ByteArrayInputStream;
29
import java.io.ByteArrayOutputStream;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.io.InputStreamReader;
33
import java.io.Reader;
34
import java.security.MessageDigest;
35
import java.security.NoSuchAlgorithmException;
36
import java.util.Date;
37

    
38
import org.apache.commons.io.IOUtils;
39
import org.dataone.client.ObjectFormatCache;
40
import org.dataone.service.Constants;
41
import org.dataone.service.impl.ObjectFormatServiceImpl;
42
import org.dataone.service.types.AccessPolicy;
43
import org.dataone.service.types.AccessRule;
44
import org.dataone.service.types.Checksum;
45
import org.dataone.service.types.ChecksumAlgorithm;
46
import org.dataone.service.types.Identifier;
47
import org.dataone.service.types.NodeReference;
48
import org.dataone.service.types.ObjectFormatList;
49
import org.dataone.service.types.Permission;
50
import org.dataone.service.types.Session;
51
import org.dataone.service.types.Subject;
52
import org.dataone.service.types.SystemMetadata;
53
import org.dataone.service.types.util.ServiceTypeUtil;
54

    
55
import edu.ucsb.nceas.MCTestCase;
56
import edu.ucsb.nceas.metacat.client.Metacat;
57
import edu.ucsb.nceas.metacat.client.MetacatFactory;
58

    
59
/**
60
 * A JUnit superclass for testing the dataone Node implementations
61
 */
62
public class D1NodeServiceTest extends MCTestCase {   
63
    
64
    /**
65
    * constructor for the test
66
    */
67
    public D1NodeServiceTest(String name) {
68
        super(name);
69
    }
70
  
71
    /**
72
	 * Establish a testing framework by initializing appropriate objects
73
	 */
74
	public void setUp() throws Exception {
75
		super.setUp();
76
	}
77

    
78
	/**
79
	 * Release any objects after tests are complete
80
	 */
81
	public void tearDown() {}
82
	
83
	/**
84
	 * constructs a "fake" session with a test subject
85
	 * @return
86
	 */
87
	protected Session getTestSession() {
88
		Session session = new Session();
89
        Subject subject = new Subject();
90
        subject.setValue("cn=test,dc=dataone,dc=org");
91
        session.setSubject(subject);
92
        return session;
93
	}
94
	
95
	
96
	
97
	
98
	/**
99
	 * Run an initial test that always passes to check that the test harness is
100
	 * working.
101
	 */
102
	public void initialize() 
103
	{
104
	    printTestHeader("initialize");
105
		assertTrue(1 == 1);
106
	}
107
		
108
    private static String checksum(InputStream is) throws NoSuchAlgorithmException, IOException {
109
        byte[] buffer = new byte[1024];
110
        MessageDigest complete = MessageDigest.getInstance("MD5");
111
        int numRead;
112

    
113
        do {
114
            numRead = is.read(buffer);
115
            if (numRead > 0) {
116
                complete.update(buffer, 0, numRead);
117
            }
118
        } while (numRead != -1);
119

    
120
        // rest to beginning -- not all streams support this
121
        is.reset();
122
        
123
        String csStr = getHex(complete.digest());
124
        return csStr;
125
    }
126
	
127
	/**
128
     * convert a byte array to a hex string
129
     */
130
    private static String getHex(byte[] raw) {
131
        final String HEXES = "0123456789ABCDEF";
132
        if (raw == null) {
133
            return null;
134
        }
135
        final StringBuilder hex = new StringBuilder(2 * raw.length);
136
        for (final byte b : raw) {
137
            hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
138
        }
139
        return hex.toString();
140
    }
141
	
142
	/**
143
	 * create system metadata with a specified id
144
	 */
145
	protected SystemMetadata createSystemMetadata(Identifier id, Subject owner, InputStream object)
146
	  throws Exception
147
	{
148
	    SystemMetadata sm = new SystemMetadata();
149
        // set the id
150
        sm.setIdentifier(id);
151
        sm.setObjectFormat(ObjectFormatCache.getInstance().getFormat("application/octet-stream"));
152
        // create the checksum
153
        String checksumS = checksum(object);
154
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
155
        Checksum checksum = new Checksum();
156
        checksum.setValue(checksumS);
157
        checksum.setAlgorithm(ca);
158
        sm.setChecksum(checksum);
159
        // set the size
160
        sm.setSize(0);
161
        sm.setSubmitter(owner);
162
        sm.setRightsHolder(owner);
163
        sm.setDateUploaded(new Date());
164
        sm.setDateSysMetadataModified(new Date());
165
        NodeReference nr = new NodeReference();
166
        nr.setValue("metacat");
167
        sm.setOriginMemberNode(nr);
168
        sm.setAuthoritativeMemberNode(nr);
169
		// set the access to public read
170
        AccessPolicy accessPolicy = new AccessPolicy();
171
        AccessRule allow = new AccessRule();
172
        allow.addPermission(Permission.READ);
173
        Subject publicSubject = new Subject();
174
        publicSubject.setValue(Constants.PUBLIC_SUBJECT);
175
		allow.addSubject(publicSubject);
176
		accessPolicy.addAllow(allow);
177
        sm.setAccessPolicy(accessPolicy);
178
        
179
        return sm;
180
	}
181
	
182
	/**
183
	 * For fresh Metacat installations without the Object Format List
184
	 * we insert the default version from d1_common.jar
185
	 */
186
	protected void setUpFormats() {
187
		try {
188
			Metacat m = MetacatFactory.createMetacatConnection(metacatUrl);
189
			m.login(username, password);
190
			// check if it exists already
191
			InputStream is = null;
192
			try {
193
				is = m.read(ObjectFormatService.OBJECT_FORMAT_DOCID);
194
			} catch (Exception e) {
195
				// probably missing the doc
196
			}
197
			if (is == null) {
198
				ObjectFormatList formats = ObjectFormatServiceImpl.getInstance().listFormats();
199
				ByteArrayOutputStream out = new ByteArrayOutputStream();
200
				ServiceTypeUtil.serializeServiceType(ObjectFormatList.class, formats, out);
201
				Reader xmlDocument = new InputStreamReader(new ByteArrayInputStream(out.toByteArray()));
202
				m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null);
203
			}
204
			m.logout();
205
		} catch (Exception e) {
206
			// any number of things could go wrong
207
			e.printStackTrace();
208
		}
209
	}
210

    
211
	/**
212
	 * print a header to start each test
213
	 */
214
	protected void printTestHeader(String testName)
215
	{
216
	    System.out.println();
217
	    System.out.println("*************** " + testName + " ***************");
218
	}
219
 
220
}
(3-3/6)