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-09-07 17:16:19 -0700 (Wed, 07 Sep 2011) $'
9
 * '$Revision: 6397 $'
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.math.BigInteger;
35
import java.security.MessageDigest;
36
import java.security.NoSuchAlgorithmException;
37
import java.util.Date;
38

    
39
import org.dataone.client.ObjectFormatCache;
40
import org.dataone.service.util.Constants;
41
import org.dataone.service.util.TypeMarshaller;
42
import org.dataone.service.types.v1.util.ObjectFormatServiceImpl;
43
import org.dataone.service.types.v1.AccessPolicy;
44
import org.dataone.service.types.v1.AccessRule;
45
import org.dataone.service.types.v1.Checksum;
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

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

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

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

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

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

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