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: 2013-04-24 19:34:36 -0700 (Wed, 24 Apr 2013) $'
9
 * '$Revision: 7622 $'
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.mock.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.HazelcastInstance;
43
import com.hazelcast.core.IMap;
44

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

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

    
69
	private HttpServletRequest request;
70

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

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

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

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

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

    
160

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