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-27 16:25:30 -0700 (Wed, 27 Jul 2011) $'
9
 * '$Revision: 6366 $'
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.types.v1.util.ObjectFormatServiceImpl;
42
import org.dataone.service.types.v1.AccessPolicy;
43
import org.dataone.service.types.v1.AccessRule;
44
import org.dataone.service.types.v1.Checksum;
45
import org.dataone.service.types.v1.ChecksumAlgorithm;
46
import org.dataone.service.types.v1.Identifier;
47
import org.dataone.service.types.v1.NodeReference;
48
import org.dataone.service.types.v1.ObjectFormatList;
49
import org.dataone.service.types.v1.Permission;
50
import org.dataone.service.types.v1.Session;
51
import org.dataone.service.types.v1.Subject;
52
import org.dataone.service.types.v1.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 = "test";
154
        if (object != null) {
155
        	checksumS = checksum(object);
156
        }
157
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
158
        Checksum checksum = new Checksum();
159
        checksum.setValue(checksumS);
160
        checksum.setAlgorithm(ca);
161
        sm.setChecksum(checksum);
162
        // set the size
163
        sm.setSize(0);
164
        sm.setSubmitter(owner);
165
        sm.setRightsHolder(owner);
166
        sm.setDateUploaded(new Date());
167
        sm.setDateSysMetadataModified(new Date());
168
        NodeReference nr = new NodeReference();
169
        nr.setValue("metacat");
170
        sm.setOriginMemberNode(nr);
171
        sm.setAuthoritativeMemberNode(nr);
172
		// set the access to public read
173
        AccessPolicy accessPolicy = new AccessPolicy();
174
        AccessRule allow = new AccessRule();
175
        allow.addPermission(Permission.READ);
176
        Subject publicSubject = new Subject();
177
        publicSubject.setValue(Constants.PUBLIC_SUBJECT);
178
		allow.addSubject(publicSubject);
179
		accessPolicy.addAllow(allow);
180
        sm.setAccessPolicy(accessPolicy);
181
        
182
        return sm;
183
	}
184
	
185
	/**
186
	 * For fresh Metacat installations without the Object Format List
187
	 * we insert the default version from d1_common.jar
188
	 */
189
	protected void setUpFormats() {
190
		try {
191
			Metacat m = MetacatFactory.createMetacatConnection(metacatUrl);
192
			m.login(username, password);
193
			// check if it exists already
194
			InputStream is = null;
195
			try {
196
				is = m.read(ObjectFormatService.OBJECT_FORMAT_DOCID);
197
			} catch (Exception e) {
198
				// probably missing the doc
199
			}
200
			if (is == null) {
201
				ObjectFormatList formats = ObjectFormatServiceImpl.getInstance().listFormats();
202
				ByteArrayOutputStream out = new ByteArrayOutputStream();
203
				ServiceTypeUtil.serializeServiceType(ObjectFormatList.class, formats, out);
204
				Reader xmlDocument = new InputStreamReader(new ByteArrayInputStream(out.toByteArray()));
205
				m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null);
206
			}
207
			m.logout();
208
		} catch (Exception e) {
209
			// any number of things could go wrong
210
			e.printStackTrace();
211
		}
212
	}
213

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