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-10-20 14:03:19 -0700 (Thu, 20 Oct 2011) $'
9
 * '$Revision: 6542 $'
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.hazelcast;
27

    
28
import java.io.ByteArrayInputStream;
29
import java.io.InputStream;
30

    
31
import javax.servlet.http.HttpServletRequest;
32

    
33
import junit.framework.Test;
34
import junit.framework.TestSuite;
35

    
36
import org.apache.wicket.protocol.http.MockHttpServletRequest;
37
import org.dataone.service.types.v1.Identifier;
38
import org.dataone.service.types.v1.Session;
39
import org.dataone.service.types.v1.SystemMetadata;
40

    
41
import com.hazelcast.core.Hazelcast;
42
import com.hazelcast.core.IMap;
43

    
44
import edu.ucsb.nceas.MCTestCase;
45
import edu.ucsb.nceas.metacat.dataone.CNodeService;
46
import edu.ucsb.nceas.metacat.dataone.CNodeServiceTest;
47
import edu.ucsb.nceas.metacat.properties.PropertyService;
48

    
49
/**
50
 * A JUnit superclass for testing the Hazelcast interactions
51
 */
52
public class HazelcastServiceTest extends MCTestCase {   
53
    
54
	static {
55
	
56
		try {
57
			// initialize the configuration
58
			HazelcastService.getInstance();
59
			
60
		} catch (Exception e) {
61
			e.printStackTrace();
62
			fail();
63
		}
64
			
65
	}
66

    
67
	private HttpServletRequest request;
68

    
69
    /**
70
    * constructor for the test
71
    */
72
    public HazelcastServiceTest(String name) {
73
        super(name);
74

    
75
        // set up the fake request (for logging)
76
        request = new MockHttpServletRequest(null, null, null);
77
    }
78
    
79
    /**
80
	 * Create a suite of tests to be run together
81
	 */
82
	public static Test suite() 
83
	{
84
		TestSuite suite = new TestSuite();
85
		suite.addTest(new HazelcastServiceTest("initialize"));
86
		suite.addTest(new HazelcastServiceTest("retrieveSystemMetadataFromMap"));
87
		suite.addTest(new HazelcastServiceTest("storeSystemMetadataToMap"));
88

    
89
		return suite;
90
	}
91
  
92
	
93
	public void retrieveSystemMetadataFromMap() {
94
		try {
95
			Identifier pid = null;
96
			// create the systemMetadata the normal way
97
			CNodeServiceTest cnst = new CNodeServiceTest("testRegisterSystemMetadata");
98
			pid = cnst.testRegisterSystemMetadata();
99
			assertNotNull(pid);
100
			// look it up from the "shared" map
101
			IMap<Object, Object> systemMetadataMap = Hazelcast.getMap(PropertyService.getProperty("dataone.hazelcast.storageCluster.systemMetadataMap"));
102
			SystemMetadata sm = (SystemMetadata) systemMetadataMap.get(pid);
103
			assertNotNull(sm);
104
		} catch (Exception e) {
105
			e.printStackTrace();
106
			fail();
107
		}
108
	}
109
	
110
	public void storeSystemMetadataToMap() {
111
		try {
112
			// create the systemMetadata and save to map
113
			CNodeServiceTest cnst = new CNodeServiceTest("testGetSystemMetadata");
114
			Session session = cnst.getTestSession();
115
			Identifier guid = new Identifier();
116
			guid.setValue("testCreate." + System.currentTimeMillis());
117
			InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
118
			SystemMetadata sysmeta = cnst.createSystemMetadata(guid, session.getSubject(), object);
119
			assertNotNull(sysmeta);
120
			// put it in the "shared" map
121
			System.out.println("Saving System Metadata to in-memory shared map: " + guid.getValue());
122
			IMap<Identifier, SystemMetadata> systemMetadataMap = Hazelcast.getMap(PropertyService.getProperty("dataone.hazelcast.storageCluster.systemMetadataMap"));
123
			systemMetadataMap.put(guid, sysmeta);
124
			
125
			// get it from the store
126
			SystemMetadata sysmetaFromStore = CNodeService.getInstance(request).getSystemMetadata(session, guid);
127
			assertNotNull(sysmetaFromStore);
128

    
129
		} catch (Exception e) {
130
			e.printStackTrace();
131
			fail();
132
		}
133
	}
134
	
135
    /**
136
	 * Establish a testing framework by initializing appropriate objects
137
	 */
138
	public void setUp() throws Exception {
139
		super.setUp();
140
	}
141

    
142
	/**
143
	 * Release any objects after tests are complete
144
	 */
145
	public void tearDown() {}
146
	
147
	
148
	/**
149
	 * Run an initial test that always passes to check that the test harness is
150
	 * working.
151
	 */
152
	public void initialize() 
153
	{
154
	    printTestHeader("initialize");
155
		assertTrue(1 == 1);
156
	}
157

    
158

    
159
	/**
160
	 * print a header to start each test
161
	 */
162
	protected void printTestHeader(String testName)
163
	{
164
	    System.out.println();
165
	    System.out.println("*************** " + testName + " ***************");
166
	}
167
 
168
}
(1-1/4)