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-15 12:57:13 -0700 (Thu, 15 Sep 2011) $'
9
 * '$Revision: 6437 $'
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.FileNotFoundException;
30
import java.io.InputStream;
31

    
32
import org.dataone.configuration.Settings;
33
import org.dataone.service.types.v1.Identifier;
34
import org.dataone.service.types.v1.Session;
35
import org.dataone.service.types.v1.SystemMetadata;
36

    
37
import com.hazelcast.config.Config;
38
import com.hazelcast.config.FileSystemXmlConfig;
39
import com.hazelcast.core.Hazelcast;
40
import com.hazelcast.core.IMap;
41

    
42
import junit.framework.Test;
43
import junit.framework.TestSuite;
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
			// initialize the entry listeners
61
			CNodeService.getInstance();
62
		} catch (Exception e) {
63
			e.printStackTrace();
64
			fail();
65
		}
66
			
67
	}
68
		
69
    /**
70
    * constructor for the test
71
    */
72
    public HazelcastServiceTest(String name) {
73
        super(name);
74

    
75
    }
76
    
77
    /**
78
	 * Create a suite of tests to be run together
79
	 */
80
	public static Test suite() 
81
	{
82
		TestSuite suite = new TestSuite();
83
		suite.addTest(new HazelcastServiceTest("initialize"));
84
		suite.addTest(new HazelcastServiceTest("retrieveSystemMetadataFromMap"));
85
		suite.addTest(new HazelcastServiceTest("storeSystemMetadataToMap"));
86

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

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

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

    
156

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