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: 2015-12-16 16:37:47 -0800 (Wed, 16 Dec 2015) $'
|
9
|
* '$Revision: 9457 $'
|
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
|
cnst.setUp();
|
101
|
pid = cnst.testRegisterSystemMetadata();
|
102
|
assertNotNull(pid);
|
103
|
// look it up from the "shared" map
|
104
|
IMap<Object, Object> systemMetadataMap = hzMember.getMap(PropertyService.getProperty("dataone.hazelcast.storageCluster.systemMetadataMap"));
|
105
|
SystemMetadata sm = (SystemMetadata) systemMetadataMap.get(pid);
|
106
|
assertNotNull(sm);
|
107
|
} catch (Exception e) {
|
108
|
e.printStackTrace();
|
109
|
fail();
|
110
|
}
|
111
|
}
|
112
|
|
113
|
public void storeSystemMetadataToMap() {
|
114
|
try {
|
115
|
// create the systemMetadata and save to map
|
116
|
CNodeServiceTest cnst = new CNodeServiceTest("testGetSystemMetadata");
|
117
|
cnst.setUp();
|
118
|
Session session = cnst.getTestSession();
|
119
|
Identifier guid = new Identifier();
|
120
|
guid.setValue("testCreate." + System.currentTimeMillis());
|
121
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
122
|
SystemMetadata sysmeta = cnst.createSystemMetadata(guid, session.getSubject(), object);
|
123
|
assertNotNull(sysmeta);
|
124
|
// put it in the "shared" map
|
125
|
System.out.println("Saving System Metadata to in-memory shared map: " + guid.getValue());
|
126
|
IMap<Identifier, SystemMetadata> systemMetadataMap = hzMember.getMap(PropertyService.getProperty("dataone.hazelcast.storageCluster.systemMetadataMap"));
|
127
|
systemMetadataMap.put(guid, sysmeta);
|
128
|
|
129
|
// get it from the store
|
130
|
SystemMetadata sysmetaFromStore = CNodeService.getInstance(request).getSystemMetadata(session, guid);
|
131
|
assertNotNull(sysmetaFromStore);
|
132
|
|
133
|
} catch (Exception e) {
|
134
|
e.printStackTrace();
|
135
|
fail();
|
136
|
}
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Establish a testing framework by initializing appropriate objects
|
141
|
*/
|
142
|
public void setUp() throws Exception {
|
143
|
super.setUp();
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* Release any objects after tests are complete
|
148
|
*/
|
149
|
public void tearDown() {}
|
150
|
|
151
|
|
152
|
/**
|
153
|
* Run an initial test that always passes to check that the test harness is
|
154
|
* working.
|
155
|
*/
|
156
|
public void initialize()
|
157
|
{
|
158
|
printTestHeader("initialize");
|
159
|
assertTrue(1 == 1);
|
160
|
}
|
161
|
|
162
|
|
163
|
/**
|
164
|
* print a header to start each test
|
165
|
*/
|
166
|
protected void printTestHeader(String testName)
|
167
|
{
|
168
|
System.out.println();
|
169
|
System.out.println("*************** " + testName + " ***************");
|
170
|
}
|
171
|
|
172
|
}
|